| // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/hmac.h> |
| 16 |
| 17 | #include <assert.h> |
| 18 | #include <string.h> |
| 19 |
| 20 | #include <openssl/digest.h> |
| 21 | #include <openssl/mem.h> |
| 22 |
| 23 | #include "../../internal.h" |
| 24 | #include "../service_indicator/internal.h" |
| 25 |
| 26 |
| 27 | uint8_t *HMAC(const EVP_MD *evp_md, const void *key, size_t key_len, |
| 28 | const uint8_t *data, size_t data_len, uint8_t *out, |
| 29 | unsigned int *out_len) { |
| 30 | HMAC_CTX ctx; |
| 31 | HMAC_CTX_init(&ctx); |
| 32 |
| 33 | // The underlying hash functions should not set the FIPS service indicator |
| 34 | // until all operations have completed. |
| 35 | FIPS_service_indicator_lock_state(); |
| 36 | const int ok = HMAC_Init_ex(&ctx, key, key_len, evp_md, NULL) && |
| 37 | HMAC_Update(&ctx, data, data_len) && |
| 38 | HMAC_Final(&ctx, out, out_len); |
| 39 | FIPS_service_indicator_unlock_state(); |
| 40 |
| 41 | HMAC_CTX_cleanup(&ctx); |
| 42 |
| 43 | if (!ok) { |
| 44 | return NULL; |
| 45 | } |
| 46 |
| 47 | HMAC_verify_service_indicator(evp_md); |
| 48 | return out; |
| 49 | } |
| 50 |
| 51 | void HMAC_CTX_init(HMAC_CTX *ctx) { |
| 52 | ctx->md = NULL; |
| 53 | EVP_MD_CTX_init(&ctx->i_ctx); |
| 54 | EVP_MD_CTX_init(&ctx->o_ctx); |
| 55 | EVP_MD_CTX_init(&ctx->md_ctx); |
| 56 | } |
| 57 |
| 58 | HMAC_CTX *HMAC_CTX_new(void) { |
| 59 | HMAC_CTX *ctx = |
| 60 | reinterpret_cast<HMAC_CTX *>(OPENSSL_malloc(sizeof(HMAC_CTX))); |
| 61 | if (ctx != NULL) { |
| 62 | HMAC_CTX_init(ctx); |
| 63 | } |
| 64 | return ctx; |
| 65 | } |
| 66 |
| 67 | void HMAC_CTX_cleanup(HMAC_CTX *ctx) { |
| 68 | EVP_MD_CTX_cleanup(&ctx->i_ctx); |
| 69 | EVP_MD_CTX_cleanup(&ctx->o_ctx); |
| 70 | EVP_MD_CTX_cleanup(&ctx->md_ctx); |
| 71 | OPENSSL_cleanse(ctx, sizeof(HMAC_CTX)); |
| 72 | } |
| 73 |
| 74 | void HMAC_CTX_cleanse(HMAC_CTX *ctx) { |
| 75 | EVP_MD_CTX_cleanse(&ctx->i_ctx); |
| 76 | EVP_MD_CTX_cleanse(&ctx->o_ctx); |
| 77 | EVP_MD_CTX_cleanse(&ctx->md_ctx); |
| 78 | OPENSSL_cleanse(ctx, sizeof(HMAC_CTX)); |
| 79 | } |
| 80 |
| 81 | void HMAC_CTX_free(HMAC_CTX *ctx) { |
| 82 | if (ctx == NULL) { |
| 83 | return; |
| 84 | } |
| 85 |
| 86 | HMAC_CTX_cleanup(ctx); |
| 87 | OPENSSL_free(ctx); |
| 88 | } |
| 89 |
| 90 | int HMAC_Init_ex(HMAC_CTX *ctx, const void *key, size_t key_len, |
| 91 | const EVP_MD *md, ENGINE *impl) { |
| 92 | int ret = 0; |
| 93 | FIPS_service_indicator_lock_state(); |
| 94 |
| 95 | if (md == NULL) { |
| 96 | md = ctx->md; |
| 97 | } |
| 98 |
| 99 | // If either |key| is non-NULL or |md| has changed, initialize with a new key |
| 100 | // rather than rewinding the previous one. |
| 101 | // |
| 102 | // TODO(davidben,eroman): Passing the previous |md| with a NULL |key| is |
| 103 | // ambiguous between using the empty key and reusing the previous key. There |
| 104 | // exist callers which intend the latter, but the former is an awkward edge |
| 105 | // case. Fix to API to avoid this. |
| 106 | if (md != ctx->md || key != NULL) { |
| 107 | uint8_t pad[EVP_MAX_MD_BLOCK_SIZE]; |
| 108 | uint8_t key_block[EVP_MAX_MD_BLOCK_SIZE]; |
| 109 | unsigned key_block_len; |
| 110 |
| 111 | size_t block_size = EVP_MD_block_size(md); |
| 112 | assert(block_size <= sizeof(key_block)); |
| 113 | assert(EVP_MD_size(md) <= block_size); |
| 114 | if (block_size < key_len) { |
| 115 | // Long keys are hashed. |
| 116 | if (!EVP_DigestInit_ex(&ctx->md_ctx, md, impl) || |
| 117 | !EVP_DigestUpdate(&ctx->md_ctx, key, key_len) || |
| 118 | !EVP_DigestFinal_ex(&ctx->md_ctx, key_block, &key_block_len)) { |
| 119 | goto out; |
| 120 | } |
| 121 | } else { |
| 122 | assert(key_len <= sizeof(key_block)); |
| 123 | OPENSSL_memcpy(key_block, key, key_len); |
| 124 | key_block_len = (unsigned)key_len; |
| 125 | } |
| 126 | // Keys are then padded with zeros. |
| 127 | OPENSSL_memset(key_block + key_block_len, 0, block_size - key_block_len); |
| 128 |
| 129 | for (size_t i = 0; i < block_size; i++) { |
| 130 | pad[i] = 0x36 ^ key_block[i]; |
| 131 | } |
| 132 | if (!EVP_DigestInit_ex(&ctx->i_ctx, md, impl) || |
| 133 | !EVP_DigestUpdate(&ctx->i_ctx, pad, block_size)) { |
| 134 | goto out; |
| 135 | } |
| 136 |
| 137 | for (size_t i = 0; i < block_size; i++) { |
| 138 | pad[i] = 0x5c ^ key_block[i]; |
| 139 | } |
| 140 | if (!EVP_DigestInit_ex(&ctx->o_ctx, md, impl) || |
| 141 | !EVP_DigestUpdate(&ctx->o_ctx, pad, block_size)) { |
| 142 | goto out; |
| 143 | } |
| 144 |
| 145 | ctx->md = md; |
| 146 | } |
| 147 |
| 148 | ret = EVP_MD_CTX_copy_ex(&ctx->md_ctx, &ctx->i_ctx); |
| 149 |
| 150 | out: |
| 151 | FIPS_service_indicator_unlock_state(); |
| 152 | return ret; |
| 153 | } |
| 154 |
| 155 | int HMAC_Update(HMAC_CTX *ctx, const uint8_t *data, size_t data_len) { |
| 156 | return EVP_DigestUpdate(&ctx->md_ctx, data, data_len); |
| 157 | } |
| 158 |
| 159 | int HMAC_Final(HMAC_CTX *ctx, uint8_t *out, unsigned int *out_len) { |
| 160 | int ret = 0; |
| 161 | unsigned int i; |
| 162 | uint8_t buf[EVP_MAX_MD_SIZE]; |
| 163 |
| 164 | FIPS_service_indicator_lock_state(); |
| 165 | // TODO(davidben): The only thing that can officially fail here is |
| 166 | // |EVP_MD_CTX_copy_ex|, but even that should be impossible in this case. |
| 167 | if (!EVP_DigestFinal_ex(&ctx->md_ctx, buf, &i) || |
| 168 | !EVP_MD_CTX_copy_ex(&ctx->md_ctx, &ctx->o_ctx) || |
| 169 | !EVP_DigestUpdate(&ctx->md_ctx, buf, i) || |
| 170 | !EVP_DigestFinal_ex(&ctx->md_ctx, out, out_len)) { |
| 171 | *out_len = 0; |
| 172 | goto out; |
| 173 | } |
| 174 |
| 175 | ret = 1; |
| 176 |
| 177 | out: |
| 178 | FIPS_service_indicator_unlock_state(); |
| 179 | if (ret) { |
| 180 | HMAC_verify_service_indicator(ctx->md); |
| 181 | } |
| 182 | return ret; |
| 183 | } |
| 184 |
| 185 | size_t HMAC_size(const HMAC_CTX *ctx) { return EVP_MD_size(ctx->md); } |
| 186 |
| 187 | const EVP_MD *HMAC_CTX_get_md(const HMAC_CTX *ctx) { return ctx->md; } |
| 188 |
| 189 | int HMAC_CTX_copy_ex(HMAC_CTX *dest, const HMAC_CTX *src) { |
| 190 | if (!EVP_MD_CTX_copy_ex(&dest->i_ctx, &src->i_ctx) || |
| 191 | !EVP_MD_CTX_copy_ex(&dest->o_ctx, &src->o_ctx) || |
| 192 | !EVP_MD_CTX_copy_ex(&dest->md_ctx, &src->md_ctx)) { |
| 193 | return 0; |
| 194 | } |
| 195 |
| 196 | dest->md = src->md; |
| 197 | return 1; |
| 198 | } |
| 199 |
| 200 | void HMAC_CTX_reset(HMAC_CTX *ctx) { |
| 201 | HMAC_CTX_cleanup(ctx); |
| 202 | HMAC_CTX_init(ctx); |
| 203 | } |
| 204 |
| 205 | int HMAC_Init(HMAC_CTX *ctx, const void *key, int key_len, const EVP_MD *md) { |
| 206 | if (key && md) { |
| 207 | HMAC_CTX_init(ctx); |
| 208 | } |
| 209 | return HMAC_Init_ex(ctx, key, key_len, md, NULL); |
| 210 | } |
| 211 |
| 212 | int HMAC_CTX_copy(HMAC_CTX *dest, const HMAC_CTX *src) { |
| 213 | HMAC_CTX_init(dest); |
| 214 | return HMAC_CTX_copy_ex(dest, src); |
| 215 | } |
| 216 |