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_md5.c

    r1001 r1265  
    1717 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA  
    1818 */ 
    19 #include <pjlib-util/md5.h> 
     19#include <pjlib-util/hmac_md5.h> 
    2020#include <pj/string.h> 
    2121 
    2222 
    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] ) 
     23PJ_DEF(void) pj_hmac_md5_init(pj_hmac_md5_context *hctx,  
     24                              const pj_uint8_t *key, unsigned key_len) 
    2925{ 
    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]; 
    3327    pj_uint8_t tk[16]; 
    3428    int i; 
     
    4640    } 
    4741 
     42    /* 
     43     * HMAC = H(K XOR opad, H(K XOR ipad, text)) 
     44     */ 
     45 
    4846    /* start out by storing key in pads */ 
    4947    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)); 
    5149    pj_memcpy( k_ipad, key, key_len); 
    52     pj_memcpy( k_opad, key, key_len); 
     50    pj_memcpy( hctx->k_opad, key, key_len); 
    5351 
    5452    /* XOR key with ipad and opad values */ 
    5553    for (i=0; i<64; i++) { 
    5654        k_ipad[i] ^= 0x36; 
    57         k_opad[i] ^= 0x5c; 
     55        hctx->k_opad[i] ^= 0x5c; 
    5856    } 
    5957    /* 
    6058     * perform inner MD5 
    6159     */ 
    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 
     65PJ_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 
     72PJ_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); 
    6676 
    6777    /* 
    6878     * perform outer MD5 
    6979     */ 
    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); 
    7484} 
    7585 
     86PJ_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.