Coverage Report

Created: 2024-11-21 06:47

/src/boringssl/crypto/fipsmodule/hmac/hmac.c.inc
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2
 * All rights reserved.
3
 *
4
 * This package is an SSL implementation written
5
 * by Eric Young (eay@cryptsoft.com).
6
 * The implementation was written so as to conform with Netscapes SSL.
7
 *
8
 * This library is free for commercial and non-commercial use as long as
9
 * the following conditions are aheared to.  The following conditions
10
 * apply to all code found in this distribution, be it the RC4, RSA,
11
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12
 * included with this distribution is covered by the same copyright terms
13
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14
 *
15
 * Copyright remains Eric Young's, and as such any Copyright notices in
16
 * the code are not to be removed.
17
 * If this package is used in a product, Eric Young should be given attribution
18
 * as the author of the parts of the library used.
19
 * This can be in the form of a textual message at program startup or
20
 * in documentation (online or textual) provided with the package.
21
 *
22
 * Redistribution and use in source and binary forms, with or without
23
 * modification, are permitted provided that the following conditions
24
 * are met:
25
 * 1. Redistributions of source code must retain the copyright
26
 *    notice, this list of conditions and the following disclaimer.
27
 * 2. Redistributions in binary form must reproduce the above copyright
28
 *    notice, this list of conditions and the following disclaimer in the
29
 *    documentation and/or other materials provided with the distribution.
30
 * 3. All advertising materials mentioning features or use of this software
31
 *    must display the following acknowledgement:
32
 *    "This product includes cryptographic software written by
33
 *     Eric Young (eay@cryptsoft.com)"
34
 *    The word 'cryptographic' can be left out if the rouines from the library
35
 *    being used are not cryptographic related :-).
36
 * 4. If you include any Windows specific code (or a derivative thereof) from
37
 *    the apps directory (application code) you must include an acknowledgement:
38
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39
 *
40
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50
 * SUCH DAMAGE.
51
 *
52
 * The licence and distribution terms for any publically available version or
53
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
54
 * copied and put under another distribution licence
55
 * [including the GNU Public Licence.] */
