|
| 1 | +/* SPDX-License-Identifier: GPL-2.0 |
| 2 | + * |
| 3 | + * Copyright (C) 2015-2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved. |
| 4 | + * |
| 5 | + * This is based in part on Andrew Moon's poly1305-donna, which is in the |
| 6 | + * public domain. |
| 7 | + */ |
| 8 | + |
| 9 | +struct poly1305_internal { |
| 10 | + u32 h[5]; |
| 11 | + u32 r[5]; |
| 12 | +}; |
| 13 | + |
| 14 | +static void poly1305_init_generic(void *ctx, const u8 key[16]) |
| 15 | +{ |
| 16 | + struct poly1305_internal *st = (struct poly1305_internal *)ctx; |
| 17 | + |
| 18 | + /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */ |
| 19 | + st->r[0] = (get_unaligned_le32(&key[0])) & 0x3ffffff; |
| 20 | + st->r[1] = (get_unaligned_le32(&key[3]) >> 2) & 0x3ffff03; |
| 21 | + st->r[2] = (get_unaligned_le32(&key[6]) >> 4) & 0x3ffc0ff; |
| 22 | + st->r[3] = (get_unaligned_le32(&key[9]) >> 6) & 0x3f03fff; |
| 23 | + st->r[4] = (get_unaligned_le32(&key[12]) >> 8) & 0x00fffff; |
| 24 | + |
| 25 | + /* h = 0 */ |
| 26 | + st->h[0] = 0; |
| 27 | + st->h[1] = 0; |
| 28 | + st->h[2] = 0; |
| 29 | + st->h[3] = 0; |
| 30 | + st->h[4] = 0; |
| 31 | +} |
| 32 | + |
| 33 | +static void poly1305_blocks_generic(void *ctx, const u8 *input, size_t len, |
| 34 | + const u32 padbit) |
| 35 | +{ |
| 36 | + struct poly1305_internal *st = (struct poly1305_internal *)ctx; |
| 37 | + const u32 hibit = padbit ? (1UL << 24) : 0; |
| 38 | + u32 r0, r1, r2, r3, r4; |
| 39 | + u32 s1, s2, s3, s4; |
| 40 | + u32 h0, h1, h2, h3, h4; |
| 41 | + u64 d0, d1, d2, d3, d4; |
| 42 | + u32 c; |
| 43 | + |
| 44 | + r0 = st->r[0]; |
| 45 | + r1 = st->r[1]; |
| 46 | + r2 = st->r[2]; |
| 47 | + r3 = st->r[3]; |
| 48 | + r4 = st->r[4]; |
| 49 | + |
| 50 | + s1 = r1 * 5; |
| 51 | + s2 = r2 * 5; |
| 52 | + s3 = r3 * 5; |
| 53 | + s4 = r4 * 5; |
| 54 | + |
| 55 | + h0 = st->h[0]; |
| 56 | + h1 = st->h[1]; |
| 57 | + h2 = st->h[2]; |
| 58 | + h3 = st->h[3]; |
| 59 | + h4 = st->h[4]; |
| 60 | + |
| 61 | + while (len >= POLY1305_BLOCK_SIZE) { |
| 62 | + /* h += m[i] */ |
| 63 | + h0 += (get_unaligned_le32(&input[0])) & 0x3ffffff; |
| 64 | + h1 += (get_unaligned_le32(&input[3]) >> 2) & 0x3ffffff; |
| 65 | + h2 += (get_unaligned_le32(&input[6]) >> 4) & 0x3ffffff; |
| 66 | + h3 += (get_unaligned_le32(&input[9]) >> 6) & 0x3ffffff; |
| 67 | + h4 += (get_unaligned_le32(&input[12]) >> 8) | hibit; |
| 68 | + |
| 69 | + /* h *= r */ |
| 70 | + d0 = ((u64)h0 * r0) + ((u64)h1 * s4) + |
| 71 | + ((u64)h2 * s3) + ((u64)h3 * s2) + |
| 72 | + ((u64)h4 * s1); |
| 73 | + d1 = ((u64)h0 * r1) + ((u64)h1 * r0) + |
| 74 | + ((u64)h2 * s4) + ((u64)h3 * s3) + |
| 75 | + ((u64)h4 * s2); |
| 76 | + d2 = ((u64)h0 * r2) + ((u64)h1 * r1) + |
| 77 | + ((u64)h2 * r0) + ((u64)h3 * s4) + |
| 78 | + ((u64)h4 * s3); |
| 79 | + d3 = ((u64)h0 * r3) + ((u64)h1 * r2) + |
| 80 | + ((u64)h2 * r1) + ((u64)h3 * r0) + |
| 81 | + ((u64)h4 * s4); |
| 82 | + d4 = ((u64)h0 * r4) + ((u64)h1 * r3) + |
| 83 | + ((u64)h2 * r2) + ((u64)h3 * r1) + |
| 84 | + ((u64)h4 * r0); |
| 85 | + |
| 86 | + /* (partial) h %= p */ |
| 87 | + c = (u32)(d0 >> 26); |
| 88 | + h0 = (u32)d0 & 0x3ffffff; |
| 89 | + d1 += c; |
| 90 | + c = (u32)(d1 >> 26); |
| 91 | + h1 = (u32)d1 & 0x3ffffff; |
| 92 | + d2 += c; |
| 93 | + c = (u32)(d2 >> 26); |
| 94 | + h2 = (u32)d2 & 0x3ffffff; |
| 95 | + d3 += c; |
| 96 | + c = (u32)(d3 >> 26); |
| 97 | + h3 = (u32)d3 & 0x3ffffff; |
| 98 | + d4 += c; |
| 99 | + c = (u32)(d4 >> 26); |
| 100 | + h4 = (u32)d4 & 0x3ffffff; |
| 101 | + h0 += c * 5; |
| 102 | + c = (h0 >> 26); |
| 103 | + h0 = h0 & 0x3ffffff; |
| 104 | + h1 += c; |
| 105 | + |
| 106 | + input += POLY1305_BLOCK_SIZE; |
| 107 | + len -= POLY1305_BLOCK_SIZE; |
| 108 | + } |
| 109 | + |
| 110 | + st->h[0] = h0; |
| 111 | + st->h[1] = h1; |
| 112 | + st->h[2] = h2; |
| 113 | + st->h[3] = h3; |
| 114 | + st->h[4] = h4; |
| 115 | +} |
| 116 | + |
| 117 | +static void poly1305_emit_generic(void *ctx, u8 mac[16], const u32 nonce[4]) |
| 118 | +{ |
| 119 | + struct poly1305_internal *st = (struct poly1305_internal *)ctx; |
| 120 | + u32 h0, h1, h2, h3, h4, c; |
| 121 | + u32 g0, g1, g2, g3, g4; |
| 122 | + u64 f; |
| 123 | + u32 mask; |
| 124 | + |
| 125 | + /* fully carry h */ |
| 126 | + h0 = st->h[0]; |
| 127 | + h1 = st->h[1]; |
| 128 | + h2 = st->h[2]; |
| 129 | + h3 = st->h[3]; |
| 130 | + h4 = st->h[4]; |
| 131 | + |
| 132 | + c = h1 >> 26; |
| 133 | + h1 = h1 & 0x3ffffff; |
| 134 | + h2 += c; |
| 135 | + c = h2 >> 26; |
| 136 | + h2 = h2 & 0x3ffffff; |
| 137 | + h3 += c; |
| 138 | + c = h3 >> 26; |
| 139 | + h3 = h3 & 0x3ffffff; |
| 140 | + h4 += c; |
| 141 | + c = h4 >> 26; |
| 142 | + h4 = h4 & 0x3ffffff; |
| 143 | + h0 += c * 5; |
| 144 | + c = h0 >> 26; |
| 145 | + h0 = h0 & 0x3ffffff; |
| 146 | + h1 += c; |
| 147 | + |
| 148 | + /* compute h + -p */ |
| 149 | + g0 = h0 + 5; |
| 150 | + c = g0 >> 26; |
| 151 | + g0 &= 0x3ffffff; |
| 152 | + g1 = h1 + c; |
| 153 | + c = g1 >> 26; |
| 154 | + g1 &= 0x3ffffff; |
| 155 | + g2 = h2 + c; |
| 156 | + c = g2 >> 26; |
| 157 | + g2 &= 0x3ffffff; |
| 158 | + g3 = h3 + c; |
| 159 | + c = g3 >> 26; |
| 160 | + g3 &= 0x3ffffff; |
| 161 | + g4 = h4 + c - (1UL << 26); |
| 162 | + |
| 163 | + /* select h if h < p, or h + -p if h >= p */ |
| 164 | + mask = (g4 >> ((sizeof(u32) * 8) - 1)) - 1; |
| 165 | + g0 &= mask; |
| 166 | + g1 &= mask; |
| 167 | + g2 &= mask; |
| 168 | + g3 &= mask; |
| 169 | + g4 &= mask; |
| 170 | + mask = ~mask; |
| 171 | + |
| 172 | + h0 = (h0 & mask) | g0; |
| 173 | + h1 = (h1 & mask) | g1; |
| 174 | + h2 = (h2 & mask) | g2; |
| 175 | + h3 = (h3 & mask) | g3; |
| 176 | + h4 = (h4 & mask) | g4; |
| 177 | + |
| 178 | + /* h = h % (2^128) */ |
| 179 | + h0 = ((h0) | (h1 << 26)) & 0xffffffff; |
| 180 | + h1 = ((h1 >> 6) | (h2 << 20)) & 0xffffffff; |
| 181 | + h2 = ((h2 >> 12) | (h3 << 14)) & 0xffffffff; |
| 182 | + h3 = ((h3 >> 18) | (h4 << 8)) & 0xffffffff; |
| 183 | + |
| 184 | + /* mac = (h + nonce) % (2^128) */ |
| 185 | + f = (u64)h0 + nonce[0]; |
| 186 | + h0 = (u32)f; |
| 187 | + f = (u64)h1 + nonce[1] + (f >> 32); |
| 188 | + h1 = (u32)f; |
| 189 | + f = (u64)h2 + nonce[2] + (f >> 32); |
| 190 | + h2 = (u32)f; |
| 191 | + f = (u64)h3 + nonce[3] + (f >> 32); |
| 192 | + h3 = (u32)f; |
| 193 | + |
| 194 | + put_unaligned_le32(h0, &mac[0]); |
| 195 | + put_unaligned_le32(h1, &mac[4]); |
| 196 | + put_unaligned_le32(h2, &mac[8]); |
| 197 | + put_unaligned_le32(h3, &mac[12]); |
| 198 | +} |
0 commit comments