rsa_test.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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. /* generated with the private key above as:
  104. echo -n 'test' | openssl rsautl -sign -inkey rsa_private.pem -pkcs -hexdump
  105. */
  106. static const unsigned char openssl_rsautl_pkcs[] = {
  107. 0x24, 0xef, 0x54, 0xea, 0x1a, 0x12, 0x0c, 0xf4, 0x04, 0x0c, 0x48, 0xc8, 0xe8, 0x17, 0xd2, 0x6f,
  108. 0xc3, 0x41, 0xb3, 0x97, 0x5c, 0xbc, 0xa3, 0x2d, 0x21, 0x00, 0x10, 0x0e, 0xbb, 0xf7, 0x30, 0x21,
  109. 0x7e, 0x12, 0xd2, 0xdf, 0x26, 0x28, 0xd8, 0x0f, 0x6d, 0x4d, 0xc8, 0x4d, 0xa8, 0x78, 0xe7, 0x03,
  110. 0xee, 0xbc, 0x68, 0xba, 0x98, 0xea, 0xe9, 0xb6, 0x06, 0x8d, 0x85, 0x5b, 0xdb, 0xa6, 0x49, 0x86,
  111. 0x6f, 0xc7, 0x3d, 0xe0, 0x53, 0x83, 0xe0, 0xea, 0xb1, 0x08, 0x6a, 0x7b, 0xbd, 0xeb, 0xb5, 0x4a,
  112. 0xdd, 0xbc, 0x64, 0x97, 0x8c, 0x17, 0x20, 0xa3, 0x5c, 0xd4, 0xb8, 0x87, 0x43, 0xc5, 0x13, 0xad,
  113. 0x41, 0x6e, 0x45, 0x41, 0x32, 0xd4, 0x09, 0x12, 0x7f, 0xdc, 0x59, 0x1f, 0x28, 0x3f, 0x1e, 0xbc,
  114. 0xef, 0x57, 0x23, 0x4b, 0x3a, 0xa3, 0x24, 0x91, 0x4d, 0xfb, 0xb2, 0xd4, 0xe7, 0x5e, 0x41, 0x7e,
  115. };
  116. extern const unsigned char _der_tests_cacert_root_cert[];
  117. extern const unsigned long _der_tests_cacert_root_cert_size;
  118. static int rsa_compat_test(void)
  119. {
  120. rsa_key key, pubkey;
  121. int stat;
  122. unsigned char buf[1024];
  123. unsigned long len;
  124. /* try reading the key */
  125. DO(rsa_import(openssl_private_rsa, sizeof(openssl_private_rsa), &key));
  126. DO(rsa_import(openssl_public_rsa, sizeof(openssl_public_rsa), &pubkey));
  127. /* sign-verify a message with PKCS #1 v1.5 no ASN.1 */
  128. len = sizeof(buf);
  129. DO(rsa_sign_hash_ex((unsigned char*)"test", 4, buf, &len, LTC_PKCS_1_V1_5_NA1, NULL, 0, 0, 0, &key));
  130. if (len != sizeof(openssl_rsautl_pkcs) || memcmp(buf, openssl_rsautl_pkcs, len)) {
  131. fprintf(stderr, "RSA rsa_sign_hash_ex + LTC_PKCS_1_V1_5_NA1 failed\n");
  132. return 1;
  133. }
  134. stat = 0;
  135. DO(rsa_verify_hash_ex(openssl_rsautl_pkcs, sizeof(openssl_rsautl_pkcs), (unsigned char*)"test", 4, LTC_PKCS_1_V1_5_NA1, 0, 0, &stat, &pubkey));
  136. if (stat != 1) {
  137. fprintf(stderr, "RSA rsa_verify_hash_ex + LTC_PKCS_1_V1_5_NA1 failed\n");
  138. return 1;
  139. }
  140. /* now try to export private/public and compare */
  141. len = sizeof(buf);
  142. DO(rsa_export(buf, &len, PK_PRIVATE, &key));
  143. if (len != sizeof(openssl_private_rsa) || memcmp(buf, openssl_private_rsa, len)) {
  144. fprintf(stderr, "RSA private export failed to match OpenSSL output, %lu, %lu\n", len, (unsigned long)sizeof(openssl_private_rsa));
  145. return 1;
  146. }
  147. len = sizeof(buf);
  148. DO(rsa_export(buf, &len, PK_PUBLIC, &key));
  149. if (len != sizeof(openssl_public_rsa_stripped) || memcmp(buf, openssl_public_rsa_stripped, len)) {
  150. fprintf(stderr, "RSA(private) public export failed to match OpenSSL output\n");
  151. return 1;
  152. }
  153. rsa_free(&key);
  154. /* try reading the public key */
  155. DO(rsa_import(openssl_public_rsa_stripped, sizeof(openssl_public_rsa_stripped), &key));
  156. len = sizeof(buf);
  157. DO(rsa_export(buf, &len, PK_PUBLIC, &key));
  158. if (len != sizeof(openssl_public_rsa_stripped) || memcmp(buf, openssl_public_rsa_stripped, len)) {
  159. fprintf(stderr, "RSA(public) stripped public import failed to match OpenSSL output\n");
  160. return 1;
  161. }
  162. rsa_free(&key);
  163. /* try reading the public key */
  164. DO(rsa_import(openssl_public_rsa, sizeof(openssl_public_rsa), &key));
  165. len = sizeof(buf);
  166. DO(rsa_export(buf, &len, PK_PUBLIC, &key));
  167. if (len != sizeof(openssl_public_rsa_stripped) || memcmp(buf, openssl_public_rsa_stripped, len)) {
  168. fprintf(stderr, "RSA(public) SSL public import failed to match OpenSSL output\n");
  169. return 1;
  170. }
  171. rsa_free(&key);
  172. /* try import private key from raw hexadecimal numbers */
  173. DO(rsa_import_radix(16, hex_N, hex_e, hex_d, hex_p, hex_q, hex_dP, hex_dQ, hex_qP, &key));
  174. len = sizeof(buf);
  175. DO(rsa_export(buf, &len, PK_PRIVATE, &key));
  176. if (len != sizeof(openssl_private_rsa) || memcmp(buf, openssl_private_rsa, len)) {
  177. fprintf(stderr, "RSA private export failed to match rsa_import_radix(16, ..)\n");
  178. return 1;
  179. }
  180. rsa_free(&key);
  181. /* try import private key from raw decimal numbers */
  182. DO(rsa_import_radix(10, dec_N, dec_e, dec_d, dec_p, dec_q, dec_dP, dec_dQ, dec_qP, &key));
  183. len = sizeof(buf);
  184. DO(rsa_export(buf, &len, PK_PRIVATE, &key));
  185. if (len != sizeof(openssl_private_rsa) || memcmp(buf, openssl_private_rsa, len)) {
  186. fprintf(stderr, "RSA private export failed to match rsa_import_radix(10, ..)\n");
  187. return 1;
  188. }
  189. rsa_free(&key);
  190. /* try import public key from raw hexadecimal numbers */
  191. DO(rsa_import_radix(16, hex_N, hex_e, NULL, NULL, NULL, NULL, NULL, NULL, &key));
  192. len = sizeof(buf);
  193. DO(rsa_export(buf, &len, PK_PUBLIC, &key));
  194. if (len != sizeof(openssl_public_rsa_stripped) || memcmp(buf, openssl_public_rsa_stripped, len)) {
  195. fprintf(stderr, "RSA public export failed to match rsa_import_radix(16, ..)\n");
  196. return 1;
  197. }
  198. rsa_free(&key);
  199. /* try import public key from raw decimal numbers */
  200. DO(rsa_import_radix(10, dec_N, dec_e, NULL, NULL, NULL, NULL, NULL, NULL, &key));
  201. len = sizeof(buf);
  202. DO(rsa_export(buf, &len, PK_PUBLIC, &key));
  203. if (len != sizeof(openssl_public_rsa_stripped) || memcmp(buf, openssl_public_rsa_stripped, len)) {
  204. fprintf(stderr, "RSA public export failed to match rsa_import_radix(10, ..)\n");
  205. return 1;
  206. }
  207. rsa_free(&key);
  208. /* try export in SubjectPublicKeyInfo format of the public key */
  209. DO(rsa_import(openssl_public_rsa, sizeof(openssl_public_rsa), &key));
  210. len = sizeof(buf);
  211. DO(rsa_export(buf, &len, PK_PUBLIC | PK_STD, &key));
  212. if (len != sizeof(openssl_public_rsa) || memcmp(buf, openssl_public_rsa, len)) {
  213. fprintf(stderr, "RSA(public) SSL public X.509 export failed to match OpenSSL output\n");
  214. print_hex("should", openssl_public_rsa, sizeof(openssl_public_rsa));
  215. print_hex("is", buf, len);
  216. return 1;
  217. }
  218. rsa_free(&key);
  219. return 0;
  220. }
  221. int rsa_test(void)
  222. {
  223. unsigned char in[1024], out[1024], tmp[3072];
  224. rsa_key key, privKey, pubKey;
  225. int hash_idx, prng_idx, stat, stat2, i, err;
  226. unsigned long rsa_msgsize, len, len2, len3, cnt, cnt2;
  227. static unsigned char lparam[] = { 0x01, 0x02, 0x03, 0x04 };
  228. void* dP;
  229. unsigned char* p;
  230. unsigned char* p2;
  231. unsigned char* p3;
  232. if (rsa_compat_test() != 0) {
  233. return 1;
  234. }
  235. hash_idx = find_hash("sha1");
  236. prng_idx = find_prng("yarrow");
  237. if (hash_idx == -1 || prng_idx == -1) {
  238. fprintf(stderr, "rsa_test requires LTC_SHA1 and yarrow");
  239. return 1;
  240. }
  241. /* make 10 random key */
  242. for (cnt = 0; cnt < 10; cnt++) {
  243. DO(rsa_make_key(&yarrow_prng, prng_idx, 1024/8, 65537, &key));
  244. if (mp_count_bits(key.N) != 1024) {
  245. fprintf(stderr, "rsa_1024 key modulus has %d bits\n", mp_count_bits(key.N));
  246. len = mp_unsigned_bin_size(key.N);
  247. mp_to_unsigned_bin(key.N, tmp);
  248. fprintf(stderr, "N == \n");
  249. for (cnt = 0; cnt < len; ) {
  250. fprintf(stderr, "%02x ", tmp[cnt]);
  251. if (!(++cnt & 15)) fprintf(stderr, "\n");
  252. }
  253. len = mp_unsigned_bin_size(key.p);
  254. mp_to_unsigned_bin(key.p, tmp);
  255. fprintf(stderr, "p == \n");
  256. for (cnt = 0; cnt < len; ) {
  257. fprintf(stderr, "%02x ", tmp[cnt]);
  258. if (!(++cnt & 15)) fprintf(stderr, "\n");
  259. }
  260. len = mp_unsigned_bin_size(key.q);
  261. mp_to_unsigned_bin(key.q, tmp);
  262. fprintf(stderr, "\nq == \n");
  263. for (cnt = 0; cnt < len; ) {
  264. fprintf(stderr, "%02x ", tmp[cnt]);
  265. if (!(++cnt & 15)) fprintf(stderr, "\n");
  266. }
  267. fprintf(stderr, "\n");
  268. return 1;
  269. }
  270. if (cnt != 9) {
  271. rsa_free(&key);
  272. }
  273. }
  274. /* encrypt the key (without lparam) */
  275. for (cnt = 0; cnt < 4; cnt++) {
  276. for (rsa_msgsize = 1; rsa_msgsize <= 86; rsa_msgsize++) {
  277. /* make a random key/msg */
  278. yarrow_read(in, rsa_msgsize, &yarrow_prng);
  279. len = sizeof(out);
  280. len2 = rsa_msgsize;
  281. DO(rsa_encrypt_key(in, rsa_msgsize, out, &len, NULL, 0, &yarrow_prng, prng_idx, hash_idx, &key));
  282. /* change a byte */
  283. out[8] ^= 1;
  284. DOX((err = rsa_decrypt_key(out, len, tmp, &len2, NULL, 0, hash_idx, &stat2, &key))
  285. == CRYPT_INVALID_PACKET ? CRYPT_OK:err, "should fail");
  286. /* change a byte back */
  287. out[8] ^= 1;
  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. len2 = rsa_msgsize;
  293. DO(rsa_decrypt_key(out, len, tmp, &len2, NULL, 0, hash_idx, &stat, &key));
  294. if (!(stat == 1 && stat2 == 0)) {
  295. fprintf(stderr, "rsa_decrypt_key (without lparam) failed (rsa_msgsize = %lu)", rsa_msgsize);
  296. fprintf(stderr, "\n stat: %i stat2: %i", stat, stat2);
  297. return 1;
  298. }
  299. if (len2 != rsa_msgsize || memcmp(tmp, in, rsa_msgsize)) {
  300. fprintf(stderr, "\nrsa_decrypt_key mismatch, len %lu (second decrypt)\n", len2);
  301. print_hex("Original", in, rsa_msgsize);
  302. print_hex("Output", tmp, len2);
  303. return 1;
  304. }
  305. }
  306. }
  307. /* encrypt the key (with lparam) */
  308. for (rsa_msgsize = 1; rsa_msgsize <= 86; rsa_msgsize++) {
  309. len = sizeof(out);
  310. len2 = rsa_msgsize;
  311. DO(rsa_encrypt_key(in, rsa_msgsize, out, &len, lparam, sizeof(lparam), &yarrow_prng, prng_idx, hash_idx, &key));
  312. /* change a byte */
  313. out[8] ^= 1;
  314. DOX((err = rsa_decrypt_key(out, len, tmp, &len2, lparam, sizeof(lparam), hash_idx, &stat2, &key))
  315. == CRYPT_INVALID_PACKET ? CRYPT_OK:err, "should fail");
  316. if (len2 != rsa_msgsize) {
  317. fprintf(stderr, "\n%i:rsa_decrypt_key mismatch len %lu (first decrypt)", __LINE__, len2);
  318. return 1;
  319. }
  320. /* change a byte back */
  321. out[8] ^= 1;
  322. len2 = rsa_msgsize;
  323. DO(rsa_decrypt_key(out, len, tmp, &len2, lparam, sizeof(lparam), hash_idx, &stat, &key));
  324. if (!(stat == 1 && stat2 == 0)) {
  325. fprintf(stderr, "rsa_decrypt_key (with lparam) failed (rsa_msgsize = %lu)", rsa_msgsize);
  326. return 1;
  327. }
  328. if (len2 != rsa_msgsize || memcmp(tmp, in, rsa_msgsize)) {
  329. fprintf(stderr, "rsa_decrypt_key mismatch len %lu", len2);
  330. print_hex("Original", in, rsa_msgsize);
  331. print_hex("Output", tmp, len2);
  332. return 1;
  333. }
  334. }
  335. /* encrypt the key PKCS #1 v1.5 (payload from 1 to 117 bytes) */
  336. for (rsa_msgsize = 1; rsa_msgsize <= 117; rsa_msgsize++) {
  337. len = sizeof(out);
  338. len2 = rsa_msgsize;
  339. /* make a random key/msg */
  340. yarrow_read(in, rsa_msgsize, &yarrow_prng);
  341. DO(rsa_encrypt_key_ex(in, rsa_msgsize, out, &len, NULL, 0, &yarrow_prng, prng_idx, 0, LTC_PKCS_1_V1_5, &key));
  342. len2 = rsa_msgsize;
  343. DO(rsa_decrypt_key_ex(out, len, tmp, &len2, NULL, 0, 0, LTC_PKCS_1_V1_5, &stat, &key));
  344. if (stat != 1) {
  345. fprintf(stderr, "rsa_decrypt_key_ex failed, %d, %d", stat, stat2);
  346. return 1;
  347. }
  348. if (len2 != rsa_msgsize) {
  349. fprintf(stderr, "rsa_decrypt_key_ex mismatch len %lu", len2);
  350. return 1;
  351. }
  352. if (memcmp(tmp, in, rsa_msgsize)) {
  353. fprintf(stderr, "rsa_decrypt_key_ex mismatch data");
  354. print_hex("Original", in, rsa_msgsize);
  355. print_hex("Output", tmp, rsa_msgsize);
  356. return 1;
  357. }
  358. }
  359. /* sign a message (unsalted, lower cholestorol and Atkins approved) now */
  360. len = sizeof(out);
  361. DO(rsa_sign_hash(in, 20, out, &len, &yarrow_prng, prng_idx, hash_idx, 0, &key));
  362. /* export key and import as both private and public */
  363. len2 = sizeof(tmp);
  364. DO(rsa_export(tmp, &len2, PK_PRIVATE, &key));
  365. DO(rsa_import(tmp, len2, &privKey));
  366. len2 = sizeof(tmp);
  367. DO(rsa_export(tmp, &len2, PK_PUBLIC, &key));
  368. DO(rsa_import(tmp, len2, &pubKey));
  369. /* verify with original */
  370. DO(rsa_verify_hash(out, len, in, 20, hash_idx, 0, &stat, &key));
  371. /* change a byte */
  372. in[0] ^= 1;
  373. DO(rsa_verify_hash(out, len, in, 20, hash_idx, 0, &stat2, &key));
  374. if (!(stat == 1 && stat2 == 0)) {
  375. fprintf(stderr, "rsa_verify_hash (unsalted, origKey) failed, %d, %d", stat, stat2);
  376. rsa_free(&key);
  377. rsa_free(&pubKey);
  378. rsa_free(&privKey);
  379. return 1;
  380. }
  381. /* verify with privKey */
  382. /* change byte back to original */
  383. in[0] ^= 1;
  384. DO(rsa_verify_hash(out, len, in, 20, hash_idx, 0, &stat, &privKey));
  385. /* change a byte */
  386. in[0] ^= 1;
  387. DO(rsa_verify_hash(out, len, in, 20, hash_idx, 0, &stat2, &privKey));
  388. if (!(stat == 1 && stat2 == 0)) {
  389. fprintf(stderr, "rsa_verify_hash (unsalted, privKey) failed, %d, %d", stat, stat2);
  390. rsa_free(&key);
  391. rsa_free(&pubKey);
  392. rsa_free(&privKey);
  393. return 1;
  394. }
  395. /* verify with privKey but remove pointer to dP to test without CRT */
  396. dP = privKey.dP;
  397. privKey.dP = NULL;
  398. /* change byte back to original */
  399. in[0] ^= 1;
  400. DO(rsa_verify_hash(out, len, in, 20, hash_idx, 0, &stat, &privKey));
  401. /* change a byte */
  402. in[0] ^= 1;
  403. DO(rsa_verify_hash(out, len, in, 20, hash_idx, 0, &stat2, &privKey));
  404. if (!(stat == 1 && stat2 == 0)) {
  405. fprintf(stderr, "rsa_verify_hash (unsalted, privKey) failed, %d, %d", stat, stat2);
  406. rsa_free(&key);
  407. rsa_free(&pubKey);
  408. rsa_free(&privKey);
  409. return 1;
  410. }
  411. privKey.dP = dP;
  412. /* verify with pubKey */
  413. /* change byte back to original */
  414. in[0] ^= 1;
  415. DO(rsa_verify_hash(out, len, in, 20, hash_idx, 0, &stat, &pubKey));
  416. /* change a byte */
  417. in[0] ^= 1;
  418. DO(rsa_verify_hash(out, len, in, 20, hash_idx, 0, &stat2, &pubKey));
  419. if (!(stat == 1 && stat2 == 0)) {
  420. fprintf(stderr, "rsa_verify_hash (unsalted, pubkey) failed, %d, %d", stat, stat2);
  421. rsa_free(&key);
  422. rsa_free(&pubKey);
  423. rsa_free(&privKey);
  424. return 1;
  425. }
  426. /* sign a message (salted) now (use privKey to make, pubKey to verify) */
  427. len = sizeof(out);
  428. DO(rsa_sign_hash(in, 20, out, &len, &yarrow_prng, prng_idx, hash_idx, 8, &privKey));
  429. DO(rsa_verify_hash(out, len, in, 20, hash_idx, 8, &stat, &pubKey));
  430. /* change a byte */
  431. in[0] ^= 1;
  432. DO(rsa_verify_hash(out, len, in, 20, hash_idx, 8, &stat2, &pubKey));
  433. if (!(stat == 1 && stat2 == 0)) {
  434. fprintf(stderr, "rsa_verify_hash (salted) failed, %d, %d", stat, stat2);
  435. rsa_free(&key);
  436. rsa_free(&pubKey);
  437. rsa_free(&privKey);
  438. return 1;
  439. }
  440. /* sign a message with PKCS #1 v1.5 */
  441. len = sizeof(out);
  442. DO(rsa_sign_hash_ex(in, 20, out, &len, LTC_PKCS_1_V1_5, &yarrow_prng, prng_idx, hash_idx, 8, &privKey));
  443. DO(rsa_verify_hash_ex(out, len, in, 20, LTC_PKCS_1_V1_5, hash_idx, 8, &stat, &pubKey));
  444. /* change a byte */
  445. in[0] ^= 1;
  446. DO(rsa_verify_hash_ex(out, len, in, 20, LTC_PKCS_1_V1_5, hash_idx, 8, &stat2, &pubKey));
  447. if (!(stat == 1 && stat2 == 0)) {
  448. fprintf(stderr, "rsa_verify_hash_ex failed, %d, %d", stat, stat2);
  449. rsa_free(&key);
  450. rsa_free(&pubKey);
  451. rsa_free(&privKey);
  452. return 1;
  453. }
  454. /* Testcase for Bleichenbacher attack
  455. *
  456. * (1) Create a valid signature
  457. * (2) Check that it can be verified
  458. * (3) Decrypt the package to fetch plain text
  459. * (4) Forge the structure of PKCS#1-EMSA encoded data
  460. * (4.1) Search for start and end of the padding string
  461. * (4.2) Move the signature to the front of the padding string
  462. * (4.3) Zero the message until the end
  463. * (5) Encrypt the package again
  464. * (6) Profit :)
  465. * For PS lengths < 8: the verification process should fail
  466. * For PS lengths >= 8: the verification process should succeed
  467. * For all PS lengths: the result should not be valid
  468. */
  469. p = in;
  470. p2 = out;
  471. p3 = tmp;
  472. for (i = 0; i < 9; ++i) {
  473. len = sizeof(in);
  474. len2 = sizeof(out);
  475. /* (1) */
  476. DO(rsa_sign_hash_ex(p, 20, p2, &len2, LTC_PKCS_1_V1_5, &yarrow_prng, prng_idx, hash_idx, 8, &privKey));
  477. /* (2) */
  478. DOX(rsa_verify_hash_ex(p2, len2, p, 20, LTC_PKCS_1_V1_5, hash_idx, -1, &stat, &pubKey), "should succeed");
  479. DOX(stat == 1?CRYPT_OK:CRYPT_FAIL_TESTVECTOR, "should succeed");
  480. len3 = sizeof(tmp);
  481. /* (3) */
  482. DO(ltc_mp.rsa_me(p2, len2, p3, &len3, PK_PUBLIC, &key));
  483. /* (4) */
  484. #ifdef LTC_TEST_DBG
  485. cnt = rsa_get_size(&key);
  486. printf("\nBefore:");
  487. for (cnt = 0; cnt < len3; ++cnt) {
  488. if (cnt%32 == 0)
  489. printf("\n%3lu:", cnt);
  490. printf(" %02x", p3[cnt]);
  491. }
  492. #endif
  493. /* (4.1) */
  494. for (cnt = 0; cnt < len3; ++cnt) {
  495. if (p3[cnt] == 0xff)
  496. break;
  497. }
  498. for (cnt2 = cnt+1; cnt2 < len3; ++cnt2) {
  499. if (p3[cnt2] != 0xff)
  500. break;
  501. }
  502. /* (4.2) */
  503. memmove(&p3[cnt+i], &p3[cnt2], len3-cnt2);
  504. /* (4.3) */
  505. for (cnt = cnt + len3-cnt2+i; cnt < len; ++cnt) {
  506. p3[cnt] = 0;
  507. }
  508. #ifdef LTC_TEST_DBG
  509. printf("\nAfter:");
  510. for (cnt = 0; cnt < len3; ++cnt) {
  511. if (cnt%32 == 0)
  512. printf("\n%3lu:", cnt);
  513. printf(" %02x", p3[cnt]);
  514. }
  515. printf("\n");
  516. #endif
  517. len2 = sizeof(out);
  518. /* (5) */
  519. DO(ltc_mp.rsa_me(p3, len3, p2, &len2, PK_PRIVATE, &key));
  520. len3 = sizeof(tmp);
  521. /* (6) */
  522. if (i < 8)
  523. DOX(rsa_verify_hash_ex(p2, len2, p, 20, LTC_PKCS_1_V1_5, hash_idx, -1, &stat, &pubKey)
  524. == CRYPT_INVALID_PACKET ? CRYPT_OK:CRYPT_INVALID_PACKET, "should fail");
  525. else
  526. DOX(rsa_verify_hash_ex(p2, len2, p, 20, LTC_PKCS_1_V1_5, hash_idx, -1, &stat, &pubKey), "should succeed");
  527. DOX(stat == 0?CRYPT_OK:CRYPT_FAIL_TESTVECTOR, "should fail");
  528. }
  529. rsa_free(&key);
  530. /* try reading the public RSA key from a X509 certificate */
  531. len3 = sizeof(tmp);
  532. DO(base64_decode(x509_public_rsa, sizeof(x509_public_rsa), tmp, &len3));
  533. DO(rsa_import_x509(tmp, len3, &key));
  534. len = sizeof(tmp);
  535. DO(rsa_export(tmp, &len, PK_PUBLIC, &key));
  536. if (len != sizeof(openssl_public_rsa_stripped) || memcmp(tmp, openssl_public_rsa_stripped, len)) {
  537. fprintf(stderr, "RSA public export failed to match rsa_import_x509\n");
  538. return 1;
  539. }
  540. rsa_free(&key);
  541. len3 = sizeof(tmp);
  542. DO(base64_decode(_der_tests_cacert_root_cert, _der_tests_cacert_root_cert_size, tmp, &len3));
  543. DO(rsa_import_x509(tmp, len3, &key));
  544. /* free the key and return */
  545. rsa_free(&key);
  546. rsa_free(&pubKey);
  547. rsa_free(&privKey);
  548. return 0;
  549. }
  550. #else
  551. int rsa_test(void)
  552. {
  553. return CRYPT_NOP;
  554. }
  555. #endif
  556. /* $Source$ */
  557. /* $Revision$ */
  558. /* $Date$ */