Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/openssl/src/crypto/hmac/hmac.c |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /* | - | ||||||||||||||||||||||||
2 | * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. | - | ||||||||||||||||||||||||
3 | * | - | ||||||||||||||||||||||||
4 | * Licensed under the OpenSSL license (the "License"). You may not use | - | ||||||||||||||||||||||||
5 | * this file except in compliance with the License. You can obtain a copy | - | ||||||||||||||||||||||||
6 | * in the file LICENSE in the source distribution or at | - | ||||||||||||||||||||||||
7 | * https://www.openssl.org/source/license.html | - | ||||||||||||||||||||||||
8 | */ | - | ||||||||||||||||||||||||
9 | - | |||||||||||||||||||||||||
10 | #include <stdio.h> | - | ||||||||||||||||||||||||
11 | #include <stdlib.h> | - | ||||||||||||||||||||||||
12 | #include <string.h> | - | ||||||||||||||||||||||||
13 | #include "internal/cryptlib.h" | - | ||||||||||||||||||||||||
14 | #include <openssl/hmac.h> | - | ||||||||||||||||||||||||
15 | #include <openssl/opensslconf.h> | - | ||||||||||||||||||||||||
16 | #include "hmac_lcl.h" | - | ||||||||||||||||||||||||
17 | - | |||||||||||||||||||||||||
18 | int HMAC_Init_ex(HMAC_CTX *ctx, const void *key, int len, | - | ||||||||||||||||||||||||
19 | const EVP_MD *md, ENGINE *impl) | - | ||||||||||||||||||||||||
20 | { | - | ||||||||||||||||||||||||
21 | int rv = 0; | - | ||||||||||||||||||||||||
22 | int i, j, reset = 0; | - | ||||||||||||||||||||||||
23 | unsigned char pad[HMAC_MAX_MD_CBLOCK_SIZE]; | - | ||||||||||||||||||||||||
24 | - | |||||||||||||||||||||||||
25 | /* If we are changing MD then we must have a key */ | - | ||||||||||||||||||||||||
26 | if (md != NULL && md != ctx->md && (key == NULL || len < 0))
| 0-53523 | ||||||||||||||||||||||||
27 | return 0; executed 3 times by 1 test: return 0; Executed by:
| 3 | ||||||||||||||||||||||||
28 | - | |||||||||||||||||||||||||
29 | if (md != NULL) {
| 55-53520 | ||||||||||||||||||||||||
30 | reset = 1; | - | ||||||||||||||||||||||||
31 | ctx->md = md; | - | ||||||||||||||||||||||||
32 | } else if (ctx->md) { executed 53520 times by 1 test: end of block Executed by:
| 2-53520 | ||||||||||||||||||||||||
33 | md = ctx->md; | - | ||||||||||||||||||||||||
34 | } else { executed 53 times by 1 test: end of block Executed by:
| 53 | ||||||||||||||||||||||||
35 | return 0; executed 2 times by 1 test: return 0; Executed by:
| 2 | ||||||||||||||||||||||||
36 | } | - | ||||||||||||||||||||||||
37 | - | |||||||||||||||||||||||||
38 | if (key != NULL) {
| 52-53521 | ||||||||||||||||||||||||
39 | reset = 1; | - | ||||||||||||||||||||||||
40 | j = EVP_MD_block_size(md); | - | ||||||||||||||||||||||||
41 | if (!ossl_assert(j <= (int)sizeof(ctx->key)))
| 0-53521 | ||||||||||||||||||||||||
42 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||
43 | if (j < len) {
| 206-53315 | ||||||||||||||||||||||||
44 | if (!EVP_DigestInit_ex(ctx->md_ctx, md, impl)
| 0-206 | ||||||||||||||||||||||||
45 | || !EVP_DigestUpdate(ctx->md_ctx, key, len)
| 0-206 | ||||||||||||||||||||||||
46 | || !EVP_DigestFinal_ex(ctx->md_ctx, ctx->key,
| 0-206 | ||||||||||||||||||||||||
47 | &ctx->key_length))
| 0-206 | ||||||||||||||||||||||||
48 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||
49 | } else { executed 206 times by 1 test: end of block Executed by:
| 206 | ||||||||||||||||||||||||
50 | if (len < 0 || len > (int)sizeof(ctx->key))
| 0-53315 | ||||||||||||||||||||||||
51 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||
52 | memcpy(ctx->key, key, len); | - | ||||||||||||||||||||||||
53 | ctx->key_length = len; | - | ||||||||||||||||||||||||
54 | } executed 53315 times by 1 test: end of block Executed by:
| 53315 | ||||||||||||||||||||||||
55 | if (ctx->key_length != HMAC_MAX_MD_CBLOCK_SIZE)
| 1-53520 | ||||||||||||||||||||||||
56 | memset(&ctx->key[ctx->key_length], 0, executed 53520 times by 1 test: memset(&ctx->key[ctx->key_length], 0, 144 - ctx->key_length); Executed by:
| 53520 | ||||||||||||||||||||||||
57 | HMAC_MAX_MD_CBLOCK_SIZE - ctx->key_length); executed 53520 times by 1 test: memset(&ctx->key[ctx->key_length], 0, 144 - ctx->key_length); Executed by:
| 53520 | ||||||||||||||||||||||||
58 | } executed 53521 times by 1 test: end of block Executed by:
| 53521 | ||||||||||||||||||||||||
59 | - | |||||||||||||||||||||||||
60 | if (reset) {
| 52-53521 | ||||||||||||||||||||||||
61 | for (i = 0; i < HMAC_MAX_MD_CBLOCK_SIZE; i++)
| 53521-7707024 | ||||||||||||||||||||||||
62 | pad[i] = 0x36 ^ ctx->key[i]; executed 7707024 times by 1 test: pad[i] = 0x36 ^ ctx->key[i]; Executed by:
| 7707024 | ||||||||||||||||||||||||
63 | if (!EVP_DigestInit_ex(ctx->i_ctx, md, impl)
| 0-53521 | ||||||||||||||||||||||||
64 | || !EVP_DigestUpdate(ctx->i_ctx, pad, EVP_MD_block_size(md)))
| 0-53521 | ||||||||||||||||||||||||
65 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||
66 | - | |||||||||||||||||||||||||
67 | for (i = 0; i < HMAC_MAX_MD_CBLOCK_SIZE; i++)
| 53521-7707024 | ||||||||||||||||||||||||
68 | pad[i] = 0x5c ^ ctx->key[i]; executed 7707024 times by 1 test: pad[i] = 0x5c ^ ctx->key[i]; Executed by:
| 7707024 | ||||||||||||||||||||||||
69 | if (!EVP_DigestInit_ex(ctx->o_ctx, md, impl)
| 0-53521 | ||||||||||||||||||||||||
70 | || !EVP_DigestUpdate(ctx->o_ctx, pad, EVP_MD_block_size(md)))
| 0-53521 | ||||||||||||||||||||||||
71 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||
72 | } executed 53521 times by 1 test: end of block Executed by:
| 53521 | ||||||||||||||||||||||||
73 | if (!EVP_MD_CTX_copy_ex(ctx->md_ctx, ctx->i_ctx))
| 0-53573 | ||||||||||||||||||||||||
74 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||
75 | rv = 1; | - | ||||||||||||||||||||||||
76 | err: code before this statement executed 53573 times by 1 test: err: Executed by:
| 53573 | ||||||||||||||||||||||||
77 | if (reset)
| 52-53521 | ||||||||||||||||||||||||
78 | OPENSSL_cleanse(pad, sizeof(pad)); executed 53521 times by 1 test: OPENSSL_cleanse(pad, sizeof(pad)); Executed by:
| 53521 | ||||||||||||||||||||||||
79 | return rv; executed 53573 times by 1 test: return rv; Executed by:
| 53573 | ||||||||||||||||||||||||
80 | } | - | ||||||||||||||||||||||||
81 | - | |||||||||||||||||||||||||
82 | #if OPENSSL_API_COMPAT < 0x10100000L | - | ||||||||||||||||||||||||
83 | int HMAC_Init(HMAC_CTX *ctx, const void *key, int len, const EVP_MD *md) | - | ||||||||||||||||||||||||
84 | { | - | ||||||||||||||||||||||||
85 | if (key && md)
| 0 | ||||||||||||||||||||||||
86 | HMAC_CTX_reset(ctx); never executed: HMAC_CTX_reset(ctx); | 0 | ||||||||||||||||||||||||
87 | return HMAC_Init_ex(ctx, key, len, md, NULL); never executed: return HMAC_Init_ex(ctx, key, len, md, ((void *)0) ); | 0 | ||||||||||||||||||||||||
88 | } | - | ||||||||||||||||||||||||
89 | #endif | - | ||||||||||||||||||||||||
90 | - | |||||||||||||||||||||||||
91 | int HMAC_Update(HMAC_CTX *ctx, const unsigned char *data, size_t len) | - | ||||||||||||||||||||||||
92 | { | - | ||||||||||||||||||||||||
93 | if (!ctx->md)
| 3-218860 | ||||||||||||||||||||||||
94 | return 0; executed 3 times by 1 test: return 0; Executed by:
| 3 | ||||||||||||||||||||||||
95 | return EVP_DigestUpdate(ctx->md_ctx, data, len); executed 218860 times by 1 test: return EVP_DigestUpdate(ctx->md_ctx, data, len); Executed by:
| 218860 | ||||||||||||||||||||||||
96 | } | - | ||||||||||||||||||||||||
97 | - | |||||||||||||||||||||||||
98 | int HMAC_Final(HMAC_CTX *ctx, unsigned char *md, unsigned int *len) | - | ||||||||||||||||||||||||
99 | { | - | ||||||||||||||||||||||||
100 | unsigned int i; | - | ||||||||||||||||||||||||
101 | unsigned char buf[EVP_MAX_MD_SIZE]; | - | ||||||||||||||||||||||||
102 | - | |||||||||||||||||||||||||
103 | if (!ctx->md)
| 0-163219 | ||||||||||||||||||||||||
104 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||
105 | - | |||||||||||||||||||||||||
106 | if (!EVP_DigestFinal_ex(ctx->md_ctx, buf, &i))
| 0-163219 | ||||||||||||||||||||||||
107 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||
108 | if (!EVP_MD_CTX_copy_ex(ctx->md_ctx, ctx->o_ctx))
| 0-163219 | ||||||||||||||||||||||||
109 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||
110 | if (!EVP_DigestUpdate(ctx->md_ctx, buf, i))
| 0-163219 | ||||||||||||||||||||||||
111 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||
112 | if (!EVP_DigestFinal_ex(ctx->md_ctx, md, len))
| 0-163219 | ||||||||||||||||||||||||
113 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||
114 | return 1; executed 163219 times by 1 test: return 1; Executed by:
| 163219 | ||||||||||||||||||||||||
115 | err: | - | ||||||||||||||||||||||||
116 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||
117 | } | - | ||||||||||||||||||||||||
118 | - | |||||||||||||||||||||||||
119 | size_t HMAC_size(const HMAC_CTX *ctx) | - | ||||||||||||||||||||||||
120 | { | - | ||||||||||||||||||||||||
121 | int size = EVP_MD_size((ctx)->md); | - | ||||||||||||||||||||||||
122 | - | |||||||||||||||||||||||||
123 | return (size < 0) ? 0 : size; executed 221 times by 1 test: return (size < 0) ? 0 : size; Executed by:
| 0-221 | ||||||||||||||||||||||||
124 | } | - | ||||||||||||||||||||||||
125 | - | |||||||||||||||||||||||||
126 | HMAC_CTX *HMAC_CTX_new(void) | - | ||||||||||||||||||||||||
127 | { | - | ||||||||||||||||||||||||
128 | HMAC_CTX *ctx = OPENSSL_zalloc(sizeof(HMAC_CTX)); | - | ||||||||||||||||||||||||
129 | - | |||||||||||||||||||||||||
130 | if (ctx != NULL) {
| 0-215927 | ||||||||||||||||||||||||
131 | if (!HMAC_CTX_reset(ctx)) {
| 0-215927 | ||||||||||||||||||||||||
132 | HMAC_CTX_free(ctx); | - | ||||||||||||||||||||||||
133 | return NULL; never executed: return ((void *)0) ; | 0 | ||||||||||||||||||||||||
134 | } | - | ||||||||||||||||||||||||
135 | } executed 215927 times by 1 test: end of block Executed by:
| 215927 | ||||||||||||||||||||||||
136 | return ctx; executed 215927 times by 1 test: return ctx; Executed by:
| 215927 | ||||||||||||||||||||||||
137 | } | - | ||||||||||||||||||||||||
138 | - | |||||||||||||||||||||||||
139 | static void hmac_ctx_cleanup(HMAC_CTX *ctx) | - | ||||||||||||||||||||||||
140 | { | - | ||||||||||||||||||||||||
141 | EVP_MD_CTX_reset(ctx->i_ctx); | - | ||||||||||||||||||||||||
142 | EVP_MD_CTX_reset(ctx->o_ctx); | - | ||||||||||||||||||||||||
143 | EVP_MD_CTX_reset(ctx->md_ctx); | - | ||||||||||||||||||||||||
144 | ctx->md = NULL; | - | ||||||||||||||||||||||||
145 | ctx->key_length = 0; | - | ||||||||||||||||||||||||
146 | OPENSSL_cleanse(ctx->key, sizeof(ctx->key)); | - | ||||||||||||||||||||||||
147 | } executed 431855 times by 1 test: end of block Executed by:
| 431855 | ||||||||||||||||||||||||
148 | - | |||||||||||||||||||||||||
149 | void HMAC_CTX_free(HMAC_CTX *ctx) | - | ||||||||||||||||||||||||
150 | { | - | ||||||||||||||||||||||||
151 | if (ctx != NULL) {
| 1191-215927 | ||||||||||||||||||||||||
152 | hmac_ctx_cleanup(ctx); | - | ||||||||||||||||||||||||
153 | EVP_MD_CTX_free(ctx->i_ctx); | - | ||||||||||||||||||||||||
154 | EVP_MD_CTX_free(ctx->o_ctx); | - | ||||||||||||||||||||||||
155 | EVP_MD_CTX_free(ctx->md_ctx); | - | ||||||||||||||||||||||||
156 | OPENSSL_free(ctx); | - | ||||||||||||||||||||||||
157 | } executed 215927 times by 1 test: end of block Executed by:
| 215927 | ||||||||||||||||||||||||
158 | } executed 217118 times by 1 test: end of block Executed by:
| 217118 | ||||||||||||||||||||||||
159 | - | |||||||||||||||||||||||||
160 | static int hmac_ctx_alloc_mds(HMAC_CTX *ctx) | - | ||||||||||||||||||||||||
161 | { | - | ||||||||||||||||||||||||
162 | if (ctx->i_ctx == NULL)
| 207331-215927 | ||||||||||||||||||||||||
163 | ctx->i_ctx = EVP_MD_CTX_new(); executed 215927 times by 1 test: ctx->i_ctx = EVP_MD_CTX_new(); Executed by:
| 215927 | ||||||||||||||||||||||||
164 | if (ctx->i_ctx == NULL)
| 0-423258 | ||||||||||||||||||||||||
165 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||
166 | if (ctx->o_ctx == NULL)
| 207331-215927 | ||||||||||||||||||||||||
167 | ctx->o_ctx = EVP_MD_CTX_new(); executed 215927 times by 1 test: ctx->o_ctx = EVP_MD_CTX_new(); Executed by:
| 215927 | ||||||||||||||||||||||||
168 | if (ctx->o_ctx == NULL)
| 0-423258 | ||||||||||||||||||||||||
169 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||
170 | if (ctx->md_ctx == NULL)
| 207331-215927 | ||||||||||||||||||||||||
171 | ctx->md_ctx = EVP_MD_CTX_new(); executed 215927 times by 1 test: ctx->md_ctx = EVP_MD_CTX_new(); Executed by:
| 215927 | ||||||||||||||||||||||||
172 | if (ctx->md_ctx == NULL)
| 0-423258 | ||||||||||||||||||||||||
173 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||
174 | return 1; executed 423258 times by 1 test: return 1; Executed by:
| 423258 | ||||||||||||||||||||||||
175 | } | - | ||||||||||||||||||||||||
176 | - | |||||||||||||||||||||||||
177 | int HMAC_CTX_reset(HMAC_CTX *ctx) | - | ||||||||||||||||||||||||
178 | { | - | ||||||||||||||||||||||||
179 | hmac_ctx_cleanup(ctx); | - | ||||||||||||||||||||||||
180 | if (!hmac_ctx_alloc_mds(ctx)) {
| 0-215928 | ||||||||||||||||||||||||
181 | hmac_ctx_cleanup(ctx); | - | ||||||||||||||||||||||||
182 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||
183 | } | - | ||||||||||||||||||||||||
184 | return 1; executed 215928 times by 1 test: return 1; Executed by:
| 215928 | ||||||||||||||||||||||||
185 | } | - | ||||||||||||||||||||||||
186 | - | |||||||||||||||||||||||||
187 | int HMAC_CTX_copy(HMAC_CTX *dctx, HMAC_CTX *sctx) | - | ||||||||||||||||||||||||
188 | { | - | ||||||||||||||||||||||||
189 | if (!hmac_ctx_alloc_mds(dctx))
| 0-207330 | ||||||||||||||||||||||||
190 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||
191 | if (!EVP_MD_CTX_copy_ex(dctx->i_ctx, sctx->i_ctx))
| 0-207330 | ||||||||||||||||||||||||
192 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||
193 | if (!EVP_MD_CTX_copy_ex(dctx->o_ctx, sctx->o_ctx))
| 0-207330 | ||||||||||||||||||||||||
194 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||
195 | if (!EVP_MD_CTX_copy_ex(dctx->md_ctx, sctx->md_ctx))
| 0-207330 | ||||||||||||||||||||||||
196 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||
197 | memcpy(dctx->key, sctx->key, HMAC_MAX_MD_CBLOCK_SIZE); | - | ||||||||||||||||||||||||
198 | dctx->key_length = sctx->key_length; | - | ||||||||||||||||||||||||
199 | dctx->md = sctx->md; | - | ||||||||||||||||||||||||
200 | return 1; executed 207330 times by 1 test: return 1; Executed by:
| 207330 | ||||||||||||||||||||||||
201 | err: | - | ||||||||||||||||||||||||
202 | hmac_ctx_cleanup(dctx); | - | ||||||||||||||||||||||||
203 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||
204 | } | - | ||||||||||||||||||||||||
205 | - | |||||||||||||||||||||||||
206 | unsigned char *HMAC(const EVP_MD *evp_md, const void *key, int key_len, | - | ||||||||||||||||||||||||
207 | const unsigned char *d, size_t n, unsigned char *md, | - | ||||||||||||||||||||||||
208 | unsigned int *md_len) | - | ||||||||||||||||||||||||
209 | { | - | ||||||||||||||||||||||||
210 | HMAC_CTX *c = NULL; | - | ||||||||||||||||||||||||
211 | static unsigned char m[EVP_MAX_MD_SIZE]; | - | ||||||||||||||||||||||||
212 | static const unsigned char dummy_key[1] = {'\0'}; | - | ||||||||||||||||||||||||
213 | - | |||||||||||||||||||||||||
214 | if (md == NULL)
| 5-4480 | ||||||||||||||||||||||||
215 | md = m; executed 5 times by 1 test: md = m; Executed by:
| 5 | ||||||||||||||||||||||||
216 | if ((c = HMAC_CTX_new()) == NULL)
| 0-4485 | ||||||||||||||||||||||||
217 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||
218 | - | |||||||||||||||||||||||||
219 | /* For HMAC_Init_ex, NULL key signals reuse. */ | - | ||||||||||||||||||||||||
220 | if (key == NULL && key_len == 0) {
| 0-2914 | ||||||||||||||||||||||||
221 | key = dummy_key; | - | ||||||||||||||||||||||||
222 | } executed 1571 times by 1 test: end of block Executed by:
| 1571 | ||||||||||||||||||||||||
223 | - | |||||||||||||||||||||||||
224 | if (!HMAC_Init_ex(c, key, key_len, evp_md, NULL))
| 0-4485 | ||||||||||||||||||||||||
225 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||
226 | if (!HMAC_Update(c, d, n))
| 0-4485 | ||||||||||||||||||||||||
227 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||
228 | if (!HMAC_Final(c, md, md_len))
| 0-4485 | ||||||||||||||||||||||||
229 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||
230 | HMAC_CTX_free(c); | - | ||||||||||||||||||||||||
231 | return md; executed 4485 times by 1 test: return md; Executed by:
| 4485 | ||||||||||||||||||||||||
232 | err: | - | ||||||||||||||||||||||||
233 | HMAC_CTX_free(c); | - | ||||||||||||||||||||||||
234 | return NULL; never executed: return ((void *)0) ; | 0 | ||||||||||||||||||||||||
235 | } | - | ||||||||||||||||||||||||
236 | - | |||||||||||||||||||||||||
237 | void HMAC_CTX_set_flags(HMAC_CTX *ctx, unsigned long flags) | - | ||||||||||||||||||||||||
238 | { | - | ||||||||||||||||||||||||
239 | EVP_MD_CTX_set_flags(ctx->i_ctx, flags); | - | ||||||||||||||||||||||||
240 | EVP_MD_CTX_set_flags(ctx->o_ctx, flags); | - | ||||||||||||||||||||||||
241 | EVP_MD_CTX_set_flags(ctx->md_ctx, flags); | - | ||||||||||||||||||||||||
242 | } executed 19206 times by 1 test: end of block Executed by:
| 19206 | ||||||||||||||||||||||||
243 | - | |||||||||||||||||||||||||
244 | const EVP_MD *HMAC_CTX_get_md(const HMAC_CTX *ctx) | - | ||||||||||||||||||||||||
245 | { | - | ||||||||||||||||||||||||
246 | return ctx->md; executed 3 times by 1 test: return ctx->md; Executed by:
| 3 | ||||||||||||||||||||||||
247 | } | - | ||||||||||||||||||||||||
Source code | Switch to Preprocessed file |