cipher_rc2.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Copyright 2019-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. /* Dispatch functions for RC2 cipher modes ecb, cbc, ofb, cfb */
  10. /*
  11. * RC2 low level APIs are deprecated for public use, but still ok for internal
  12. * use.
  13. */
  14. #include "internal/deprecated.h"
  15. #include <openssl/proverr.h>
  16. #include "cipher_rc2.h"
  17. #include "prov/implementations.h"
  18. #include "prov/providercommon.h"
  19. #define RC2_40_MAGIC 0xa0
  20. #define RC2_64_MAGIC 0x78
  21. #define RC2_128_MAGIC 0x3a
  22. #define RC2_FLAGS PROV_CIPHER_FLAG_VARIABLE_LENGTH
  23. static OSSL_FUNC_cipher_encrypt_init_fn rc2_einit;
  24. static OSSL_FUNC_cipher_decrypt_init_fn rc2_dinit;
  25. static OSSL_FUNC_cipher_freectx_fn rc2_freectx;
  26. static OSSL_FUNC_cipher_dupctx_fn rc2_dupctx;
  27. static OSSL_FUNC_cipher_gettable_ctx_params_fn rc2_gettable_ctx_params;
  28. static OSSL_FUNC_cipher_settable_ctx_params_fn rc2_settable_ctx_params;
  29. static OSSL_FUNC_cipher_set_ctx_params_fn rc2_set_ctx_params;
  30. static void rc2_freectx(void *vctx)
  31. {
  32. PROV_RC2_CTX *ctx = (PROV_RC2_CTX *)vctx;
  33. ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
  34. OPENSSL_clear_free(ctx, sizeof(*ctx));
  35. }
  36. static void *rc2_dupctx(void *ctx)
  37. {
  38. PROV_RC2_CTX *in = (PROV_RC2_CTX *)ctx;
  39. PROV_RC2_CTX *ret;
  40. if (!ossl_prov_is_running())
  41. return NULL;
  42. ret = OPENSSL_malloc(sizeof(*ret));
  43. if (ret == NULL)
  44. return NULL;
  45. *ret = *in;
  46. return ret;
  47. }
  48. static int rc2_keybits_to_magic(int keybits)
  49. {
  50. switch (keybits) {
  51. case 128:
  52. return RC2_128_MAGIC;
  53. case 64:
  54. return RC2_64_MAGIC;
  55. case 40:
  56. return RC2_40_MAGIC;
  57. }
  58. ERR_raise(ERR_LIB_PROV, PROV_R_UNSUPPORTED_KEY_SIZE);
  59. return 0;
  60. }
  61. static int rc2_magic_to_keybits(int magic)
  62. {
  63. switch (magic) {
  64. case RC2_128_MAGIC:
  65. return 128;
  66. case RC2_64_MAGIC:
  67. return 64;
  68. case RC2_40_MAGIC:
  69. return 40;
  70. }
  71. ERR_raise(ERR_LIB_PROV, PROV_R_UNSUPPORTED_KEY_SIZE);
  72. return 0;
  73. }
  74. static int rc2_einit(void *ctx, const unsigned char *key, size_t keylen,
  75. const unsigned char *iv, size_t ivlen,
  76. const OSSL_PARAM params[])
  77. {
  78. if (!ossl_cipher_generic_einit(ctx, key, keylen, iv, ivlen, NULL))
  79. return 0;
  80. return rc2_set_ctx_params(ctx, params);
  81. }
  82. static int rc2_dinit(void *ctx, const unsigned char *key, size_t keylen,
  83. const unsigned char *iv, size_t ivlen,
  84. const OSSL_PARAM params[])
  85. {
  86. if (!ossl_cipher_generic_dinit(ctx, key, keylen, iv, ivlen, NULL))
  87. return 0;
  88. return rc2_set_ctx_params(ctx, params);
  89. }
  90. static int rc2_get_ctx_params(void *vctx, OSSL_PARAM params[])
  91. {
  92. PROV_RC2_CTX *ctx = (PROV_RC2_CTX *)vctx;
  93. OSSL_PARAM *p;
  94. if (!ossl_cipher_generic_get_ctx_params(vctx, params))
  95. return 0;
  96. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_RC2_KEYBITS);
  97. if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->key_bits)) {
  98. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  99. return 0;
  100. }
  101. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS);
  102. if (p != NULL) {
  103. long num;
  104. int i;
  105. ASN1_TYPE *type;
  106. unsigned char *d = p->data;
  107. unsigned char **dd = d == NULL ? NULL : &d;
  108. if (p->data_type != OSSL_PARAM_OCTET_STRING) {
  109. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  110. return 0;
  111. }
  112. if ((type = ASN1_TYPE_new()) == NULL) {
  113. ERR_raise(ERR_LIB_PROV, ERR_R_ASN1_LIB);
  114. return 0;
  115. }
  116. /* Is this the original IV or the running IV? */
  117. num = rc2_keybits_to_magic(ctx->key_bits);
  118. if (!ASN1_TYPE_set_int_octetstring(type, num,
  119. ctx->base.iv, ctx->base.ivlen)) {
  120. ASN1_TYPE_free(type);
  121. ERR_raise(ERR_LIB_PROV, ERR_R_ASN1_LIB);
  122. return 0;
  123. }
  124. /*
  125. * IF the caller has a buffer, we pray to the gods they got the
  126. * size right. There's no way to tell the i2d functions...
  127. */
  128. i = i2d_ASN1_TYPE(type, dd);
  129. if (i >= 0)
  130. p->return_size = (size_t)i;
  131. ASN1_TYPE_free(type);
  132. if (i < 0) {
  133. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  134. return 0;
  135. }
  136. }
  137. return 1;
  138. }
  139. static int rc2_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  140. {
  141. PROV_RC2_CTX *ctx = (PROV_RC2_CTX *)vctx;
  142. const OSSL_PARAM *p;
  143. if (params == NULL)
  144. return 1;
  145. if (!ossl_cipher_var_keylen_set_ctx_params(vctx, params))
  146. return 0;
  147. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_RC2_KEYBITS);
  148. if (p != NULL) {
  149. if (!OSSL_PARAM_get_size_t(p, &ctx->key_bits)) {
  150. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  151. return 0;
  152. }
  153. }
  154. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS);
  155. if (p != NULL) {
  156. ASN1_TYPE *type = NULL;
  157. long num = 0;
  158. const unsigned char *d = p->data;
  159. int ret = 1;
  160. unsigned char iv[16];
  161. if (p->data_type != OSSL_PARAM_OCTET_STRING
  162. || ctx->base.ivlen > sizeof(iv)
  163. || (type = d2i_ASN1_TYPE(NULL, &d, p->data_size)) == NULL
  164. || ((size_t)ASN1_TYPE_get_int_octetstring(type, &num, iv,
  165. ctx->base.ivlen)
  166. != ctx->base.ivlen)
  167. || !ossl_cipher_generic_initiv(&ctx->base, iv, ctx->base.ivlen)
  168. || (ctx->key_bits = rc2_magic_to_keybits(num)) == 0) {
  169. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  170. ret = 0;
  171. }
  172. ASN1_TYPE_free(type);
  173. if (ret == 0)
  174. return 0;
  175. /*
  176. * This code assumes that the caller will call
  177. * EVP_CipherInit_ex() with a non NULL key in order to setup a key that
  178. * uses the keylen and keybits that were set here.
  179. */
  180. ctx->base.keylen = ctx->key_bits / 8;
  181. }
  182. return 1;
  183. }
  184. CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_START(rc2)
  185. OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_RC2_KEYBITS, NULL),
  186. OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS, NULL, 0),
  187. CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_END(rc2)
  188. CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_START(rc2)
  189. OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
  190. OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_RC2_KEYBITS, NULL),
  191. OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS, NULL, 0),
  192. CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_END(rc2)
  193. #define IMPLEMENT_cipher(alg, UCALG, lcmode, UCMODE, flags, kbits, blkbits, \
  194. ivbits, typ) \
  195. static OSSL_FUNC_cipher_get_params_fn alg##_##kbits##_##lcmode##_get_params; \
  196. static int alg##_##kbits##_##lcmode##_get_params(OSSL_PARAM params[]) \
  197. { \
  198. return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, \
  199. flags, kbits, blkbits, ivbits); \
  200. } \
  201. static OSSL_FUNC_cipher_newctx_fn alg##_##kbits##_##lcmode##_newctx; \
  202. static void * alg##_##kbits##_##lcmode##_newctx(void *provctx) \
  203. { \
  204. PROV_##UCALG##_CTX *ctx; \
  205. if (!ossl_prov_is_running()) \
  206. return NULL; \
  207. ctx = OPENSSL_zalloc(sizeof(*ctx)); \
  208. if (ctx != NULL) { \
  209. ossl_cipher_generic_initkey(ctx, kbits, blkbits, ivbits, \
  210. EVP_CIPH_##UCMODE##_MODE, flags, \
  211. ossl_prov_cipher_hw_##alg##_##lcmode(kbits), \
  212. NULL); \
  213. ctx->key_bits = kbits; \
  214. } \
  215. return ctx; \
  216. } \
  217. const OSSL_DISPATCH ossl_##alg##kbits##lcmode##_functions[] = { \
  218. { OSSL_FUNC_CIPHER_NEWCTX, \
  219. (void (*)(void)) alg##_##kbits##_##lcmode##_newctx }, \
  220. { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void)) alg##_freectx }, \
  221. { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void)) alg##_dupctx }, \
  222. { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))rc2_einit }, \
  223. { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))rc2_dinit }, \
  224. { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))ossl_cipher_generic_##typ##_update },\
  225. { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))ossl_cipher_generic_##typ##_final }, \
  226. { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))ossl_cipher_generic_cipher }, \
  227. { OSSL_FUNC_CIPHER_GET_PARAMS, \
  228. (void (*)(void)) alg##_##kbits##_##lcmode##_get_params }, \
  229. { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \
  230. (void (*)(void))ossl_cipher_generic_gettable_params }, \
  231. { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, \
  232. (void (*)(void))rc2_get_ctx_params }, \
  233. { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \
  234. (void (*)(void))rc2_gettable_ctx_params }, \
  235. { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \
  236. (void (*)(void))rc2_set_ctx_params }, \
  237. { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \
  238. (void (*)(void))rc2_settable_ctx_params }, \
  239. OSSL_DISPATCH_END \
  240. };
  241. /* ossl_rc2128ecb_functions */
  242. IMPLEMENT_cipher(rc2, RC2, ecb, ECB, RC2_FLAGS, 128, 64, 0, block)
  243. /* ossl_rc2128cbc_functions */
  244. IMPLEMENT_cipher(rc2, RC2, cbc, CBC, RC2_FLAGS, 128, 64, 64, block)
  245. /* ossl_rc240cbc_functions */
  246. IMPLEMENT_cipher(rc2, RC2, cbc, CBC, RC2_FLAGS, 40, 64, 64, block)
  247. /* ossl_rc264cbc_functions */
  248. IMPLEMENT_cipher(rc2, RC2, cbc, CBC, RC2_FLAGS, 64, 64, 64, block)
  249. /* ossl_rc2128ofb128_functions */
  250. IMPLEMENT_cipher(rc2, RC2, ofb128, OFB, RC2_FLAGS, 128, 8, 64, stream)
  251. /* ossl_rc2128cfb128_functions */
  252. IMPLEMENT_cipher(rc2, RC2, cfb128, CFB, RC2_FLAGS, 128, 8, 64, stream)