mutils_error _mhash_gen_key_hex(void *keyword, mutils_word32 key_size, mutils_word8 *password, mutils_word32 plen) { mutils_word8 *chain = password; mutils_word8 *pkeyword = keyword; mutils_word8 tmp[3]; mutils_word32 i; mutils_bzero(keyword, key_size); /* The chain should have 2*n characters */ if (plen % 2 != 0 || plen > key_size*2) return(-MUTILS_INVALID_SIZE); if (check_hex(chain, plen) == MUTILS_FALSE) return(-MUTILS_INVALID_FORMAT); mutils_bzero( keyword, key_size); for (i = 0; i < plen; i += 2) { mutils_memcpy(tmp, &chain[i], 2); tmp[2] = '\0'; pkeyword[i / 2] = mutils_strtol(tmp, (mutils_word8 **) NULL, 16); } return(MUTILS_OK); }
WIN32DLL_DEFINE MHASH mhash_hmac_init(__const hashid type, void *key, mutils_word32 keysize, mutils_word32 block) { MHASH ret = MHASH_FAILED; MHASH tmptd; mutils_word8 *ipad; mutils_word8 _ipad[MAX_BLOCK_SIZE]; mutils_word32 i; mutils_boolean ipad_alloc = MUTILS_FALSE; mutils_boolean res; if (block == 0) { block = 64; /* the default for ripemd,md5,sha-1 */ } ret = mhash_init_int(type); if (ret != MHASH_FAILED) { /* Initial hmac calculations */ ret->hmac_block = block; if ( ret->hmac_block > MAX_BLOCK_SIZE) { ipad = mutils_malloc(ret->hmac_block); if (ipad == NULL) { return MHASH_FAILED; } ipad_alloc = MUTILS_TRUE; } else { ipad = _ipad; } if (keysize > ret->hmac_block) { tmptd = mhash_init(type); mhash(tmptd, key, keysize); ret->hmac_key_size = mhash_get_block_size(type); ret->hmac_key = mhash_end(tmptd); } else { ret->hmac_key = mutils_malloc(ret->hmac_block); mutils_bzero(ret->hmac_key, ret->hmac_block); mutils_memcpy(ret->hmac_key, key, keysize); ret->hmac_key_size = ret->hmac_block; } /* IPAD */ for (i = 0; i < ret->hmac_key_size; i++) { ipad[i] = (0x36) ^ ret->hmac_key[i]; } for (; i < ret->hmac_block; i++) { ipad[i] = (0x36); } res = mhash(ret, ipad, ret->hmac_block); if (ipad_alloc == MUTILS_TRUE) { mutils_free(ipad); } } return(ret); }
WIN32DLL_DEFINE mutils_error mhash_hmac_deinit(MHASH td, void *result) { mutils_word8 *opad; mutils_word8 _opad[MAX_BLOCK_SIZE]; MHASH tmptd; mutils_word32 i; mutils_word32 opad_alloc = 0; if (td->hmac_block > MAX_BLOCK_SIZE) { opad = mutils_malloc(td->hmac_block); if (opad == NULL) { return(-MUTILS_SYSTEM_RESOURCE_ERROR); } opad_alloc = 1; } else { opad = _opad; } for (i = 0; i < td->hmac_key_size; i++) { opad[i] = (0x5C) ^ td->hmac_key[i]; } for (; i < td->hmac_block; i++) { opad[i] = (0x5C); } tmptd = mhash_init(td->algorithm_given); mhash(tmptd, opad, td->hmac_block); if (td->final_func != NULL) { td->final_func(td->state); } if (td->deinit_func != NULL) { td->deinit_func(td->state, result); } if (result != NULL) { mhash(tmptd, result, mhash_get_block_size(td->algorithm_given)); } mutils_free(td->state); if (opad_alloc!=0) { mutils_free(opad); } mutils_bzero(td->hmac_key, td->hmac_key_size); mutils_free(td->hmac_key); mutils_free(td); mhash_deinit(tmptd, result); return(MUTILS_OK); }