aesgcmtest.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright 2019-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. #include <openssl/evp.h>
  10. #include "testutil.h"
  11. static const unsigned char gcm_key[] = {
  12. 0xee, 0xbc, 0x1f, 0x57, 0x48, 0x7f, 0x51, 0x92, 0x1c, 0x04, 0x65, 0x66,
  13. 0x5f, 0x8a, 0xe6, 0xd1, 0x65, 0x8b, 0xb2, 0x6d, 0xe6, 0xf8, 0xa0, 0x69,
  14. 0xa3, 0x52, 0x02, 0x93, 0xa5, 0x72, 0x07, 0x8f
  15. };
  16. static const unsigned char gcm_iv[] = {
  17. 0x99, 0xaa, 0x3e, 0x68, 0xed, 0x81, 0x73, 0xa0, 0xee, 0xd0, 0x66, 0x84
  18. };
  19. static const unsigned char gcm_pt[] = {
  20. 0xf5, 0x6e, 0x87, 0x05, 0x5b, 0xc3, 0x2d, 0x0e, 0xeb, 0x31, 0xb2, 0xea,
  21. 0xcc, 0x2b, 0xf2, 0xa5
  22. };
  23. static const unsigned char gcm_aad[] = {
  24. 0x4d, 0x23, 0xc3, 0xce, 0xc3, 0x34, 0xb4, 0x9b, 0xdb, 0x37, 0x0c, 0x43,
  25. 0x7f, 0xec, 0x78, 0xde
  26. };
  27. static const unsigned char gcm_ct[] = {
  28. 0xf7, 0x26, 0x44, 0x13, 0xa8, 0x4c, 0x0e, 0x7c, 0xd5, 0x36, 0x86, 0x7e,
  29. 0xb9, 0xf2, 0x17, 0x36
  30. };
  31. static const unsigned char gcm_tag[] = {
  32. 0x67, 0xba, 0x05, 0x10, 0x26, 0x2a, 0xe4, 0x87, 0xd7, 0x37, 0xee, 0x62,
  33. 0x98, 0xf7, 0x7e, 0x0c
  34. };
  35. static int do_encrypt(unsigned char *iv_gen, unsigned char *ct, int *ct_len,
  36. unsigned char *tag, int *tag_len)
  37. {
  38. int ret = 0;
  39. EVP_CIPHER_CTX *ctx = NULL;
  40. int outlen;
  41. unsigned char outbuf[64];
  42. *tag_len = 16;
  43. ret = TEST_ptr(ctx = EVP_CIPHER_CTX_new())
  44. && TEST_true(EVP_EncryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL,
  45. NULL) > 0)
  46. && TEST_true(EVP_EncryptInit_ex(ctx, NULL, NULL, gcm_key,
  47. iv_gen != NULL ? NULL : gcm_iv) > 0)
  48. && TEST_true(EVP_EncryptUpdate(ctx, NULL, &outlen, gcm_aad,
  49. sizeof(gcm_aad)) > 0)
  50. && TEST_true(EVP_EncryptUpdate(ctx, ct, ct_len, gcm_pt,
  51. sizeof(gcm_pt)) > 0)
  52. && TEST_true(EVP_EncryptFinal_ex(ctx, outbuf, &outlen) > 0)
  53. && TEST_int_eq(EVP_CIPHER_CTX_tag_length(ctx), 16)
  54. && TEST_true(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  55. tag) > 0)
  56. && TEST_true(iv_gen == NULL
  57. || EVP_CIPHER_CTX_get_iv(ctx, iv_gen, 12));
  58. EVP_CIPHER_CTX_free(ctx);
  59. return ret;
  60. }
  61. static int do_decrypt(const unsigned char *iv, const unsigned char *ct,
  62. int ct_len, const unsigned char *tag, int tag_len)
  63. {
  64. int ret = 0;
  65. EVP_CIPHER_CTX *ctx = NULL;
  66. int outlen, ptlen;
  67. unsigned char pt[32];
  68. unsigned char outbuf[32];
  69. ret = TEST_ptr(ctx = EVP_CIPHER_CTX_new())
  70. && TEST_true(EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL,
  71. NULL, NULL) > 0)
  72. && TEST_true(EVP_DecryptInit_ex(ctx, NULL, NULL, gcm_key, iv) > 0)
  73. && TEST_int_eq(EVP_CIPHER_CTX_tag_length(ctx), 16)
  74. && TEST_true(EVP_DecryptUpdate(ctx, NULL, &outlen, gcm_aad,
  75. sizeof(gcm_aad)) > 0)
  76. && TEST_true(EVP_DecryptUpdate(ctx, pt, &ptlen, ct,
  77. ct_len) > 0)
  78. && TEST_true(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  79. tag_len, (void *)tag) > 0)
  80. && TEST_true(EVP_DecryptFinal_ex(ctx, outbuf, &outlen) > 0)
  81. && TEST_mem_eq(gcm_pt, sizeof(gcm_pt), pt, ptlen);
  82. EVP_CIPHER_CTX_free(ctx);
  83. return ret;
  84. }
  85. static int kat_test(void)
  86. {
  87. unsigned char tag[32];
  88. unsigned char ct[32];
  89. int ctlen = 0, taglen = 0;
  90. return do_encrypt(NULL, ct, &ctlen, tag, &taglen)
  91. && TEST_mem_eq(gcm_ct, sizeof(gcm_ct), ct, ctlen)
  92. && TEST_mem_eq(gcm_tag, sizeof(gcm_tag), tag, taglen)
  93. && do_decrypt(gcm_iv, ct, ctlen, tag, taglen);
  94. }
  95. static int badkeylen_test(void)
  96. {
  97. int ret;
  98. EVP_CIPHER_CTX *ctx = NULL;
  99. const EVP_CIPHER *cipher;
  100. ret = TEST_ptr(cipher = EVP_aes_192_gcm())
  101. && TEST_ptr(ctx = EVP_CIPHER_CTX_new())
  102. && TEST_true(EVP_EncryptInit_ex(ctx, cipher, NULL, NULL, NULL))
  103. && TEST_false(EVP_CIPHER_CTX_set_key_length(ctx, 2));
  104. EVP_CIPHER_CTX_free(ctx);
  105. return ret;
  106. }
  107. #ifdef FIPS_MODULE
  108. static int ivgen_test(void)
  109. {
  110. unsigned char iv_gen[16];
  111. unsigned char tag[32];
  112. unsigned char ct[32];
  113. int ctlen = 0, taglen = 0;
  114. return do_encrypt(iv_gen, ct, &ctlen, tag, &taglen)
  115. && do_decrypt(iv_gen, ct, ctlen, tag, taglen);
  116. }
  117. #endif /* FIPS_MODULE */
  118. int setup_tests(void)
  119. {
  120. ADD_TEST(kat_test);
  121. ADD_TEST(badkeylen_test);
  122. #ifdef FIPS_MODULE
  123. ADD_TEST(ivgen_test);
  124. #endif /* FIPS_MODULE */
  125. return 1;
  126. }