56
57
#include <openssl/hmac.h>
58
59
#include <assert.h>
60
#include <string.h>
61
62
#include <openssl/digest.h>
63
#include <openssl/mem.h>
64
65
#include "../../internal.h"
66
#include "../service_indicator/internal.h"
67
68
69
uint8_t *HMAC(const EVP_MD *evp_md, const void *key, size_t key_len,
70
              const uint8_t *data, size_t data_len, uint8_t *out,
71
0
              unsigned int *out_len) {
72
0
  HMAC_CTX ctx;
73
0
  HMAC_CTX_init(&ctx);
74
75
  // The underlying hash functions should not set the FIPS service indicator
76
  // until all operations have completed.
77
0
  FIPS_service_indicator_lock_state();
78
0
  const int ok = HMAC_Init_ex(&ctx, key, key_len, evp_md, NULL) &&
79
0
                 HMAC_Update(&ctx, data, data_len) &&
80
0
                 HMAC_Final(&ctx, out, out_len);
81
0
  FIPS_service_indicator_unlock_state();
82
83
0
  HMAC_CTX_cleanup(&ctx);
84
85
0
  if (!ok) {
86
0
    return NULL;
87
0
  }
88
89
0
  HMAC_verify_service_indicator(evp_md);
90
0
  return out;
91
0
}
92
93
0
void HMAC_CTX_init(HMAC_CTX *ctx) {
94
0
  ctx->md = NULL;
95
0
  EVP_MD_CTX_init(&ctx->i_ctx);
96
0
  EVP_MD_CTX_init(&ctx->o_ctx);
97
0
  EVP_MD_CTX_init(&ctx->md_ctx);
98
0
}
99
100
0
HMAC_CTX *HMAC_CTX_new(void) {
101
0
  HMAC_CTX *ctx = OPENSSL_malloc(sizeof(HMAC_CTX));
102
0
  if (ctx != NULL) {
103
0
    HMAC_CTX_init(ctx);
104
0
  }
105
0
  return ctx;
106
0
}
107
108
0
void HMAC_CTX_cleanup(HMAC_CTX *ctx) {
109
0
  EVP_MD_CTX_cleanup(&ctx->i_ctx);
110
0
  EVP_MD_CTX_cleanup(&ctx->o_ctx);
111
0
  EVP_MD_CTX_cleanup(&ctx->md_ctx);
112
0
  OPENSSL_cleanse(ctx, sizeof(HMAC_CTX));
113
0
}
114
115
0
void HMAC_CTX_cleanse(HMAC_CTX *ctx) {
116
0
  EVP_MD_CTX_cleanse(&ctx->i_ctx);
117
0
  EVP_MD_CTX_cleanse(&ctx->o_ctx);
118
0
  EVP_MD_CTX_cleanse(&ctx->md_ctx);
119
0
  OPENSSL_cleanse(ctx, sizeof(HMAC_CTX));
120
0
}
121
122
0
void HMAC_CTX_free(HMAC_CTX *ctx) {
123
0
  if (ctx == NULL) {
124
0
    return;
125
0
  }
126
127
0
  HMAC_CTX_cleanup(ctx);
128
0
  OPENSSL_free(ctx);
129
0
}
130
131
int HMAC_Init_ex(HMAC_CTX *ctx, const void *key, size_t key_len,
132
0
                 const EVP_MD *md, ENGINE *impl) {
133
0
  int ret = 0;
134
0
  FIPS_service_indicator_lock_state();
135
136
0
  if (md == NULL) {
137
0
    md = ctx->md;
138
0
  }
139
140
  // If either |key| is non-NULL or |md| has changed, initialize with a new key
141
  // rather than rewinding the previous one.
142
  //
143
  // TODO(davidben,eroman): Passing the previous |md| with a NULL |key| is
144
  // ambiguous between using the empty key and reusing the previous key. There
145
  // exist callers which intend the latter, but the former is an awkward edge
146
  // case. Fix to API to avoid this.
147
0
  if (md != ctx->md || key != NULL) {
148
0
    uint8_t pad[EVP_MAX_MD_BLOCK_SIZE];
149
0
    uint8_t key_block[EVP_MAX_MD_BLOCK_SIZE];
150
0
    unsigned key_block_len;
151
152
0
    size_t block_size = EVP_MD_block_size(md);
153
0
    assert(block_size <= sizeof(key_block));
154
0
    assert(EVP_MD_size(md) <= block_size);
155
0
    if (block_size < key_len) {
156
      // Long keys are hashed.
157
0
      if (!EVP_DigestInit_ex(&ctx->md_ctx, md, impl) ||
158
0
          !EVP_DigestUpdate(&ctx->md_ctx, key, key_len) ||
159
0
          !EVP_DigestFinal_ex(&ctx->md_ctx, key_block, &key_block_len)) {
160
0
        goto out;
161
0
      }
162
0
    } else {
163
0
      assert(key_len <= sizeof(key_block));
164
0
      OPENSSL_memcpy(key_block, key, key_len);
165
0
      key_block_len = (unsigned)key_len;
166
0
    }
167
    // Keys are then padded with zeros.
168
0
    OPENSSL_memset(key_block + key_block_len, 0, block_size - key_block_len);
169
170
0
    for (size_t i = 0; i < block_size; i++) {
171
0
      pad[i] = 0x36 ^ key_block[i];
172
0
    }
173
0
    if (!EVP_DigestInit_ex(&ctx->i_ctx, md, impl) ||
174
0
        !EVP_DigestUpdate(&ctx->i_ctx, pad, block_size)) {
175
0
      goto out;
176
0
    }
177
178
0
    for (size_t i = 0; i < block_size; i++) {
179
0
      pad[i] = 0x5c ^ key_block[i];
180
0
    }
181
0
    if (!EVP_DigestInit_ex(&ctx->o_ctx, md, impl) ||
182
0
        !EVP_DigestUpdate(&ctx->o_ctx, pad, block_size)) {
183
0
      goto out;
184
0
    }
185
186
0
    ctx->md = md;
187
0
  }
188
189
0
  ret = EVP_MD_CTX_copy_ex(&ctx->md_ctx, &ctx->i_ctx);
190
191
0
out:
192
0
  FIPS_service_indicator_unlock_state();
193
0
  return ret;
194
0
}
195
196
0
int HMAC_Update(HMAC_CTX *ctx, const uint8_t *data, size_t data_len) {
197
0
  return EVP_DigestUpdate(&ctx->md_ctx, data, data_len);
198
0
}
199
200
0
int HMAC_Final(HMAC_CTX *ctx, uint8_t *out, unsigned int *out_len) {
201
0
  int ret = 0;
202
0
  unsigned int i;
203
0
  uint8_t buf[EVP_MAX_MD_SIZE];
204
205
0
  FIPS_service_indicator_lock_state();
206
  // TODO(davidben): The only thing that can officially fail here is
207
  // |EVP_MD_CTX_copy_ex|, but even that should be impossible in this case.
208
0
  if (!EVP_DigestFinal_ex(&ctx->md_ctx, buf, &i) ||
209
0
      !EVP_MD_CTX_copy_ex(&ctx->md_ctx, &ctx->o_ctx) ||
210
0
      !EVP_DigestUpdate(&ctx->md_ctx, buf, i) ||
211
0
      !EVP_DigestFinal_ex(&ctx->md_ctx, out, out_len)) {
212
0
    *out_len = 0;
213
0
    goto out;
214
0
  }
215
216
0
  ret = 1;
217
218
0
 out:
219
0
  FIPS_service_indicator_unlock_state();
220
0
  if (ret) {
221
0
    HMAC_verify_service_indicator(ctx->md);
222
0
  }
223
0
  return ret;
224
0
}
225
226
0
size_t HMAC_size(const HMAC_CTX *ctx) { return EVP_MD_size(ctx->md); }
227
228
0
const EVP_MD *HMAC_CTX_get_md(const HMAC_CTX *ctx) { return ctx->md; }
229
230
0
int HMAC_CTX_copy_ex(HMAC_CTX *dest, const HMAC_CTX *src) {
231
0
  if (!EVP_MD_CTX_copy_ex(&dest->i_ctx, &src->i_ctx) ||
232
0
      !EVP_MD_CTX_copy_ex(&dest->o_ctx, &src->o_ctx) ||
233
0
      !EVP_MD_CTX_copy_ex(&dest->md_ctx, &src->md_ctx)) {
234
0
    return 0;
235
0
  }
236
237
0
  dest->md = src->md;
238
0
  return 1;
239
0
}
240
241
0
void HMAC_CTX_reset(HMAC_CTX *ctx) {
242
0
  HMAC_CTX_cleanup(ctx);
243
0
  HMAC_CTX_init(ctx);
244
0
}
245
246
0
int HMAC_Init(HMAC_CTX *ctx, const void *key, int key_len, const EVP_MD *md) {
247
0
  if (key && md) {
248
0
    HMAC_CTX_init(ctx);
249
0
  }
250
0
  return HMAC_Init_ex(ctx, key, key_len, md, NULL);
251
0
}
252
253
0
int HMAC_CTX_copy(HMAC_CTX *dest, const HMAC_CTX *src) {
254
0
  HMAC_CTX_init(dest);
255
0
  return HMAC_CTX_copy_ex(dest, src);
256
0
}