aeskeywrap.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright 2022-2023 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 wrap encryption demonstration program.
  11. */
  12. #include <stdio.h>
  13. #include <openssl/err.h>
  14. #include <openssl/bio.h>
  15. #include <openssl/evp.h>
  16. #include <openssl/crypto.h>
  17. #include <openssl/core_names.h>
  18. /* aes key */
  19. static const unsigned char wrap_key[] = {
  20. 0xee, 0xbc, 0x1f, 0x57, 0x48, 0x7f, 0x51, 0x92, 0x1c, 0x04, 0x65, 0x66,
  21. 0x5f, 0x8a, 0xe6, 0xd1, 0x65, 0x8b, 0xb2, 0x6d, 0xe6, 0xf8, 0xa0, 0x69,
  22. 0xa3, 0x52, 0x02, 0x93, 0xa5, 0x72, 0x07, 0x8f
  23. };
  24. /* Unique initialisation vector */
  25. static const unsigned char wrap_iv[] = {
  26. 0x99, 0xaa, 0x3e, 0x68, 0xed, 0x81, 0x73, 0xa0, 0xee, 0xd0, 0x66, 0x84,
  27. 0x99, 0xaa, 0x3e, 0x68,
  28. };
  29. /* Example plaintext to encrypt */
  30. static const unsigned char wrap_pt[] = {
  31. 0xad, 0x4f, 0xc9, 0xfc, 0x77, 0x69, 0xc9, 0xea, 0xfc, 0xdf, 0x00, 0xac,
  32. 0x34, 0xec, 0x40, 0xbc, 0x28, 0x3f, 0xa4, 0x5e, 0xd8, 0x99, 0xe4, 0x5d,
  33. 0x5e, 0x7a, 0xc4, 0xe6, 0xca, 0x7b, 0xa5, 0xb7,
  34. };
  35. /* Expected ciphertext value */
  36. static const unsigned char wrap_ct[] = {
  37. 0x97, 0x99, 0x55, 0xca, 0xf6, 0x3e, 0x95, 0x54, 0x39, 0xd6, 0xaf, 0x63, 0xff, 0x2c, 0xe3, 0x96,
  38. 0xf7, 0x0d, 0x2c, 0x9c, 0xc7, 0x43, 0xc0, 0xb6, 0x31, 0x43, 0xb9, 0x20, 0xac, 0x6b, 0xd3, 0x67,
  39. 0xad, 0x01, 0xaf, 0xa7, 0x32, 0x74, 0x26, 0x92,
  40. };
  41. /*
  42. * A library context and property query can be used to select & filter
  43. * algorithm implementations. If they are NULL then the default library
  44. * context and properties are used.
  45. */
  46. OSSL_LIB_CTX *libctx = NULL;
  47. const char *propq = NULL;
  48. int aes_wrap_encrypt(void)
  49. {
  50. int ret = 0;
  51. EVP_CIPHER_CTX *ctx;
  52. EVP_CIPHER *cipher = NULL;
  53. int outlen, tmplen;
  54. unsigned char outbuf[1024];
  55. printf("aes wrap Encrypt:\n");
  56. printf("Plaintext:\n");
  57. BIO_dump_fp(stdout, wrap_pt, sizeof(wrap_pt));
  58. /* Create a context for the encrypt operation */
  59. if ((ctx = EVP_CIPHER_CTX_new()) == NULL)
  60. goto err;
  61. EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
  62. /* Fetch the cipher implementation */
  63. if ((cipher = EVP_CIPHER_fetch(libctx, "AES-256-WRAP", propq)) == NULL)
  64. goto err;
  65. /*
  66. * Initialise an encrypt operation with the cipher/mode, key and IV.
  67. * We are not setting any custom params so let params be just NULL.
  68. */
  69. if (!EVP_EncryptInit_ex2(ctx, cipher, wrap_key, wrap_iv, /* params */ NULL))
  70. goto err;
  71. /* Encrypt plaintext */
  72. if (!EVP_EncryptUpdate(ctx, outbuf, &outlen, wrap_pt, sizeof(wrap_pt)))
  73. goto err;
  74. /* Finalise: there can be some additional output from padding */
  75. if (!EVP_EncryptFinal_ex(ctx, outbuf + outlen, &tmplen))
  76. goto err;
  77. outlen += tmplen;
  78. /* Output encrypted block */
  79. printf("Ciphertext (outlen:%d):\n", outlen);
  80. BIO_dump_fp(stdout, outbuf, outlen);
  81. if (sizeof(wrap_ct) == outlen && !CRYPTO_memcmp(outbuf, wrap_ct, outlen))
  82. printf("Final ciphertext matches expected ciphertext\n");
  83. else
  84. printf("Final ciphertext differs from expected ciphertext\n");
  85. ret = 1;
  86. err:
  87. if (!ret)
  88. ERR_print_errors_fp(stderr);
  89. EVP_CIPHER_free(cipher);
  90. EVP_CIPHER_CTX_free(ctx);
  91. return ret;
  92. }
  93. int aes_wrap_decrypt(void)
  94. {
  95. int ret = 0;
  96. EVP_CIPHER_CTX *ctx;
  97. EVP_CIPHER *cipher = NULL;
  98. int outlen, tmplen;
  99. unsigned char outbuf[1024];
  100. printf("aes wrap Decrypt:\n");
  101. printf("Ciphertext:\n");
  102. BIO_dump_fp(stdout, wrap_ct, sizeof(wrap_ct));
  103. if ((ctx = EVP_CIPHER_CTX_new()) == NULL)
  104. goto err;
  105. EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
  106. /* Fetch the cipher implementation */
  107. if ((cipher = EVP_CIPHER_fetch(libctx, "aes-256-wrap", propq)) == NULL)
  108. goto err;
  109. /*
  110. * Initialise an encrypt operation with the cipher/mode, key and IV.
  111. * We are not setting any custom params so let params be just NULL.
  112. */
  113. if (!EVP_DecryptInit_ex2(ctx, cipher, wrap_key, wrap_iv, /* params */ NULL))
  114. goto err;
  115. /* Decrypt plaintext */
  116. if (!EVP_DecryptUpdate(ctx, outbuf, &outlen, wrap_ct, sizeof(wrap_ct)))
  117. goto err;
  118. /* Finalise: there can be some additional output from padding */
  119. if (!EVP_DecryptFinal_ex(ctx, outbuf + outlen, &tmplen))
  120. goto err;
  121. outlen += tmplen;
  122. /* Output decrypted block */
  123. printf("Plaintext (outlen:%d):\n", outlen);
  124. BIO_dump_fp(stdout, outbuf, outlen);
  125. if (sizeof(wrap_pt) == outlen && !CRYPTO_memcmp(outbuf, wrap_pt, outlen))
  126. printf("Final plaintext matches original plaintext\n");
  127. else
  128. printf("Final plaintext differs from original plaintext\n");
  129. ret = 1;
  130. err:
  131. if (!ret)
  132. ERR_print_errors_fp(stderr);
  133. EVP_CIPHER_free(cipher);
  134. EVP_CIPHER_CTX_free(ctx);
  135. return ret;
  136. }
  137. int main(int argc, char **argv)
  138. {
  139. if (!aes_wrap_encrypt())
  140. return EXIT_FAILURE;
  141. if (!aes_wrap_decrypt())
  142. return EXIT_FAILURE;
  143. return EXIT_SUCCESS;
  144. }