|
| 1 | +/* test_wolf.c |
| 2 | + * |
| 3 | + * Copyright (C) 2006-2023 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 | + |
| 22 | +/* Example for running wolfCrypt test and benchmark from |
| 23 | + * SiLabs Simplicity Studio's CLI example */ |
| 24 | + |
| 25 | +#include <wolfssl/wolfcrypt/settings.h> |
| 26 | +#include <wolfssl/wolfcrypt/signature.h> |
| 27 | +#include <wolfssl/wolfcrypt/ecc.h> |
| 28 | +#include <wolfssl/wolfcrypt/asn_public.h> |
| 29 | +#include <wolfssl/wolfcrypt/logging.h> |
| 30 | +#include <wolfssl/wolfcrypt/wc_port.h> |
| 31 | +#include <wolfssl/wolfcrypt/error-crypt.h> |
| 32 | +#include <wolfcrypt/test/test.h> |
| 33 | +#include <wolfcrypt/benchmark/benchmark.h> |
| 34 | +#include <stdio.h> |
| 35 | + |
| 36 | +#include "sl_cli.h" |
| 37 | +#include "sl_cli_instances.h" |
| 38 | +#include "sl_cli_arguments.h" |
| 39 | +#include "sl_cli_handles.h" |
| 40 | + |
| 41 | +#ifndef NO_CRYPT_TEST |
| 42 | +typedef struct func_args { |
| 43 | + int argc; |
| 44 | + char** argv; |
| 45 | + int return_code; |
| 46 | +} func_args; |
| 47 | + |
| 48 | +static func_args args = { 0 }; |
| 49 | +#endif |
| 50 | + |
| 51 | +void wolf_test(sl_cli_command_arg_t *arguments) |
| 52 | +{ |
| 53 | + int ret; |
| 54 | +#ifndef NO_CRYPT_TEST |
| 55 | + wolfCrypt_Init(); |
| 56 | + |
| 57 | + printf("\nCrypt Test\n"); |
| 58 | + wolfcrypt_test(&args); |
| 59 | + ret = args.return_code; |
| 60 | + printf("Crypt Test: Return code %d\n", ret); |
| 61 | + |
| 62 | + wolfCrypt_Cleanup(); |
| 63 | +#else |
| 64 | + ret = NOT_COMPILED_IN; |
| 65 | +#endif |
| 66 | + (void)arguments; |
| 67 | + (void)ret; |
| 68 | +} |
| 69 | + |
| 70 | +void wolf_bench(sl_cli_command_arg_t *arguments) |
| 71 | +{ |
| 72 | + int ret; |
| 73 | +#ifndef NO_CRYPT_BENCHMARK |
| 74 | + wolfCrypt_Init(); |
| 75 | + |
| 76 | + printf("\nBenchmark Test\n"); |
| 77 | + benchmark_test(&args); |
| 78 | + ret = args.return_code; |
| 79 | + printf("Benchmark Test: Return code %d\n", ret); |
| 80 | + |
| 81 | + wolfCrypt_Cleanup(); |
| 82 | +#else |
| 83 | + ret = NOT_COMPILED_IN; |
| 84 | +#endif |
| 85 | + (void)arguments; |
| 86 | + (void)ret; |
| 87 | +} |
| 88 | + |
| 89 | +/* ecc key gen, sign and verify examples */ |
| 90 | +#define TEST_ECC_KEYSZ 32 |
| 91 | +#define TEST_DATA_SIZE 128 |
| 92 | +#define TEST_KEYGEN_TRIES 100 |
| 93 | +#define TEST_ECDSA_TRIES 100 |
| 94 | +/* #define TEST_VERIFY_ONLY */ |
| 95 | +/* #define TEST_PRIVATE_IMPORT */ |
| 96 | + |
| 97 | +void wolf_ecc_test(sl_cli_command_arg_t *arguments) |
| 98 | +{ |
| 99 | + int ret = 0, i, j; |
| 100 | + byte data[TEST_DATA_SIZE]; |
| 101 | + word32 dataLen = (word32)sizeof(data); |
| 102 | + WC_RNG rng; |
| 103 | + ecc_key eccKey; |
| 104 | + word32 inOutIdx; |
| 105 | +#ifdef TEST_PRIVATE_IMPORT |
| 106 | + /* ./certs/ecc-client-key.der, ECC */ |
| 107 | + const unsigned char ecc_clikey_der_256[] = { |
| 108 | + 0x30, 0x77, 0x02, 0x01, 0x01, 0x04, 0x20, 0xF8, 0xCF, 0x92, |
| 109 | + 0x6B, 0xBD, 0x1E, 0x28, 0xF1, 0xA8, 0xAB, 0xA1, 0x23, 0x4F, |
| 110 | + 0x32, 0x74, 0x18, 0x88, 0x50, 0xAD, 0x7E, 0xC7, 0xEC, 0x92, |
| 111 | + 0xF8, 0x8F, 0x97, 0x4D, 0xAF, 0x56, 0x89, 0x65, 0xC7, 0xA0, |
| 112 | + 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, |
| 113 | + 0x07, 0xA1, 0x44, 0x03, 0x42, 0x00, 0x04, 0x55, 0xBF, 0xF4, |
| 114 | + 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5, |
| 115 | + 0x4D, 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80, |
| 116 | + 0xEC, 0x5A, 0x4C, 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA, |
| 117 | + 0xEF, 0xA2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xC6, 0x56, |
| 118 | + 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6, 0x75, 0x1A, 0x42, |
| 119 | + 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D, 0x7F, |
| 120 | + 0xB4 |
| 121 | + }; |
| 122 | + const int sizeof_ecc_clikey_der_256 = sizeof(ecc_clikey_der_256); |
| 123 | +#endif |
| 124 | +#ifdef TEST_VERIFY_ONLY |
| 125 | + /* ./certs/ecc-client-keyPub.der, ECC */ |
| 126 | + const unsigned char ecc_clikeypub_der_256[] = { |
| 127 | + 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE, |
| 128 | + 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, |
| 129 | + 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xBF, 0xF4, |
| 130 | + 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5, |
| 131 | + 0x4D, 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80, |
| 132 | + 0xEC, 0x5A, 0x4C, 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA, |
| 133 | + 0xEF, 0xA2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xC6, 0x56, |
| 134 | + 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6, 0x75, 0x1A, 0x42, |
| 135 | + 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D, 0x7F, |
| 136 | + 0xB4 |
| 137 | + }; |
| 138 | + const int sizeof_ecc_clikeypub_der_256 = sizeof(ecc_clikeypub_der_256); |
| 139 | + |
| 140 | + /* Signature for hash of 128-bytes data 0,1,2.... */ |
| 141 | + const byte sig[] = { |
| 142 | + 0x30, 0x45, |
| 143 | + 0x02, 0x20, |
| 144 | + 0x5e, 0x01, 0x3d, 0xfb, 0x18, 0x61, 0xe3, 0xe3, |
| 145 | + 0xa0, 0xff, 0x9b, 0x99, 0xb5, 0x2f, 0x89, 0xba, |
| 146 | + 0xb3, 0xbb, 0x62, 0x08, 0x9b, 0x94, 0x0e, 0x61, |
| 147 | + 0x9c, 0x00, 0x7b, 0x43, 0x9c, 0x28, 0xcb, 0xf6, |
| 148 | + 0x02, 0x21, |
| 149 | + 0x00, |
| 150 | + 0xb4, 0x72, 0x2f, 0x22, 0x92, 0xa2, 0x8d, 0x01, |
| 151 | + 0x6b, 0x01, 0x35, 0x5c, 0xcb, 0x34, 0xa2, 0x21, |
| 152 | + 0x32, 0xe2, 0xe6, 0x66, 0xd0, 0x9e, 0x3e, 0x8e, |
| 153 | + 0x84, 0x5d, 0xa4, 0x69, 0x54, 0x52, 0xfc, 0xef |
| 154 | + }; |
| 155 | + word32 sigLen = (word32)sizeof(sig); |
| 156 | +#else |
| 157 | + byte sig[ECC_MAX_SIG_SIZE]; |
| 158 | + word32 sigLen; |
| 159 | +#endif |
| 160 | + |
| 161 | + memset(&rng, 0, sizeof(rng)); |
| 162 | + memset(&eccKey, 0, sizeof(eccKey)); |
| 163 | + |
| 164 | + wolfSSL_Debugging_ON(); |
| 165 | + |
| 166 | + wolfCrypt_Init(); |
| 167 | + |
| 168 | + /* test data */ |
| 169 | + for (i=0; i<(int)dataLen; i++) { |
| 170 | + data[i] = (byte)i; |
| 171 | + } |
| 172 | + |
| 173 | + ret = wc_InitRng(&rng); |
| 174 | + if (ret != 0) { |
| 175 | + goto exit; |
| 176 | + } |
| 177 | + |
| 178 | + for (i=0; i<TEST_KEYGEN_TRIES; i++) { |
| 179 | + ret = wc_ecc_init_ex(&eccKey, NULL, 0); |
| 180 | + if (ret == 0) { |
| 181 | + inOutIdx = 0; |
| 182 | +#ifdef TEST_VERIFY_ONLY |
| 183 | + ret = wc_EccPublicKeyDecode(ecc_clikeypub_der_256, &inOutIdx, |
| 184 | + &eccKey, sizeof_ecc_clikeypub_der_256); |
| 185 | +#else |
| 186 | + #if (defined(_SILICON_LABS_SECURITY_FEATURE) && \ |
| 187 | + _SILICON_LABS_SECURITY_FEATURE == _SILICON_LABS_SECURITY_FEATURE_VAULT) |
| 188 | + /* Load ecc_key with vault's public key. |
| 189 | + * When only the public area of a key is loaded silabs_ecc.c |
| 190 | + * (silabs_ecc_sign_hash) will use the vault key to sign */ |
| 191 | + ret = silabs_ecc_load_vault(&eccKey); |
| 192 | + #elif defined(TEST_PRIVATE_IMPORT) |
| 193 | + ret = wc_EccPrivateKeyDecode(ecc_clikey_der_256, &inOutIdx, |
| 194 | + &eccKey, sizeof_ecc_clikey_der_256); |
| 195 | + #else |
| 196 | + ret = wc_ecc_make_key(&rng, TEST_ECC_KEYSZ, &eccKey); |
| 197 | + #endif |
| 198 | +#endif |
| 199 | + } |
| 200 | + |
| 201 | + for (j=0; j<TEST_ECDSA_TRIES; j++) { |
| 202 | +#ifndef TEST_VERIFY_ONLY |
| 203 | + if (ret == 0) { |
| 204 | + /* generate signature using ecc key */ |
| 205 | + sigLen = (word32)sizeof(sig); |
| 206 | + |
| 207 | + ret = wc_SignatureGenerate( |
| 208 | + WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, |
| 209 | + data, dataLen, |
| 210 | + sig, &sigLen, |
| 211 | + &eccKey, (word32)sizeof(eccKey), |
| 212 | + &rng); |
| 213 | + } |
| 214 | +#endif |
| 215 | + if (ret == 0) { |
| 216 | + ret = wc_SignatureVerify( |
| 217 | + WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, |
| 218 | + data, dataLen, |
| 219 | + sig, sigLen, |
| 220 | + &eccKey, (word32)sizeof(eccKey)); |
| 221 | + } |
| 222 | + |
| 223 | + if (ret == 0) { |
| 224 | + fprintf(stderr, "Verification Passed %d %d\n", i, j); |
| 225 | + } |
| 226 | + else { |
| 227 | + fprintf(stderr, "Verification failed!! (ret %d) %d %d\n", |
| 228 | + ret, i, j); |
| 229 | + break; |
| 230 | + } |
| 231 | + } /* sign/verify tries */ |
| 232 | + |
| 233 | + wc_ecc_free(&eccKey); |
| 234 | + if (ret != 0) |
| 235 | + break; |
| 236 | + } /* key gen tries */ |
| 237 | + |
| 238 | +exit: |
| 239 | + wc_FreeRng(&rng); |
| 240 | + |
| 241 | + wolfCrypt_Cleanup(); |
| 242 | + |
| 243 | + (void)arguments; |
| 244 | +} |
0 commit comments