/src/boringssl/crypto/fipsmodule/digestsign/digestsign.cc.inc
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
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/evp.h> |
16 | | |
17 | | #include <openssl/err.h> |
18 | | |
19 | | #include "../../evp/internal.h" |
20 | | #include "../delocate.h" |
21 | | #include "../digest/internal.h" |
22 | | #include "../service_indicator/internal.h" |
23 | | |
24 | | |
25 | | enum evp_sign_verify_t { |
26 | | evp_sign, |
27 | | evp_verify, |
28 | | }; |
29 | | |
30 | 6 | DEFINE_LOCAL_DATA(struct evp_md_pctx_ops, md_pctx_ops) { |
31 | 6 | out->free = EVP_PKEY_CTX_free; |
32 | 6 | out->dup = EVP_PKEY_CTX_dup; |
33 | 6 | } |
34 | | |
35 | 396k | static int uses_prehash(EVP_MD_CTX *ctx, enum evp_sign_verify_t op) { |
36 | 396k | return (op == evp_sign) ? (ctx->pctx->pmeth->sign != NULL) |
37 | 396k | : (ctx->pctx->pmeth->verify != NULL); |
38 | 396k | } |
39 | | |
40 | | static int do_sigver_init(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, |
41 | | const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey, |
42 | 99.2k | enum evp_sign_verify_t op) { |
43 | 99.2k | if (ctx->pctx == NULL) { |
44 | 99.2k | ctx->pctx = EVP_PKEY_CTX_new(pkey, e); |
45 | 99.2k | } |
46 | 99.2k | if (ctx->pctx == NULL) { |
47 | 0 | return 0; |
48 | 0 | } |
49 | 99.2k | ctx->pctx_ops = md_pctx_ops(); |
50 | | |
51 | 99.2k | if (op == evp_verify) { |
52 | 57.8k | if (!EVP_PKEY_verify_init(ctx->pctx)) { |
53 | 0 | return 0; |
54 | 0 | } |
55 | 57.8k | } else { |
56 | 41.3k | if (!EVP_PKEY_sign_init(ctx->pctx)) { |
57 | 0 | return 0; |
58 | 0 | } |
59 | 41.3k | } |
60 | | |
61 | 99.2k | if (type != NULL && |
62 | 99.2k | !EVP_PKEY_CTX_set_signature_md(ctx->pctx, type)) { |
63 | 0 | return 0; |
64 | 0 | } |
65 | | |
66 | 99.2k | if (uses_prehash(ctx, op)) { |
67 | 99.2k | if (type == NULL) { |
68 | 0 | OPENSSL_PUT_ERROR(EVP, EVP_R_NO_DEFAULT_DIGEST); |
69 | 0 | return 0; |
70 | 0 | } |
71 | 99.2k | if (!EVP_DigestInit_ex(ctx, type, e)) { |
72 | 0 | return 0; |
73 | 0 | } |
74 | 99.2k | } |
75 | | |
76 | 99.2k | if (pctx) { |
77 | 99.2k | *pctx = ctx->pctx; |
78 | 99.2k | } |
79 | 99.2k | return 1; |
80 | 99.2k | } |
81 | | |
82 | | int EVP_DigestSignInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, const EVP_MD *type, |
83 | 41.3k | ENGINE *e, EVP_PKEY *pkey) { |
84 | 41.3k | return do_sigver_init(ctx, pctx, type, e, pkey, evp_sign); |
85 | 41.3k | } |
86 | | |
87 | | int EVP_DigestVerifyInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, |
88 | 57.8k | const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey) { |
89 | 57.8k | return do_sigver_init(ctx, pctx, type, e, pkey, evp_verify); |
90 | 57.8k | } |
91 | | |
92 | 41.3k | int EVP_DigestSignUpdate(EVP_MD_CTX *ctx, const void *data, size_t len) { |
93 | 41.3k | if (!uses_prehash(ctx, evp_sign)) { |
94 | 0 | OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
95 | 0 | return 0; |
96 | 0 | } |
97 | | |
98 | 41.3k | return EVP_DigestUpdate(ctx, data, len); |
99 | 41.3k | } |
100 | | |
101 | 57.8k | int EVP_DigestVerifyUpdate(EVP_MD_CTX *ctx, const void *data, size_t len) { |
102 | 57.8k | if (!uses_prehash(ctx, evp_verify)) { |
103 | 0 | OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
104 | 0 | return 0; |
105 | 0 | } |
106 | | |
107 | 57.8k | return EVP_DigestUpdate(ctx, data, len); |
108 | 57.8k | } |
109 | | |
110 | | int EVP_DigestSignFinal(EVP_MD_CTX *ctx, uint8_t *out_sig, |
111 | 41.3k | size_t *out_sig_len) { |
112 | 41.3k | if (!uses_prehash(ctx, evp_sign)) { |
113 | 0 | OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
114 | 0 | return 0; |
115 | 0 | } |
116 | | |
117 | 41.3k | if (out_sig) { |
118 | 41.3k | EVP_MD_CTX tmp_ctx; |
119 | 41.3k | int ret; |
120 | 41.3k | uint8_t md[EVP_MAX_MD_SIZE]; |
121 | 41.3k | unsigned int mdlen; |
122 | | |
123 | 41.3k | FIPS_service_indicator_lock_state(); |
124 | 41.3k | EVP_MD_CTX_init(&tmp_ctx); |
125 | 41.3k | ret = EVP_MD_CTX_copy_ex(&tmp_ctx, ctx) && |
126 | 41.3k | EVP_DigestFinal_ex(&tmp_ctx, md, &mdlen) && |
127 | 41.3k | EVP_PKEY_sign(ctx->pctx, out_sig, out_sig_len, md, mdlen); |
128 | 41.3k | EVP_MD_CTX_cleanup(&tmp_ctx); |
129 | 41.3k | FIPS_service_indicator_unlock_state(); |
130 | | |
131 | 41.3k | if (ret) { |
132 | 41.3k | EVP_DigestSign_verify_service_indicator(ctx); |
133 | 41.3k | } |
134 | | |
135 | 41.3k | return ret; |
136 | 41.3k | } else { |
137 | 0 | size_t s = EVP_MD_size(ctx->digest); |
138 | 0 | return EVP_PKEY_sign(ctx->pctx, out_sig, out_sig_len, NULL, s); |
139 | 0 | } |
140 | 41.3k | } |
141 | | |
142 | | int EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const uint8_t *sig, |
143 | 57.8k | size_t sig_len) { |
144 | 57.8k | if (!uses_prehash(ctx, evp_verify)) { |
145 | 0 | OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
146 | 0 | return 0; |
147 | 0 | } |
148 | | |
149 | 57.8k | EVP_MD_CTX tmp_ctx; |
150 | 57.8k | int ret; |
151 | 57.8k | uint8_t md[EVP_MAX_MD_SIZE]; |
152 | 57.8k | unsigned int mdlen; |
153 | | |
154 | 57.8k | FIPS_service_indicator_lock_state(); |
155 | 57.8k | EVP_MD_CTX_init(&tmp_ctx); |
156 | 57.8k | ret = EVP_MD_CTX_copy_ex(&tmp_ctx, ctx) && |
157 | 57.8k | EVP_DigestFinal_ex(&tmp_ctx, md, &mdlen) && |
158 | 57.8k | EVP_PKEY_verify(ctx->pctx, sig, sig_len, md, mdlen); |
159 | 57.8k | FIPS_service_indicator_unlock_state(); |
160 | 57.8k | EVP_MD_CTX_cleanup(&tmp_ctx); |
161 | | |
162 | 57.8k | if (ret) { |
163 | 0 | EVP_DigestVerify_verify_service_indicator(ctx); |
164 | 0 | } |
165 | | |
166 | 57.8k | return ret; |
167 | 57.8k | } |
168 | | |
169 | | int EVP_DigestSign(EVP_MD_CTX *ctx, uint8_t *out_sig, size_t *out_sig_len, |
170 | 41.3k | const uint8_t *data, size_t data_len) { |
171 | 41.3k | FIPS_service_indicator_lock_state(); |
172 | 41.3k | int ret = 0; |
173 | | |
174 | 41.3k | if (uses_prehash(ctx, evp_sign)) { |
175 | | // If |out_sig| is NULL, the caller is only querying the maximum output |
176 | | // length. |data| should only be incorporated in the final call. |
177 | 41.3k | if (out_sig != NULL && |
178 | 41.3k | !EVP_DigestSignUpdate(ctx, data, data_len)) { |
179 | 0 | goto end; |
180 | 0 | } |
181 | | |
182 | 41.3k | ret = EVP_DigestSignFinal(ctx, out_sig, out_sig_len); |
183 | 41.3k | goto end; |
184 | 41.3k | } |
185 | | |
186 | 0 | if (ctx->pctx->pmeth->sign_message == NULL) { |
187 | 0 | OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
188 | 0 | goto end; |
189 | 0 | } |
190 | | |
191 | 0 | ret = ctx->pctx->pmeth->sign_message(ctx->pctx, out_sig, out_sig_len, data, |
192 | 0 | data_len); |
193 | |
|
194 | 41.3k | end: |
195 | 41.3k | FIPS_service_indicator_unlock_state(); |
196 | 41.3k | if (ret) { |
197 | 41.3k | EVP_DigestSign_verify_service_indicator(ctx); |
198 | 41.3k | } |
199 | 41.3k | return ret; |
200 | 0 | } |
201 | | |
202 | | int EVP_DigestVerify(EVP_MD_CTX *ctx, const uint8_t *sig, size_t sig_len, |
203 | 57.8k | const uint8_t *data, size_t len) { |
204 | 57.8k | FIPS_service_indicator_lock_state(); |
205 | 57.8k | int ret = 0; |
206 | | |
207 | 57.8k | if (uses_prehash(ctx, evp_verify)) { |
208 | 57.8k | ret = EVP_DigestVerifyUpdate(ctx, data, len) && |
209 | 57.8k | EVP_DigestVerifyFinal(ctx, sig, sig_len); |
210 | 57.8k | goto end; |
211 | 57.8k | } |
212 | | |
213 | 0 | if (ctx->pctx->pmeth->verify_message == NULL) { |
214 | 0 | OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
215 | 0 | goto end; |
216 | 0 | } |
217 | | |
218 | 0 | ret = ctx->pctx->pmeth->verify_message(ctx->pctx, sig, sig_len, data, len); |
219 | |
|
220 | 57.8k | end: |
221 | 57.8k | FIPS_service_indicator_unlock_state(); |
222 | 57.8k | if (ret) { |
223 | 0 | EVP_DigestVerify_verify_service_indicator(ctx); |
224 | 0 | } |
225 | 57.8k | return ret; |
226 | 0 | } |