Back to home page

LXR

 
 

    


0001 /*
0002  * Poly1305 authenticator algorithm, RFC7539
0003  *
0004  * Copyright (C) 2015 Martin Willi
0005  *
0006  * Based on public domain code by Andrew Moon and Daniel J. Bernstein.
0007  *
0008  * This program is free software; you can redistribute it and/or modify
0009  * it under the terms of the GNU General Public License as published by
0010  * the Free Software Foundation; either version 2 of the License, or
0011  * (at your option) any later version.
0012  */
0013 
0014 #include <crypto/algapi.h>
0015 #include <crypto/internal/hash.h>
0016 #include <crypto/poly1305.h>
0017 #include <linux/crypto.h>
0018 #include <linux/kernel.h>
0019 #include <linux/module.h>
0020 #include <asm/unaligned.h>
0021 
0022 static inline u64 mlt(u64 a, u64 b)
0023 {
0024     return a * b;
0025 }
0026 
0027 static inline u32 sr(u64 v, u_char n)
0028 {
0029     return v >> n;
0030 }
0031 
0032 static inline u32 and(u32 v, u32 mask)
0033 {
0034     return v & mask;
0035 }
0036 
0037 int crypto_poly1305_init(struct shash_desc *desc)
0038 {
0039     struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
0040 
0041     memset(dctx->h, 0, sizeof(dctx->h));
0042     dctx->buflen = 0;
0043     dctx->rset = false;
0044     dctx->sset = false;
0045 
0046     return 0;
0047 }
0048 EXPORT_SYMBOL_GPL(crypto_poly1305_init);
0049 
0050 int crypto_poly1305_setkey(struct crypto_shash *tfm,
0051                const u8 *key, unsigned int keylen)
0052 {
0053     /* Poly1305 requires a unique key for each tag, which implies that
0054      * we can't set it on the tfm that gets accessed by multiple users
0055      * simultaneously. Instead we expect the key as the first 32 bytes in
0056      * the update() call. */
0057     return -ENOTSUPP;
0058 }
0059 EXPORT_SYMBOL_GPL(crypto_poly1305_setkey);
0060 
0061 static void poly1305_setrkey(struct poly1305_desc_ctx *dctx, const u8 *key)
0062 {
0063     /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */
0064     dctx->r[0] = (get_unaligned_le32(key +  0) >> 0) & 0x3ffffff;
0065     dctx->r[1] = (get_unaligned_le32(key +  3) >> 2) & 0x3ffff03;
0066     dctx->r[2] = (get_unaligned_le32(key +  6) >> 4) & 0x3ffc0ff;
0067     dctx->r[3] = (get_unaligned_le32(key +  9) >> 6) & 0x3f03fff;
0068     dctx->r[4] = (get_unaligned_le32(key + 12) >> 8) & 0x00fffff;
0069 }
0070 
0071 static void poly1305_setskey(struct poly1305_desc_ctx *dctx, const u8 *key)
0072 {
0073     dctx->s[0] = get_unaligned_le32(key +  0);
0074     dctx->s[1] = get_unaligned_le32(key +  4);
0075     dctx->s[2] = get_unaligned_le32(key +  8);
0076     dctx->s[3] = get_unaligned_le32(key + 12);
0077 }
0078 
0079 unsigned int crypto_poly1305_setdesckey(struct poly1305_desc_ctx *dctx,
0080                     const u8 *src, unsigned int srclen)
0081 {
0082     if (!dctx->sset) {
0083         if (!dctx->rset && srclen >= POLY1305_BLOCK_SIZE) {
0084             poly1305_setrkey(dctx, src);
0085             src += POLY1305_BLOCK_SIZE;
0086             srclen -= POLY1305_BLOCK_SIZE;
0087             dctx->rset = true;
0088         }
0089         if (srclen >= POLY1305_BLOCK_SIZE) {
0090             poly1305_setskey(dctx, src);
0091             src += POLY1305_BLOCK_SIZE;
0092             srclen -= POLY1305_BLOCK_SIZE;
0093             dctx->sset = true;
0094         }
0095     }
0096     return srclen;
0097 }
0098 EXPORT_SYMBOL_GPL(crypto_poly1305_setdesckey);
0099 
0100 static unsigned int poly1305_blocks(struct poly1305_desc_ctx *dctx,
0101                     const u8 *src, unsigned int srclen,
0102                     u32 hibit)
0103 {
0104     u32 r0, r1, r2, r3, r4;
0105     u32 s1, s2, s3, s4;
0106     u32 h0, h1, h2, h3, h4;
0107     u64 d0, d1, d2, d3, d4;
0108     unsigned int datalen;
0109 
0110     if (unlikely(!dctx->sset)) {
0111         datalen = crypto_poly1305_setdesckey(dctx, src, srclen);
0112         src += srclen - datalen;
0113         srclen = datalen;
0114     }
0115 
0116     r0 = dctx->r[0];
0117     r1 = dctx->r[1];
0118     r2 = dctx->r[2];
0119     r3 = dctx->r[3];
0120     r4 = dctx->r[4];
0121 
0122     s1 = r1 * 5;
0123     s2 = r2 * 5;
0124     s3 = r3 * 5;
0125     s4 = r4 * 5;
0126 
0127     h0 = dctx->h[0];
0128     h1 = dctx->h[1];
0129     h2 = dctx->h[2];
0130     h3 = dctx->h[3];
0131     h4 = dctx->h[4];
0132 
0133     while (likely(srclen >= POLY1305_BLOCK_SIZE)) {
0134 
0135         /* h += m[i] */
0136         h0 += (get_unaligned_le32(src +  0) >> 0) & 0x3ffffff;
0137         h1 += (get_unaligned_le32(src +  3) >> 2) & 0x3ffffff;
0138         h2 += (get_unaligned_le32(src +  6) >> 4) & 0x3ffffff;
0139         h3 += (get_unaligned_le32(src +  9) >> 6) & 0x3ffffff;
0140         h4 += (get_unaligned_le32(src + 12) >> 8) | hibit;
0141 
0142         /* h *= r */
0143         d0 = mlt(h0, r0) + mlt(h1, s4) + mlt(h2, s3) +
0144              mlt(h3, s2) + mlt(h4, s1);
0145         d1 = mlt(h0, r1) + mlt(h1, r0) + mlt(h2, s4) +
0146              mlt(h3, s3) + mlt(h4, s2);
0147         d2 = mlt(h0, r2) + mlt(h1, r1) + mlt(h2, r0) +
0148              mlt(h3, s4) + mlt(h4, s3);
0149         d3 = mlt(h0, r3) + mlt(h1, r2) + mlt(h2, r1) +
0150              mlt(h3, r0) + mlt(h4, s4);
0151         d4 = mlt(h0, r4) + mlt(h1, r3) + mlt(h2, r2) +
0152              mlt(h3, r1) + mlt(h4, r0);
0153 
0154         /* (partial) h %= p */
0155         d1 += sr(d0, 26);     h0 = and(d0, 0x3ffffff);
0156         d2 += sr(d1, 26);     h1 = and(d1, 0x3ffffff);
0157         d3 += sr(d2, 26);     h2 = and(d2, 0x3ffffff);
0158         d4 += sr(d3, 26);     h3 = and(d3, 0x3ffffff);
0159         h0 += sr(d4, 26) * 5; h4 = and(d4, 0x3ffffff);
0160         h1 += h0 >> 26;       h0 = h0 & 0x3ffffff;
0161 
0162         src += POLY1305_BLOCK_SIZE;
0163         srclen -= POLY1305_BLOCK_SIZE;
0164     }
0165 
0166     dctx->h[0] = h0;
0167     dctx->h[1] = h1;
0168     dctx->h[2] = h2;
0169     dctx->h[3] = h3;
0170     dctx->h[4] = h4;
0171 
0172     return srclen;
0173 }
0174 
0175 int crypto_poly1305_update(struct shash_desc *desc,
0176                const u8 *src, unsigned int srclen)
0177 {
0178     struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
0179     unsigned int bytes;
0180 
0181     if (unlikely(dctx->buflen)) {
0182         bytes = min(srclen, POLY1305_BLOCK_SIZE - dctx->buflen);
0183         memcpy(dctx->buf + dctx->buflen, src, bytes);
0184         src += bytes;
0185         srclen -= bytes;
0186         dctx->buflen += bytes;
0187 
0188         if (dctx->buflen == POLY1305_BLOCK_SIZE) {
0189             poly1305_blocks(dctx, dctx->buf,
0190                     POLY1305_BLOCK_SIZE, 1 << 24);
0191             dctx->buflen = 0;
0192         }
0193     }
0194 
0195     if (likely(srclen >= POLY1305_BLOCK_SIZE)) {
0196         bytes = poly1305_blocks(dctx, src, srclen, 1 << 24);
0197         src += srclen - bytes;
0198         srclen = bytes;
0199     }
0200 
0201     if (unlikely(srclen)) {
0202         dctx->buflen = srclen;
0203         memcpy(dctx->buf, src, srclen);
0204     }
0205 
0206     return 0;
0207 }
0208 EXPORT_SYMBOL_GPL(crypto_poly1305_update);
0209 
0210 int crypto_poly1305_final(struct shash_desc *desc, u8 *dst)
0211 {
0212     struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
0213     __le32 *mac = (__le32 *)dst;
0214     u32 h0, h1, h2, h3, h4;
0215     u32 g0, g1, g2, g3, g4;
0216     u32 mask;
0217     u64 f = 0;
0218 
0219     if (unlikely(!dctx->sset))
0220         return -ENOKEY;
0221 
0222     if (unlikely(dctx->buflen)) {
0223         dctx->buf[dctx->buflen++] = 1;
0224         memset(dctx->buf + dctx->buflen, 0,
0225                POLY1305_BLOCK_SIZE - dctx->buflen);
0226         poly1305_blocks(dctx, dctx->buf, POLY1305_BLOCK_SIZE, 0);
0227     }
0228 
0229     /* fully carry h */
0230     h0 = dctx->h[0];
0231     h1 = dctx->h[1];
0232     h2 = dctx->h[2];
0233     h3 = dctx->h[3];
0234     h4 = dctx->h[4];
0235 
0236     h2 += (h1 >> 26);     h1 = h1 & 0x3ffffff;
0237     h3 += (h2 >> 26);     h2 = h2 & 0x3ffffff;
0238     h4 += (h3 >> 26);     h3 = h3 & 0x3ffffff;
0239     h0 += (h4 >> 26) * 5; h4 = h4 & 0x3ffffff;
0240     h1 += (h0 >> 26);     h0 = h0 & 0x3ffffff;
0241 
0242     /* compute h + -p */
0243     g0 = h0 + 5;
0244     g1 = h1 + (g0 >> 26);             g0 &= 0x3ffffff;
0245     g2 = h2 + (g1 >> 26);             g1 &= 0x3ffffff;
0246     g3 = h3 + (g2 >> 26);             g2 &= 0x3ffffff;
0247     g4 = h4 + (g3 >> 26) - (1 << 26); g3 &= 0x3ffffff;
0248 
0249     /* select h if h < p, or h + -p if h >= p */
0250     mask = (g4 >> ((sizeof(u32) * 8) - 1)) - 1;
0251     g0 &= mask;
0252     g1 &= mask;
0253     g2 &= mask;
0254     g3 &= mask;
0255     g4 &= mask;
0256     mask = ~mask;
0257     h0 = (h0 & mask) | g0;
0258     h1 = (h1 & mask) | g1;
0259     h2 = (h2 & mask) | g2;
0260     h3 = (h3 & mask) | g3;
0261     h4 = (h4 & mask) | g4;
0262 
0263     /* h = h % (2^128) */
0264     h0 = (h0 >>  0) | (h1 << 26);
0265     h1 = (h1 >>  6) | (h2 << 20);
0266     h2 = (h2 >> 12) | (h3 << 14);
0267     h3 = (h3 >> 18) | (h4 <<  8);
0268 
0269     /* mac = (h + s) % (2^128) */
0270     f = (f >> 32) + h0 + dctx->s[0]; mac[0] = cpu_to_le32(f);
0271     f = (f >> 32) + h1 + dctx->s[1]; mac[1] = cpu_to_le32(f);
0272     f = (f >> 32) + h2 + dctx->s[2]; mac[2] = cpu_to_le32(f);
0273     f = (f >> 32) + h3 + dctx->s[3]; mac[3] = cpu_to_le32(f);
0274 
0275     return 0;
0276 }
0277 EXPORT_SYMBOL_GPL(crypto_poly1305_final);
0278 
0279 static struct shash_alg poly1305_alg = {
0280     .digestsize = POLY1305_DIGEST_SIZE,
0281     .init       = crypto_poly1305_init,
0282     .update     = crypto_poly1305_update,
0283     .final      = crypto_poly1305_final,
0284     .setkey     = crypto_poly1305_setkey,
0285     .descsize   = sizeof(struct poly1305_desc_ctx),
0286     .base       = {
0287         .cra_name       = "poly1305",
0288         .cra_driver_name    = "poly1305-generic",
0289         .cra_priority       = 100,
0290         .cra_flags      = CRYPTO_ALG_TYPE_SHASH,
0291         .cra_alignmask      = sizeof(u32) - 1,
0292         .cra_blocksize      = POLY1305_BLOCK_SIZE,
0293         .cra_module     = THIS_MODULE,
0294     },
0295 };
0296 
0297 static int __init poly1305_mod_init(void)
0298 {
0299     return crypto_register_shash(&poly1305_alg);
0300 }
0301 
0302 static void __exit poly1305_mod_exit(void)
0303 {
0304     crypto_unregister_shash(&poly1305_alg);
0305 }
0306 
0307 module_init(poly1305_mod_init);
0308 module_exit(poly1305_mod_exit);
0309 
0310 MODULE_LICENSE("GPL");
0311 MODULE_AUTHOR("Martin Willi <martin@strongswan.org>");
0312 MODULE_DESCRIPTION("Poly1305 authenticator");
0313 MODULE_ALIAS_CRYPTO("poly1305");
0314 MODULE_ALIAS_CRYPTO("poly1305-generic");