rsa_test.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. #include <tomcrypt_test.h>
  2. #ifdef LTC_MRSA
  3. #define RSA_MSGSIZE 78
  4. /* These are test keys [see file test.key] that I use to test my import/export against */
  5. static const unsigned char openssl_private_rsa[] = {
  6. 0x30, 0x82, 0x02, 0x5e, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0xcf, 0x9a, 0xde, 0x64, 0x8a,
  7. 0xda, 0xc8, 0x33, 0x20, 0xa9, 0xd7, 0x83, 0x31, 0x19, 0x54, 0xb2, 0x9a, 0x85, 0xa7, 0xa1, 0xb7,
  8. 0x75, 0x33, 0xb6, 0xa9, 0xac, 0x84, 0x24, 0xb3, 0xde, 0xdb, 0x7d, 0x85, 0x2d, 0x96, 0x65, 0xe5,
  9. 0x3f, 0x72, 0x95, 0x24, 0x9f, 0x28, 0x68, 0xca, 0x4f, 0xdb, 0x44, 0x1c, 0x3e, 0x60, 0x12, 0x8a,
  10. 0xdd, 0x26, 0xa5, 0xeb, 0xff, 0x0b, 0x5e, 0xd4, 0x88, 0x38, 0x49, 0x2a, 0x6e, 0x5b, 0xbf, 0x12,
  11. 0x37, 0x47, 0xbd, 0x05, 0x6b, 0xbc, 0xdb, 0xf3, 0xee, 0xe4, 0x11, 0x8e, 0x41, 0x68, 0x7c, 0x61,
  12. 0x13, 0xd7, 0x42, 0xc8, 0x80, 0xbe, 0x36, 0x8f, 0xdc, 0x08, 0x8b, 0x4f, 0xac, 0xa4, 0xe2, 0x76,
  13. 0x0c, 0xc9, 0x63, 0x6c, 0x49, 0x58, 0x93, 0xed, 0xcc, 0xaa, 0xdc, 0x25, 0x3b, 0x0a, 0x60, 0x3f,
  14. 0x8b, 0x54, 0x3a, 0xc3, 0x4d, 0x31, 0xe7, 0x94, 0xa4, 0x44, 0xfd, 0x02, 0x03, 0x01, 0x00, 0x01,
  15. 0x02, 0x81, 0x81, 0x00, 0xc8, 0x62, 0xb9, 0xea, 0xde, 0x44, 0x53, 0x1d, 0x56, 0x97, 0xd9, 0x97,
  16. 0x9e, 0x1a, 0xcf, 0x30, 0x1e, 0x0a, 0x88, 0x45, 0x86, 0x29, 0x30, 0xa3, 0x4d, 0x9f, 0x61, 0x65,
  17. 0x73, 0xe0, 0xd6, 0x87, 0x8f, 0xb6, 0xf3, 0x06, 0xa3, 0x82, 0xdc, 0x7c, 0xac, 0xfe, 0x9b, 0x28,
  18. 0x9a, 0xae, 0xfd, 0xfb, 0xfe, 0x2f, 0x0e, 0xd8, 0x97, 0x04, 0xe3, 0xbb, 0x1f, 0xd1, 0xec, 0x0d,
  19. 0xba, 0xa3, 0x49, 0x7f, 0x47, 0xac, 0x8a, 0x44, 0x04, 0x7e, 0x86, 0xb7, 0x39, 0x42, 0x3f, 0xad,
  20. 0x1e, 0xb7, 0x0e, 0xa5, 0x51, 0xf4, 0x40, 0x63, 0x1e, 0xfd, 0xbd, 0xea, 0x9f, 0x41, 0x9f, 0xa8,
  21. 0x90, 0x1d, 0x6f, 0x0a, 0x5a, 0x95, 0x13, 0x11, 0x0d, 0x80, 0xaf, 0x5f, 0x64, 0x98, 0x8a, 0x2c,
  22. 0x78, 0x68, 0x65, 0xb0, 0x2b, 0x8b, 0xa2, 0x53, 0x87, 0xca, 0xf1, 0x64, 0x04, 0xab, 0xf2, 0x7b,
  23. 0xdb, 0x83, 0xc8, 0x81, 0x02, 0x41, 0x00, 0xf7, 0xbe, 0x5e, 0x23, 0xc3, 0x32, 0x3f, 0xbf, 0x8b,
  24. 0x8e, 0x3a, 0xee, 0xfc, 0xfc, 0xcb, 0xe5, 0xf7, 0xf1, 0x0b, 0xbc, 0x42, 0x82, 0xae, 0xd5, 0x7a,
  25. 0x3e, 0xca, 0xf7, 0xd5, 0x69, 0x3f, 0x64, 0x25, 0xa2, 0x1f, 0xb7, 0x75, 0x75, 0x05, 0x92, 0x42,
  26. 0xeb, 0xb8, 0xf1, 0xf3, 0x0a, 0x05, 0xe3, 0x94, 0xd1, 0x55, 0x78, 0x35, 0xa0, 0x36, 0xa0, 0x9b,
  27. 0x7c, 0x92, 0x84, 0x6c, 0xdd, 0xdc, 0x4d, 0x02, 0x41, 0x00, 0xd6, 0x86, 0x0e, 0x85, 0x42, 0x0b,
  28. 0x04, 0x08, 0x84, 0x21, 0x60, 0xf0, 0x0e, 0x0d, 0x88, 0xfd, 0x1e, 0x36, 0x10, 0x65, 0x4f, 0x1e,
  29. 0x53, 0xb4, 0x08, 0x72, 0x80, 0x5c, 0x3f, 0x59, 0x66, 0x17, 0xe6, 0x98, 0xf2, 0xe9, 0x6c, 0x7a,
  30. 0x06, 0x4c, 0xac, 0x76, 0x3d, 0xed, 0x8c, 0xa1, 0xce, 0xad, 0x1b, 0xbd, 0xb4, 0x7d, 0x28, 0xbc,
  31. 0xe3, 0x0e, 0x38, 0x8d, 0x99, 0xd8, 0x05, 0xb5, 0xa3, 0x71, 0x02, 0x40, 0x6d, 0xeb, 0xc3, 0x2d,
  32. 0x2e, 0xf0, 0x5e, 0xa4, 0x88, 0x31, 0x05, 0x29, 0x00, 0x8a, 0xd1, 0x95, 0x29, 0x9b, 0x83, 0xcf,
  33. 0x75, 0xdb, 0x31, 0xe3, 0x7a, 0x27, 0xde, 0x3a, 0x74, 0x30, 0x0c, 0x76, 0x4c, 0xd4, 0x50, 0x2a,
  34. 0x40, 0x2d, 0x39, 0xd9, 0x99, 0x63, 0xa9, 0x5d, 0x80, 0xae, 0x53, 0xca, 0x94, 0x3f, 0x05, 0x23,
  35. 0x1e, 0xf8, 0x05, 0x04, 0xe1, 0xb8, 0x35, 0xf2, 0x17, 0xb3, 0xa0, 0x89, 0x02, 0x41, 0x00, 0xab,
  36. 0x90, 0x88, 0xfa, 0x60, 0x08, 0x29, 0x50, 0x9a, 0x43, 0x8b, 0xa0, 0x50, 0xcc, 0xd8, 0x5a, 0xfe,
  37. 0x97, 0x64, 0x63, 0x71, 0x74, 0x22, 0xa3, 0x20, 0x02, 0x5a, 0xcf, 0xeb, 0xc6, 0x16, 0x95, 0x54,
  38. 0xd1, 0xcb, 0xab, 0x8d, 0x1a, 0xc6, 0x00, 0xfa, 0x08, 0x92, 0x9c, 0x71, 0xd5, 0x52, 0x52, 0x35,
  39. 0x96, 0x71, 0x4b, 0x8b, 0x92, 0x0c, 0xd0, 0xe9, 0xbf, 0xad, 0x63, 0x0b, 0xa5, 0xe9, 0xb1, 0x02,
  40. 0x41, 0x00, 0xdc, 0xcc, 0x27, 0xc8, 0xe4, 0xdc, 0x62, 0x48, 0xd5, 0x9b, 0xaf, 0xf5, 0xab, 0x60,
  41. 0xf6, 0x21, 0xfd, 0x53, 0xe2, 0xb7, 0x5d, 0x09, 0xc9, 0x1a, 0xa1, 0x04, 0xa9, 0xfc, 0x61, 0x2c,
  42. 0x5d, 0x04, 0x58, 0x3a, 0x5a, 0x39, 0xf1, 0x4a, 0x21, 0x56, 0x67, 0xfd, 0xcc, 0x20, 0xa3, 0x8f,
  43. 0x78, 0x18, 0x5a, 0x79, 0x3d, 0x2e, 0x8e, 0x7e, 0x86, 0x0a, 0xe6, 0xa8, 0x33, 0xc1, 0x04, 0x17,
  44. 0x4a, 0x9f, };
  45. static const unsigned char x509_public_rsa[] =
  46. "MIICdTCCAd4CCQCYjCwz0l9JpjANBgkqhkiG9w0BAQsFADB+MQswCQYDVQQGEwJD\
  47. WjEPMA0GA1UECAwGTW9yYXZhMQ0wCwYDVQQHDARCcm5vMRAwDgYDVQQKDAdMVEMg\
  48. THRkMQ8wDQYDVQQLDAZDcnlwdG8xEjAQBgNVBAMMCVRlc3QgQ2VydDEYMBYGCSqG\
  49. SIb3DQEJARYJdGVzdEBjZXJ0MCAXDTE3MDMwOTIzNDMzOVoYDzIyOTAxMjIyMjM0\
  50. MzM5WjB+MQswCQYDVQQGEwJDWjEPMA0GA1UECAwGTW9yYXZhMQ0wCwYDVQQHDARC\
  51. cm5vMRAwDgYDVQQKDAdMVEMgTHRkMQ8wDQYDVQQLDAZDcnlwdG8xEjAQBgNVBAMM\
  52. CVRlc3QgQ2VydDEYMBYGCSqGSIb3DQEJARYJdGVzdEBjZXJ0MIGfMA0GCSqGSIb3\
  53. DQEBAQUAA4GNADCBiQKBgQDPmt5kitrIMyCp14MxGVSymoWnobd1M7aprIQks97b\
  54. fYUtlmXlP3KVJJ8oaMpP20QcPmASit0mpev/C17UiDhJKm5bvxI3R70Fa7zb8+7k\
  55. EY5BaHxhE9dCyIC+No/cCItPrKTidgzJY2xJWJPtzKrcJTsKYD+LVDrDTTHnlKRE\
  56. /QIDAQABMA0GCSqGSIb3DQEBCwUAA4GBAApwWqupmmLGHeKOLFLcthQpAXXYep6T\
  57. 3S3e8X7fIG6TGhfvn5DHn+/V/C4184oOCwImI+VYRokdXdQ1AMGfVUomHJxsFPia\
  58. bv5Aw3hiKsIG3jigKHwmMScgkl3yn+8hLkx6thNbqQoa6Yyo20RqaEFBwlZ5G8lF\
  59. rZsdeO84SeCH";
  60. /* private keay - hexadecimal */
  61. static char *hex_d = "C862B9EADE44531D5697D9979E1ACF301E0A8845862930A34D9F616573E0D6878FB6F306A382DC7CACFE9B289AAEFDFBFE2F0ED89704E3BB1FD1EC0DBAA3497F47AC8A44047E86B739423FAD1EB70EA551F440631EFDBDEA9F419FA8901D6F0A5A9513110D80AF5F64988A2C786865B02B8BA25387CAF16404ABF27BDB83C881";
  62. static char *hex_dP = "6DEBC32D2EF05EA488310529008AD195299B83CF75DB31E37A27DE3A74300C764CD4502A402D39D99963A95D80AE53CA943F05231EF80504E1B835F217B3A089";
  63. static char *hex_dQ = "AB9088FA600829509A438BA050CCD85AFE976463717422A320025ACFEBC6169554D1CBAB8D1AC600FA08929C71D552523596714B8B920CD0E9BFAD630BA5E9B1";
  64. static char *hex_e = "010001";
  65. static char *hex_N = "CF9ADE648ADAC83320A9D783311954B29A85A7A1B77533B6A9AC8424B3DEDB7D852D9665E53F7295249F2868CA4FDB441C3E60128ADD26A5EBFF0B5ED48838492A6E5BBF123747BD056BBCDBF3EEE4118E41687C6113D742C880BE368FDC088B4FACA4E2760CC9636C495893EDCCAADC253B0A603F8B543AC34D31E794A444FD";
  66. static char *hex_p = "F7BE5E23C3323FBF8B8E3AEEFCFCCBE5F7F10BBC4282AED57A3ECAF7D5693F6425A21FB77575059242EBB8F1F30A05E394D1557835A036A09B7C92846CDDDC4D";
  67. static char *hex_q = "D6860E85420B0408842160F00E0D88FD1E3610654F1E53B40872805C3F596617E698F2E96C7A064CAC763DED8CA1CEAD1BBDB47D28BCE30E388D99D805B5A371";
  68. static char *hex_qP = "DCCC27C8E4DC6248D59BAFF5AB60F621FD53E2B75D09C91AA104A9FC612C5D04583A5A39F14A215667FDCC20A38F78185A793D2E8E7E860AE6A833C104174A9F";
  69. /* private keay - decimal */
  70. static char *dec_d = "140715588362011445903700789698620706303856890313846506579552319155852306603445626455616876267358538338151320072087950597426668358843246116141391746806252390039505422193715556188330352166601762210959618868365359433828069868584168017348772565936127608284367789455480066115411950431014508224203325089671253575809";
  71. static char *dec_dP = "5757027123463051531073361217943880203685183318942602176865989327630429772398553254013771630974725523559703665512845231173916766336576994271809362147385481";
  72. static char *dec_dQ = "8985566687080619280443708121716583572314829758991088624433980393739288226842152842353421251125477168722728289150354056572727675764519591179919295246625201";
  73. static char *dec_e = "65537";
  74. static char *dec_N = "145785157837445763858971808379627955816432214431353481009581718367907499729204464589803079767521523397316119124291441688063985017444589154155338311524887989148444674974298105211582428885045820631376256167593861203305479546421254276833052913791538765775697977909548553897629170045372476652935456198173974086909";
  75. static char *dec_p = "12975386429272921390465467849934248466500992474501042673679976015025637113752114471707151502138750486193421113099777767227628554763059580218432153760685133";
  76. static char *dec_q = "11235515692122231999359687466333538198133993435121038200055897831921312127192760781281669977582095991578071163376390471936482431583372835883432943212143473";
  77. static char *dec_qP = "11564102464723136702427739477324729528451027211272900753079601723449664482225846595388433622640284454614991112736446376964904474099700895632145077333609119";
  78. /*** openssl public RSA key in DER format */
  79. static const unsigned char openssl_public_rsa[] = {
  80. 0x30, 0x81, 0x9f, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  81. 0x05, 0x00, 0x03, 0x81, 0x8d, 0x00, 0x30, 0x81, 0x89, 0x02, 0x81, 0x81, 0x00, 0xcf, 0x9a, 0xde,
  82. 0x64, 0x8a, 0xda, 0xc8, 0x33, 0x20, 0xa9, 0xd7, 0x83, 0x31, 0x19, 0x54, 0xb2, 0x9a, 0x85, 0xa7,
  83. 0xa1, 0xb7, 0x75, 0x33, 0xb6, 0xa9, 0xac, 0x84, 0x24, 0xb3, 0xde, 0xdb, 0x7d, 0x85, 0x2d, 0x96,
  84. 0x65, 0xe5, 0x3f, 0x72, 0x95, 0x24, 0x9f, 0x28, 0x68, 0xca, 0x4f, 0xdb, 0x44, 0x1c, 0x3e, 0x60,
  85. 0x12, 0x8a, 0xdd, 0x26, 0xa5, 0xeb, 0xff, 0x0b, 0x5e, 0xd4, 0x88, 0x38, 0x49, 0x2a, 0x6e, 0x5b,
  86. 0xbf, 0x12, 0x37, 0x47, 0xbd, 0x05, 0x6b, 0xbc, 0xdb, 0xf3, 0xee, 0xe4, 0x11, 0x8e, 0x41, 0x68,
  87. 0x7c, 0x61, 0x13, 0xd7, 0x42, 0xc8, 0x80, 0xbe, 0x36, 0x8f, 0xdc, 0x08, 0x8b, 0x4f, 0xac, 0xa4,
  88. 0xe2, 0x76, 0x0c, 0xc9, 0x63, 0x6c, 0x49, 0x58, 0x93, 0xed, 0xcc, 0xaa, 0xdc, 0x25, 0x3b, 0x0a,
  89. 0x60, 0x3f, 0x8b, 0x54, 0x3a, 0xc3, 0x4d, 0x31, 0xe7, 0x94, 0xa4, 0x44, 0xfd, 0x02, 0x03, 0x01,
  90. 0x00, 0x01, };
  91. /* same key but with extra headers stripped */
  92. static const unsigned char openssl_public_rsa_stripped[] = {
  93. 0x30, 0x81, 0x89, 0x02, 0x81, 0x81, 0x00, 0xcf, 0x9a, 0xde,
  94. 0x64, 0x8a, 0xda, 0xc8, 0x33, 0x20, 0xa9, 0xd7, 0x83, 0x31, 0x19, 0x54, 0xb2, 0x9a, 0x85, 0xa7,
  95. 0xa1, 0xb7, 0x75, 0x33, 0xb6, 0xa9, 0xac, 0x84, 0x24, 0xb3, 0xde, 0xdb, 0x7d, 0x85, 0x2d, 0x96,
  96. 0x65, 0xe5, 0x3f, 0x72, 0x95, 0x24, 0x9f, 0x28, 0x68, 0xca, 0x4f, 0xdb, 0x44, 0x1c, 0x3e, 0x60,
  97. 0x12, 0x8a, 0xdd, 0x26, 0xa5, 0xeb, 0xff, 0x0b, 0x5e, 0xd4, 0x88, 0x38, 0x49, 0x2a, 0x6e, 0x5b,
  98. 0xbf, 0x12, 0x37, 0x47, 0xbd, 0x05, 0x6b, 0xbc, 0xdb, 0xf3, 0xee, 0xe4, 0x11, 0x8e, 0x41, 0x68,
  99. 0x7c, 0x61, 0x13, 0xd7, 0x42, 0xc8, 0x80, 0xbe, 0x36, 0x8f, 0xdc, 0x08, 0x8b, 0x4f, 0xac, 0xa4,
  100. 0xe2, 0x76, 0x0c, 0xc9, 0x63, 0x6c, 0x49, 0x58, 0x93, 0xed, 0xcc, 0xaa, 0xdc, 0x25, 0x3b, 0x0a,
  101. 0x60, 0x3f, 0x8b, 0x54, 0x3a, 0xc3, 0x4d, 0x31, 0xe7, 0x94, 0xa4, 0x44, 0xfd, 0x02, 0x03, 0x01,
  102. 0x00, 0x01, };
  103. extern const unsigned char _der_tests_cacert_root_cert[];
  104. extern const unsigned long _der_tests_cacert_root_cert_size;
  105. static int rsa_compat_test(void)
  106. {
  107. rsa_key key;
  108. unsigned char buf[1024];
  109. unsigned long len;
  110. /* try reading the key */
  111. DO(rsa_import(openssl_private_rsa, sizeof(openssl_private_rsa), &key));
  112. /* now try to export private/public and compare */
  113. len = sizeof(buf);
  114. DO(rsa_export(buf, &len, PK_PRIVATE, &key));
  115. if (len != sizeof(openssl_private_rsa) || memcmp(buf, openssl_private_rsa, len)) {
  116. fprintf(stderr, "RSA private export failed to match OpenSSL output, %lu, %lu\n", len, (unsigned long)sizeof(openssl_private_rsa));
  117. return 1;
  118. }
  119. len = sizeof(buf);
  120. DO(rsa_export(buf, &len, PK_PUBLIC, &key));
  121. if (len != sizeof(openssl_public_rsa_stripped) || memcmp(buf, openssl_public_rsa_stripped, len)) {
  122. fprintf(stderr, "RSA(private) public export failed to match OpenSSL output\n");
  123. return 1;
  124. }
  125. rsa_free(&key);
  126. /* try reading the public key */
  127. DO(rsa_import(openssl_public_rsa_stripped, sizeof(openssl_public_rsa_stripped), &key));
  128. len = sizeof(buf);
  129. DO(rsa_export(buf, &len, PK_PUBLIC, &key));
  130. if (len != sizeof(openssl_public_rsa_stripped) || memcmp(buf, openssl_public_rsa_stripped, len)) {
  131. fprintf(stderr, "RSA(public) stripped public import failed to match OpenSSL output\n");
  132. return 1;
  133. }
  134. rsa_free(&key);
  135. /* try reading the public key */
  136. DO(rsa_import(openssl_public_rsa, sizeof(openssl_public_rsa), &key));
  137. len = sizeof(buf);
  138. DO(rsa_export(buf, &len, PK_PUBLIC, &key));
  139. if (len != sizeof(openssl_public_rsa_stripped) || memcmp(buf, openssl_public_rsa_stripped, len)) {
  140. fprintf(stderr, "RSA(public) SSL public import failed to match OpenSSL output\n");
  141. return 1;
  142. }
  143. rsa_free(&key);
  144. /* try import private key from raw hexadecimal numbers */
  145. DO(rsa_import_radix(16, hex_N, hex_e, hex_d, hex_p, hex_q, hex_dP, hex_dQ, hex_qP, &key));
  146. len = sizeof(buf);
  147. DO(rsa_export(buf, &len, PK_PRIVATE, &key));
  148. if (len != sizeof(openssl_private_rsa) || memcmp(buf, openssl_private_rsa, len)) {
  149. fprintf(stderr, "RSA private export failed to match rsa_import_radix(16, ..)\n");
  150. return 1;
  151. }
  152. rsa_free(&key);
  153. /* try import private key from raw decimal numbers */
  154. DO(rsa_import_radix(10, dec_N, dec_e, dec_d, dec_p, dec_q, dec_dP, dec_dQ, dec_qP, &key));
  155. len = sizeof(buf);
  156. DO(rsa_export(buf, &len, PK_PRIVATE, &key));
  157. if (len != sizeof(openssl_private_rsa) || memcmp(buf, openssl_private_rsa, len)) {
  158. fprintf(stderr, "RSA private export failed to match rsa_import_radix(10, ..)\n");
  159. return 1;
  160. }
  161. rsa_free(&key);
  162. /* try import public key from raw hexadecimal numbers */
  163. DO(rsa_import_radix(16, hex_N, hex_e, NULL, NULL, NULL, NULL, NULL, NULL, &key));
  164. len = sizeof(buf);
  165. DO(rsa_export(buf, &len, PK_PUBLIC, &key));
  166. if (len != sizeof(openssl_public_rsa_stripped) || memcmp(buf, openssl_public_rsa_stripped, len)) {
  167. fprintf(stderr, "RSA public export failed to match rsa_import_radix(16, ..)\n");
  168. return 1;
  169. }
  170. rsa_free(&key);
  171. /* try import public key from raw decimal numbers */
  172. DO(rsa_import_radix(10, dec_N, dec_e, NULL, NULL, NULL, NULL, NULL, NULL, &key));
  173. len = sizeof(buf);
  174. DO(rsa_export(buf, &len, PK_PUBLIC, &key));
  175. if (len != sizeof(openssl_public_rsa_stripped) || memcmp(buf, openssl_public_rsa_stripped, len)) {
  176. fprintf(stderr, "RSA public export failed to match rsa_import_radix(10, ..)\n");
  177. return 1;
  178. }
  179. rsa_free(&key);
  180. /* try export in SubjectPublicKeyInfo format of the public key */
  181. DO(rsa_import(openssl_public_rsa, sizeof(openssl_public_rsa), &key));
  182. len = sizeof(buf);
  183. DO(rsa_export(buf, &len, PK_PUBLIC | PK_STD, &key));
  184. if (len != sizeof(openssl_public_rsa) || memcmp(buf, openssl_public_rsa, len)) {
  185. fprintf(stderr, "RSA(public) SSL public X.509 export failed to match OpenSSL output\n");
  186. print_hex("should", openssl_public_rsa, sizeof(openssl_public_rsa));
  187. print_hex("is", buf, len);
  188. return 1;
  189. }
  190. rsa_free(&key);
  191. return 0;
  192. }
  193. int rsa_test(void)
  194. {
  195. unsigned char in[1024], out[1024], tmp[3072];
  196. rsa_key key, privKey, pubKey;
  197. int hash_idx, prng_idx, stat, stat2, i, err;
  198. unsigned long rsa_msgsize, len, len2, len3, cnt, cnt2;
  199. static unsigned char lparam[] = { 0x01, 0x02, 0x03, 0x04 };
  200. void* dP;
  201. unsigned char* p;
  202. unsigned char* p2;
  203. unsigned char* p3;
  204. if (rsa_compat_test() != 0) {
  205. return 1;
  206. }
  207. hash_idx = find_hash("sha1");
  208. prng_idx = find_prng("yarrow");
  209. if (hash_idx == -1 || prng_idx == -1) {
  210. fprintf(stderr, "rsa_test requires LTC_SHA1 and yarrow");
  211. return 1;
  212. }
  213. /* make 10 random key */
  214. for (cnt = 0; cnt < 10; cnt++) {
  215. DO(rsa_make_key(&yarrow_prng, prng_idx, 1024/8, 65537, &key));
  216. if (mp_count_bits(key.N) != 1024) {
  217. fprintf(stderr, "rsa_1024 key modulus has %d bits\n", mp_count_bits(key.N));
  218. len = mp_unsigned_bin_size(key.N);
  219. mp_to_unsigned_bin(key.N, tmp);
  220. fprintf(stderr, "N == \n");
  221. for (cnt = 0; cnt < len; ) {
  222. fprintf(stderr, "%02x ", tmp[cnt]);
  223. if (!(++cnt & 15)) fprintf(stderr, "\n");
  224. }
  225. len = mp_unsigned_bin_size(key.p);
  226. mp_to_unsigned_bin(key.p, tmp);
  227. fprintf(stderr, "p == \n");
  228. for (cnt = 0; cnt < len; ) {
  229. fprintf(stderr, "%02x ", tmp[cnt]);
  230. if (!(++cnt & 15)) fprintf(stderr, "\n");
  231. }
  232. len = mp_unsigned_bin_size(key.q);
  233. mp_to_unsigned_bin(key.q, tmp);
  234. fprintf(stderr, "\nq == \n");
  235. for (cnt = 0; cnt < len; ) {
  236. fprintf(stderr, "%02x ", tmp[cnt]);
  237. if (!(++cnt & 15)) fprintf(stderr, "\n");
  238. }
  239. fprintf(stderr, "\n");
  240. return 1;
  241. }
  242. if (cnt != 9) {
  243. rsa_free(&key);
  244. }
  245. }
  246. /* encrypt the key (without lparam) */
  247. for (cnt = 0; cnt < 4; cnt++) {
  248. for (rsa_msgsize = 1; rsa_msgsize <= 86; rsa_msgsize++) {
  249. /* make a random key/msg */
  250. yarrow_read(in, rsa_msgsize, &yarrow_prng);
  251. len = sizeof(out);
  252. len2 = rsa_msgsize;
  253. DO(rsa_encrypt_key(in, rsa_msgsize, out, &len, NULL, 0, &yarrow_prng, prng_idx, hash_idx, &key));
  254. /* change a byte */
  255. out[8] ^= 1;
  256. DOX((err = rsa_decrypt_key(out, len, tmp, &len2, NULL, 0, hash_idx, &stat2, &key))
  257. == CRYPT_INVALID_PACKET ? CRYPT_OK:err, "should fail");
  258. /* change a byte back */
  259. out[8] ^= 1;
  260. if (len2 != rsa_msgsize) {
  261. fprintf(stderr, "\n%i:rsa_decrypt_key mismatch len %lu (first decrypt)", __LINE__, len2);
  262. return 1;
  263. }
  264. len2 = rsa_msgsize;
  265. DO(rsa_decrypt_key(out, len, tmp, &len2, NULL, 0, hash_idx, &stat, &key));
  266. if (!(stat == 1 && stat2 == 0)) {
  267. fprintf(stderr, "rsa_decrypt_key (without lparam) failed (rsa_msgsize = %lu)", rsa_msgsize);
  268. fprintf(stderr, "\n stat: %i stat2: %i", stat, stat2);
  269. return 1;
  270. }
  271. if (len2 != rsa_msgsize || memcmp(tmp, in, rsa_msgsize)) {
  272. fprintf(stderr, "\nrsa_decrypt_key mismatch, len %lu (second decrypt)\n", len2);
  273. print_hex("Original", in, rsa_msgsize);
  274. print_hex("Output", tmp, len2);
  275. return 1;
  276. }
  277. }
  278. }
  279. /* encrypt the key (with lparam) */
  280. for (rsa_msgsize = 1; rsa_msgsize <= 86; rsa_msgsize++) {
  281. len = sizeof(out);
  282. len2 = rsa_msgsize;
  283. DO(rsa_encrypt_key(in, rsa_msgsize, out, &len, lparam, sizeof(lparam), &yarrow_prng, prng_idx, hash_idx, &key));
  284. /* change a byte */
  285. out[8] ^= 1;
  286. DOX((err = rsa_decrypt_key(out, len, tmp, &len2, lparam, sizeof(lparam), hash_idx, &stat2, &key))
  287. == CRYPT_INVALID_PACKET ? CRYPT_OK:err, "should fail");
  288. if (len2 != rsa_msgsize) {
  289. fprintf(stderr, "\n%i:rsa_decrypt_key mismatch len %lu (first decrypt)", __LINE__, len2);
  290. return 1;
  291. }
  292. /* change a byte back */
  293. out[8] ^= 1;
  294. len2 = rsa_msgsize;
  295. DO(rsa_decrypt_key(out, len, tmp, &len2, lparam, sizeof(lparam), hash_idx, &stat, &key));
  296. if (!(stat == 1 && stat2 == 0)) {
  297. fprintf(stderr, "rsa_decrypt_key (with lparam) failed (rsa_msgsize = %lu)", rsa_msgsize);
  298. return 1;
  299. }
  300. if (len2 != rsa_msgsize || memcmp(tmp, in, rsa_msgsize)) {
  301. fprintf(stderr, "rsa_decrypt_key mismatch len %lu", len2);
  302. print_hex("Original", in, rsa_msgsize);
  303. print_hex("Output", tmp, len2);
  304. return 1;
  305. }
  306. }
  307. /* encrypt the key PKCS #1 v1.5 (payload from 1 to 117 bytes) */
  308. for (rsa_msgsize = 1; rsa_msgsize <= 117; rsa_msgsize++) {
  309. len = sizeof(out);
  310. len2 = rsa_msgsize;
  311. /* make a random key/msg */
  312. yarrow_read(in, rsa_msgsize, &yarrow_prng);
  313. DO(rsa_encrypt_key_ex(in, rsa_msgsize, out, &len, NULL, 0, &yarrow_prng, prng_idx, 0, LTC_PKCS_1_V1_5, &key));
  314. len2 = rsa_msgsize;
  315. DO(rsa_decrypt_key_ex(out, len, tmp, &len2, NULL, 0, 0, LTC_PKCS_1_V1_5, &stat, &key));
  316. if (stat != 1) {
  317. fprintf(stderr, "rsa_decrypt_key_ex failed, %d, %d", stat, stat2);
  318. return 1;
  319. }
  320. if (len2 != rsa_msgsize) {
  321. fprintf(stderr, "rsa_decrypt_key_ex mismatch len %lu", len2);
  322. return 1;
  323. }
  324. if (memcmp(tmp, in, rsa_msgsize)) {
  325. fprintf(stderr, "rsa_decrypt_key_ex mismatch data");
  326. print_hex("Original", in, rsa_msgsize);
  327. print_hex("Output", tmp, rsa_msgsize);
  328. return 1;
  329. }
  330. }
  331. /* sign a message (unsalted, lower cholestorol and Atkins approved) now */
  332. len = sizeof(out);
  333. DO(rsa_sign_hash(in, 20, out, &len, &yarrow_prng, prng_idx, hash_idx, 0, &key));
  334. /* export key and import as both private and public */
  335. len2 = sizeof(tmp);
  336. DO(rsa_export(tmp, &len2, PK_PRIVATE, &key));
  337. DO(rsa_import(tmp, len2, &privKey));
  338. len2 = sizeof(tmp);
  339. DO(rsa_export(tmp, &len2, PK_PUBLIC, &key));
  340. DO(rsa_import(tmp, len2, &pubKey));
  341. /* verify with original */
  342. DO(rsa_verify_hash(out, len, in, 20, hash_idx, 0, &stat, &key));
  343. /* change a byte */
  344. in[0] ^= 1;
  345. DO(rsa_verify_hash(out, len, in, 20, hash_idx, 0, &stat2, &key));
  346. if (!(stat == 1 && stat2 == 0)) {
  347. fprintf(stderr, "rsa_verify_hash (unsalted, origKey) failed, %d, %d", stat, stat2);
  348. rsa_free(&key);
  349. rsa_free(&pubKey);
  350. rsa_free(&privKey);
  351. return 1;
  352. }
  353. /* verify with privKey */
  354. /* change byte back to original */
  355. in[0] ^= 1;
  356. DO(rsa_verify_hash(out, len, in, 20, hash_idx, 0, &stat, &privKey));
  357. /* change a byte */
  358. in[0] ^= 1;
  359. DO(rsa_verify_hash(out, len, in, 20, hash_idx, 0, &stat2, &privKey));
  360. if (!(stat == 1 && stat2 == 0)) {
  361. fprintf(stderr, "rsa_verify_hash (unsalted, privKey) failed, %d, %d", stat, stat2);
  362. rsa_free(&key);
  363. rsa_free(&pubKey);
  364. rsa_free(&privKey);
  365. return 1;
  366. }
  367. /* verify with privKey but remove pointer to dP to test without CRT */
  368. dP = privKey.dP;
  369. privKey.dP = NULL;
  370. /* change byte back to original */
  371. in[0] ^= 1;
  372. DO(rsa_verify_hash(out, len, in, 20, hash_idx, 0, &stat, &privKey));
  373. /* change a byte */
  374. in[0] ^= 1;
  375. DO(rsa_verify_hash(out, len, in, 20, hash_idx, 0, &stat2, &privKey));
  376. if (!(stat == 1 && stat2 == 0)) {
  377. fprintf(stderr, "rsa_verify_hash (unsalted, privKey) failed, %d, %d", stat, stat2);
  378. rsa_free(&key);
  379. rsa_free(&pubKey);
  380. rsa_free(&privKey);
  381. return 1;
  382. }
  383. privKey.dP = dP;
  384. /* verify with pubKey */
  385. /* change byte back to original */
  386. in[0] ^= 1;
  387. DO(rsa_verify_hash(out, len, in, 20, hash_idx, 0, &stat, &pubKey));
  388. /* change a byte */
  389. in[0] ^= 1;
  390. DO(rsa_verify_hash(out, len, in, 20, hash_idx, 0, &stat2, &pubKey));
  391. if (!(stat == 1 && stat2 == 0)) {
  392. fprintf(stderr, "rsa_verify_hash (unsalted, pubkey) failed, %d, %d", stat, stat2);
  393. rsa_free(&key);
  394. rsa_free(&pubKey);
  395. rsa_free(&privKey);
  396. return 1;
  397. }
  398. /* sign a message (salted) now (use privKey to make, pubKey to verify) */
  399. len = sizeof(out);
  400. DO(rsa_sign_hash(in, 20, out, &len, &yarrow_prng, prng_idx, hash_idx, 8, &privKey));
  401. DO(rsa_verify_hash(out, len, in, 20, hash_idx, 8, &stat, &pubKey));
  402. /* change a byte */
  403. in[0] ^= 1;
  404. DO(rsa_verify_hash(out, len, in, 20, hash_idx, 8, &stat2, &pubKey));
  405. if (!(stat == 1 && stat2 == 0)) {
  406. fprintf(stderr, "rsa_verify_hash (salted) failed, %d, %d", stat, stat2);
  407. rsa_free(&key);
  408. rsa_free(&pubKey);
  409. rsa_free(&privKey);
  410. return 1;
  411. }
  412. /* sign a message with PKCS #1 v1.5 */
  413. len = sizeof(out);
  414. DO(rsa_sign_hash_ex(in, 20, out, &len, LTC_PKCS_1_V1_5, &yarrow_prng, prng_idx, hash_idx, 8, &privKey));
  415. DO(rsa_verify_hash_ex(out, len, in, 20, LTC_PKCS_1_V1_5, hash_idx, 8, &stat, &pubKey));
  416. /* change a byte */
  417. in[0] ^= 1;
  418. DO(rsa_verify_hash_ex(out, len, in, 20, LTC_PKCS_1_V1_5, hash_idx, 8, &stat2, &pubKey));
  419. if (!(stat == 1 && stat2 == 0)) {
  420. fprintf(stderr, "rsa_verify_hash_ex failed, %d, %d", stat, stat2);
  421. rsa_free(&key);
  422. rsa_free(&pubKey);
  423. rsa_free(&privKey);
  424. return 1;
  425. }
  426. /* Testcase for Bleichenbacher attack
  427. *
  428. * (1) Create a valid signature
  429. * (2) Check that it can be verified
  430. * (3) Decrypt the package to fetch plain text
  431. * (4) Forge the structure of PKCS#1-EMSA encoded data
  432. * (4.1) Search for start and end of the padding string
  433. * (4.2) Move the signature to the front of the padding string
  434. * (4.3) Zero the message until the end
  435. * (5) Encrypt the package again
  436. * (6) Profit :)
  437. * For PS lengths < 8: the verification process should fail
  438. * For PS lengths >= 8: the verification process should succeed
  439. * For all PS lengths: the result should not be valid
  440. */
  441. p = in;
  442. p2 = out;
  443. p3 = tmp;
  444. for (i = 0; i < 9; ++i) {
  445. len = sizeof(in);
  446. len2 = sizeof(out);
  447. cnt = rsa_get_size(&key);
  448. /* (1) */
  449. DO(rsa_sign_hash_ex(p, 20, p2, &len2, LTC_PKCS_1_V1_5, &yarrow_prng, prng_idx, hash_idx, 8, &privKey));
  450. /* (2) */
  451. DOX(rsa_verify_hash_ex(p2, len2, p, 20, LTC_PKCS_1_V1_5, hash_idx, -1, &stat, &pubKey), "should succeed");
  452. DOX(stat == 1?CRYPT_OK:CRYPT_FAIL_TESTVECTOR, "should succeed");
  453. len3 = sizeof(tmp);
  454. /* (3) */
  455. DO(ltc_mp.rsa_me(p2, len2, p3, &len3, PK_PUBLIC, &key));
  456. /* (4) */
  457. #if 0
  458. printf("\nBefore:");
  459. for (cnt = 0; cnt < len3; ++cnt) {
  460. if (cnt%32 == 0)
  461. printf("\n%3lu:", cnt);
  462. printf(" %02x", p3[cnt]);
  463. }
  464. #endif
  465. /* (4.1) */
  466. for (cnt = 0; cnt < len3; ++cnt) {
  467. if (p3[cnt] == 0xff)
  468. break;
  469. }
  470. for (cnt2 = cnt+1; cnt2 < len3; ++cnt2) {
  471. if (p3[cnt2] != 0xff)
  472. break;
  473. }
  474. /* (4.2) */
  475. memmove(&p3[cnt+i], &p3[cnt2], len3-cnt2);
  476. /* (4.3) */
  477. for (cnt = cnt + len3-cnt2+i; cnt < len; ++cnt) {
  478. p3[cnt] = 0;
  479. }
  480. #if 0
  481. printf("\nAfter:");
  482. for (cnt = 0; cnt < len3; ++cnt) {
  483. if (cnt%32 == 0)
  484. printf("\n%3lu:", cnt);
  485. printf(" %02x", p3[cnt]);
  486. }
  487. printf("\n");
  488. #endif
  489. len2 = sizeof(out);
  490. /* (5) */
  491. DO(ltc_mp.rsa_me(p3, len3, p2, &len2, PK_PRIVATE, &key));
  492. len3 = sizeof(tmp);
  493. /* (6) */
  494. if (i < 8)
  495. DOX(rsa_verify_hash_ex(p2, len2, p, 20, LTC_PKCS_1_V1_5, hash_idx, -1, &stat, &pubKey)
  496. == CRYPT_INVALID_PACKET ? CRYPT_OK:CRYPT_INVALID_PACKET, "should fail");
  497. else
  498. DOX(rsa_verify_hash_ex(p2, len2, p, 20, LTC_PKCS_1_V1_5, hash_idx, -1, &stat, &pubKey), "should succeed");
  499. DOX(stat == 0?CRYPT_OK:CRYPT_FAIL_TESTVECTOR, "should fail");
  500. }
  501. rsa_free(&key);
  502. /* try reading the public RSA key from a X509 certificate */
  503. len3 = sizeof(tmp);
  504. DO(base64_decode(x509_public_rsa, sizeof(x509_public_rsa), tmp, &len3));
  505. DO(rsa_import_x509(tmp, len3, &key));
  506. len = sizeof(tmp);
  507. DO(rsa_export(tmp, &len, PK_PUBLIC, &key));
  508. if (len != sizeof(openssl_public_rsa_stripped) || memcmp(tmp, openssl_public_rsa_stripped, len)) {
  509. fprintf(stderr, "RSA public export failed to match rsa_import_x509\n");
  510. return 1;
  511. }
  512. rsa_free(&key);
  513. len3 = sizeof(tmp);
  514. DO(base64_decode(_der_tests_cacert_root_cert, _der_tests_cacert_root_cert_size, tmp, &len3));
  515. DO(rsa_import_x509(tmp, len3, &key));
  516. /* free the key and return */
  517. rsa_free(&key);
  518. rsa_free(&pubKey);
  519. rsa_free(&privKey);
  520. return 0;
  521. }
  522. #else
  523. int rsa_test(void)
  524. {
  525. fprintf(stderr, "NOP");
  526. return 0;
  527. }
  528. #endif
  529. /* $Source$ */
  530. /* $Revision$ */
  531. /* $Date$ */