ariacbc.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright 2012-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 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. unsigned char outbuf[1024];
  54. printf("ARIA CBC Encrypt:\n");
  55. printf("Plaintext:\n");
  56. BIO_dump_fp(stdout, cbc_pt, sizeof(cbc_pt));
  57. /* Create a context for the encrypt operation */
  58. if ((ctx = EVP_CIPHER_CTX_new()) == NULL)
  59. goto err;
  60. /* Fetch the cipher implementation */
  61. if ((cipher = EVP_CIPHER_fetch(libctx, "ARIA-256-CBC", propq)) == NULL)
  62. goto err;
  63. /*
  64. * Initialise an encrypt operation with the cipher/mode, key and IV.
  65. * We are not setting any custom params so let params be just NULL.
  66. */
  67. if (!EVP_EncryptInit_ex2(ctx, cipher, cbc_key, cbc_iv, /* params */ NULL))
  68. goto err;
  69. /* Encrypt plaintext */
  70. if (!EVP_EncryptUpdate(ctx, outbuf, &outlen, cbc_pt, sizeof(cbc_pt)))
  71. goto err;
  72. /* Finalise: there can be some additional output from padding */
  73. if (!EVP_EncryptFinal_ex(ctx, outbuf + outlen, &tmplen))
  74. goto err;
  75. outlen += tmplen;
  76. /* Output encrypted block */
  77. printf("Ciphertext (outlen:%d):\n", outlen);
  78. BIO_dump_fp(stdout, outbuf, outlen);
  79. if (sizeof(cbc_ct) == outlen && !CRYPTO_memcmp(outbuf, cbc_ct, outlen))
  80. printf("Final ciphertext matches expected ciphertext\n");
  81. else
  82. printf("Final ciphertext differs from expected ciphertext\n");
  83. ret = 1;
  84. err:
  85. if (!ret)
  86. ERR_print_errors_fp(stderr);
  87. EVP_CIPHER_free(cipher);
  88. EVP_CIPHER_CTX_free(ctx);
  89. return ret;
  90. }
  91. int aria_cbc_decrypt(void)
  92. {
  93. int ret = 0;
  94. EVP_CIPHER_CTX *ctx;
  95. EVP_CIPHER *cipher = NULL;
  96. int outlen, tmplen;
  97. unsigned char outbuf[1024];
  98. printf("ARIA CBC Decrypt:\n");
  99. printf("Ciphertext:\n");
  100. BIO_dump_fp(stdout, cbc_ct, sizeof(cbc_ct));
  101. if ((ctx = EVP_CIPHER_CTX_new()) == NULL)
  102. goto err;
  103. /* Fetch the cipher implementation */
  104. if ((cipher = EVP_CIPHER_fetch(libctx, "ARIA-256-CBC", propq)) == NULL)
  105. goto err;
  106. /*
  107. * Initialise an encrypt operation with the cipher/mode, key and IV.
  108. * We are not setting any custom params so let params be just NULL.
  109. */
  110. if (!EVP_DecryptInit_ex2(ctx, cipher, cbc_key, cbc_iv, /* params */ NULL))
  111. goto err;
  112. /* Decrypt plaintext */
  113. if (!EVP_DecryptUpdate(ctx, outbuf, &outlen, cbc_ct, sizeof(cbc_ct)))
  114. goto err;
  115. /* Finalise: there can be some additional output from padding */
  116. if (!EVP_DecryptFinal_ex(ctx, outbuf + outlen, &tmplen))
  117. goto err;
  118. outlen += tmplen;
  119. /* Output decrypted block */
  120. printf("Plaintext (outlen:%d):\n", outlen);
  121. BIO_dump_fp(stdout, outbuf, outlen);
  122. if (sizeof(cbc_pt) == outlen && !CRYPTO_memcmp(outbuf, cbc_pt, outlen))
  123. printf("Final plaintext matches original plaintext\n");
  124. else
  125. printf("Final plaintext differs from original plaintext\n");
  126. ret = 1;
  127. err:
  128. if (!ret)
  129. ERR_print_errors_fp(stderr);
  130. EVP_CIPHER_free(cipher);
  131. EVP_CIPHER_CTX_free(ctx);
  132. return ret;
  133. }
  134. int main(int argc, char **argv)
  135. {
  136. if (!aria_cbc_encrypt())
  137. return EXIT_FAILURE;
  138. if (!aria_cbc_decrypt())
  139. return EXIT_FAILURE;
  140. return EXIT_SUCCESS;
  141. }