blob: 94af47eb6fa699745a7cc6dffaebc10a1b2e455c [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001/*
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>
Olivier Deprez157378f2022-04-04 15:47:50 +020016#include <crypto/internal/poly1305.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000017#include <linux/crypto.h>
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <asm/unaligned.h>
21
Olivier Deprez157378f2022-04-04 15:47:50 +020022static int crypto_poly1305_init(struct shash_desc *desc)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000023{
24 struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
25
David Brazdil0f672f62019-12-10 10:32:29 +000026 poly1305_core_init(&dctx->h);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000027 dctx->buflen = 0;
Olivier Deprez157378f2022-04-04 15:47:50 +020028 dctx->rset = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000029 dctx->sset = false;
30
31 return 0;
32}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000033
Olivier Deprez157378f2022-04-04 15:47:50 +020034static unsigned int crypto_poly1305_setdesckey(struct poly1305_desc_ctx *dctx,
35 const u8 *src, unsigned int srclen)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000036{
37 if (!dctx->sset) {
38 if (!dctx->rset && srclen >= POLY1305_BLOCK_SIZE) {
Olivier Deprez157378f2022-04-04 15:47:50 +020039 poly1305_core_setkey(&dctx->core_r, src);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000040 src += POLY1305_BLOCK_SIZE;
41 srclen -= POLY1305_BLOCK_SIZE;
Olivier Deprez157378f2022-04-04 15:47:50 +020042 dctx->rset = 2;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000043 }
44 if (srclen >= POLY1305_BLOCK_SIZE) {
David Brazdil0f672f62019-12-10 10:32:29 +000045 dctx->s[0] = get_unaligned_le32(src + 0);
46 dctx->s[1] = get_unaligned_le32(src + 4);
47 dctx->s[2] = get_unaligned_le32(src + 8);
48 dctx->s[3] = get_unaligned_le32(src + 12);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000049 src += POLY1305_BLOCK_SIZE;
50 srclen -= POLY1305_BLOCK_SIZE;
51 dctx->sset = true;
52 }
53 }
54 return srclen;
55}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000056
Olivier Deprez157378f2022-04-04 15:47:50 +020057static void poly1305_blocks(struct poly1305_desc_ctx *dctx, const u8 *src,
58 unsigned int srclen)
David Brazdil0f672f62019-12-10 10:32:29 +000059{
60 unsigned int datalen;
61
62 if (unlikely(!dctx->sset)) {
63 datalen = crypto_poly1305_setdesckey(dctx, src, srclen);
64 src += srclen - datalen;
65 srclen = datalen;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000066 }
67
Olivier Deprez157378f2022-04-04 15:47:50 +020068 poly1305_core_blocks(&dctx->h, &dctx->core_r, src,
69 srclen / POLY1305_BLOCK_SIZE, 1);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000070}
71
Olivier Deprez157378f2022-04-04 15:47:50 +020072static int crypto_poly1305_update(struct shash_desc *desc,
73 const u8 *src, unsigned int srclen)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000074{
75 struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
76 unsigned int bytes;
77
78 if (unlikely(dctx->buflen)) {
79 bytes = min(srclen, POLY1305_BLOCK_SIZE - dctx->buflen);
80 memcpy(dctx->buf + dctx->buflen, src, bytes);
81 src += bytes;
82 srclen -= bytes;
83 dctx->buflen += bytes;
84
85 if (dctx->buflen == POLY1305_BLOCK_SIZE) {
86 poly1305_blocks(dctx, dctx->buf,
Olivier Deprez157378f2022-04-04 15:47:50 +020087 POLY1305_BLOCK_SIZE);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000088 dctx->buflen = 0;
89 }
90 }
91
92 if (likely(srclen >= POLY1305_BLOCK_SIZE)) {
Olivier Deprez157378f2022-04-04 15:47:50 +020093 poly1305_blocks(dctx, src, srclen);
David Brazdil0f672f62019-12-10 10:32:29 +000094 src += srclen - (srclen % POLY1305_BLOCK_SIZE);
95 srclen %= POLY1305_BLOCK_SIZE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000096 }
97
98 if (unlikely(srclen)) {
99 dctx->buflen = srclen;
100 memcpy(dctx->buf, src, srclen);
101 }
102
103 return 0;
104}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000105
Olivier Deprez157378f2022-04-04 15:47:50 +0200106static int crypto_poly1305_final(struct shash_desc *desc, u8 *dst)
David Brazdil0f672f62019-12-10 10:32:29 +0000107{
108 struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
David Brazdil0f672f62019-12-10 10:32:29 +0000109
110 if (unlikely(!dctx->sset))
111 return -ENOKEY;
112
Olivier Deprez157378f2022-04-04 15:47:50 +0200113 poly1305_final_generic(dctx, dst);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000114 return 0;
115}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000116
117static struct shash_alg poly1305_alg = {
118 .digestsize = POLY1305_DIGEST_SIZE,
119 .init = crypto_poly1305_init,
120 .update = crypto_poly1305_update,
121 .final = crypto_poly1305_final,
122 .descsize = sizeof(struct poly1305_desc_ctx),
123 .base = {
124 .cra_name = "poly1305",
125 .cra_driver_name = "poly1305-generic",
126 .cra_priority = 100,
127 .cra_blocksize = POLY1305_BLOCK_SIZE,
128 .cra_module = THIS_MODULE,
129 },
130};
131
132static int __init poly1305_mod_init(void)
133{
134 return crypto_register_shash(&poly1305_alg);
135}
136
137static void __exit poly1305_mod_exit(void)
138{
139 crypto_unregister_shash(&poly1305_alg);
140}
141
David Brazdil0f672f62019-12-10 10:32:29 +0000142subsys_initcall(poly1305_mod_init);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000143module_exit(poly1305_mod_exit);
144
145MODULE_LICENSE("GPL");
146MODULE_AUTHOR("Martin Willi <martin@strongswan.org>");
147MODULE_DESCRIPTION("Poly1305 authenticator");
148MODULE_ALIAS_CRYPTO("poly1305");
149MODULE_ALIAS_CRYPTO("poly1305-generic");