Ignore:
Timestamp:
May 11, 2007 10:37:14 AM (17 years ago)
Author:
bennylp
Message:

Fixed missing padding when calculating MESSAGE-INTEGRITY in STUN

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjlib-util/src/pjlib-util/hmac_sha1.c

    r1001 r1265  
    1818 */ 
    1919#include <pjlib-util/hmac_sha1.h> 
    20 #include <pjlib-util/sha1.h> 
    2120#include <pj/string.h> 
    2221 
    2322 
    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] ) 
     23PJ_DEF(void) pj_hmac_sha1_init(pj_hmac_sha1_context *hctx,  
     24                               const pj_uint8_t *key, unsigned key_len) 
    2725{ 
    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]; 
    3127    pj_uint8_t tk[20]; 
    32     int i; 
     28    unsigned i; 
    3329 
    3430    /* if key is longer than 64 bytes reset it to key=SHA1(key) */ 
     
    4440    } 
    4541 
     42    /* 
     43     * HMAC = H(K XOR opad, H(K XOR ipad, text)) 
     44     */ 
     45 
    4646    /* start out by storing key in pads */ 
    4747    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)); 
    4949    pj_memcpy( k_ipad, key, key_len); 
    50     pj_memcpy( k_opad, key, key_len); 
     50    pj_memcpy( hctx->k_opad, key, key_len); 
    5151 
    5252    /* XOR key with ipad and opad values */ 
    5353    for (i=0; i<64; i++) { 
    5454        k_ipad[i] ^= 0x36; 
    55         k_opad[i] ^= 0x5c; 
     55        hctx->k_opad[i] ^= 0x5c; 
    5656    } 
    5757    /* 
    5858     * perform inner SHA1 
    5959     */ 
    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 
     64PJ_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 
     70PJ_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); 
    6474 
    6575    /* 
    6676     * perform outer SHA1 
    6777     */ 
    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); 
    7282} 
    7383 
     84PJ_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; 
    7489 
     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.