e_rc2.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright 1995-2018 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. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #ifndef OPENSSL_NO_RC2
  12. # include <openssl/evp.h>
  13. # include <openssl/objects.h>
  14. # include "crypto/evp.h"
  15. # include <openssl/rc2.h>
  16. static int rc2_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  17. const unsigned char *iv, int enc);
  18. static int rc2_meth_to_magic(EVP_CIPHER_CTX *ctx);
  19. static int rc2_magic_to_meth(int i);
  20. static int rc2_set_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type);
  21. static int rc2_get_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type);
  22. static int rc2_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr);
  23. typedef struct {
  24. int key_bits; /* effective key bits */
  25. RC2_KEY ks; /* key schedule */
  26. } EVP_RC2_KEY;
  27. # define data(ctx) EVP_C_DATA(EVP_RC2_KEY,ctx)
  28. IMPLEMENT_BLOCK_CIPHER(rc2, ks, RC2, EVP_RC2_KEY, NID_rc2,
  29. 8,
  30. RC2_KEY_LENGTH, 8, 64,
  31. EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT,
  32. rc2_init_key, NULL,
  33. rc2_set_asn1_type_and_iv, rc2_get_asn1_type_and_iv,
  34. rc2_ctrl)
  35. # define RC2_40_MAGIC 0xa0
  36. # define RC2_64_MAGIC 0x78
  37. # define RC2_128_MAGIC 0x3a
  38. static const EVP_CIPHER r2_64_cbc_cipher = {
  39. NID_rc2_64_cbc,
  40. 8, 8 /* 64 bit */ , 8,
  41. EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT,
  42. rc2_init_key,
  43. rc2_cbc_cipher,
  44. NULL,
  45. sizeof(EVP_RC2_KEY),
  46. rc2_set_asn1_type_and_iv,
  47. rc2_get_asn1_type_and_iv,
  48. rc2_ctrl,
  49. NULL
  50. };
  51. static const EVP_CIPHER r2_40_cbc_cipher = {
  52. NID_rc2_40_cbc,
  53. 8, 5 /* 40 bit */ , 8,
  54. EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT,
  55. rc2_init_key,
  56. rc2_cbc_cipher,
  57. NULL,
  58. sizeof(EVP_RC2_KEY),
  59. rc2_set_asn1_type_and_iv,
  60. rc2_get_asn1_type_and_iv,
  61. rc2_ctrl,
  62. NULL
  63. };
  64. const EVP_CIPHER *EVP_rc2_64_cbc(void)
  65. {
  66. return &r2_64_cbc_cipher;
  67. }
  68. const EVP_CIPHER *EVP_rc2_40_cbc(void)
  69. {
  70. return &r2_40_cbc_cipher;
  71. }
  72. static int rc2_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  73. const unsigned char *iv, int enc)
  74. {
  75. RC2_set_key(&data(ctx)->ks, EVP_CIPHER_CTX_key_length(ctx),
  76. key, data(ctx)->key_bits);
  77. return 1;
  78. }
  79. static int rc2_meth_to_magic(EVP_CIPHER_CTX *e)
  80. {
  81. int i;
  82. if (EVP_CIPHER_CTX_ctrl(e, EVP_CTRL_GET_RC2_KEY_BITS, 0, &i) <= 0)
  83. return 0;
  84. if (i == 128)
  85. return RC2_128_MAGIC;
  86. else if (i == 64)
  87. return RC2_64_MAGIC;
  88. else if (i == 40)
  89. return RC2_40_MAGIC;
  90. else
  91. return 0;
  92. }
  93. static int rc2_magic_to_meth(int i)
  94. {
  95. if (i == RC2_128_MAGIC)
  96. return 128;
  97. else if (i == RC2_64_MAGIC)
  98. return 64;
  99. else if (i == RC2_40_MAGIC)
  100. return 40;
  101. else {
  102. EVPerr(EVP_F_RC2_MAGIC_TO_METH, EVP_R_UNSUPPORTED_KEY_SIZE);
  103. return 0;
  104. }
  105. }
  106. static int rc2_get_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
  107. {
  108. long num = 0;
  109. int i = 0;
  110. int key_bits;
  111. unsigned int l;
  112. unsigned char iv[EVP_MAX_IV_LENGTH];
  113. if (type != NULL) {
  114. l = EVP_CIPHER_CTX_iv_length(c);
  115. OPENSSL_assert(l <= sizeof(iv));
  116. i = ASN1_TYPE_get_int_octetstring(type, &num, iv, l);
  117. if (i != (int)l)
  118. return -1;
  119. key_bits = rc2_magic_to_meth((int)num);
  120. if (!key_bits)
  121. return -1;
  122. if (i > 0 && !EVP_CipherInit_ex(c, NULL, NULL, NULL, iv, -1))
  123. return -1;
  124. if (EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_RC2_KEY_BITS, key_bits,
  125. NULL) <= 0
  126. || EVP_CIPHER_CTX_set_key_length(c, key_bits / 8) <= 0)
  127. return -1;
  128. }
  129. return i;
  130. }
  131. static int rc2_set_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
  132. {
  133. long num;
  134. int i = 0, j;
  135. if (type != NULL) {
  136. num = rc2_meth_to_magic(c);
  137. j = EVP_CIPHER_CTX_iv_length(c);
  138. i = ASN1_TYPE_set_int_octetstring(type, num,
  139. (unsigned char *)EVP_CIPHER_CTX_original_iv(c),
  140. j);
  141. }
  142. return i;
  143. }
  144. static int rc2_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
  145. {
  146. switch (type) {
  147. case EVP_CTRL_INIT:
  148. data(c)->key_bits = EVP_CIPHER_CTX_key_length(c) * 8;
  149. return 1;
  150. case EVP_CTRL_GET_RC2_KEY_BITS:
  151. *(int *)ptr = data(c)->key_bits;
  152. return 1;
  153. case EVP_CTRL_SET_RC2_KEY_BITS:
  154. if (arg > 0) {
  155. data(c)->key_bits = arg;
  156. return 1;
  157. }
  158. return 0;
  159. # ifdef PBE_PRF_TEST
  160. case EVP_CTRL_PBE_PRF_NID:
  161. *(int *)ptr = NID_hmacWithMD5;
  162. return 1;
  163. # endif
  164. default:
  165. return -1;
  166. }
  167. }
  168. #endif