Martin Willi | f979e01 | 2015-06-01 13:43:58 +0200 | [diff] [blame] | 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> |
Ard Biesheuvel | 48ea8c6 | 2019-11-08 13:22:19 +0100 | [diff] [blame^] | 16 | #include <crypto/internal/poly1305.h> |
Martin Willi | f979e01 | 2015-06-01 13:43:58 +0200 | [diff] [blame] | 17 | #include <linux/crypto.h> |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/module.h> |
Jason A. Donenfeld | 109e23b | 2016-11-07 20:47:09 +0100 | [diff] [blame] | 20 | #include <asm/unaligned.h> |
Martin Willi | f979e01 | 2015-06-01 13:43:58 +0200 | [diff] [blame] | 21 | |
Martin Willi | 2546f81 | 2015-07-16 19:14:05 +0200 | [diff] [blame] | 22 | int crypto_poly1305_init(struct shash_desc *desc) |
Martin Willi | f979e01 | 2015-06-01 13:43:58 +0200 | [diff] [blame] | 23 | { |
| 24 | struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc); |
| 25 | |
Eric Biggers | 1b6fd3d | 2018-11-16 17:26:28 -0800 | [diff] [blame] | 26 | poly1305_core_init(&dctx->h); |
Martin Willi | f979e01 | 2015-06-01 13:43:58 +0200 | [diff] [blame] | 27 | dctx->buflen = 0; |
Martin Willi | c2b7b20a | 2015-06-16 11:34:16 +0200 | [diff] [blame] | 28 | dctx->rset = false; |
| 29 | dctx->sset = false; |
Martin Willi | f979e01 | 2015-06-01 13:43:58 +0200 | [diff] [blame] | 30 | |
| 31 | return 0; |
| 32 | } |
Martin Willi | 2546f81 | 2015-07-16 19:14:05 +0200 | [diff] [blame] | 33 | EXPORT_SYMBOL_GPL(crypto_poly1305_init); |
Martin Willi | f979e01 | 2015-06-01 13:43:58 +0200 | [diff] [blame] | 34 | |
Ard Biesheuvel | 48ea8c6 | 2019-11-08 13:22:19 +0100 | [diff] [blame^] | 35 | static void poly1305_blocks(struct poly1305_desc_ctx *dctx, const u8 *src, |
| 36 | unsigned int srclen) |
Eric Biggers | 1b6fd3d | 2018-11-16 17:26:28 -0800 | [diff] [blame] | 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; |
Martin Willi | f979e01 | 2015-06-01 13:43:58 +0200 | [diff] [blame] | 44 | } |
| 45 | |
Ard Biesheuvel | 48ea8c6 | 2019-11-08 13:22:19 +0100 | [diff] [blame^] | 46 | poly1305_core_blocks(&dctx->h, &dctx->r, src, |
| 47 | srclen / POLY1305_BLOCK_SIZE, 1); |
Martin Willi | f979e01 | 2015-06-01 13:43:58 +0200 | [diff] [blame] | 48 | } |
| 49 | |
Martin Willi | 2546f81 | 2015-07-16 19:14:05 +0200 | [diff] [blame] | 50 | int crypto_poly1305_update(struct shash_desc *desc, |
Martin Willi | f979e01 | 2015-06-01 13:43:58 +0200 | [diff] [blame] | 51 | const u8 *src, unsigned int srclen) |
| 52 | { |
| 53 | struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc); |
Martin Willi | f979e01 | 2015-06-01 13:43:58 +0200 | [diff] [blame] | 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) { |
Martin Willi | c2b7b20a | 2015-06-16 11:34:16 +0200 | [diff] [blame] | 64 | poly1305_blocks(dctx, dctx->buf, |
Ard Biesheuvel | 48ea8c6 | 2019-11-08 13:22:19 +0100 | [diff] [blame^] | 65 | POLY1305_BLOCK_SIZE); |
Martin Willi | f979e01 | 2015-06-01 13:43:58 +0200 | [diff] [blame] | 66 | dctx->buflen = 0; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | if (likely(srclen >= POLY1305_BLOCK_SIZE)) { |
Ard Biesheuvel | 48ea8c6 | 2019-11-08 13:22:19 +0100 | [diff] [blame^] | 71 | poly1305_blocks(dctx, src, srclen); |
Eric Biggers | 1b6fd3d | 2018-11-16 17:26:28 -0800 | [diff] [blame] | 72 | src += srclen - (srclen % POLY1305_BLOCK_SIZE); |
| 73 | srclen %= POLY1305_BLOCK_SIZE; |
Martin Willi | f979e01 | 2015-06-01 13:43:58 +0200 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | if (unlikely(srclen)) { |
| 77 | dctx->buflen = srclen; |
| 78 | memcpy(dctx->buf, src, srclen); |
| 79 | } |
| 80 | |
| 81 | return 0; |
| 82 | } |
Martin Willi | 2546f81 | 2015-07-16 19:14:05 +0200 | [diff] [blame] | 83 | EXPORT_SYMBOL_GPL(crypto_poly1305_update); |
Martin Willi | f979e01 | 2015-06-01 13:43:58 +0200 | [diff] [blame] | 84 | |
Eric Biggers | 1b6fd3d | 2018-11-16 17:26:28 -0800 | [diff] [blame] | 85 | int crypto_poly1305_final(struct shash_desc *desc, u8 *dst) |
| 86 | { |
| 87 | struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc); |
| 88 | __le32 digest[4]; |
| 89 | u64 f = 0; |
| 90 | |
| 91 | if (unlikely(!dctx->sset)) |
| 92 | return -ENOKEY; |
| 93 | |
| 94 | if (unlikely(dctx->buflen)) { |
| 95 | dctx->buf[dctx->buflen++] = 1; |
| 96 | memset(dctx->buf + dctx->buflen, 0, |
| 97 | POLY1305_BLOCK_SIZE - dctx->buflen); |
Ard Biesheuvel | 48ea8c6 | 2019-11-08 13:22:19 +0100 | [diff] [blame^] | 98 | poly1305_core_blocks(&dctx->h, &dctx->r, dctx->buf, 1, 0); |
Eric Biggers | 1b6fd3d | 2018-11-16 17:26:28 -0800 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | poly1305_core_emit(&dctx->h, digest); |
Martin Willi | f979e01 | 2015-06-01 13:43:58 +0200 | [diff] [blame] | 102 | |
| 103 | /* mac = (h + s) % (2^128) */ |
Eric Biggers | 1b6fd3d | 2018-11-16 17:26:28 -0800 | [diff] [blame] | 104 | f = (f >> 32) + le32_to_cpu(digest[0]) + dctx->s[0]; |
| 105 | put_unaligned_le32(f, dst + 0); |
| 106 | f = (f >> 32) + le32_to_cpu(digest[1]) + dctx->s[1]; |
| 107 | put_unaligned_le32(f, dst + 4); |
| 108 | f = (f >> 32) + le32_to_cpu(digest[2]) + dctx->s[2]; |
| 109 | put_unaligned_le32(f, dst + 8); |
| 110 | f = (f >> 32) + le32_to_cpu(digest[3]) + dctx->s[3]; |
| 111 | put_unaligned_le32(f, dst + 12); |
Martin Willi | f979e01 | 2015-06-01 13:43:58 +0200 | [diff] [blame] | 112 | |
| 113 | return 0; |
| 114 | } |
Martin Willi | 2546f81 | 2015-07-16 19:14:05 +0200 | [diff] [blame] | 115 | EXPORT_SYMBOL_GPL(crypto_poly1305_final); |
Martin Willi | f979e01 | 2015-06-01 13:43:58 +0200 | [diff] [blame] | 116 | |
| 117 | static struct shash_alg poly1305_alg = { |
| 118 | .digestsize = POLY1305_DIGEST_SIZE, |
Martin Willi | 2546f81 | 2015-07-16 19:14:05 +0200 | [diff] [blame] | 119 | .init = crypto_poly1305_init, |
| 120 | .update = crypto_poly1305_update, |
| 121 | .final = crypto_poly1305_final, |
Martin Willi | f979e01 | 2015-06-01 13:43:58 +0200 | [diff] [blame] | 122 | .descsize = sizeof(struct poly1305_desc_ctx), |
| 123 | .base = { |
| 124 | .cra_name = "poly1305", |
| 125 | .cra_driver_name = "poly1305-generic", |
| 126 | .cra_priority = 100, |
Martin Willi | f979e01 | 2015-06-01 13:43:58 +0200 | [diff] [blame] | 127 | .cra_blocksize = POLY1305_BLOCK_SIZE, |
Martin Willi | f979e01 | 2015-06-01 13:43:58 +0200 | [diff] [blame] | 128 | .cra_module = THIS_MODULE, |
| 129 | }, |
| 130 | }; |
| 131 | |
| 132 | static int __init poly1305_mod_init(void) |
| 133 | { |
| 134 | return crypto_register_shash(&poly1305_alg); |
| 135 | } |
| 136 | |
| 137 | static void __exit poly1305_mod_exit(void) |
| 138 | { |
| 139 | crypto_unregister_shash(&poly1305_alg); |
| 140 | } |
| 141 | |
Eric Biggers | c4741b2 | 2019-04-11 21:57:42 -0700 | [diff] [blame] | 142 | subsys_initcall(poly1305_mod_init); |
Martin Willi | f979e01 | 2015-06-01 13:43:58 +0200 | [diff] [blame] | 143 | module_exit(poly1305_mod_exit); |
| 144 | |
| 145 | MODULE_LICENSE("GPL"); |
| 146 | MODULE_AUTHOR("Martin Willi <martin@strongswan.org>"); |
| 147 | MODULE_DESCRIPTION("Poly1305 authenticator"); |
| 148 | MODULE_ALIAS_CRYPTO("poly1305"); |
| 149 | MODULE_ALIAS_CRYPTO("poly1305-generic"); |