2 * Poly1305 authenticator algorithm, RFC7539
4 * Copyright (C) 2015 Martin Willi
6 * Based on public domain code by Andrew Moon and Daniel J. Bernstein.
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.
14 #include <crypto/algapi.h>
15 #include <crypto/internal/hash.h>
16 #include <crypto/internal/poly1305.h>
17 #include <linux/crypto.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <asm/unaligned.h>
22 int crypto_poly1305_init(struct shash_desc *desc)
24 struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
26 poly1305_core_init(&dctx->h);
33 EXPORT_SYMBOL_GPL(crypto_poly1305_init);
35 static void poly1305_blocks(struct poly1305_desc_ctx *dctx, const u8 *src,
40 if (unlikely(!dctx->sset)) {
41 datalen = crypto_poly1305_setdesckey(dctx, src, srclen);
42 src += srclen - datalen;
46 poly1305_core_blocks(&dctx->h, dctx->r, src,
47 srclen / POLY1305_BLOCK_SIZE, 1);
50 int crypto_poly1305_update(struct shash_desc *desc,
51 const u8 *src, unsigned int srclen)
53 struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
56 if (unlikely(dctx->buflen)) {
57 bytes = min(srclen, POLY1305_BLOCK_SIZE - dctx->buflen);
58 memcpy(dctx->buf + dctx->buflen, src, bytes);
61 dctx->buflen += bytes;
63 if (dctx->buflen == POLY1305_BLOCK_SIZE) {
64 poly1305_blocks(dctx, dctx->buf,
70 if (likely(srclen >= POLY1305_BLOCK_SIZE)) {
71 poly1305_blocks(dctx, src, srclen);
72 src += srclen - (srclen % POLY1305_BLOCK_SIZE);
73 srclen %= POLY1305_BLOCK_SIZE;
76 if (unlikely(srclen)) {
77 dctx->buflen = srclen;
78 memcpy(dctx->buf, src, srclen);
83 EXPORT_SYMBOL_GPL(crypto_poly1305_update);
85 int crypto_poly1305_final(struct shash_desc *desc, u8 *dst)
87 struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
89 if (unlikely(!dctx->sset))
92 poly1305_final_generic(dctx, dst);
95 EXPORT_SYMBOL_GPL(crypto_poly1305_final);
97 static struct shash_alg poly1305_alg = {
98 .digestsize = POLY1305_DIGEST_SIZE,
99 .init = crypto_poly1305_init,
100 .update = crypto_poly1305_update,
101 .final = crypto_poly1305_final,
102 .descsize = sizeof(struct poly1305_desc_ctx),
104 .cra_name = "poly1305",
105 .cra_driver_name = "poly1305-generic",
107 .cra_blocksize = POLY1305_BLOCK_SIZE,
108 .cra_module = THIS_MODULE,
112 static int __init poly1305_mod_init(void)
114 return crypto_register_shash(&poly1305_alg);
117 static void __exit poly1305_mod_exit(void)
119 crypto_unregister_shash(&poly1305_alg);
122 subsys_initcall(poly1305_mod_init);
123 module_exit(poly1305_mod_exit);
125 MODULE_LICENSE("GPL");
126 MODULE_AUTHOR("Martin Willi <martin@strongswan.org>");
127 MODULE_DESCRIPTION("Poly1305 authenticator");
128 MODULE_ALIAS_CRYPTO("poly1305");
129 MODULE_ALIAS_CRYPTO("poly1305-generic");