e_rc2.c 5.1 KB

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