Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

crypto: poly1305 - Export common Poly1305 helpers

As architecture specific drivers need a software fallback, export Poly1305
init/update/final functions together with some helpers in a header file.

Signed-off-by: Martin Willi <martin@strongswan.org>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

authored by

Martin Willi and committed by
Herbert Xu
2546f811 6692cbc2

+77 -41
+1 -3
crypto/chacha20poly1305.c
··· 14 14 #include <crypto/internal/skcipher.h> 15 15 #include <crypto/scatterwalk.h> 16 16 #include <crypto/chacha20.h> 17 + #include <crypto/poly1305.h> 17 18 #include <linux/err.h> 18 19 #include <linux/init.h> 19 20 #include <linux/kernel.h> ··· 22 21 23 22 #include "internal.h" 24 23 25 - #define POLY1305_BLOCK_SIZE 16 26 - #define POLY1305_DIGEST_SIZE 16 27 - #define POLY1305_KEY_SIZE 32 28 24 #define CHACHAPOLY_IV_SIZE 12 29 25 30 26 struct chachapoly_instance_ctx {
+35 -38
crypto/poly1305_generic.c
··· 13 13 14 14 #include <crypto/algapi.h> 15 15 #include <crypto/internal/hash.h> 16 + #include <crypto/poly1305.h> 16 17 #include <linux/crypto.h> 17 18 #include <linux/kernel.h> 18 19 #include <linux/module.h> 19 - 20 - #define POLY1305_BLOCK_SIZE 16 21 - #define POLY1305_KEY_SIZE 32 22 - #define POLY1305_DIGEST_SIZE 16 23 - 24 - struct poly1305_desc_ctx { 25 - /* key */ 26 - u32 r[5]; 27 - /* finalize key */ 28 - u32 s[4]; 29 - /* accumulator */ 30 - u32 h[5]; 31 - /* partial buffer */ 32 - u8 buf[POLY1305_BLOCK_SIZE]; 33 - /* bytes used in partial buffer */ 34 - unsigned int buflen; 35 - /* r key has been set */ 36 - bool rset; 37 - /* s key has been set */ 38 - bool sset; 39 - }; 40 20 41 21 static inline u64 mlt(u64 a, u64 b) 42 22 { ··· 38 58 return le32_to_cpup(p); 39 59 } 40 60 41 - static int poly1305_init(struct shash_desc *desc) 61 + int crypto_poly1305_init(struct shash_desc *desc) 42 62 { 43 63 struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc); 44 64 ··· 49 69 50 70 return 0; 51 71 } 72 + EXPORT_SYMBOL_GPL(crypto_poly1305_init); 52 73 53 - static int poly1305_setkey(struct crypto_shash *tfm, 74 + int crypto_poly1305_setkey(struct crypto_shash *tfm, 54 75 const u8 *key, unsigned int keylen) 55 76 { 56 77 /* Poly1305 requires a unique key for each tag, which implies that ··· 60 79 * the update() call. */ 61 80 return -ENOTSUPP; 62 81 } 82 + EXPORT_SYMBOL_GPL(crypto_poly1305_setkey); 63 83 64 84 static void poly1305_setrkey(struct poly1305_desc_ctx *dctx, const u8 *key) 65 85 { ··· 80 98 dctx->s[3] = le32_to_cpuvp(key + 12); 81 99 } 82 100 83 - static unsigned int poly1305_blocks(struct poly1305_desc_ctx *dctx, 84 - const u8 *src, unsigned int srclen, 85 - u32 hibit) 101 + unsigned int crypto_poly1305_setdesckey(struct poly1305_desc_ctx *dctx, 102 + const u8 *src, unsigned int srclen) 86 103 { 87 - u32 r0, r1, r2, r3, r4; 88 - u32 s1, s2, s3, s4; 89 - u32 h0, h1, h2, h3, h4; 90 - u64 d0, d1, d2, d3, d4; 91 - 92 - if (unlikely(!dctx->sset)) { 104 + if (!dctx->sset) { 93 105 if (!dctx->rset && srclen >= POLY1305_BLOCK_SIZE) { 94 106 poly1305_setrkey(dctx, src); 95 107 src += POLY1305_BLOCK_SIZE; ··· 96 120 srclen -= POLY1305_BLOCK_SIZE; 97 121 dctx->sset = true; 98 122 } 123 + } 124 + return srclen; 125 + } 126 + EXPORT_SYMBOL_GPL(crypto_poly1305_setdesckey); 127 + 128 + static unsigned int poly1305_blocks(struct poly1305_desc_ctx *dctx, 129 + const u8 *src, unsigned int srclen, 130 + u32 hibit) 131 + { 132 + u32 r0, r1, r2, r3, r4; 133 + u32 s1, s2, s3, s4; 134 + u32 h0, h1, h2, h3, h4; 135 + u64 d0, d1, d2, d3, d4; 136 + unsigned int datalen; 137 + 138 + if (unlikely(!dctx->sset)) { 139 + datalen = crypto_poly1305_setdesckey(dctx, src, srclen); 140 + src += srclen - datalen; 141 + srclen = datalen; 99 142 } 100 143 101 144 r0 = dctx->r[0]; ··· 176 181 return srclen; 177 182 } 178 183 179 - static int poly1305_update(struct shash_desc *desc, 184 + int crypto_poly1305_update(struct shash_desc *desc, 180 185 const u8 *src, unsigned int srclen) 181 186 { 182 187 struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc); ··· 209 214 210 215 return 0; 211 216 } 217 + EXPORT_SYMBOL_GPL(crypto_poly1305_update); 212 218 213 - static int poly1305_final(struct shash_desc *desc, u8 *dst) 219 + int crypto_poly1305_final(struct shash_desc *desc, u8 *dst) 214 220 { 215 221 struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc); 216 222 __le32 *mac = (__le32 *)dst; ··· 278 282 279 283 return 0; 280 284 } 285 + EXPORT_SYMBOL_GPL(crypto_poly1305_final); 281 286 282 287 static struct shash_alg poly1305_alg = { 283 288 .digestsize = POLY1305_DIGEST_SIZE, 284 - .init = poly1305_init, 285 - .update = poly1305_update, 286 - .final = poly1305_final, 287 - .setkey = poly1305_setkey, 289 + .init = crypto_poly1305_init, 290 + .update = crypto_poly1305_update, 291 + .final = crypto_poly1305_final, 292 + .setkey = crypto_poly1305_setkey, 288 293 .descsize = sizeof(struct poly1305_desc_ctx), 289 294 .base = { 290 295 .cra_name = "poly1305",
+41
include/crypto/poly1305.h
··· 1 + /* 2 + * Common values for the Poly1305 algorithm 3 + */ 4 + 5 + #ifndef _CRYPTO_POLY1305_H 6 + #define _CRYPTO_POLY1305_H 7 + 8 + #include <linux/types.h> 9 + #include <linux/crypto.h> 10 + 11 + #define POLY1305_BLOCK_SIZE 16 12 + #define POLY1305_KEY_SIZE 32 13 + #define POLY1305_DIGEST_SIZE 16 14 + 15 + struct poly1305_desc_ctx { 16 + /* key */ 17 + u32 r[5]; 18 + /* finalize key */ 19 + u32 s[4]; 20 + /* accumulator */ 21 + u32 h[5]; 22 + /* partial buffer */ 23 + u8 buf[POLY1305_BLOCK_SIZE]; 24 + /* bytes used in partial buffer */ 25 + unsigned int buflen; 26 + /* r key has been set */ 27 + bool rset; 28 + /* s key has been set */ 29 + bool sset; 30 + }; 31 + 32 + int crypto_poly1305_init(struct shash_desc *desc); 33 + int crypto_poly1305_setkey(struct crypto_shash *tfm, 34 + const u8 *key, unsigned int keylen); 35 + unsigned int crypto_poly1305_setdesckey(struct poly1305_desc_ctx *dctx, 36 + const u8 *src, unsigned int srclen); 37 + int crypto_poly1305_update(struct shash_desc *desc, 38 + const u8 *src, unsigned int srclen); 39 + int crypto_poly1305_final(struct shash_desc *desc, u8 *dst); 40 + 41 + #endif