rsa_test.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  1. /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
  2. /* SPDX-License-Identifier: Unlicense */
  3. #include <tomcrypt_test.h>
  4. #if defined(LTC_MRSA)
  5. #if defined(LTC_TEST_DBG) && LTC_TEST_DBG > 1
  6. #include <malloc.h>
  7. #define dbg_malloc_stats() do{ malloc_stats(); }while(0)
  8. #else
  9. #define dbg_malloc_stats() do{ }while(0)
  10. #endif
  11. /* These are test keys [see file test.key] that I use to test my import/export against */
  12. const unsigned char ltc_rsa_private_test_key[] = {
  13. 0x30, 0x82, 0x02, 0x5e, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0xcf, 0x9a, 0xde, 0x64, 0x8a,
  14. 0xda, 0xc8, 0x33, 0x20, 0xa9, 0xd7, 0x83, 0x31, 0x19, 0x54, 0xb2, 0x9a, 0x85, 0xa7, 0xa1, 0xb7,
  15. 0x75, 0x33, 0xb6, 0xa9, 0xac, 0x84, 0x24, 0xb3, 0xde, 0xdb, 0x7d, 0x85, 0x2d, 0x96, 0x65, 0xe5,
  16. 0x3f, 0x72, 0x95, 0x24, 0x9f, 0x28, 0x68, 0xca, 0x4f, 0xdb, 0x44, 0x1c, 0x3e, 0x60, 0x12, 0x8a,
  17. 0xdd, 0x26, 0xa5, 0xeb, 0xff, 0x0b, 0x5e, 0xd4, 0x88, 0x38, 0x49, 0x2a, 0x6e, 0x5b, 0xbf, 0x12,
  18. 0x37, 0x47, 0xbd, 0x05, 0x6b, 0xbc, 0xdb, 0xf3, 0xee, 0xe4, 0x11, 0x8e, 0x41, 0x68, 0x7c, 0x61,
  19. 0x13, 0xd7, 0x42, 0xc8, 0x80, 0xbe, 0x36, 0x8f, 0xdc, 0x08, 0x8b, 0x4f, 0xac, 0xa4, 0xe2, 0x76,
  20. 0x0c, 0xc9, 0x63, 0x6c, 0x49, 0x58, 0x93, 0xed, 0xcc, 0xaa, 0xdc, 0x25, 0x3b, 0x0a, 0x60, 0x3f,
  21. 0x8b, 0x54, 0x3a, 0xc3, 0x4d, 0x31, 0xe7, 0x94, 0xa4, 0x44, 0xfd, 0x02, 0x03, 0x01, 0x00, 0x01,
  22. 0x02, 0x81, 0x81, 0x00, 0xc8, 0x62, 0xb9, 0xea, 0xde, 0x44, 0x53, 0x1d, 0x56, 0x97, 0xd9, 0x97,
  23. 0x9e, 0x1a, 0xcf, 0x30, 0x1e, 0x0a, 0x88, 0x45, 0x86, 0x29, 0x30, 0xa3, 0x4d, 0x9f, 0x61, 0x65,
  24. 0x73, 0xe0, 0xd6, 0x87, 0x8f, 0xb6, 0xf3, 0x06, 0xa3, 0x82, 0xdc, 0x7c, 0xac, 0xfe, 0x9b, 0x28,
  25. 0x9a, 0xae, 0xfd, 0xfb, 0xfe, 0x2f, 0x0e, 0xd8, 0x97, 0x04, 0xe3, 0xbb, 0x1f, 0xd1, 0xec, 0x0d,
  26. 0xba, 0xa3, 0x49, 0x7f, 0x47, 0xac, 0x8a, 0x44, 0x04, 0x7e, 0x86, 0xb7, 0x39, 0x42, 0x3f, 0xad,
  27. 0x1e, 0xb7, 0x0e, 0xa5, 0x51, 0xf4, 0x40, 0x63, 0x1e, 0xfd, 0xbd, 0xea, 0x9f, 0x41, 0x9f, 0xa8,
  28. 0x90, 0x1d, 0x6f, 0x0a, 0x5a, 0x95, 0x13, 0x11, 0x0d, 0x80, 0xaf, 0x5f, 0x64, 0x98, 0x8a, 0x2c,
  29. 0x78, 0x68, 0x65, 0xb0, 0x2b, 0x8b, 0xa2, 0x53, 0x87, 0xca, 0xf1, 0x64, 0x04, 0xab, 0xf2, 0x7b,
  30. 0xdb, 0x83, 0xc8, 0x81, 0x02, 0x41, 0x00, 0xf7, 0xbe, 0x5e, 0x23, 0xc3, 0x32, 0x3f, 0xbf, 0x8b,
  31. 0x8e, 0x3a, 0xee, 0xfc, 0xfc, 0xcb, 0xe5, 0xf7, 0xf1, 0x0b, 0xbc, 0x42, 0x82, 0xae, 0xd5, 0x7a,
  32. 0x3e, 0xca, 0xf7, 0xd5, 0x69, 0x3f, 0x64, 0x25, 0xa2, 0x1f, 0xb7, 0x75, 0x75, 0x05, 0x92, 0x42,
  33. 0xeb, 0xb8, 0xf1, 0xf3, 0x0a, 0x05, 0xe3, 0x94, 0xd1, 0x55, 0x78, 0x35, 0xa0, 0x36, 0xa0, 0x9b,
  34. 0x7c, 0x92, 0x84, 0x6c, 0xdd, 0xdc, 0x4d, 0x02, 0x41, 0x00, 0xd6, 0x86, 0x0e, 0x85, 0x42, 0x0b,
  35. 0x04, 0x08, 0x84, 0x21, 0x60, 0xf0, 0x0e, 0x0d, 0x88, 0xfd, 0x1e, 0x36, 0x10, 0x65, 0x4f, 0x1e,
  36. 0x53, 0xb4, 0x08, 0x72, 0x80, 0x5c, 0x3f, 0x59, 0x66, 0x17, 0xe6, 0x98, 0xf2, 0xe9, 0x6c, 0x7a,
  37. 0x06, 0x4c, 0xac, 0x76, 0x3d, 0xed, 0x8c, 0xa1, 0xce, 0xad, 0x1b, 0xbd, 0xb4, 0x7d, 0x28, 0xbc,
  38. 0xe3, 0x0e, 0x38, 0x8d, 0x99, 0xd8, 0x05, 0xb5, 0xa3, 0x71, 0x02, 0x40, 0x6d, 0xeb, 0xc3, 0x2d,
  39. 0x2e, 0xf0, 0x5e, 0xa4, 0x88, 0x31, 0x05, 0x29, 0x00, 0x8a, 0xd1, 0x95, 0x29, 0x9b, 0x83, 0xcf,
  40. 0x75, 0xdb, 0x31, 0xe3, 0x7a, 0x27, 0xde, 0x3a, 0x74, 0x30, 0x0c, 0x76, 0x4c, 0xd4, 0x50, 0x2a,
  41. 0x40, 0x2d, 0x39, 0xd9, 0x99, 0x63, 0xa9, 0x5d, 0x80, 0xae, 0x53, 0xca, 0x94, 0x3f, 0x05, 0x23,
  42. 0x1e, 0xf8, 0x05, 0x04, 0xe1, 0xb8, 0x35, 0xf2, 0x17, 0xb3, 0xa0, 0x89, 0x02, 0x41, 0x00, 0xab,
  43. 0x90, 0x88, 0xfa, 0x60, 0x08, 0x29, 0x50, 0x9a, 0x43, 0x8b, 0xa0, 0x50, 0xcc, 0xd8, 0x5a, 0xfe,
  44. 0x97, 0x64, 0x63, 0x71, 0x74, 0x22, 0xa3, 0x20, 0x02, 0x5a, 0xcf, 0xeb, 0xc6, 0x16, 0x95, 0x54,
  45. 0xd1, 0xcb, 0xab, 0x8d, 0x1a, 0xc6, 0x00, 0xfa, 0x08, 0x92, 0x9c, 0x71, 0xd5, 0x52, 0x52, 0x35,
  46. 0x96, 0x71, 0x4b, 0x8b, 0x92, 0x0c, 0xd0, 0xe9, 0xbf, 0xad, 0x63, 0x0b, 0xa5, 0xe9, 0xb1, 0x02,
  47. 0x41, 0x00, 0xdc, 0xcc, 0x27, 0xc8, 0xe4, 0xdc, 0x62, 0x48, 0xd5, 0x9b, 0xaf, 0xf5, 0xab, 0x60,
  48. 0xf6, 0x21, 0xfd, 0x53, 0xe2, 0xb7, 0x5d, 0x09, 0xc9, 0x1a, 0xa1, 0x04, 0xa9, 0xfc, 0x61, 0x2c,
  49. 0x5d, 0x04, 0x58, 0x3a, 0x5a, 0x39, 0xf1, 0x4a, 0x21, 0x56, 0x67, 0xfd, 0xcc, 0x20, 0xa3, 0x8f,
  50. 0x78, 0x18, 0x5a, 0x79, 0x3d, 0x2e, 0x8e, 0x7e, 0x86, 0x0a, 0xe6, 0xa8, 0x33, 0xc1, 0x04, 0x17,
  51. 0x4a, 0x9f, };
  52. const unsigned long ltc_rsa_private_test_key_sz = sizeof(ltc_rsa_private_test_key);
  53. static const char x509_public_rsa[] =
  54. "MIICdTCCAd4CCQCYjCwz0l9JpjANBgkqhkiG9w0BAQsFADB+MQswCQYDVQQGEwJD\
  55. WjEPMA0GA1UECAwGTW9yYXZhMQ0wCwYDVQQHDARCcm5vMRAwDgYDVQQKDAdMVEMg\
  56. THRkMQ8wDQYDVQQLDAZDcnlwdG8xEjAQBgNVBAMMCVRlc3QgQ2VydDEYMBYGCSqG\
  57. SIb3DQEJARYJdGVzdEBjZXJ0MCAXDTE3MDMwOTIzNDMzOVoYDzIyOTAxMjIyMjM0\
  58. MzM5WjB+MQswCQYDVQQGEwJDWjEPMA0GA1UECAwGTW9yYXZhMQ0wCwYDVQQHDARC\
  59. cm5vMRAwDgYDVQQKDAdMVEMgTHRkMQ8wDQYDVQQLDAZDcnlwdG8xEjAQBgNVBAMM\
  60. CVRlc3QgQ2VydDEYMBYGCSqGSIb3DQEJARYJdGVzdEBjZXJ0MIGfMA0GCSqGSIb3\
  61. DQEBAQUAA4GNADCBiQKBgQDPmt5kitrIMyCp14MxGVSymoWnobd1M7aprIQks97b\
  62. fYUtlmXlP3KVJJ8oaMpP20QcPmASit0mpev/C17UiDhJKm5bvxI3R70Fa7zb8+7k\
  63. EY5BaHxhE9dCyIC+No/cCItPrKTidgzJY2xJWJPtzKrcJTsKYD+LVDrDTTHnlKRE\
  64. /QIDAQABMA0GCSqGSIb3DQEBCwUAA4GBAApwWqupmmLGHeKOLFLcthQpAXXYep6T\
  65. 3S3e8X7fIG6TGhfvn5DHn+/V/C4184oOCwImI+VYRokdXdQ1AMGfVUomHJxsFPia\
  66. bv5Aw3hiKsIG3jigKHwmMScgkl3yn+8hLkx6thNbqQoa6Yyo20RqaEFBwlZ5G8lF\
  67. rZsdeO84SeCH";
  68. static const unsigned char pkcs8_private_rsa[] = {
  69. 0x30, 0x82, 0x02, 0x78, 0x02, 0x01, 0x00, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
  70. 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82, 0x02, 0x62, 0x30, 0x82, 0x02, 0x5e, 0x02, 0x01,
  71. 0x00, 0x02, 0x81, 0x81, 0x00, 0xcf, 0x9a, 0xde, 0x64, 0x8a, 0xda, 0xc8, 0x33, 0x20, 0xa9, 0xd7,
  72. 0x83, 0x31, 0x19, 0x54, 0xb2, 0x9a, 0x85, 0xa7, 0xa1, 0xb7, 0x75, 0x33, 0xb6, 0xa9, 0xac, 0x84,
  73. 0x24, 0xb3, 0xde, 0xdb, 0x7d, 0x85, 0x2d, 0x96, 0x65, 0xe5, 0x3f, 0x72, 0x95, 0x24, 0x9f, 0x28,
  74. 0x68, 0xca, 0x4f, 0xdb, 0x44, 0x1c, 0x3e, 0x60, 0x12, 0x8a, 0xdd, 0x26, 0xa5, 0xeb, 0xff, 0x0b,
  75. 0x5e, 0xd4, 0x88, 0x38, 0x49, 0x2a, 0x6e, 0x5b, 0xbf, 0x12, 0x37, 0x47, 0xbd, 0x05, 0x6b, 0xbc,
  76. 0xdb, 0xf3, 0xee, 0xe4, 0x11, 0x8e, 0x41, 0x68, 0x7c, 0x61, 0x13, 0xd7, 0x42, 0xc8, 0x80, 0xbe,
  77. 0x36, 0x8f, 0xdc, 0x08, 0x8b, 0x4f, 0xac, 0xa4, 0xe2, 0x76, 0x0c, 0xc9, 0x63, 0x6c, 0x49, 0x58,
  78. 0x93, 0xed, 0xcc, 0xaa, 0xdc, 0x25, 0x3b, 0x0a, 0x60, 0x3f, 0x8b, 0x54, 0x3a, 0xc3, 0x4d, 0x31,
  79. 0xe7, 0x94, 0xa4, 0x44, 0xfd, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x81, 0x81, 0x00, 0xc8, 0x62,
  80. 0xb9, 0xea, 0xde, 0x44, 0x53, 0x1d, 0x56, 0x97, 0xd9, 0x97, 0x9e, 0x1a, 0xcf, 0x30, 0x1e, 0x0a,
  81. 0x88, 0x45, 0x86, 0x29, 0x30, 0xa3, 0x4d, 0x9f, 0x61, 0x65, 0x73, 0xe0, 0xd6, 0x87, 0x8f, 0xb6,
  82. 0xf3, 0x06, 0xa3, 0x82, 0xdc, 0x7c, 0xac, 0xfe, 0x9b, 0x28, 0x9a, 0xae, 0xfd, 0xfb, 0xfe, 0x2f,
  83. 0x0e, 0xd8, 0x97, 0x04, 0xe3, 0xbb, 0x1f, 0xd1, 0xec, 0x0d, 0xba, 0xa3, 0x49, 0x7f, 0x47, 0xac,
  84. 0x8a, 0x44, 0x04, 0x7e, 0x86, 0xb7, 0x39, 0x42, 0x3f, 0xad, 0x1e, 0xb7, 0x0e, 0xa5, 0x51, 0xf4,
  85. 0x40, 0x63, 0x1e, 0xfd, 0xbd, 0xea, 0x9f, 0x41, 0x9f, 0xa8, 0x90, 0x1d, 0x6f, 0x0a, 0x5a, 0x95,
  86. 0x13, 0x11, 0x0d, 0x80, 0xaf, 0x5f, 0x64, 0x98, 0x8a, 0x2c, 0x78, 0x68, 0x65, 0xb0, 0x2b, 0x8b,
  87. 0xa2, 0x53, 0x87, 0xca, 0xf1, 0x64, 0x04, 0xab, 0xf2, 0x7b, 0xdb, 0x83, 0xc8, 0x81, 0x02, 0x41,
  88. 0x00, 0xf7, 0xbe, 0x5e, 0x23, 0xc3, 0x32, 0x3f, 0xbf, 0x8b, 0x8e, 0x3a, 0xee, 0xfc, 0xfc, 0xcb,
  89. 0xe5, 0xf7, 0xf1, 0x0b, 0xbc, 0x42, 0x82, 0xae, 0xd5, 0x7a, 0x3e, 0xca, 0xf7, 0xd5, 0x69, 0x3f,
  90. 0x64, 0x25, 0xa2, 0x1f, 0xb7, 0x75, 0x75, 0x05, 0x92, 0x42, 0xeb, 0xb8, 0xf1, 0xf3, 0x0a, 0x05,
  91. 0xe3, 0x94, 0xd1, 0x55, 0x78, 0x35, 0xa0, 0x36, 0xa0, 0x9b, 0x7c, 0x92, 0x84, 0x6c, 0xdd, 0xdc,
  92. 0x4d, 0x02, 0x41, 0x00, 0xd6, 0x86, 0x0e, 0x85, 0x42, 0x0b, 0x04, 0x08, 0x84, 0x21, 0x60, 0xf0,
  93. 0x0e, 0x0d, 0x88, 0xfd, 0x1e, 0x36, 0x10, 0x65, 0x4f, 0x1e, 0x53, 0xb4, 0x08, 0x72, 0x80, 0x5c,
  94. 0x3f, 0x59, 0x66, 0x17, 0xe6, 0x98, 0xf2, 0xe9, 0x6c, 0x7a, 0x06, 0x4c, 0xac, 0x76, 0x3d, 0xed,
  95. 0x8c, 0xa1, 0xce, 0xad, 0x1b, 0xbd, 0xb4, 0x7d, 0x28, 0xbc, 0xe3, 0x0e, 0x38, 0x8d, 0x99, 0xd8,
  96. 0x05, 0xb5, 0xa3, 0x71, 0x02, 0x40, 0x6d, 0xeb, 0xc3, 0x2d, 0x2e, 0xf0, 0x5e, 0xa4, 0x88, 0x31,
  97. 0x05, 0x29, 0x00, 0x8a, 0xd1, 0x95, 0x29, 0x9b, 0x83, 0xcf, 0x75, 0xdb, 0x31, 0xe3, 0x7a, 0x27,
  98. 0xde, 0x3a, 0x74, 0x30, 0x0c, 0x76, 0x4c, 0xd4, 0x50, 0x2a, 0x40, 0x2d, 0x39, 0xd9, 0x99, 0x63,
  99. 0xa9, 0x5d, 0x80, 0xae, 0x53, 0xca, 0x94, 0x3f, 0x05, 0x23, 0x1e, 0xf8, 0x05, 0x04, 0xe1, 0xb8,
  100. 0x35, 0xf2, 0x17, 0xb3, 0xa0, 0x89, 0x02, 0x41, 0x00, 0xab, 0x90, 0x88, 0xfa, 0x60, 0x08, 0x29,
  101. 0x50, 0x9a, 0x43, 0x8b, 0xa0, 0x50, 0xcc, 0xd8, 0x5a, 0xfe, 0x97, 0x64, 0x63, 0x71, 0x74, 0x22,
  102. 0xa3, 0x20, 0x02, 0x5a, 0xcf, 0xeb, 0xc6, 0x16, 0x95, 0x54, 0xd1, 0xcb, 0xab, 0x8d, 0x1a, 0xc6,
  103. 0x00, 0xfa, 0x08, 0x92, 0x9c, 0x71, 0xd5, 0x52, 0x52, 0x35, 0x96, 0x71, 0x4b, 0x8b, 0x92, 0x0c,
  104. 0xd0, 0xe9, 0xbf, 0xad, 0x63, 0x0b, 0xa5, 0xe9, 0xb1, 0x02, 0x41, 0x00, 0xdc, 0xcc, 0x27, 0xc8,
  105. 0xe4, 0xdc, 0x62, 0x48, 0xd5, 0x9b, 0xaf, 0xf5, 0xab, 0x60, 0xf6, 0x21, 0xfd, 0x53, 0xe2, 0xb7,
  106. 0x5d, 0x09, 0xc9, 0x1a, 0xa1, 0x04, 0xa9, 0xfc, 0x61, 0x2c, 0x5d, 0x04, 0x58, 0x3a, 0x5a, 0x39,
  107. 0xf1, 0x4a, 0x21, 0x56, 0x67, 0xfd, 0xcc, 0x20, 0xa3, 0x8f, 0x78, 0x18, 0x5a, 0x79, 0x3d, 0x2e,
  108. 0x8e, 0x7e, 0x86, 0x0a, 0xe6, 0xa8, 0x33, 0xc1, 0x04, 0x17, 0x4a, 0x9f };
  109. /* private key - hexadecimal */
  110. enum {
  111. pk_d ,
  112. pk_dP,
  113. pk_dQ,
  114. pk_e ,
  115. pk_N ,
  116. pk_p ,
  117. pk_q ,
  118. pk_qP,
  119. };
  120. static const char *hex_key[] = {
  121. "C862B9EADE44531D5697D9979E1ACF301E0A8845862930A34D9F616573E0D6878FB6F306A382DC7CACFE9B289AAEFDFBFE2F0ED89704E3BB1FD1EC0DBAA3497F47AC8A44047E86B739423FAD1EB70EA551F440631EFDBDEA9F419FA8901D6F0A5A9513110D80AF5F64988A2C786865B02B8BA25387CAF16404ABF27BDB83C881",
  122. "6DEBC32D2EF05EA488310529008AD195299B83CF75DB31E37A27DE3A74300C764CD4502A402D39D99963A95D80AE53CA943F05231EF80504E1B835F217B3A089",
  123. "AB9088FA600829509A438BA050CCD85AFE976463717422A320025ACFEBC6169554D1CBAB8D1AC600FA08929C71D552523596714B8B920CD0E9BFAD630BA5E9B1",
  124. "010001",
  125. "CF9ADE648ADAC83320A9D783311954B29A85A7A1B77533B6A9AC8424B3DEDB7D852D9665E53F7295249F2868CA4FDB441C3E60128ADD26A5EBFF0B5ED48838492A6E5BBF123747BD056BBCDBF3EEE4118E41687C6113D742C880BE368FDC088B4FACA4E2760CC9636C495893EDCCAADC253B0A603F8B543AC34D31E794A444FD",
  126. "F7BE5E23C3323FBF8B8E3AEEFCFCCBE5F7F10BBC4282AED57A3ECAF7D5693F6425A21FB77575059242EBB8F1F30A05E394D1557835A036A09B7C92846CDDDC4D",
  127. "D6860E85420B0408842160F00E0D88FD1E3610654F1E53B40872805C3F596617E698F2E96C7A064CAC763DED8CA1CEAD1BBDB47D28BCE30E388D99D805B5A371",
  128. "DCCC27C8E4DC6248D59BAFF5AB60F621FD53E2B75D09C91AA104A9FC612C5D04583A5A39F14A215667FDCC20A38F78185A793D2E8E7E860AE6A833C104174A9F" };
  129. /*** openssl public RSA key in DER format */
  130. const unsigned char ltc_openssl_public_rsa[] = {
  131. 0x30, 0x81, 0x9f, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  132. 0x05, 0x00, 0x03, 0x81, 0x8d, 0x00, 0x30, 0x81, 0x89, 0x02, 0x81, 0x81, 0x00, 0xcf, 0x9a, 0xde,
  133. 0x64, 0x8a, 0xda, 0xc8, 0x33, 0x20, 0xa9, 0xd7, 0x83, 0x31, 0x19, 0x54, 0xb2, 0x9a, 0x85, 0xa7,
  134. 0xa1, 0xb7, 0x75, 0x33, 0xb6, 0xa9, 0xac, 0x84, 0x24, 0xb3, 0xde, 0xdb, 0x7d, 0x85, 0x2d, 0x96,
  135. 0x65, 0xe5, 0x3f, 0x72, 0x95, 0x24, 0x9f, 0x28, 0x68, 0xca, 0x4f, 0xdb, 0x44, 0x1c, 0x3e, 0x60,
  136. 0x12, 0x8a, 0xdd, 0x26, 0xa5, 0xeb, 0xff, 0x0b, 0x5e, 0xd4, 0x88, 0x38, 0x49, 0x2a, 0x6e, 0x5b,
  137. 0xbf, 0x12, 0x37, 0x47, 0xbd, 0x05, 0x6b, 0xbc, 0xdb, 0xf3, 0xee, 0xe4, 0x11, 0x8e, 0x41, 0x68,
  138. 0x7c, 0x61, 0x13, 0xd7, 0x42, 0xc8, 0x80, 0xbe, 0x36, 0x8f, 0xdc, 0x08, 0x8b, 0x4f, 0xac, 0xa4,
  139. 0xe2, 0x76, 0x0c, 0xc9, 0x63, 0x6c, 0x49, 0x58, 0x93, 0xed, 0xcc, 0xaa, 0xdc, 0x25, 0x3b, 0x0a,
  140. 0x60, 0x3f, 0x8b, 0x54, 0x3a, 0xc3, 0x4d, 0x31, 0xe7, 0x94, 0xa4, 0x44, 0xfd, 0x02, 0x03, 0x01,
  141. 0x00, 0x01, };
  142. const unsigned long ltc_openssl_public_rsa_sz = sizeof(ltc_openssl_public_rsa);
  143. /* same key but with extra headers stripped */
  144. static const unsigned char openssl_public_rsa_stripped[] = {
  145. 0x30, 0x81, 0x89, 0x02, 0x81, 0x81, 0x00, 0xcf, 0x9a, 0xde,
  146. 0x64, 0x8a, 0xda, 0xc8, 0x33, 0x20, 0xa9, 0xd7, 0x83, 0x31, 0x19, 0x54, 0xb2, 0x9a, 0x85, 0xa7,
  147. 0xa1, 0xb7, 0x75, 0x33, 0xb6, 0xa9, 0xac, 0x84, 0x24, 0xb3, 0xde, 0xdb, 0x7d, 0x85, 0x2d, 0x96,
  148. 0x65, 0xe5, 0x3f, 0x72, 0x95, 0x24, 0x9f, 0x28, 0x68, 0xca, 0x4f, 0xdb, 0x44, 0x1c, 0x3e, 0x60,
  149. 0x12, 0x8a, 0xdd, 0x26, 0xa5, 0xeb, 0xff, 0x0b, 0x5e, 0xd4, 0x88, 0x38, 0x49, 0x2a, 0x6e, 0x5b,
  150. 0xbf, 0x12, 0x37, 0x47, 0xbd, 0x05, 0x6b, 0xbc, 0xdb, 0xf3, 0xee, 0xe4, 0x11, 0x8e, 0x41, 0x68,
  151. 0x7c, 0x61, 0x13, 0xd7, 0x42, 0xc8, 0x80, 0xbe, 0x36, 0x8f, 0xdc, 0x08, 0x8b, 0x4f, 0xac, 0xa4,
  152. 0xe2, 0x76, 0x0c, 0xc9, 0x63, 0x6c, 0x49, 0x58, 0x93, 0xed, 0xcc, 0xaa, 0xdc, 0x25, 0x3b, 0x0a,
  153. 0x60, 0x3f, 0x8b, 0x54, 0x3a, 0xc3, 0x4d, 0x31, 0xe7, 0x94, 0xa4, 0x44, 0xfd, 0x02, 0x03, 0x01,
  154. 0x00, 0x01, };
  155. /* generated with the private key above as:
  156. echo -n 'test' | openssl rsautl -sign -inkey rsa_private.pem -pkcs -hexdump
  157. */
  158. static const unsigned char openssl_rsautl_pkcs[] = {
  159. 0x24, 0xef, 0x54, 0xea, 0x1a, 0x12, 0x0c, 0xf4, 0x04, 0x0c, 0x48, 0xc8, 0xe8, 0x17, 0xd2, 0x6f,
  160. 0xc3, 0x41, 0xb3, 0x97, 0x5c, 0xbc, 0xa3, 0x2d, 0x21, 0x00, 0x10, 0x0e, 0xbb, 0xf7, 0x30, 0x21,
  161. 0x7e, 0x12, 0xd2, 0xdf, 0x26, 0x28, 0xd8, 0x0f, 0x6d, 0x4d, 0xc8, 0x4d, 0xa8, 0x78, 0xe7, 0x03,
  162. 0xee, 0xbc, 0x68, 0xba, 0x98, 0xea, 0xe9, 0xb6, 0x06, 0x8d, 0x85, 0x5b, 0xdb, 0xa6, 0x49, 0x86,
  163. 0x6f, 0xc7, 0x3d, 0xe0, 0x53, 0x83, 0xe0, 0xea, 0xb1, 0x08, 0x6a, 0x7b, 0xbd, 0xeb, 0xb5, 0x4a,
  164. 0xdd, 0xbc, 0x64, 0x97, 0x8c, 0x17, 0x20, 0xa3, 0x5c, 0xd4, 0xb8, 0x87, 0x43, 0xc5, 0x13, 0xad,
  165. 0x41, 0x6e, 0x45, 0x41, 0x32, 0xd4, 0x09, 0x12, 0x7f, 0xdc, 0x59, 0x1f, 0x28, 0x3f, 0x1e, 0xbc,
  166. 0xef, 0x57, 0x23, 0x4b, 0x3a, 0xa3, 0x24, 0x91, 0x4d, 0xfb, 0xb2, 0xd4, 0xe7, 0x5e, 0x41, 0x7e,
  167. };
  168. extern const char ltc_der_tests_cacert_root_cert[];
  169. extern const unsigned long ltc_der_tests_cacert_root_cert_size;
  170. static int rsa_compat_test(void)
  171. {
  172. rsa_key key, pubkey;
  173. int stat, i;
  174. unsigned char buf[1024], key_parts[8][128];
  175. unsigned long len, key_lens[8];
  176. ltc_rsa_op_parameters rsa_params = {0};
  177. /* try reading the key */
  178. DO(rsa_import(ltc_rsa_private_test_key, sizeof(ltc_rsa_private_test_key), &key));
  179. DO(rsa_import(ltc_openssl_public_rsa, sizeof(ltc_openssl_public_rsa), &pubkey));
  180. /* sign-verify a message with PKCS #1 v1.5 no ASN.1 */
  181. len = sizeof(buf);
  182. rsa_params.padding = LTC_PKCS_1_V1_5_NA1;
  183. DO(rsa_sign_hash_v2((unsigned char*)"test", 4, buf, &len, &rsa_params, &key));
  184. if (len != sizeof(openssl_rsautl_pkcs) || memcmp(buf, openssl_rsautl_pkcs, len)) {
  185. fprintf(stderr, "RSA rsa_sign_hash_v2 + LTC_PKCS_1_V1_5_NA1 failed\n");
  186. return 1;
  187. }
  188. stat = 0;
  189. DO(rsa_verify_hash_v2(openssl_rsautl_pkcs, sizeof(openssl_rsautl_pkcs), (unsigned char*)"test", 4, &rsa_params, &stat, &pubkey));
  190. if (stat != 1) {
  191. fprintf(stderr, "RSA rsa_verify_hash_v2 + LTC_PKCS_1_V1_5_NA1 failed\n");
  192. return 1;
  193. }
  194. rsa_free(&pubkey);
  195. /* now try to export private/public and compare */
  196. len = sizeof(buf);
  197. DO(rsa_export(buf, &len, PK_PRIVATE, &key));
  198. COMPARE_TESTVECTOR(buf, len, ltc_rsa_private_test_key, sizeof(ltc_rsa_private_test_key), "RSA private export (from OpenSSL)", 0);
  199. len = sizeof(buf);
  200. DO(rsa_export(buf, &len, PK_PUBLIC, &key));
  201. COMPARE_TESTVECTOR(buf, len, openssl_public_rsa_stripped, sizeof(openssl_public_rsa_stripped), "RSA public export (from OpenSSL private key)", 0);
  202. rsa_free(&key);
  203. /* try reading the public key */
  204. DO(rsa_import(openssl_public_rsa_stripped, sizeof(openssl_public_rsa_stripped), &key));
  205. len = sizeof(buf);
  206. DO(rsa_export(buf, &len, PK_PUBLIC, &key));
  207. COMPARE_TESTVECTOR(buf, len, openssl_public_rsa_stripped, sizeof(openssl_public_rsa_stripped), "RSA public export (from stripped OpenSSL)", 0);
  208. rsa_free(&key);
  209. /* try reading the public key */
  210. DO(rsa_import(ltc_openssl_public_rsa, sizeof(ltc_openssl_public_rsa), &key));
  211. len = sizeof(buf);
  212. DO(rsa_export(buf, &len, PK_PUBLIC, &key));
  213. COMPARE_TESTVECTOR(buf, len, openssl_public_rsa_stripped, sizeof(openssl_public_rsa_stripped), "RSA public export (from OpenSSL)", 0);
  214. rsa_free(&key);
  215. /* try import private key in pkcs8 format */
  216. DO(rsa_import_pkcs8(pkcs8_private_rsa, sizeof(pkcs8_private_rsa), NULL, &key));
  217. len = sizeof(buf);
  218. DO(rsa_export(buf, &len, PK_PRIVATE, &key));
  219. COMPARE_TESTVECTOR(buf, len, ltc_rsa_private_test_key, sizeof(ltc_rsa_private_test_key), "RSA private export (from PKCS#8)", 0);
  220. rsa_free(&key);
  221. /* convert raw hexadecimal numbers to binary */
  222. for (i = 0; i < 8; ++i) {
  223. key_lens[i] = sizeof(key_parts[i]);
  224. DO(radix_to_bin(hex_key[i], 16, key_parts[i], &key_lens[i]));
  225. }
  226. /* try import private key from converted raw hexadecimal numbers */
  227. DO(rsa_set_key(key_parts[pk_N], key_lens[pk_N], key_parts[pk_e], key_lens[pk_e], key_parts[pk_d], key_lens[pk_d], &key));
  228. DO(rsa_set_factors(key_parts[pk_p], key_lens[pk_p], key_parts[pk_q], key_lens[pk_q], &key));
  229. DO(rsa_set_crt_params(key_parts[pk_dP], key_lens[pk_dP], key_parts[pk_dQ], key_lens[pk_dQ], key_parts[pk_qP], key_lens[pk_qP], &key));
  230. len = sizeof(buf);
  231. DO(rsa_export(buf, &len, PK_PRIVATE, &key));
  232. COMPARE_TESTVECTOR(buf, len, ltc_rsa_private_test_key, sizeof(ltc_rsa_private_test_key), "RSA private export (from hex)", 0);
  233. rsa_free(&key);
  234. /* try import public key from converted raw hexadecimal numbers */
  235. DO(rsa_set_key(key_parts[pk_N], key_lens[pk_N], key_parts[pk_e], key_lens[pk_e], NULL, 0, &key));
  236. len = sizeof(buf);
  237. DO(rsa_export(buf, &len, PK_PUBLIC, &key));
  238. COMPARE_TESTVECTOR(buf, len, openssl_public_rsa_stripped, sizeof(openssl_public_rsa_stripped), "RSA public export (from hex)", 0);
  239. rsa_free(&key);
  240. /* try export in SubjectPublicKeyInfo format of the public key */
  241. DO(rsa_import(ltc_openssl_public_rsa, sizeof(ltc_openssl_public_rsa), &key));
  242. len = sizeof(buf);
  243. DO(rsa_export(buf, &len, PK_PUBLIC | PK_STD, &key));
  244. COMPARE_TESTVECTOR(buf, len, ltc_openssl_public_rsa, sizeof(ltc_openssl_public_rsa), "RSA public export (X.509)", 0);
  245. rsa_free(&key);
  246. return 0;
  247. }
  248. int rsa_key_cmp(const int should_type, const rsa_key *should, const rsa_key *is)
  249. {
  250. if(should_type != is->type)
  251. return CRYPT_ERROR;
  252. if(should_type == PK_PRIVATE) {
  253. if(ltc_mp_cmp(should->q, is->q) != LTC_MP_EQ)
  254. return CRYPT_ERROR;
  255. if(ltc_mp_cmp(should->p, is->p) != LTC_MP_EQ)
  256. return CRYPT_ERROR;
  257. if(ltc_mp_cmp(should->qP, is->qP) != LTC_MP_EQ)
  258. return CRYPT_ERROR;
  259. if(ltc_mp_cmp(should->dP, is->dP) != LTC_MP_EQ)
  260. return CRYPT_ERROR;
  261. if(ltc_mp_cmp(should->dQ, is->dQ) != LTC_MP_EQ)
  262. return CRYPT_ERROR;
  263. if(ltc_mp_cmp(should->d, is->d) != LTC_MP_EQ)
  264. return CRYPT_ERROR;
  265. }
  266. if(ltc_mp_cmp(should->N, is->N) != LTC_MP_EQ)
  267. return CRYPT_ERROR;
  268. if(ltc_mp_cmp(should->e, is->e) != LTC_MP_EQ)
  269. return CRYPT_ERROR;
  270. return CRYPT_OK;
  271. }
  272. /* https://github.com/DCIT/perl-CryptX/issues/69 */
  273. static int s_rsa_cryptx_issue_69(void)
  274. {
  275. static const char *e = "03";
  276. static const char *N = "E932AC92252F585B3A80A4DD76A897C8B7652952FE788F6EC8DD640587A1EE5647670A8AD4C2BE0F9FA6E49C605ADF77B5174230"
  277. "AF7BD50E5D6D6D6D28CCF0A886A514CC72E51D209CC772A52EF419F6A953F3135929588EBE9B351FCA61CED78F346FE00DBB6306"
  278. "E5C2A4C6DFC3779AF85AB417371CF34D8387B9B30AE46D7A5FF5A655B8D8455F1B94AE736989D60A6F2FD5CADBFFBD504C5A756A"
  279. "2E6BB5CECC13BCA7503F6DF8B52ACE5C410997E98809DB4DC30D943DE4E812A47553DCE54844A78E36401D13F77DC650619FED88"
  280. "D8B3926E3D8E319C80C744779AC5D6ABE252896950917476ECE5E8FC27D5F053D6018D91B502C4787558A002B9283DA7";
  281. static const char *sig1 = "8df69d774c6ac8b5f8aa16576ca37a4f948706c5daecb3c15cfd247a7657616b2bbb786b50158cac8c23e3"
  282. "289d300d3fbb82380b8746d929df36bdaf43a5fc5d1d04c61c98d47c22de02d051be3ba9e42b1c47aa5192"
  283. "66d4cae244e5ce99b24771a13a7c8c7b08868a3eccf70b4bc7570d5131a1ac8943d91b0151c39da2ad75cd"
  284. "1b9a697d100eef6747217df581b272cfd1f549a901ff4951036a4eb28fd2ea1e9df3fa9fa457663f4259be"
  285. "8e5f2f2fb84f831a0ca5320e2b79f04a17830f43062c4c8fc0d0b1ff90567f3342d524f682ca26661caadf"
  286. "4272f2585e6013a92bfa68de72fe6174096890e4296aedd72da43aa508007df53fb852bd7162ab635b";
  287. static const char *sig2 = "1ee08947536e6b11d8923c3b00061d26a6933b5345077ea0214fdcbcc1ad68395008ff709117047e6b01dd"
  288. "2a371dfa032c0732abc86ab2e0273bbd0dfe6b1c769e21bb9079982801d8f72e01be3244959312ab09bb8f"
  289. "88572dc23216719b9810c73edf826749604feb8da1345f83f0209271aca462c1235b4cb4ba538f85a9c03d"
  290. "d1dde1856fe73fd86b95566df2dfe8b0895c34489b97e02c8e48dabad7067619edec6267a776fa416fbcac"
  291. "0fcacf3efa7852ce33ed63a9149c685c303d98c3dc37ee87521bc5b130377345fc95c87aa48505470deaf6"
  292. "fb1064df041e3f03322b1ec90d3608deb17bf77f47066ecc6c511bfba69eed6da42881dcce603fcb2a";
  293. static const char *hash = "7509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9";
  294. rsa_key key;
  295. unsigned char buf0[512], buf1[512];
  296. unsigned long l0, l1;
  297. int stat;
  298. ltc_rsa_op_parameters rsa_params = {0};
  299. l0 = sizeof(buf0);
  300. l1 = sizeof(buf1);
  301. DO(radix_to_bin(e, 16, buf0, &l0));
  302. DO(radix_to_bin(N, 16, buf1, &l1));
  303. DO(rsa_set_key(buf1, l1, buf0, l0, NULL, 0, &key));
  304. l0 = sizeof(buf0);
  305. l1 = sizeof(buf1);
  306. DO(radix_to_bin(sig1, 16, buf0, &l0));
  307. DO(radix_to_bin(hash, 16, buf1, &l1));
  308. rsa_params.padding = LTC_PKCS_1_V1_5;
  309. SHOULD_FAIL(rsa_verify_hash_v2(buf0, l0, buf1, l1, &rsa_params, &stat, &key));
  310. DO(radix_to_bin(sig2, 16, buf0, &l0));
  311. SHOULD_FAIL(rsa_verify_hash_v2(buf0, l0, buf1, l1, &rsa_params, &stat, &key));
  312. rsa_free(&key);
  313. return CRYPT_OK;
  314. }
  315. static int s_rsa_issue_301(int prng_idx)
  316. {
  317. rsa_key key, key_in;
  318. unsigned char buf[4096];
  319. unsigned long len;
  320. DO(rsa_make_key(&yarrow_prng, prng_idx, sizeof(buf)/8, 65537, &key));
  321. len = sizeof(buf);
  322. DO(rsa_export(buf, &len, PK_PRIVATE, &key));
  323. DO(rsa_import(buf, len, &key_in));
  324. DO(rsa_key_cmp(PK_PRIVATE, &key, &key_in));
  325. rsa_free(&key_in);
  326. len = sizeof(buf);
  327. DO(rsa_export(buf, &len, PK_PUBLIC, &key));
  328. DO(rsa_import(buf, len, &key_in));
  329. DO(rsa_key_cmp(PK_PUBLIC, &key, &key_in));
  330. rsa_free(&key_in);
  331. len = sizeof(buf);
  332. DO(rsa_export(buf, &len, PK_PUBLIC | PK_STD, &key));
  333. DO(rsa_import(buf, len, &key_in));
  334. DO(rsa_key_cmp(PK_PUBLIC, &key, &key_in));
  335. rsa_free(&key_in);
  336. rsa_free(&key);
  337. return CRYPT_OK;
  338. }
  339. static int s_rsa_public_ubin_e(int prng_idx)
  340. {
  341. rsa_key key;
  342. unsigned char e[32] = {0};
  343. unsigned long elen = sizeof(e);
  344. /* Check public exponent too small */
  345. e[elen - 1] = 1;
  346. SHOULD_FAIL_WITH(rsa_make_key_ubin_e(&yarrow_prng, prng_idx, 128, e, elen, &key),
  347. CRYPT_INVALID_ARG);
  348. /*
  349. * Generate about 256 bits to check error when public exponent
  350. * overflow.
  351. */
  352. ENSURE(yarrow_read(e, elen, &yarrow_prng) == elen);
  353. /* Ensure that public exponent is:
  354. * - odd value
  355. * - MSB is even
  356. */
  357. e[elen - 1] |= 0x1;
  358. e[0] &= ~0x1;
  359. /* Check public exponent overflow */
  360. /* Set high bit of MSB set to get 256 bits, to get e overflow */
  361. e[0] |= 0x80;
  362. SHOULD_FAIL_WITH(rsa_make_key_ubin_e(&yarrow_prng, prng_idx, 128, e, elen, &key),
  363. CRYPT_INVALID_ARG);
  364. /* Check public exponent not odd but e value < 256 bits */
  365. e[elen - 1] &= ~0x1;
  366. e[0] &= ~0x80;
  367. SHOULD_FAIL_WITH(rsa_make_key_ubin_e(&yarrow_prng, prng_idx, 128, e, elen, &key),
  368. CRYPT_INVALID_ARG);
  369. /* Ensure that public exponent is odd value and e value < 256 bits */
  370. e[elen - 1] |= 0x1;
  371. DO(rsa_make_key_ubin_e(&yarrow_prng, prng_idx, 128, e, elen, &key));
  372. rsa_free(&key);
  373. return CRYPT_OK;
  374. }
  375. #ifdef LTC_TEST_READDIR
  376. static int s_rsa_import_x509(const void *in, unsigned long inlen, void *key)
  377. {
  378. /* here we use the filesize as indicator for the rsa size
  379. * that would fail to import for tfm because it's fixed-size
  380. */
  381. if ((strcmp(ltc_mp.name, "TomsFastMath") == 0) && (inlen > 2048)) {
  382. #if defined(LTC_TEST_DBG) && LTC_TEST_DBG > 1
  383. fprintf(stderr, "Skipping testcase because of TomsFastMath\n");
  384. #endif
  385. return CRYPT_NOP;
  386. }
  387. return rsa_import_x509(in, inlen, key);
  388. }
  389. #if defined(LTC_TEST_READDIR)
  390. static int password_get(void **p, unsigned long *l, void *u)
  391. {
  392. LTC_UNUSED_PARAM(u);
  393. *p = strdup("secret");
  394. *l = 6;
  395. return 0;
  396. }
  397. static int s_rsa_import_pkcs8(const void *in, unsigned long inlen, void *key)
  398. {
  399. password_ctx pw_ctx = { .callback = password_get };
  400. return rsa_import_pkcs8(in, inlen, &pw_ctx, key);
  401. }
  402. #endif
  403. #endif
  404. static int s_rsa_macros_test(void)
  405. {
  406. rsa_key key, pubkey;
  407. int stat;
  408. const unsigned char tv[] = "test";
  409. unsigned char buf0[1024], buf1[1024];
  410. unsigned long buf0len, buf1len;
  411. DO(rsa_import(ltc_rsa_private_test_key, sizeof(ltc_rsa_private_test_key), &key));
  412. DO(rsa_import(ltc_openssl_public_rsa, sizeof(ltc_openssl_public_rsa), &pubkey));
  413. buf0len = sizeof(buf0);
  414. DO(ltc_rsa_sign_hash(tv, 4, buf0, &buf0len, &yarrow_prng, find_prng("yarrow"), find_hash("sha1"), 8, &key));
  415. buf1len = sizeof(buf1);
  416. DO(ltc_rsa_verify_hash(buf0, buf0len, tv, 4, find_hash("sha1"), 8, &stat, &key));
  417. ENSURE(stat == 1);
  418. buf0len = sizeof(buf0);
  419. DO(ltc_rsa_encrypt_key(tv, 4, buf0, &buf0len, NULL, 0, &yarrow_prng, find_prng("yarrow"), find_hash("sha1"), &pubkey));
  420. buf1len = sizeof(buf1);
  421. DO(ltc_rsa_decrypt_key(buf0, buf0len, buf1, &buf1len, NULL, 0, find_hash("sha1"), &stat, &key));
  422. ENSURE(stat == 1);
  423. COMPARE_TESTVECTOR(buf1, buf1len, tv, 4, "s_rsa_macros_test", 0);
  424. rsa_free(&pubkey);
  425. rsa_free(&key);
  426. return CRYPT_OK;
  427. }
  428. static int s_rsa_pss_test(void)
  429. {
  430. rsa_key key;
  431. const unsigned char tv[] = "test";
  432. unsigned char buf0[1024];
  433. unsigned long buf0len;
  434. ltc_rsa_op_parameters rsa_oparams = {
  435. .prng = &yarrow_prng,
  436. .wprng = find_prng("yarrow"),
  437. .padding = LTC_PKCS_1_OAEP,
  438. .u.crypt.lparam = tv,
  439. .u.crypt.lparamlen = (unsigned long)4,
  440. .params.hash_alg = "sha1",
  441. .params.mgf1_hash_alg = "sha256",
  442. .params.saltlen = 7,
  443. };
  444. DO(rsa_import(ltc_rsa_private_test_key, sizeof(ltc_rsa_private_test_key), &key));
  445. buf0len = sizeof(buf0);
  446. DO(rsa_encrypt_key_v2(tv, 4, buf0, &buf0len, &rsa_oparams, &key));
  447. DO(rsa_encrypt_key_v2(tv, 4, buf0, &buf0len, &rsa_oparams, &key));
  448. key.params = rsa_oparams.params;
  449. DO(rsa_encrypt_key_v2(tv, 4, buf0, &buf0len, &rsa_oparams, &key));
  450. /* If the key is a PSS key, we must do a PSS operation */
  451. rsa_oparams.padding = LTC_PKCS_1_V1_5;
  452. SHOULD_FAIL(rsa_encrypt_key_v2(tv, 4, buf0, &buf0len, &rsa_oparams, &key));
  453. rsa_free(&key);
  454. return CRYPT_OK;
  455. }
  456. int rsa_test(void)
  457. {
  458. unsigned char in[1024], out[1024], tmp[3072];
  459. rsa_key key, privKey, pubKey;
  460. int prng_idx, stat, stat2, i, mgf_hash;
  461. unsigned long rsa_msgsize, len, len2, len3, cnt, cnt2, max_msgsize, label_hash;
  462. static unsigned char lparam[] = { 0x01, 0x02, 0x03, 0x04 };
  463. void* dP;
  464. unsigned char* p;
  465. unsigned char* p2;
  466. unsigned char* p3;
  467. ltc_rsa_op_parameters rsa_params = {0};
  468. if (ltc_mp.name == NULL) return CRYPT_NOP;
  469. if (rsa_compat_test() != 0) {
  470. return 1;
  471. }
  472. rsa_params.params.hash_alg = "sha1";
  473. rsa_params.wprng = find_prng("yarrow");
  474. mgf_hash = find_hash(rsa_params.params.hash_alg);
  475. if (mgf_hash == -1 || rsa_params.wprng == -1) {
  476. fprintf(stderr, "rsa_test requires LTC_SHA1 and yarrow");
  477. return 1;
  478. }
  479. rsa_params.prng = &yarrow_prng;
  480. prng_idx = rsa_params.wprng;
  481. #ifdef LTC_TEST_READDIR
  482. DO(test_process_dir("tests/rsa", &key, s_rsa_import_x509, NULL, (dir_cleanup_cb)rsa_free, "rsa_test"));
  483. #if defined(LTC_MD2) && defined(LTC_MD5) && defined(LTC_RC2)
  484. DO(test_process_dir("tests/rsa-pkcs8/old", &key, s_rsa_import_pkcs8, NULL, (dir_cleanup_cb)rsa_free, "rsa_pkcs8_test"));
  485. #endif
  486. DO(test_process_dir("tests/rsa-pkcs8", &key, s_rsa_import_pkcs8, NULL, (dir_cleanup_cb)rsa_free, "rsa_pkcs8_test"));
  487. #endif
  488. DO(s_rsa_pss_test());
  489. DO(s_rsa_macros_test());
  490. DO(s_rsa_cryptx_issue_69());
  491. DO(s_rsa_issue_301(prng_idx));
  492. DO(s_rsa_public_ubin_e(prng_idx));
  493. /* make 10 random key */
  494. for (cnt = 0; cnt < 10; cnt++) {
  495. DO(rsa_make_key(&yarrow_prng, prng_idx, 1024/8, 65537, &key));
  496. if (ltc_mp_count_bits(key.N) != 1024) {
  497. fprintf(stderr, "rsa_1024 key modulus has %d bits\n", ltc_mp_count_bits(key.N));
  498. len = ltc_mp_unsigned_bin_size(key.N);
  499. ltc_mp_to_unsigned_bin(key.N, tmp);
  500. print_hex("N", tmp, len);
  501. len = ltc_mp_unsigned_bin_size(key.p);
  502. ltc_mp_to_unsigned_bin(key.p, tmp);
  503. print_hex("p", tmp, len);
  504. len = ltc_mp_unsigned_bin_size(key.q);
  505. ltc_mp_to_unsigned_bin(key.q, tmp);
  506. print_hex("q", tmp, len);
  507. return 1;
  508. }
  509. if (cnt != 9) {
  510. rsa_free(&key);
  511. }
  512. }
  513. /* make a random key/msg */
  514. ENSURE(yarrow_read(in, 117, &yarrow_prng) == 117);
  515. rsa_params.padding = LTC_PKCS_1_OAEP;
  516. #ifdef LTC_TEST_EXT
  517. for (mgf_hash = 0; mgf_hash < TAB_SIZE; ++mgf_hash) {
  518. if (hash_is_valid(mgf_hash) != CRYPT_OK)
  519. continue;
  520. #else
  521. {
  522. #endif
  523. for (label_hash = 0; label_hash < TAB_SIZE; ++label_hash) {
  524. if (hash_is_valid(label_hash) != CRYPT_OK)
  525. continue;
  526. if (2 * hash_descriptor[label_hash].hashsize > 126)
  527. continue;
  528. max_msgsize = 128 - (2 * hash_descriptor[label_hash].hashsize) - 2;
  529. rsa_params.params.hash_alg = hash_descriptor[label_hash].name;
  530. rsa_params.params.mgf1_hash_alg = hash_descriptor[mgf_hash].name;
  531. #if defined(LTC_TEST_DBG) && LTC_TEST_DBG > 1
  532. fprintf(stderr, "Test MGF(%s), Labelhash(%s) with max_msgsize %lu\n", hash_descriptor[mgf_hash].name, hash_descriptor[label_hash].name, max_msgsize);
  533. #endif
  534. /* encrypt the key (without lparam) */
  535. for (rsa_msgsize = 0; rsa_msgsize <= max_msgsize; rsa_msgsize++) {
  536. len = sizeof(out);
  537. len2 = rsa_msgsize;
  538. DO(rsa_encrypt_key_v2(in, rsa_msgsize, out, &len, &rsa_params, &key));
  539. /* change a byte */
  540. out[8] ^= 1;
  541. SHOULD_FAIL(rsa_decrypt_key_v2(out, len, tmp, &len2, &rsa_params, &stat2, &key));
  542. /* change a byte back */
  543. out[8] ^= 1;
  544. ENSURE(len2 == rsa_msgsize);
  545. len2 = rsa_msgsize;
  546. DO(rsa_decrypt_key_v2(out, len, tmp, &len2, &rsa_params, &stat, &key));
  547. ENSUREX(stat == 1 && stat2 == 0, "rsa_decrypt_key (without lparam)");
  548. COMPARE_TESTVECTOR(tmp, len2, in, rsa_msgsize, "rsa_decrypt_key (without lparam)", cnt << 8 | rsa_msgsize);
  549. }
  550. /* encrypt the key (with lparam) */
  551. rsa_params.u.crypt.lparam = lparam;
  552. rsa_params.u.crypt.lparamlen = sizeof(lparam);
  553. for (rsa_msgsize = 0; rsa_msgsize <= max_msgsize; rsa_msgsize++) {
  554. len = sizeof(out);
  555. len2 = rsa_msgsize;
  556. DO(rsa_encrypt_key_v2(rsa_msgsize ? in : NULL, rsa_msgsize, out, &len, &rsa_params, &key));
  557. /* change a byte */
  558. out[8] ^= 1;
  559. SHOULD_FAIL(rsa_decrypt_key_v2(out, len, tmp, &len2, &rsa_params, &stat2, &key));
  560. ENSURE(len2 == rsa_msgsize);
  561. /* change a byte back */
  562. out[8] ^= 1;
  563. len2 = rsa_msgsize;
  564. DO(rsa_decrypt_key_v2(out, len, tmp, &len2, &rsa_params, &stat, &key));
  565. ENSURE(stat == 1 && stat2 == 0);
  566. COMPARE_TESTVECTOR(tmp, len2, in, rsa_msgsize, "rsa_decrypt_key (with lparam)", rsa_msgsize);
  567. }
  568. rsa_params.u.crypt.lparam = NULL;
  569. rsa_params.u.crypt.lparamlen = 0;
  570. }
  571. }
  572. rsa_params.params.hash_alg = "sha1";
  573. rsa_params.params.mgf1_hash_alg = "sha1";
  574. /* encrypt the key PKCS #1 v1.5 (payload from 1 to 117 bytes) */
  575. rsa_params.padding = LTC_PKCS_1_V1_5;
  576. for (rsa_msgsize = 0; rsa_msgsize <= 117; rsa_msgsize++) {
  577. len = sizeof(out);
  578. len2 = rsa_msgsize;
  579. DO(rsa_encrypt_key_v2(in, rsa_msgsize, out, &len, &rsa_params, &key));
  580. len2 = rsa_msgsize;
  581. DO(rsa_decrypt_key_v2(out, len, tmp, &len2, &rsa_params, &stat, &key));
  582. ENSURE(stat == 1);
  583. COMPARE_TESTVECTOR(tmp, len2, in, rsa_msgsize, "rsa_decrypt_key_ex", rsa_msgsize);
  584. }
  585. /* sign a message (unsalted, lower cholestorol and Atkins approved) now */
  586. len = sizeof(out);
  587. rsa_params.padding = LTC_PKCS_1_PSS;
  588. DO(rsa_sign_hash_v2(in, 20, out, &len, &rsa_params, &key));
  589. /* export key and import as both private and public */
  590. len2 = sizeof(tmp);
  591. DO(rsa_export(tmp, &len2, PK_PRIVATE, &key));
  592. DO(rsa_import(tmp, len2, &privKey));
  593. len2 = sizeof(tmp);
  594. DO(rsa_export(tmp, &len2, PK_PUBLIC, &key));
  595. DO(rsa_import(tmp, len2, &pubKey));
  596. dbg_malloc_stats();
  597. rsa_shrink_key(&key);
  598. dbg_malloc_stats();
  599. rsa_shrink_key(&pubKey);
  600. dbg_malloc_stats();
  601. rsa_shrink_key(&privKey);
  602. dbg_malloc_stats();
  603. /* verify with original */
  604. DO(rsa_verify_hash_v2(out, len, in, 20, &rsa_params, &stat, &key));
  605. /* change a byte */
  606. in[0] ^= 1;
  607. DO(rsa_verify_hash_v2(out, len, in, 20, &rsa_params, &stat2, &key));
  608. ENSUREX(stat == 1 && stat2 == 0, "rsa_verify_hash (unsalted, origKey) failed");
  609. /* verify with privKey */
  610. /* change byte back to original */
  611. in[0] ^= 1;
  612. DO(rsa_verify_hash_v2(out, len, in, 20, &rsa_params, &stat, &privKey));
  613. /* change a byte */
  614. in[0] ^= 1;
  615. DO(rsa_verify_hash_v2(out, len, in, 20, &rsa_params, &stat2, &privKey));
  616. if (!(stat == 1 && stat2 == 0)) {
  617. fprintf(stderr, "rsa_verify_hash (unsalted, privKey) failed, %d, %d", stat, stat2);
  618. rsa_free(&key);
  619. rsa_free(&pubKey);
  620. rsa_free(&privKey);
  621. return 1;
  622. }
  623. /* verify with privKey but remove pointer to dP to test without CRT */
  624. dP = privKey.dP;
  625. privKey.dP = NULL;
  626. /* change byte back to original */
  627. in[0] ^= 1;
  628. DO(rsa_verify_hash_v2(out, len, in, 20, &rsa_params, &stat, &privKey));
  629. /* change a byte */
  630. in[0] ^= 1;
  631. DO(rsa_verify_hash_v2(out, len, in, 20, &rsa_params, &stat2, &privKey));
  632. if (!(stat == 1 && stat2 == 0)) {
  633. fprintf(stderr, "rsa_verify_hash (unsalted, privKey) failed, %d, %d", stat, stat2);
  634. rsa_free(&key);
  635. rsa_free(&pubKey);
  636. rsa_free(&privKey);
  637. return 1;
  638. }
  639. privKey.dP = dP;
  640. /* verify with pubKey */
  641. /* change byte back to original */
  642. in[0] ^= 1;
  643. DO(rsa_verify_hash_v2(out, len, in, 20, &rsa_params, &stat, &pubKey));
  644. /* change a byte */
  645. in[0] ^= 1;
  646. DO(rsa_verify_hash_v2(out, len, in, 20, &rsa_params, &stat2, &pubKey));
  647. if (!(stat == 1 && stat2 == 0)) {
  648. fprintf(stderr, "rsa_verify_hash (unsalted, pubkey) failed, %d, %d", stat, stat2);
  649. rsa_free(&key);
  650. rsa_free(&pubKey);
  651. rsa_free(&privKey);
  652. return 1;
  653. }
  654. /* sign a message (salted) now (use privKey to make, pubKey to verify) */
  655. len = sizeof(out);
  656. rsa_params.params.saltlen = 8;
  657. DO(rsa_sign_hash_v2(in, 20, out, &len, &rsa_params, &privKey));
  658. DO(rsa_verify_hash_v2(out, len, in, 20, &rsa_params, &stat, &pubKey));
  659. /* change a byte */
  660. in[0] ^= 1;
  661. DO(rsa_verify_hash_v2(out, len, in, 20, &rsa_params, &stat2, &pubKey));
  662. if (!(stat == 1 && stat2 == 0)) {
  663. fprintf(stderr, "rsa_verify_hash (salted) failed, %d, %d", stat, stat2);
  664. rsa_free(&key);
  665. rsa_free(&pubKey);
  666. rsa_free(&privKey);
  667. return 1;
  668. }
  669. rsa_params.params.saltlen = 0;
  670. /* sign a message with PKCS #1 v1.5 */
  671. len = sizeof(out);
  672. rsa_params.padding = LTC_PKCS_1_V1_5;
  673. DO(rsa_sign_hash_v2(in, 20, out, &len, &rsa_params, &privKey));
  674. DO(rsa_verify_hash_v2(out, len, in, 20, &rsa_params, &stat, &pubKey));
  675. /* change a byte */
  676. in[0] ^= 1;
  677. DO(rsa_verify_hash_v2(out, len, in, 20, &rsa_params, &stat2, &pubKey));
  678. if (!(stat == 1 && stat2 == 0)) {
  679. fprintf(stderr, "rsa_verify_hash_v2 failed, %d, %d", stat, stat2);
  680. rsa_free(&key);
  681. rsa_free(&pubKey);
  682. rsa_free(&privKey);
  683. return 1;
  684. }
  685. /* Testcase for Bleichenbacher attack
  686. *
  687. * (1) Create a valid signature
  688. * (2) Check that it can be verified
  689. * (3) Decrypt the package to fetch plain text
  690. * (4) Forge the structure of PKCS#1-EMSA encoded data
  691. * (4.1) Search for start and end of the padding string
  692. * (4.2) Move the signature to the front of the padding string
  693. * (4.3) Zero the message until the end
  694. * (5) Encrypt the package again
  695. * (6) Profit :)
  696. * For PS lengths < 8: the verification process should fail
  697. * For PS lengths >= 8: the verification process should succeed
  698. * For all PS lengths: the result should not be valid
  699. */
  700. p = in;
  701. p2 = out;
  702. p3 = tmp;
  703. for (i = 0; i < 10; ++i) {
  704. len = sizeof(in);
  705. len2 = sizeof(out);
  706. /* (1) */
  707. DO(rsa_sign_hash_v2(p, 20, p2, &len2, &rsa_params, &privKey));
  708. /* (2) */
  709. DOX(rsa_verify_hash_v2(p2, len2, p, 20, &rsa_params, &stat, &pubKey), "should succeed");
  710. ENSURE(stat == 1);
  711. len3 = sizeof(tmp);
  712. /* (3) */
  713. DO(ltc_mp.rsa_me(p2, len2, p3, &len3, PK_PUBLIC, &key));
  714. /* (4) */
  715. #if defined(LTC_TEST_DBG) && LTC_TEST_DBG > 1
  716. print_hex("Original signature", p3, len3);
  717. #endif
  718. /* (4.1) */
  719. for (cnt = 0; cnt < len3; ++cnt) {
  720. if (p3[cnt] == 0xff)
  721. break;
  722. }
  723. for (cnt2 = cnt+1; cnt2 < len3; ++cnt2) {
  724. if (p3[cnt2] != 0xff)
  725. break;
  726. }
  727. /* (4.2) */
  728. memmove(&p3[cnt+i], &p3[cnt2], len3-cnt2);
  729. /* (4.3) */
  730. for (cnt = cnt + len3-cnt2+i; cnt < len; ++cnt) {
  731. p3[cnt] = 0;
  732. }
  733. #if defined(LTC_TEST_DBG) && LTC_TEST_DBG > 1
  734. print_hex("Forged signature", p3, len3);
  735. #endif
  736. len2 = sizeof(out);
  737. /* (5) */
  738. DO(ltc_mp.rsa_me(p3, len3, p2, &len2, PK_PRIVATE, &key));
  739. len3 = sizeof(tmp);
  740. /* (6) */
  741. if (i < 8)
  742. SHOULD_FAIL(rsa_verify_hash_v2(p2, len2, p, 20, &rsa_params, &stat, &pubKey));
  743. else
  744. DO(rsa_verify_hash_v2(p2, len2, p, 20, &rsa_params, &stat, &pubKey));
  745. ENSURE(stat == 0);
  746. }
  747. rsa_free(&key);
  748. /* try reading the public RSA key from a X509 certificate */
  749. len3 = sizeof(tmp);
  750. DO(base64_decode(x509_public_rsa, sizeof(x509_public_rsa), tmp, &len3));
  751. DO(rsa_import_x509(tmp, len3, &key));
  752. len = sizeof(tmp);
  753. DO(rsa_export(tmp, &len, PK_PUBLIC, &key));
  754. COMPARE_TESTVECTOR(tmp, len, openssl_public_rsa_stripped, sizeof(openssl_public_rsa_stripped), "RSA public export to match rsa_import_x509", 0);
  755. rsa_free(&key);
  756. len3 = sizeof(tmp);
  757. DO(base64_decode(ltc_der_tests_cacert_root_cert, ltc_der_tests_cacert_root_cert_size, tmp, &len3));
  758. DO(rsa_import_x509(tmp, len3, &key));
  759. /* free the key and return */
  760. rsa_free(&key);
  761. rsa_free(&pubKey);
  762. rsa_free(&privKey);
  763. return 0;
  764. }
  765. #else
  766. int rsa_test(void)
  767. {
  768. return CRYPT_NOP;
  769. }
  770. #endif