deprecated_test.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
  2. /* SPDX-License-Identifier: Unlicense */
  3. #ifndef LTC_NO_DEPRECATED_APIS
  4. #define LTC_DEPRECATED(x)
  5. #include <tomcrypt_test.h>
  6. #ifdef LTC_MECC
  7. static void s_ecc_test(void)
  8. {
  9. const ltc_ecc_curve* dp;
  10. unsigned char buf[128];
  11. unsigned long len;
  12. ecc_key key;
  13. int stat;
  14. unsigned char data16[16] = { 0xd1, 0xd1, 0xd1, 0xd1, 0xd1, 0xd1, 0xd1, 0xd1, 0xd1, 0xd1, 0xd1, 0xd1, 0xd1, 0xd1, 0xd1, 0xd1 };
  15. /* We need an MPI provider for ECC */
  16. if (ltc_mp.name == NULL) return;
  17. ENSURE(ltc_ecc_curves[0].OID != NULL);
  18. DO(ecc_find_curve(ltc_ecc_curves[0].OID, &dp));
  19. DO(ecc_make_key_ex(&yarrow_prng, find_prng ("yarrow"), &key, dp));
  20. len = sizeof(buf);
  21. DO(ecc_sign_hash(data16, 16, buf, &len, &yarrow_prng, find_prng ("yarrow"), &key));
  22. stat = 0;
  23. DO(ecc_verify_hash(buf, len, data16, 16, &stat, &key));
  24. SHOULD_FAIL(ecc_verify_hash_rfc7518(buf, len, data16, 16, &stat, &key));
  25. len = sizeof(buf);
  26. DO(ecc_sign_hash_rfc7518(data16, 16, buf, &len, &yarrow_prng, find_prng ("yarrow"), &key));
  27. stat = 0;
  28. DO(ecc_verify_hash_rfc7518(buf, len, data16, 16, &stat, &key));
  29. SHOULD_FAIL(ecc_verify_hash(buf, len, data16, 16, &stat, &key));
  30. ecc_free(&key);
  31. }
  32. #endif
  33. #ifdef LTC_MRSA
  34. extern const unsigned char ltc_rsa_private_test_key[];
  35. extern const unsigned long ltc_rsa_private_test_key_sz;
  36. extern const unsigned char ltc_openssl_public_rsa[];
  37. extern const unsigned long ltc_openssl_public_rsa_sz;
  38. static void s_rsa_test(void)
  39. {
  40. rsa_key key, pubkey;
  41. int stat;
  42. const unsigned char tv[] = "test";
  43. unsigned char buf0[1024], buf1[1024];
  44. unsigned long buf0len, buf1len;
  45. /* We need an MPI provider for RSA */
  46. if (ltc_mp.name == NULL) return;
  47. DO(rsa_import(ltc_rsa_private_test_key, ltc_rsa_private_test_key_sz, &key));
  48. DO(rsa_import(ltc_openssl_public_rsa, ltc_openssl_public_rsa_sz, &pubkey));
  49. buf0len = sizeof(buf0);
  50. DO(rsa_sign_hash_ex(tv, 4, buf0, &buf0len, LTC_PKCS_1_PSS, &yarrow_prng, find_prng("yarrow"), find_hash("sha1"), 8, &key));
  51. buf1len = sizeof(buf1);
  52. DO(rsa_verify_hash_ex(buf0, buf0len, tv, 4, LTC_PKCS_1_PSS, find_hash("sha1"), 8, &stat, &pubkey));
  53. ENSURE(stat == 1);
  54. buf0len = sizeof(buf0);
  55. DO(rsa_encrypt_key_ex(tv, 4, buf0, &buf0len, NULL, 0, &yarrow_prng, find_prng("yarrow"), find_hash("sha1"), LTC_PKCS_1_OAEP, &pubkey));
  56. buf1len = sizeof(buf1);
  57. DO(rsa_decrypt_key_ex(buf0, buf0len, buf1, &buf1len, NULL, 0, find_hash("sha1"), LTC_PKCS_1_OAEP, &stat, &key));
  58. ENSURE(stat == 1);
  59. COMPARE_TESTVECTOR(buf1, buf1len, tv, 4, "s_rsa_test", 0);
  60. rsa_free(&pubkey);
  61. rsa_free(&key);
  62. }
  63. #endif
  64. #endif /* LTC_NO_DEPRECATED_APIS */
  65. int deprecated_test(void)
  66. {
  67. #ifndef LTC_NO_DEPRECATED_APIS
  68. #ifdef LTC_MECC
  69. s_ecc_test();
  70. #endif
  71. #ifdef LTC_MRSA
  72. s_rsa_test();
  73. #endif
  74. #endif /* LTC_NO_DEPRECATED_APIS */
  75. return 0;
  76. }