Changeset 1265 for pjproject/trunk/pjlib-util/src/pjlib-util/hmac_md5.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_md5.c
r1001 r1265 17 17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 18 */ 19 #include <pjlib-util/ md5.h>19 #include <pjlib-util/hmac_md5.h> 20 20 #include <pj/string.h> 21 21 22 22 23 /* This code is taken from RFC 2104 */ 24 25 26 PJ_DEF(void) pj_hmac_md5( const pj_uint8_t *input, unsigned input_len, 27 const pj_uint8_t *key, unsigned key_len, 28 pj_uint8_t digest[16] ) 23 PJ_DEF(void) pj_hmac_md5_init(pj_hmac_md5_context *hctx, 24 const pj_uint8_t *key, unsigned key_len) 29 25 { 30 pj_md5_context context; 31 pj_uint8_t k_ipad[65]; 32 pj_uint8_t k_opad[65]; 26 pj_uint8_t k_ipad[64]; 33 27 pj_uint8_t tk[16]; 34 28 int i; … … 46 40 } 47 41 42 /* 43 * HMAC = H(K XOR opad, H(K XOR ipad, text)) 44 */ 45 48 46 /* start out by storing key in pads */ 49 47 pj_bzero( k_ipad, sizeof(k_ipad)); 50 pj_bzero( k_opad, sizeof(k_opad));48 pj_bzero( hctx->k_opad, sizeof(hctx->k_opad)); 51 49 pj_memcpy( k_ipad, key, key_len); 52 pj_memcpy( k_opad, key, key_len);50 pj_memcpy( hctx->k_opad, key, key_len); 53 51 54 52 /* XOR key with ipad and opad values */ 55 53 for (i=0; i<64; i++) { 56 54 k_ipad[i] ^= 0x36; 57 k_opad[i] ^= 0x5c;55 hctx->k_opad[i] ^= 0x5c; 58 56 } 59 57 /* 60 58 * perform inner MD5 61 59 */ 62 pj_md5_init(&context); 63 pj_md5_update(&context, k_ipad, 64); 64 pj_md5_update(&context, input, input_len); 65 pj_md5_final(&context, digest); 60 pj_md5_init(&hctx->context); 61 pj_md5_update(&hctx->context, k_ipad, 64); 62 63 } 64 65 PJ_DEF(void) pj_hmac_md5_update(pj_hmac_md5_context *hctx, 66 const pj_uint8_t *input, 67 unsigned input_len) 68 { 69 pj_md5_update(&hctx->context, input, input_len); 70 } 71 72 PJ_DEF(void) pj_hmac_md5_final(pj_hmac_md5_context *hctx, 73 pj_uint8_t digest[16]) 74 { 75 pj_md5_final(&hctx->context, digest); 66 76 67 77 /* 68 78 * perform outer MD5 69 79 */ 70 pj_md5_init(& context);71 pj_md5_update(& context,k_opad, 64);72 pj_md5_update(& context, digest, 16);73 pj_md5_final(& context, digest);80 pj_md5_init(&hctx->context); 81 pj_md5_update(&hctx->context, hctx->k_opad, 64); 82 pj_md5_update(&hctx->context, digest, 16); 83 pj_md5_final(&hctx->context, digest); 74 84 } 75 85 86 PJ_DEF(void) pj_hmac_md5( const pj_uint8_t *input, unsigned input_len, 87 const pj_uint8_t *key, unsigned key_len, 88 pj_uint8_t digest[16] ) 89 { 90 pj_hmac_md5_context ctx; 91 92 pj_hmac_md5_init(&ctx, key, key_len); 93 pj_hmac_md5_update(&ctx, input, input_len); 94 pj_hmac_md5_final(&ctx, digest); 95 } 96
Note: See TracChangeset
for help on using the changeset viewer.