self_check.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. /* Copyright (c) 2017, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #include <openssl/crypto.h>
  15. #include <stdio.h>
  16. #include <openssl/aead.h>
  17. #include <openssl/aes.h>
  18. #include <openssl/bn.h>
  19. #include <openssl/des.h>
  20. #include <openssl/ecdsa.h>
  21. #include <openssl/ec_key.h>
  22. #include <openssl/nid.h>
  23. #include <openssl/rsa.h>
  24. #include <openssl/sha.h>
  25. #include "../../internal.h"
  26. #include "../ec/internal.h"
  27. #include "../rand/internal.h"
  28. // MSVC wants to put a NUL byte at the end of non-char arrays and so cannot
  29. // compile this.
  30. #if !defined(_MSC_VER)
  31. static void hexdump(const uint8_t *in, size_t len) {
  32. for (size_t i = 0; i < len; i++) {
  33. printf("%02x", in[i]);
  34. }
  35. }
  36. static int check_test(const void *expected, const void *actual,
  37. size_t expected_len, const char *name) {
  38. if (OPENSSL_memcmp(actual, expected, expected_len) != 0) {
  39. printf("%s failed.\nExpected: ", name);
  40. hexdump(expected, expected_len);
  41. printf("\nCalculated: ");
  42. hexdump(actual, expected_len);
  43. printf("\n");
  44. return 0;
  45. }
  46. return 1;
  47. }
  48. static int set_bignum(BIGNUM **out, const uint8_t *in, size_t len) {
  49. *out = BN_bin2bn(in, len, NULL);
  50. return *out != NULL;
  51. }
  52. static RSA *self_test_rsa_key(void) {
  53. static const uint8_t kN[] = {
  54. 0xd3, 0x3a, 0x62, 0x9f, 0x07, 0x77, 0xb0, 0x18, 0xf3, 0xff, 0xfe, 0xcc,
  55. 0xc9, 0xa2, 0xc2, 0x3a, 0xa6, 0x1d, 0xd8, 0xf0, 0x26, 0x5b, 0x38, 0x90,
  56. 0x17, 0x48, 0x15, 0xce, 0x21, 0xcd, 0xd6, 0x62, 0x99, 0xe2, 0xd7, 0xda,
  57. 0x40, 0x80, 0x3c, 0xad, 0x18, 0xb7, 0x26, 0xe9, 0x30, 0x8a, 0x23, 0x3f,
  58. 0x68, 0x9a, 0x9c, 0x31, 0x34, 0x91, 0x99, 0x06, 0x11, 0x36, 0xb2, 0x9e,
  59. 0x3a, 0xd0, 0xbc, 0xb9, 0x93, 0x4e, 0xb8, 0x72, 0xa1, 0x9f, 0xb6, 0x8c,
  60. 0xd5, 0x17, 0x1f, 0x7e, 0xaa, 0x75, 0xbb, 0xdf, 0xa1, 0x70, 0x48, 0xc4,
  61. 0xec, 0x9a, 0x51, 0xed, 0x41, 0xc9, 0x74, 0xc0, 0x3e, 0x1e, 0x85, 0x2f,
  62. 0xbe, 0x34, 0xc7, 0x65, 0x34, 0x8b, 0x4d, 0x55, 0x4b, 0xe1, 0x45, 0x54,
  63. 0x0d, 0x75, 0x7e, 0x89, 0x4d, 0x0c, 0xf6, 0x33, 0xe5, 0xfc, 0xfb, 0x56,
  64. 0x1b, 0xf2, 0x39, 0x9d, 0xe0, 0xff, 0x55, 0xcf, 0x02, 0x05, 0xb9, 0x74,
  65. 0xd2, 0x91, 0xfc, 0x87, 0xe1, 0xbb, 0x97, 0x2a, 0xe4, 0xdd, 0x20, 0xc0,
  66. 0x38, 0x47, 0xc0, 0x76, 0x3f, 0xa1, 0x9b, 0x5c, 0x20, 0xff, 0xff, 0xc7,
  67. 0x49, 0x3b, 0x4c, 0xaf, 0x99, 0xa6, 0x3e, 0x82, 0x5c, 0x58, 0x27, 0xce,
  68. 0x01, 0x03, 0xc3, 0x16, 0x35, 0x20, 0xe9, 0xf0, 0x15, 0x7a, 0x41, 0xd5,
  69. 0x1f, 0x52, 0xea, 0xdf, 0xad, 0x4c, 0xbb, 0x0d, 0xcb, 0x04, 0x91, 0xb0,
  70. 0x95, 0xa8, 0xce, 0x25, 0xfd, 0xd2, 0x62, 0x47, 0x77, 0xee, 0x13, 0xf1,
  71. 0x48, 0x72, 0x9e, 0xd9, 0x2d, 0xe6, 0x5f, 0xa4, 0xc6, 0x9e, 0x5a, 0xb2,
  72. 0xc6, 0xa2, 0xf7, 0x0a, 0x16, 0x17, 0xae, 0x6b, 0x1c, 0x30, 0x7c, 0x63,
  73. 0x08, 0x83, 0xe7, 0x43, 0xec, 0x54, 0x5e, 0x2c, 0x08, 0x0b, 0x5e, 0x46,
  74. 0xa7, 0x10, 0x93, 0x43, 0x53, 0x4e, 0xe3, 0x16, 0x73, 0x55, 0xce, 0xf2,
  75. 0x94, 0xc0, 0xbe, 0xb3,
  76. };
  77. static const uint8_t kE[] = {0x01, 0x00, 0x01}; // 65537
  78. static const uint8_t kD[] = {
  79. 0x2f, 0x2c, 0x1e, 0xd2, 0x3d, 0x2c, 0xb1, 0x9b, 0x21, 0x02, 0xce, 0xb8,
  80. 0x95, 0x5f, 0x4f, 0xd9, 0x21, 0x38, 0x11, 0x36, 0xb0, 0x9a, 0x36, 0xab,
  81. 0x97, 0x47, 0x75, 0xf7, 0x2e, 0xfd, 0x75, 0x1f, 0x58, 0x16, 0x9c, 0xf6,
  82. 0x14, 0xe9, 0x8e, 0xa3, 0x69, 0x9d, 0x9d, 0x86, 0xfe, 0x5c, 0x1b, 0x3b,
  83. 0x11, 0xf5, 0x55, 0x64, 0x77, 0xc4, 0xfc, 0x53, 0xaa, 0x8c, 0x78, 0x9f,
  84. 0x75, 0xab, 0x20, 0x3a, 0xa1, 0x77, 0x37, 0x22, 0x02, 0x8e, 0x54, 0x8a,
  85. 0x67, 0x1c, 0x5e, 0xe0, 0x3e, 0xd9, 0x44, 0x37, 0xd1, 0x29, 0xee, 0x56,
  86. 0x6c, 0x30, 0x9a, 0x93, 0x4d, 0xd9, 0xdb, 0xc5, 0x03, 0x1a, 0x75, 0xcc,
  87. 0x0f, 0xc2, 0x61, 0xb5, 0x6c, 0x62, 0x9f, 0xc6, 0xa8, 0xc7, 0x8a, 0x60,
  88. 0x17, 0x11, 0x62, 0x4c, 0xef, 0x74, 0x31, 0x97, 0xad, 0x89, 0x2d, 0xe8,
  89. 0x31, 0x1d, 0x8b, 0x58, 0x82, 0xe3, 0x03, 0x1a, 0x6b, 0xdf, 0x3f, 0x3e,
  90. 0xa4, 0x27, 0x19, 0xef, 0x46, 0x7a, 0x90, 0xdf, 0xa7, 0xe7, 0xc9, 0x66,
  91. 0xab, 0x41, 0x1d, 0x65, 0x78, 0x1c, 0x18, 0x40, 0x5c, 0xd6, 0x87, 0xb5,
  92. 0xea, 0x29, 0x44, 0xb3, 0xf5, 0xb3, 0xd2, 0x4f, 0xce, 0x88, 0x78, 0x49,
  93. 0x27, 0x4e, 0x0b, 0x30, 0x85, 0xfb, 0x73, 0xfd, 0x8b, 0x32, 0x15, 0xee,
  94. 0x1f, 0xc9, 0x0e, 0x89, 0xb9, 0x43, 0x2f, 0xe9, 0x60, 0x8d, 0xda, 0xae,
  95. 0x2b, 0x30, 0x99, 0xee, 0x88, 0x81, 0x20, 0x7b, 0x4a, 0xc3, 0x18, 0xf2,
  96. 0x94, 0x02, 0x79, 0x94, 0xaa, 0x65, 0xd9, 0x1b, 0x45, 0x2a, 0xac, 0x6e,
  97. 0x30, 0x48, 0x57, 0xea, 0xbe, 0x79, 0x7d, 0xfc, 0x67, 0xaa, 0x47, 0xc0,
  98. 0xf7, 0x52, 0xfd, 0x0b, 0x63, 0x4e, 0x3d, 0x2e, 0xcc, 0x36, 0xa0, 0xdb,
  99. 0x92, 0x0b, 0xa9, 0x1b, 0xeb, 0xc2, 0xd5, 0x08, 0xd3, 0x85, 0x87, 0xf8,
  100. 0x5d, 0x1a, 0xf6, 0xc1,
  101. };
  102. static const uint8_t kP[] = {
  103. 0xf7, 0x06, 0xa3, 0x98, 0x8a, 0x52, 0xf8, 0x63, 0x68, 0x27, 0x4f, 0x68,
  104. 0x7f, 0x34, 0xec, 0x8e, 0x5d, 0xf8, 0x30, 0x92, 0xb3, 0x62, 0x4c, 0xeb,
  105. 0xdb, 0x19, 0x6b, 0x09, 0xc5, 0xa3, 0xf0, 0xbb, 0xff, 0x0f, 0xc2, 0xd4,
  106. 0x9b, 0xc9, 0x54, 0x4f, 0xb9, 0xf9, 0xe1, 0x4c, 0xf0, 0xe3, 0x4c, 0x90,
  107. 0xda, 0x7a, 0x01, 0xc2, 0x9f, 0xc4, 0xc8, 0x8e, 0xb1, 0x1e, 0x93, 0x75,
  108. 0x75, 0xc6, 0x13, 0x25, 0xc3, 0xee, 0x3b, 0xcc, 0xb8, 0x72, 0x6c, 0x49,
  109. 0xb0, 0x09, 0xfb, 0xab, 0x44, 0xeb, 0x4d, 0x40, 0xf0, 0x61, 0x6b, 0xe5,
  110. 0xe6, 0xfe, 0x3e, 0x0a, 0x77, 0x26, 0x39, 0x76, 0x3d, 0x4c, 0x3e, 0x9b,
  111. 0x5b, 0xc0, 0xaf, 0xa2, 0x58, 0x76, 0xb0, 0xe9, 0xda, 0x7f, 0x0e, 0x78,
  112. 0xc9, 0x76, 0x49, 0x5c, 0xfa, 0xb3, 0xb0, 0x15, 0x4b, 0x41, 0xc7, 0x27,
  113. 0xa4, 0x75, 0x28, 0x5c, 0x30, 0x69, 0x50, 0x29,
  114. };
  115. static const uint8_t kQ[] = {
  116. 0xda, 0xe6, 0xd2, 0xbb, 0x44, 0xff, 0x4f, 0xdf, 0x57, 0xc1, 0x11, 0xa3,
  117. 0x51, 0xba, 0x17, 0x89, 0x4c, 0x01, 0xc0, 0x0c, 0x97, 0x34, 0x50, 0xcf,
  118. 0x32, 0x1e, 0xc0, 0xbd, 0x7b, 0x35, 0xb5, 0x6a, 0x26, 0xcc, 0xea, 0x4c,
  119. 0x8e, 0x87, 0x4a, 0x67, 0x8b, 0xd3, 0xe5, 0x4f, 0x3a, 0x60, 0x48, 0x59,
  120. 0x04, 0x93, 0x39, 0xd7, 0x7c, 0xfb, 0x19, 0x1a, 0x34, 0xd5, 0xe8, 0xaf,
  121. 0xe7, 0x22, 0x2c, 0x0d, 0xc2, 0x91, 0x69, 0xb6, 0xe9, 0x2a, 0xe9, 0x1c,
  122. 0x4c, 0x6e, 0x8f, 0x40, 0xf5, 0xa8, 0x3e, 0x82, 0x69, 0x69, 0xbe, 0x9f,
  123. 0x7d, 0x5c, 0x7f, 0x92, 0x78, 0x17, 0xa3, 0x6d, 0x41, 0x2d, 0x72, 0xed,
  124. 0x3f, 0x71, 0xfa, 0x97, 0xb4, 0x63, 0xe4, 0x4f, 0xd9, 0x46, 0x03, 0xfb,
  125. 0x00, 0xeb, 0x30, 0x70, 0xb9, 0x51, 0xd9, 0x0a, 0xd2, 0xf8, 0x50, 0xd4,
  126. 0xfb, 0x43, 0x84, 0xf8, 0xac, 0x58, 0xc3, 0x7b,
  127. };
  128. static const uint8_t kDModPMinusOne[] = {
  129. 0xf5, 0x50, 0x8f, 0x88, 0x7d, 0xdd, 0xb5, 0xb4, 0x2a, 0x8b, 0xd7, 0x4d,
  130. 0x23, 0xfe, 0xaf, 0xe9, 0x16, 0x22, 0xd2, 0x41, 0xed, 0x88, 0xf2, 0x70,
  131. 0xcb, 0x4d, 0xeb, 0xc1, 0x71, 0x97, 0xc4, 0x0b, 0x3e, 0x5a, 0x2d, 0x96,
  132. 0xab, 0xfa, 0xfd, 0x12, 0x8b, 0xd3, 0x3e, 0x4e, 0x05, 0x6f, 0x04, 0xeb,
  133. 0x59, 0x3c, 0x0e, 0xa1, 0x73, 0xbe, 0x9d, 0x99, 0x2f, 0x05, 0xf9, 0x54,
  134. 0x8d, 0x98, 0x1e, 0x0d, 0xc4, 0x0c, 0xc3, 0x30, 0x23, 0xff, 0xe5, 0xd0,
  135. 0x2b, 0xd5, 0x4e, 0x2b, 0xa0, 0xae, 0xb8, 0x32, 0x84, 0x45, 0x8b, 0x3c,
  136. 0x6d, 0xf0, 0x10, 0x36, 0x9e, 0x6a, 0xc4, 0x67, 0xca, 0xa9, 0xfc, 0x06,
  137. 0x96, 0xd0, 0xbc, 0xda, 0xd1, 0x55, 0x55, 0x8d, 0x77, 0x21, 0xf4, 0x82,
  138. 0x39, 0x37, 0x91, 0xd5, 0x97, 0x56, 0x78, 0xc8, 0x3c, 0xcb, 0x5e, 0xf6,
  139. 0xdc, 0x58, 0x48, 0xb3, 0x7c, 0x94, 0x29, 0x39,
  140. };
  141. static const uint8_t kDModQMinusOne[] = {
  142. 0x64, 0x65, 0xbd, 0x7d, 0x1a, 0x96, 0x26, 0xa1, 0xfe, 0xf3, 0x94, 0x0d,
  143. 0x5d, 0xec, 0x85, 0xe2, 0xf8, 0xb3, 0x4c, 0xcb, 0xf9, 0x85, 0x8b, 0x12,
  144. 0x9c, 0xa0, 0x32, 0x32, 0x35, 0x92, 0x5a, 0x94, 0x47, 0x1b, 0x70, 0xd2,
  145. 0x90, 0x04, 0x49, 0x01, 0xd8, 0xc5, 0xe4, 0xc4, 0x43, 0xb7, 0xe9, 0x36,
  146. 0xba, 0xbc, 0x73, 0xa8, 0xfb, 0xaf, 0x86, 0xc1, 0xd8, 0x3d, 0xcb, 0xac,
  147. 0xf1, 0xcb, 0x60, 0x7d, 0x27, 0x21, 0xde, 0x64, 0x7f, 0xe8, 0xa8, 0x65,
  148. 0xcc, 0x40, 0x60, 0xff, 0xa0, 0x2b, 0xfc, 0x0f, 0x80, 0x1d, 0x79, 0xca,
  149. 0x58, 0x8a, 0xd6, 0x0f, 0xed, 0x78, 0x9a, 0x02, 0x00, 0x04, 0xc2, 0x53,
  150. 0x41, 0xe8, 0x1a, 0xd0, 0xfd, 0x71, 0x5b, 0x43, 0xac, 0x19, 0x4a, 0xb6,
  151. 0x12, 0xa3, 0xcb, 0xe1, 0xc7, 0x7d, 0x5c, 0x98, 0x74, 0x4e, 0x63, 0x74,
  152. 0x6b, 0x91, 0x7a, 0x29, 0x3b, 0x92, 0xb2, 0x85,
  153. };
  154. static const uint8_t kQInverseModP[] = {
  155. 0xd0, 0xde, 0x19, 0xda, 0x1e, 0xa2, 0xd8, 0x8f, 0x1c, 0x92, 0x73, 0xb0,
  156. 0xc9, 0x90, 0xc7, 0xf5, 0xec, 0xc5, 0x89, 0x01, 0x05, 0x78, 0x11, 0x2d,
  157. 0x74, 0x34, 0x44, 0xad, 0xd5, 0xf7, 0xa4, 0xfe, 0x9f, 0x25, 0x4d, 0x0b,
  158. 0x92, 0xe3, 0xb8, 0x7d, 0xd3, 0xfd, 0xa5, 0xca, 0x95, 0x60, 0xa3, 0xf9,
  159. 0x55, 0x42, 0x14, 0xb2, 0x45, 0x51, 0x9f, 0x73, 0x88, 0x43, 0x8a, 0xd1,
  160. 0x65, 0x9e, 0xd1, 0xf7, 0x82, 0x2a, 0x2a, 0x8d, 0x70, 0x56, 0xe3, 0xef,
  161. 0xc9, 0x0e, 0x2a, 0x2c, 0x15, 0xaf, 0x7f, 0x97, 0x81, 0x66, 0xf3, 0xb5,
  162. 0x00, 0xa9, 0x26, 0xcc, 0x1e, 0xc2, 0x98, 0xdd, 0xd3, 0x37, 0x06, 0x79,
  163. 0xb3, 0x60, 0x58, 0x79, 0x99, 0x3f, 0xa3, 0x15, 0x1f, 0x31, 0xe3, 0x11,
  164. 0x88, 0x4c, 0x35, 0x57, 0xfa, 0x79, 0xd7, 0xd8, 0x72, 0xee, 0x73, 0x95,
  165. 0x89, 0x29, 0xc7, 0x05, 0x27, 0x68, 0x90, 0x15,
  166. };
  167. RSA *rsa = RSA_new();
  168. if (rsa == NULL ||
  169. !set_bignum(&rsa->n, kN, sizeof(kN)) ||
  170. !set_bignum(&rsa->e, kE, sizeof(kE)) ||
  171. !set_bignum(&rsa->d, kD, sizeof(kD)) ||
  172. !set_bignum(&rsa->p, kP, sizeof(kP)) ||
  173. !set_bignum(&rsa->q, kQ, sizeof(kQ)) ||
  174. !set_bignum(&rsa->dmp1, kDModPMinusOne, sizeof(kDModPMinusOne)) ||
  175. !set_bignum(&rsa->dmq1, kDModQMinusOne, sizeof(kDModQMinusOne)) ||
  176. !set_bignum(&rsa->iqmp, kQInverseModP, sizeof(kQInverseModP))) {
  177. RSA_free(rsa);
  178. return NULL;
  179. }
  180. return rsa;
  181. }
  182. static EC_KEY *self_test_ecdsa_key(void) {
  183. static const uint8_t kQx[] = {
  184. 0xc8, 0x15, 0x61, 0xec, 0xf2, 0xe5, 0x4e, 0xde, 0xfe, 0x66, 0x17,
  185. 0xdb, 0x1c, 0x7a, 0x34, 0xa7, 0x07, 0x44, 0xdd, 0xb2, 0x61, 0xf2,
  186. 0x69, 0xb8, 0x3d, 0xac, 0xfc, 0xd2, 0xad, 0xe5, 0xa6, 0x81,
  187. };
  188. static const uint8_t kQy[] = {
  189. 0xe0, 0xe2, 0xaf, 0xa3, 0xf9, 0xb6, 0xab, 0xe4, 0xc6, 0x98, 0xef,
  190. 0x64, 0x95, 0xf1, 0xbe, 0x49, 0xa3, 0x19, 0x6c, 0x50, 0x56, 0xac,
  191. 0xb3, 0x76, 0x3f, 0xe4, 0x50, 0x7e, 0xec, 0x59, 0x6e, 0x88,
  192. };
  193. static const uint8_t kD[] = {
  194. 0xc6, 0xc1, 0xaa, 0xda, 0x15, 0xb0, 0x76, 0x61, 0xf8, 0x14, 0x2c,
  195. 0x6c, 0xaf, 0x0f, 0xdb, 0x24, 0x1a, 0xff, 0x2e, 0xfe, 0x46, 0xc0,
  196. 0x93, 0x8b, 0x74, 0xf2, 0xbc, 0xc5, 0x30, 0x52, 0xb0, 0x77,
  197. };
  198. EC_KEY *ec_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
  199. BIGNUM *qx = BN_bin2bn(kQx, sizeof(kQx), NULL);
  200. BIGNUM *qy = BN_bin2bn(kQy, sizeof(kQy), NULL);
  201. BIGNUM *d = BN_bin2bn(kD, sizeof(kD), NULL);
  202. if (ec_key == NULL || qx == NULL || qy == NULL || d == NULL ||
  203. !EC_KEY_set_public_key_affine_coordinates(ec_key, qx, qy) ||
  204. !EC_KEY_set_private_key(ec_key, d)) {
  205. EC_KEY_free(ec_key);
  206. ec_key = NULL;
  207. }
  208. BN_free(qx);
  209. BN_free(qy);
  210. BN_free(d);
  211. return ec_key;
  212. }
  213. int BORINGSSL_self_test(void) {
  214. static const uint8_t kAESKey[16] = "BoringCrypto Key";
  215. static const uint8_t kAESIV[16] = {0};
  216. static const uint8_t kPlaintext[64] =
  217. "BoringCryptoModule FIPS KAT Encryption and Decryption Plaintext!";
  218. static const uint8_t kAESCBCCiphertext[64] = {
  219. 0x87, 0x2d, 0x98, 0xc2, 0xcc, 0x31, 0x5b, 0x41, 0xe0, 0xfa, 0x7b,
  220. 0x0a, 0x71, 0xc0, 0x42, 0xbf, 0x4f, 0x61, 0xd0, 0x0d, 0x58, 0x8c,
  221. 0xf7, 0x05, 0xfb, 0x94, 0x89, 0xd3, 0xbc, 0xaa, 0x1a, 0x50, 0x45,
  222. 0x1f, 0xc3, 0x8c, 0xb8, 0x98, 0x86, 0xa3, 0xe3, 0x6c, 0xfc, 0xad,
  223. 0x3a, 0xb5, 0x59, 0x27, 0x7d, 0x21, 0x07, 0xca, 0x4c, 0x1d, 0x55,
  224. 0x34, 0xdd, 0x5a, 0x2d, 0xc4, 0xb4, 0xf5, 0xa8,
  225. #if !defined(BORINGSSL_FIPS_BREAK_AES_CBC)
  226. 0x35
  227. #else
  228. 0x00
  229. #endif
  230. };
  231. static const uint8_t kAESGCMCiphertext[80] = {
  232. 0x4a, 0xd8, 0xe7, 0x7d, 0x78, 0xd7, 0x7d, 0x5e, 0xb2, 0x11, 0xb6, 0xc9,
  233. 0xa4, 0xbc, 0xb2, 0xae, 0xbe, 0x93, 0xd1, 0xb7, 0xfe, 0x65, 0xc1, 0x82,
  234. 0x2a, 0xb6, 0x71, 0x5f, 0x1a, 0x7c, 0xe0, 0x1b, 0x2b, 0xe2, 0x53, 0xfa,
  235. 0xa0, 0x47, 0xfa, 0xd7, 0x8f, 0xb1, 0x4a, 0xc4, 0xdc, 0x89, 0xf9, 0xb4,
  236. 0x14, 0x4d, 0xde, 0x95, 0xea, 0x29, 0x69, 0x76, 0x81, 0xa3, 0x5c, 0x33,
  237. 0xd8, 0x37, 0xd8, 0xfa, 0x47, 0x19, 0x46, 0x2f, 0xf1, 0x90, 0xb7, 0x61,
  238. 0x8f, 0x6f, 0xdd, 0x31, 0x3f, 0x6a, 0x64,
  239. #if !defined(BORINGSSL_FIPS_BREAK_AES_GCM)
  240. 0x0d
  241. #else
  242. 0x00
  243. #endif
  244. };
  245. static const DES_cblock kDESKey1 = {"BCMDESK1"};
  246. static const DES_cblock kDESKey2 = {"BCMDESK2"};
  247. static const DES_cblock kDESKey3 = {"BCMDESK3"};
  248. static const DES_cblock kDESIV = {"BCMDESIV"};
  249. static const uint8_t kDESCiphertext[64] = {
  250. 0xa4, 0x30, 0x7a, 0x4c, 0x1f, 0x60, 0x16, 0xd7, 0x4f, 0x41, 0xe1,
  251. 0xbb, 0x27, 0xc4, 0x27, 0x37, 0xd4, 0x7f, 0xb9, 0x10, 0xf8, 0xbc,
  252. 0xaf, 0x93, 0x91, 0xb8, 0x88, 0x24, 0xb1, 0xf6, 0xf8, 0xbd, 0x31,
  253. 0x96, 0x06, 0x76, 0xde, 0x32, 0xcd, 0x29, 0x29, 0xba, 0x70, 0x5f,
  254. 0xea, 0xc0, 0xcb, 0xde, 0xc7, 0x75, 0x90, 0xe0, 0x0f, 0x5e, 0x2c,
  255. 0x0d, 0x49, 0x20, 0xd5, 0x30, 0x83, 0xf8, 0x08,
  256. #if !defined(BORINGSSL_FIPS_BREAK_DES)
  257. 0x5a
  258. #else
  259. 0x00
  260. #endif
  261. };
  262. static const uint8_t kPlaintextSHA1[20] = {
  263. 0xc6, 0xf8, 0xc9, 0x63, 0x1c, 0x14, 0x23, 0x62, 0x9b, 0xbd,
  264. 0x55, 0x82, 0xf4, 0xd6, 0x1d, 0xf2, 0xab, 0x7d, 0xc8,
  265. #if !defined(BORINGSSL_FIPS_BREAK_SHA_1)
  266. 0x28
  267. #else
  268. 0x00
  269. #endif
  270. };
  271. static const uint8_t kPlaintextSHA256[32] = {
  272. 0x37, 0xbd, 0x70, 0x53, 0x72, 0xfc, 0xd4, 0x03, 0x79, 0x70, 0xfb,
  273. 0x06, 0x95, 0xb1, 0x2a, 0x82, 0x48, 0xe1, 0x3e, 0xf2, 0x33, 0xfb,
  274. 0xef, 0x29, 0x81, 0x22, 0x45, 0x40, 0x43, 0x70, 0xce,
  275. #if !defined(BORINGSSL_FIPS_BREAK_SHA_256)
  276. 0x0f
  277. #else
  278. 0x00
  279. #endif
  280. };
  281. static const uint8_t kPlaintextSHA512[64] = {
  282. 0x08, 0x6a, 0x1c, 0x84, 0x61, 0x9d, 0x8e, 0xb3, 0xc0, 0x97, 0x4e,
  283. 0xa1, 0x9f, 0x9c, 0xdc, 0xaf, 0x3b, 0x5c, 0x31, 0xf0, 0xf2, 0x74,
  284. 0xc3, 0xbd, 0x6e, 0xd6, 0x1e, 0xb2, 0xbb, 0x34, 0x74, 0x72, 0x5c,
  285. 0x51, 0x29, 0x8b, 0x87, 0x3a, 0xa3, 0xf2, 0x25, 0x23, 0xd4, 0x1c,
  286. 0x82, 0x1b, 0xfe, 0xd3, 0xc6, 0xee, 0xb5, 0xd6, 0xaf, 0x07, 0x7b,
  287. 0x98, 0xca, 0xa7, 0x01, 0xf3, 0x94, 0xf3, 0x68,
  288. #if !defined(BORINGSSL_FIPS_BREAK_SHA_512)
  289. 0x14
  290. #else
  291. 0x00
  292. #endif
  293. };
  294. static const uint8_t kRSASignature[256] = {
  295. 0x62, 0x66, 0x4b, 0xe3, 0xb1, 0xd2, 0x83, 0xf1, 0xa8, 0x56, 0x2b, 0x33,
  296. 0x60, 0x1e, 0xdb, 0x1e, 0x06, 0xf7, 0xa7, 0x1e, 0xa8, 0xef, 0x03, 0x4d,
  297. 0x0c, 0xf6, 0x83, 0x75, 0x7a, 0xf0, 0x14, 0xc7, 0xe2, 0x94, 0x3a, 0xb5,
  298. 0x67, 0x56, 0xa5, 0x48, 0x7f, 0x3a, 0xa5, 0xbf, 0xf7, 0x1d, 0x44, 0xa6,
  299. 0x34, 0xed, 0x9b, 0xd6, 0x51, 0xaa, 0x2c, 0x4e, 0xce, 0x60, 0x5f, 0xe9,
  300. 0x0e, 0xd5, 0xcd, 0xeb, 0x23, 0x27, 0xf8, 0xfb, 0x45, 0xe5, 0x34, 0x63,
  301. 0x77, 0x7f, 0x2e, 0x80, 0xcf, 0x9d, 0x2e, 0xfc, 0xe2, 0x50, 0x75, 0x29,
  302. 0x46, 0xf4, 0xaf, 0x91, 0xed, 0x36, 0xe1, 0x5e, 0xef, 0x66, 0xa1, 0xff,
  303. 0x27, 0xfc, 0x87, 0x7e, 0x60, 0x84, 0x0f, 0x54, 0x51, 0x56, 0x0f, 0x68,
  304. 0x99, 0xc0, 0x3f, 0xeb, 0xa5, 0xa0, 0x46, 0xb0, 0x86, 0x02, 0xb0, 0xc8,
  305. 0xe8, 0x46, 0x13, 0x06, 0xcd, 0xb7, 0x8a, 0xd0, 0x3b, 0x46, 0xd0, 0x14,
  306. 0x64, 0x53, 0x9b, 0x5b, 0x5e, 0x02, 0x45, 0xba, 0x6e, 0x7e, 0x0a, 0xb9,
  307. 0x9e, 0x62, 0xb7, 0xd5, 0x7a, 0x87, 0xea, 0xd3, 0x24, 0xa5, 0xef, 0xb3,
  308. 0xdc, 0x05, 0x9c, 0x04, 0x60, 0x4b, 0xde, 0xa8, 0x90, 0x08, 0x7b, 0x6a,
  309. 0x5f, 0xb4, 0x3f, 0xda, 0xc5, 0x1f, 0x6e, 0xd6, 0x15, 0xde, 0x65, 0xa4,
  310. 0x6e, 0x62, 0x9d, 0x8f, 0xa8, 0xbe, 0x86, 0xf6, 0x09, 0x90, 0x40, 0xa5,
  311. 0xf4, 0x23, 0xc5, 0xf6, 0x38, 0x86, 0x0d, 0x1c, 0xed, 0x4a, 0x0a, 0xae,
  312. 0xa4, 0x26, 0xc2, 0x2e, 0xd3, 0x13, 0x66, 0x61, 0xea, 0x35, 0x01, 0x0e,
  313. 0x13, 0xda, 0x78, 0x20, 0xae, 0x59, 0x5f, 0x9b, 0xa9, 0x6c, 0xf9, 0x1b,
  314. 0xdf, 0x76, 0x53, 0xc8, 0xa7, 0xf5, 0x63, 0x6d, 0xf3, 0xff, 0xfd, 0xaf,
  315. 0x75, 0x4b, 0xac, 0x67, 0xb1, 0x3c, 0xbf, 0x5e, 0xde, 0x73, 0x02, 0x6d,
  316. 0xd2, 0x0c, 0xb1,
  317. #if !defined(BORINGSSL_FIPS_BREAK_RSA_SIG)
  318. 0x64
  319. #else
  320. 0x00
  321. #endif
  322. };
  323. const uint8_t kDRBGEntropy[48] =
  324. "BCM Known Answer Test DBRG Initial Entropy ";
  325. const uint8_t kDRBGPersonalization[18] = "BCMPersonalization";
  326. const uint8_t kDRBGAD[16] = "BCM DRBG KAT AD ";
  327. const uint8_t kDRBGOutput[64] = {
  328. 0x1d, 0x63, 0xdf, 0x05, 0x51, 0x49, 0x22, 0x46, 0xcd, 0x9b, 0xc5,
  329. 0xbb, 0xf1, 0x5d, 0x44, 0xae, 0x13, 0x78, 0xb1, 0xe4, 0x7c, 0xf1,
  330. 0x96, 0x33, 0x3d, 0x60, 0xb6, 0x29, 0xd4, 0xbb, 0x6b, 0x44, 0xf9,
  331. 0xef, 0xd9, 0xf4, 0xa2, 0xba, 0x48, 0xea, 0x39, 0x75, 0x59, 0x32,
  332. 0xf7, 0x31, 0x2c, 0x98, 0x14, 0x2b, 0x49, 0xdf, 0x02, 0xb6, 0x5d,
  333. 0x71, 0x09, 0x50, 0xdb, 0x23, 0xdb, 0xe5, 0x22,
  334. #if !defined(BORINGSSL_FIPS_BREAK_DRBG)
  335. 0x95
  336. #else
  337. 0x00
  338. #endif
  339. };
  340. const uint8_t kDRBGEntropy2[48] =
  341. "BCM Known Answer Test DBRG Reseed Entropy ";
  342. const uint8_t kDRBGReseedOutput[64] = {
  343. 0xa4, 0x77, 0x05, 0xdb, 0x14, 0x11, 0x76, 0x71, 0x42, 0x5b, 0xd8,
  344. 0xd7, 0xa5, 0x4f, 0x8b, 0x39, 0xf2, 0x10, 0x4a, 0x50, 0x5b, 0xa2,
  345. 0xc8, 0xf0, 0xbb, 0x3e, 0xa1, 0xa5, 0x90, 0x7d, 0x54, 0xd9, 0xc6,
  346. 0xb0, 0x96, 0xc0, 0x2b, 0x7e, 0x9b, 0xc9, 0xa1, 0xdd, 0x78, 0x2e,
  347. 0xd5, 0xa8, 0x66, 0x16, 0xbd, 0x18, 0x3c, 0xf2, 0xaa, 0x7a, 0x2b,
  348. 0x37, 0xf9, 0xab, 0x35, 0x64, 0x15, 0x01, 0x3f, 0xc4,
  349. };
  350. const uint8_t kECDSASigR[32] = {
  351. 0x67, 0x80, 0xc5, 0xfc, 0x70, 0x27, 0x5e, 0x2c, 0x70, 0x61, 0xa0,
  352. 0xe7, 0x87, 0x7b, 0xb1, 0x74, 0xde, 0xad, 0xeb, 0x98, 0x87, 0x02,
  353. 0x7f, 0x3f, 0xa8, 0x36, 0x54, 0x15, 0x8b, 0xa7, 0xf5,
  354. #if !defined(BORINGSSL_FIPS_BREAK_ECDSA_SIG)
  355. 0x0c,
  356. #else
  357. 0x00,
  358. #endif
  359. };
  360. const uint8_t kECDSASigS[32] = {
  361. 0xa5, 0x93, 0xe0, 0x23, 0x91, 0xe7, 0x4b, 0x8d, 0x77, 0x25, 0xa6,
  362. 0xba, 0x4d, 0xd9, 0x86, 0x77, 0xda, 0x7d, 0x8f, 0xef, 0xc4, 0x1a,
  363. 0xf0, 0xcc, 0x81, 0xe5, 0xea, 0x3f, 0xc2, 0x41, 0x7f, 0xd8,
  364. };
  365. EVP_AEAD_CTX aead_ctx;
  366. EVP_AEAD_CTX_zero(&aead_ctx);
  367. RSA *rsa_key = NULL;
  368. EC_KEY *ec_key = NULL;
  369. ECDSA_SIG *sig = NULL;
  370. int ret = 0;
  371. AES_KEY aes_key;
  372. uint8_t aes_iv[16];
  373. uint8_t output[256];
  374. // AES-CBC Encryption KAT
  375. memcpy(aes_iv, kAESIV, sizeof(kAESIV));
  376. if (AES_set_encrypt_key(kAESKey, 8 * sizeof(kAESKey), &aes_key) != 0) {
  377. goto err;
  378. }
  379. AES_cbc_encrypt(kPlaintext, output, sizeof(kPlaintext), &aes_key, aes_iv,
  380. AES_ENCRYPT);
  381. if (!check_test(kAESCBCCiphertext, output, sizeof(kAESCBCCiphertext),
  382. "AES-CBC Encryption KAT")) {
  383. goto err;
  384. }
  385. // AES-CBC Decryption KAT
  386. memcpy(aes_iv, kAESIV, sizeof(kAESIV));
  387. if (AES_set_decrypt_key(kAESKey, 8 * sizeof(kAESKey), &aes_key) != 0) {
  388. goto err;
  389. }
  390. AES_cbc_encrypt(kAESCBCCiphertext, output, sizeof(kAESCBCCiphertext),
  391. &aes_key, aes_iv, AES_DECRYPT);
  392. if (!check_test(kPlaintext, output, sizeof(kPlaintext),
  393. "AES-CBC Decryption KAT")) {
  394. goto err;
  395. }
  396. size_t out_len;
  397. uint8_t nonce[EVP_AEAD_MAX_NONCE_LENGTH];
  398. OPENSSL_memset(nonce, 0, sizeof(nonce));
  399. if (!EVP_AEAD_CTX_init(&aead_ctx, EVP_aead_aes_128_gcm(), kAESKey,
  400. sizeof(kAESKey), 0, NULL)) {
  401. goto err;
  402. }
  403. // AES-GCM Encryption KAT
  404. if (!EVP_AEAD_CTX_seal(&aead_ctx, output, &out_len, sizeof(output), nonce,
  405. EVP_AEAD_nonce_length(EVP_aead_aes_128_gcm()),
  406. kPlaintext, sizeof(kPlaintext), NULL, 0) ||
  407. !check_test(kAESGCMCiphertext, output, sizeof(kAESGCMCiphertext),
  408. "AES-GCM Encryption KAT")) {
  409. goto err;
  410. }
  411. // AES-GCM Decryption KAT
  412. if (!EVP_AEAD_CTX_open(&aead_ctx, output, &out_len, sizeof(output), nonce,
  413. EVP_AEAD_nonce_length(EVP_aead_aes_128_gcm()),
  414. kAESGCMCiphertext, sizeof(kAESGCMCiphertext), NULL,
  415. 0) ||
  416. !check_test(kPlaintext, output, sizeof(kPlaintext),
  417. "AES-GCM Decryption KAT")) {
  418. goto err;
  419. }
  420. DES_key_schedule des1, des2, des3;
  421. DES_cblock des_iv;
  422. DES_set_key(&kDESKey1, &des1);
  423. DES_set_key(&kDESKey2, &des2);
  424. DES_set_key(&kDESKey3, &des3);
  425. // 3DES Encryption KAT
  426. memcpy(&des_iv, &kDESIV, sizeof(des_iv));
  427. DES_ede3_cbc_encrypt(kPlaintext, output, sizeof(kPlaintext), &des1, &des2,
  428. &des3, &des_iv, DES_ENCRYPT);
  429. if (!check_test(kDESCiphertext, output, sizeof(kDESCiphertext),
  430. "3DES Encryption KAT")) {
  431. goto err;
  432. }
  433. // 3DES Decryption KAT
  434. memcpy(&des_iv, &kDESIV, sizeof(des_iv));
  435. DES_ede3_cbc_encrypt(kDESCiphertext, output, sizeof(kDESCiphertext), &des1,
  436. &des2, &des3, &des_iv, DES_DECRYPT);
  437. if (!check_test(kPlaintext, output, sizeof(kPlaintext),
  438. "3DES Decryption KAT")) {
  439. goto err;
  440. }
  441. // SHA-1 KAT
  442. SHA1(kPlaintext, sizeof(kPlaintext), output);
  443. if (!check_test(kPlaintextSHA1, output, sizeof(kPlaintextSHA1),
  444. "SHA-1 KAT")) {
  445. goto err;
  446. }
  447. // SHA-256 KAT
  448. SHA256(kPlaintext, sizeof(kPlaintext), output);
  449. if (!check_test(kPlaintextSHA256, output, sizeof(kPlaintextSHA256),
  450. "SHA-256 KAT")) {
  451. goto err;
  452. }
  453. // SHA-512 KAT
  454. SHA512(kPlaintext, sizeof(kPlaintext), output);
  455. if (!check_test(kPlaintextSHA512, output, sizeof(kPlaintextSHA512),
  456. "SHA-512 KAT")) {
  457. goto err;
  458. }
  459. rsa_key = self_test_rsa_key();
  460. if (rsa_key == NULL) {
  461. printf("RSA KeyGen failed\n");
  462. goto err;
  463. }
  464. // RSA Sign KAT
  465. unsigned sig_len;
  466. // Disable blinding for the power-on tests because it's not needed and
  467. // triggers an entropy draw.
  468. rsa_key->flags |= RSA_FLAG_NO_BLINDING;
  469. if (!RSA_sign(NID_sha256, kPlaintextSHA256, sizeof(kPlaintextSHA256), output,
  470. &sig_len, rsa_key) ||
  471. !check_test(kRSASignature, output, sizeof(kRSASignature),
  472. "RSA Sign KAT")) {
  473. goto err;
  474. }
  475. // RSA Verify KAT
  476. if (!RSA_verify(NID_sha256, kPlaintextSHA256, sizeof(kPlaintextSHA256),
  477. kRSASignature, sizeof(kRSASignature), rsa_key)) {
  478. printf("RSA Verify KAT failed.\n");
  479. goto err;
  480. }
  481. ec_key = self_test_ecdsa_key();
  482. if (ec_key == NULL) {
  483. printf("ECDSA KeyGen failed\n");
  484. goto err;
  485. }
  486. // ECDSA Sign/Verify PWCT
  487. // The 'k' value for ECDSA is fixed to avoid an entropy draw.
  488. ec_key->fixed_k = BN_new();
  489. if (ec_key->fixed_k == NULL ||
  490. !BN_set_word(ec_key->fixed_k, 42)) {
  491. printf("Out of memory\n");
  492. goto err;
  493. }
  494. sig = ECDSA_do_sign(kPlaintextSHA256, sizeof(kPlaintextSHA256), ec_key);
  495. uint8_t ecdsa_r_bytes[sizeof(kECDSASigR)];
  496. uint8_t ecdsa_s_bytes[sizeof(kECDSASigS)];
  497. if (sig == NULL ||
  498. BN_num_bytes(sig->r) != sizeof(ecdsa_r_bytes) ||
  499. !BN_bn2bin(sig->r, ecdsa_r_bytes) ||
  500. BN_num_bytes(sig->s) != sizeof(ecdsa_s_bytes) ||
  501. !BN_bn2bin(sig->s, ecdsa_s_bytes) ||
  502. !check_test(kECDSASigR, ecdsa_r_bytes, sizeof(kECDSASigR), "ECDSA R") ||
  503. !check_test(kECDSASigS, ecdsa_s_bytes, sizeof(kECDSASigS), "ECDSA S")) {
  504. printf("ECDSA KAT failed.\n");
  505. goto err;
  506. }
  507. // DBRG KAT
  508. CTR_DRBG_STATE drbg;
  509. if (!CTR_DRBG_init(&drbg, kDRBGEntropy, kDRBGPersonalization,
  510. sizeof(kDRBGPersonalization)) ||
  511. !CTR_DRBG_generate(&drbg, output, sizeof(kDRBGOutput), kDRBGAD,
  512. sizeof(kDRBGAD)) ||
  513. !check_test(kDRBGOutput, output, sizeof(kDRBGOutput),
  514. "DBRG Generate KAT") ||
  515. !CTR_DRBG_reseed(&drbg, kDRBGEntropy2, kDRBGAD, sizeof(kDRBGAD)) ||
  516. !CTR_DRBG_generate(&drbg, output, sizeof(kDRBGReseedOutput), kDRBGAD,
  517. sizeof(kDRBGAD)) ||
  518. !check_test(kDRBGReseedOutput, output, sizeof(kDRBGReseedOutput),
  519. "DRBG Reseed KAT")) {
  520. goto err;
  521. }
  522. CTR_DRBG_clear(&drbg);
  523. CTR_DRBG_STATE kZeroDRBG;
  524. memset(&kZeroDRBG, 0, sizeof(kZeroDRBG));
  525. if (!check_test(&kZeroDRBG, &drbg, sizeof(drbg), "DRBG Clear KAT")) {
  526. goto err;
  527. }
  528. ret = 1;
  529. err:
  530. EVP_AEAD_CTX_cleanup(&aead_ctx);
  531. RSA_free(rsa_key);
  532. EC_KEY_free(ec_key);
  533. ECDSA_SIG_free(sig);
  534. return ret;
  535. }
  536. #endif // !_MSC_VER