c - HMAC_Init_ex Corrupting Stack Space -
i'm trying create sha256 hash key using openssl's hmac functions. stack keeps getting corrupted (every value set 0) after call hmac_init_ex. i'm using xcode , running os x 10.8.5. running "openssl version" in terminal outputs "openssl 0.9.8y 5 feb 2013".
here function , #include
s:
#include <stdio.h> #include <openssl/hmac.h> char* hash(char *str, char* key){ int inputlen = strlen(str); int keylen = strlen(key); hmac_ctx ctx; hmac_ctx_init(&ctx); hmac_init_ex(&ctx, key, keylen, evp_sha256(), null); // fine here. hmac_update(&ctx, str, inputlen); // time line runs, str , key null, , inputlen , keylen 0. char* ret = malloc(65*sizeof(char)); hmac_final(&ctx, ret, 65); hmac_ctx_cleanup(&ctx); ret[65] = '\0'; return ret; }
this code should working. has libraries, don't know what. did wrong when importing libraries?
update: found example here uses encapsulated hmac
function , says same doing before, , strangely, works. have circumvented problem, still answer in case helps else. except it's weird, specific issue libraries. working function:
char* hash(char *str, char* key){ int inputlen = strlen(str); int keylen = strlen(key); unsigned int retlen = 65; char* ret = emalloc(retlen*sizeof(char)); ret = hmac(evp_sha256(), key, keylen, (unsigned char*)str, inputlen, null, null); return ret; }
you missed openssl/initialization.the engine or ssh config provides cipher methods, digest methods, etc.
#include <openssl/engine.h> #include <openssl/hmac.h> hmac_ctx ctx; result = (unsigned char*) malloc(sizeof(char) * result_len); engine_load_builtin_engines(); engine_register_all_complete(); hmac_ctx_init(&ctx); hmac_init_ex(&ctx, key, 16, evp_sha256(), null); hmac_update(&ctx, data, 8); hmac_final(&ctx, result, &result_len); hmac_ctx_cleanup(&ctx);
Comments
Post a Comment