int fko_set_spa_hmac(fko_ctx_t ctx, const char * const hmac_key, const int hmac_key_len) { unsigned char hmac[SHA512_DIGEST_STR_LEN] = {0}; char *hmac_base64 = NULL; int hmac_digest_str_len = 0; int hmac_digest_len = 0; /* Must be initialized */ if(!CTX_INITIALIZED(ctx)) return(FKO_ERROR_CTX_NOT_INITIALIZED); if(hmac_key_len > MAX_DIGEST_BLOCK_LEN) return(FKO_ERROR_INVALID_HMAC_KEY_LEN); if(ctx->hmac_type == FKO_HMAC_MD5) { hmac_md5(ctx->encrypted_msg, ctx->encrypted_msg_len, hmac, hmac_key, hmac_key_len); hmac_digest_len = MD5_DIGEST_LEN; hmac_digest_str_len = MD5_DIGEST_STR_LEN; } else if(ctx->hmac_type == FKO_HMAC_SHA1) { hmac_sha1(ctx->encrypted_msg, ctx->encrypted_msg_len, hmac, hmac_key, hmac_key_len); hmac_digest_len = SHA1_DIGEST_LEN; hmac_digest_str_len = SHA1_DIGEST_STR_LEN; } else if(ctx->hmac_type == FKO_HMAC_SHA256) { hmac_sha256(ctx->encrypted_msg, ctx->encrypted_msg_len, hmac, hmac_key, hmac_key_len); hmac_digest_len = SHA256_DIGEST_LEN; hmac_digest_str_len = SHA256_DIGEST_STR_LEN; } else if(ctx->hmac_type == FKO_HMAC_SHA384) { hmac_sha384(ctx->encrypted_msg, ctx->encrypted_msg_len, hmac, hmac_key, hmac_key_len); hmac_digest_len = SHA384_DIGEST_LEN; hmac_digest_str_len = SHA384_DIGEST_STR_LEN; } else if(ctx->hmac_type == FKO_HMAC_SHA512) { hmac_sha512(ctx->encrypted_msg, ctx->encrypted_msg_len, hmac, hmac_key, hmac_key_len); hmac_digest_len = SHA512_DIGEST_LEN; hmac_digest_str_len = SHA512_DIGEST_STR_LEN; } hmac_base64 = calloc(1, MD_HEX_SIZE(hmac_digest_len)+1); if (hmac_base64 == NULL) return(FKO_ERROR_MEMORY_ALLOCATION); b64_encode(hmac, hmac_base64, hmac_digest_len); strip_b64_eq(hmac_base64); if(ctx->msg_hmac != NULL) free(ctx->msg_hmac); ctx->msg_hmac = strdup(hmac_base64); ctx->msg_hmac_len = strnlen(ctx->msg_hmac, hmac_digest_str_len); free(hmac_base64); switch(ctx->msg_hmac_len) { case MD5_B64_LEN: break; case SHA1_B64_LEN: break; case SHA256_B64_LEN: break; case SHA384_B64_LEN: break; case SHA512_B64_LEN: break; default: return(FKO_ERROR_INVALID_DATA_HMAC_LEN_VALIDFAIL); } return FKO_SUCCESS; }
static int set_digest(char *data, char **digest, short digest_type, int *digest_len) { char *md = NULL; int data_len; data_len = strnlen(data, MAX_SPA_ENCODED_MSG_SIZE); if(data_len == MAX_SPA_ENCODED_MSG_SIZE) return(FKO_ERROR_INVALID_DATA_ENCODE_DIGEST_TOOBIG); switch(digest_type) { case FKO_DIGEST_MD5: md = calloc(1, MD_HEX_SIZE(MD5_DIGEST_LEN)+1); if(md == NULL) return(FKO_ERROR_MEMORY_ALLOCATION); md5_base64(md, (unsigned char*)data, data_len); *digest_len = MD5_B64_LEN; break; case FKO_DIGEST_SHA1: md = calloc(1, MD_HEX_SIZE(SHA1_DIGEST_LEN)+1); if(md == NULL) return(FKO_ERROR_MEMORY_ALLOCATION); sha1_base64(md, (unsigned char*)data, data_len); *digest_len = SHA1_B64_LEN; break; case FKO_DIGEST_SHA256: md = calloc(1, MD_HEX_SIZE(SHA256_DIGEST_LEN)+1); if(md == NULL) return(FKO_ERROR_MEMORY_ALLOCATION); sha256_base64(md, (unsigned char*)data, data_len); *digest_len = SHA256_B64_LEN; break; case FKO_DIGEST_SHA384: md = calloc(1, MD_HEX_SIZE(SHA384_DIGEST_LEN)+1); if(md == NULL) return(FKO_ERROR_MEMORY_ALLOCATION); sha384_base64(md, (unsigned char*)data, data_len); *digest_len = SHA384_B64_LEN; break; case FKO_DIGEST_SHA512: md = calloc(1, MD_HEX_SIZE(SHA512_DIGEST_LEN)+1); if(md == NULL) return(FKO_ERROR_MEMORY_ALLOCATION); sha512_base64(md, (unsigned char*)data, data_len); *digest_len = SHA512_B64_LEN; break; default: return(FKO_ERROR_INVALID_DIGEST_TYPE); } /* Just in case this is a subsquent call to this function. We * do not want to be leaking memory. */ if(*digest != NULL) free(*digest); *digest = md; return(FKO_SUCCESS); }