aesgcm.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Copyright 2012-2021 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. * Simple AES GCM authenticated encryption with additional data (AEAD)
  11. * demonstration program.
  12. */
  13. #include <stdio.h>
  14. #include <openssl/err.h>
  15. #include <openssl/bio.h>
  16. #include <openssl/evp.h>
  17. #include <openssl/core_names.h>
  18. /* AES-GCM test data obtained from NIST public test vectors */
  19. /* AES key */
  20. static const unsigned char gcm_key[] = {
  21. 0xee, 0xbc, 0x1f, 0x57, 0x48, 0x7f, 0x51, 0x92, 0x1c, 0x04, 0x65, 0x66,
  22. 0x5f, 0x8a, 0xe6, 0xd1, 0x65, 0x8b, 0xb2, 0x6d, 0xe6, 0xf8, 0xa0, 0x69,
  23. 0xa3, 0x52, 0x02, 0x93, 0xa5, 0x72, 0x07, 0x8f
  24. };
  25. /* Unique initialisation vector */
  26. static const unsigned char gcm_iv[] = {
  27. 0x99, 0xaa, 0x3e, 0x68, 0xed, 0x81, 0x73, 0xa0, 0xee, 0xd0, 0x66, 0x84
  28. };
  29. /* Example plaintext to encrypt */
  30. static const unsigned char gcm_pt[] = {
  31. 0xf5, 0x6e, 0x87, 0x05, 0x5b, 0xc3, 0x2d, 0x0e, 0xeb, 0x31, 0xb2, 0xea,
  32. 0xcc, 0x2b, 0xf2, 0xa5
  33. };
  34. /*
  35. * Example of Additional Authenticated Data (AAD), i.e. unencrypted data
  36. * which can be authenticated using the generated Tag value.
  37. */
  38. static const unsigned char gcm_aad[] = {
  39. 0x4d, 0x23, 0xc3, 0xce, 0xc3, 0x34, 0xb4, 0x9b, 0xdb, 0x37, 0x0c, 0x43,
  40. 0x7f, 0xec, 0x78, 0xde
  41. };
  42. /* Expected ciphertext value */
  43. static const unsigned char gcm_ct[] = {
  44. 0xf7, 0x26, 0x44, 0x13, 0xa8, 0x4c, 0x0e, 0x7c, 0xd5, 0x36, 0x86, 0x7e,
  45. 0xb9, 0xf2, 0x17, 0x36
  46. };
  47. /* Expected AEAD Tag value */
  48. static const unsigned char gcm_tag[] = {
  49. 0x67, 0xba, 0x05, 0x10, 0x26, 0x2a, 0xe4, 0x87, 0xd7, 0x37, 0xee, 0x62,
  50. 0x98, 0xf7, 0x7e, 0x0c
  51. };
  52. /*
  53. * A library context and property query can be used to select & filter
  54. * algorithm implementations. If they are NULL then the default library
  55. * context and properties are used.
  56. */
  57. OSSL_LIB_CTX *libctx = NULL;
  58. const char *propq = NULL;
  59. int aes_gcm_encrypt(void)
  60. {
  61. int ret = 0;
  62. EVP_CIPHER_CTX *ctx;
  63. EVP_CIPHER *cipher = NULL;
  64. int outlen, tmplen;
  65. size_t gcm_ivlen = sizeof(gcm_iv);
  66. unsigned char outbuf[1024];
  67. unsigned char outtag[16];
  68. OSSL_PARAM params[2] = {
  69. OSSL_PARAM_END, OSSL_PARAM_END
  70. };
  71. printf("AES GCM Encrypt:\n");
  72. printf("Plaintext:\n");
  73. BIO_dump_fp(stdout, gcm_pt, sizeof(gcm_pt));
  74. /* Create a context for the encrypt operation */
  75. if ((ctx = EVP_CIPHER_CTX_new()) == NULL)
  76. goto err;
  77. /* Fetch the cipher implementation */
  78. if ((cipher = EVP_CIPHER_fetch(libctx, "AES-256-GCM", propq)) == NULL)
  79. goto err;
  80. /* Set IV length if default 96 bits is not appropriate */
  81. params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_IVLEN,
  82. &gcm_ivlen);
  83. /*
  84. * Initialise an encrypt operation with the cipher/mode, key, IV and
  85. * IV length parameter.
  86. * For demonstration purposes the IV is being set here. In a compliant
  87. * application the IV would be generated internally so the iv passed in
  88. * would be NULL.
  89. */
  90. if (!EVP_EncryptInit_ex2(ctx, cipher, gcm_key, gcm_iv, params))
  91. goto err;
  92. /* Zero or more calls to specify any AAD */
  93. if (!EVP_EncryptUpdate(ctx, NULL, &outlen, gcm_aad, sizeof(gcm_aad)))
  94. goto err;
  95. /* Encrypt plaintext */
  96. if (!EVP_EncryptUpdate(ctx, outbuf, &outlen, gcm_pt, sizeof(gcm_pt)))
  97. goto err;
  98. /* Output encrypted block */
  99. printf("Ciphertext:\n");
  100. BIO_dump_fp(stdout, outbuf, outlen);
  101. /* Finalise: note get no output for GCM */
  102. if (!EVP_EncryptFinal_ex(ctx, outbuf, &tmplen))
  103. goto err;
  104. /* Get tag */
  105. params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,
  106. outtag, 16);
  107. if (!EVP_CIPHER_CTX_get_params(ctx, params))
  108. goto err;
  109. /* Output tag */
  110. printf("Tag:\n");
  111. BIO_dump_fp(stdout, outtag, 16);
  112. ret = 1;
  113. err:
  114. if (!ret)
  115. ERR_print_errors_fp(stderr);
  116. EVP_CIPHER_free(cipher);
  117. EVP_CIPHER_CTX_free(ctx);
  118. return ret;
  119. }
  120. int aes_gcm_decrypt(void)
  121. {
  122. int ret = 0;
  123. EVP_CIPHER_CTX *ctx;
  124. EVP_CIPHER *cipher = NULL;
  125. int outlen, rv;
  126. size_t gcm_ivlen = sizeof(gcm_iv);
  127. unsigned char outbuf[1024];
  128. OSSL_PARAM params[2] = {
  129. OSSL_PARAM_END, OSSL_PARAM_END
  130. };
  131. printf("AES GCM Decrypt:\n");
  132. printf("Ciphertext:\n");
  133. BIO_dump_fp(stdout, gcm_ct, sizeof(gcm_ct));
  134. if ((ctx = EVP_CIPHER_CTX_new()) == NULL)
  135. goto err;
  136. /* Fetch the cipher implementation */
  137. if ((cipher = EVP_CIPHER_fetch(libctx, "AES-256-GCM", propq)) == NULL)
  138. goto err;
  139. /* Set IV length if default 96 bits is not appropriate */
  140. params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_IVLEN,
  141. &gcm_ivlen);
  142. /*
  143. * Initialise an encrypt operation with the cipher/mode, key, IV and
  144. * IV length parameter.
  145. */
  146. if (!EVP_DecryptInit_ex2(ctx, cipher, gcm_key, gcm_iv, params))
  147. goto err;
  148. /* Zero or more calls to specify any AAD */
  149. if (!EVP_DecryptUpdate(ctx, NULL, &outlen, gcm_aad, sizeof(gcm_aad)))
  150. goto err;
  151. /* Decrypt plaintext */
  152. if (!EVP_DecryptUpdate(ctx, outbuf, &outlen, gcm_ct, sizeof(gcm_ct)))
  153. goto err;
  154. /* Output decrypted block */
  155. printf("Plaintext:\n");
  156. BIO_dump_fp(stdout, outbuf, outlen);
  157. /* Set expected tag value. */
  158. params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,
  159. (void*)gcm_tag, sizeof(gcm_tag));
  160. if (!EVP_CIPHER_CTX_set_params(ctx, params))
  161. goto err;
  162. /* Finalise: note get no output for GCM */
  163. rv = EVP_DecryptFinal_ex(ctx, outbuf, &outlen);
  164. /*
  165. * Print out return value. If this is not successful authentication
  166. * failed and plaintext is not trustworthy.
  167. */
  168. printf("Tag Verify %s\n", rv > 0 ? "Successful!" : "Failed!");
  169. ret = 1;
  170. err:
  171. if (!ret)
  172. ERR_print_errors_fp(stderr);
  173. EVP_CIPHER_free(cipher);
  174. EVP_CIPHER_CTX_free(ctx);
  175. return ret;
  176. }
  177. int main(int argc, char **argv)
  178. {
  179. if (!aes_gcm_encrypt())
  180. return 1;
  181. if (!aes_gcm_decrypt())
  182. return 1;
  183. return 0;
  184. }