67 ctx->
nonce.
c[0] = ((
u8)(L-1)&7) | (
u8)(((M-2)/2)&7)<<3;
77 const unsigned char *nonce,
size_t nlen,
size_t mlen)
79 unsigned int L = ctx->
nonce.
c[0]&7;
81 if (nlen<(14-L))
return -1;
83 if (
sizeof(mlen)==8 && L>=3) {
84 ctx->
nonce.
c[8] = (
u8)(mlen>>(56%(
sizeof(mlen)*8)));
85 ctx->
nonce.
c[9] = (
u8)(mlen>>(48%(
sizeof(mlen)*8)));
86 ctx->
nonce.
c[10] = (
u8)(mlen>>(40%(
sizeof(mlen)*8)));
87 ctx->
nonce.
c[11] = (
u8)(mlen>>(32%(
sizeof(mlen)*8)));
98 memcpy(&ctx->
nonce.
c[1],nonce,14-L);
105 const unsigned char *aad,
size_t alen)
115 if (alen<(0x10000-0x100)) {
116 ctx->
cmac.
c[0] ^= (
u8)(alen>>8);
120 else if (
sizeof(alen)==8 && alen>=(size_t)1<<(32%(
sizeof(alen)*8))) {
121 ctx->
cmac.
c[0] ^= 0xFF;
122 ctx->
cmac.
c[1] ^= 0xFF;
123 ctx->
cmac.
c[2] ^= (
u8)(alen>>(56%(
sizeof(alen)*8)));
124 ctx->
cmac.
c[3] ^= (
u8)(alen>>(48%(
sizeof(alen)*8)));
125 ctx->
cmac.
c[4] ^= (
u8)(alen>>(40%(
sizeof(alen)*8)));
126 ctx->
cmac.
c[5] ^= (
u8)(alen>>(32%(
sizeof(alen)*8)));
127 ctx->
cmac.
c[6] ^= (
u8)(alen>>24);
128 ctx->
cmac.
c[7] ^= (
u8)(alen>>16);
129 ctx->
cmac.
c[8] ^= (
u8)(alen>>8);
134 ctx->
cmac.
c[0] ^= 0xFF;
135 ctx->
cmac.
c[1] ^= 0xFE;
136 ctx->
cmac.
c[2] ^= (
u8)(alen>>24);
137 ctx->
cmac.
c[3] ^= (
u8)(alen>>16);
138 ctx->
cmac.
c[4] ^= (
u8)(alen>>8);
144 for(;i<16 && alen;++i,++aad,--alen)
145 ctx->
cmac.
c[i] ^= *aad;
156 static void ctr64_inc(
unsigned char *counter) {
171 const unsigned char *inp,
unsigned char *out,
176 unsigned char flags0 = ctx->
nonce.
c[0];
179 union {
u64 u[2];
u8 c[16]; } scratch;
185 ctx->
nonce.
c[0] = L = flags0&7;
186 for (n=0,i=15-L;i<15;++i) {
194 if (n!=len)
return -1;
196 ctx->
blocks += ((len+15)>>3)|1;
197 if (ctx->
blocks > (
U64(1)<<61))
return -2;
200 #if defined(STRICT_ALIGNMENT)
201 union {
u64 u[2];
u8 c[16]; } temp;
203 memcpy (temp.c,inp,16);
204 ctx->
cmac.
u[0] ^= temp.u[0];
205 ctx->
cmac.
u[1] ^= temp.u[1];
213 #if defined(STRICT_ALIGNMENT)
214 temp.u[0] ^= scratch.u[0];
215 temp.u[1] ^= scratch.u[1];
216 memcpy(out,temp.c,16);
218 ((
u64*)out)[0] = scratch.u[0]^((
u64*)inp)[0];
219 ((
u64*)out)[1] = scratch.u[1]^((
u64*)inp)[1];
227 for (i=0; i<
len; ++i) ctx->
cmac.
c[i] ^= inp[i];
230 for (i=0; i<
len; ++i) out[i] = scratch.c[i]^inp[i];
233 for (i=15-L;i<16;++i)
237 ctx->
cmac.
u[0] ^= scratch.u[0];
238 ctx->
cmac.
u[1] ^= scratch.u[1];
246 const unsigned char *inp,
unsigned char *out,
251 unsigned char flags0 = ctx->
nonce.
c[0];
254 union {
u64 u[2];
u8 c[16]; } scratch;
259 ctx->
nonce.
c[0] = L = flags0&7;
260 for (n=0,i=15-L;i<15;++i) {
268 if (n!=len)
return -1;
271 #if defined(STRICT_ALIGNMENT)
272 union {
u64 u[2];
u8 c[16]; } temp;
276 #if defined(STRICT_ALIGNMENT)
277 memcpy (temp.c,inp,16);
278 ctx->
cmac.
u[0] ^= (scratch.u[0] ^= temp.u[0]);
279 ctx->
cmac.
u[1] ^= (scratch.u[1] ^= temp.u[1]);
280 memcpy (out,scratch.c,16);
282 ctx->
cmac.
u[0] ^= (((
u64*)out)[0] = scratch.u[0]^((
u64*)inp)[0]);
283 ctx->
cmac.
u[1] ^= (((
u64*)out)[1] = scratch.u[1]^((
u64*)inp)[1]);
294 for (i=0; i<
len; ++i)
295 ctx->
cmac.
c[i] ^= (out[i] = scratch.c[i]^inp[i]);
299 for (i=15-L;i<16;++i)
303 ctx->
cmac.
u[0] ^= scratch.u[0];
304 ctx->
cmac.
u[1] ^= scratch.u[1];
311 static void ctr64_add (
unsigned char *counter,
size_t inc)
317 val += counter[n] + (inc&0xff);
318 counter[n] = (
unsigned char)val;
321 }
while(n && (inc || val));
325 const unsigned char *inp,
unsigned char *out,
330 unsigned char flags0 = ctx->
nonce.
c[0];
333 union {
u64 u[2];
u8 c[16]; } scratch;
339 ctx->
nonce.
c[0] = L = flags0&7;
340 for (n=0,i=15-L;i<15;++i) {
348 if (n!=len)
return -1;
350 ctx->
blocks += ((len+15)>>3)|1;
351 if (ctx->
blocks > (
U64(1)<<61))
return -2;
359 if (len) ctr64_add(ctx->
nonce.
c,n/16);
363 for (i=0; i<
len; ++i) ctx->
cmac.
c[i] ^= inp[i];
366 for (i=0; i<
len; ++i) out[i] = scratch.c[i]^inp[i];
369 for (i=15-L;i<16;++i)
373 ctx->
cmac.
u[0] ^= scratch.u[0];
374 ctx->
cmac.
u[1] ^= scratch.u[1];
382 const unsigned char *inp,
unsigned char *out,
387 unsigned char flags0 = ctx->
nonce.
c[0];
390 union {
u64 u[2];
u8 c[16]; } scratch;
395 ctx->
nonce.
c[0] = L = flags0&7;
396 for (n=0,i=15-L;i<15;++i) {
404 if (n!=len)
return -1;
412 if (len) ctr64_add(ctx->
nonce.
c,n/16);
417 for (i=0; i<
len; ++i)
418 ctx->
cmac.
c[i] ^= (out[i] = scratch.c[i]^inp[i]);
422 for (i=15-L;i<16;++i)
426 ctx->
cmac.
u[0] ^= scratch.u[0];
427 ctx->
cmac.
u[1] ^= scratch.u[1];
435 {
unsigned int M = (ctx->
nonce.
c[0]>>3)&7;
439 memcpy(tag,ctx->
cmac.
c,M);