dsa_no_digest_size_test.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include "testutil.h"
  12. #include <openssl/evp.h>
  13. #include <openssl/err.h>
  14. #include <openssl/rand.h>
  15. #ifndef OPENSSL_NO_DSA
  16. #include <openssl/dsa.h>
  17. static DSA *dsakey;
  18. /*
  19. * These parameters are from test/recipes/04-test_pem_data/dsaparam.pem,
  20. * converted using dsaparam -C
  21. */
  22. static DSA *load_dsa_params(void)
  23. {
  24. static unsigned char dsap_2048[] = {
  25. 0xAE, 0x35, 0x7D, 0x4E, 0x1D, 0x96, 0xE2, 0x9F, 0x00, 0x96,
  26. 0x60, 0x5A, 0x6E, 0x4D, 0x07, 0x8D, 0xA5, 0x7C, 0xBC, 0xF9,
  27. 0xAD, 0xD7, 0x9F, 0xD5, 0xE9, 0xEE, 0xA6, 0x33, 0x51, 0xDE,
  28. 0x7B, 0x72, 0xD2, 0x75, 0xAA, 0x71, 0x77, 0xF1, 0x63, 0xFB,
  29. 0xB6, 0xEC, 0x5A, 0xBA, 0x0D, 0x72, 0xA2, 0x1A, 0x1C, 0x64,
  30. 0xB8, 0xE5, 0x89, 0x09, 0x6D, 0xC9, 0x6F, 0x0B, 0x7F, 0xD2,
  31. 0xCE, 0x9F, 0xEF, 0x87, 0x5A, 0xB6, 0x67, 0x2F, 0xEF, 0xEE,
  32. 0xEB, 0x59, 0xF5, 0x5E, 0xFF, 0xA8, 0x28, 0x84, 0x9E, 0x5B,
  33. 0x37, 0x09, 0x11, 0x80, 0x7C, 0x08, 0x5C, 0xD5, 0xE1, 0x48,
  34. 0x4B, 0xD2, 0x68, 0xFB, 0x3F, 0x9F, 0x2B, 0x6B, 0x6C, 0x0D,
  35. 0x48, 0x1B, 0x1A, 0x80, 0xC2, 0xEB, 0x11, 0x1B, 0x37, 0x79,
  36. 0xD6, 0x8C, 0x8B, 0x72, 0x3E, 0x67, 0xA5, 0x05, 0x0E, 0x41,
  37. 0x8A, 0x9E, 0x35, 0x50, 0xB4, 0xD2, 0x40, 0x27, 0x6B, 0xFD,
  38. 0xE0, 0x64, 0x6B, 0x5B, 0x38, 0x42, 0x94, 0xB5, 0x49, 0xDA,
  39. 0xEF, 0x6E, 0x78, 0x37, 0xCD, 0x30, 0x89, 0xC3, 0x45, 0x50,
  40. 0x7B, 0x9C, 0x8C, 0xE7, 0x1C, 0x98, 0x70, 0x71, 0x5D, 0x79,
  41. 0x5F, 0xEF, 0xE8, 0x94, 0x85, 0x53, 0x3E, 0xEF, 0xA3, 0x2C,
  42. 0xCE, 0x1A, 0xAB, 0x7D, 0xD6, 0x5E, 0x14, 0xCD, 0x51, 0x54,
  43. 0x89, 0x9D, 0x77, 0xE4, 0xF8, 0x22, 0xF0, 0x35, 0x10, 0x75,
  44. 0x05, 0x71, 0x51, 0x4F, 0x8C, 0x4C, 0x5C, 0x0D, 0x2C, 0x2C,
  45. 0xBE, 0x6C, 0x34, 0xEE, 0x12, 0x82, 0x87, 0x03, 0x19, 0x06,
  46. 0x12, 0xA8, 0xAA, 0xF4, 0x0D, 0x3C, 0x49, 0xCC, 0x70, 0x5A,
  47. 0xD8, 0x32, 0xEE, 0x32, 0x50, 0x85, 0x70, 0xE8, 0x18, 0xFD,
  48. 0x74, 0x80, 0x53, 0x32, 0x57, 0xEE, 0x50, 0xC9, 0xAE, 0xEB,
  49. 0xAE, 0xB6, 0x22, 0x32, 0x16, 0x6B, 0x8C, 0x59, 0xDA, 0xEE,
  50. 0x1D, 0x33, 0xDF, 0x4C, 0xA2, 0x3D
  51. };
  52. static unsigned char dsaq_2048[] = {
  53. 0xAD, 0x2D, 0x6E, 0x17, 0xB0, 0xF3, 0xEB, 0xC7, 0xB8, 0xEE,
  54. 0x95, 0x78, 0xF2, 0x17, 0xF5, 0x33, 0x01, 0x67, 0xBC, 0xDE,
  55. 0x93, 0xFF, 0xEE, 0x40, 0xE8, 0x7F, 0xF1, 0x93, 0x6D, 0x4B,
  56. 0x87, 0x13
  57. };
  58. static unsigned char dsag_2048[] = {
  59. 0x66, 0x6F, 0xDA, 0x63, 0xA5, 0x8E, 0xD2, 0x4C, 0xD5, 0x45,
  60. 0x2D, 0x76, 0x5D, 0x5F, 0xCD, 0x4A, 0xB4, 0x1A, 0x42, 0x35,
  61. 0x86, 0x3A, 0x6F, 0xA9, 0xFA, 0x27, 0xAB, 0xDE, 0x03, 0x21,
  62. 0x36, 0x0A, 0x07, 0x29, 0xC9, 0x2F, 0x6D, 0x49, 0xA8, 0xF7,
  63. 0xC6, 0xF4, 0x92, 0xD7, 0x73, 0xC1, 0xD8, 0x76, 0x0E, 0x61,
  64. 0xA7, 0x0B, 0x6E, 0x96, 0xB8, 0xC8, 0xCB, 0x38, 0x35, 0x12,
  65. 0x20, 0x79, 0xA5, 0x08, 0x28, 0x35, 0x5C, 0xBC, 0x52, 0x16,
  66. 0xAF, 0x52, 0xBA, 0x0F, 0xC3, 0xB1, 0x63, 0x12, 0x27, 0x0B,
  67. 0x74, 0xA4, 0x47, 0x43, 0xD6, 0x30, 0xB8, 0x9C, 0x2E, 0x40,
  68. 0x14, 0xCD, 0x99, 0x7F, 0xE8, 0x8E, 0x37, 0xB0, 0xA9, 0x3F,
  69. 0x54, 0xE9, 0x66, 0x22, 0x61, 0x4C, 0xF8, 0x49, 0x03, 0x57,
  70. 0x14, 0x32, 0x1D, 0x37, 0x3D, 0xE2, 0x92, 0xF8, 0x8E, 0xA0,
  71. 0x6A, 0x66, 0x63, 0xF0, 0xB0, 0x6E, 0x07, 0x2B, 0x3D, 0xBF,
  72. 0xD0, 0x84, 0x6A, 0xAA, 0x1F, 0x30, 0x77, 0x65, 0xE5, 0xFC,
  73. 0xF5, 0xEC, 0x55, 0xCE, 0x73, 0xDB, 0xBE, 0xA7, 0x8D, 0x3A,
  74. 0x9F, 0x7A, 0xED, 0x4F, 0xAF, 0xA2, 0x80, 0x4C, 0x30, 0x9E,
  75. 0x28, 0x49, 0x65, 0x40, 0xF0, 0x03, 0x45, 0x56, 0x99, 0xA2,
  76. 0x93, 0x1B, 0x9C, 0x46, 0xDE, 0xBD, 0xA8, 0xAB, 0x5F, 0x90,
  77. 0x3F, 0xB7, 0x3F, 0xD4, 0x6F, 0x8D, 0x5A, 0x30, 0xE1, 0xD4,
  78. 0x63, 0x3A, 0x6A, 0x7C, 0x8F, 0x24, 0xFC, 0xD9, 0x14, 0x28,
  79. 0x09, 0xE4, 0x84, 0x4E, 0x17, 0x43, 0x56, 0xB8, 0xD4, 0x4B,
  80. 0xA2, 0x29, 0x45, 0xD3, 0x13, 0xF0, 0xC2, 0x76, 0x9B, 0x01,
  81. 0xA0, 0x80, 0x6E, 0x93, 0x63, 0x5E, 0x87, 0x24, 0x20, 0x2A,
  82. 0xFF, 0xBB, 0x9F, 0xA8, 0x99, 0x6C, 0xA7, 0x9A, 0x00, 0xB9,
  83. 0x7D, 0xDA, 0x66, 0xC9, 0xC0, 0x72, 0x72, 0x22, 0x0F, 0x1A,
  84. 0xCC, 0x23, 0xD9, 0xB7, 0x5F, 0x1B
  85. };
  86. DSA *dsa = DSA_new();
  87. BIGNUM *p, *q, *g;
  88. if (dsa == NULL)
  89. return NULL;
  90. if (!DSA_set0_pqg(dsa, p = BN_bin2bn(dsap_2048, sizeof(dsap_2048), NULL),
  91. q = BN_bin2bn(dsaq_2048, sizeof(dsaq_2048), NULL),
  92. g = BN_bin2bn(dsag_2048, sizeof(dsag_2048), NULL))) {
  93. DSA_free(dsa);
  94. BN_free(p);
  95. BN_free(q);
  96. BN_free(g);
  97. return NULL;
  98. }
  99. return dsa;
  100. }
  101. static int genkeys(void)
  102. {
  103. if (!TEST_ptr(dsakey = load_dsa_params()))
  104. return 0;
  105. if (!TEST_int_eq(DSA_generate_key(dsakey), 1))
  106. return 0;
  107. return 1;
  108. }
  109. static int sign_and_verify(int len)
  110. {
  111. /*
  112. * Per FIPS 186-4, the hash is recommended to be the same length as q.
  113. * If the hash is longer than q, the leftmost N bits are used; if the hash
  114. * is shorter, then we left-pad (see appendix C.2.1).
  115. */
  116. size_t sigLength;
  117. int digestlen = BN_num_bytes(DSA_get0_q(dsakey));
  118. int ok = 0;
  119. unsigned char *dataToSign = OPENSSL_malloc(len);
  120. unsigned char *paddedData = OPENSSL_malloc(digestlen);
  121. unsigned char *signature = NULL;
  122. EVP_PKEY_CTX *ctx = NULL;
  123. EVP_PKEY *pkey = NULL;
  124. if (!TEST_ptr(dataToSign) ||
  125. !TEST_ptr(paddedData) ||
  126. !TEST_int_eq(RAND_bytes(dataToSign, len), 1))
  127. goto end;
  128. memset(paddedData, 0, digestlen);
  129. if (len > digestlen)
  130. memcpy(paddedData, dataToSign, digestlen);
  131. else
  132. memcpy(paddedData + digestlen - len, dataToSign, len);
  133. if (!TEST_ptr(pkey = EVP_PKEY_new()))
  134. goto end;
  135. EVP_PKEY_set1_DSA(pkey, dsakey);
  136. if (!TEST_ptr(ctx = EVP_PKEY_CTX_new(pkey, NULL)))
  137. goto end;
  138. if (!TEST_int_eq(EVP_PKEY_sign_init(ctx), 1))
  139. goto end;
  140. if (EVP_PKEY_sign(ctx, NULL, &sigLength, dataToSign, len) != 1) {
  141. TEST_error("Failed to get signature length, len=%d", len);
  142. goto end;
  143. }
  144. if (!TEST_ptr(signature = OPENSSL_malloc(sigLength)))
  145. goto end;
  146. if (EVP_PKEY_sign(ctx, signature, &sigLength, dataToSign, len) != 1) {
  147. TEST_error("Failed to sign, len=%d", len);
  148. goto end;
  149. }
  150. /* Check that the signature is okay via the EVP interface */
  151. if (!TEST_int_eq(EVP_PKEY_verify_init(ctx), 1))
  152. goto end;
  153. /* ... using the same data we just signed */
  154. if (EVP_PKEY_verify(ctx, signature, sigLength, dataToSign, len) != 1) {
  155. TEST_error("EVP verify with unpadded length %d failed\n", len);
  156. goto end;
  157. }
  158. /* ... padding/truncating the data to the appropriate digest size */
  159. if (EVP_PKEY_verify(ctx, signature, sigLength, paddedData, digestlen) != 1) {
  160. TEST_error("EVP verify with length %d failed\n", len);
  161. goto end;
  162. }
  163. /* Verify again using the raw DSA interface */
  164. if (DSA_verify(0, dataToSign, len, signature, sigLength, dsakey) != 1) {
  165. TEST_error("Verification with unpadded data failed, len=%d", len);
  166. goto end;
  167. }
  168. if (DSA_verify(0, paddedData, digestlen, signature, sigLength, dsakey) != 1) {
  169. TEST_error("verify with length %d failed\n", len);
  170. goto end;
  171. }
  172. ok = 1;
  173. end:
  174. EVP_PKEY_CTX_free(ctx);
  175. EVP_PKEY_free(pkey);
  176. OPENSSL_free(signature);
  177. OPENSSL_free(paddedData);
  178. OPENSSL_free(dataToSign);
  179. return ok;
  180. }
  181. static int dsa_exact_size_test(void) {
  182. /*
  183. * For a 2048-bit p, q should be either 224 or 256 bits per the table in
  184. * FIPS 186-4 4.2.
  185. */
  186. return sign_and_verify(224 / 8) && sign_and_verify(256 / 8);
  187. }
  188. static int dsa_small_digest_test(void) {
  189. return sign_and_verify(16) && sign_and_verify(1);
  190. }
  191. static int dsa_large_digest_test(void) {
  192. return sign_and_verify(33) && sign_and_verify(64);
  193. }
  194. void cleanup_tests(void)
  195. {
  196. DSA_free(dsakey);
  197. }
  198. #endif /* OPENSSL_NO_DSA */
  199. int setup_tests(void)
  200. {
  201. #ifndef OPENSSL_NO_DSA
  202. if (!genkeys())
  203. return 0;
  204. ADD_TEST(dsa_exact_size_test);
  205. ADD_TEST(dsa_small_digest_test);
  206. ADD_TEST(dsa_large_digest_test);
  207. #endif
  208. return 1;
  209. }