Changeset 1265


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

Fixed missing padding when calculating MESSAGE-INTEGRITY in STUN

Location:
pjproject/trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjlib-util/include/pjlib-util/hmac_md5.h

    r1001 r1265  
    3030 
    3131#include <pj/types.h> 
     32#include <pjlib-util/md5.h> 
    3233 
    3334PJ_BEGIN_DECL 
     
    4142 * for Message Authentication, as described in RFC 2104 
    4243 */ 
     44 
     45/** 
     46 * The HMAC-MD5 context used in the incremental HMAC calculation. 
     47 */ 
     48typedef struct pj_hmac_md5_context 
     49{ 
     50    pj_md5_context  context;    /**< MD5 context            */ 
     51    pj_uint8_t      k_opad[64]; /**< opad xor-ed with key   */ 
     52} pj_hmac_md5_context; 
    4353 
    4454 
     
    5868 
    5969/** 
     70 * Initiate HMAC-MD5 context for incremental hashing. 
     71 * 
     72 * @param hctx          HMAC-MD5 context. 
     73 * @param key           Pointer to the authentication key. 
     74 * @param key_len       Length of the authentication key. 
     75 */ 
     76PJ_DECL(void) pj_hmac_md5_init(pj_hmac_md5_context *hctx,  
     77                               const pj_uint8_t *key, unsigned key_len); 
     78 
     79/** 
     80 * Append string to the message. 
     81 * 
     82 * @param hctx          HMAC-MD5 context. 
     83 * @param input         Pointer to the input stream. 
     84 * @param input_len     Length of input stream in bytes. 
     85 */ 
     86PJ_DECL(void) pj_hmac_md5_update(pj_hmac_md5_context *hctx, 
     87                                 const pj_uint8_t *input,  
     88                                 unsigned input_len); 
     89 
     90/** 
     91 * Finish the message and return the digest.  
     92 * 
     93 * @param hctx          HMAC-MD5 context. 
     94 * @param digest        Buffer to be filled with HMAC MD5 digest. 
     95 */ 
     96PJ_DECL(void) pj_hmac_md5_final(pj_hmac_md5_context *hctx, 
     97                                pj_uint8_t digest[16]); 
     98 
     99/** 
    60100 * @} 
    61101 */ 
  • pjproject/trunk/pjlib-util/include/pjlib-util/hmac_sha1.h

    r1001 r1265  
    2626 
    2727#include <pj/types.h> 
     28#include <pjlib-util/sha1.h> 
    2829 
    2930PJ_BEGIN_DECL 
     
    3536 * 
    3637 * This module contains the implementation of HMAC: Keyed-Hashing  
    37  * for Message Authentication, as described in RFC 2104 
     38 * for Message Authentication, as described in RFC 2104. 
    3839 */ 
     40 
     41/** 
     42 * The HMAC-SHA1 context used in the incremental HMAC calculation. 
     43 */ 
     44typedef struct pj_hmac_sha1_context 
     45{ 
     46    pj_sha1_context context;    /**< SHA1 context           */ 
     47    pj_uint8_t      k_opad[64]; /**< opad xor-ed with key   */ 
     48} pj_hmac_sha1_context; 
    3949 
    4050 
    4151/** 
    42  * Calculate HMAC SHA1 digest for the specified input and key. 
     52 * Calculate HMAC-SHA1 digest for the specified input and key with this 
     53 * single function call. 
    4354 * 
    4455 * @param input         Pointer to the input stream. 
     
    5465 
    5566/** 
     67 * Initiate HMAC-SHA1 context for incremental hashing. 
     68 * 
     69 * @param hctx          HMAC-SHA1 context. 
     70 * @param key           Pointer to the authentication key. 
     71 * @param key_len       Length of the authentication key. 
     72 */ 
     73PJ_DECL(void) pj_hmac_sha1_init(pj_hmac_sha1_context *hctx,  
     74                                const pj_uint8_t *key, unsigned key_len); 
     75 
     76/** 
     77 * Append string to the message. 
     78 * 
     79 * @param hctx          HMAC-SHA1 context. 
     80 * @param input         Pointer to the input stream. 
     81 * @param input_len     Length of input stream in bytes. 
     82 */ 
     83PJ_DECL(void) pj_hmac_sha1_update(pj_hmac_sha1_context *hctx, 
     84                                  const pj_uint8_t *input,  
     85                                  unsigned input_len); 
     86 
     87/** 
     88 * Finish the message and return the digest.  
     89 * 
     90 * @param hctx          HMAC-SHA1 context. 
     91 * @param digest        Buffer to be filled with HMAC SHA1 digest. 
     92 */ 
     93PJ_DECL(void) pj_hmac_sha1_final(pj_hmac_sha1_context *hctx, 
     94                                 pj_uint8_t digest[20]); 
     95 
     96 
     97/** 
    5698 * @} 
    5799 */ 
  • 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 
  • 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 
  • pjproject/trunk/pjnath/src/pjnath/stun_auth.c

    r1126 r1265  
    120120    const pj_stun_realm_attr *arealm; 
    121121    const pj_stun_realm_attr *anonce; 
     122    pj_hmac_sha1_context ctx; 
    122123    pj_uint8_t digest[PJ_SHA1_DIGEST_SIZE]; 
    123124    pj_uint8_t md5_digest[16]; 
     
    328329    } 
    329330 
    330     /* Now calculate HMAC of the message */ 
    331     pj_hmac_sha1(pkt, amsgi_pos, (pj_uint8_t*)key.ptr, key.slen, digest); 
     331    /* Now calculate HMAC of the message, adding zero padding if necessary 
     332     * to make the input 64 bytes aligned. 
     333     */ 
     334    pj_hmac_sha1_init(&ctx, (pj_uint8_t*)key.ptr, key.slen); 
     335    pj_hmac_sha1_update(&ctx, pkt, amsgi_pos); 
     336    if (amsgi_pos & 0x3F) { 
     337        pj_uint8_t zeroes[64]; 
     338        pj_bzero(zeroes, sizeof(zeroes)); 
     339        pj_hmac_sha1_update(&ctx, zeroes, 64-(amsgi_pos & 0x3F)); 
     340    } 
     341    pj_hmac_sha1_final(&ctx, digest); 
    332342 
    333343    /* Compare HMACs */ 
  • pjproject/trunk/pjnath/src/pjnath/stun_msg.c

    r1239 r1265  
    21402140 
    21412141        pj_uint8_t md5_key_buf[16]; 
     2142        pj_hmac_sha1_context ctx; 
    21422143        pj_str_t key; 
    21432144 
     
    21822183        } 
    21832184 
    2184         /* Calculate HMAC-SHA1 digest */ 
    2185         pj_hmac_sha1((pj_uint8_t*)start, buf-start,  
    2186                      (pj_uint8_t*)key.ptr, key.slen, 
    2187                      amsgint->hmac); 
     2185        /* Calculate HMAC-SHA1 digest, add zero padding to input 
     2186         * if necessary to make the input 64 bytes aligned. 
     2187         */ 
     2188        pj_hmac_sha1_init(&ctx, (pj_uint8_t*)key.ptr, key.slen); 
     2189        pj_hmac_sha1_update(&ctx, (pj_uint8_t*)start, buf-start); 
     2190        if ((buf-start) & 0x3F) { 
     2191            pj_uint8_t zeroes[64]; 
     2192            pj_bzero(zeroes, sizeof(zeroes)); 
     2193            pj_hmac_sha1_update(&ctx, zeroes, 64-((buf-start) & 0x3F)); 
     2194        } 
     2195        pj_hmac_sha1_final(&ctx, amsgint->hmac); 
    21882196 
    21892197        /* Put this attribute in the message */ 
Note: See TracChangeset for help on using the changeset viewer.