1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
|
#include <stdint.h> #include <stddef.h> #include <string.h>
static inline uint32_t load32_le(const void *p){ const uint8_t *b=(const uint8_t*)p; return (uint32_t)b[0] | ((uint32_t)b[1]<<8) | ((uint32_t)b[2]<<16) | ((uint32_t)b[3]<<24); } static inline uint64_t load64_le(const void *p){ const uint8_t *b=(const uint8_t*)p; return (uint64_t)load32_le(b) | ((uint64_t)load32_le(b+4)<<32); } static inline void store32_le(void *p,uint32_t v){ uint8_t *b=(uint8_t*)p; b[0]=(uint8_t)v; b[1]=(uint8_t)(v>>8); b[2]=(uint8_t)(v>>16); b[3]=(uint8_t)(v>>24); } static inline void store64_le(void *p,uint64_t v){ store32_le(p,(uint32_t)v); store32_le((uint8_t*)p+4,(uint32_t)(v>>32)); } static inline uint32_t rotl32(uint32_t x,int n){ return (x<<n) | (x>>(32-n)); }
static int ct_mem_eq(const void *a,const void *b,size_t n){ const uint8_t *x=(const uint8_t*)a, *y=(const uint8_t*)b; uint32_t d=0; for(size_t i=0;i<n;i++) d |= (uint32_t)(x[i]^y[i]); d |= d>>16; d |= d>>8; return (int)((d^0xFF)==0xFF); }
static void chacha20_block(uint8_t out[64], const uint8_t key[32], uint32_t counter, const uint8_t nonce[12]){ static const uint32_t C[4]={0x61707865,0x3320646e,0x79622d32,0x6b206574}; uint32_t s[16]; s[0]=C[0]; s[1]=C[1]; s[2]=C[2]; s[3]=C[3]; for(int i=0;i<8;i++) s[4+i]=load32_le(key+4*i); s[12]=counter; s[13]=load32_le(nonce+0); s[14]=load32_le(nonce+4); s[15]=load32_le(nonce+8);
uint32_t w[16]; for(int i=0;i<16;i++) w[i]=s[i];
#define QR(a,b,c,d) \ a+=b; d^=a; d=rotl32(d,16); \ c+=d; b^=c; b=rotl32(b,12); \ a+=b; d^=a; d=rotl32(d,8); \ c+=d; b^=c; b=rotl32(b,7);
for(int i=0;i<10;i++){ QR(w[0],w[4],w[8],w[12]); QR(w[1],w[5],w[9],w[13]); QR(w[2],w[6],w[10],w[14]); QR(w[3],w[7],w[11],w[15]); QR(w[0],w[5],w[10],w[15]); QR(w[1],w[6],w[11],w[12]); QR(w[2],w[7],w[8],w[13]); QR(w[3],w[4],w[9],w[14]); } #undef QR
for(int i=0;i<16;i++) w[i]+=s[i]; for(int i=0;i<16;i++) store32_le(out+4*i,w[i]); }
static void chacha20_xor(uint8_t *out, const uint8_t *in, size_t len, const uint8_t key[32], const uint8_t nonce[12], uint32_t counter){ uint8_t block[64]; size_t off=0; while(len){ chacha20_block(block,key,counter,nonce); counter++; size_t n = len>64?64:len; for(size_t i=0;i<n;i++) out[off+i] = in[off+i] ^ block[i]; off+=n; len-=n; } }
typedef struct { uint32_t r[5]; uint32_t s[5]; uint32_t h[5]; } poly1305_state;
static void poly1305_init(poly1305_state *st, const uint8_t key[32]){ uint32_t t0 = load32_le(key+0); uint32_t t1 = load32_le(key+4); uint32_t t2 = load32_le(key+8); uint32_t t3 = load32_le(key+12); uint32_t t4 = load32_le(key+16); uint32_t t5 = load32_le(key+20); uint32_t t6 = load32_le(key+24); uint32_t t7 = load32_le(key+28);
uint64_t r0 = (uint64_t)( t0 ) & 0x3ffffff; uint64_t r1 = ((uint64_t)(t0>>26) | ((uint64_t)t1<<6)) & 0x3ffff03; uint64_t r2 = ((uint64_t)(t1>>20) | ((uint64_t)t2<<12)) & 0x3ffc0ff; uint64_t r3 = ((uint64_t)(t2>>14) | ((uint64_t)t3<<18)) & 0x3f03fff; uint64_t r4 = ((uint64_t)(t3>>8)) & 0x00fffff;
st->r[0]=(uint32_t)r0; st->r[1]=(uint32_t)r1; st->r[2]=(uint32_t)r2; st->r[3]=(uint32_t)r3; st->r[4]=(uint32_t)r4;
st->s[0]=st->r[0]*5u; st->s[1]=st->r[1]*5u; st->s[2]=st->r[2]*5u; st->s[3]=st->r[3]*5u; st->s[4]=st->r[4]*5u;
st->h[0]=st->h[1]=st->h[2]=st->h[3]=st->h[4]=0;
(void)t4; (void)t5; (void)t6; (void)t7; }
static void poly1305_blocks(poly1305_state *st, const uint8_t *m, size_t bytes, uint32_t hibit){ uint32_t r0=st->r[0], r1=st->r[1], r2=st->r[2], r3=st->r[3], r4=st->r[4]; uint32_t s1=st->s[1], s2=st->s[2], s3=st->s[3], s4=st->s[4]; uint32_t h0=st->h[0], h1=st->h[1], h2=st->h[2], h3=st->h[3], h4=st->h[4];
while(bytes >= 16){ uint64_t t0 = load32_le(m+0); uint64_t t1 = load32_le(m+4); uint64_t t2 = load32_le(m+8); uint64_t t3 = load32_le(m+12);
h0 += (uint32_t)( t0 & 0x3ffffff); h1 += (uint32_t)(((t0>>26) | (t1<<6)) & 0x3ffffff); h2 += (uint32_t)(((t1>>20) | (t2<<12))& 0x3ffffff); h3 += (uint32_t)(((t2>>14) | (t3<<18))& 0x3ffffff); h4 += (uint32_t)(( t3>>8 ) ) + hibit;
uint64_t d0 = (uint64_t)h0*r0 + (uint64_t)h1*s4 + (uint64_t)h2*s3 + (uint64_t)h3*s2 + (uint64_t)h4*s1; uint64_t d1 = (uint64_t)h0*r1 + (uint64_t)h1*r0 + (uint64_t)h2*s4 + (uint64_t)h3*s3 + (uint64_t)h4*s2; uint64_t d2 = (uint64_t)h0*r2 + (uint64_t)h1*r1 + (uint64_t)h2*r0 + (uint64_t)h3*s4 + (uint64_t)h4*s3; uint64_t d3 = (uint64_t)h0*r3 + (uint64_t)h1*r2 + (uint64_t)h2*r1 + (uint64_t)h3*r0 + (uint64_t)h4*s4; uint64_t d4 = (uint64_t)h0*r4 + (uint64_t)h1*r3 + (uint64_t)h2*r2 + (uint64_t)h3*r1 + (uint64_t)h4*r0;
uint64_t c; c = (d0 >> 26); h0 = (uint32_t)(d0 & 0x3ffffff); d1 += c; c = (d1 >> 26); h1 = (uint32_t)(d1 & 0x3ffffff); d2 += c; c = (d2 >> 26); h2 = (uint32_t)(d2 & 0x3ffffff); d3 += c; c = (d3 >> 26); h3 = (uint32_t)(d3 & 0x3ffffff); d4 += c; c = (d4 >> 26); h4 = (uint32_t)(d4 & 0x3ffffff); h0 += (uint32_t)(c * 5); c = h0 >> 26; h0 &= 0x3ffffff; h1 += (uint32_t)c;
m += 16; bytes -= 16; }
st->h[0]=h0; st->h[1]=h1; st->h[2]=h2; st->h[3]=h3; st->h[4]=h4; }
static void poly1305_finish(uint8_t mac[16], poly1305_state *st, const uint8_t key[32]){ uint32_t h0=st->h[0], h1=st->h[1], h2=st->h[2], h3=st->h[3], h4=st->h[4];
uint32_t c; c = h1 >> 26; h1 &= 0x3ffffff; h2 += c; c = h2 >> 26; h2 &= 0x3ffffff; h3 += c; c = h3 >> 26; h3 &= 0x3ffffff; h4 += c; c = h4 >> 26; h4 &= 0x3ffffff; h0 += c * 5; c = h0 >> 26; h0 &= 0x3ffffff; h1 += c;
uint32_t g0 = h0 + 5; c = g0 >> 26; g0 &= 0x3ffffff; uint32_t g1 = h1 + c; c = g1 >> 26; g1 &= 0x3ffffff; uint32_t g2 = h2 + c; c = g2 >> 26; g2 &= 0x3ffffff; uint32_t g3 = h3 + c; c = g3 >> 26; g3 &= 0x3ffffff; uint32_t g4 = h4 + c - (1u<<26);
uint32_t mask = (g4 >> 31) - 1; h0 = (h0 & ~mask) | (g0 & mask); h1 = (h1 & ~mask) | (g1 & mask); h2 = (h2 & ~mask) | (g2 & mask); h3 = (h3 & ~mask) | (g3 & mask); h4 = (h4 & ~mask) | (g4 & mask);
uint64_t f0 = ((uint64_t)h0 ) | ((uint64_t)h1<<26); uint64_t f1 = ((uint64_t)h2 ) | ((uint64_t)h3<<26) | ((uint64_t)h4<<52);
uint64_t s0 = load64_le(key+16); uint64_t s1 = load64_le(key+24); f0 += s0; f1 += s1 + (f0 < s0);
store64_le(mac+0, f0); store64_le(mac+8, f1); }
static void poly1305_auth(uint8_t mac[16], const uint8_t *m, size_t mlen, const uint8_t one_time_key[32]){ poly1305_state st; poly1305_init(&st, one_time_key); if(mlen){ size_t n = mlen & ~(size_t)15; if(n){ poly1305_blocks(&st, m, n, 1u<<24); m += n; mlen -= n; } } if(mlen){ uint8_t last[16]={0}; for(size_t i=0;i<mlen;i++) last[i]=m[i]; last[mlen]=1; poly1305_blocks(&st, last, 16, 0); } poly1305_finish(mac, &st, one_time_key); }
static void aead_poly1305_keygen(uint8_t otk[32], const uint8_t key[32], const uint8_t nonce[12]){ uint8_t block0[64]; chacha20_block(block0, key, 0, nonce); memcpy(otk, block0, 32); memset(block0,0,sizeof(block0)); }
int aead_chacha20poly1305_encrypt( uint8_t *out, uint8_t tag[16], const uint8_t *plaintext, size_t plen, const uint8_t *aad, size_t aad_len, const uint8_t key[32], const uint8_t nonce[12]) { uint8_t otk[32]; aead_poly1305_keygen(otk,key,nonce);
chacha20_xor(out, plaintext, plen, key, nonce, 1);
uint8_t block[16];
if(aad_len){ poly1305_auth(tag, NULL, 0, otk); } { poly1305_state st; poly1305_init(&st, otk); size_t n = aad_len & ~(size_t)15; if(n) poly1305_blocks(&st, aad, n, 1u<<24); if(aad_len & 15){ uint8_t tmp[16]={0}; size_t r = aad_len & 15; memcpy(tmp, aad+n, r); tmp[r]=1; poly1305_blocks(&st, tmp, 16, 0); } n = plen & ~(size_t)15; if(n) poly1305_blocks(&st, out, n, 1u<<24); if(plen & 15){ uint8_t tmp2[16]={0}; size_t r = plen & 15; memcpy(tmp2, out+n, r); tmp2[r]=1; poly1305_blocks(&st, tmp2, 16, 0); } uint8_t lens[16]; store64_le(lens+0, (uint64_t)aad_len); store64_le(lens+8, (uint64_t)plen); poly1305_blocks(&st, lens, 16, 0); poly1305_finish(tag, &st, otk); }
memset(otk,0,sizeof(otk)); memset(block,0,sizeof(block)); return 1; }
int aead_chacha20poly1305_decrypt( uint8_t *out, const uint8_t *ciphertext, size_t clen, const uint8_t tag[16], const uint8_t *aad, size_t aad_len, const uint8_t key[32], const uint8_t nonce[12]) { uint8_t otk[32]; uint8_t comp[16]; aead_poly1305_keygen(otk,key,nonce);
{ poly1305_state st; poly1305_init(&st, otk); size_t n = aad_len & ~(size_t)15; if(n) poly1305_blocks(&st, aad, n, 1u<<24); if(aad_len & 15){ uint8_t t[16]={0}; size_t r=aad_len&15; memcpy(t,aad+n,r); t[r]=1; poly1305_blocks(&st,t,16,0); } n = clen & ~(size_t)15; if(n) poly1305_blocks(&st, ciphertext, n, 1u<<24); if(clen & 15){ uint8_t t2[16]={0}; size_t r=clen&15; memcpy(t2,ciphertext+n,r); t2[r]=1; poly1305_blocks(&st,t2,16,0);} uint8_t lens[16]; store64_le(lens+0,(uint64_t)aad_len); store64_le(lens+8,(uint64_t)clen); poly1305_blocks(&st,lens,16,0); poly1305_finish(comp,&st,otk); }
int ok = ct_mem_eq(comp, tag, 16);
if(ok){ chacha20_xor(out, ciphertext, clen, key, nonce, 1); }
memset(otk,0,sizeof(otk)); memset((void*)comp,0,sizeof(comp));
return ok; }
#ifdef TEST_MAIN #include <stdio.h>
static void hex(const char *label, const uint8_t *b, size_t n){ printf("%s", label); for(size_t i=0;i<n;i++) printf("%02x", b[i]); printf("\n"); }
int main(void){ uint8_t key[32]={0}; for(int i=0;i<32;i++) key[i]=(uint8_t)i; uint8_t nonce[12]={0}; for(int i=0;i<12;i++) nonce[i]=(uint8_t)(0xA0+i); const uint8_t aad[] = "example aead aad"; const uint8_t msg[] = "ChaCha20-Poly1305 test message";
uint8_t ct[sizeof msg]; uint8_t tag[16]; aead_chacha20poly1305_encrypt(ct, tag, msg, sizeof msg, aad, sizeof aad-1, key, nonce);
uint8_t pt[sizeof msg]; int ok = aead_chacha20poly1305_decrypt(pt, ct, sizeof ct, tag, aad, sizeof aad-1, key, nonce);
printf("verify: %s\n", ok?"OK":"FAIL"); hex("tag: ", tag, 16); hex("ct : ", ct, sizeof ct); printf("pt : %.*s\n", (int)sizeof pt, pt); return ok?0:1; } #endif
|