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