* pg_hmac_create * * Allocate a hash context. Returns NULL on failure for an OOM. The * backend issues an error, without returning. */
| 64 | * backend issues an error, without returning. |
| 65 | */ |
| 66 | pg_hmac_ctx * |
| 67 | pg_hmac_create(pg_cryptohash_type type) |
| 68 | { |
| 69 | pg_hmac_ctx *ctx; |
| 70 | |
| 71 | ctx = ALLOC(sizeof(pg_hmac_ctx)); |
| 72 | if (ctx == NULL) |
| 73 | return NULL; |
| 74 | memset(ctx, 0, sizeof(pg_hmac_ctx)); |
| 75 | ctx->type = type; |
| 76 | |
| 77 | /* |
| 78 | * Initialize the context data. This requires to know the digest and |
| 79 | * block lengths, that depend on the type of hash used. |
| 80 | */ |
| 81 | switch (type) |
| 82 | { |
| 83 | case PG_MD5: |
| 84 | ctx->digest_size = MD5_DIGEST_LENGTH; |
| 85 | ctx->block_size = MD5_BLOCK_SIZE; |
| 86 | break; |
| 87 | case PG_SHA1: |
| 88 | ctx->digest_size = SHA1_DIGEST_LENGTH; |
| 89 | ctx->block_size = SHA1_BLOCK_SIZE; |
| 90 | break; |
| 91 | case PG_SHA224: |
| 92 | ctx->digest_size = PG_SHA224_DIGEST_LENGTH; |
| 93 | ctx->block_size = PG_SHA224_BLOCK_LENGTH; |
| 94 | break; |
| 95 | case PG_SHA256: |
| 96 | ctx->digest_size = PG_SHA256_DIGEST_LENGTH; |
| 97 | ctx->block_size = PG_SHA256_BLOCK_LENGTH; |
| 98 | break; |
| 99 | case PG_SHA384: |
| 100 | ctx->digest_size = PG_SHA384_DIGEST_LENGTH; |
| 101 | ctx->block_size = PG_SHA384_BLOCK_LENGTH; |
| 102 | break; |
| 103 | case PG_SHA512: |
| 104 | ctx->digest_size = PG_SHA512_DIGEST_LENGTH; |
| 105 | ctx->block_size = PG_SHA512_BLOCK_LENGTH; |
| 106 | break; |
| 107 | case PG_SM3: |
| 108 | ctx->digest_size = PG_SM3_DIGEST_LENGTH; |
| 109 | ctx->block_size = PG_SM3_BLOCK_LENGTH; |
| 110 | break; |
| 111 | } |
| 112 | |
| 113 | ctx->hash = pg_cryptohash_create(type); |
| 114 | if (ctx->hash == NULL) |
| 115 | { |
| 116 | explicit_bzero(ctx, sizeof(pg_hmac_ctx)); |
| 117 | FREE(ctx); |
| 118 | return NULL; |
| 119 | } |
| 120 | |
| 121 | return ctx; |
| 122 | } |
| 123 |
no test coverage detected