aesccm.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright 2013-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 test program, uses the same NIST data used for the FIPS
  11. * self test but uses the application level EVP APIs.
  12. */
  13. #include <stdio.h>
  14. #include <openssl/bio.h>
  15. #include <openssl/evp.h>
  16. /* AES-CCM test data from NIST public test vectors */
  17. static const unsigned char ccm_key[] = {
  18. 0xce, 0xb0, 0x09, 0xae, 0xa4, 0x45, 0x44, 0x51, 0xfe, 0xad, 0xf0, 0xe6,
  19. 0xb3, 0x6f, 0x45, 0x55, 0x5d, 0xd0, 0x47, 0x23, 0xba, 0xa4, 0x48, 0xe8
  20. };
  21. static const unsigned char ccm_nonce[] = {
  22. 0x76, 0x40, 0x43, 0xc4, 0x94, 0x60, 0xb7
  23. };
  24. static const unsigned char ccm_adata[] = {
  25. 0x6e, 0x80, 0xdd, 0x7f, 0x1b, 0xad, 0xf3, 0xa1, 0xc9, 0xab, 0x25, 0xc7,
  26. 0x5f, 0x10, 0xbd, 0xe7, 0x8c, 0x23, 0xfa, 0x0e, 0xb8, 0xf9, 0xaa, 0xa5,
  27. 0x3a, 0xde, 0xfb, 0xf4, 0xcb, 0xf7, 0x8f, 0xe4
  28. };
  29. static const unsigned char ccm_pt[] = {
  30. 0xc8, 0xd2, 0x75, 0xf9, 0x19, 0xe1, 0x7d, 0x7f, 0xe6, 0x9c, 0x2a, 0x1f,
  31. 0x58, 0x93, 0x9d, 0xfe, 0x4d, 0x40, 0x37, 0x91, 0xb5, 0xdf, 0x13, 0x10
  32. };
  33. static const unsigned char ccm_ct[] = {
  34. 0x8a, 0x0f, 0x3d, 0x82, 0x29, 0xe4, 0x8e, 0x74, 0x87, 0xfd, 0x95, 0xa2,
  35. 0x8a, 0xd3, 0x92, 0xc8, 0x0b, 0x36, 0x81, 0xd4, 0xfb, 0xc7, 0xbb, 0xfd
  36. };
  37. static const unsigned char ccm_tag[] = {
  38. 0x2d, 0xd6, 0xef, 0x1c, 0x45, 0xd4, 0xcc, 0xb7, 0x23, 0xdc, 0x07, 0x44,
  39. 0x14, 0xdb, 0x50, 0x6d
  40. };
  41. void aes_ccm_encrypt(void)
  42. {
  43. EVP_CIPHER_CTX *ctx;
  44. int outlen, tmplen;
  45. unsigned char outbuf[1024];
  46. printf("AES CCM Encrypt:\n");
  47. printf("Plaintext:\n");
  48. BIO_dump_fp(stdout, ccm_pt, sizeof(ccm_pt));
  49. ctx = EVP_CIPHER_CTX_new();
  50. /* Set cipher type and mode */
  51. EVP_EncryptInit_ex(ctx, EVP_aes_192_ccm(), NULL, NULL, NULL);
  52. /* Set nonce length if default 96 bits is not appropriate */
  53. EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, sizeof(ccm_nonce),
  54. NULL);
  55. /* Set tag length */
  56. EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, sizeof(ccm_tag), NULL);
  57. /* Initialise key and IV */
  58. EVP_EncryptInit_ex(ctx, NULL, NULL, ccm_key, ccm_nonce);
  59. /* Set plaintext length: only needed if AAD is used */
  60. EVP_EncryptUpdate(ctx, NULL, &outlen, NULL, sizeof(ccm_pt));
  61. /* Zero or one call to specify any AAD */
  62. EVP_EncryptUpdate(ctx, NULL, &outlen, ccm_adata, sizeof(ccm_adata));
  63. /* Encrypt plaintext: can only be called once */
  64. EVP_EncryptUpdate(ctx, outbuf, &outlen, ccm_pt, sizeof(ccm_pt));
  65. /* Output encrypted block */
  66. printf("Ciphertext:\n");
  67. BIO_dump_fp(stdout, outbuf, outlen);
  68. /* Finalise: note get no output for CCM */
  69. EVP_EncryptFinal_ex(ctx, outbuf, &outlen);
  70. /* Get tag */
  71. EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16, outbuf);
  72. /* Output tag */
  73. printf("Tag:\n");
  74. BIO_dump_fp(stdout, outbuf, 16);
  75. EVP_CIPHER_CTX_free(ctx);
  76. }
  77. void aes_ccm_decrypt(void)
  78. {
  79. EVP_CIPHER_CTX *ctx;
  80. int outlen, tmplen, rv;
  81. unsigned char outbuf[1024];
  82. printf("AES CCM Derypt:\n");
  83. printf("Ciphertext:\n");
  84. BIO_dump_fp(stdout, ccm_ct, sizeof(ccm_ct));
  85. ctx = EVP_CIPHER_CTX_new();
  86. /* Select cipher */
  87. EVP_DecryptInit_ex(ctx, EVP_aes_192_ccm(), NULL, NULL, NULL);
  88. /* Set nonce length, omit for 96 bits */
  89. EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, sizeof(ccm_nonce),
  90. NULL);
  91. /* Set expected tag value */
  92. EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  93. sizeof(ccm_tag), (void *)ccm_tag);
  94. /* Specify key and IV */
  95. EVP_DecryptInit_ex(ctx, NULL, NULL, ccm_key, ccm_nonce);
  96. /* Set ciphertext length: only needed if we have AAD */
  97. EVP_DecryptUpdate(ctx, NULL, &outlen, NULL, sizeof(ccm_ct));
  98. /* Zero or one call to specify any AAD */
  99. EVP_DecryptUpdate(ctx, NULL, &outlen, ccm_adata, sizeof(ccm_adata));
  100. /* Decrypt plaintext, verify tag: can only be called once */
  101. rv = EVP_DecryptUpdate(ctx, outbuf, &outlen, ccm_ct, sizeof(ccm_ct));
  102. /* Output decrypted block: if tag verify failed we get nothing */
  103. if (rv > 0) {
  104. printf("Plaintext:\n");
  105. BIO_dump_fp(stdout, outbuf, outlen);
  106. } else
  107. printf("Plaintext not available: tag verify failed.\n");
  108. EVP_CIPHER_CTX_free(ctx);
  109. }
  110. int main(int argc, char **argv)
  111. {
  112. aes_ccm_encrypt();
  113. aes_ccm_decrypt();
  114. }