e_rc2.c 5.1 KB

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