aesccm.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright 2013-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 CCM 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-CCM test data obtained from NIST public test vectors */
  19. /* AES key */
  20. static const unsigned char ccm_key[] = {
  21. 0xce, 0xb0, 0x09, 0xae, 0xa4, 0x45, 0x44, 0x51, 0xfe, 0xad, 0xf0, 0xe6,
  22. 0xb3, 0x6f, 0x45, 0x55, 0x5d, 0xd0, 0x47, 0x23, 0xba, 0xa4, 0x48, 0xe8
  23. };
  24. /* Unique nonce to be used for this message */
  25. static const unsigned char ccm_nonce[] = {
  26. 0x76, 0x40, 0x43, 0xc4, 0x94, 0x60, 0xb7
  27. };
  28. /*
  29. * Example of Additional Authenticated Data (AAD), i.e. unencrypted data
  30. * which can be authenticated using the generated Tag value.
  31. */
  32. static const unsigned char ccm_adata[] = {
  33. 0x6e, 0x80, 0xdd, 0x7f, 0x1b, 0xad, 0xf3, 0xa1, 0xc9, 0xab, 0x25, 0xc7,
  34. 0x5f, 0x10, 0xbd, 0xe7, 0x8c, 0x23, 0xfa, 0x0e, 0xb8, 0xf9, 0xaa, 0xa5,
  35. 0x3a, 0xde, 0xfb, 0xf4, 0xcb, 0xf7, 0x8f, 0xe4
  36. };
  37. /* Example plaintext to encrypt */
  38. static const unsigned char ccm_pt[] = {
  39. 0xc8, 0xd2, 0x75, 0xf9, 0x19, 0xe1, 0x7d, 0x7f, 0xe6, 0x9c, 0x2a, 0x1f,
  40. 0x58, 0x93, 0x9d, 0xfe, 0x4d, 0x40, 0x37, 0x91, 0xb5, 0xdf, 0x13, 0x10
  41. };
  42. /* Expected ciphertext value */
  43. static const unsigned char ccm_ct[] = {
  44. 0x8a, 0x0f, 0x3d, 0x82, 0x29, 0xe4, 0x8e, 0x74, 0x87, 0xfd, 0x95, 0xa2,
  45. 0x8a, 0xd3, 0x92, 0xc8, 0x0b, 0x36, 0x81, 0xd4, 0xfb, 0xc7, 0xbb, 0xfd
  46. };
  47. /* Expected AEAD Tag value */
  48. static const unsigned char ccm_tag[] = {
  49. 0x2d, 0xd6, 0xef, 0x1c, 0x45, 0xd4, 0xcc, 0xb7, 0x23, 0xdc, 0x07, 0x44,
  50. 0x14, 0xdb, 0x50, 0x6d
  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_ccm_encrypt(void)
  60. {
  61. int ret = 0;
  62. EVP_CIPHER_CTX *ctx;
  63. EVP_CIPHER *cipher = NULL;
  64. int outlen, tmplen;
  65. size_t ccm_nonce_len = sizeof(ccm_nonce);
  66. size_t ccm_tag_len = sizeof(ccm_tag);
  67. unsigned char outbuf[1024];
  68. unsigned char outtag[16];
  69. OSSL_PARAM params[3] = {
  70. OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END
  71. };
  72. printf("AES CCM Encrypt:\n");
  73. printf("Plaintext:\n");
  74. BIO_dump_fp(stdout, ccm_pt, sizeof(ccm_pt));
  75. /* Create a context for the encrypt operation */
  76. if ((ctx = EVP_CIPHER_CTX_new()) == NULL)
  77. goto err;
  78. /* Fetch the cipher implementation */
  79. if ((cipher = EVP_CIPHER_fetch(libctx, "AES-192-CCM", propq)) == NULL)
  80. goto err;
  81. /* Set nonce length if default 96 bits is not appropriate */
  82. params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_IVLEN,
  83. &ccm_nonce_len);
  84. /* Set tag length */
  85. params[1] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,
  86. NULL, ccm_tag_len);
  87. /*
  88. * Initialise encrypt operation with the cipher & mode,
  89. * nonce length and tag length parameters.
  90. */
  91. if (!EVP_EncryptInit_ex2(ctx, cipher, NULL, NULL, params))
  92. goto err;
  93. /* Initialise key and nonce */
  94. if (!EVP_EncryptInit_ex(ctx, NULL, NULL, ccm_key, ccm_nonce))
  95. goto err;
  96. /* Set plaintext length: only needed if AAD is used */
  97. if (!EVP_EncryptUpdate(ctx, NULL, &outlen, NULL, sizeof(ccm_pt)))
  98. goto err;
  99. /* Zero or one call to specify any AAD */
  100. if (!EVP_EncryptUpdate(ctx, NULL, &outlen, ccm_adata, sizeof(ccm_adata)))
  101. goto err;
  102. /* Encrypt plaintext: can only be called once */
  103. if (!EVP_EncryptUpdate(ctx, outbuf, &outlen, ccm_pt, sizeof(ccm_pt)))
  104. goto err;
  105. /* Output encrypted block */
  106. printf("Ciphertext:\n");
  107. BIO_dump_fp(stdout, outbuf, outlen);
  108. /* Finalise: note get no output for CCM */
  109. if (!EVP_EncryptFinal_ex(ctx, NULL, &tmplen))
  110. goto err;
  111. /* Get tag */
  112. params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,
  113. outtag, ccm_tag_len);
  114. params[1] = OSSL_PARAM_construct_end();
  115. if (!EVP_CIPHER_CTX_get_params(ctx, params))
  116. goto err;
  117. /* Output tag */
  118. printf("Tag:\n");
  119. BIO_dump_fp(stdout, outtag, ccm_tag_len);
  120. ret = 1;
  121. err:
  122. if (!ret)
  123. ERR_print_errors_fp(stderr);
  124. EVP_CIPHER_free(cipher);
  125. EVP_CIPHER_CTX_free(ctx);
  126. return ret;
  127. }
  128. int aes_ccm_decrypt(void)
  129. {
  130. int ret = 0;
  131. EVP_CIPHER_CTX *ctx;
  132. EVP_CIPHER *cipher = NULL;
  133. int outlen, rv;
  134. unsigned char outbuf[1024];
  135. size_t ccm_nonce_len = sizeof(ccm_nonce);
  136. OSSL_PARAM params[3] = {
  137. OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END
  138. };
  139. printf("AES CCM Decrypt:\n");
  140. printf("Ciphertext:\n");
  141. BIO_dump_fp(stdout, ccm_ct, sizeof(ccm_ct));
  142. if ((ctx = EVP_CIPHER_CTX_new()) == NULL)
  143. goto err;
  144. /* Fetch the cipher implementation */
  145. if ((cipher = EVP_CIPHER_fetch(libctx, "AES-192-CCM", propq)) == NULL)
  146. goto err;
  147. /* Set nonce length if default 96 bits is not appropriate */
  148. params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_IVLEN,
  149. &ccm_nonce_len);
  150. /* Set tag length */
  151. params[1] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,
  152. (unsigned char *)ccm_tag,
  153. sizeof(ccm_tag));
  154. /*
  155. * Initialise decrypt operation with the cipher & mode,
  156. * nonce length and expected tag parameters.
  157. */
  158. if (!EVP_DecryptInit_ex2(ctx, cipher, NULL, NULL, params))
  159. goto err;
  160. /* Specify key and IV */
  161. if (!EVP_DecryptInit_ex(ctx, NULL, NULL, ccm_key, ccm_nonce))
  162. goto err;
  163. /* Set ciphertext length: only needed if we have AAD */
  164. if (!EVP_DecryptUpdate(ctx, NULL, &outlen, NULL, sizeof(ccm_ct)))
  165. goto err;
  166. /* Zero or one call to specify any AAD */
  167. if (!EVP_DecryptUpdate(ctx, NULL, &outlen, ccm_adata, sizeof(ccm_adata)))
  168. goto err;
  169. /* Decrypt plaintext, verify tag: can only be called once */
  170. rv = EVP_DecryptUpdate(ctx, outbuf, &outlen, ccm_ct, sizeof(ccm_ct));
  171. /* Output decrypted block: if tag verify failed we get nothing */
  172. if (rv > 0) {
  173. printf("Tag verify successful!\nPlaintext:\n");
  174. BIO_dump_fp(stdout, outbuf, outlen);
  175. } else {
  176. printf("Tag verify failed!\nPlaintext not available\n");
  177. goto err;
  178. }
  179. ret = 1;
  180. err:
  181. if (!ret)
  182. ERR_print_errors_fp(stderr);
  183. EVP_CIPHER_free(cipher);
  184. EVP_CIPHER_CTX_free(ctx);
  185. return ret;
  186. }
  187. int main(int argc, char **argv)
  188. {
  189. if (!aes_ccm_encrypt())
  190. return 1;
  191. if (!aes_ccm_decrypt())
  192. return 1;
  193. return 0;
  194. }