fips_ecdsa_selftest.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* fips/ecdsa/fips_ecdsa_selftest.c */
  2. /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  3. * project 2011.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2011 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * licensing@OpenSSL.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. */
  54. #define OPENSSL_FIPSAPI
  55. #include <string.h>
  56. #include <openssl/crypto.h>
  57. #include <openssl/ec.h>
  58. #include <openssl/ecdsa.h>
  59. #include <openssl/fips.h>
  60. #include <openssl/err.h>
  61. #include <openssl/evp.h>
  62. #include <openssl/bn.h>
  63. #ifdef OPENSSL_FIPS
  64. static const char P_384_name[] = "ECDSA P-384";
  65. static const unsigned char P_384_d[] = {
  66. 0x1d,0x84,0x42,0xde,0xa2,0x35,0x29,0xbd,0x9f,0xe2,0x6e,0x6d,
  67. 0x01,0x26,0x30,0x79,0x33,0x57,0x01,0xf3,0x97,0x88,0x41,0xb3,
  68. 0x82,0x07,0x08,0x5e,0x63,0x8e,0x1a,0xa6,0x9b,0x08,0xb6,0xe2,
  69. 0xa2,0x98,0xac,0x1c,0x9b,0x25,0xb3,0xf1,0x5c,0x20,0xe9,0x85
  70. };
  71. static const unsigned char P_384_qx[] = {
  72. 0x6b,0x7e,0x9c,0xbb,0x3d,0xc5,0x4d,0x53,0xf7,0x6c,0x8d,0xcc,
  73. 0xf8,0xc3,0xa8,0x26,0xba,0xeb,0xa6,0x56,0x6a,0x41,0x98,0xb1,
  74. 0x90,0x90,0xcc,0xe7,0x48,0x74,0x3d,0xe6,0xd7,0x65,0x90,0x3b,
  75. 0x13,0x69,0xdc,0x8f,0x48,0xc1,0xb4,0xf4,0xb1,0x91,0x36,0x3f
  76. };
  77. static const unsigned char P_384_qy[] = {
  78. 0x40,0xc2,0x62,0x2a,0xea,0xfb,0x47,0x75,0xb5,0xdc,0x2e,0x1e,
  79. 0xa0,0xa9,0x1f,0x6a,0xb7,0x54,0xac,0xce,0x91,0xe8,0x5b,0x8c,
  80. 0xe3,0xf5,0xb8,0x0e,0xcb,0x82,0xb0,0xd9,0x57,0x1d,0xeb,0x25,
  81. 0xfc,0x03,0xe5,0x12,0x50,0x17,0x98,0x7f,0x14,0x7e,0x95,0x17
  82. };
  83. #ifndef OPENSSL_NO_EC2M
  84. static const char K_409_name[] = "ECDSA K-409";
  85. static const unsigned char K_409_d[] = {
  86. 0x68,0xe1,0x64,0x0a,0xe6,0x80,0x57,0x53,0x8d,0x35,0xd1,0xec,
  87. 0x69,0xea,0x82,0x05,0x47,0x48,0x4d,0xda,0x9f,0x8c,0xa0,0xf3,
  88. 0x06,0xc7,0x77,0xcb,0x14,0x05,0x9f,0x5d,0xdd,0xe0,0x5d,0x68,
  89. 0x4e,0x1a,0xe4,0x9c,0xe0,0x4d,0x4a,0x74,0x47,0x54,0x4e,0x55,
  90. 0xae,0x70,0x8c
  91. };
  92. static const unsigned char K_409_qx[] = {
  93. 0x01,0x07,0xd6,0x6f,0xa8,0xf8,0x0e,0xbb,0xb8,0xa7,0x83,0x04,
  94. 0xc3,0x19,0x67,0x9e,0x73,0x7b,0xeb,0xf4,0x6c,0xf3,0xeb,0xda,
  95. 0x0d,0xe7,0x60,0xaf,0x29,0x37,0x13,0x32,0x51,0xac,0xb6,0x35,
  96. 0x00,0x60,0xfa,0xd5,0x8b,0x6d,0xae,0xb0,0xe9,0x46,0x7f,0xe2,
  97. 0x2d,0x50,0x04,0x40
  98. };
  99. static const unsigned char K_409_qy[] = {
  100. 0x0a,0x53,0xf1,0x4f,0x2a,0xa5,0x5a,0xfb,0x37,0xb4,0x76,0x47,
  101. 0x1b,0x14,0xd1,0x8d,0x86,0x94,0x75,0x26,0xc3,0x0b,0x09,0x57,
  102. 0x1d,0x26,0x38,0x33,0x84,0x97,0x9d,0x56,0xe1,0x0d,0x51,0x9b,
  103. 0x2c,0xbb,0x3d,0x92,0x48,0xaa,0x2a,0x39,0x4f,0x07,0x92,0xbd,
  104. 0xb0,0x4d,0x2e
  105. };
  106. #endif
  107. typedef struct
  108. {
  109. int curve;
  110. const char *name;
  111. const unsigned char *x;
  112. size_t xlen;
  113. const unsigned char *y;
  114. size_t ylen;
  115. const unsigned char *d;
  116. size_t dlen;
  117. } EC_SELFTEST_DATA;
  118. #define make_ecdsa_test(nid, pr) { nid, pr##_name, \
  119. pr##_qx, sizeof(pr##_qx), \
  120. pr##_qy, sizeof(pr##_qy), \
  121. pr##_d, sizeof(pr##_d)}
  122. static EC_SELFTEST_DATA test_ec_data[] =
  123. {
  124. make_ecdsa_test(NID_secp384r1, P_384),
  125. #ifndef OPENSSL_NO_EC2M
  126. make_ecdsa_test(NID_sect409k1, K_409)
  127. #endif
  128. };
  129. int FIPS_selftest_ecdsa()
  130. {
  131. EC_KEY *ec = NULL;
  132. BIGNUM *x = NULL, *y = NULL, *d = NULL;
  133. EVP_PKEY pk;
  134. int rv = 0;
  135. size_t i;
  136. for (i = 0; i < sizeof(test_ec_data)/sizeof(EC_SELFTEST_DATA); i++)
  137. {
  138. EC_SELFTEST_DATA *ecd = test_ec_data + i;
  139. x = BN_bin2bn(ecd->x, ecd->xlen, x);
  140. y = BN_bin2bn(ecd->y, ecd->ylen, y);
  141. d = BN_bin2bn(ecd->d, ecd->dlen, d);
  142. if (!x || !y || !d)
  143. goto err;
  144. ec = EC_KEY_new_by_curve_name(ecd->curve);
  145. if (!ec)
  146. goto err;
  147. if (!EC_KEY_set_public_key_affine_coordinates(ec, x, y))
  148. goto err;
  149. if (!EC_KEY_set_private_key(ec, d))
  150. goto err;
  151. pk.type = EVP_PKEY_EC;
  152. pk.pkey.ec = ec;
  153. if (!fips_pkey_signature_test(FIPS_TEST_SIGNATURE, &pk, NULL, 0,
  154. NULL, 0, EVP_sha512(), 0,
  155. ecd->name))
  156. goto err;
  157. EC_KEY_free(ec);
  158. ec = NULL;
  159. }
  160. rv = 1;
  161. err:
  162. if (x)
  163. BN_clear_free(x);
  164. if (y)
  165. BN_clear_free(y);
  166. if (d)
  167. BN_clear_free(d);
  168. if (ec)
  169. EC_KEY_free(ec);
  170. return rv;
  171. }
  172. #endif