crypto: poly1305 - expose init/update/final library interface
[linux-block.git] / crypto / poly1305_generic.c
CommitLineData
f979e014
MW
1/*
2 * Poly1305 authenticator algorithm, RFC7539
3 *
4 * Copyright (C) 2015 Martin Willi
5 *
6 * Based on public domain code by Andrew Moon and Daniel J. Bernstein.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
14#include <crypto/algapi.h>
15#include <crypto/internal/hash.h>
48ea8c6e 16#include <crypto/internal/poly1305.h>
f979e014
MW
17#include <linux/crypto.h>
18#include <linux/kernel.h>
19#include <linux/module.h>
109e23bd 20#include <asm/unaligned.h>
f979e014 21
2546f811 22int crypto_poly1305_init(struct shash_desc *desc)
f979e014
MW
23{
24 struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
25
1b6fd3d5 26 poly1305_core_init(&dctx->h);
f979e014 27 dctx->buflen = 0;
ad8f5b88 28 dctx->rset = 0;
c2b7b20a 29 dctx->sset = false;
f979e014
MW
30
31 return 0;
32}
2546f811 33EXPORT_SYMBOL_GPL(crypto_poly1305_init);
f979e014 34
48ea8c6e
AB
35static void poly1305_blocks(struct poly1305_desc_ctx *dctx, const u8 *src,
36 unsigned int srclen)
1b6fd3d5
EB
37{
38 unsigned int datalen;
39
40 if (unlikely(!dctx->sset)) {
41 datalen = crypto_poly1305_setdesckey(dctx, src, srclen);
42 src += srclen - datalen;
43 srclen = datalen;
44 }
45
ad8f5b88 46 poly1305_core_blocks(&dctx->h, dctx->r, src,
48ea8c6e 47 srclen / POLY1305_BLOCK_SIZE, 1);
f979e014
MW
48}
49
2546f811 50int crypto_poly1305_update(struct shash_desc *desc,
f979e014
MW
51 const u8 *src, unsigned int srclen)
52{
53 struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
f979e014
MW
54 unsigned int bytes;
55
56 if (unlikely(dctx->buflen)) {
57 bytes = min(srclen, POLY1305_BLOCK_SIZE - dctx->buflen);
58 memcpy(dctx->buf + dctx->buflen, src, bytes);
59 src += bytes;
60 srclen -= bytes;
61 dctx->buflen += bytes;
62
63 if (dctx->buflen == POLY1305_BLOCK_SIZE) {
c2b7b20a 64 poly1305_blocks(dctx, dctx->buf,
48ea8c6e 65 POLY1305_BLOCK_SIZE);
f979e014
MW
66 dctx->buflen = 0;
67 }
68 }
69
70 if (likely(srclen >= POLY1305_BLOCK_SIZE)) {
48ea8c6e 71 poly1305_blocks(dctx, src, srclen);
1b6fd3d5
EB
72 src += srclen - (srclen % POLY1305_BLOCK_SIZE);
73 srclen %= POLY1305_BLOCK_SIZE;
f979e014
MW
74 }
75
76 if (unlikely(srclen)) {
77 dctx->buflen = srclen;
78 memcpy(dctx->buf, src, srclen);
79 }
80
81 return 0;
82}
2546f811 83EXPORT_SYMBOL_GPL(crypto_poly1305_update);
f979e014 84
1b6fd3d5
EB
85int crypto_poly1305_final(struct shash_desc *desc, u8 *dst)
86{
87 struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
1b6fd3d5
EB
88
89 if (unlikely(!dctx->sset))
90 return -ENOKEY;
91
a1d93064 92 poly1305_final_generic(dctx, dst);
f979e014
MW
93 return 0;
94}
2546f811 95EXPORT_SYMBOL_GPL(crypto_poly1305_final);
f979e014
MW
96
97static struct shash_alg poly1305_alg = {
98 .digestsize = POLY1305_DIGEST_SIZE,
2546f811
MW
99 .init = crypto_poly1305_init,
100 .update = crypto_poly1305_update,
101 .final = crypto_poly1305_final,
f979e014
MW
102 .descsize = sizeof(struct poly1305_desc_ctx),
103 .base = {
104 .cra_name = "poly1305",
105 .cra_driver_name = "poly1305-generic",
106 .cra_priority = 100,
f979e014 107 .cra_blocksize = POLY1305_BLOCK_SIZE,
f979e014
MW
108 .cra_module = THIS_MODULE,
109 },
110};
111
112static int __init poly1305_mod_init(void)
113{
114 return crypto_register_shash(&poly1305_alg);
115}
116
117static void __exit poly1305_mod_exit(void)
118{
119 crypto_unregister_shash(&poly1305_alg);
120}
121
c4741b23 122subsys_initcall(poly1305_mod_init);
f979e014
MW
123module_exit(poly1305_mod_exit);
124
125MODULE_LICENSE("GPL");
126MODULE_AUTHOR("Martin Willi <martin@strongswan.org>");
127MODULE_DESCRIPTION("Poly1305 authenticator");
128MODULE_ALIAS_CRYPTO("poly1305");
129MODULE_ALIAS_CRYPTO("poly1305-generic");