Changeset 1265 for pjproject/trunk/pjlib-util/src/pjlib-util/hmac_sha1.c
- Timestamp:
- May 11, 2007 10:37:14 AM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjlib-util/src/pjlib-util/hmac_sha1.c
r1001 r1265 18 18 */ 19 19 #include <pjlib-util/hmac_sha1.h> 20 #include <pjlib-util/sha1.h>21 20 #include <pj/string.h> 22 21 23 22 24 PJ_DEF(void) pj_hmac_sha1(const pj_uint8_t *input, unsigned input_len, 25 const pj_uint8_t *key, unsigned key_len, 26 pj_uint8_t digest[20] ) 23 PJ_DEF(void) pj_hmac_sha1_init(pj_hmac_sha1_context *hctx, 24 const pj_uint8_t *key, unsigned key_len) 27 25 { 28 pj_sha1_context context; 29 pj_uint8_t k_ipad[65]; 30 pj_uint8_t k_opad[65]; 26 pj_uint8_t k_ipad[64]; 31 27 pj_uint8_t tk[20]; 32 inti;28 unsigned i; 33 29 34 30 /* if key is longer than 64 bytes reset it to key=SHA1(key) */ … … 44 40 } 45 41 42 /* 43 * HMAC = H(K XOR opad, H(K XOR ipad, text)) 44 */ 45 46 46 /* start out by storing key in pads */ 47 47 pj_bzero( k_ipad, sizeof(k_ipad)); 48 pj_bzero( k_opad, sizeof(k_opad));48 pj_bzero( hctx->k_opad, sizeof(hctx->k_opad)); 49 49 pj_memcpy( k_ipad, key, key_len); 50 pj_memcpy( k_opad, key, key_len);50 pj_memcpy( hctx->k_opad, key, key_len); 51 51 52 52 /* XOR key with ipad and opad values */ 53 53 for (i=0; i<64; i++) { 54 54 k_ipad[i] ^= 0x36; 55 k_opad[i] ^= 0x5c;55 hctx->k_opad[i] ^= 0x5c; 56 56 } 57 57 /* 58 58 * perform inner SHA1 59 59 */ 60 pj_sha1_init(&context); 61 pj_sha1_update(&context, k_ipad, 64); 62 pj_sha1_update(&context, input, input_len); 63 pj_sha1_final(&context, digest); 60 pj_sha1_init(&hctx->context); 61 pj_sha1_update(&hctx->context, k_ipad, 64); 62 } 63 64 PJ_DEF(void) pj_hmac_sha1_update(pj_hmac_sha1_context *hctx, 65 const pj_uint8_t *input, unsigned input_len) 66 { 67 pj_sha1_update(&hctx->context, input, input_len); 68 } 69 70 PJ_DEF(void) pj_hmac_sha1_final(pj_hmac_sha1_context *hctx, 71 pj_uint8_t digest[20]) 72 { 73 pj_sha1_final(&hctx->context, digest); 64 74 65 75 /* 66 76 * perform outer SHA1 67 77 */ 68 pj_sha1_init(& context);69 pj_sha1_update(& context,k_opad, 64);70 pj_sha1_update(& context, digest, 20);71 pj_sha1_final(& context, digest);78 pj_sha1_init(&hctx->context); 79 pj_sha1_update(&hctx->context, hctx->k_opad, 64); 80 pj_sha1_update(&hctx->context, digest, 20); 81 pj_sha1_final(&hctx->context, digest); 72 82 } 73 83 84 PJ_DEF(void) pj_hmac_sha1(const pj_uint8_t *input, unsigned input_len, 85 const pj_uint8_t *key, unsigned key_len, 86 pj_uint8_t digest[20] ) 87 { 88 pj_hmac_sha1_context ctx; 74 89 90 pj_hmac_sha1_init(&ctx, key, key_len); 91 pj_hmac_sha1_update(&ctx, input, input_len); 92 pj_hmac_sha1_final(&ctx, digest); 93 } 94
Note: See TracChangeset
for help on using the changeset viewer.