| 1 |
/* ==================================================================== |
| 2 |
* Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved. |
| 3 |
* |
| 4 |
* Redistribution and use in source and binary forms, with or without |
| 5 |
* modification, are permitted provided that the following conditions |
| 6 |
* are met: |
| 7 |
* |
| 8 |
* 1. Redistributions of source code must retain the above copyright |
| 9 |
* notice, this list of conditions and the following disclaimer. |
| 10 |
* |
| 11 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 12 |
* notice, this list of conditions and the following disclaimer in |
| 13 |
* the documentation and/or other materials provided with the |
| 14 |
* distribution. |
| 15 |
* |
| 16 |
* 3. All advertising materials mentioning features or use of this |
| 17 |
* software must display the following acknowledgment: |
| 18 |
* "This product includes software developed by the OpenSSL Project |
| 19 |
* for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" |
| 20 |
* |
| 21 |
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 22 |
* endorse or promote products derived from this software without |
| 23 |
* prior written permission. For written permission, please contact |
| 24 |
* [email protected]. |
| 25 |
* |
| 26 |
* 5. Products derived from this software may not be called "OpenSSL" |
| 27 |
* nor may "OpenSSL" appear in their names without prior written |
| 28 |
* permission of the OpenSSL Project. |
| 29 |
* |
| 30 |
* 6. Redistributions of any form whatsoever must retain the following |
| 31 |
* acknowledgment: |
| 32 |
* "This product includes software developed by the OpenSSL Project |
| 33 |
* for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" |
| 34 |
* |
| 35 |
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 36 |
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 37 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 38 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 39 |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 40 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 41 |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 42 |
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 43 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 44 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 45 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 46 |
* OF THE POSSIBILITY OF SUCH DAMAGE. |
| 47 |
* ==================================================================== |
| 48 |
* |
| 49 |
* This product includes cryptographic software written by Eric Young |
| 50 |
* ([email protected]). This product includes software written by Tim |
| 51 |
* Hudson ([email protected]). */ |
| 52 |
|
| 53 |
#include <openssl/ecdsa.h> |
| 54 |
|
| 55 |
#include <vector> |
| 56 |
|
| 57 |
#include <openssl/bn.h> |
| 58 |
#include <openssl/crypto.h> |
| 59 |
#include <openssl/ec.h> |
| 60 |
#include <openssl/err.h> |
| 61 |
#include <openssl/mem.h> |
| 62 |
#include <openssl/nid.h> |
| 63 |
#include <openssl/rand.h> |
| 64 |
|
| 65 |
#include "../test/scoped_types.h" |
| 66 |
|
| 67 |
enum Api { |
| 68 |
kEncodedApi, |
| 69 |
kRawApi, |
| 70 |
}; |
| 71 |
|
| 72 |
// VerifyECDSASig returns true on success, false on failure. |
| 73 |
static bool VerifyECDSASig(Api api, const uint8_t *digest, |
| 74 |
size_t digest_len, const ECDSA_SIG *ecdsa_sig, |
| 75 |
EC_KEY *eckey, int expected_result) { |
| 76 |
int actual_result; |
| 77 |
|
| 78 |
switch (api) { |
| 79 |
case kEncodedApi: { |
| 80 |
uint8_t *der; |
| 81 |
size_t der_len; |
| 82 |
if (!ECDSA_SIG_to_bytes(&der, &der_len, ecdsa_sig)) { |
| 83 |
return false; |
| 84 |
} |
| 85 |
ScopedOpenSSLBytes delete_der(der); |
| 86 |
actual_result = ECDSA_verify(0, digest, digest_len, der, der_len, eckey); |
| 87 |
break; |
| 88 |
} |
| 89 |
|
| 90 |
case kRawApi: |
| 91 |
actual_result = ECDSA_do_verify(digest, digest_len, ecdsa_sig, eckey); |
| 92 |
break; |
| 93 |
|
| 94 |
default: |
| 95 |
return false; |
| 96 |
} |
| 97 |
return expected_result == actual_result; |
| 98 |
} |
| 99 |
|
| 100 |
// TestTamperedSig verifies that signature verification fails when a valid |
| 101 |
// signature is tampered with. |ecdsa_sig| must be a valid signature, which will |
| 102 |
// be modified. TestTamperedSig returns true on success, false on failure. |
| 103 |
static bool TestTamperedSig(FILE *out, Api api, const uint8_t *digest, |
| 104 |
size_t digest_len, ECDSA_SIG *ecdsa_sig, |
| 105 |
EC_KEY *eckey, const BIGNUM *order) { |
| 106 |
// Modify a single byte of the signature: to ensure we don't |
| 107 |
// garble the ASN1 structure, we read the raw signature and |
| 108 |
// modify a byte in one of the bignums directly. |
| 109 |
|
| 110 |
// Store the two BIGNUMs in raw_buf. |
| 111 |
size_t r_len = BN_num_bytes(ecdsa_sig->r); |
| 112 |
size_t s_len = BN_num_bytes(ecdsa_sig->s); |
| 113 |
size_t bn_len = BN_num_bytes(order); |
| 114 |
if (r_len > bn_len || s_len > bn_len) { |
| 115 |
return false; |
| 116 |
} |
| 117 |
size_t buf_len = 2 * bn_len; |
| 118 |
std::vector<uint8_t> raw_buf(buf_len); |
| 119 |
// Pad the bignums with leading zeroes. |
| 120 |
if (!BN_bn2bin_padded(raw_buf.data(), bn_len, ecdsa_sig->r) || |
| 121 |
!BN_bn2bin_padded(raw_buf.data() + bn_len, bn_len, ecdsa_sig->s)) { |
| 122 |
return false; |
| 123 |
} |
| 124 |
|
| 125 |
// Modify a single byte in the buffer. |
| 126 |
size_t offset = raw_buf[10] % buf_len; |
| 127 |
uint8_t dirt = raw_buf[11] ? raw_buf[11] : 1; |
| 128 |
raw_buf[offset] ^= dirt; |
| 129 |
// Now read the BIGNUMs back in from raw_buf. |
| 130 |
if (BN_bin2bn(raw_buf.data(), bn_len, ecdsa_sig->r) == NULL || |
| 131 |
BN_bin2bn(raw_buf.data() + bn_len, bn_len, ecdsa_sig->s) == NULL || |
| 132 |
!VerifyECDSASig(api, digest, digest_len, ecdsa_sig, eckey, 0)) { |
| 133 |
return false; |
| 134 |
} |
| 135 |
|
| 136 |
// Sanity check: Undo the modification and verify signature. |
| 137 |
raw_buf[offset] ^= dirt; |
| 138 |
if (BN_bin2bn(raw_buf.data(), bn_len, ecdsa_sig->r) == NULL || |
| 139 |
BN_bin2bn(raw_buf.data() + bn_len, bn_len, ecdsa_sig->s) == NULL || |
| 140 |
!VerifyECDSASig(api, digest, digest_len, ecdsa_sig, eckey, 1)) { |
| 141 |
return false; |
| 142 |
} |
| 143 |
|
| 144 |
return true; |
| 145 |
} |
| 146 |
|
| 147 |
static bool TestBuiltin(FILE *out) { |
| 148 |
// Fill digest values with some random data. |
| 149 |
uint8_t digest[20], wrong_digest[20]; |
| 150 |
if (!RAND_bytes(digest, 20) || !RAND_bytes(wrong_digest, 20)) { |
| 151 |
fprintf(out, "ERROR: unable to get random data\n"); |
| 152 |
return false; |
| 153 |
} |
| 154 |
|
| 155 |
static const struct { |
| 156 |
int nid; |
| 157 |
const char *name; |
| 158 |
} kCurves[] = { |
| 159 |
{ NID_secp224r1, "secp224r1" }, |
| 160 |
{ NID_X9_62_prime256v1, "secp256r1" }, |
| 161 |
{ NID_secp384r1, "secp384r1" }, |
| 162 |
{ NID_secp521r1, "secp521r1" }, |
| 163 |
{ NID_undef, NULL } |
| 164 |
}; |
| 165 |
|
| 166 |
// Create and verify ECDSA signatures with every available curve. |
| 167 |
fputs("\ntesting ECDSA_sign(), ECDSA_verify(), ECDSA_do_sign(), and " |
| 168 |
"ECDSA_do_verify() with some internal curves:\n", out); |
| 169 |
|
| 170 |
for (size_t n = 0; kCurves[n].nid != NID_undef; n++) { |
| 171 |
fprintf(out, "%s: ", kCurves[n].name); |
| 172 |
|
| 173 |
int nid = kCurves[n].nid; |
| 174 |
ScopedEC_GROUP group(EC_GROUP_new_by_curve_name(nid)); |
| 175 |
if (!group) { |
| 176 |
fprintf(out, " failed\n"); |
| 177 |
return false; |
| 178 |
} |
| 179 |
const BIGNUM *order = EC_GROUP_get0_order(group.get()); |
| 180 |
if (BN_num_bits(order) < 160) { |
| 181 |
// Too small to test. |
| 182 |
fprintf(out, " skipped\n"); |
| 183 |
continue; |
| 184 |
} |
| 185 |
|
| 186 |
// Create a new ECDSA key. |
| 187 |
ScopedEC_KEY eckey(EC_KEY_new()); |
| 188 |
if (!eckey || !EC_KEY_set_group(eckey.get(), group.get()) || |
| 189 |
!EC_KEY_generate_key(eckey.get())) { |
| 190 |
fprintf(out, " failed\n"); |
| 191 |
return false; |
| 192 |
} |
| 193 |
// Create a second key. |
| 194 |
ScopedEC_KEY wrong_eckey(EC_KEY_new()); |
| 195 |
if (!wrong_eckey || !EC_KEY_set_group(wrong_eckey.get(), group.get()) || |
| 196 |
!EC_KEY_generate_key(wrong_eckey.get())) { |
| 197 |
fprintf(out, " failed\n"); |
| 198 |
return false; |
| 199 |
} |
| 200 |
|
| 201 |
fprintf(out, "."); |
| 202 |
fflush(out); |
| 203 |
|
| 204 |
// Check the key. |
| 205 |
if (!EC_KEY_check_key(eckey.get())) { |
| 206 |
fprintf(out, " failed\n"); |
| 207 |
return false; |
| 208 |
} |
| 209 |
fprintf(out, "."); |
| 210 |
fflush(out); |
| 211 |
|
| 212 |
// Test ASN.1-encoded signatures. |
| 213 |
// Create a signature. |
| 214 |
unsigned sig_len = ECDSA_size(eckey.get()); |
| 215 |
std::vector<uint8_t> signature(sig_len); |
| 216 |
if (!ECDSA_sign(0, digest, 20, signature.data(), &sig_len, eckey.get())) { |
| 217 |
fprintf(out, " failed\n"); |
| 218 |
return false; |
| 219 |
} |
| 220 |
signature.resize(sig_len); |
| 221 |
fprintf(out, "."); |
| 222 |
fflush(out); |
| 223 |
// Verify the signature. |
| 224 |
if (!ECDSA_verify(0, digest, 20, signature.data(), signature.size(), |
| 225 |
eckey.get())) { |
| 226 |
fprintf(out, " failed\n"); |
| 227 |
return false; |
| 228 |
} |
| 229 |
fprintf(out, "."); |
| 230 |
fflush(out); |
| 231 |
// Verify the signature with the wrong key. |
| 232 |
if (ECDSA_verify(0, digest, 20, signature.data(), signature.size(), |
| 233 |
wrong_eckey.get())) { |
| 234 |
fprintf(out, " failed\n"); |
| 235 |
return false; |
| 236 |
} |
| 237 |
fprintf(out, "."); |
| 238 |
fflush(out); |
| 239 |
// Verify the signature using the wrong digest. |
| 240 |
if (ECDSA_verify(0, wrong_digest, 20, signature.data(), signature.size(), |
| 241 |
eckey.get())) { |
| 242 |
fprintf(out, " failed\n"); |
| 243 |
return false; |
| 244 |
} |
| 245 |
fprintf(out, "."); |
| 246 |
fflush(out); |
| 247 |
// Verify a truncated signature. |
| 248 |
if (ECDSA_verify(0, digest, 20, signature.data(), signature.size() - 1, |
| 249 |
eckey.get())) { |
| 250 |
fprintf(out, " failed\n"); |
| 251 |
return false; |
| 252 |
} |
| 253 |
fprintf(out, "."); |
| 254 |
fflush(out); |
| 255 |
// Verify a tampered signature. |
| 256 |
ScopedECDSA_SIG ecdsa_sig(ECDSA_SIG_from_bytes( |
| 257 |
signature.data(), signature.size())); |
| 258 |
if (!ecdsa_sig || |
| 259 |
!TestTamperedSig(out, kEncodedApi, digest, 20, ecdsa_sig.get(), |
| 260 |
eckey.get(), order)) { |
| 261 |
fprintf(out, " failed\n"); |
| 262 |
return false; |
| 263 |
} |
| 264 |
fprintf(out, "."); |
| 265 |
fflush(out); |
| 266 |
|
| 267 |
// Test ECDSA_SIG signing and verification. |
| 268 |
// Create a signature. |
| 269 |
ecdsa_sig.reset(ECDSA_do_sign(digest, 20, eckey.get())); |
| 270 |
if (!ecdsa_sig) { |
| 271 |
fprintf(out, " failed\n"); |
| 272 |
return false; |
| 273 |
} |
| 274 |
fprintf(out, "."); |
| 275 |
fflush(out); |
| 276 |
// Verify the signature using the correct key. |
| 277 |
if (!ECDSA_do_verify(digest, 20, ecdsa_sig.get(), eckey.get())) { |
| 278 |
fprintf(out, " failed\n"); |
| 279 |
return false; |
| 280 |
} |
| 281 |
fprintf(out, "."); |
| 282 |
fflush(out); |
| 283 |
// Verify the signature with the wrong key. |
| 284 |
if (ECDSA_do_verify(digest, 20, ecdsa_sig.get(), wrong_eckey.get())) { |
| 285 |
fprintf(out, " failed\n"); |
| 286 |
return false; |
| 287 |
} |
| 288 |
fprintf(out, "."); |
| 289 |
fflush(out); |
| 290 |
// Verify the signature using the wrong digest. |
| 291 |
if (ECDSA_do_verify(wrong_digest, 20, ecdsa_sig.get(), eckey.get())) { |
| 292 |
fprintf(out, " failed\n"); |
| 293 |
return false; |
| 294 |
} |
| 295 |
fprintf(out, "."); |
| 296 |
fflush(out); |
| 297 |
// Verify a tampered signature. |
| 298 |
if (!TestTamperedSig(out, kRawApi, digest, 20, ecdsa_sig.get(), eckey.get(), |
| 299 |
order)) { |
| 300 |
fprintf(out, " failed\n"); |
| 301 |
return false; |
| 302 |
} |
| 303 |
fprintf(out, "."); |
| 304 |
fflush(out); |
| 305 |
|
| 306 |
fprintf(out, " ok\n"); |
| 307 |
// Clear bogus errors. |
| 308 |
ERR_clear_error(); |
| 309 |
} |
| 310 |
|
| 311 |
return true; |
| 312 |
} |
| 313 |
|
| 314 |
static bool TestECDSA_SIG_max_len(size_t order_len) { |
| 315 |
/* Create the largest possible |ECDSA_SIG| of the given constraints. */ |
| 316 |
ScopedECDSA_SIG sig(ECDSA_SIG_new()); |
| 317 |
if (!sig) { |
| 318 |
return false; |
| 319 |
} |
| 320 |
std::vector<uint8_t> bytes(order_len, 0xff); |
| 321 |
if (!BN_bin2bn(bytes.data(), bytes.size(), sig->r) || |
| 322 |
!BN_bin2bn(bytes.data(), bytes.size(), sig->s)) { |
| 323 |
return false; |
| 324 |
} |
| 325 |
/* Serialize it. */ |
| 326 |
uint8_t *der; |
| 327 |
size_t der_len; |
| 328 |
if (!ECDSA_SIG_to_bytes(&der, &der_len, sig.get())) { |
| 329 |
return false; |
| 330 |
} |
| 331 |
ScopedOpenSSLBytes delete_der(der); |
| 332 |
|
| 333 |
size_t max_len = ECDSA_SIG_max_len(order_len); |
| 334 |
if (max_len != der_len) { |
| 335 |
fprintf(stderr, "ECDSA_SIG_max_len(%u) returned %u, wanted %u\n", |
| 336 |
static_cast<unsigned>(order_len), static_cast<unsigned>(max_len), |
| 337 |
static_cast<unsigned>(der_len)); |
| 338 |
return false; |
| 339 |
} |
| 340 |
return true; |
| 341 |
} |
| 342 |
|
| 343 |
static size_t BitsToBytes(size_t bits) { |
| 344 |
return (bits / 8) + (7 + (bits % 8)) / 8; |
| 345 |
} |
| 346 |
|
| 347 |
int main(void) { |
| 348 |
CRYPTO_library_init(); |
| 349 |
|
| 350 |
if (!TestBuiltin(stdout) || |
| 351 |
!TestECDSA_SIG_max_len(BitsToBytes(224)) || |
| 352 |
!TestECDSA_SIG_max_len(BitsToBytes(256)) || |
| 353 |
!TestECDSA_SIG_max_len(BitsToBytes(384)) || |
| 354 |
!TestECDSA_SIG_max_len(BitsToBytes(521)) || |
| 355 |
!TestECDSA_SIG_max_len(BitsToBytes(10000))) { |
| 356 |
printf("\nECDSA test failed\n"); |
| 357 |
ERR_print_errors_fp(stdout); |
| 358 |
return 1; |
| 359 |
} |
| 360 |
|
| 361 |
printf("\nPASS\n"); |
| 362 |
return 0; |
| 363 |
} |