|
| 1 | +/* ecc-verify-minimal.c |
| 2 | + * |
| 3 | + * Copyright (C) 2006-2026 wolfSSL Inc. |
| 4 | + * |
| 5 | + * This file is part of wolfSSL. |
| 6 | + * |
| 7 | + * wolfSSL is free software; you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation; either version 2 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * wolfSSL is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with this program; if not, write to the Free Software |
| 19 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
| 20 | + */ |
| 21 | +#include <wolfssl/options.h> |
| 22 | +#include <wolfssl/wolfcrypt/settings.h> |
| 23 | + |
| 24 | +#include <wolfssl/wolfcrypt/ecc.h> |
| 25 | + |
| 26 | +#include <stdio.h> |
| 27 | +#include <string.h> |
| 28 | + |
| 29 | +static const byte signedMessageSha256Hash[32] = { |
| 30 | + 0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5, |
| 31 | + 0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5, |
| 32 | + 0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5 |
| 33 | +}; |
| 34 | + |
| 35 | +static const byte ecdsaSignatureDer[72] = { |
| 36 | + 0x30, 0x46, 0x02, 0x21, 0x00, 0xd9, 0x8a, 0xe0, 0xea, 0xcf, 0xae, 0x30, |
| 37 | + 0x70, 0xd1, 0x9c, 0xdd, 0x0d, 0xd0, 0x77, 0x73, 0x77, 0x87, 0xb6, 0x2a, |
| 38 | + 0x99, 0xe5, 0x41, 0x91, 0x29, 0xda, 0xff, 0x5e, 0x95, 0xe8, 0xec, 0xf9, |
| 39 | + 0x9a, 0x02, 0x21, 0x00, 0xa6, 0x71, 0x98, 0xed, 0x9b, 0x82, 0xa0, 0xba, |
| 40 | + 0x60, 0xc5, 0xd2, 0x56, 0xae, 0x68, 0xb2, 0xd3, 0x29, 0x56, 0x88, 0xe8, |
| 41 | + 0x47, 0xd5, 0xf1, 0x91, 0x0b, 0xac, 0xe4, 0xe9, 0x00, 0xf8, 0x31, 0x4c |
| 42 | +}; |
| 43 | + |
| 44 | +/* X9.63 uncompressed public key (0x04 || X || Y) */ |
| 45 | +static const byte signerPublicKey[65] = { |
| 46 | + 0x04, 0xf6, 0x7f, 0x27, 0xc2, 0xa3, 0xeb, 0x3b, 0x4f, 0xc9, 0xec, 0xdb, |
| 47 | + 0x64, 0x72, 0xe7, 0x16, 0x51, 0xc3, 0xfb, 0xdd, 0x5c, 0xe0, 0x82, 0xc6, |
| 48 | + 0x0c, 0x9e, 0x62, 0x6a, 0x34, 0xfc, 0x47, 0xcb, 0xe6, 0x1a, 0x08, 0x7c, |
| 49 | + 0x44, 0x54, 0x88, 0x69, 0xb4, 0x6f, 0x5d, 0x93, 0xde, 0xdc, 0x4e, 0x7f, |
| 50 | + 0x1a, 0xb7, 0x75, 0xe2, 0xfd, 0x5f, 0xd6, 0x7d, 0x6a, 0xd3, 0x00, 0x2c, |
| 51 | + 0x09, 0x99, 0xaf, 0x2f, 0x0f |
| 52 | +}; |
| 53 | + |
| 54 | +int main(void) |
| 55 | +{ |
| 56 | + int ret, is_valid_sig = 0; |
| 57 | + ecc_key key; |
| 58 | + |
| 59 | + /* Initialize the ecc_key structure before use. */ |
| 60 | + if ((ret = wc_ecc_init(&key)) != 0) { |
| 61 | + printf("wc_ecc_init failed: %d\n", ret); |
| 62 | + return ret; |
| 63 | + } |
| 64 | + |
| 65 | + /* Load the signer's public key in X9.63 uncompressed form. */ |
| 66 | + ret = wc_ecc_import_x963_ex(signerPublicKey, sizeof(signerPublicKey), |
| 67 | + &key, ECC_SECP256R1); |
| 68 | + if (ret != 0) { |
| 69 | + printf("wc_ecc_import_x963_ex failed: %d\n", ret); |
| 70 | + goto cleanup; |
| 71 | + } |
| 72 | + |
| 73 | + /* Reject short digests to guard against the short-hash bypass CVE. */ |
| 74 | + if (sizeof(signedMessageSha256Hash) < WC_MIN_DIGEST_SIZE) { |
| 75 | + goto cleanup; |
| 76 | + } |
| 77 | + |
| 78 | + /* Verify the ECDSA signature over the precomputed hash. */ |
| 79 | + ret = wc_ecc_verify_hash(ecdsaSignatureDer, sizeof(ecdsaSignatureDer), |
| 80 | + signedMessageSha256Hash, sizeof(signedMessageSha256Hash), |
| 81 | + &is_valid_sig, &key); |
| 82 | + |
| 83 | + printf("wc_ecc_verify_hash: ret=%d, is_valid_sig=%d\n", ret, is_valid_sig); |
| 84 | + |
| 85 | +cleanup: |
| 86 | + wc_ecc_free(&key); |
| 87 | + return ret; |
| 88 | +} |
0 commit comments