|
| 1 | +#include <u.h> |
| 2 | +#include <libc.h> |
| 3 | +#include <libsec.h> |
| 4 | + |
| 5 | +/* |
| 6 | + poly1305 implementation using 32 bit * 32 bit = 64 bit multiplication and 64 bit addition |
| 7 | +
|
| 8 | + derived from http://github.com/floodberry/poly1305-donna |
| 9 | +*/ |
| 10 | + |
| 11 | +#define U8TO32(p) ((u32int)(p)[0] | (u32int)(p)[1]<<8 | (u32int)(p)[2]<<16 | (u32int)(p)[3]<<24) |
| 12 | +#define U32TO8(p, v) (p)[0]=(v), (p)[1]=(v)>>8, (p)[2]=(v)>>16, (p)[3]=(v)>>24 |
| 13 | + |
| 14 | +/* (r,s) = (key[0:15],key[16:31]), the one time key */ |
| 15 | +DigestState* |
| 16 | +poly1305(uchar *m, ulong len, uchar *key, ulong klen, uchar *digest, DigestState *s) |
| 17 | +{ |
| 18 | + u32int r0,r1,r2,r3,r4, s1,s2,s3,s4, h0,h1,h2,h3,h4, g0,g1,g2,g3,g4; |
| 19 | + u64int d0,d1,d2,d3,d4, f; |
| 20 | + u32int hibit, mask, c; |
| 21 | + |
| 22 | + if(s == nil){ |
| 23 | + s = malloc(sizeof(*s)); |
| 24 | + if(s == nil) |
| 25 | + return nil; |
| 26 | + memset(s, 0, sizeof(*s)); |
| 27 | + s->malloced = 1; |
| 28 | + } |
| 29 | + |
| 30 | + if(s->seeded == 0){ |
| 31 | + assert(klen == 32); |
| 32 | + |
| 33 | + /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */ |
| 34 | + s->state[0] = (U8TO32(&key[ 0]) ) & 0x3ffffff; |
| 35 | + s->state[1] = (U8TO32(&key[ 3]) >> 2) & 0x3ffff03; |
| 36 | + s->state[2] = (U8TO32(&key[ 6]) >> 4) & 0x3ffc0ff; |
| 37 | + s->state[3] = (U8TO32(&key[ 9]) >> 6) & 0x3f03fff; |
| 38 | + s->state[4] = (U8TO32(&key[12]) >> 8) & 0x00fffff; |
| 39 | + |
| 40 | + /* h = 0 */ |
| 41 | + s->state[5] = 0; |
| 42 | + s->state[6] = 0; |
| 43 | + s->state[7] = 0; |
| 44 | + s->state[8] = 0; |
| 45 | + s->state[9] = 0; |
| 46 | + |
| 47 | + /* save pad for later */ |
| 48 | + s->state[10] = U8TO32(&key[16]); |
| 49 | + s->state[11] = U8TO32(&key[20]); |
| 50 | + s->state[12] = U8TO32(&key[24]); |
| 51 | + s->state[13] = U8TO32(&key[28]); |
| 52 | + |
| 53 | + s->seeded = 1; |
| 54 | + } |
| 55 | + |
| 56 | + if(s->blen){ |
| 57 | + c = 16 - s->blen; |
| 58 | + if(c > len) |
| 59 | + c = len; |
| 60 | + memmove(s->buf + s->blen, m, c); |
| 61 | + len -= c, m += c; |
| 62 | + s->blen += c; |
| 63 | + if(s->blen == 16){ |
| 64 | + s->blen = 0; |
| 65 | + poly1305(s->buf, 16, key, klen, nil, s); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + r0 = s->state[0]; |
| 70 | + r1 = s->state[1]; |
| 71 | + r2 = s->state[2]; |
| 72 | + r3 = s->state[3]; |
| 73 | + r4 = s->state[4]; |
| 74 | + |
| 75 | + h0 = s->state[5]; |
| 76 | + h1 = s->state[6]; |
| 77 | + h2 = s->state[7]; |
| 78 | + h3 = s->state[8]; |
| 79 | + h4 = s->state[9]; |
| 80 | + |
| 81 | + s1 = r1 * 5; |
| 82 | + s2 = r2 * 5; |
| 83 | + s3 = r3 * 5; |
| 84 | + s4 = r4 * 5; |
| 85 | + |
| 86 | + hibit = 1<<24; /* 1<<128 */ |
| 87 | + |
| 88 | + while(len >= 16){ |
| 89 | +Block: |
| 90 | + /* h += m[i] */ |
| 91 | + h0 += (U8TO32(&m[0]) ) & 0x3ffffff; |
| 92 | + h1 += (U8TO32(&m[3]) >> 2) & 0x3ffffff; |
| 93 | + h2 += (U8TO32(&m[6]) >> 4) & 0x3ffffff; |
| 94 | + h3 += (U8TO32(&m[9]) >> 6) & 0x3ffffff; |
| 95 | + h4 += (U8TO32(&m[12])>> 8) | hibit; |
| 96 | + |
| 97 | + /* h *= r */ |
| 98 | + d0 = ((u64int)h0 * r0) + ((u64int)h1 * s4) + ((u64int)h2 * s3) + ((u64int)h3 * s2) + ((u64int)h4 * s1); |
| 99 | + d1 = ((u64int)h0 * r1) + ((u64int)h1 * r0) + ((u64int)h2 * s4) + ((u64int)h3 * s3) + ((u64int)h4 * s2); |
| 100 | + d2 = ((u64int)h0 * r2) + ((u64int)h1 * r1) + ((u64int)h2 * r0) + ((u64int)h3 * s4) + ((u64int)h4 * s3); |
| 101 | + d3 = ((u64int)h0 * r3) + ((u64int)h1 * r2) + ((u64int)h2 * r1) + ((u64int)h3 * r0) + ((u64int)h4 * s4); |
| 102 | + d4 = ((u64int)h0 * r4) + ((u64int)h1 * r3) + ((u64int)h2 * r2) + ((u64int)h3 * r1) + ((u64int)h4 * r0); |
| 103 | + |
| 104 | + /* (partial) h %= p */ |
| 105 | + c = (u32int)(d0 >> 26); h0 = (u32int)d0 & 0x3ffffff; |
| 106 | + d1 += c; c = (u32int)(d1 >> 26); h1 = (u32int)d1 & 0x3ffffff; |
| 107 | + d2 += c; c = (u32int)(d2 >> 26); h2 = (u32int)d2 & 0x3ffffff; |
| 108 | + d3 += c; c = (u32int)(d3 >> 26); h3 = (u32int)d3 & 0x3ffffff; |
| 109 | + d4 += c; c = (u32int)(d4 >> 26); h4 = (u32int)d4 & 0x3ffffff; |
| 110 | + h0 += c * 5; c = (h0 >> 26); h0 = h0 & 0x3ffffff; |
| 111 | + h1 += c; |
| 112 | + |
| 113 | + len -= 16, m += 16; |
| 114 | + } |
| 115 | + |
| 116 | + if(len){ |
| 117 | + s->blen = len; |
| 118 | + memmove(s->buf, m, len); |
| 119 | + } |
| 120 | + |
| 121 | + if(digest == nil){ |
| 122 | + s->state[5] = h0; |
| 123 | + s->state[6] = h1; |
| 124 | + s->state[7] = h2; |
| 125 | + s->state[8] = h3; |
| 126 | + s->state[9] = h4; |
| 127 | + return s; |
| 128 | + } |
| 129 | + |
| 130 | + if(len){ |
| 131 | + m = s->buf; |
| 132 | + m[len++] = 1; |
| 133 | + while(len < 16) |
| 134 | + m[len++] = 0; |
| 135 | + hibit = 0; |
| 136 | + goto Block; |
| 137 | + } |
| 138 | + |
| 139 | + c = h1 >> 26; h1 = h1 & 0x3ffffff; |
| 140 | + h2 += c; c = h2 >> 26; h2 = h2 & 0x3ffffff; |
| 141 | + h3 += c; c = h3 >> 26; h3 = h3 & 0x3ffffff; |
| 142 | + h4 += c; c = h4 >> 26; h4 = h4 & 0x3ffffff; |
| 143 | + h0 += c * 5; c = h0 >> 26; h0 = h0 & 0x3ffffff; |
| 144 | + h1 += c; |
| 145 | + |
| 146 | + /* compute h + -p */ |
| 147 | + g0 = h0 + 5; c = g0 >> 26; g0 &= 0x3ffffff; |
| 148 | + g1 = h1 + c; c = g1 >> 26; g1 &= 0x3ffffff; |
| 149 | + g2 = h2 + c; c = g2 >> 26; g2 &= 0x3ffffff; |
| 150 | + g3 = h3 + c; c = g3 >> 26; g3 &= 0x3ffffff; |
| 151 | + g4 = h4 + c - (1 << 26); |
| 152 | + |
| 153 | + /* select h if h < p, or h + -p if h >= p */ |
| 154 | + mask = (g4 >> 31) - 1; |
| 155 | + g0 &= mask; |
| 156 | + g1 &= mask; |
| 157 | + g2 &= mask; |
| 158 | + g3 &= mask; |
| 159 | + g4 &= mask; |
| 160 | + mask = ~mask; |
| 161 | + h0 = (h0 & mask) | g0; |
| 162 | + h1 = (h1 & mask) | g1; |
| 163 | + h2 = (h2 & mask) | g2; |
| 164 | + h3 = (h3 & mask) | g3; |
| 165 | + h4 = (h4 & mask) | g4; |
| 166 | + |
| 167 | + /* h = h % (2^128) */ |
| 168 | + h0 = (h0 ) | (h1 << 26); |
| 169 | + h1 = (h1 >> 6) | (h2 << 20); |
| 170 | + h2 = (h2 >> 12) | (h3 << 14); |
| 171 | + h3 = (h3 >> 18) | (h4 << 8); |
| 172 | + |
| 173 | + /* digest = (h + pad) % (2^128) */ |
| 174 | + f = (u64int)h0 + s->state[10] ; h0 = (u32int)f; |
| 175 | + f = (u64int)h1 + s->state[11] + (f >> 32); h1 = (u32int)f; |
| 176 | + f = (u64int)h2 + s->state[12] + (f >> 32); h2 = (u32int)f; |
| 177 | + f = (u64int)h3 + s->state[13] + (f >> 32); h3 = (u32int)f; |
| 178 | + |
| 179 | + U32TO8(&digest[0], h0); |
| 180 | + U32TO8(&digest[4], h1); |
| 181 | + U32TO8(&digest[8], h2); |
| 182 | + U32TO8(&digest[12], h3); |
| 183 | + |
| 184 | + if(s->malloced){ |
| 185 | + memset(s, 0, sizeof(*s)); |
| 186 | + free(s); |
| 187 | + return nil; |
| 188 | + } |
| 189 | + |
| 190 | + memset(s, 0, sizeof(*s)); |
| 191 | + return nil; |
| 192 | +} |
0 commit comments