e_aria.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright 2006-2017 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. * Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
  11. */
  12. #include "internal/cryptlib.h"
  13. #ifndef OPENSSL_NO_ARIA
  14. # include <openssl/evp.h>
  15. # include <openssl/modes.h>
  16. # include"internal/aria.h"
  17. # include "internal/evp_int.h"
  18. /* ARIA subkey Structure */
  19. typedef struct {
  20. ARIA_KEY ks;
  21. } EVP_ARIA_KEY;
  22. /* The subkey for ARIA is generated. */
  23. static int aria_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  24. const unsigned char *iv, int enc)
  25. {
  26. int ret;
  27. int mode = EVP_CIPHER_CTX_mode(ctx);
  28. if (enc || (mode != EVP_CIPH_ECB_MODE && mode != EVP_CIPH_CBC_MODE))
  29. ret = aria_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
  30. EVP_CIPHER_CTX_get_cipher_data(ctx));
  31. else
  32. ret = aria_set_decrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
  33. EVP_CIPHER_CTX_get_cipher_data(ctx));
  34. if (ret < 0) {
  35. EVPerr(EVP_F_ARIA_INIT_KEY,EVP_R_ARIA_KEY_SETUP_FAILED);
  36. return 0;
  37. }
  38. return 1;
  39. }
  40. static void aria_cbc_encrypt(const unsigned char *in, unsigned char *out,
  41. size_t len, const ARIA_KEY *key,
  42. unsigned char *ivec, const int enc)
  43. {
  44. if (enc)
  45. CRYPTO_cbc128_encrypt(in, out, len, key, ivec,
  46. (block128_f) aria_encrypt);
  47. else
  48. CRYPTO_cbc128_decrypt(in, out, len, key, ivec,
  49. (block128_f) aria_encrypt);
  50. }
  51. static void aria_cfb128_encrypt(const unsigned char *in, unsigned char *out,
  52. size_t length, const ARIA_KEY *key,
  53. unsigned char *ivec, int *num, const int enc)
  54. {
  55. CRYPTO_cfb128_encrypt(in, out, length, key, ivec, num, enc,
  56. (block128_f) aria_encrypt);
  57. }
  58. static void aria_cfb1_encrypt(const unsigned char *in, unsigned char *out,
  59. size_t length, const ARIA_KEY *key,
  60. unsigned char *ivec, int *num, const int enc)
  61. {
  62. CRYPTO_cfb128_1_encrypt(in, out, length, key, ivec, num, enc,
  63. (block128_f) aria_encrypt);
  64. }
  65. static void aria_cfb8_encrypt(const unsigned char *in, unsigned char *out,
  66. size_t length, const ARIA_KEY *key,
  67. unsigned char *ivec, int *num, const int enc)
  68. {
  69. CRYPTO_cfb128_8_encrypt(in, out, length, key, ivec, num, enc,
  70. (block128_f) aria_encrypt);
  71. }
  72. static void aria_ecb_encrypt(const unsigned char *in, unsigned char *out,
  73. const ARIA_KEY *key, const int enc)
  74. {
  75. aria_encrypt(in, out, key);
  76. }
  77. static void aria_ofb128_encrypt(const unsigned char *in, unsigned char *out,
  78. size_t length, const ARIA_KEY *key,
  79. unsigned char *ivec, int *num)
  80. {
  81. CRYPTO_ofb128_encrypt(in, out, length, key, ivec, num,
  82. (block128_f) aria_encrypt);
  83. }
  84. IMPLEMENT_BLOCK_CIPHER(aria_128, ks, aria, EVP_ARIA_KEY,
  85. NID_aria_128, 16, 16, 16, 128,
  86. 0, aria_init_key, NULL,
  87. EVP_CIPHER_set_asn1_iv,
  88. EVP_CIPHER_get_asn1_iv,
  89. NULL)
  90. IMPLEMENT_BLOCK_CIPHER(aria_192, ks, aria, EVP_ARIA_KEY,
  91. NID_aria_192, 16, 24, 16, 128,
  92. 0, aria_init_key, NULL,
  93. EVP_CIPHER_set_asn1_iv,
  94. EVP_CIPHER_get_asn1_iv,
  95. NULL)
  96. IMPLEMENT_BLOCK_CIPHER(aria_256, ks, aria, EVP_ARIA_KEY,
  97. NID_aria_256, 16, 32, 16, 128,
  98. 0, aria_init_key, NULL,
  99. EVP_CIPHER_set_asn1_iv,
  100. EVP_CIPHER_get_asn1_iv,
  101. NULL)
  102. # define IMPLEMENT_ARIA_CFBR(ksize,cbits) \
  103. IMPLEMENT_CFBR(aria,aria,EVP_ARIA_KEY,ks,ksize,cbits,16,0)
  104. IMPLEMENT_ARIA_CFBR(128,1)
  105. IMPLEMENT_ARIA_CFBR(192,1)
  106. IMPLEMENT_ARIA_CFBR(256,1)
  107. IMPLEMENT_ARIA_CFBR(128,8)
  108. IMPLEMENT_ARIA_CFBR(192,8)
  109. IMPLEMENT_ARIA_CFBR(256,8)
  110. # define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
  111. static const EVP_CIPHER aria_##keylen##_##mode = { \
  112. nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \
  113. flags|EVP_CIPH_##MODE##_MODE, \
  114. aria_init_key, \
  115. aria_##mode##_cipher, \
  116. NULL, \
  117. sizeof(EVP_ARIA_KEY), \
  118. NULL,NULL,NULL,NULL }; \
  119. const EVP_CIPHER *EVP_aria_##keylen##_##mode(void) \
  120. { return &aria_##keylen##_##mode; }
  121. static int aria_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  122. const unsigned char *in, size_t len)
  123. {
  124. unsigned int num = EVP_CIPHER_CTX_num(ctx);
  125. EVP_ARIA_KEY *dat = EVP_C_DATA(EVP_ARIA_KEY,ctx);
  126. CRYPTO_ctr128_encrypt(in, out, len, &dat->ks,
  127. EVP_CIPHER_CTX_iv_noconst(ctx),
  128. EVP_CIPHER_CTX_buf_noconst(ctx), &num,
  129. (block128_f) aria_encrypt);
  130. EVP_CIPHER_CTX_set_num(ctx, num);
  131. return 1;
  132. }
  133. BLOCK_CIPHER_generic(NID_aria, 128, 1, 16, ctr, ctr, CTR, 0)
  134. BLOCK_CIPHER_generic(NID_aria, 192, 1, 16, ctr, ctr, CTR, 0)
  135. BLOCK_CIPHER_generic(NID_aria, 256, 1, 16, ctr, ctr, CTR, 0)
  136. #endif