1
0

test_wolf.c 7.7 KB

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