hmac.c (885B)
1 #include "os.h" 2 #include "libsec.h" 3 4 /* rfc2104 */ 5 DigestState* 6 hmac_x(uchar *p, ulong len, uchar *key, ulong klen, uchar *digest, DigestState *s, 7 DigestState*(*x)(uchar*, ulong, uchar*, DigestState*), int xlen) 8 { 9 int i; 10 uchar pad[Hmacblksz+1], innerdigest[256]; 11 12 if(xlen > sizeof(innerdigest)) 13 return nil; 14 if(klen > Hmacblksz) 15 return nil; 16 17 /* first time through */ 18 if(s == nil || s->seeded == 0){ 19 memset(pad, 0x36, Hmacblksz); 20 pad[Hmacblksz] = 0; 21 for(i = 0; i < klen; i++) 22 pad[i] ^= key[i]; 23 s = (*x)(pad, Hmacblksz, nil, s); 24 if(s == nil) 25 return nil; 26 } 27 28 s = (*x)(p, len, nil, s); 29 if(digest == nil) 30 return s; 31 32 /* last time through */ 33 memset(pad, 0x5c, Hmacblksz); 34 pad[Hmacblksz] = 0; 35 for(i = 0; i < klen; i++) 36 pad[i] ^= key[i]; 37 (*x)(nil, 0, innerdigest, s); 38 s = (*x)(pad, Hmacblksz, nil, nil); 39 (*x)(innerdigest, xlen, digest, s); 40 return nil; 41 }