I2C toy code
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

643 lines
30 KiB

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