signature.c 17 KB

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