25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

260 satır
8.1 KiB

  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. /* test_fips exercises various cryptographic primitives for demonstration
  15. * purposes in the validation process only. */
  16. #include <stdio.h>
  17. #include <openssl/aead.h>
  18. #include <openssl/aes.h>
  19. #include <openssl/bn.h>
  20. #include <openssl/crypto.h>
  21. #include <openssl/des.h>
  22. #include <openssl/ecdsa.h>
  23. #include <openssl/ec_key.h>
  24. #include <openssl/hmac.h>
  25. #include <openssl/nid.h>
  26. #include <openssl/rsa.h>
  27. #include <openssl/sha.h>
  28. #include "../crypto/fipsmodule/rand/internal.h"
  29. #include "../crypto/internal.h"
  30. static void hexdump(const void *a, size_t len) {
  31. const unsigned char *in = (const unsigned char *)a;
  32. for (size_t i = 0; i < len; i++) {
  33. printf("%02x", in[i]);
  34. }
  35. printf("\n");
  36. }
  37. int main(int argc, char **argv) {
  38. CRYPTO_library_init();
  39. static const uint8_t kAESKey[16] = "BoringCrypto Key";
  40. static const uint8_t kPlaintext[64] =
  41. "BoringCryptoModule FIPS KAT Encryption and Decryption Plaintext!";
  42. static const DES_cblock kDESKey1 = {"BCMDESK1"};
  43. static const DES_cblock kDESKey2 = {"BCMDESK2"};
  44. static const DES_cblock kDESKey3 = {"BCMDESK3"};
  45. static const DES_cblock kDESIV = {"BCMDESIV"};
  46. static const uint8_t kPlaintextSHA256[32] = {
  47. 0x37, 0xbd, 0x70, 0x53, 0x72, 0xfc, 0xd4, 0x03, 0x79, 0x70, 0xfb,
  48. 0x06, 0x95, 0xb1, 0x2a, 0x82, 0x48, 0xe1, 0x3e, 0xf2, 0x33, 0xfb,
  49. 0xef, 0x29, 0x81, 0x22, 0x45, 0x40, 0x43, 0x70, 0xce, 0x0f};
  50. const uint8_t kDRBGEntropy[48] =
  51. "DBRG Initial Entropy ";
  52. const uint8_t kDRBGPersonalization[18] = "BCMPersonalization";
  53. const uint8_t kDRBGAD[16] = "BCM DRBG AD ";
  54. const uint8_t kDRBGEntropy2[48] =
  55. "DBRG Reseed Entropy ";
  56. AES_KEY aes_key;
  57. uint8_t aes_iv[16];
  58. uint8_t output[256];
  59. /* AES-CBC Encryption */
  60. memset(aes_iv, 0, sizeof(aes_iv));
  61. if (AES_set_encrypt_key(kAESKey, 8 * sizeof(kAESKey), &aes_key) != 0) {
  62. printf("AES_set_encrypt_key failed\n");
  63. goto err;
  64. }
  65. printf("About to AES-CBC encrypt ");
  66. hexdump(kPlaintext, sizeof(kPlaintext));
  67. AES_cbc_encrypt(kPlaintext, output, sizeof(kPlaintext), &aes_key, aes_iv,
  68. AES_ENCRYPT);
  69. printf(" got ");
  70. hexdump(output, sizeof(kPlaintext));
  71. /* AES-CBC Decryption */
  72. memset(aes_iv, 0, sizeof(aes_iv));
  73. if (AES_set_decrypt_key(kAESKey, 8 * sizeof(kAESKey), &aes_key) != 0) {
  74. printf("AES decrypt failed\n");
  75. goto err;
  76. }
  77. printf("About to AES-CBC decrypt ");
  78. hexdump(output, sizeof(kPlaintext));
  79. AES_cbc_encrypt(output, output, sizeof(kPlaintext), &aes_key, aes_iv,
  80. AES_DECRYPT);
  81. printf(" got ");
  82. hexdump(output, sizeof(kPlaintext));
  83. size_t out_len;
  84. uint8_t nonce[EVP_AEAD_MAX_NONCE_LENGTH];
  85. OPENSSL_memset(nonce, 0, sizeof(nonce));
  86. EVP_AEAD_CTX aead_ctx;
  87. if (!EVP_AEAD_CTX_init(&aead_ctx, EVP_aead_aes_128_gcm(), kAESKey,
  88. sizeof(kAESKey), 0, NULL)) {
  89. printf("EVP_AEAD_CTX_init failed\n");
  90. goto err;
  91. }
  92. /* AES-GCM Encryption */
  93. printf("About to AES-GCM seal ");
  94. hexdump(output, sizeof(kPlaintext));
  95. if (!EVP_AEAD_CTX_seal(&aead_ctx, output, &out_len, sizeof(output), nonce,
  96. EVP_AEAD_nonce_length(EVP_aead_aes_128_gcm()),
  97. kPlaintext, sizeof(kPlaintext), NULL, 0)) {
  98. printf("AES-GCM encrypt failed\n");
  99. goto err;
  100. }
  101. printf(" got ");
  102. hexdump(output, out_len);
  103. /* AES-GCM Decryption */
  104. printf("About to AES-GCM open ");
  105. hexdump(output, out_len);
  106. if (!EVP_AEAD_CTX_open(&aead_ctx, output, &out_len, sizeof(output), nonce,
  107. EVP_AEAD_nonce_length(EVP_aead_aes_128_gcm()),
  108. output, out_len, NULL, 0)) {
  109. printf("AES-GCM decrypt failed\n");
  110. goto err;
  111. }
  112. printf(" got ");
  113. hexdump(output, out_len);
  114. EVP_AEAD_CTX_cleanup(&aead_ctx);
  115. DES_key_schedule des1, des2, des3;
  116. DES_cblock des_iv;
  117. DES_set_key(&kDESKey1, &des1);
  118. DES_set_key(&kDESKey2, &des2);
  119. DES_set_key(&kDESKey3, &des3);
  120. /* 3DES Encryption */
  121. memcpy(&des_iv, &kDESIV, sizeof(des_iv));
  122. printf("About to 3DES-CBC encrypt ");
  123. hexdump(kPlaintext, sizeof(kPlaintext));
  124. DES_ede3_cbc_encrypt(kPlaintext, output, sizeof(kPlaintext), &des1, &des2,
  125. &des3, &des_iv, DES_ENCRYPT);
  126. printf(" got ");
  127. hexdump(output, sizeof(kPlaintext));
  128. /* 3DES Decryption */
  129. memcpy(&des_iv, &kDESIV, sizeof(des_iv));
  130. printf("About to 3DES-CBC decrypt ");
  131. hexdump(kPlaintext, sizeof(kPlaintext));
  132. DES_ede3_cbc_encrypt(output, output, sizeof(kPlaintext), &des1,
  133. &des2, &des3, &des_iv, DES_DECRYPT);
  134. printf(" got ");
  135. hexdump(output, sizeof(kPlaintext));
  136. /* SHA-1 */
  137. printf("About to SHA-1 hash ");
  138. hexdump(kPlaintext, sizeof(kPlaintext));
  139. SHA1(kPlaintext, sizeof(kPlaintext), output);
  140. printf(" got ");
  141. hexdump(output, SHA_DIGEST_LENGTH);
  142. /* SHA-256 */
  143. printf("About to SHA-256 hash ");
  144. hexdump(kPlaintext, sizeof(kPlaintext));
  145. SHA256(kPlaintext, sizeof(kPlaintext), output);
  146. printf(" got ");
  147. hexdump(output, SHA256_DIGEST_LENGTH);
  148. /* SHA-512 */
  149. printf("About to SHA-512 hash ");
  150. hexdump(kPlaintext, sizeof(kPlaintext));
  151. SHA512(kPlaintext, sizeof(kPlaintext), output);
  152. printf(" got ");
  153. hexdump(output, SHA512_DIGEST_LENGTH);
  154. RSA *rsa_key = RSA_new();
  155. printf("About to generate RSA key\n");
  156. if (!RSA_generate_key_fips(rsa_key, 2048, NULL)) {
  157. printf("RSA_generate_key_fips failed\n");
  158. goto err;
  159. }
  160. /* RSA Sign */
  161. unsigned sig_len;
  162. printf("About to RSA sign ");
  163. hexdump(kPlaintextSHA256, sizeof(kPlaintextSHA256));
  164. if (!RSA_sign(NID_sha256, kPlaintextSHA256, sizeof(kPlaintextSHA256), output,
  165. &sig_len, rsa_key)) {
  166. printf("RSA Sign failed\n");
  167. goto err;
  168. }
  169. printf(" got ");
  170. hexdump(output, sig_len);
  171. /* RSA Verify */
  172. printf("About to RSA verify ");
  173. hexdump(output, sig_len);
  174. if (!RSA_verify(NID_sha256, kPlaintextSHA256, sizeof(kPlaintextSHA256),
  175. output, sig_len, rsa_key)) {
  176. printf("RSA Verify failed.\n");
  177. goto err;
  178. }
  179. RSA_free(rsa_key);
  180. EC_KEY *ec_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
  181. if (ec_key == NULL) {
  182. printf("invalid ECDSA key\n");
  183. goto err;
  184. }
  185. printf("About to generate P-256 key\n");
  186. if (!EC_KEY_generate_key_fips(ec_key)) {
  187. printf("EC_KEY_generate_key_fips failed\n");
  188. goto err;
  189. }
  190. /* ECDSA Sign/Verify PWCT */
  191. printf("About to ECDSA sign ");
  192. hexdump(kPlaintextSHA256, sizeof(kPlaintextSHA256));
  193. ECDSA_SIG *sig =
  194. ECDSA_do_sign(kPlaintextSHA256, sizeof(kPlaintextSHA256), ec_key);
  195. if (sig == NULL ||
  196. !ECDSA_do_verify(kPlaintextSHA256, sizeof(kPlaintextSHA256), sig,
  197. ec_key)) {
  198. printf("ECDSA Sign/Verify PWCT failed.\n");
  199. goto err;
  200. }
  201. ECDSA_SIG_free(sig);
  202. EC_KEY_free(ec_key);
  203. /* DBRG */
  204. CTR_DRBG_STATE drbg;
  205. printf("About to seed CTR-DRBG with ");
  206. hexdump(kDRBGEntropy, sizeof(kDRBGEntropy));
  207. if (!CTR_DRBG_init(&drbg, kDRBGEntropy, kDRBGPersonalization,
  208. sizeof(kDRBGPersonalization)) ||
  209. !CTR_DRBG_generate(&drbg, output, sizeof(output), kDRBGAD,
  210. sizeof(kDRBGAD)) ||
  211. !CTR_DRBG_reseed(&drbg, kDRBGEntropy2, kDRBGAD, sizeof(kDRBGAD)) ||
  212. !CTR_DRBG_generate(&drbg, output, sizeof(output), kDRBGAD,
  213. sizeof(kDRBGAD))) {
  214. printf("DRBG failed\n");
  215. goto err;
  216. }
  217. printf(" generated ");
  218. hexdump(output, sizeof(output));
  219. CTR_DRBG_clear(&drbg);
  220. printf("PASS\n");
  221. return 0;
  222. err:
  223. printf("FAIL\n");
  224. abort();
  225. }