1 /* 1 /* 2 * Poly1305 authenticator algorithm, RFC7539 2 * Poly1305 authenticator algorithm, RFC7539 3 * 3 * 4 * Copyright (C) 2015 Martin Willi 4 * Copyright (C) 2015 Martin Willi 5 * 5 * 6 * Based on public domain code by Andrew Moon 6 * Based on public domain code by Andrew Moon and Daniel J. Bernstein. 7 * 7 * 8 * This program is free software; you can redi 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Publi 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation; either versio 10 * the Free Software Foundation; either version 2 of the License, or 11 * (at your option) any later version. 11 * (at your option) any later version. 12 */ 12 */ 13 13 14 #include <crypto/algapi.h> 14 #include <crypto/algapi.h> 15 #include <crypto/internal/hash.h> 15 #include <crypto/internal/hash.h> 16 #include <crypto/internal/poly1305.h> 16 #include <crypto/internal/poly1305.h> 17 #include <linux/crypto.h> 17 #include <linux/crypto.h> 18 #include <linux/kernel.h> 18 #include <linux/kernel.h> 19 #include <linux/module.h> 19 #include <linux/module.h> 20 #include <asm/unaligned.h> 20 #include <asm/unaligned.h> 21 21 22 static int crypto_poly1305_init(struct shash_d 22 static int crypto_poly1305_init(struct shash_desc *desc) 23 { 23 { 24 struct poly1305_desc_ctx *dctx = shash 24 struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc); 25 25 26 poly1305_core_init(&dctx->h); 26 poly1305_core_init(&dctx->h); 27 dctx->buflen = 0; 27 dctx->buflen = 0; 28 dctx->rset = 0; 28 dctx->rset = 0; 29 dctx->sset = false; 29 dctx->sset = false; 30 30 31 return 0; 31 return 0; 32 } 32 } 33 33 34 static unsigned int crypto_poly1305_setdesckey << 35 << 36 { << 37 if (!dctx->sset) { << 38 if (!dctx->rset && srclen >= P << 39 poly1305_core_setkey(& << 40 src += POLY1305_BLOCK_ << 41 srclen -= POLY1305_BLO << 42 dctx->rset = 2; << 43 } << 44 if (srclen >= POLY1305_BLOCK_S << 45 dctx->s[0] = get_unali << 46 dctx->s[1] = get_unali << 47 dctx->s[2] = get_unali << 48 dctx->s[3] = get_unali << 49 src += POLY1305_BLOCK_ << 50 srclen -= POLY1305_BLO << 51 dctx->sset = true; << 52 } << 53 } << 54 return srclen; << 55 } << 56 << 57 static void poly1305_blocks(struct poly1305_de 34 static void poly1305_blocks(struct poly1305_desc_ctx *dctx, const u8 *src, 58 unsigned int srcle 35 unsigned int srclen) 59 { 36 { 60 unsigned int datalen; 37 unsigned int datalen; 61 38 62 if (unlikely(!dctx->sset)) { 39 if (unlikely(!dctx->sset)) { 63 datalen = crypto_poly1305_setd 40 datalen = crypto_poly1305_setdesckey(dctx, src, srclen); 64 src += srclen - datalen; 41 src += srclen - datalen; 65 srclen = datalen; 42 srclen = datalen; 66 } 43 } 67 44 68 poly1305_core_blocks(&dctx->h, &dctx-> !! 45 poly1305_core_blocks(&dctx->h, dctx->r, src, 69 srclen / POLY1305 46 srclen / POLY1305_BLOCK_SIZE, 1); 70 } 47 } 71 48 72 static int crypto_poly1305_update(struct shash 49 static int crypto_poly1305_update(struct shash_desc *desc, 73 const u8 *sr 50 const u8 *src, unsigned int srclen) 74 { 51 { 75 struct poly1305_desc_ctx *dctx = shash 52 struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc); 76 unsigned int bytes; 53 unsigned int bytes; 77 54 78 if (unlikely(dctx->buflen)) { 55 if (unlikely(dctx->buflen)) { 79 bytes = min(srclen, POLY1305_B 56 bytes = min(srclen, POLY1305_BLOCK_SIZE - dctx->buflen); 80 memcpy(dctx->buf + dctx->bufle 57 memcpy(dctx->buf + dctx->buflen, src, bytes); 81 src += bytes; 58 src += bytes; 82 srclen -= bytes; 59 srclen -= bytes; 83 dctx->buflen += bytes; 60 dctx->buflen += bytes; 84 61 85 if (dctx->buflen == POLY1305_B 62 if (dctx->buflen == POLY1305_BLOCK_SIZE) { 86 poly1305_blocks(dctx, 63 poly1305_blocks(dctx, dctx->buf, 87 POLY13 64 POLY1305_BLOCK_SIZE); 88 dctx->buflen = 0; 65 dctx->buflen = 0; 89 } 66 } 90 } 67 } 91 68 92 if (likely(srclen >= POLY1305_BLOCK_SI 69 if (likely(srclen >= POLY1305_BLOCK_SIZE)) { 93 poly1305_blocks(dctx, src, src 70 poly1305_blocks(dctx, src, srclen); 94 src += srclen - (srclen % POLY 71 src += srclen - (srclen % POLY1305_BLOCK_SIZE); 95 srclen %= POLY1305_BLOCK_SIZE; 72 srclen %= POLY1305_BLOCK_SIZE; 96 } 73 } 97 74 98 if (unlikely(srclen)) { 75 if (unlikely(srclen)) { 99 dctx->buflen = srclen; 76 dctx->buflen = srclen; 100 memcpy(dctx->buf, src, srclen) 77 memcpy(dctx->buf, src, srclen); 101 } 78 } 102 79 103 return 0; 80 return 0; 104 } 81 } 105 82 106 static int crypto_poly1305_final(struct shash_ 83 static int crypto_poly1305_final(struct shash_desc *desc, u8 *dst) 107 { 84 { 108 struct poly1305_desc_ctx *dctx = shash 85 struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc); 109 86 110 if (unlikely(!dctx->sset)) 87 if (unlikely(!dctx->sset)) 111 return -ENOKEY; 88 return -ENOKEY; 112 89 113 poly1305_final_generic(dctx, dst); 90 poly1305_final_generic(dctx, dst); 114 return 0; 91 return 0; 115 } 92 } 116 93 117 static struct shash_alg poly1305_alg = { 94 static struct shash_alg poly1305_alg = { 118 .digestsize = POLY1305_DIGEST_SIZE 95 .digestsize = POLY1305_DIGEST_SIZE, 119 .init = crypto_poly1305_init 96 .init = crypto_poly1305_init, 120 .update = crypto_poly1305_upda 97 .update = crypto_poly1305_update, 121 .final = crypto_poly1305_fina 98 .final = crypto_poly1305_final, 122 .descsize = sizeof(struct poly13 99 .descsize = sizeof(struct poly1305_desc_ctx), 123 .base = { 100 .base = { 124 .cra_name = "pol 101 .cra_name = "poly1305", 125 .cra_driver_name = "pol 102 .cra_driver_name = "poly1305-generic", 126 .cra_priority = 100, 103 .cra_priority = 100, 127 .cra_blocksize = POLY 104 .cra_blocksize = POLY1305_BLOCK_SIZE, 128 .cra_module = THIS 105 .cra_module = THIS_MODULE, 129 }, 106 }, 130 }; 107 }; 131 108 132 static int __init poly1305_mod_init(void) 109 static int __init poly1305_mod_init(void) 133 { 110 { 134 return crypto_register_shash(&poly1305 111 return crypto_register_shash(&poly1305_alg); 135 } 112 } 136 113 137 static void __exit poly1305_mod_exit(void) 114 static void __exit poly1305_mod_exit(void) 138 { 115 { 139 crypto_unregister_shash(&poly1305_alg) 116 crypto_unregister_shash(&poly1305_alg); 140 } 117 } 141 118 142 subsys_initcall(poly1305_mod_init); 119 subsys_initcall(poly1305_mod_init); 143 module_exit(poly1305_mod_exit); 120 module_exit(poly1305_mod_exit); 144 121 145 MODULE_LICENSE("GPL"); 122 MODULE_LICENSE("GPL"); 146 MODULE_AUTHOR("Martin Willi <martin@strongswan 123 MODULE_AUTHOR("Martin Willi <martin@strongswan.org>"); 147 MODULE_DESCRIPTION("Poly1305 authenticator"); 124 MODULE_DESCRIPTION("Poly1305 authenticator"); 148 MODULE_ALIAS_CRYPTO("poly1305"); 125 MODULE_ALIAS_CRYPTO("poly1305"); 149 MODULE_ALIAS_CRYPTO("poly1305-generic"); 126 MODULE_ALIAS_CRYPTO("poly1305-generic"); 150 127
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.