dsa_no_digest_size_test.c 8.5 KB

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