rsa_test.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. /* LibTomCrypt, modular cryptographic library -- Tom St Denis
  2. *
  3. * LibTomCrypt is a library that provides various cryptographic
  4. * algorithms in a highly modular and flexible manner.
  5. *
  6. * The library is free for all purposes without any express
  7. * guarantee it works.
  8. */
  9. #include <tomcrypt_test.h>
  10. #if defined(LTC_MRSA)
  11. #include <sys/stat.h>
  12. #include <sys/types.h>
  13. #include <dirent.h>
  14. #define RSA_MSGSIZE 78
  15. /* These are test keys [see file test.key] that I use to test my import/export against */
  16. static const unsigned char openssl_private_rsa[] = {
  17. 0x30, 0x82, 0x02, 0x5e, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0xcf, 0x9a, 0xde, 0x64, 0x8a,
  18. 0xda, 0xc8, 0x33, 0x20, 0xa9, 0xd7, 0x83, 0x31, 0x19, 0x54, 0xb2, 0x9a, 0x85, 0xa7, 0xa1, 0xb7,
  19. 0x75, 0x33, 0xb6, 0xa9, 0xac, 0x84, 0x24, 0xb3, 0xde, 0xdb, 0x7d, 0x85, 0x2d, 0x96, 0x65, 0xe5,
  20. 0x3f, 0x72, 0x95, 0x24, 0x9f, 0x28, 0x68, 0xca, 0x4f, 0xdb, 0x44, 0x1c, 0x3e, 0x60, 0x12, 0x8a,
  21. 0xdd, 0x26, 0xa5, 0xeb, 0xff, 0x0b, 0x5e, 0xd4, 0x88, 0x38, 0x49, 0x2a, 0x6e, 0x5b, 0xbf, 0x12,
  22. 0x37, 0x47, 0xbd, 0x05, 0x6b, 0xbc, 0xdb, 0xf3, 0xee, 0xe4, 0x11, 0x8e, 0x41, 0x68, 0x7c, 0x61,
  23. 0x13, 0xd7, 0x42, 0xc8, 0x80, 0xbe, 0x36, 0x8f, 0xdc, 0x08, 0x8b, 0x4f, 0xac, 0xa4, 0xe2, 0x76,
  24. 0x0c, 0xc9, 0x63, 0x6c, 0x49, 0x58, 0x93, 0xed, 0xcc, 0xaa, 0xdc, 0x25, 0x3b, 0x0a, 0x60, 0x3f,
  25. 0x8b, 0x54, 0x3a, 0xc3, 0x4d, 0x31, 0xe7, 0x94, 0xa4, 0x44, 0xfd, 0x02, 0x03, 0x01, 0x00, 0x01,
  26. 0x02, 0x81, 0x81, 0x00, 0xc8, 0x62, 0xb9, 0xea, 0xde, 0x44, 0x53, 0x1d, 0x56, 0x97, 0xd9, 0x97,
  27. 0x9e, 0x1a, 0xcf, 0x30, 0x1e, 0x0a, 0x88, 0x45, 0x86, 0x29, 0x30, 0xa3, 0x4d, 0x9f, 0x61, 0x65,
  28. 0x73, 0xe0, 0xd6, 0x87, 0x8f, 0xb6, 0xf3, 0x06, 0xa3, 0x82, 0xdc, 0x7c, 0xac, 0xfe, 0x9b, 0x28,
  29. 0x9a, 0xae, 0xfd, 0xfb, 0xfe, 0x2f, 0x0e, 0xd8, 0x97, 0x04, 0xe3, 0xbb, 0x1f, 0xd1, 0xec, 0x0d,
  30. 0xba, 0xa3, 0x49, 0x7f, 0x47, 0xac, 0x8a, 0x44, 0x04, 0x7e, 0x86, 0xb7, 0x39, 0x42, 0x3f, 0xad,
  31. 0x1e, 0xb7, 0x0e, 0xa5, 0x51, 0xf4, 0x40, 0x63, 0x1e, 0xfd, 0xbd, 0xea, 0x9f, 0x41, 0x9f, 0xa8,
  32. 0x90, 0x1d, 0x6f, 0x0a, 0x5a, 0x95, 0x13, 0x11, 0x0d, 0x80, 0xaf, 0x5f, 0x64, 0x98, 0x8a, 0x2c,
  33. 0x78, 0x68, 0x65, 0xb0, 0x2b, 0x8b, 0xa2, 0x53, 0x87, 0xca, 0xf1, 0x64, 0x04, 0xab, 0xf2, 0x7b,
  34. 0xdb, 0x83, 0xc8, 0x81, 0x02, 0x41, 0x00, 0xf7, 0xbe, 0x5e, 0x23, 0xc3, 0x32, 0x3f, 0xbf, 0x8b,
  35. 0x8e, 0x3a, 0xee, 0xfc, 0xfc, 0xcb, 0xe5, 0xf7, 0xf1, 0x0b, 0xbc, 0x42, 0x82, 0xae, 0xd5, 0x7a,
  36. 0x3e, 0xca, 0xf7, 0xd5, 0x69, 0x3f, 0x64, 0x25, 0xa2, 0x1f, 0xb7, 0x75, 0x75, 0x05, 0x92, 0x42,
  37. 0xeb, 0xb8, 0xf1, 0xf3, 0x0a, 0x05, 0xe3, 0x94, 0xd1, 0x55, 0x78, 0x35, 0xa0, 0x36, 0xa0, 0x9b,
  38. 0x7c, 0x92, 0x84, 0x6c, 0xdd, 0xdc, 0x4d, 0x02, 0x41, 0x00, 0xd6, 0x86, 0x0e, 0x85, 0x42, 0x0b,
  39. 0x04, 0x08, 0x84, 0x21, 0x60, 0xf0, 0x0e, 0x0d, 0x88, 0xfd, 0x1e, 0x36, 0x10, 0x65, 0x4f, 0x1e,
  40. 0x53, 0xb4, 0x08, 0x72, 0x80, 0x5c, 0x3f, 0x59, 0x66, 0x17, 0xe6, 0x98, 0xf2, 0xe9, 0x6c, 0x7a,
  41. 0x06, 0x4c, 0xac, 0x76, 0x3d, 0xed, 0x8c, 0xa1, 0xce, 0xad, 0x1b, 0xbd, 0xb4, 0x7d, 0x28, 0xbc,
  42. 0xe3, 0x0e, 0x38, 0x8d, 0x99, 0xd8, 0x05, 0xb5, 0xa3, 0x71, 0x02, 0x40, 0x6d, 0xeb, 0xc3, 0x2d,
  43. 0x2e, 0xf0, 0x5e, 0xa4, 0x88, 0x31, 0x05, 0x29, 0x00, 0x8a, 0xd1, 0x95, 0x29, 0x9b, 0x83, 0xcf,
  44. 0x75, 0xdb, 0x31, 0xe3, 0x7a, 0x27, 0xde, 0x3a, 0x74, 0x30, 0x0c, 0x76, 0x4c, 0xd4, 0x50, 0x2a,
  45. 0x40, 0x2d, 0x39, 0xd9, 0x99, 0x63, 0xa9, 0x5d, 0x80, 0xae, 0x53, 0xca, 0x94, 0x3f, 0x05, 0x23,
  46. 0x1e, 0xf8, 0x05, 0x04, 0xe1, 0xb8, 0x35, 0xf2, 0x17, 0xb3, 0xa0, 0x89, 0x02, 0x41, 0x00, 0xab,
  47. 0x90, 0x88, 0xfa, 0x60, 0x08, 0x29, 0x50, 0x9a, 0x43, 0x8b, 0xa0, 0x50, 0xcc, 0xd8, 0x5a, 0xfe,
  48. 0x97, 0x64, 0x63, 0x71, 0x74, 0x22, 0xa3, 0x20, 0x02, 0x5a, 0xcf, 0xeb, 0xc6, 0x16, 0x95, 0x54,
  49. 0xd1, 0xcb, 0xab, 0x8d, 0x1a, 0xc6, 0x00, 0xfa, 0x08, 0x92, 0x9c, 0x71, 0xd5, 0x52, 0x52, 0x35,
  50. 0x96, 0x71, 0x4b, 0x8b, 0x92, 0x0c, 0xd0, 0xe9, 0xbf, 0xad, 0x63, 0x0b, 0xa5, 0xe9, 0xb1, 0x02,
  51. 0x41, 0x00, 0xdc, 0xcc, 0x27, 0xc8, 0xe4, 0xdc, 0x62, 0x48, 0xd5, 0x9b, 0xaf, 0xf5, 0xab, 0x60,
  52. 0xf6, 0x21, 0xfd, 0x53, 0xe2, 0xb7, 0x5d, 0x09, 0xc9, 0x1a, 0xa1, 0x04, 0xa9, 0xfc, 0x61, 0x2c,
  53. 0x5d, 0x04, 0x58, 0x3a, 0x5a, 0x39, 0xf1, 0x4a, 0x21, 0x56, 0x67, 0xfd, 0xcc, 0x20, 0xa3, 0x8f,
  54. 0x78, 0x18, 0x5a, 0x79, 0x3d, 0x2e, 0x8e, 0x7e, 0x86, 0x0a, 0xe6, 0xa8, 0x33, 0xc1, 0x04, 0x17,
  55. 0x4a, 0x9f, };
  56. static const unsigned char x509_public_rsa[] =
  57. "MIICdTCCAd4CCQCYjCwz0l9JpjANBgkqhkiG9w0BAQsFADB+MQswCQYDVQQGEwJD\
  58. WjEPMA0GA1UECAwGTW9yYXZhMQ0wCwYDVQQHDARCcm5vMRAwDgYDVQQKDAdMVEMg\
  59. THRkMQ8wDQYDVQQLDAZDcnlwdG8xEjAQBgNVBAMMCVRlc3QgQ2VydDEYMBYGCSqG\
  60. SIb3DQEJARYJdGVzdEBjZXJ0MCAXDTE3MDMwOTIzNDMzOVoYDzIyOTAxMjIyMjM0\
  61. MzM5WjB+MQswCQYDVQQGEwJDWjEPMA0GA1UECAwGTW9yYXZhMQ0wCwYDVQQHDARC\
  62. cm5vMRAwDgYDVQQKDAdMVEMgTHRkMQ8wDQYDVQQLDAZDcnlwdG8xEjAQBgNVBAMM\
  63. CVRlc3QgQ2VydDEYMBYGCSqGSIb3DQEJARYJdGVzdEBjZXJ0MIGfMA0GCSqGSIb3\
  64. DQEBAQUAA4GNADCBiQKBgQDPmt5kitrIMyCp14MxGVSymoWnobd1M7aprIQks97b\
  65. fYUtlmXlP3KVJJ8oaMpP20QcPmASit0mpev/C17UiDhJKm5bvxI3R70Fa7zb8+7k\
  66. EY5BaHxhE9dCyIC+No/cCItPrKTidgzJY2xJWJPtzKrcJTsKYD+LVDrDTTHnlKRE\
  67. /QIDAQABMA0GCSqGSIb3DQEBCwUAA4GBAApwWqupmmLGHeKOLFLcthQpAXXYep6T\
  68. 3S3e8X7fIG6TGhfvn5DHn+/V/C4184oOCwImI+VYRokdXdQ1AMGfVUomHJxsFPia\
  69. bv5Aw3hiKsIG3jigKHwmMScgkl3yn+8hLkx6thNbqQoa6Yyo20RqaEFBwlZ5G8lF\
  70. rZsdeO84SeCH";
  71. static const unsigned char pkcs8_private_rsa[] = {
  72. 0x30, 0x82, 0x02, 0x78, 0x02, 0x01, 0x00, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
  73. 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82, 0x02, 0x62, 0x30, 0x82, 0x02, 0x5e, 0x02, 0x01,
  74. 0x00, 0x02, 0x81, 0x81, 0x00, 0xcf, 0x9a, 0xde, 0x64, 0x8a, 0xda, 0xc8, 0x33, 0x20, 0xa9, 0xd7,
  75. 0x83, 0x31, 0x19, 0x54, 0xb2, 0x9a, 0x85, 0xa7, 0xa1, 0xb7, 0x75, 0x33, 0xb6, 0xa9, 0xac, 0x84,
  76. 0x24, 0xb3, 0xde, 0xdb, 0x7d, 0x85, 0x2d, 0x96, 0x65, 0xe5, 0x3f, 0x72, 0x95, 0x24, 0x9f, 0x28,
  77. 0x68, 0xca, 0x4f, 0xdb, 0x44, 0x1c, 0x3e, 0x60, 0x12, 0x8a, 0xdd, 0x26, 0xa5, 0xeb, 0xff, 0x0b,
  78. 0x5e, 0xd4, 0x88, 0x38, 0x49, 0x2a, 0x6e, 0x5b, 0xbf, 0x12, 0x37, 0x47, 0xbd, 0x05, 0x6b, 0xbc,
  79. 0xdb, 0xf3, 0xee, 0xe4, 0x11, 0x8e, 0x41, 0x68, 0x7c, 0x61, 0x13, 0xd7, 0x42, 0xc8, 0x80, 0xbe,
  80. 0x36, 0x8f, 0xdc, 0x08, 0x8b, 0x4f, 0xac, 0xa4, 0xe2, 0x76, 0x0c, 0xc9, 0x63, 0x6c, 0x49, 0x58,
  81. 0x93, 0xed, 0xcc, 0xaa, 0xdc, 0x25, 0x3b, 0x0a, 0x60, 0x3f, 0x8b, 0x54, 0x3a, 0xc3, 0x4d, 0x31,
  82. 0xe7, 0x94, 0xa4, 0x44, 0xfd, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x81, 0x81, 0x00, 0xc8, 0x62,
  83. 0xb9, 0xea, 0xde, 0x44, 0x53, 0x1d, 0x56, 0x97, 0xd9, 0x97, 0x9e, 0x1a, 0xcf, 0x30, 0x1e, 0x0a,
  84. 0x88, 0x45, 0x86, 0x29, 0x30, 0xa3, 0x4d, 0x9f, 0x61, 0x65, 0x73, 0xe0, 0xd6, 0x87, 0x8f, 0xb6,
  85. 0xf3, 0x06, 0xa3, 0x82, 0xdc, 0x7c, 0xac, 0xfe, 0x9b, 0x28, 0x9a, 0xae, 0xfd, 0xfb, 0xfe, 0x2f,
  86. 0x0e, 0xd8, 0x97, 0x04, 0xe3, 0xbb, 0x1f, 0xd1, 0xec, 0x0d, 0xba, 0xa3, 0x49, 0x7f, 0x47, 0xac,
  87. 0x8a, 0x44, 0x04, 0x7e, 0x86, 0xb7, 0x39, 0x42, 0x3f, 0xad, 0x1e, 0xb7, 0x0e, 0xa5, 0x51, 0xf4,
  88. 0x40, 0x63, 0x1e, 0xfd, 0xbd, 0xea, 0x9f, 0x41, 0x9f, 0xa8, 0x90, 0x1d, 0x6f, 0x0a, 0x5a, 0x95,
  89. 0x13, 0x11, 0x0d, 0x80, 0xaf, 0x5f, 0x64, 0x98, 0x8a, 0x2c, 0x78, 0x68, 0x65, 0xb0, 0x2b, 0x8b,
  90. 0xa2, 0x53, 0x87, 0xca, 0xf1, 0x64, 0x04, 0xab, 0xf2, 0x7b, 0xdb, 0x83, 0xc8, 0x81, 0x02, 0x41,
  91. 0x00, 0xf7, 0xbe, 0x5e, 0x23, 0xc3, 0x32, 0x3f, 0xbf, 0x8b, 0x8e, 0x3a, 0xee, 0xfc, 0xfc, 0xcb,
  92. 0xe5, 0xf7, 0xf1, 0x0b, 0xbc, 0x42, 0x82, 0xae, 0xd5, 0x7a, 0x3e, 0xca, 0xf7, 0xd5, 0x69, 0x3f,
  93. 0x64, 0x25, 0xa2, 0x1f, 0xb7, 0x75, 0x75, 0x05, 0x92, 0x42, 0xeb, 0xb8, 0xf1, 0xf3, 0x0a, 0x05,
  94. 0xe3, 0x94, 0xd1, 0x55, 0x78, 0x35, 0xa0, 0x36, 0xa0, 0x9b, 0x7c, 0x92, 0x84, 0x6c, 0xdd, 0xdc,
  95. 0x4d, 0x02, 0x41, 0x00, 0xd6, 0x86, 0x0e, 0x85, 0x42, 0x0b, 0x04, 0x08, 0x84, 0x21, 0x60, 0xf0,
  96. 0x0e, 0x0d, 0x88, 0xfd, 0x1e, 0x36, 0x10, 0x65, 0x4f, 0x1e, 0x53, 0xb4, 0x08, 0x72, 0x80, 0x5c,
  97. 0x3f, 0x59, 0x66, 0x17, 0xe6, 0x98, 0xf2, 0xe9, 0x6c, 0x7a, 0x06, 0x4c, 0xac, 0x76, 0x3d, 0xed,
  98. 0x8c, 0xa1, 0xce, 0xad, 0x1b, 0xbd, 0xb4, 0x7d, 0x28, 0xbc, 0xe3, 0x0e, 0x38, 0x8d, 0x99, 0xd8,
  99. 0x05, 0xb5, 0xa3, 0x71, 0x02, 0x40, 0x6d, 0xeb, 0xc3, 0x2d, 0x2e, 0xf0, 0x5e, 0xa4, 0x88, 0x31,
  100. 0x05, 0x29, 0x00, 0x8a, 0xd1, 0x95, 0x29, 0x9b, 0x83, 0xcf, 0x75, 0xdb, 0x31, 0xe3, 0x7a, 0x27,
  101. 0xde, 0x3a, 0x74, 0x30, 0x0c, 0x76, 0x4c, 0xd4, 0x50, 0x2a, 0x40, 0x2d, 0x39, 0xd9, 0x99, 0x63,
  102. 0xa9, 0x5d, 0x80, 0xae, 0x53, 0xca, 0x94, 0x3f, 0x05, 0x23, 0x1e, 0xf8, 0x05, 0x04, 0xe1, 0xb8,
  103. 0x35, 0xf2, 0x17, 0xb3, 0xa0, 0x89, 0x02, 0x41, 0x00, 0xab, 0x90, 0x88, 0xfa, 0x60, 0x08, 0x29,
  104. 0x50, 0x9a, 0x43, 0x8b, 0xa0, 0x50, 0xcc, 0xd8, 0x5a, 0xfe, 0x97, 0x64, 0x63, 0x71, 0x74, 0x22,
  105. 0xa3, 0x20, 0x02, 0x5a, 0xcf, 0xeb, 0xc6, 0x16, 0x95, 0x54, 0xd1, 0xcb, 0xab, 0x8d, 0x1a, 0xc6,
  106. 0x00, 0xfa, 0x08, 0x92, 0x9c, 0x71, 0xd5, 0x52, 0x52, 0x35, 0x96, 0x71, 0x4b, 0x8b, 0x92, 0x0c,
  107. 0xd0, 0xe9, 0xbf, 0xad, 0x63, 0x0b, 0xa5, 0xe9, 0xb1, 0x02, 0x41, 0x00, 0xdc, 0xcc, 0x27, 0xc8,
  108. 0xe4, 0xdc, 0x62, 0x48, 0xd5, 0x9b, 0xaf, 0xf5, 0xab, 0x60, 0xf6, 0x21, 0xfd, 0x53, 0xe2, 0xb7,
  109. 0x5d, 0x09, 0xc9, 0x1a, 0xa1, 0x04, 0xa9, 0xfc, 0x61, 0x2c, 0x5d, 0x04, 0x58, 0x3a, 0x5a, 0x39,
  110. 0xf1, 0x4a, 0x21, 0x56, 0x67, 0xfd, 0xcc, 0x20, 0xa3, 0x8f, 0x78, 0x18, 0x5a, 0x79, 0x3d, 0x2e,
  111. 0x8e, 0x7e, 0x86, 0x0a, 0xe6, 0xa8, 0x33, 0xc1, 0x04, 0x17, 0x4a, 0x9f };
  112. /* private key - hexadecimal */
  113. enum {
  114. pk_d ,
  115. pk_dP,
  116. pk_dQ,
  117. pk_e ,
  118. pk_N ,
  119. pk_p ,
  120. pk_q ,
  121. pk_qP,
  122. };
  123. static const char *hex_key[] = {
  124. "C862B9EADE44531D5697D9979E1ACF301E0A8845862930A34D9F616573E0D6878FB6F306A382DC7CACFE9B289AAEFDFBFE2F0ED89704E3BB1FD1EC0DBAA3497F47AC8A44047E86B739423FAD1EB70EA551F440631EFDBDEA9F419FA8901D6F0A5A9513110D80AF5F64988A2C786865B02B8BA25387CAF16404ABF27BDB83C881",
  125. "6DEBC32D2EF05EA488310529008AD195299B83CF75DB31E37A27DE3A74300C764CD4502A402D39D99963A95D80AE53CA943F05231EF80504E1B835F217B3A089",
  126. "AB9088FA600829509A438BA050CCD85AFE976463717422A320025ACFEBC6169554D1CBAB8D1AC600FA08929C71D552523596714B8B920CD0E9BFAD630BA5E9B1",
  127. "010001",
  128. "CF9ADE648ADAC83320A9D783311954B29A85A7A1B77533B6A9AC8424B3DEDB7D852D9665E53F7295249F2868CA4FDB441C3E60128ADD26A5EBFF0B5ED48838492A6E5BBF123747BD056BBCDBF3EEE4118E41687C6113D742C880BE368FDC088B4FACA4E2760CC9636C495893EDCCAADC253B0A603F8B543AC34D31E794A444FD",
  129. "F7BE5E23C3323FBF8B8E3AEEFCFCCBE5F7F10BBC4282AED57A3ECAF7D5693F6425A21FB77575059242EBB8F1F30A05E394D1557835A036A09B7C92846CDDDC4D",
  130. "D6860E85420B0408842160F00E0D88FD1E3610654F1E53B40872805C3F596617E698F2E96C7A064CAC763DED8CA1CEAD1BBDB47D28BCE30E388D99D805B5A371",
  131. "DCCC27C8E4DC6248D59BAFF5AB60F621FD53E2B75D09C91AA104A9FC612C5D04583A5A39F14A215667FDCC20A38F78185A793D2E8E7E860AE6A833C104174A9F" };
  132. /*** openssl public RSA key in DER format */
  133. static const unsigned char openssl_public_rsa[] = {
  134. 0x30, 0x81, 0x9f, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  135. 0x05, 0x00, 0x03, 0x81, 0x8d, 0x00, 0x30, 0x81, 0x89, 0x02, 0x81, 0x81, 0x00, 0xcf, 0x9a, 0xde,
  136. 0x64, 0x8a, 0xda, 0xc8, 0x33, 0x20, 0xa9, 0xd7, 0x83, 0x31, 0x19, 0x54, 0xb2, 0x9a, 0x85, 0xa7,
  137. 0xa1, 0xb7, 0x75, 0x33, 0xb6, 0xa9, 0xac, 0x84, 0x24, 0xb3, 0xde, 0xdb, 0x7d, 0x85, 0x2d, 0x96,
  138. 0x65, 0xe5, 0x3f, 0x72, 0x95, 0x24, 0x9f, 0x28, 0x68, 0xca, 0x4f, 0xdb, 0x44, 0x1c, 0x3e, 0x60,
  139. 0x12, 0x8a, 0xdd, 0x26, 0xa5, 0xeb, 0xff, 0x0b, 0x5e, 0xd4, 0x88, 0x38, 0x49, 0x2a, 0x6e, 0x5b,
  140. 0xbf, 0x12, 0x37, 0x47, 0xbd, 0x05, 0x6b, 0xbc, 0xdb, 0xf3, 0xee, 0xe4, 0x11, 0x8e, 0x41, 0x68,
  141. 0x7c, 0x61, 0x13, 0xd7, 0x42, 0xc8, 0x80, 0xbe, 0x36, 0x8f, 0xdc, 0x08, 0x8b, 0x4f, 0xac, 0xa4,
  142. 0xe2, 0x76, 0x0c, 0xc9, 0x63, 0x6c, 0x49, 0x58, 0x93, 0xed, 0xcc, 0xaa, 0xdc, 0x25, 0x3b, 0x0a,
  143. 0x60, 0x3f, 0x8b, 0x54, 0x3a, 0xc3, 0x4d, 0x31, 0xe7, 0x94, 0xa4, 0x44, 0xfd, 0x02, 0x03, 0x01,
  144. 0x00, 0x01, };
  145. /* same key but with extra headers stripped */
  146. static const unsigned char openssl_public_rsa_stripped[] = {
  147. 0x30, 0x81, 0x89, 0x02, 0x81, 0x81, 0x00, 0xcf, 0x9a, 0xde,
  148. 0x64, 0x8a, 0xda, 0xc8, 0x33, 0x20, 0xa9, 0xd7, 0x83, 0x31, 0x19, 0x54, 0xb2, 0x9a, 0x85, 0xa7,
  149. 0xa1, 0xb7, 0x75, 0x33, 0xb6, 0xa9, 0xac, 0x84, 0x24, 0xb3, 0xde, 0xdb, 0x7d, 0x85, 0x2d, 0x96,
  150. 0x65, 0xe5, 0x3f, 0x72, 0x95, 0x24, 0x9f, 0x28, 0x68, 0xca, 0x4f, 0xdb, 0x44, 0x1c, 0x3e, 0x60,
  151. 0x12, 0x8a, 0xdd, 0x26, 0xa5, 0xeb, 0xff, 0x0b, 0x5e, 0xd4, 0x88, 0x38, 0x49, 0x2a, 0x6e, 0x5b,
  152. 0xbf, 0x12, 0x37, 0x47, 0xbd, 0x05, 0x6b, 0xbc, 0xdb, 0xf3, 0xee, 0xe4, 0x11, 0x8e, 0x41, 0x68,
  153. 0x7c, 0x61, 0x13, 0xd7, 0x42, 0xc8, 0x80, 0xbe, 0x36, 0x8f, 0xdc, 0x08, 0x8b, 0x4f, 0xac, 0xa4,
  154. 0xe2, 0x76, 0x0c, 0xc9, 0x63, 0x6c, 0x49, 0x58, 0x93, 0xed, 0xcc, 0xaa, 0xdc, 0x25, 0x3b, 0x0a,
  155. 0x60, 0x3f, 0x8b, 0x54, 0x3a, 0xc3, 0x4d, 0x31, 0xe7, 0x94, 0xa4, 0x44, 0xfd, 0x02, 0x03, 0x01,
  156. 0x00, 0x01, };
  157. /* generated with the private key above as:
  158. echo -n 'test' | openssl rsautl -sign -inkey rsa_private.pem -pkcs -hexdump
  159. */
  160. static const unsigned char openssl_rsautl_pkcs[] = {
  161. 0x24, 0xef, 0x54, 0xea, 0x1a, 0x12, 0x0c, 0xf4, 0x04, 0x0c, 0x48, 0xc8, 0xe8, 0x17, 0xd2, 0x6f,
  162. 0xc3, 0x41, 0xb3, 0x97, 0x5c, 0xbc, 0xa3, 0x2d, 0x21, 0x00, 0x10, 0x0e, 0xbb, 0xf7, 0x30, 0x21,
  163. 0x7e, 0x12, 0xd2, 0xdf, 0x26, 0x28, 0xd8, 0x0f, 0x6d, 0x4d, 0xc8, 0x4d, 0xa8, 0x78, 0xe7, 0x03,
  164. 0xee, 0xbc, 0x68, 0xba, 0x98, 0xea, 0xe9, 0xb6, 0x06, 0x8d, 0x85, 0x5b, 0xdb, 0xa6, 0x49, 0x86,
  165. 0x6f, 0xc7, 0x3d, 0xe0, 0x53, 0x83, 0xe0, 0xea, 0xb1, 0x08, 0x6a, 0x7b, 0xbd, 0xeb, 0xb5, 0x4a,
  166. 0xdd, 0xbc, 0x64, 0x97, 0x8c, 0x17, 0x20, 0xa3, 0x5c, 0xd4, 0xb8, 0x87, 0x43, 0xc5, 0x13, 0xad,
  167. 0x41, 0x6e, 0x45, 0x41, 0x32, 0xd4, 0x09, 0x12, 0x7f, 0xdc, 0x59, 0x1f, 0x28, 0x3f, 0x1e, 0xbc,
  168. 0xef, 0x57, 0x23, 0x4b, 0x3a, 0xa3, 0x24, 0x91, 0x4d, 0xfb, 0xb2, 0xd4, 0xe7, 0x5e, 0x41, 0x7e,
  169. };
  170. extern const unsigned char _der_tests_cacert_root_cert[];
  171. extern const unsigned long _der_tests_cacert_root_cert_size;
  172. static int rsa_compat_test(void)
  173. {
  174. rsa_key key, pubkey;
  175. int stat, i;
  176. unsigned char buf[1024], key_parts[8][128];
  177. unsigned long len, key_lens[8];
  178. /* try reading the key */
  179. DO(rsa_import(openssl_private_rsa, sizeof(openssl_private_rsa), &key));
  180. DO(rsa_import(openssl_public_rsa, sizeof(openssl_public_rsa), &pubkey));
  181. /* sign-verify a message with PKCS #1 v1.5 no ASN.1 */
  182. len = sizeof(buf);
  183. DO(rsa_sign_hash_ex((unsigned char*)"test", 4, buf, &len, LTC_PKCS_1_V1_5_NA1, NULL, 0, 0, 0, &key));
  184. if (len != sizeof(openssl_rsautl_pkcs) || memcmp(buf, openssl_rsautl_pkcs, len)) {
  185. fprintf(stderr, "RSA rsa_sign_hash_ex + LTC_PKCS_1_V1_5_NA1 failed\n");
  186. return 1;
  187. }
  188. stat = 0;
  189. 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));
  190. if (stat != 1) {
  191. fprintf(stderr, "RSA rsa_verify_hash_ex + 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. if (compare_testvector(buf, len, openssl_private_rsa, sizeof(openssl_private_rsa), "RSA private export (from OpenSSL)", 0)) {
  199. return 1;
  200. }
  201. len = sizeof(buf);
  202. DO(rsa_export(buf, &len, PK_PUBLIC, &key));
  203. if (compare_testvector(buf, len, openssl_public_rsa_stripped, sizeof(openssl_public_rsa_stripped), "RSA public export (from OpenSSL private key)", 0)) {
  204. return 1;
  205. }
  206. rsa_free(&key);
  207. /* try reading the public key */
  208. DO(rsa_import(openssl_public_rsa_stripped, sizeof(openssl_public_rsa_stripped), &key));
  209. len = sizeof(buf);
  210. DO(rsa_export(buf, &len, PK_PUBLIC, &key));
  211. if (compare_testvector(buf, len, openssl_public_rsa_stripped, sizeof(openssl_public_rsa_stripped), "RSA public export (from stripped OpenSSL)", 0)) {
  212. return 1;
  213. }
  214. rsa_free(&key);
  215. /* try reading the public key */
  216. DO(rsa_import(openssl_public_rsa, sizeof(openssl_public_rsa), &key));
  217. len = sizeof(buf);
  218. DO(rsa_export(buf, &len, PK_PUBLIC, &key));
  219. if (compare_testvector(buf, len, openssl_public_rsa_stripped, sizeof(openssl_public_rsa_stripped), "RSA public export (from OpenSSL)", 0)) {
  220. return 1;
  221. }
  222. rsa_free(&key);
  223. /* try import private key in pkcs8 format */
  224. DO(rsa_import_pkcs8(pkcs8_private_rsa, sizeof(pkcs8_private_rsa), NULL, 0, &key));
  225. len = sizeof(buf);
  226. DO(rsa_export(buf, &len, PK_PRIVATE, &key));
  227. if (compare_testvector(buf, len, openssl_private_rsa, sizeof(openssl_private_rsa), "RSA private export (from PKCS#8)", 0)) {
  228. return 1;
  229. }
  230. rsa_free(&key);
  231. /* convert raw hexadecimal numbers to binary */
  232. for (i = 0; i < 8; ++i) {
  233. key_lens[i] = sizeof(key_parts[i]);
  234. DO(radix_to_bin(hex_key[i], 16, key_parts[i], &key_lens[i]));
  235. }
  236. /* try import private key from converted raw hexadecimal numbers */
  237. 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));
  238. DO(rsa_set_factors(key_parts[pk_p], key_lens[pk_p], key_parts[pk_q], key_lens[pk_q], &key));
  239. 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));
  240. len = sizeof(buf);
  241. DO(rsa_export(buf, &len, PK_PRIVATE, &key));
  242. if (compare_testvector(buf, len, openssl_private_rsa, sizeof(openssl_private_rsa), "RSA private export (from hex)", 0)) {
  243. return 1;
  244. }
  245. rsa_free(&key);
  246. /* try import public key from converted raw hexadecimal numbers */
  247. DO(rsa_set_key(key_parts[pk_N], key_lens[pk_N], key_parts[pk_e], key_lens[pk_e], NULL, 0, &key));
  248. len = sizeof(buf);
  249. DO(rsa_export(buf, &len, PK_PUBLIC, &key));
  250. if (compare_testvector(buf, len, openssl_public_rsa_stripped, sizeof(openssl_public_rsa_stripped), "RSA public export (from hex)", 0)) {
  251. return 1;
  252. }
  253. rsa_free(&key);
  254. /* try export in SubjectPublicKeyInfo format of the public key */
  255. DO(rsa_import(openssl_public_rsa, sizeof(openssl_public_rsa), &key));
  256. len = sizeof(buf);
  257. DO(rsa_export(buf, &len, PK_PUBLIC | PK_STD, &key));
  258. if (len != sizeof(openssl_public_rsa) || memcmp(buf, openssl_public_rsa, len)) {
  259. fprintf(stderr, "RSA(public) SSL public X.509 export failed to match OpenSSL output\n");
  260. print_hex("should", openssl_public_rsa, sizeof(openssl_public_rsa));
  261. print_hex("is", buf, len);
  262. return 1;
  263. }
  264. rsa_free(&key);
  265. return 0;
  266. }
  267. static int _rsa_key_cmp(const int should_type, const rsa_key *should, const rsa_key *is)
  268. {
  269. if(should_type != is->type)
  270. return CRYPT_ERROR;
  271. if(should_type == PK_PRIVATE) {
  272. if(mp_cmp(should->q, is->q) != LTC_MP_EQ)
  273. return CRYPT_ERROR;
  274. if(mp_cmp(should->p, is->p) != LTC_MP_EQ)
  275. return CRYPT_ERROR;
  276. if(mp_cmp(should->qP, is->qP) != LTC_MP_EQ)
  277. return CRYPT_ERROR;
  278. if(mp_cmp(should->dP, is->dP) != LTC_MP_EQ)
  279. return CRYPT_ERROR;
  280. if(mp_cmp(should->dQ, is->dQ) != LTC_MP_EQ)
  281. return CRYPT_ERROR;
  282. if(mp_cmp(should->d, is->d) != LTC_MP_EQ)
  283. return CRYPT_ERROR;
  284. }
  285. if(mp_cmp(should->N, is->N) != LTC_MP_EQ)
  286. return CRYPT_ERROR;
  287. if(mp_cmp(should->e, is->e) != LTC_MP_EQ)
  288. return CRYPT_ERROR;
  289. return CRYPT_OK;
  290. }
  291. static int _rsa_issue_301(int prng_idx)
  292. {
  293. rsa_key key, key_in;
  294. unsigned char buf[4096];
  295. unsigned long len;
  296. DO(rsa_make_key(&yarrow_prng, prng_idx, sizeof(buf)/8, 65537, &key));
  297. len = sizeof(buf);
  298. DO(rsa_export(buf, &len, PK_PRIVATE, &key));
  299. DO(rsa_import(buf, len, &key_in));
  300. DO(_rsa_key_cmp(PK_PRIVATE, &key, &key_in));
  301. rsa_free(&key_in);
  302. len = sizeof(buf);
  303. DO(rsa_export(buf, &len, PK_PUBLIC, &key));
  304. DO(rsa_import(buf, len, &key_in));
  305. DO(_rsa_key_cmp(PK_PUBLIC, &key, &key_in));
  306. rsa_free(&key_in);
  307. len = sizeof(buf);
  308. DO(rsa_export(buf, &len, PK_PUBLIC | PK_STD, &key));
  309. DO(rsa_import(buf, len, &key_in));
  310. DO(_rsa_key_cmp(PK_PUBLIC, &key, &key_in));
  311. rsa_free(&key_in);
  312. rsa_free(&key);
  313. return CRYPT_OK;
  314. }
  315. static off_t fsize(const char *filename)
  316. {
  317. struct stat st;
  318. if (stat(filename, &st) == 0) return st.st_size;
  319. return -1;
  320. }
  321. static int _rsa_size_test(void)
  322. {
  323. DIR *d = opendir("tests/rsa");
  324. struct dirent *de;
  325. char fname[PATH_MAX];
  326. void* buf = NULL;
  327. FILE *f = NULL;
  328. off_t fsz;
  329. unsigned long sz;
  330. int err = CRYPT_FILE_NOTFOUND;
  331. rsa_key k;
  332. if (d == NULL)
  333. return CRYPT_FILE_NOTFOUND;
  334. while((de = readdir(d)) != NULL) {
  335. fname[0] = '\0';
  336. if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0)
  337. continue;
  338. strcat(fname, "tests/rsa/");
  339. strcat(fname, de->d_name);
  340. fsz = fsize(fname);
  341. if (fsz == -1)
  342. break;
  343. /* here we use the filesize as indicator for the rsa size
  344. * that would fail to import for tfm because it's fixed-size
  345. */
  346. if ((strcmp(ltc_mp.name, "TomsFastMath") == 0) && (fsz > 2048)) {
  347. #if defined(LTC_TEST_DBG) && LTC_TEST_DBG > 1
  348. fprintf(stderr, "TomsFastMath skip: %s\n", fname);
  349. #endif
  350. continue;
  351. }
  352. #if defined(LTC_TEST_DBG) && LTC_TEST_DBG > 1
  353. fprintf(stderr, "Try to import %s\n", fname);
  354. #endif
  355. f = fopen(fname, "rb");
  356. sz = fsz;
  357. buf = XMALLOC(fsz);
  358. if (fread(buf, 1, sz, f) != sz) {
  359. err = CRYPT_ERROR;
  360. break;
  361. }
  362. if ((err = rsa_import_x509(buf, sz, &k)) == CRYPT_OK) {
  363. rsa_free(&k);
  364. } else {
  365. #if defined(LTC_TEST_DBG)
  366. fprintf(stderr, "Could not import RSA key of %s: %s\n\n", fname, error_to_string(err));
  367. #endif
  368. break;
  369. }
  370. XFREE(buf);
  371. buf = NULL;
  372. fclose(f);
  373. f = NULL;
  374. }
  375. if (buf != NULL) XFREE(buf);
  376. if (f != NULL) fclose(f);
  377. closedir(d);
  378. return err;
  379. }
  380. int rsa_test(void)
  381. {
  382. unsigned char in[1024], out[1024], tmp[3072];
  383. rsa_key key, privKey, pubKey;
  384. int hash_idx, prng_idx, stat, stat2, i, err;
  385. unsigned long rsa_msgsize, len, len2, len3, cnt, cnt2;
  386. static unsigned char lparam[] = { 0x01, 0x02, 0x03, 0x04 };
  387. void* dP;
  388. unsigned char* p;
  389. unsigned char* p2;
  390. unsigned char* p3;
  391. if (ltc_mp.name == NULL) return CRYPT_NOP;
  392. if (rsa_compat_test() != 0) {
  393. return 1;
  394. }
  395. hash_idx = find_hash("sha1");
  396. prng_idx = find_prng("yarrow");
  397. if (hash_idx == -1 || prng_idx == -1) {
  398. fprintf(stderr, "rsa_test requires LTC_SHA1 and yarrow");
  399. return 1;
  400. }
  401. DO(_rsa_size_test());
  402. DO(_rsa_issue_301(prng_idx));
  403. /* make 10 random key */
  404. for (cnt = 0; cnt < 10; cnt++) {
  405. DO(rsa_make_key(&yarrow_prng, prng_idx, 1024/8, 65537, &key));
  406. if (mp_count_bits(key.N) != 1024) {
  407. fprintf(stderr, "rsa_1024 key modulus has %d bits\n", mp_count_bits(key.N));
  408. len = mp_unsigned_bin_size(key.N);
  409. mp_to_unsigned_bin(key.N, tmp);
  410. print_hex("N", tmp, len);
  411. len = mp_unsigned_bin_size(key.p);
  412. mp_to_unsigned_bin(key.p, tmp);
  413. print_hex("p", tmp, len);
  414. len = mp_unsigned_bin_size(key.q);
  415. mp_to_unsigned_bin(key.q, tmp);
  416. print_hex("q", tmp, len);
  417. return 1;
  418. }
  419. if (cnt != 9) {
  420. rsa_free(&key);
  421. }
  422. }
  423. /* encrypt the key (without lparam) */
  424. for (cnt = 0; cnt < 4; cnt++) {
  425. for (rsa_msgsize = 1; rsa_msgsize <= 86; rsa_msgsize++) {
  426. /* make a random key/msg */
  427. yarrow_read(in, rsa_msgsize, &yarrow_prng);
  428. len = sizeof(out);
  429. len2 = rsa_msgsize;
  430. DO(rsa_encrypt_key(in, rsa_msgsize, out, &len, NULL, 0, &yarrow_prng, prng_idx, hash_idx, &key));
  431. /* change a byte */
  432. out[8] ^= 1;
  433. DOX((err = rsa_decrypt_key(out, len, tmp, &len2, NULL, 0, hash_idx, &stat2, &key))
  434. == CRYPT_INVALID_PACKET ? CRYPT_OK:err, "should fail");
  435. /* change a byte back */
  436. out[8] ^= 1;
  437. if (len2 != rsa_msgsize) {
  438. fprintf(stderr, "\n%i:rsa_decrypt_key mismatch len %lu (first decrypt)", __LINE__, len2);
  439. return 1;
  440. }
  441. len2 = rsa_msgsize;
  442. DO(rsa_decrypt_key(out, len, tmp, &len2, NULL, 0, hash_idx, &stat, &key));
  443. if (!(stat == 1 && stat2 == 0)) {
  444. fprintf(stderr, "rsa_decrypt_key (without lparam) failed (rsa_msgsize = %lu)", rsa_msgsize);
  445. fprintf(stderr, "\n stat: %i stat2: %i", stat, stat2);
  446. return 1;
  447. }
  448. if (len2 != rsa_msgsize || memcmp(tmp, in, rsa_msgsize)) {
  449. fprintf(stderr, "\nrsa_decrypt_key mismatch, len %lu (second decrypt)\n", len2);
  450. print_hex("Original", in, rsa_msgsize);
  451. print_hex("Output", tmp, len2);
  452. return 1;
  453. }
  454. }
  455. }
  456. /* encrypt the key (with lparam) */
  457. for (rsa_msgsize = 1; rsa_msgsize <= 86; rsa_msgsize++) {
  458. len = sizeof(out);
  459. len2 = rsa_msgsize;
  460. DO(rsa_encrypt_key(in, rsa_msgsize, out, &len, lparam, sizeof(lparam), &yarrow_prng, prng_idx, hash_idx, &key));
  461. /* change a byte */
  462. out[8] ^= 1;
  463. DOX((err = rsa_decrypt_key(out, len, tmp, &len2, lparam, sizeof(lparam), hash_idx, &stat2, &key))
  464. == CRYPT_INVALID_PACKET ? CRYPT_OK:err, "should fail");
  465. if (len2 != rsa_msgsize) {
  466. fprintf(stderr, "\n%i:rsa_decrypt_key mismatch len %lu (first decrypt)", __LINE__, len2);
  467. return 1;
  468. }
  469. /* change a byte back */
  470. out[8] ^= 1;
  471. len2 = rsa_msgsize;
  472. DO(rsa_decrypt_key(out, len, tmp, &len2, lparam, sizeof(lparam), hash_idx, &stat, &key));
  473. if (!(stat == 1 && stat2 == 0)) {
  474. fprintf(stderr, "rsa_decrypt_key (with lparam) failed (rsa_msgsize = %lu)", rsa_msgsize);
  475. return 1;
  476. }
  477. if (len2 != rsa_msgsize || memcmp(tmp, in, rsa_msgsize)) {
  478. fprintf(stderr, "rsa_decrypt_key mismatch len %lu", len2);
  479. print_hex("Original", in, rsa_msgsize);
  480. print_hex("Output", tmp, len2);
  481. return 1;
  482. }
  483. }
  484. /* encrypt the key PKCS #1 v1.5 (payload from 1 to 117 bytes) */
  485. for (rsa_msgsize = 1; rsa_msgsize <= 117; rsa_msgsize++) {
  486. len = sizeof(out);
  487. len2 = rsa_msgsize;
  488. /* make a random key/msg */
  489. yarrow_read(in, rsa_msgsize, &yarrow_prng);
  490. DO(rsa_encrypt_key_ex(in, rsa_msgsize, out, &len, NULL, 0, &yarrow_prng, prng_idx, 0, LTC_PKCS_1_V1_5, &key));
  491. len2 = rsa_msgsize;
  492. DO(rsa_decrypt_key_ex(out, len, tmp, &len2, NULL, 0, 0, LTC_PKCS_1_V1_5, &stat, &key));
  493. if (stat != 1) {
  494. fprintf(stderr, "rsa_decrypt_key_ex failed, %d, %d", stat, stat2);
  495. return 1;
  496. }
  497. if (len2 != rsa_msgsize) {
  498. fprintf(stderr, "rsa_decrypt_key_ex mismatch len %lu", len2);
  499. return 1;
  500. }
  501. if (memcmp(tmp, in, rsa_msgsize)) {
  502. fprintf(stderr, "rsa_decrypt_key_ex mismatch data");
  503. print_hex("Original", in, rsa_msgsize);
  504. print_hex("Output", tmp, rsa_msgsize);
  505. return 1;
  506. }
  507. }
  508. /* sign a message (unsalted, lower cholestorol and Atkins approved) now */
  509. len = sizeof(out);
  510. DO(rsa_sign_hash(in, 20, out, &len, &yarrow_prng, prng_idx, hash_idx, 0, &key));
  511. /* export key and import as both private and public */
  512. len2 = sizeof(tmp);
  513. DO(rsa_export(tmp, &len2, PK_PRIVATE, &key));
  514. DO(rsa_import(tmp, len2, &privKey));
  515. len2 = sizeof(tmp);
  516. DO(rsa_export(tmp, &len2, PK_PUBLIC, &key));
  517. DO(rsa_import(tmp, len2, &pubKey));
  518. /* verify with original */
  519. DO(rsa_verify_hash(out, len, in, 20, hash_idx, 0, &stat, &key));
  520. /* change a byte */
  521. in[0] ^= 1;
  522. DO(rsa_verify_hash(out, len, in, 20, hash_idx, 0, &stat2, &key));
  523. if (!(stat == 1 && stat2 == 0)) {
  524. fprintf(stderr, "rsa_verify_hash (unsalted, origKey) failed, %d, %d", stat, stat2);
  525. rsa_free(&key);
  526. rsa_free(&pubKey);
  527. rsa_free(&privKey);
  528. return 1;
  529. }
  530. /* verify with privKey */
  531. /* change byte back to original */
  532. in[0] ^= 1;
  533. DO(rsa_verify_hash(out, len, in, 20, hash_idx, 0, &stat, &privKey));
  534. /* change a byte */
  535. in[0] ^= 1;
  536. DO(rsa_verify_hash(out, len, in, 20, hash_idx, 0, &stat2, &privKey));
  537. if (!(stat == 1 && stat2 == 0)) {
  538. fprintf(stderr, "rsa_verify_hash (unsalted, privKey) failed, %d, %d", stat, stat2);
  539. rsa_free(&key);
  540. rsa_free(&pubKey);
  541. rsa_free(&privKey);
  542. return 1;
  543. }
  544. /* verify with privKey but remove pointer to dP to test without CRT */
  545. dP = privKey.dP;
  546. privKey.dP = NULL;
  547. /* change byte back to original */
  548. in[0] ^= 1;
  549. DO(rsa_verify_hash(out, len, in, 20, hash_idx, 0, &stat, &privKey));
  550. /* change a byte */
  551. in[0] ^= 1;
  552. DO(rsa_verify_hash(out, len, in, 20, hash_idx, 0, &stat2, &privKey));
  553. if (!(stat == 1 && stat2 == 0)) {
  554. fprintf(stderr, "rsa_verify_hash (unsalted, privKey) failed, %d, %d", stat, stat2);
  555. rsa_free(&key);
  556. rsa_free(&pubKey);
  557. rsa_free(&privKey);
  558. return 1;
  559. }
  560. privKey.dP = dP;
  561. /* verify with pubKey */
  562. /* change byte back to original */
  563. in[0] ^= 1;
  564. DO(rsa_verify_hash(out, len, in, 20, hash_idx, 0, &stat, &pubKey));
  565. /* change a byte */
  566. in[0] ^= 1;
  567. DO(rsa_verify_hash(out, len, in, 20, hash_idx, 0, &stat2, &pubKey));
  568. if (!(stat == 1 && stat2 == 0)) {
  569. fprintf(stderr, "rsa_verify_hash (unsalted, pubkey) failed, %d, %d", stat, stat2);
  570. rsa_free(&key);
  571. rsa_free(&pubKey);
  572. rsa_free(&privKey);
  573. return 1;
  574. }
  575. /* sign a message (salted) now (use privKey to make, pubKey to verify) */
  576. len = sizeof(out);
  577. DO(rsa_sign_hash(in, 20, out, &len, &yarrow_prng, prng_idx, hash_idx, 8, &privKey));
  578. DO(rsa_verify_hash(out, len, in, 20, hash_idx, 8, &stat, &pubKey));
  579. /* change a byte */
  580. in[0] ^= 1;
  581. DO(rsa_verify_hash(out, len, in, 20, hash_idx, 8, &stat2, &pubKey));
  582. if (!(stat == 1 && stat2 == 0)) {
  583. fprintf(stderr, "rsa_verify_hash (salted) failed, %d, %d", stat, stat2);
  584. rsa_free(&key);
  585. rsa_free(&pubKey);
  586. rsa_free(&privKey);
  587. return 1;
  588. }
  589. /* sign a message with PKCS #1 v1.5 */
  590. len = sizeof(out);
  591. DO(rsa_sign_hash_ex(in, 20, out, &len, LTC_PKCS_1_V1_5, &yarrow_prng, prng_idx, hash_idx, 8, &privKey));
  592. DO(rsa_verify_hash_ex(out, len, in, 20, LTC_PKCS_1_V1_5, hash_idx, 8, &stat, &pubKey));
  593. /* change a byte */
  594. in[0] ^= 1;
  595. DO(rsa_verify_hash_ex(out, len, in, 20, LTC_PKCS_1_V1_5, hash_idx, 8, &stat2, &pubKey));
  596. if (!(stat == 1 && stat2 == 0)) {
  597. fprintf(stderr, "rsa_verify_hash_ex failed, %d, %d", stat, stat2);
  598. rsa_free(&key);
  599. rsa_free(&pubKey);
  600. rsa_free(&privKey);
  601. return 1;
  602. }
  603. /* Testcase for Bleichenbacher attack
  604. *
  605. * (1) Create a valid signature
  606. * (2) Check that it can be verified
  607. * (3) Decrypt the package to fetch plain text
  608. * (4) Forge the structure of PKCS#1-EMSA encoded data
  609. * (4.1) Search for start and end of the padding string
  610. * (4.2) Move the signature to the front of the padding string
  611. * (4.3) Zero the message until the end
  612. * (5) Encrypt the package again
  613. * (6) Profit :)
  614. * For PS lengths < 8: the verification process should fail
  615. * For PS lengths >= 8: the verification process should succeed
  616. * For all PS lengths: the result should not be valid
  617. */
  618. p = in;
  619. p2 = out;
  620. p3 = tmp;
  621. for (i = 0; i < 9; ++i) {
  622. len = sizeof(in);
  623. len2 = sizeof(out);
  624. /* (1) */
  625. DO(rsa_sign_hash_ex(p, 20, p2, &len2, LTC_PKCS_1_V1_5, &yarrow_prng, prng_idx, hash_idx, 8, &privKey));
  626. /* (2) */
  627. DOX(rsa_verify_hash_ex(p2, len2, p, 20, LTC_PKCS_1_V1_5, hash_idx, -1, &stat, &pubKey), "should succeed");
  628. DOX(stat == 1?CRYPT_OK:CRYPT_FAIL_TESTVECTOR, "should succeed");
  629. len3 = sizeof(tmp);
  630. /* (3) */
  631. DO(ltc_mp.rsa_me(p2, len2, p3, &len3, PK_PUBLIC, &key));
  632. /* (4) */
  633. #if defined(LTC_TEST_DBG) && LTC_TEST_DBG > 1
  634. print_hex("Original signature", p3, len3);
  635. #endif
  636. /* (4.1) */
  637. for (cnt = 0; cnt < len3; ++cnt) {
  638. if (p3[cnt] == 0xff)
  639. break;
  640. }
  641. for (cnt2 = cnt+1; cnt2 < len3; ++cnt2) {
  642. if (p3[cnt2] != 0xff)
  643. break;
  644. }
  645. /* (4.2) */
  646. memmove(&p3[cnt+i], &p3[cnt2], len3-cnt2);
  647. /* (4.3) */
  648. for (cnt = cnt + len3-cnt2+i; cnt < len; ++cnt) {
  649. p3[cnt] = 0;
  650. }
  651. #if defined(LTC_TEST_DBG) && LTC_TEST_DBG > 1
  652. print_hex("Forged signature", p3, len3);
  653. #endif
  654. len2 = sizeof(out);
  655. /* (5) */
  656. DO(ltc_mp.rsa_me(p3, len3, p2, &len2, PK_PRIVATE, &key));
  657. len3 = sizeof(tmp);
  658. /* (6) */
  659. DOX(rsa_verify_hash_ex(p2, len2, p, 20, LTC_PKCS_1_V1_5, hash_idx, -1, &stat, &pubKey)
  660. == CRYPT_INVALID_PACKET ? CRYPT_OK:CRYPT_INVALID_PACKET, "should fail");
  661. DOX(stat == 0?CRYPT_OK:CRYPT_FAIL_TESTVECTOR, "should fail");
  662. }
  663. rsa_free(&key);
  664. /* try reading the public RSA key from a X509 certificate */
  665. len3 = sizeof(tmp);
  666. DO(base64_decode(x509_public_rsa, sizeof(x509_public_rsa), tmp, &len3));
  667. DO(rsa_import_x509(tmp, len3, &key));
  668. len = sizeof(tmp);
  669. DO(rsa_export(tmp, &len, PK_PUBLIC, &key));
  670. if (len != sizeof(openssl_public_rsa_stripped) || memcmp(tmp, openssl_public_rsa_stripped, len)) {
  671. fprintf(stderr, "RSA public export failed to match rsa_import_x509\n");
  672. return 1;
  673. }
  674. rsa_free(&key);
  675. len3 = sizeof(tmp);
  676. DO(base64_decode(_der_tests_cacert_root_cert, _der_tests_cacert_root_cert_size, tmp, &len3));
  677. DO(rsa_import_x509(tmp, len3, &key));
  678. /* free the key and return */
  679. rsa_free(&key);
  680. rsa_free(&pubKey);
  681. rsa_free(&privKey);
  682. return 0;
  683. }
  684. #else
  685. int rsa_test(void)
  686. {
  687. return CRYPT_NOP;
  688. }
  689. #endif
  690. /* ref: $Format:%D$ */
  691. /* git commit: $Format:%H$ */
  692. /* commit time: $Format:%ai$ */