blob: afe9a9e576ddabab03593bc438af2ed3ac27aced [file] [log] [blame]
Martin Willif979e012015-06-01 13:43:58 +02001/*
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>
Ard Biesheuvel48ea8c62019-11-08 13:22:19 +010016#include <crypto/internal/poly1305.h>
Martin Willif979e012015-06-01 13:43:58 +020017#include <linux/crypto.h>
18#include <linux/kernel.h>
19#include <linux/module.h>
Jason A. Donenfeld109e23b2016-11-07 20:47:09 +010020#include <asm/unaligned.h>
Martin Willif979e012015-06-01 13:43:58 +020021
Martin Willi2546f812015-07-16 19:14:05 +020022int crypto_poly1305_init(struct shash_desc *desc)
Martin Willif979e012015-06-01 13:43:58 +020023{
24 struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
25
Eric Biggers1b6fd3d2018-11-16 17:26:28 -080026 poly1305_core_init(&dctx->h);
Martin Willif979e012015-06-01 13:43:58 +020027 dctx->buflen = 0;
Ard Biesheuvelad8f5b82019-11-08 13:22:20 +010028 dctx->rset = 0;
Martin Willic2b7b20a2015-06-16 11:34:16 +020029 dctx->sset = false;
Martin Willif979e012015-06-01 13:43:58 +020030
31 return 0;
32}
Martin Willi2546f812015-07-16 19:14:05 +020033EXPORT_SYMBOL_GPL(crypto_poly1305_init);
Martin Willif979e012015-06-01 13:43:58 +020034
Ard Biesheuvel48ea8c62019-11-08 13:22:19 +010035static void poly1305_blocks(struct poly1305_desc_ctx *dctx, const u8 *src,
36 unsigned int srclen)
Eric Biggers1b6fd3d2018-11-16 17:26:28 -080037{
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;
Martin Willif979e012015-06-01 13:43:58 +020044 }
45
Ard Biesheuvelad8f5b82019-11-08 13:22:20 +010046 poly1305_core_blocks(&dctx->h, dctx->r, src,
Ard Biesheuvel48ea8c62019-11-08 13:22:19 +010047 srclen / POLY1305_BLOCK_SIZE, 1);
Martin Willif979e012015-06-01 13:43:58 +020048}
49
Martin Willi2546f812015-07-16 19:14:05 +020050int crypto_poly1305_update(struct shash_desc *desc,
Martin Willif979e012015-06-01 13:43:58 +020051 const u8 *src, unsigned int srclen)
52{
53 struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
Martin Willif979e012015-06-01 13:43:58 +020054 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) {
Martin Willic2b7b20a2015-06-16 11:34:16 +020064 poly1305_blocks(dctx, dctx->buf,
Ard Biesheuvel48ea8c62019-11-08 13:22:19 +010065 POLY1305_BLOCK_SIZE);
Martin Willif979e012015-06-01 13:43:58 +020066 dctx->buflen = 0;
67 }
68 }
69
70 if (likely(srclen >= POLY1305_BLOCK_SIZE)) {
Ard Biesheuvel48ea8c62019-11-08 13:22:19 +010071 poly1305_blocks(dctx, src, srclen);
Eric Biggers1b6fd3d2018-11-16 17:26:28 -080072 src += srclen - (srclen % POLY1305_BLOCK_SIZE);
73 srclen %= POLY1305_BLOCK_SIZE;
Martin Willif979e012015-06-01 13:43:58 +020074 }
75
76 if (unlikely(srclen)) {
77 dctx->buflen = srclen;
78 memcpy(dctx->buf, src, srclen);
79 }
80
81 return 0;
82}
Martin Willi2546f812015-07-16 19:14:05 +020083EXPORT_SYMBOL_GPL(crypto_poly1305_update);
Martin Willif979e012015-06-01 13:43:58 +020084
Eric Biggers1b6fd3d2018-11-16 17:26:28 -080085int crypto_poly1305_final(struct shash_desc *desc, u8 *dst)
86{
87 struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
Eric Biggers1b6fd3d2018-11-16 17:26:28 -080088
89 if (unlikely(!dctx->sset))
90 return -ENOKEY;
91
Ard Biesheuvela1d93062019-11-08 13:22:21 +010092 poly1305_final_generic(dctx, dst);
Martin Willif979e012015-06-01 13:43:58 +020093 return 0;
94}
Martin Willi2546f812015-07-16 19:14:05 +020095EXPORT_SYMBOL_GPL(crypto_poly1305_final);
Martin Willif979e012015-06-01 13:43:58 +020096
97static struct shash_alg poly1305_alg = {
98 .digestsize = POLY1305_DIGEST_SIZE,
Martin Willi2546f812015-07-16 19:14:05 +020099 .init = crypto_poly1305_init,
100 .update = crypto_poly1305_update,
101 .final = crypto_poly1305_final,
Martin Willif979e012015-06-01 13:43:58 +0200102 .descsize = sizeof(struct poly1305_desc_ctx),
103 .base = {
104 .cra_name = "poly1305",
105 .cra_driver_name = "poly1305-generic",
106 .cra_priority = 100,
Martin Willif979e012015-06-01 13:43:58 +0200107 .cra_blocksize = POLY1305_BLOCK_SIZE,
Martin Willif979e012015-06-01 13:43:58 +0200108 .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
Eric Biggersc4741b22019-04-11 21:57:42 -0700122subsys_initcall(poly1305_mod_init);
Martin Willif979e012015-06-01 13:43:58 +0200123module_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");