signature.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /* signature.c
  2. *
  3. * Copyright (C) 2006-2022 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. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <wolfssl/wolfcrypt/settings.h>
  25. #include <wolfssl/wolfcrypt/signature.h>
  26. #include <wolfssl/wolfcrypt/error-crypt.h>
  27. #include <wolfssl/wolfcrypt/logging.h>
  28. #ifndef NO_ASN
  29. #include <wolfssl/wolfcrypt/asn.h>
  30. #endif
  31. #ifdef HAVE_ECC
  32. #include <wolfssl/wolfcrypt/ecc.h>
  33. #endif
  34. #ifndef NO_RSA
  35. #include <wolfssl/wolfcrypt/rsa.h>
  36. #endif
  37. /* If ECC and RSA are disabled then disable signature wrapper */
  38. #if (!defined(HAVE_ECC) || (defined(HAVE_ECC) && !defined(HAVE_ECC_SIGN) \
  39. && !defined(HAVE_ECC_VERIFY))) && defined(NO_RSA)
  40. #undef NO_SIG_WRAPPER
  41. #define NO_SIG_WRAPPER
  42. #endif
  43. /* Signature wrapper disabled check */
  44. #ifndef NO_SIG_WRAPPER
  45. #if !defined(NO_RSA) && defined(WOLFSSL_CRYPTOCELL)
  46. extern int cc310_RsaSSL_Verify(const byte* in, word32 inLen, byte* sig,
  47. RsaKey* key, CRYS_RSA_HASH_OpMode_t mode);
  48. extern int cc310_RsaSSL_Sign(const byte* in, word32 inLen, byte* out,
  49. word32 outLen, RsaKey* key, CRYS_RSA_HASH_OpMode_t mode);
  50. #endif
  51. #if !defined(NO_RSA) && !defined(NO_ASN)
  52. static int wc_SignatureDerEncode(enum wc_HashType hash_type, byte* hash_data,
  53. word32 hash_len, word32* hash_enc_len)
  54. {
  55. int ret, oid;
  56. ret = wc_HashGetOID(hash_type);
  57. if (ret < 0) {
  58. return ret;
  59. }
  60. oid = ret;
  61. ret = wc_EncodeSignature(hash_data, hash_data, hash_len, oid);
  62. if (ret > 0) {
  63. *hash_enc_len = ret;
  64. ret = 0;
  65. }
  66. return ret;
  67. }
  68. #endif /* !NO_RSA && !NO_ASN */
  69. int wc_SignatureGetSize(enum wc_SignatureType sig_type,
  70. const void* key, word32 key_len)
  71. {
  72. int sig_len = BAD_FUNC_ARG;
  73. /* Suppress possible unused args if all signature types are disabled */
  74. (void)key;
  75. (void)key_len;
  76. switch(sig_type) {
  77. case WC_SIGNATURE_TYPE_ECC:
  78. #ifdef HAVE_ECC
  79. /* Sanity check that void* key is at least ecc_key in size */
  80. if (key_len >= sizeof(ecc_key)) {
  81. sig_len = wc_ecc_sig_size((ecc_key*)key);
  82. }
  83. else {
  84. WOLFSSL_MSG("wc_SignatureGetSize: Invalid ECC key size");
  85. }
  86. #else
  87. sig_len = SIG_TYPE_E;
  88. #endif
  89. break;
  90. case WC_SIGNATURE_TYPE_RSA_W_ENC:
  91. case WC_SIGNATURE_TYPE_RSA:
  92. #ifndef NO_RSA
  93. /* Sanity check that void* key is at least RsaKey in size */
  94. if (key_len >= sizeof(RsaKey)) {
  95. sig_len = wc_RsaEncryptSize((RsaKey*)key);
  96. }
  97. else {
  98. WOLFSSL_MSG("wc_SignatureGetSize: Invalid RsaKey key size");
  99. }
  100. #else
  101. sig_len = SIG_TYPE_E;
  102. #endif
  103. break;
  104. case WC_SIGNATURE_TYPE_NONE:
  105. default:
  106. sig_len = BAD_FUNC_ARG;
  107. break;
  108. }
  109. return sig_len;
  110. }
  111. int wc_SignatureVerifyHash(
  112. enum wc_HashType hash_type, enum wc_SignatureType sig_type,
  113. const byte* hash_data, word32 hash_len,
  114. const byte* sig, word32 sig_len,
  115. const void* key, word32 key_len)
  116. {
  117. int ret;
  118. /* Check arguments */
  119. if (hash_data == NULL || hash_len == 0 ||
  120. sig == NULL || sig_len == 0 ||
  121. key == NULL || key_len == 0) {
  122. return BAD_FUNC_ARG;
  123. }
  124. /* Validate signature len (1 to max is okay) */
  125. if ((int)sig_len > wc_SignatureGetSize(sig_type, key, key_len)) {
  126. WOLFSSL_MSG("wc_SignatureVerify: Invalid sig type/len");
  127. return BAD_FUNC_ARG;
  128. }
  129. /* Validate hash size */
  130. ret = wc_HashGetDigestSize(hash_type);
  131. if (ret < 0) {
  132. WOLFSSL_MSG("wc_SignatureVerify: Invalid hash type/len");
  133. return ret;
  134. }
  135. ret = 0;
  136. /* Verify signature using hash */
  137. switch (sig_type) {
  138. case WC_SIGNATURE_TYPE_ECC:
  139. {
  140. #if defined(HAVE_ECC) && defined(HAVE_ECC_VERIFY)
  141. int is_valid_sig = 0;
  142. /* Perform verification of signature using provided ECC key */
  143. do {
  144. #ifdef WOLFSSL_ASYNC_CRYPT
  145. ret = wc_AsyncWait(ret, &((ecc_key*)key)->asyncDev,
  146. WC_ASYNC_FLAG_CALL_AGAIN);
  147. #endif
  148. if (ret >= 0)
  149. ret = wc_ecc_verify_hash(sig, sig_len, hash_data, hash_len,
  150. &is_valid_sig, (ecc_key*)key);
  151. } while (ret == WC_PENDING_E);
  152. if (ret != 0 || is_valid_sig != 1) {
  153. ret = SIG_VERIFY_E;
  154. }
  155. #else
  156. ret = SIG_TYPE_E;
  157. #endif
  158. break;
  159. }
  160. case WC_SIGNATURE_TYPE_RSA_W_ENC:
  161. case WC_SIGNATURE_TYPE_RSA:
  162. {
  163. #ifndef NO_RSA
  164. #ifdef WOLFSSL_CRYPTOCELL
  165. if (sig_type == WC_SIGNATURE_TYPE_RSA_W_ENC) {
  166. ret = cc310_RsaSSL_Verify(hash_data, hash_len, (byte*)sig,
  167. (RsaKey*)key, cc310_hashModeRSA(hash_type, 0));
  168. }
  169. else {
  170. ret = cc310_RsaSSL_Verify(hash_data, hash_len, (byte*)sig,
  171. (RsaKey*)key, cc310_hashModeRSA(hash_type, 1));
  172. }
  173. if (ret != 0) {
  174. ret = SIG_VERIFY_E;
  175. }
  176. #else
  177. word32 plain_len = hash_len;
  178. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  179. byte *plain_data;
  180. #else
  181. ALIGN64 byte plain_data[MAX_ENCODED_SIG_SZ];
  182. #endif
  183. /* Make sure the plain text output is at least key size */
  184. if (plain_len < sig_len) {
  185. plain_len = sig_len;
  186. }
  187. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  188. plain_data = (byte*)XMALLOC(plain_len, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  189. if (plain_data)
  190. #else
  191. if (plain_len <= sizeof(plain_data))
  192. #endif
  193. {
  194. byte* plain_ptr = NULL;
  195. XMEMSET(plain_data, 0, plain_len);
  196. XMEMCPY(plain_data, sig, sig_len);
  197. /* Perform verification of signature using provided RSA key */
  198. do {
  199. #ifdef WOLFSSL_ASYNC_CRYPT
  200. ret = wc_AsyncWait(ret, &((RsaKey*)key)->asyncDev,
  201. WC_ASYNC_FLAG_CALL_AGAIN);
  202. #endif
  203. if (ret >= 0)
  204. ret = wc_RsaSSL_VerifyInline(plain_data, sig_len, &plain_ptr, (RsaKey*)key);
  205. } while (ret == WC_PENDING_E);
  206. if (ret >= 0 && plain_ptr) {
  207. if ((word32)ret == hash_len &&
  208. XMEMCMP(plain_ptr, hash_data, hash_len) == 0) {
  209. ret = 0; /* Success */
  210. }
  211. else {
  212. ret = SIG_VERIFY_E;
  213. }
  214. }
  215. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  216. XFREE(plain_data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  217. #endif
  218. }
  219. else {
  220. ret = MEMORY_E;
  221. }
  222. #endif /* WOLFSSL_CRYPTOCELL */
  223. if (ret != 0) {
  224. WOLFSSL_MSG("RSA Signature Verify failed!");
  225. }
  226. #else
  227. ret = SIG_TYPE_E;
  228. #endif
  229. break;
  230. }
  231. case WC_SIGNATURE_TYPE_NONE:
  232. default:
  233. ret = BAD_FUNC_ARG;
  234. break;
  235. }
  236. return ret;
  237. }
  238. int wc_SignatureVerify(
  239. enum wc_HashType hash_type, enum wc_SignatureType sig_type,
  240. const byte* data, word32 data_len,
  241. const byte* sig, word32 sig_len,
  242. const void* key, word32 key_len)
  243. {
  244. int ret;
  245. word32 hash_len, hash_enc_len;
  246. #if defined(WOLFSSL_SMALL_STACK) || defined(NO_ASN)
  247. byte *hash_data;
  248. #else
  249. byte hash_data[MAX_DER_DIGEST_SZ];
  250. #endif
  251. /* Check arguments */
  252. if (data == NULL || data_len == 0 ||
  253. sig == NULL || sig_len == 0 ||
  254. key == NULL || key_len == 0) {
  255. return BAD_FUNC_ARG;
  256. }
  257. /* Validate signature len (1 to max is okay) */
  258. if ((int)sig_len > wc_SignatureGetSize(sig_type, key, key_len)) {
  259. WOLFSSL_MSG("wc_SignatureVerify: Invalid sig type/len");
  260. return BAD_FUNC_ARG;
  261. }
  262. /* Validate hash size */
  263. ret = wc_HashGetDigestSize(hash_type);
  264. if (ret < 0) {
  265. WOLFSSL_MSG("wc_SignatureVerify: Invalid hash type/len");
  266. return ret;
  267. }
  268. hash_enc_len = hash_len = ret;
  269. #ifndef NO_RSA
  270. if (sig_type == WC_SIGNATURE_TYPE_RSA_W_ENC) {
  271. /* For RSA with ASN.1 encoding include room */
  272. hash_enc_len += MAX_DER_DIGEST_ASN_SZ;
  273. }
  274. #endif
  275. #if defined(WOLFSSL_SMALL_STACK) || defined(NO_ASN)
  276. /* Allocate temporary buffer for hash data */
  277. hash_data = (byte*)XMALLOC(hash_enc_len, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  278. if (hash_data == NULL) {
  279. return MEMORY_E;
  280. }
  281. #endif
  282. /* Perform hash of data */
  283. ret = wc_Hash(hash_type, data, data_len, hash_data, hash_len);
  284. if (ret == 0) {
  285. /* Handle RSA with DER encoding */
  286. if (sig_type == WC_SIGNATURE_TYPE_RSA_W_ENC) {
  287. #if defined(NO_RSA) || defined(NO_ASN)
  288. ret = SIG_TYPE_E;
  289. #else
  290. ret = wc_SignatureDerEncode(hash_type, hash_data, hash_len,
  291. &hash_enc_len);
  292. #endif
  293. }
  294. if (ret == 0) {
  295. /* Verify signature using hash */
  296. ret = wc_SignatureVerifyHash(hash_type, sig_type,
  297. hash_data, hash_enc_len, sig, sig_len, key, key_len);
  298. }
  299. }
  300. #if defined(WOLFSSL_SMALL_STACK) || defined(NO_ASN)
  301. XFREE(hash_data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  302. #endif
  303. return ret;
  304. }
  305. int wc_SignatureGenerateHash(
  306. enum wc_HashType hash_type, enum wc_SignatureType sig_type,
  307. const byte* hash_data, word32 hash_len,
  308. byte* sig, word32 *sig_len,
  309. const void* key, word32 key_len, WC_RNG* rng)
  310. {
  311. return wc_SignatureGenerateHash_ex(hash_type, sig_type, hash_data, hash_len,
  312. sig, sig_len, key, key_len, rng, 1);
  313. }
  314. int wc_SignatureGenerateHash_ex(
  315. enum wc_HashType hash_type, enum wc_SignatureType sig_type,
  316. const byte* hash_data, word32 hash_len,
  317. byte* sig, word32 *sig_len,
  318. const void* key, word32 key_len, WC_RNG* rng, int verify)
  319. {
  320. int ret;
  321. /* Suppress possible unused arg if all signature types are disabled */
  322. (void)rng;
  323. /* Check arguments */
  324. if (hash_data == NULL || hash_len == 0 ||
  325. sig == NULL || sig_len == NULL || *sig_len == 0 ||
  326. key == NULL || key_len == 0) {
  327. return BAD_FUNC_ARG;
  328. }
  329. /* Validate signature len (needs to be at least max) */
  330. if ((int)*sig_len < wc_SignatureGetSize(sig_type, key, key_len)) {
  331. WOLFSSL_MSG("wc_SignatureGenerate: Invalid sig type/len");
  332. return BAD_FUNC_ARG;
  333. }
  334. /* Validate hash size */
  335. ret = wc_HashGetDigestSize(hash_type);
  336. if (ret < 0) {
  337. WOLFSSL_MSG("wc_SignatureGenerate: Invalid hash type/len");
  338. return ret;
  339. }
  340. ret = 0;
  341. /* Create signature using hash as data */
  342. switch (sig_type) {
  343. case WC_SIGNATURE_TYPE_ECC:
  344. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN)
  345. /* Create signature using provided ECC key */
  346. do {
  347. #ifdef WOLFSSL_ASYNC_CRYPT
  348. ret = wc_AsyncWait(ret, &((ecc_key*)key)->asyncDev,
  349. WC_ASYNC_FLAG_CALL_AGAIN);
  350. #endif
  351. if (ret >= 0)
  352. ret = wc_ecc_sign_hash(hash_data, hash_len, sig, sig_len,
  353. rng, (ecc_key*)key);
  354. } while (ret == WC_PENDING_E);
  355. #else
  356. ret = SIG_TYPE_E;
  357. #endif
  358. break;
  359. case WC_SIGNATURE_TYPE_RSA_W_ENC:
  360. case WC_SIGNATURE_TYPE_RSA:
  361. #if !defined(NO_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  362. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  363. #ifdef WOLFSSL_CRYPTOCELL
  364. /* use expected signature size (incoming sig_len could be larger buffer */
  365. *sig_len = wc_SignatureGetSize(sig_type, key, key_len);
  366. if (sig_type == WC_SIGNATURE_TYPE_RSA_W_ENC) {
  367. ret = cc310_RsaSSL_Sign(hash_data, hash_len, sig, *sig_len,
  368. (RsaKey*)key, cc310_hashModeRSA(hash_type, 0));
  369. }
  370. else {
  371. ret = cc310_RsaSSL_Sign(hash_data, hash_len, sig, *sig_len,
  372. (RsaKey*)key, cc310_hashModeRSA(hash_type, 1));
  373. }
  374. #else
  375. /* Create signature using provided RSA key */
  376. do {
  377. #ifdef WOLFSSL_ASYNC_CRYPT
  378. ret = wc_AsyncWait(ret, &((RsaKey*)key)->asyncDev,
  379. WC_ASYNC_FLAG_CALL_AGAIN);
  380. #endif
  381. if (ret >= 0)
  382. ret = wc_RsaSSL_Sign(hash_data, hash_len, sig, *sig_len,
  383. (RsaKey*)key, rng);
  384. } while (ret == WC_PENDING_E);
  385. #endif /* WOLFSSL_CRYPTOCELL */
  386. if (ret >= 0) {
  387. *sig_len = ret;
  388. ret = 0; /* Success */
  389. }
  390. #else
  391. ret = SIG_TYPE_E;
  392. #endif
  393. break;
  394. case WC_SIGNATURE_TYPE_NONE:
  395. default:
  396. ret = BAD_FUNC_ARG;
  397. break;
  398. }
  399. if (ret == 0 && verify) {
  400. ret = wc_SignatureVerifyHash(hash_type, sig_type, hash_data, hash_len,
  401. sig, *sig_len, key, key_len);
  402. }
  403. return ret;
  404. }
  405. int wc_SignatureGenerate(
  406. enum wc_HashType hash_type, enum wc_SignatureType sig_type,
  407. const byte* data, word32 data_len,
  408. byte* sig, word32 *sig_len,
  409. const void* key, word32 key_len, WC_RNG* rng)
  410. {
  411. return wc_SignatureGenerate_ex(hash_type, sig_type, data, data_len, sig,
  412. sig_len, key, key_len, rng, 1);
  413. }
  414. int wc_SignatureGenerate_ex(
  415. enum wc_HashType hash_type, enum wc_SignatureType sig_type,
  416. const byte* data, word32 data_len,
  417. byte* sig, word32 *sig_len,
  418. const void* key, word32 key_len, WC_RNG* rng, int verify)
  419. {
  420. int ret;
  421. word32 hash_len, hash_enc_len;
  422. #if defined(WOLFSSL_SMALL_STACK) || defined(NO_ASN)
  423. byte *hash_data;
  424. #else
  425. byte hash_data[MAX_DER_DIGEST_SZ];
  426. #endif
  427. /* Check arguments */
  428. if (data == NULL || data_len == 0 ||
  429. sig == NULL || sig_len == NULL || *sig_len == 0 ||
  430. key == NULL || key_len == 0) {
  431. return BAD_FUNC_ARG;
  432. }
  433. /* Validate signature len (needs to be at least max) */
  434. if ((int)*sig_len < wc_SignatureGetSize(sig_type, key, key_len)) {
  435. WOLFSSL_MSG("wc_SignatureGenerate: Invalid sig type/len");
  436. return BAD_FUNC_ARG;
  437. }
  438. /* Validate hash size */
  439. ret = wc_HashGetDigestSize(hash_type);
  440. if (ret < 0) {
  441. WOLFSSL_MSG("wc_SignatureGenerate: Invalid hash type/len");
  442. return ret;
  443. }
  444. hash_enc_len = hash_len = ret;
  445. #if !defined(NO_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  446. if (sig_type == WC_SIGNATURE_TYPE_RSA_W_ENC) {
  447. /* For RSA with ASN.1 encoding include room */
  448. hash_enc_len += MAX_DER_DIGEST_ASN_SZ;
  449. }
  450. #endif
  451. #if defined(WOLFSSL_SMALL_STACK) || defined(NO_ASN)
  452. /* Allocate temporary buffer for hash data */
  453. hash_data = (byte*)XMALLOC(hash_enc_len, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  454. if (hash_data == NULL) {
  455. return MEMORY_E;
  456. }
  457. #endif
  458. /* Perform hash of data */
  459. ret = wc_Hash(hash_type, data, data_len, hash_data, hash_len);
  460. if (ret == 0) {
  461. /* Handle RSA with DER encoding */
  462. if (sig_type == WC_SIGNATURE_TYPE_RSA_W_ENC) {
  463. #if defined(NO_RSA) || defined(NO_ASN) || \
  464. defined(WOLFSSL_RSA_PUBLIC_ONLY)
  465. ret = SIG_TYPE_E;
  466. #else
  467. ret = wc_SignatureDerEncode(hash_type, hash_data, hash_len,
  468. &hash_enc_len);
  469. #endif
  470. }
  471. if (ret == 0) {
  472. /* Generate signature using hash */
  473. ret = wc_SignatureGenerateHash(hash_type, sig_type,
  474. hash_data, hash_enc_len, sig, sig_len, key, key_len, rng);
  475. }
  476. }
  477. if (ret == 0 && verify) {
  478. ret = wc_SignatureVerifyHash(hash_type, sig_type, hash_data,
  479. hash_enc_len, sig, *sig_len, key, key_len);
  480. }
  481. #if defined(WOLFSSL_SMALL_STACK) || defined(NO_ASN)
  482. XFREE(hash_data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  483. #endif
  484. return ret;
  485. }
  486. #endif /* NO_SIG_WRAPPER */