signature.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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. #ifdef WOLFSSL_CRYPTOCELL
  159. if (sig_type == WC_SIGNATURE_TYPE_RSA_W_ENC) {
  160. ret = cc310_RsaSSL_Verify(hash_data, hash_len, (byte*)sig, key,
  161. cc310_hashModeRSA(hash_type, 0));
  162. }
  163. else {
  164. ret = cc310_RsaSSL_Verify(hash_data, hash_len, (byte*)sig, key,
  165. cc310_hashModeRSA(hash_type, 1));
  166. }
  167. #else
  168. word32 plain_len = hash_len;
  169. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  170. byte *plain_data;
  171. #else
  172. byte plain_data[MAX_ENCODED_SIG_SZ];
  173. #endif
  174. /* Make sure the plain text output is at least key size */
  175. if (plain_len < sig_len) {
  176. plain_len = sig_len;
  177. }
  178. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  179. plain_data = (byte*)XMALLOC(plain_len, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  180. if (plain_data)
  181. #else
  182. if (plain_len <= sizeof(plain_data))
  183. #endif
  184. {
  185. byte* plain_ptr = NULL;
  186. XMEMSET(plain_data, 0, plain_len);
  187. XMEMCPY(plain_data, sig, sig_len);
  188. /* Perform verification of signature using provided RSA key */
  189. do {
  190. #ifdef WOLFSSL_ASYNC_CRYPT
  191. ret = wc_AsyncWait(ret, &((RsaKey*)key)->asyncDev,
  192. WC_ASYNC_FLAG_CALL_AGAIN);
  193. #endif
  194. if (ret >= 0)
  195. ret = wc_RsaSSL_VerifyInline(plain_data, sig_len, &plain_ptr, (RsaKey*)key);
  196. } while (ret == WC_PENDING_E);
  197. if (ret >= 0 && plain_ptr) {
  198. if ((word32)ret == hash_len &&
  199. XMEMCMP(plain_ptr, hash_data, hash_len) == 0) {
  200. ret = 0; /* Success */
  201. }
  202. else {
  203. ret = SIG_VERIFY_E;
  204. }
  205. }
  206. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  207. XFREE(plain_data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  208. #endif
  209. }
  210. else {
  211. ret = MEMORY_E;
  212. }
  213. #endif /* WOLFSSL_CRYPTOCELL */
  214. if (ret != 0) {
  215. WOLFSSL_MSG("RSA Signature Verify difference!");
  216. ret = SIG_VERIFY_E;
  217. }
  218. #else
  219. ret = SIG_TYPE_E;
  220. #endif
  221. break;
  222. }
  223. case WC_SIGNATURE_TYPE_NONE:
  224. default:
  225. ret = BAD_FUNC_ARG;
  226. break;
  227. }
  228. return ret;
  229. }
  230. int wc_SignatureVerify(
  231. enum wc_HashType hash_type, enum wc_SignatureType sig_type,
  232. const byte* data, word32 data_len,
  233. const byte* sig, word32 sig_len,
  234. const void* key, word32 key_len)
  235. {
  236. int ret;
  237. word32 hash_len, hash_enc_len;
  238. #if defined(WOLFSSL_SMALL_STACK) || defined(NO_ASN)
  239. byte *hash_data;
  240. #else
  241. byte hash_data[MAX_DER_DIGEST_SZ];
  242. #endif
  243. /* Check arguments */
  244. if (data == NULL || data_len == 0 ||
  245. sig == NULL || sig_len == 0 ||
  246. key == NULL || key_len == 0) {
  247. return BAD_FUNC_ARG;
  248. }
  249. /* Validate signature len (1 to max is okay) */
  250. if ((int)sig_len > wc_SignatureGetSize(sig_type, key, key_len)) {
  251. WOLFSSL_MSG("wc_SignatureVerify: Invalid sig type/len");
  252. return BAD_FUNC_ARG;
  253. }
  254. /* Validate hash size */
  255. ret = wc_HashGetDigestSize(hash_type);
  256. if (ret < 0) {
  257. WOLFSSL_MSG("wc_SignatureVerify: Invalid hash type/len");
  258. return ret;
  259. }
  260. hash_enc_len = hash_len = ret;
  261. #ifndef NO_RSA
  262. if (sig_type == WC_SIGNATURE_TYPE_RSA_W_ENC) {
  263. /* For RSA with ASN.1 encoding include room */
  264. hash_enc_len += MAX_DER_DIGEST_ASN_SZ;
  265. }
  266. #endif
  267. #if defined(WOLFSSL_SMALL_STACK) || defined(NO_ASN)
  268. /* Allocate temporary buffer for hash data */
  269. hash_data = (byte*)XMALLOC(hash_enc_len, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  270. if (hash_data == NULL) {
  271. return MEMORY_E;
  272. }
  273. #endif
  274. /* Perform hash of data */
  275. ret = wc_Hash(hash_type, data, data_len, hash_data, hash_len);
  276. if (ret == 0) {
  277. /* Handle RSA with DER encoding */
  278. if (sig_type == WC_SIGNATURE_TYPE_RSA_W_ENC) {
  279. #if defined(NO_RSA) || defined(NO_ASN)
  280. ret = SIG_TYPE_E;
  281. #else
  282. ret = wc_SignatureDerEncode(hash_type, hash_data, hash_len,
  283. &hash_enc_len);
  284. #endif
  285. }
  286. if (ret == 0) {
  287. /* Verify signature using hash */
  288. ret = wc_SignatureVerifyHash(hash_type, sig_type,
  289. hash_data, hash_enc_len, sig, sig_len, key, key_len);
  290. }
  291. }
  292. #if defined(WOLFSSL_SMALL_STACK) || defined(NO_ASN)
  293. XFREE(hash_data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  294. #endif
  295. return ret;
  296. }
  297. int wc_SignatureGenerateHash(
  298. enum wc_HashType hash_type, enum wc_SignatureType sig_type,
  299. const byte* hash_data, word32 hash_len,
  300. byte* sig, word32 *sig_len,
  301. const void* key, word32 key_len, WC_RNG* rng)
  302. {
  303. return wc_SignatureGenerateHash_ex(hash_type, sig_type, hash_data, hash_len,
  304. sig, sig_len, key, key_len, rng, 1);
  305. }
  306. int wc_SignatureGenerateHash_ex(
  307. enum wc_HashType hash_type, enum wc_SignatureType sig_type,
  308. const byte* hash_data, word32 hash_len,
  309. byte* sig, word32 *sig_len,
  310. const void* key, word32 key_len, WC_RNG* rng, int verify)
  311. {
  312. int ret;
  313. /* Suppress possible unused arg if all signature types are disabled */
  314. (void)rng;
  315. /* Check arguments */
  316. if (hash_data == NULL || hash_len == 0 ||
  317. sig == NULL || sig_len == NULL || *sig_len == 0 ||
  318. key == NULL || key_len == 0) {
  319. return BAD_FUNC_ARG;
  320. }
  321. /* Validate signature len (needs to be at least max) */
  322. if ((int)*sig_len < wc_SignatureGetSize(sig_type, key, key_len)) {
  323. WOLFSSL_MSG("wc_SignatureGenerate: Invalid sig type/len");
  324. return BAD_FUNC_ARG;
  325. }
  326. /* Validate hash size */
  327. ret = wc_HashGetDigestSize(hash_type);
  328. if (ret < 0) {
  329. WOLFSSL_MSG("wc_SignatureGenerate: Invalid hash type/len");
  330. return ret;
  331. }
  332. ret = 0;
  333. /* Create signature using hash as data */
  334. switch (sig_type) {
  335. case WC_SIGNATURE_TYPE_ECC:
  336. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN)
  337. /* Create signature using provided ECC key */
  338. do {
  339. #ifdef WOLFSSL_ASYNC_CRYPT
  340. ret = wc_AsyncWait(ret, &((ecc_key*)key)->asyncDev,
  341. WC_ASYNC_FLAG_CALL_AGAIN);
  342. #endif
  343. if (ret >= 0)
  344. ret = wc_ecc_sign_hash(hash_data, hash_len, sig, sig_len,
  345. rng, (ecc_key*)key);
  346. } while (ret == WC_PENDING_E);
  347. #else
  348. ret = SIG_TYPE_E;
  349. #endif
  350. break;
  351. case WC_SIGNATURE_TYPE_RSA_W_ENC:
  352. case WC_SIGNATURE_TYPE_RSA:
  353. #if !defined(NO_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  354. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  355. #ifdef WOLFSSL_CRYPTOCELL
  356. /* use expected signature size (incoming sig_len could be larger buffer */
  357. *sig_len = wc_SignatureGetSize(sig_type, key, key_len);
  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. #if defined(WOLFSSL_SMALL_STACK) || defined(NO_ASN)
  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. #if defined(WOLFSSL_SMALL_STACK) || defined(NO_ASN)
  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. /* Generate signature using hash */
  465. ret = wc_SignatureGenerateHash(hash_type, sig_type,
  466. hash_data, hash_enc_len, sig, sig_len, key, key_len, rng);
  467. }
  468. }
  469. if (ret == 0 && verify) {
  470. ret = wc_SignatureVerifyHash(hash_type, sig_type, hash_data,
  471. hash_enc_len, sig, *sig_len, key, key_len);
  472. }
  473. #if defined(WOLFSSL_SMALL_STACK) || defined(NO_ASN)
  474. XFREE(hash_data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  475. #endif
  476. return ret;
  477. }
  478. #endif /* NO_SIG_WRAPPER */