e_rc2.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright 1995-2016 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 "internal/evp_int.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. EVP_CIPHER_CTX_ctrl(e, EVP_CTRL_GET_RC2_KEY_BITS, 0, &i);
  83. if (i == 128)
  84. return (RC2_128_MAGIC);
  85. else if (i == 64)
  86. return (RC2_64_MAGIC);
  87. else if (i == 40)
  88. return (RC2_40_MAGIC);
  89. else
  90. return (0);
  91. }
  92. static int rc2_magic_to_meth(int i)
  93. {
  94. if (i == RC2_128_MAGIC)
  95. return 128;
  96. else if (i == RC2_64_MAGIC)
  97. return 64;
  98. else if (i == RC2_40_MAGIC)
  99. return 40;
  100. else {
  101. EVPerr(EVP_F_RC2_MAGIC_TO_METH, EVP_R_UNSUPPORTED_KEY_SIZE);
  102. return (0);
  103. }
  104. }
  105. static int rc2_get_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
  106. {
  107. long num = 0;
  108. int i = 0;
  109. int key_bits;
  110. unsigned int l;
  111. unsigned char iv[EVP_MAX_IV_LENGTH];
  112. if (type != NULL) {
  113. l = EVP_CIPHER_CTX_iv_length(c);
  114. OPENSSL_assert(l <= sizeof(iv));
  115. i = ASN1_TYPE_get_int_octetstring(type, &num, iv, l);
  116. if (i != (int)l)
  117. return -1;
  118. key_bits = rc2_magic_to_meth((int)num);
  119. if (!key_bits)
  120. return -1;
  121. if (i > 0 && !EVP_CipherInit_ex(c, NULL, NULL, NULL, iv, -1))
  122. return -1;
  123. EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_RC2_KEY_BITS, key_bits, NULL);
  124. if (EVP_CIPHER_CTX_set_key_length(c, key_bits / 8) <= 0)
  125. return -1;
  126. }
  127. return i;
  128. }
  129. static int rc2_set_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
  130. {
  131. long num;
  132. int i = 0, j;
  133. if (type != NULL) {
  134. num = rc2_meth_to_magic(c);
  135. j = EVP_CIPHER_CTX_iv_length(c);
  136. i = ASN1_TYPE_set_int_octetstring(type, num,
  137. (unsigned char *)EVP_CIPHER_CTX_original_iv(c),
  138. j);
  139. }
  140. return (i);
  141. }
  142. static int rc2_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
  143. {
  144. switch (type) {
  145. case EVP_CTRL_INIT:
  146. data(c)->key_bits = EVP_CIPHER_CTX_key_length(c) * 8;
  147. return 1;
  148. case EVP_CTRL_GET_RC2_KEY_BITS:
  149. *(int *)ptr = data(c)->key_bits;
  150. return 1;
  151. case EVP_CTRL_SET_RC2_KEY_BITS:
  152. if (arg > 0) {
  153. data(c)->key_bits = arg;
  154. return 1;
  155. }
  156. return 0;
  157. # ifdef PBE_PRF_TEST
  158. case EVP_CTRL_PBE_PRF_NID:
  159. *(int *)ptr = NID_hmacWithMD5;
  160. return 1;
  161. # endif
  162. default:
  163. return -1;
  164. }
  165. }
  166. #endif