diff options
author | Benny Prijono <bennylp@teluu.com> | 2007-05-11 10:37:14 +0000 |
---|---|---|
committer | Benny Prijono <bennylp@teluu.com> | 2007-05-11 10:37:14 +0000 |
commit | b4e01cefd4f354128396e85f205ccfa0879bc319 (patch) | |
tree | 07c6a259606d0cefa3d05bdbac9d4392e9bcc237 /pjlib-util/src | |
parent | c7b1c88b23e55ca897ebacfdeabb9c17c4cd8f34 (diff) |
Fixed missing padding when calculating MESSAGE-INTEGRITY in STUN
git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@1265 74dad513-b988-da41-8d7b-12977e46ad98
Diffstat (limited to 'pjlib-util/src')
-rw-r--r-- | pjlib-util/src/pjlib-util/hmac_md5.c | 63 | ||||
-rw-r--r-- | pjlib-util/src/pjlib-util/hmac_sha1.c | 58 |
2 files changed, 81 insertions, 40 deletions
diff --git a/pjlib-util/src/pjlib-util/hmac_md5.c b/pjlib-util/src/pjlib-util/hmac_md5.c index d9c4e466..5ce36c2c 100644 --- a/pjlib-util/src/pjlib-util/hmac_md5.c +++ b/pjlib-util/src/pjlib-util/hmac_md5.c @@ -16,20 +16,14 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#include <pjlib-util/md5.h> +#include <pjlib-util/hmac_md5.h> #include <pj/string.h> -/* This code is taken from RFC 2104 */ - - -PJ_DEF(void) pj_hmac_md5( const pj_uint8_t *input, unsigned input_len, - const pj_uint8_t *key, unsigned key_len, - pj_uint8_t digest[16] ) +PJ_DEF(void) pj_hmac_md5_init(pj_hmac_md5_context *hctx, + const pj_uint8_t *key, unsigned key_len) { - pj_md5_context context; - pj_uint8_t k_ipad[65]; - pj_uint8_t k_opad[65]; + pj_uint8_t k_ipad[64]; pj_uint8_t tk[16]; int i; @@ -45,31 +39,58 @@ PJ_DEF(void) pj_hmac_md5( const pj_uint8_t *input, unsigned input_len, key_len = 16; } + /* + * HMAC = H(K XOR opad, H(K XOR ipad, text)) + */ + /* start out by storing key in pads */ pj_bzero( k_ipad, sizeof(k_ipad)); - pj_bzero( k_opad, sizeof(k_opad)); + pj_bzero( hctx->k_opad, sizeof(hctx->k_opad)); pj_memcpy( k_ipad, key, key_len); - pj_memcpy( k_opad, key, key_len); + pj_memcpy( hctx->k_opad, key, key_len); /* XOR key with ipad and opad values */ for (i=0; i<64; i++) { k_ipad[i] ^= 0x36; - k_opad[i] ^= 0x5c; + hctx->k_opad[i] ^= 0x5c; } /* * perform inner MD5 */ - pj_md5_init(&context); - pj_md5_update(&context, k_ipad, 64); - pj_md5_update(&context, input, input_len); - pj_md5_final(&context, digest); + pj_md5_init(&hctx->context); + pj_md5_update(&hctx->context, k_ipad, 64); + +} + +PJ_DEF(void) pj_hmac_md5_update(pj_hmac_md5_context *hctx, + const pj_uint8_t *input, + unsigned input_len) +{ + pj_md5_update(&hctx->context, input, input_len); +} + +PJ_DEF(void) pj_hmac_md5_final(pj_hmac_md5_context *hctx, + pj_uint8_t digest[16]) +{ + pj_md5_final(&hctx->context, digest); /* * perform outer MD5 */ - pj_md5_init(&context); - pj_md5_update(&context, k_opad, 64); - pj_md5_update(&context, digest, 16); - pj_md5_final(&context, digest); + pj_md5_init(&hctx->context); + pj_md5_update(&hctx->context, hctx->k_opad, 64); + pj_md5_update(&hctx->context, digest, 16); + pj_md5_final(&hctx->context, digest); +} + +PJ_DEF(void) pj_hmac_md5( const pj_uint8_t *input, unsigned input_len, + const pj_uint8_t *key, unsigned key_len, + pj_uint8_t digest[16] ) +{ + pj_hmac_md5_context ctx; + + pj_hmac_md5_init(&ctx, key, key_len); + pj_hmac_md5_update(&ctx, input, input_len); + pj_hmac_md5_final(&ctx, digest); } diff --git a/pjlib-util/src/pjlib-util/hmac_sha1.c b/pjlib-util/src/pjlib-util/hmac_sha1.c index 127434b7..b921febc 100644 --- a/pjlib-util/src/pjlib-util/hmac_sha1.c +++ b/pjlib-util/src/pjlib-util/hmac_sha1.c @@ -17,19 +17,15 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include <pjlib-util/hmac_sha1.h> -#include <pjlib-util/sha1.h> #include <pj/string.h> -PJ_DEF(void) pj_hmac_sha1(const pj_uint8_t *input, unsigned input_len, - const pj_uint8_t *key, unsigned key_len, - pj_uint8_t digest[20] ) +PJ_DEF(void) pj_hmac_sha1_init(pj_hmac_sha1_context *hctx, + const pj_uint8_t *key, unsigned key_len) { - pj_sha1_context context; - pj_uint8_t k_ipad[65]; - pj_uint8_t k_opad[65]; + pj_uint8_t k_ipad[64]; pj_uint8_t tk[20]; - int i; + unsigned i; /* if key is longer than 64 bytes reset it to key=SHA1(key) */ if (key_len > 64) { @@ -43,32 +39,56 @@ PJ_DEF(void) pj_hmac_sha1(const pj_uint8_t *input, unsigned input_len, key_len = 20; } + /* + * HMAC = H(K XOR opad, H(K XOR ipad, text)) + */ + /* start out by storing key in pads */ pj_bzero( k_ipad, sizeof(k_ipad)); - pj_bzero( k_opad, sizeof(k_opad)); + pj_bzero( hctx->k_opad, sizeof(hctx->k_opad)); pj_memcpy( k_ipad, key, key_len); - pj_memcpy( k_opad, key, key_len); + pj_memcpy( hctx->k_opad, key, key_len); /* XOR key with ipad and opad values */ for (i=0; i<64; i++) { k_ipad[i] ^= 0x36; - k_opad[i] ^= 0x5c; + hctx->k_opad[i] ^= 0x5c; } /* * perform inner SHA1 */ - pj_sha1_init(&context); - pj_sha1_update(&context, k_ipad, 64); - pj_sha1_update(&context, input, input_len); - pj_sha1_final(&context, digest); + pj_sha1_init(&hctx->context); + pj_sha1_update(&hctx->context, k_ipad, 64); +} + +PJ_DEF(void) pj_hmac_sha1_update(pj_hmac_sha1_context *hctx, + const pj_uint8_t *input, unsigned input_len) +{ + pj_sha1_update(&hctx->context, input, input_len); +} + +PJ_DEF(void) pj_hmac_sha1_final(pj_hmac_sha1_context *hctx, + pj_uint8_t digest[20]) +{ + pj_sha1_final(&hctx->context, digest); /* * perform outer SHA1 */ - pj_sha1_init(&context); - pj_sha1_update(&context, k_opad, 64); - pj_sha1_update(&context, digest, 20); - pj_sha1_final(&context, digest); + pj_sha1_init(&hctx->context); + pj_sha1_update(&hctx->context, hctx->k_opad, 64); + pj_sha1_update(&hctx->context, digest, 20); + pj_sha1_final(&hctx->context, digest); } +PJ_DEF(void) pj_hmac_sha1(const pj_uint8_t *input, unsigned input_len, + const pj_uint8_t *key, unsigned key_len, + pj_uint8_t digest[20] ) +{ + pj_hmac_sha1_context ctx; + + pj_hmac_sha1_init(&ctx, key, key_len); + pj_hmac_sha1_update(&ctx, input, input_len); + pj_hmac_sha1_final(&ctx, digest); +} |