Changeset 1265
- Timestamp:
- May 11, 2007 10:37:14 AM (18 years ago)
- Location:
- pjproject/trunk
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjlib-util/include/pjlib-util/hmac_md5.h
r1001 r1265 30 30 31 31 #include <pj/types.h> 32 #include <pjlib-util/md5.h> 32 33 33 34 PJ_BEGIN_DECL … … 41 42 * for Message Authentication, as described in RFC 2104 42 43 */ 44 45 /** 46 * The HMAC-MD5 context used in the incremental HMAC calculation. 47 */ 48 typedef 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; 43 53 44 54 … … 58 68 59 69 /** 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 */ 76 PJ_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 */ 86 PJ_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 */ 96 PJ_DECL(void) pj_hmac_md5_final(pj_hmac_md5_context *hctx, 97 pj_uint8_t digest[16]); 98 99 /** 60 100 * @} 61 101 */ -
pjproject/trunk/pjlib-util/include/pjlib-util/hmac_sha1.h
r1001 r1265 26 26 27 27 #include <pj/types.h> 28 #include <pjlib-util/sha1.h> 28 29 29 30 PJ_BEGIN_DECL … … 35 36 * 36 37 * 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. 38 39 */ 40 41 /** 42 * The HMAC-SHA1 context used in the incremental HMAC calculation. 43 */ 44 typedef 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; 39 49 40 50 41 51 /** 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. 43 54 * 44 55 * @param input Pointer to the input stream. … … 54 65 55 66 /** 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 */ 73 PJ_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 */ 83 PJ_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 */ 93 PJ_DECL(void) pj_hmac_sha1_final(pj_hmac_sha1_context *hctx, 94 pj_uint8_t digest[20]); 95 96 97 /** 56 98 * @} 57 99 */ -
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 -
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 -
pjproject/trunk/pjnath/src/pjnath/stun_auth.c
r1126 r1265 120 120 const pj_stun_realm_attr *arealm; 121 121 const pj_stun_realm_attr *anonce; 122 pj_hmac_sha1_context ctx; 122 123 pj_uint8_t digest[PJ_SHA1_DIGEST_SIZE]; 123 124 pj_uint8_t md5_digest[16]; … … 328 329 } 329 330 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); 332 342 333 343 /* Compare HMACs */ -
pjproject/trunk/pjnath/src/pjnath/stun_msg.c
r1239 r1265 2140 2140 2141 2141 pj_uint8_t md5_key_buf[16]; 2142 pj_hmac_sha1_context ctx; 2142 2143 pj_str_t key; 2143 2144 … … 2182 2183 } 2183 2184 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); 2188 2196 2189 2197 /* Put this attribute in the message */
Note: See TracChangeset
for help on using the changeset viewer.