| 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/ecdsa.h> |
| 16 |
| 17 | #include <limits.h> |
| 18 | #include <string.h> |
| 19 |
| 20 | #include <openssl/bn.h> |
| 21 | #include <openssl/bytestring.h> |
| 22 | #include <openssl/ec_key.h> |
| 23 | #include <openssl/err.h> |
| 24 | #include <openssl/mem.h> |
| 25 |
| 26 | #include "../bytestring/internal.h" |
| 27 | #include "../fipsmodule/ecdsa/internal.h" |
| 28 | #include "../internal.h" |
| 29 |
| 30 |
| 31 | static ECDSA_SIG *ecdsa_sig_from_fixed(const EC_KEY *key, const uint8_t *in, |
| 32 | size_t len) { |
| 33 | const EC_GROUP *group = EC_KEY_get0_group(key); |
| 34 | if (group == NULL) { |
| 35 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_PASSED_NULL_PARAMETER); |
| 36 | return NULL; |
| 37 | } |
| 38 | size_t scalar_len = BN_num_bytes(EC_GROUP_get0_order(group)); |
| 39 | if (len != 2 * scalar_len) { |
| 40 | OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_BAD_SIGNATURE); |
| 41 | return NULL; |
| 42 | } |
| 43 | ECDSA_SIG *ret = ECDSA_SIG_new(); |
| 44 | if (ret == NULL || !BN_bin2bn(in, scalar_len, ret->r) || |
| 45 | !BN_bin2bn(in + scalar_len, scalar_len, ret->s)) { |
| 46 | ECDSA_SIG_free(ret); |
| 47 | return NULL; |
| 48 | } |
| 49 | return ret; |
| 50 | } |
| 51 |
| 52 | static int ecdsa_sig_to_fixed(const EC_KEY *key, uint8_t *out, size_t *out_len, |
| 53 | size_t max_out, const ECDSA_SIG *sig) { |
| 54 | const EC_GROUP *group = EC_KEY_get0_group(key); |
| 55 | if (group == NULL) { |
| 56 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_PASSED_NULL_PARAMETER); |
| 57 | return 0; |
| 58 | } |
| 59 | size_t scalar_len = BN_num_bytes(EC_GROUP_get0_order(group)); |
| 60 | if (max_out < 2 * scalar_len) { |
| 61 | OPENSSL_PUT_ERROR(EC, EC_R_BUFFER_TOO_SMALL); |
| 62 | return 0; |
| 63 | } |
| 64 | if (BN_is_negative(sig->r) || !BN_bn2bin_padded(out, scalar_len, sig->r) || |
| 65 | BN_is_negative(sig->s) || |
| 66 | !BN_bn2bin_padded(out + scalar_len, scalar_len, sig->s)) { |
| 67 | OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_BAD_SIGNATURE); |
| 68 | return 0; |
| 69 | } |
| 70 | *out_len = 2 * scalar_len; |
| 71 | return 1; |
| 72 | } |
| 73 |
| 74 | int ECDSA_sign(int type, const uint8_t *digest, size_t digest_len, uint8_t *sig, |
| 75 | unsigned int *out_sig_len, const EC_KEY *eckey) { |
| 76 | if (eckey->ecdsa_meth && eckey->ecdsa_meth->sign) { |
| 77 | return eckey->ecdsa_meth->sign(digest, digest_len, sig, out_sig_len, |
| 78 | (EC_KEY *)eckey /* cast away const */); |
| 79 | } |
| 80 |
| 81 | *out_sig_len = 0; |
| 82 | uint8_t fixed[ECDSA_MAX_FIXED_LEN]; |
| 83 | size_t fixed_len; |
| 84 | if (!ecdsa_sign_fixed(digest, digest_len, fixed, &fixed_len, sizeof(fixed), |
| 85 | eckey)) { |
| 86 | return 0; |
| 87 | } |
| 88 |
| 89 | // TODO(davidben): We can actually do better and go straight from the DER |
| 90 | // format to the fixed-width format without a malloc. |
| 91 | ECDSA_SIG *s = ecdsa_sig_from_fixed(eckey, fixed, fixed_len); |
| 92 | if (s == NULL) { |
| 93 | return 0; |
| 94 | } |
| 95 |
| 96 | int ret = 0; |
| 97 | CBB cbb; |
| 98 | CBB_init_fixed(&cbb, sig, ECDSA_size(eckey)); |
| 99 | size_t len; |
| 100 | if (!ECDSA_SIG_marshal(&cbb, s) || !CBB_finish(&cbb, NULL, &len)) { |
| 101 | OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_ENCODE_ERROR); |
| 102 | goto err; |
| 103 | } |
| 104 | *out_sig_len = (unsigned)len; |
| 105 | ret = 1; |
| 106 |
| 107 | err: |
| 108 | ECDSA_SIG_free(s); |
| 109 | return ret; |
| 110 | } |
| 111 |
| 112 | int ECDSA_verify(int type, const uint8_t *digest, size_t digest_len, |
| 113 | const uint8_t *sig, size_t sig_len, const EC_KEY *eckey) { |
| 114 | // Decode the ECDSA signature. |
| 115 | // |
| 116 | // TODO(davidben): We can actually do better and go straight from the DER |
| 117 | // format to the fixed-width format without a malloc. |
| 118 | int ret = 0; |
| 119 | uint8_t *der = NULL; |
| 120 | ECDSA_SIG *s = ECDSA_SIG_from_bytes(sig, sig_len); |
| 121 | if (s == NULL) { |
| 122 | goto err; |
| 123 | } |
| 124 |
| 125 | // Defend against potential laxness in the DER parser. |
| 126 | size_t der_len; |
| 127 | if (!ECDSA_SIG_to_bytes(&der, &der_len, s) || der_len != sig_len || |
| 128 | OPENSSL_memcmp(sig, der, sig_len) != 0) { |
| 129 | // This should never happen. crypto/bytestring is strictly DER. |
| 130 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_INTERNAL_ERROR); |
| 131 | goto err; |
| 132 | } |
| 133 |
| 134 | uint8_t fixed[ECDSA_MAX_FIXED_LEN]; |
| 135 | size_t fixed_len; |
| 136 | ret = ecdsa_sig_to_fixed(eckey, fixed, &fixed_len, sizeof(fixed), s) && |
| 137 | ecdsa_verify_fixed(digest, digest_len, fixed, fixed_len, eckey); |
| 138 |
| 139 | err: |
| 140 | OPENSSL_free(der); |
| 141 | ECDSA_SIG_free(s); |
| 142 | return ret; |
| 143 | } |
| 144 |
| 145 |
| 146 | size_t ECDSA_size(const EC_KEY *key) { |
| 147 | if (key == NULL) { |
| 148 | return 0; |
| 149 | } |
| 150 |
| 151 | const EC_GROUP *group = EC_KEY_get0_group(key); |
| 152 | if (group == NULL) { |
| 153 | return 0; |
| 154 | } |
| 155 |
| 156 | size_t group_order_size = BN_num_bytes(EC_GROUP_get0_order(group)); |
| 157 | return ECDSA_SIG_max_len(group_order_size); |
| 158 | } |
| 159 |
| 160 | ECDSA_SIG *ECDSA_SIG_new(void) { |
| 161 | ECDSA_SIG *sig = |
| 162 | reinterpret_cast<ECDSA_SIG *>(OPENSSL_malloc(sizeof(ECDSA_SIG))); |
| 163 | if (sig == NULL) { |
| 164 | return NULL; |
| 165 | } |
| 166 | sig->r = BN_new(); |
| 167 | sig->s = BN_new(); |
| 168 | if (sig->r == NULL || sig->s == NULL) { |
| 169 | ECDSA_SIG_free(sig); |
| 170 | return NULL; |
| 171 | } |
| 172 | return sig; |
| 173 | } |
| 174 |
| 175 | void ECDSA_SIG_free(ECDSA_SIG *sig) { |
| 176 | if (sig == NULL) { |
| 177 | return; |
| 178 | } |
| 179 |
| 180 | BN_free(sig->r); |
| 181 | BN_free(sig->s); |
| 182 | OPENSSL_free(sig); |
| 183 | } |
| 184 |
| 185 | const BIGNUM *ECDSA_SIG_get0_r(const ECDSA_SIG *sig) { return sig->r; } |
| 186 |
| 187 | const BIGNUM *ECDSA_SIG_get0_s(const ECDSA_SIG *sig) { return sig->s; } |
| 188 |
| 189 | void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **out_r, |
| 190 | const BIGNUM **out_s) { |
| 191 | if (out_r != NULL) { |
| 192 | *out_r = sig->r; |
| 193 | } |
| 194 | if (out_s != NULL) { |
| 195 | *out_s = sig->s; |
| 196 | } |
| 197 | } |
| 198 |
| 199 | int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s) { |
| 200 | if (r == NULL || s == NULL) { |
| 201 | return 0; |
| 202 | } |
| 203 | BN_free(sig->r); |
| 204 | BN_free(sig->s); |
| 205 | sig->r = r; |
| 206 | sig->s = s; |
| 207 | return 1; |
| 208 | } |
| 209 |
| 210 | int ECDSA_do_verify(const uint8_t *digest, size_t digest_len, |
| 211 | const ECDSA_SIG *sig, const EC_KEY *eckey) { |
| 212 | uint8_t fixed[ECDSA_MAX_FIXED_LEN]; |
| 213 | size_t fixed_len; |
| 214 | return ecdsa_sig_to_fixed(eckey, fixed, &fixed_len, sizeof(fixed), sig) && |
| 215 | ecdsa_verify_fixed(digest, digest_len, fixed, fixed_len, eckey); |
| 216 | } |
| 217 |
| 218 | // This function is only exported for testing and is not called in production |
| 219 | // code. |
| 220 | ECDSA_SIG *ECDSA_sign_with_nonce_and_leak_private_key_for_testing( |
| 221 | const uint8_t *digest, size_t digest_len, const EC_KEY *eckey, |
| 222 | const uint8_t *nonce, size_t nonce_len) { |
| 223 | uint8_t sig[ECDSA_MAX_FIXED_LEN]; |
| 224 | size_t sig_len; |
| 225 | if (!ecdsa_sign_fixed_with_nonce_for_known_answer_test( |
| 226 | digest, digest_len, sig, &sig_len, sizeof(sig), eckey, nonce, |
| 227 | nonce_len)) { |
| 228 | return NULL; |
| 229 | } |
| 230 |
| 231 | return ecdsa_sig_from_fixed(eckey, sig, sig_len); |
| 232 | } |
| 233 |
| 234 | ECDSA_SIG *ECDSA_do_sign(const uint8_t *digest, size_t digest_len, |
| 235 | const EC_KEY *eckey) { |
| 236 | uint8_t sig[ECDSA_MAX_FIXED_LEN]; |
| 237 | size_t sig_len; |
| 238 | if (!ecdsa_sign_fixed(digest, digest_len, sig, &sig_len, sizeof(sig), |
| 239 | eckey)) { |
| 240 | return NULL; |
| 241 | } |
| 242 |
| 243 | return ecdsa_sig_from_fixed(eckey, sig, sig_len); |
| 244 | } |
| 245 |
| 246 | ECDSA_SIG *ECDSA_SIG_parse(CBS *cbs) { |
| 247 | ECDSA_SIG *ret = ECDSA_SIG_new(); |
| 248 | if (ret == NULL) { |
| 249 | return NULL; |
| 250 | } |
| 251 | CBS child; |
| 252 | if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) || |
| 253 | !BN_parse_asn1_unsigned(&child, ret->r) || |
| 254 | !BN_parse_asn1_unsigned(&child, ret->s) || CBS_len(&child) != 0) { |
| 255 | OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_BAD_SIGNATURE); |
| 256 | ECDSA_SIG_free(ret); |
| 257 | return NULL; |
| 258 | } |
| 259 | return ret; |
| 260 | } |
| 261 |
| 262 | ECDSA_SIG *ECDSA_SIG_from_bytes(const uint8_t *in, size_t in_len) { |
| 263 | CBS cbs; |
| 264 | CBS_init(&cbs, in, in_len); |
| 265 | ECDSA_SIG *ret = ECDSA_SIG_parse(&cbs); |
| 266 | if (ret == NULL || CBS_len(&cbs) != 0) { |
| 267 | OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_BAD_SIGNATURE); |
| 268 | ECDSA_SIG_free(ret); |
| 269 | return NULL; |
| 270 | } |
| 271 | return ret; |
| 272 | } |
| 273 |
| 274 | int ECDSA_SIG_marshal(CBB *cbb, const ECDSA_SIG *sig) { |
| 275 | CBB child; |
| 276 | if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) || |
| 277 | !BN_marshal_asn1(&child, sig->r) || !BN_marshal_asn1(&child, sig->s) || |
| 278 | !CBB_flush(cbb)) { |
| 279 | OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_ENCODE_ERROR); |
| 280 | return 0; |
| 281 | } |
| 282 | return 1; |
| 283 | } |
| 284 |
| 285 | int ECDSA_SIG_to_bytes(uint8_t **out_bytes, size_t *out_len, |
| 286 | const ECDSA_SIG *sig) { |
| 287 | CBB cbb; |
| 288 | CBB_zero(&cbb); |
| 289 | if (!CBB_init(&cbb, 0) || !ECDSA_SIG_marshal(&cbb, sig) || |
| 290 | !CBB_finish(&cbb, out_bytes, out_len)) { |
| 291 | OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_ENCODE_ERROR); |
| 292 | CBB_cleanup(&cbb); |
| 293 | return 0; |
| 294 | } |
| 295 | return 1; |
| 296 | } |
| 297 |
| 298 | // der_len_len returns the number of bytes needed to represent a length of |len| |
| 299 | // in DER. |
| 300 | static size_t der_len_len(size_t len) { |
| 301 | if (len < 0x80) { |
| 302 | return 1; |
| 303 | } |
| 304 | size_t ret = 1; |
| 305 | while (len > 0) { |
| 306 | ret++; |
| 307 | len >>= 8; |
| 308 | } |
| 309 | return ret; |
| 310 | } |
| 311 |
| 312 | size_t ECDSA_SIG_max_len(size_t order_len) { |
| 313 | // Compute the maximum length of an |order_len| byte integer. Defensively |
| 314 | // assume that the leading 0x00 is included. |
| 315 | size_t integer_len = 1 /* tag */ + der_len_len(order_len + 1) + 1 + order_len; |
| 316 | if (integer_len < order_len) { |
| 317 | return 0; |
| 318 | } |
| 319 | // An ECDSA signature is two INTEGERs. |
| 320 | size_t value_len = 2 * integer_len; |
| 321 | if (value_len < integer_len) { |
| 322 | return 0; |
| 323 | } |
| 324 | // Add the header. |
| 325 | size_t ret = 1 /* tag */ + der_len_len(value_len) + value_len; |
| 326 | if (ret < value_len) { |
| 327 | return 0; |
| 328 | } |
| 329 | return ret; |
| 330 | } |
| 331 |
| 332 | ECDSA_SIG *d2i_ECDSA_SIG(ECDSA_SIG **out, const uint8_t **inp, long len) { |
| 333 | if (len < 0) { |
| 334 | return NULL; |
| 335 | } |
| 336 | CBS cbs; |
| 337 | CBS_init(&cbs, *inp, (size_t)len); |
| 338 | ECDSA_SIG *ret = ECDSA_SIG_parse(&cbs); |
| 339 | if (ret == NULL) { |
| 340 | return NULL; |
| 341 | } |
| 342 | if (out != NULL) { |
| 343 | ECDSA_SIG_free(*out); |
| 344 | *out = ret; |
| 345 | } |
| 346 | *inp = CBS_data(&cbs); |
| 347 | return ret; |
| 348 | } |
| 349 |
| 350 | int i2d_ECDSA_SIG(const ECDSA_SIG *sig, uint8_t **outp) { |
| 351 | CBB cbb; |
| 352 | if (!CBB_init(&cbb, 0) || !ECDSA_SIG_marshal(&cbb, sig)) { |
| 353 | CBB_cleanup(&cbb); |
| 354 | return -1; |
| 355 | } |
| 356 | return CBB_finish_i2d(&cbb, outp); |
| 357 | } |
| 358 |