ariacbc.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright 2012-2022 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 ARIA CBC 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. /* ARIA key */
  19. static const unsigned char cbc_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 cbc_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 cbc_pt[] = {
  31. 0xf5, 0x6e, 0x87, 0x05, 0x5b, 0xc3, 0x2d, 0x0e, 0xeb, 0x31, 0xb2, 0xea,
  32. 0xcc, 0x2b, 0xf2, 0xa5
  33. };
  34. /* Expected ciphertext value */
  35. static const unsigned char cbc_ct[] = {
  36. 0x9a, 0x44, 0xe6, 0x85, 0x94, 0x26, 0xff, 0x30, 0x03, 0xd3, 0x7e, 0xc6,
  37. 0xb5, 0x4a, 0x09, 0x66, 0x39, 0x28, 0xf3, 0x67, 0x14, 0xbc, 0xe8, 0xe2,
  38. 0xcf, 0x31, 0xb8, 0x60, 0x42, 0x72, 0x6d, 0xc8
  39. };
  40. /*
  41. * A library context and property query can be used to select & filter
  42. * algorithm implementations. If they are NULL then the default library
  43. * context and properties are used.
  44. */
  45. OSSL_LIB_CTX *libctx = NULL;
  46. const char *propq = NULL;
  47. int aria_cbc_encrypt(void)
  48. {
  49. int ret = 0;
  50. EVP_CIPHER_CTX *ctx;
  51. EVP_CIPHER *cipher = NULL;
  52. int outlen, tmplen;
  53. size_t cbc_ivlen = sizeof(cbc_iv);
  54. unsigned char outbuf[1024];
  55. unsigned char outtag[16];
  56. printf("ARIA CBC Encrypt:\n");
  57. printf("Plaintext:\n");
  58. BIO_dump_fp(stdout, cbc_pt, sizeof(cbc_pt));
  59. /* Create a context for the encrypt operation */
  60. if ((ctx = EVP_CIPHER_CTX_new()) == NULL)
  61. goto err;
  62. /* Fetch the cipher implementation */
  63. if ((cipher = EVP_CIPHER_fetch(libctx, "ARIA-256-CBC", 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, cbc_key, cbc_iv, /* params */ NULL))
  70. goto err;
  71. /* Encrypt plaintext */
  72. if (!EVP_EncryptUpdate(ctx, outbuf, &outlen, cbc_pt, sizeof(cbc_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(cbc_ct) == outlen && !CRYPTO_memcmp(outbuf, cbc_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 aria_cbc_decrypt(void)
  94. {
  95. int ret = 0;
  96. EVP_CIPHER_CTX *ctx;
  97. EVP_CIPHER *cipher = NULL;
  98. int outlen, tmplen, rv;
  99. size_t cbc_ivlen = sizeof(cbc_iv);
  100. unsigned char outbuf[1024];
  101. printf("ARIA CBC Decrypt:\n");
  102. printf("Ciphertext:\n");
  103. BIO_dump_fp(stdout, cbc_ct, sizeof(cbc_ct));
  104. if ((ctx = EVP_CIPHER_CTX_new()) == NULL)
  105. goto err;
  106. /* Fetch the cipher implementation */
  107. if ((cipher = EVP_CIPHER_fetch(libctx, "ARIA-256-CBC", 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, cbc_key, cbc_iv, /* params */ NULL))
  114. goto err;
  115. /* Decrypt plaintext */
  116. if (!EVP_DecryptUpdate(ctx, outbuf, &outlen, cbc_ct, sizeof(cbc_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(cbc_pt) == outlen && !CRYPTO_memcmp(outbuf, cbc_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 (!aria_cbc_encrypt())
  140. return 1;
  141. if (!aria_cbc_decrypt())
  142. return 1;
  143. return 0;
  144. }