~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/crypto/poly1305_generic.c

Version: ~ [ linux-6.11-rc3 ] ~ [ linux-6.10.4 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.45 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.104 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.164 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.223 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.281 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.319 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /crypto/poly1305_generic.c (Version linux-6.11-rc3) and /crypto/poly1305_generic.c (Version linux-4.9.337)


  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/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>                     << 
 21                                                    20 
 22 static int crypto_poly1305_init(struct shash_d !!  21 static inline u64 mlt(u64 a, u64 b)
                                                   >>  22 {
                                                   >>  23         return a * b;
                                                   >>  24 }
                                                   >>  25 
                                                   >>  26 static inline u32 sr(u64 v, u_char n)
                                                   >>  27 {
                                                   >>  28         return v >> n;
                                                   >>  29 }
                                                   >>  30 
                                                   >>  31 static inline u32 and(u32 v, u32 mask)
                                                   >>  32 {
                                                   >>  33         return v & mask;
                                                   >>  34 }
                                                   >>  35 
                                                   >>  36 static inline u32 le32_to_cpuvp(const void *p)
                                                   >>  37 {
                                                   >>  38         return le32_to_cpup(p);
                                                   >>  39 }
                                                   >>  40 
                                                   >>  41 int crypto_poly1305_init(struct shash_desc *desc)
 23 {                                                  42 {
 24         struct poly1305_desc_ctx *dctx = shash     43         struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
 25                                                    44 
 26         poly1305_core_init(&dctx->h);          !!  45         memset(dctx->h, 0, sizeof(dctx->h));
 27         dctx->buflen = 0;                          46         dctx->buflen = 0;
 28         dctx->rset = 0;                        !!  47         dctx->rset = false;
 29         dctx->sset = false;                        48         dctx->sset = false;
 30                                                    49 
 31         return 0;                                  50         return 0;
 32 }                                                  51 }
                                                   >>  52 EXPORT_SYMBOL_GPL(crypto_poly1305_init);
 33                                                    53 
 34 static unsigned int crypto_poly1305_setdesckey !!  54 static void poly1305_setrkey(struct poly1305_desc_ctx *dctx, const u8 *key)
 35                                                !!  55 {
                                                   >>  56         /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */
                                                   >>  57         dctx->r[0] = (le32_to_cpuvp(key +  0) >> 0) & 0x3ffffff;
                                                   >>  58         dctx->r[1] = (le32_to_cpuvp(key +  3) >> 2) & 0x3ffff03;
                                                   >>  59         dctx->r[2] = (le32_to_cpuvp(key +  6) >> 4) & 0x3ffc0ff;
                                                   >>  60         dctx->r[3] = (le32_to_cpuvp(key +  9) >> 6) & 0x3f03fff;
                                                   >>  61         dctx->r[4] = (le32_to_cpuvp(key + 12) >> 8) & 0x00fffff;
                                                   >>  62 }
                                                   >>  63 
                                                   >>  64 static void poly1305_setskey(struct poly1305_desc_ctx *dctx, const u8 *key)
                                                   >>  65 {
                                                   >>  66         dctx->s[0] = le32_to_cpuvp(key +  0);
                                                   >>  67         dctx->s[1] = le32_to_cpuvp(key +  4);
                                                   >>  68         dctx->s[2] = le32_to_cpuvp(key +  8);
                                                   >>  69         dctx->s[3] = le32_to_cpuvp(key + 12);
                                                   >>  70 }
                                                   >>  71 
                                                   >>  72 /*
                                                   >>  73  * Poly1305 requires a unique key for each tag, which implies that we can't set
                                                   >>  74  * it on the tfm that gets accessed by multiple users simultaneously. Instead we
                                                   >>  75  * expect the key as the first 32 bytes in the update() call.
                                                   >>  76  */
                                                   >>  77 unsigned int crypto_poly1305_setdesckey(struct poly1305_desc_ctx *dctx,
                                                   >>  78                                         const u8 *src, unsigned int srclen)
 36 {                                                  79 {
 37         if (!dctx->sset) {                         80         if (!dctx->sset) {
 38                 if (!dctx->rset && srclen >= P     81                 if (!dctx->rset && srclen >= POLY1305_BLOCK_SIZE) {
 39                         poly1305_core_setkey(& !!  82                         poly1305_setrkey(dctx, src);
 40                         src += POLY1305_BLOCK_     83                         src += POLY1305_BLOCK_SIZE;
 41                         srclen -= POLY1305_BLO     84                         srclen -= POLY1305_BLOCK_SIZE;
 42                         dctx->rset = 2;        !!  85                         dctx->rset = true;
 43                 }                                  86                 }
 44                 if (srclen >= POLY1305_BLOCK_S     87                 if (srclen >= POLY1305_BLOCK_SIZE) {
 45                         dctx->s[0] = get_unali !!  88                         poly1305_setskey(dctx, src);
 46                         dctx->s[1] = get_unali << 
 47                         dctx->s[2] = get_unali << 
 48                         dctx->s[3] = get_unali << 
 49                         src += POLY1305_BLOCK_     89                         src += POLY1305_BLOCK_SIZE;
 50                         srclen -= POLY1305_BLO     90                         srclen -= POLY1305_BLOCK_SIZE;
 51                         dctx->sset = true;         91                         dctx->sset = true;
 52                 }                                  92                 }
 53         }                                          93         }
 54         return srclen;                             94         return srclen;
 55 }                                                  95 }
                                                   >>  96 EXPORT_SYMBOL_GPL(crypto_poly1305_setdesckey);
 56                                                    97 
 57 static void poly1305_blocks(struct poly1305_de !!  98 static unsigned int poly1305_blocks(struct poly1305_desc_ctx *dctx,
 58                             unsigned int srcle !!  99                                     const u8 *src, unsigned int srclen,
                                                   >> 100                                     u32 hibit)
 59 {                                                 101 {
                                                   >> 102         u32 r0, r1, r2, r3, r4;
                                                   >> 103         u32 s1, s2, s3, s4;
                                                   >> 104         u32 h0, h1, h2, h3, h4;
                                                   >> 105         u64 d0, d1, d2, d3, d4;
 60         unsigned int datalen;                     106         unsigned int datalen;
 61                                                   107 
 62         if (unlikely(!dctx->sset)) {              108         if (unlikely(!dctx->sset)) {
 63                 datalen = crypto_poly1305_setd    109                 datalen = crypto_poly1305_setdesckey(dctx, src, srclen);
 64                 src += srclen - datalen;          110                 src += srclen - datalen;
 65                 srclen = datalen;                 111                 srclen = datalen;
 66         }                                         112         }
 67                                                   113 
 68         poly1305_core_blocks(&dctx->h, &dctx-> !! 114         r0 = dctx->r[0];
 69                              srclen / POLY1305 !! 115         r1 = dctx->r[1];
                                                   >> 116         r2 = dctx->r[2];
                                                   >> 117         r3 = dctx->r[3];
                                                   >> 118         r4 = dctx->r[4];
                                                   >> 119 
                                                   >> 120         s1 = r1 * 5;
                                                   >> 121         s2 = r2 * 5;
                                                   >> 122         s3 = r3 * 5;
                                                   >> 123         s4 = r4 * 5;
                                                   >> 124 
                                                   >> 125         h0 = dctx->h[0];
                                                   >> 126         h1 = dctx->h[1];
                                                   >> 127         h2 = dctx->h[2];
                                                   >> 128         h3 = dctx->h[3];
                                                   >> 129         h4 = dctx->h[4];
                                                   >> 130 
                                                   >> 131         while (likely(srclen >= POLY1305_BLOCK_SIZE)) {
                                                   >> 132 
                                                   >> 133                 /* h += m[i] */
                                                   >> 134                 h0 += (le32_to_cpuvp(src +  0) >> 0) & 0x3ffffff;
                                                   >> 135                 h1 += (le32_to_cpuvp(src +  3) >> 2) & 0x3ffffff;
                                                   >> 136                 h2 += (le32_to_cpuvp(src +  6) >> 4) & 0x3ffffff;
                                                   >> 137                 h3 += (le32_to_cpuvp(src +  9) >> 6) & 0x3ffffff;
                                                   >> 138                 h4 += (le32_to_cpuvp(src + 12) >> 8) | hibit;
                                                   >> 139 
                                                   >> 140                 /* h *= r */
                                                   >> 141                 d0 = mlt(h0, r0) + mlt(h1, s4) + mlt(h2, s3) +
                                                   >> 142                      mlt(h3, s2) + mlt(h4, s1);
                                                   >> 143                 d1 = mlt(h0, r1) + mlt(h1, r0) + mlt(h2, s4) +
                                                   >> 144                      mlt(h3, s3) + mlt(h4, s2);
                                                   >> 145                 d2 = mlt(h0, r2) + mlt(h1, r1) + mlt(h2, r0) +
                                                   >> 146                      mlt(h3, s4) + mlt(h4, s3);
                                                   >> 147                 d3 = mlt(h0, r3) + mlt(h1, r2) + mlt(h2, r1) +
                                                   >> 148                      mlt(h3, r0) + mlt(h4, s4);
                                                   >> 149                 d4 = mlt(h0, r4) + mlt(h1, r3) + mlt(h2, r2) +
                                                   >> 150                      mlt(h3, r1) + mlt(h4, r0);
                                                   >> 151 
                                                   >> 152                 /* (partial) h %= p */
                                                   >> 153                 d1 += sr(d0, 26);     h0 = and(d0, 0x3ffffff);
                                                   >> 154                 d2 += sr(d1, 26);     h1 = and(d1, 0x3ffffff);
                                                   >> 155                 d3 += sr(d2, 26);     h2 = and(d2, 0x3ffffff);
                                                   >> 156                 d4 += sr(d3, 26);     h3 = and(d3, 0x3ffffff);
                                                   >> 157                 h0 += sr(d4, 26) * 5; h4 = and(d4, 0x3ffffff);
                                                   >> 158                 h1 += h0 >> 26;       h0 = h0 & 0x3ffffff;
                                                   >> 159 
                                                   >> 160                 src += POLY1305_BLOCK_SIZE;
                                                   >> 161                 srclen -= POLY1305_BLOCK_SIZE;
                                                   >> 162         }
                                                   >> 163 
                                                   >> 164         dctx->h[0] = h0;
                                                   >> 165         dctx->h[1] = h1;
                                                   >> 166         dctx->h[2] = h2;
                                                   >> 167         dctx->h[3] = h3;
                                                   >> 168         dctx->h[4] = h4;
                                                   >> 169 
                                                   >> 170         return srclen;
 70 }                                                 171 }
 71                                                   172 
 72 static int crypto_poly1305_update(struct shash !! 173 int crypto_poly1305_update(struct shash_desc *desc,
 73                                   const u8 *sr !! 174                            const u8 *src, unsigned int srclen)
 74 {                                                 175 {
 75         struct poly1305_desc_ctx *dctx = shash    176         struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
 76         unsigned int bytes;                       177         unsigned int bytes;
 77                                                   178 
 78         if (unlikely(dctx->buflen)) {             179         if (unlikely(dctx->buflen)) {
 79                 bytes = min(srclen, POLY1305_B    180                 bytes = min(srclen, POLY1305_BLOCK_SIZE - dctx->buflen);
 80                 memcpy(dctx->buf + dctx->bufle    181                 memcpy(dctx->buf + dctx->buflen, src, bytes);
 81                 src += bytes;                     182                 src += bytes;
 82                 srclen -= bytes;                  183                 srclen -= bytes;
 83                 dctx->buflen += bytes;            184                 dctx->buflen += bytes;
 84                                                   185 
 85                 if (dctx->buflen == POLY1305_B    186                 if (dctx->buflen == POLY1305_BLOCK_SIZE) {
 86                         poly1305_blocks(dctx,     187                         poly1305_blocks(dctx, dctx->buf,
 87                                         POLY13 !! 188                                         POLY1305_BLOCK_SIZE, 1 << 24);
 88                         dctx->buflen = 0;         189                         dctx->buflen = 0;
 89                 }                                 190                 }
 90         }                                         191         }
 91                                                   192 
 92         if (likely(srclen >= POLY1305_BLOCK_SI    193         if (likely(srclen >= POLY1305_BLOCK_SIZE)) {
 93                 poly1305_blocks(dctx, src, src !! 194                 bytes = poly1305_blocks(dctx, src, srclen, 1 << 24);
 94                 src += srclen - (srclen % POLY !! 195                 src += srclen - bytes;
 95                 srclen %= POLY1305_BLOCK_SIZE; !! 196                 srclen = bytes;
 96         }                                         197         }
 97                                                   198 
 98         if (unlikely(srclen)) {                   199         if (unlikely(srclen)) {
 99                 dctx->buflen = srclen;            200                 dctx->buflen = srclen;
100                 memcpy(dctx->buf, src, srclen)    201                 memcpy(dctx->buf, src, srclen);
101         }                                         202         }
102                                                   203 
103         return 0;                                 204         return 0;
104 }                                                 205 }
                                                   >> 206 EXPORT_SYMBOL_GPL(crypto_poly1305_update);
105                                                   207 
106 static int crypto_poly1305_final(struct shash_ !! 208 int crypto_poly1305_final(struct shash_desc *desc, u8 *dst)
107 {                                                 209 {
108         struct poly1305_desc_ctx *dctx = shash    210         struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
                                                   >> 211         __le32 *mac = (__le32 *)dst;
                                                   >> 212         u32 h0, h1, h2, h3, h4;
                                                   >> 213         u32 g0, g1, g2, g3, g4;
                                                   >> 214         u32 mask;
                                                   >> 215         u64 f = 0;
109                                                   216 
110         if (unlikely(!dctx->sset))                217         if (unlikely(!dctx->sset))
111                 return -ENOKEY;                   218                 return -ENOKEY;
112                                                   219 
113         poly1305_final_generic(dctx, dst);     !! 220         if (unlikely(dctx->buflen)) {
                                                   >> 221                 dctx->buf[dctx->buflen++] = 1;
                                                   >> 222                 memset(dctx->buf + dctx->buflen, 0,
                                                   >> 223                        POLY1305_BLOCK_SIZE - dctx->buflen);
                                                   >> 224                 poly1305_blocks(dctx, dctx->buf, POLY1305_BLOCK_SIZE, 0);
                                                   >> 225         }
                                                   >> 226 
                                                   >> 227         /* fully carry h */
                                                   >> 228         h0 = dctx->h[0];
                                                   >> 229         h1 = dctx->h[1];
                                                   >> 230         h2 = dctx->h[2];
                                                   >> 231         h3 = dctx->h[3];
                                                   >> 232         h4 = dctx->h[4];
                                                   >> 233 
                                                   >> 234         h2 += (h1 >> 26);     h1 = h1 & 0x3ffffff;
                                                   >> 235         h3 += (h2 >> 26);     h2 = h2 & 0x3ffffff;
                                                   >> 236         h4 += (h3 >> 26);     h3 = h3 & 0x3ffffff;
                                                   >> 237         h0 += (h4 >> 26) * 5; h4 = h4 & 0x3ffffff;
                                                   >> 238         h1 += (h0 >> 26);     h0 = h0 & 0x3ffffff;
                                                   >> 239 
                                                   >> 240         /* compute h + -p */
                                                   >> 241         g0 = h0 + 5;
                                                   >> 242         g1 = h1 + (g0 >> 26);             g0 &= 0x3ffffff;
                                                   >> 243         g2 = h2 + (g1 >> 26);             g1 &= 0x3ffffff;
                                                   >> 244         g3 = h3 + (g2 >> 26);             g2 &= 0x3ffffff;
                                                   >> 245         g4 = h4 + (g3 >> 26) - (1 << 26); g3 &= 0x3ffffff;
                                                   >> 246 
                                                   >> 247         /* select h if h < p, or h + -p if h >= p */
                                                   >> 248         mask = (g4 >> ((sizeof(u32) * 8) - 1)) - 1;
                                                   >> 249         g0 &= mask;
                                                   >> 250         g1 &= mask;
                                                   >> 251         g2 &= mask;
                                                   >> 252         g3 &= mask;
                                                   >> 253         g4 &= mask;
                                                   >> 254         mask = ~mask;
                                                   >> 255         h0 = (h0 & mask) | g0;
                                                   >> 256         h1 = (h1 & mask) | g1;
                                                   >> 257         h2 = (h2 & mask) | g2;
                                                   >> 258         h3 = (h3 & mask) | g3;
                                                   >> 259         h4 = (h4 & mask) | g4;
                                                   >> 260 
                                                   >> 261         /* h = h % (2^128) */
                                                   >> 262         h0 = (h0 >>  0) | (h1 << 26);
                                                   >> 263         h1 = (h1 >>  6) | (h2 << 20);
                                                   >> 264         h2 = (h2 >> 12) | (h3 << 14);
                                                   >> 265         h3 = (h3 >> 18) | (h4 <<  8);
                                                   >> 266 
                                                   >> 267         /* mac = (h + s) % (2^128) */
                                                   >> 268         f = (f >> 32) + h0 + dctx->s[0]; mac[0] = cpu_to_le32(f);
                                                   >> 269         f = (f >> 32) + h1 + dctx->s[1]; mac[1] = cpu_to_le32(f);
                                                   >> 270         f = (f >> 32) + h2 + dctx->s[2]; mac[2] = cpu_to_le32(f);
                                                   >> 271         f = (f >> 32) + h3 + dctx->s[3]; mac[3] = cpu_to_le32(f);
                                                   >> 272 
114         return 0;                                 273         return 0;
115 }                                                 274 }
                                                   >> 275 EXPORT_SYMBOL_GPL(crypto_poly1305_final);
116                                                   276 
117 static struct shash_alg poly1305_alg = {          277 static struct shash_alg poly1305_alg = {
118         .digestsize     = POLY1305_DIGEST_SIZE    278         .digestsize     = POLY1305_DIGEST_SIZE,
119         .init           = crypto_poly1305_init    279         .init           = crypto_poly1305_init,
120         .update         = crypto_poly1305_upda    280         .update         = crypto_poly1305_update,
121         .final          = crypto_poly1305_fina    281         .final          = crypto_poly1305_final,
122         .descsize       = sizeof(struct poly13    282         .descsize       = sizeof(struct poly1305_desc_ctx),
123         .base           = {                       283         .base           = {
124                 .cra_name               = "pol    284                 .cra_name               = "poly1305",
125                 .cra_driver_name        = "pol    285                 .cra_driver_name        = "poly1305-generic",
126                 .cra_priority           = 100,    286                 .cra_priority           = 100,
                                                   >> 287                 .cra_flags              = CRYPTO_ALG_TYPE_SHASH,
                                                   >> 288                 .cra_alignmask          = sizeof(u32) - 1,
127                 .cra_blocksize          = POLY    289                 .cra_blocksize          = POLY1305_BLOCK_SIZE,
128                 .cra_module             = THIS    290                 .cra_module             = THIS_MODULE,
129         },                                        291         },
130 };                                                292 };
131                                                   293 
132 static int __init poly1305_mod_init(void)         294 static int __init poly1305_mod_init(void)
133 {                                                 295 {
134         return crypto_register_shash(&poly1305    296         return crypto_register_shash(&poly1305_alg);
135 }                                                 297 }
136                                                   298 
137 static void __exit poly1305_mod_exit(void)        299 static void __exit poly1305_mod_exit(void)
138 {                                                 300 {
139         crypto_unregister_shash(&poly1305_alg)    301         crypto_unregister_shash(&poly1305_alg);
140 }                                                 302 }
141                                                   303 
142 subsys_initcall(poly1305_mod_init);            !! 304 module_init(poly1305_mod_init);
143 module_exit(poly1305_mod_exit);                   305 module_exit(poly1305_mod_exit);
144                                                   306 
145 MODULE_LICENSE("GPL");                            307 MODULE_LICENSE("GPL");
146 MODULE_AUTHOR("Martin Willi <martin@strongswan    308 MODULE_AUTHOR("Martin Willi <martin@strongswan.org>");
147 MODULE_DESCRIPTION("Poly1305 authenticator");     309 MODULE_DESCRIPTION("Poly1305 authenticator");
148 MODULE_ALIAS_CRYPTO("poly1305");                  310 MODULE_ALIAS_CRYPTO("poly1305");
149 MODULE_ALIAS_CRYPTO("poly1305-generic");          311 MODULE_ALIAS_CRYPTO("poly1305-generic");
150                                                   312 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php