dsa_test.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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_MDSA) && defined(LTC_TEST_MPI)
  11. /* This is the private key from test_dsa.key */
  12. static const unsigned char openssl_priv_dsa[] = {
  13. 0x30, 0x82, 0x01, 0xbb, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0xc5,
  14. 0x0a, 0x37, 0x51, 0x5c, 0xab, 0xd6, 0x18, 0xd5, 0xa2, 0x70, 0xbd, 0x4a,
  15. 0x6f, 0x6b, 0x4a, 0xf9, 0xe1, 0x39, 0x95, 0x0f, 0x2b, 0x99, 0x38, 0x7d,
  16. 0x9a, 0x64, 0xd6, 0x4c, 0xb5, 0x96, 0x7a, 0xdc, 0xed, 0xac, 0xa8, 0xac,
  17. 0xc6, 0x1b, 0x65, 0x5a, 0xde, 0xdb, 0x00, 0x61, 0x25, 0x1a, 0x18, 0x2c,
  18. 0xee, 0xa1, 0x07, 0x90, 0x62, 0x5e, 0x4d, 0x12, 0x31, 0x90, 0xc7, 0x03,
  19. 0x21, 0xfa, 0x09, 0xe7, 0xb1, 0x73, 0xd7, 0x8e, 0xaf, 0xdb, 0xfd, 0xbf,
  20. 0xb3, 0xef, 0xad, 0xd1, 0xa1, 0x2a, 0x03, 0x6d, 0xe7, 0x06, 0x92, 0x4a,
  21. 0x85, 0x2a, 0xff, 0x7a, 0x01, 0x66, 0x53, 0x1f, 0xea, 0xc6, 0x67, 0x41,
  22. 0x84, 0x5a, 0xc0, 0x6c, 0xed, 0x62, 0xf9, 0xc2, 0x62, 0x62, 0x05, 0xa4,
  23. 0xfa, 0x48, 0xa0, 0x66, 0xec, 0x35, 0xc9, 0xa8, 0x11, 0xfe, 0xb9, 0x81,
  24. 0xab, 0xee, 0xbe, 0x31, 0xb6, 0xbf, 0xcf, 0x02, 0x15, 0x00, 0xaa, 0x5b,
  25. 0xd7, 0xf4, 0xe5, 0x06, 0x24, 0x13, 0xe5, 0x88, 0x35, 0xca, 0x00, 0xc7,
  26. 0xa6, 0x35, 0x71, 0x61, 0x94, 0xc5, 0x02, 0x81, 0x80, 0x3b, 0x92, 0xe4,
  27. 0xff, 0x59, 0x29, 0x15, 0x0b, 0x08, 0x99, 0x5a, 0x7b, 0xf2, 0xad, 0x14,
  28. 0x40, 0x55, 0x6f, 0xa0, 0x47, 0xff, 0x90, 0x99, 0xb3, 0x44, 0xb3, 0xd4,
  29. 0xfc, 0x45, 0x15, 0x05, 0xae, 0x67, 0x22, 0x43, 0x9c, 0xba, 0x37, 0x10,
  30. 0xa5, 0x89, 0x47, 0x37, 0xec, 0xcc, 0xf5, 0xae, 0xad, 0xa8, 0xb4, 0x7a,
  31. 0x35, 0xcb, 0x9d, 0x93, 0x5c, 0xed, 0xe6, 0xb0, 0x7e, 0x96, 0x94, 0xc4,
  32. 0xa6, 0x0c, 0x7d, 0xd6, 0x70, 0x8a, 0x09, 0x4f, 0x81, 0x4a, 0x0e, 0xc2,
  33. 0x13, 0xfb, 0xeb, 0x16, 0xbf, 0xea, 0xa4, 0xf4, 0x56, 0xff, 0x72, 0x30,
  34. 0x05, 0xde, 0x8a, 0x44, 0x3f, 0xbe, 0xc6, 0x85, 0x26, 0x55, 0xd6, 0x2d,
  35. 0x1d, 0x1e, 0xdb, 0x15, 0xda, 0xa4, 0x45, 0x83, 0x3c, 0x17, 0x97, 0x98,
  36. 0x0b, 0x8d, 0x87, 0xf3, 0x49, 0x0d, 0x90, 0xbd, 0xa9, 0xab, 0x67, 0x6e,
  37. 0x87, 0x68, 0x72, 0x23, 0xdc, 0x02, 0x81, 0x80, 0x53, 0x16, 0xb0, 0xfb,
  38. 0xbf, 0x59, 0x8a, 0x5e, 0x55, 0x95, 0xc1, 0x4f, 0xac, 0x43, 0xb8, 0x08,
  39. 0x53, 0xe6, 0xcf, 0x0d, 0x92, 0x23, 0xfa, 0xb1, 0x84, 0x59, 0x52, 0x39,
  40. 0xbf, 0xcb, 0xf2, 0x2d, 0x38, 0x3a, 0xdd, 0x93, 0x52, 0x05, 0x49, 0x7e,
  41. 0x2b, 0x12, 0xc4, 0x61, 0x73, 0xe3, 0x6f, 0x54, 0xbd, 0x96, 0xe5, 0xa7,
  42. 0xaa, 0xa9, 0x5a, 0x58, 0xa4, 0xb7, 0x67, 0xd2, 0xc0, 0xbd, 0xc8, 0x1e,
  43. 0xb1, 0x3a, 0x12, 0x4f, 0x98, 0xc0, 0x05, 0xef, 0x39, 0x5d, 0x6a, 0xba,
  44. 0xb7, 0x0b, 0x3b, 0xd8, 0xb7, 0x95, 0xdd, 0x79, 0x6e, 0xa2, 0xd2, 0x84,
  45. 0x73, 0x47, 0x03, 0x88, 0xb4, 0x64, 0xd9, 0xb9, 0xb8, 0x4f, 0xf1, 0xc9,
  46. 0x34, 0xbb, 0xf9, 0x73, 0x66, 0xf5, 0x7c, 0x2e, 0x11, 0xfe, 0xc3, 0x31,
  47. 0xe6, 0x08, 0x38, 0x59, 0x67, 0x81, 0xeb, 0x6d, 0x41, 0x27, 0xd7, 0x0d,
  48. 0x74, 0xaf, 0xa0, 0x35, 0x02, 0x15, 0x00, 0x99, 0x36, 0xe5, 0xe4, 0xe9,
  49. 0xfb, 0x28, 0xbe, 0x91, 0xf5, 0x06, 0x5f, 0xe8, 0xc9, 0x35, 0xb3, 0xf5,
  50. 0xd8, 0x1f, 0xc5
  51. };
  52. /* private key - raw hexadecimal numbers */
  53. static const char *hex_g = "3B92E4FF5929150B08995A7BF2AD1440556FA047FF9099B344B3D4FC451505AE6722439CBA3710A5894737ECCCF5AEADA8B47A35CB9D935CEDE6B07E9694C4A60C7DD6708A094F814A0EC213FBEB16BFEAA4F456FF723005DE8A443FBEC6852655D62D1D1EDB15DAA445833C1797980B8D87F3490D90BDA9AB676E87687223DC";
  54. static const char *hex_p = "C50A37515CABD618D5A270BD4A6F6B4AF9E139950F2B99387D9A64D64CB5967ADCEDACA8ACC61B655ADEDB0061251A182CEEA10790625E4D123190C70321FA09E7B173D78EAFDBFDBFB3EFADD1A12A036DE706924A852AFF7A0166531FEAC66741845AC06CED62F9C2626205A4FA48A066EC35C9A811FEB981ABEEBE31B6BFCF";
  55. static const char *hex_q = "AA5BD7F4E5062413E58835CA00C7A635716194C5";
  56. static const char *hex_x = "9936E5E4E9FB28BE91F5065FE8C935B3F5D81FC5";
  57. static const char *hex_y = "5316B0FBBF598A5E5595C14FAC43B80853E6CF0D9223FAB184595239BFCBF22D383ADD935205497E2B12C46173E36F54BD96E5A7AAA95A58A4B767D2C0BDC81EB13A124F98C005EF395D6ABAB70B3BD8B795DD796EA2D28473470388B464D9B9B84FF1C934BBF97366F57C2E11FEC331E60838596781EB6D4127D70D74AFA035";
  58. /* The public part of test_dsa.key in SubjectPublicKeyInfo format */
  59. static const unsigned char openssl_pub_dsa[] = {
  60. 0x30, 0x82, 0x01, 0xb6, 0x30, 0x82, 0x01, 0x2b, 0x06, 0x07, 0x2a, 0x86,
  61. 0x48, 0xce, 0x38, 0x04, 0x01, 0x30, 0x82, 0x01, 0x1e, 0x02, 0x81, 0x81,
  62. 0x00, 0xc5, 0x0a, 0x37, 0x51, 0x5c, 0xab, 0xd6, 0x18, 0xd5, 0xa2, 0x70,
  63. 0xbd, 0x4a, 0x6f, 0x6b, 0x4a, 0xf9, 0xe1, 0x39, 0x95, 0x0f, 0x2b, 0x99,
  64. 0x38, 0x7d, 0x9a, 0x64, 0xd6, 0x4c, 0xb5, 0x96, 0x7a, 0xdc, 0xed, 0xac,
  65. 0xa8, 0xac, 0xc6, 0x1b, 0x65, 0x5a, 0xde, 0xdb, 0x00, 0x61, 0x25, 0x1a,
  66. 0x18, 0x2c, 0xee, 0xa1, 0x07, 0x90, 0x62, 0x5e, 0x4d, 0x12, 0x31, 0x90,
  67. 0xc7, 0x03, 0x21, 0xfa, 0x09, 0xe7, 0xb1, 0x73, 0xd7, 0x8e, 0xaf, 0xdb,
  68. 0xfd, 0xbf, 0xb3, 0xef, 0xad, 0xd1, 0xa1, 0x2a, 0x03, 0x6d, 0xe7, 0x06,
  69. 0x92, 0x4a, 0x85, 0x2a, 0xff, 0x7a, 0x01, 0x66, 0x53, 0x1f, 0xea, 0xc6,
  70. 0x67, 0x41, 0x84, 0x5a, 0xc0, 0x6c, 0xed, 0x62, 0xf9, 0xc2, 0x62, 0x62,
  71. 0x05, 0xa4, 0xfa, 0x48, 0xa0, 0x66, 0xec, 0x35, 0xc9, 0xa8, 0x11, 0xfe,
  72. 0xb9, 0x81, 0xab, 0xee, 0xbe, 0x31, 0xb6, 0xbf, 0xcf, 0x02, 0x15, 0x00,
  73. 0xaa, 0x5b, 0xd7, 0xf4, 0xe5, 0x06, 0x24, 0x13, 0xe5, 0x88, 0x35, 0xca,
  74. 0x00, 0xc7, 0xa6, 0x35, 0x71, 0x61, 0x94, 0xc5, 0x02, 0x81, 0x80, 0x3b,
  75. 0x92, 0xe4, 0xff, 0x59, 0x29, 0x15, 0x0b, 0x08, 0x99, 0x5a, 0x7b, 0xf2,
  76. 0xad, 0x14, 0x40, 0x55, 0x6f, 0xa0, 0x47, 0xff, 0x90, 0x99, 0xb3, 0x44,
  77. 0xb3, 0xd4, 0xfc, 0x45, 0x15, 0x05, 0xae, 0x67, 0x22, 0x43, 0x9c, 0xba,
  78. 0x37, 0x10, 0xa5, 0x89, 0x47, 0x37, 0xec, 0xcc, 0xf5, 0xae, 0xad, 0xa8,
  79. 0xb4, 0x7a, 0x35, 0xcb, 0x9d, 0x93, 0x5c, 0xed, 0xe6, 0xb0, 0x7e, 0x96,
  80. 0x94, 0xc4, 0xa6, 0x0c, 0x7d, 0xd6, 0x70, 0x8a, 0x09, 0x4f, 0x81, 0x4a,
  81. 0x0e, 0xc2, 0x13, 0xfb, 0xeb, 0x16, 0xbf, 0xea, 0xa4, 0xf4, 0x56, 0xff,
  82. 0x72, 0x30, 0x05, 0xde, 0x8a, 0x44, 0x3f, 0xbe, 0xc6, 0x85, 0x26, 0x55,
  83. 0xd6, 0x2d, 0x1d, 0x1e, 0xdb, 0x15, 0xda, 0xa4, 0x45, 0x83, 0x3c, 0x17,
  84. 0x97, 0x98, 0x0b, 0x8d, 0x87, 0xf3, 0x49, 0x0d, 0x90, 0xbd, 0xa9, 0xab,
  85. 0x67, 0x6e, 0x87, 0x68, 0x72, 0x23, 0xdc, 0x03, 0x81, 0x84, 0x00, 0x02,
  86. 0x81, 0x80, 0x53, 0x16, 0xb0, 0xfb, 0xbf, 0x59, 0x8a, 0x5e, 0x55, 0x95,
  87. 0xc1, 0x4f, 0xac, 0x43, 0xb8, 0x08, 0x53, 0xe6, 0xcf, 0x0d, 0x92, 0x23,
  88. 0xfa, 0xb1, 0x84, 0x59, 0x52, 0x39, 0xbf, 0xcb, 0xf2, 0x2d, 0x38, 0x3a,
  89. 0xdd, 0x93, 0x52, 0x05, 0x49, 0x7e, 0x2b, 0x12, 0xc4, 0x61, 0x73, 0xe3,
  90. 0x6f, 0x54, 0xbd, 0x96, 0xe5, 0xa7, 0xaa, 0xa9, 0x5a, 0x58, 0xa4, 0xb7,
  91. 0x67, 0xd2, 0xc0, 0xbd, 0xc8, 0x1e, 0xb1, 0x3a, 0x12, 0x4f, 0x98, 0xc0,
  92. 0x05, 0xef, 0x39, 0x5d, 0x6a, 0xba, 0xb7, 0x0b, 0x3b, 0xd8, 0xb7, 0x95,
  93. 0xdd, 0x79, 0x6e, 0xa2, 0xd2, 0x84, 0x73, 0x47, 0x03, 0x88, 0xb4, 0x64,
  94. 0xd9, 0xb9, 0xb8, 0x4f, 0xf1, 0xc9, 0x34, 0xbb, 0xf9, 0x73, 0x66, 0xf5,
  95. 0x7c, 0x2e, 0x11, 0xfe, 0xc3, 0x31, 0xe6, 0x08, 0x38, 0x59, 0x67, 0x81,
  96. 0xeb, 0x6d, 0x41, 0x27, 0xd7, 0x0d, 0x74, 0xaf, 0xa0, 0x35
  97. };
  98. static unsigned char dsaparam_der[] = {
  99. 0x30, 0x82, 0x01, 0x1e, 0x02, 0x81, 0x81, 0x00, 0xc5, 0x0a, 0x37, 0x51,
  100. 0x5c, 0xab, 0xd6, 0x18, 0xd5, 0xa2, 0x70, 0xbd, 0x4a, 0x6f, 0x6b, 0x4a,
  101. 0xf9, 0xe1, 0x39, 0x95, 0x0f, 0x2b, 0x99, 0x38, 0x7d, 0x9a, 0x64, 0xd6,
  102. 0x4c, 0xb5, 0x96, 0x7a, 0xdc, 0xed, 0xac, 0xa8, 0xac, 0xc6, 0x1b, 0x65,
  103. 0x5a, 0xde, 0xdb, 0x00, 0x61, 0x25, 0x1a, 0x18, 0x2c, 0xee, 0xa1, 0x07,
  104. 0x90, 0x62, 0x5e, 0x4d, 0x12, 0x31, 0x90, 0xc7, 0x03, 0x21, 0xfa, 0x09,
  105. 0xe7, 0xb1, 0x73, 0xd7, 0x8e, 0xaf, 0xdb, 0xfd, 0xbf, 0xb3, 0xef, 0xad,
  106. 0xd1, 0xa1, 0x2a, 0x03, 0x6d, 0xe7, 0x06, 0x92, 0x4a, 0x85, 0x2a, 0xff,
  107. 0x7a, 0x01, 0x66, 0x53, 0x1f, 0xea, 0xc6, 0x67, 0x41, 0x84, 0x5a, 0xc0,
  108. 0x6c, 0xed, 0x62, 0xf9, 0xc2, 0x62, 0x62, 0x05, 0xa4, 0xfa, 0x48, 0xa0,
  109. 0x66, 0xec, 0x35, 0xc9, 0xa8, 0x11, 0xfe, 0xb9, 0x81, 0xab, 0xee, 0xbe,
  110. 0x31, 0xb6, 0xbf, 0xcf, 0x02, 0x15, 0x00, 0xaa, 0x5b, 0xd7, 0xf4, 0xe5,
  111. 0x06, 0x24, 0x13, 0xe5, 0x88, 0x35, 0xca, 0x00, 0xc7, 0xa6, 0x35, 0x71,
  112. 0x61, 0x94, 0xc5, 0x02, 0x81, 0x80, 0x3b, 0x92, 0xe4, 0xff, 0x59, 0x29,
  113. 0x15, 0x0b, 0x08, 0x99, 0x5a, 0x7b, 0xf2, 0xad, 0x14, 0x40, 0x55, 0x6f,
  114. 0xa0, 0x47, 0xff, 0x90, 0x99, 0xb3, 0x44, 0xb3, 0xd4, 0xfc, 0x45, 0x15,
  115. 0x05, 0xae, 0x67, 0x22, 0x43, 0x9c, 0xba, 0x37, 0x10, 0xa5, 0x89, 0x47,
  116. 0x37, 0xec, 0xcc, 0xf5, 0xae, 0xad, 0xa8, 0xb4, 0x7a, 0x35, 0xcb, 0x9d,
  117. 0x93, 0x5c, 0xed, 0xe6, 0xb0, 0x7e, 0x96, 0x94, 0xc4, 0xa6, 0x0c, 0x7d,
  118. 0xd6, 0x70, 0x8a, 0x09, 0x4f, 0x81, 0x4a, 0x0e, 0xc2, 0x13, 0xfb, 0xeb,
  119. 0x16, 0xbf, 0xea, 0xa4, 0xf4, 0x56, 0xff, 0x72, 0x30, 0x05, 0xde, 0x8a,
  120. 0x44, 0x3f, 0xbe, 0xc6, 0x85, 0x26, 0x55, 0xd6, 0x2d, 0x1d, 0x1e, 0xdb,
  121. 0x15, 0xda, 0xa4, 0x45, 0x83, 0x3c, 0x17, 0x97, 0x98, 0x0b, 0x8d, 0x87,
  122. 0xf3, 0x49, 0x0d, 0x90, 0xbd, 0xa9, 0xab, 0x67, 0x6e, 0x87, 0x68, 0x72,
  123. 0x23, 0xdc
  124. };
  125. static int _dsa_compat_test(void)
  126. {
  127. dsa_key key;
  128. unsigned char tmp[1024], buf[1024];
  129. unsigned long x, len;
  130. unsigned char key_parts[5][256];
  131. unsigned long key_lens[5];
  132. int stat;
  133. DO(dsa_import(openssl_priv_dsa, sizeof(openssl_priv_dsa), &key));
  134. x = sizeof(tmp);
  135. DO(dsa_export(tmp, &x, PK_PRIVATE | PK_STD, &key));
  136. if (compare_testvector(tmp, x, openssl_priv_dsa, sizeof(openssl_priv_dsa),
  137. "DSA private export failed from dsa_import(priv_key)\n", __LINE__)) {
  138. return CRYPT_FAIL_TESTVECTOR;
  139. }
  140. x = sizeof(tmp);
  141. DO(dsa_export(tmp, &x, PK_PUBLIC | PK_STD, &key));
  142. if (compare_testvector(tmp, x, openssl_pub_dsa, sizeof(openssl_pub_dsa),
  143. "DSA public export failed from dsa_import(priv_key)\n", __LINE__)) {
  144. return CRYPT_FAIL_TESTVECTOR;
  145. }
  146. dsa_free(&key);
  147. DO(dsa_import(openssl_pub_dsa, sizeof(openssl_pub_dsa), &key));
  148. x = sizeof(tmp);
  149. DO(dsa_export(tmp, &x, PK_PUBLIC | PK_STD, &key));
  150. if (compare_testvector(tmp, x, openssl_pub_dsa, sizeof(openssl_pub_dsa),
  151. "DSA public export failed from dsa_import(pub_key)\n", __LINE__)) {
  152. return CRYPT_FAIL_TESTVECTOR;
  153. }
  154. dsa_free(&key);
  155. /* try import private key from raw hexadecimal numbers */
  156. for (x = 0; x < 5; ++x) {
  157. key_lens[x] = sizeof(key_parts[x]);
  158. }
  159. DO(radix_to_bin(hex_p, 16, key_parts[0], &key_lens[0]));
  160. DO(radix_to_bin(hex_q, 16, key_parts[1], &key_lens[1]));
  161. DO(radix_to_bin(hex_g, 16, key_parts[2], &key_lens[2]));
  162. DO(radix_to_bin(hex_y, 16, key_parts[3], &key_lens[3]));
  163. DO(radix_to_bin(hex_x, 16, key_parts[4], &key_lens[4]));
  164. DO(dsa_set_pqg(key_parts[0], key_lens[0],
  165. key_parts[1], key_lens[1],
  166. key_parts[2], key_lens[2],
  167. &key));
  168. DO(dsa_set_key(key_parts[4], key_lens[4],
  169. PK_PRIVATE,
  170. &key));
  171. len = sizeof(buf);
  172. DO(dsa_export(buf, &len, PK_PRIVATE | PK_STD, &key));
  173. if (compare_testvector(buf, len, openssl_priv_dsa, sizeof(openssl_priv_dsa),
  174. "DSA private export failed from dsa_set_pqg() & dsa_set_key()\n", __LINE__)) {
  175. return CRYPT_FAIL_TESTVECTOR;
  176. }
  177. dsa_free(&key);
  178. /* try import public key from raw hexadecimal numbers */
  179. DO(dsa_set_pqg(key_parts[0], key_lens[0],
  180. key_parts[1], key_lens[1],
  181. key_parts[2], key_lens[2],
  182. &key));
  183. DO(dsa_set_key(key_parts[3], key_lens[3],
  184. PK_PUBLIC,
  185. &key));
  186. len = sizeof(buf);
  187. DO(dsa_export(buf, &len, PK_PUBLIC | PK_STD, &key));
  188. if (compare_testvector(buf, len, openssl_pub_dsa, sizeof(openssl_pub_dsa),
  189. "DSA public export failed from dsa_set_pqg() & dsa_set_key()\n", __LINE__)) {
  190. return CRYPT_FAIL_TESTVECTOR;
  191. }
  192. dsa_free(&key);
  193. /* try import dsaparam */
  194. DO(dsa_set_pqg_dsaparam(dsaparam_der, sizeof(dsaparam_der), &key));
  195. DO(dsa_generate_key(&yarrow_prng, find_prng("yarrow"), &key));
  196. /* verify it */
  197. DO(dsa_verify_key(&key, &stat));
  198. if (stat == 0) {
  199. fprintf(stderr, "dsa_verify_key after dsa_set_pqg_dsaparam()");
  200. return CRYPT_FAIL_TESTVECTOR;
  201. }
  202. dsa_free(&key);
  203. /* try import dsaparam - our public key */
  204. DO(dsa_set_pqg_dsaparam(dsaparam_der, sizeof(dsaparam_der), &key));
  205. DO(dsa_set_key(key_parts[3], key_lens[3],
  206. PK_PUBLIC,
  207. &key));
  208. len = sizeof(buf);
  209. DO(dsa_export(buf, &len, PK_PUBLIC | PK_STD, &key));
  210. if (compare_testvector(buf, len, openssl_pub_dsa, sizeof(openssl_pub_dsa),
  211. "DSA public export failed from dsa_set_pqg_dsaparam()\n", __LINE__)) {
  212. return CRYPT_FAIL_TESTVECTOR;
  213. }
  214. dsa_free(&key);
  215. /* try import dsaparam - our private key */
  216. DO(dsa_set_pqg_dsaparam(dsaparam_der, sizeof(dsaparam_der), &key));
  217. DO(dsa_set_key(key_parts[4], key_lens[4],
  218. PK_PRIVATE,
  219. &key));
  220. len = sizeof(buf);
  221. DO(dsa_export(buf, &len, PK_PRIVATE | PK_STD, &key));
  222. if (compare_testvector(buf, len, openssl_priv_dsa, sizeof(openssl_priv_dsa),
  223. "DSA private export failed from dsa_set_pqg_dsaparam()\n", __LINE__)) {
  224. return CRYPT_FAIL_TESTVECTOR;
  225. }
  226. dsa_free(&key);
  227. return CRYPT_OK;
  228. }
  229. static int _dsa_wycheproof_test(void)
  230. {
  231. /* test case from https://github.com/google/wycheproof/blob/master/testvectors/dsa_test.json
  232. *
  233. * "comment" : "appending unused 0's",
  234. * "message" : "48656c6c6f",
  235. * "result" : "invalid",
  236. * "sig" : "303d021c1e41b479ad576905b960fe14eadb91b0ccf34843dab916173bb8c9cd021d00ade65988d237d30f9ef41dd424a4e1c8f16967cf3365813fe87862360000",
  237. * "tcId" : 55
  238. */
  239. unsigned char msg[] = { 0x48, 0x65, 0x6c, 0x6c, 0x6f };
  240. unsigned char sig[] = { 0x30, 0x3d, 0x02, 0x1c, 0x1e, 0x41, 0xb4, 0x79, 0xad, 0x57, 0x69, 0x05, 0xb9, 0x60, 0xfe,
  241. 0x14, 0xea, 0xdb, 0x91, 0xb0, 0xcc, 0xf3, 0x48, 0x43, 0xda, 0xb9, 0x16, 0x17, 0x3b, 0xb8,
  242. 0xc9, 0xcd, 0x02, 0x1d, 0x00, 0xad, 0xe6, 0x59, 0x88, 0xd2, 0x37, 0xd3, 0x0f, 0x9e, 0xf4,
  243. 0x1d, 0xd4, 0x24, 0xa4, 0xe1, 0xc8, 0xf1, 0x69, 0x67, 0xcf, 0x33, 0x65, 0x81, 0x3f, 0xe8,
  244. 0x78, 0x62, 0x36, 0x00, 0x00 };
  245. const char* b64key =
  246. "MIIDQjCCAjUGByqGSM44BAEwggIoAoIBAQCPeTXZuarpv6vtiHrPSVG28y7FnjuvNxjo6sSWHz79"
  247. "NgbnQ1GpxBgzObgJ58KuHFObp0dbhdARrbi0eYd1SYRpXKwOjxSzNggooi/6JxEKPWKpk0U0CaD+"
  248. "aWxGWPhL3SCBnDcJoBBXsZWtzQAjPbpUhLYpH51kjviDRIZ3l5zsBLQ0pqwudemYXeI9sCkvwRGM"
  249. "n/qdgYHnM423krcw17njSVkvaAmYchU5Feo9a4tGU8YzRY+AOzKkwuDycpAlbk4/ijsIOKHEUOTh"
  250. "jBopo33fXqFD3ktm/wSQPtXPFiPhWNSHxgjpfyEc2B3KI8tuOAdl+CLjQr5ITAV2OTlgHNZnAh0A"
  251. "uvaWpoV499/e5/pnyXfHhe8ysjO65YDAvNVpXQKCAQAWplxYIEhQcE51AqOXVwQNNNo6NHjBVNTk"
  252. "pcAtJC7gT5bmHkvQkEq9rI837rHgnzGC0jyQQ8tkL4gAQWDt+coJsyB2p5wypifyRz6Rh5uixOdE"
  253. "vSCBVEy1W4AsNo0fqD7UielOD6BojjJCilx4xHjGjQUntxyaOrsLC+EsRGiWOefTznTbEBplqiuH"
  254. "9kxoJts+xy9LVZmDS7TtsC98kOmkltOlXVNb6/xF1PYZ9j897buHOSXC8iTgdzEpbaiH7B5HSPh+"
  255. "+1/et1SEMWsiMt7lU92vAhErDR8C2jCXMiT+J67ai51LKSLZuovjntnhA6Y8UoELxoi34u1DFuHv"
  256. "F9veA4IBBQACggEAHnf4QrGuD82ZKdOUFh1B4UYU/3UHqaMfSh8U0i4qYnofTllmJIg/GlsWjpQl"
  257. "FG8i1fbuKHV0FHFLuZS6ESnwFdbgSnF+35tTCl1cq5TxRjHotM95rrNYzHQYRVU4QeisRhYw6ASm"
  258. "L0Nna6Z5SvZomcN3uGnqYSp7n+ZhGqlr5S64tiyXkRe7vMqKfsHh/6scffz8cEhwDTrjhYE26Jdw"
  259. "HXwpIbXf7x0fiX9Q2WyhtcLtxYytoYkZ41ZC8IB+6/oAyZoy9NCVwxiPeO1UcRvgMlxLUyrszWVA"
  260. "pWfDJyJUQOoVMZveBlEEeaGGF5niW1fezHPANtdaBwK9NzyiMTSZMQ==";
  261. unsigned char derkey[838];
  262. unsigned long derlen = sizeof(derkey);
  263. unsigned char hash[32];
  264. unsigned long hashlen = sizeof(hash);
  265. dsa_key key;
  266. int stat;
  267. DO(base64_decode((unsigned char*)b64key, strlen(b64key), derkey, &derlen));
  268. if (derlen != 838) {
  269. fprintf(stderr, "base64_decode failed, derlen=%lu (expected 838)\n", derlen);
  270. return CRYPT_FAIL_TESTVECTOR;
  271. }
  272. DO(dsa_import(derkey, derlen, &key));
  273. DO(hash_memory(find_hash("sha224"), msg, sizeof(msg), hash, &hashlen));
  274. if (hashlen != 28) {
  275. fprintf(stderr, "hash_memory failed, hashlen=%lu (expected 32)\n", hashlen);
  276. return CRYPT_FAIL_TESTVECTOR;
  277. }
  278. stat = 666; /* intentionally not one, not zero */
  279. DO(dsa_verify_hash(sig, sizeof(sig)-2, hash, hashlen, &stat, &key));
  280. /* without the last two 0x00 bytes it is a valid signature */
  281. if (stat != 1) {
  282. fprintf(stderr, "dsa_verify_hash rejected valid signature\n");
  283. return CRYPT_FAIL_TESTVECTOR;
  284. }
  285. stat = 666; /* intentionally not one, not zero */
  286. DO(dsa_verify_hash(sig, sizeof(sig), hash, hashlen, &stat, &key));
  287. /* this should be invalid */
  288. if (stat != 0) {
  289. fprintf(stderr, "dsa_verify_hash did not reject invalid signature\n");
  290. return CRYPT_FAIL_TESTVECTOR;
  291. }
  292. dsa_free(&key);
  293. return CRYPT_OK;
  294. }
  295. int dsa_test(void)
  296. {
  297. unsigned char msg[16], out[1024], out2[1024], ch;
  298. unsigned long x, y;
  299. int stat1, stat2;
  300. dsa_key key, key2;
  301. DO(_dsa_compat_test());
  302. DO(_dsa_wycheproof_test());
  303. /* make a random key */
  304. DO(dsa_generate_pqg(&yarrow_prng, find_prng("yarrow"), 20, 128, &key));
  305. DO(dsa_generate_key(&yarrow_prng, find_prng("yarrow"), &key));
  306. /* verify it */
  307. DO(dsa_verify_key(&key, &stat1));
  308. if (stat1 == 0) { fprintf(stderr, "dsa_verify_key "); return 1; }
  309. /* encrypt a message */
  310. for (ch = 0; ch < 16; ch++) { msg[ch] = ch; }
  311. x = sizeof(out);
  312. DO(dsa_encrypt_key(msg, 16, out, &x, &yarrow_prng, find_prng("yarrow"), find_hash("sha1"), &key));
  313. /* decrypt */
  314. y = sizeof(out2);
  315. DO(dsa_decrypt_key(out, x, out2, &y, &key));
  316. if (y != 16 || memcmp(out2, msg, 16)) {
  317. fprintf(stderr, "dsa_decrypt failed, y == %lu\n", y);
  318. return 1;
  319. }
  320. /* sign the message */
  321. x = sizeof(out);
  322. DO(dsa_sign_hash(msg, sizeof(msg), out, &x, &yarrow_prng, find_prng("yarrow"), &key));
  323. /* verify it once */
  324. DO(dsa_verify_hash(out, x, msg, sizeof(msg), &stat1, &key));
  325. /* Modify and verify again */
  326. msg[0] ^= 1;
  327. DO(dsa_verify_hash(out, x, msg, sizeof(msg), &stat2, &key));
  328. msg[0] ^= 1;
  329. if (!(stat1 == 1 && stat2 == 0)) { fprintf(stderr, "dsa_verify %d %d", stat1, stat2); return 1; }
  330. /* test exporting it */
  331. y = sizeof(out2);
  332. DO(dsa_export(out2, &y, PK_PRIVATE, &key));
  333. DO(dsa_import(out2, y, &key2));
  334. /* verify a signature with it */
  335. DO(dsa_verify_hash(out, x, msg, sizeof(msg), &stat1, &key2));
  336. if (stat1 == 0) { fprintf(stderr, "dsa_verify (import private) %d ", stat1); return 1; }
  337. dsa_free(&key2);
  338. /* export as public now */
  339. y = sizeof(out2);
  340. DO(dsa_export(out2, &y, PK_PUBLIC, &key));
  341. DO(dsa_import(out2, y, &key2));
  342. /* verify a signature with it */
  343. DO(dsa_verify_hash(out, x, msg, sizeof(msg), &stat1, &key2));
  344. if (stat1 == 0) { fprintf(stderr, "dsa_verify (import public) %d ", stat1); return 1; }
  345. dsa_free(&key2);
  346. dsa_free(&key);
  347. return 0;
  348. }
  349. #else
  350. int dsa_test(void)
  351. {
  352. return CRYPT_NOP;
  353. }
  354. #endif
  355. /* ref: $Format:%D$ */
  356. /* git commit: $Format:%H$ */
  357. /* commit time: $Format:%ai$ */