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. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  45. return NULL;
  46. }
  47. *ret = *in;
  48. return ret;
  49. }
  50. static int rc2_keybits_to_magic(int keybits)
  51. {
  52. switch (keybits) {
  53. case 128:
  54. return RC2_128_MAGIC;
  55. case 64:
  56. return RC2_64_MAGIC;
  57. case 40:
  58. return RC2_40_MAGIC;
  59. }
  60. ERR_raise(ERR_LIB_PROV, PROV_R_UNSUPPORTED_KEY_SIZE);
  61. return 0;
  62. }
  63. static int rc2_magic_to_keybits(int magic)
  64. {
  65. switch (magic) {
  66. case RC2_128_MAGIC:
  67. return 128;
  68. case RC2_64_MAGIC:
  69. return 64;
  70. case RC2_40_MAGIC:
  71. return 40;
  72. }
  73. ERR_raise(ERR_LIB_PROV, PROV_R_UNSUPPORTED_KEY_SIZE);
  74. return 0;
  75. }
  76. static int rc2_einit(void *ctx, const unsigned char *key, size_t keylen,
  77. const unsigned char *iv, size_t ivlen,
  78. const OSSL_PARAM params[])
  79. {
  80. if (!ossl_cipher_generic_einit(ctx, key, keylen, iv, ivlen, NULL))
  81. return 0;
  82. return rc2_set_ctx_params(ctx, params);
  83. }
  84. static int rc2_dinit(void *ctx, const unsigned char *key, size_t keylen,
  85. const unsigned char *iv, size_t ivlen,
  86. const OSSL_PARAM params[])
  87. {
  88. if (!ossl_cipher_generic_dinit(ctx, key, keylen, iv, ivlen, NULL))
  89. return 0;
  90. return rc2_set_ctx_params(ctx, params);
  91. }
  92. static int rc2_get_ctx_params(void *vctx, OSSL_PARAM params[])
  93. {
  94. PROV_RC2_CTX *ctx = (PROV_RC2_CTX *)vctx;
  95. OSSL_PARAM *p;
  96. if (!ossl_cipher_generic_get_ctx_params(vctx, params))
  97. return 0;
  98. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_RC2_KEYBITS);
  99. if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->key_bits)) {
  100. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  101. return 0;
  102. }
  103. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_ALG_ID);
  104. if (p != NULL) {
  105. long num;
  106. int i;
  107. ASN1_TYPE *type;
  108. unsigned char *d = p->data;
  109. unsigned char **dd = d == NULL ? NULL : &d;
  110. if (p->data_type != OSSL_PARAM_OCTET_STRING) {
  111. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  112. return 0;
  113. }
  114. if ((type = ASN1_TYPE_new()) == NULL) {
  115. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  116. return 0;
  117. }
  118. /* Is this the original IV or the running IV? */
  119. num = rc2_keybits_to_magic(ctx->key_bits);
  120. if (!ASN1_TYPE_set_int_octetstring(type, num,
  121. ctx->base.iv, ctx->base.ivlen)) {
  122. ASN1_TYPE_free(type);
  123. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  124. return 0;
  125. }
  126. /*
  127. * IF the caller has a buffer, we pray to the gods they got the
  128. * size right. There's no way to tell the i2d functions...
  129. */
  130. i = i2d_ASN1_TYPE(type, dd);
  131. if (i >= 0)
  132. p->return_size = (size_t)i;
  133. ASN1_TYPE_free(type);
  134. if (i < 0) {
  135. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  136. return 0;
  137. }
  138. }
  139. return 1;
  140. }
  141. static int rc2_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  142. {
  143. PROV_RC2_CTX *ctx = (PROV_RC2_CTX *)vctx;
  144. const OSSL_PARAM *p;
  145. if (params == NULL)
  146. return 1;
  147. if (!ossl_cipher_var_keylen_set_ctx_params(vctx, params))
  148. return 0;
  149. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_RC2_KEYBITS);
  150. if (p != NULL) {
  151. if (!OSSL_PARAM_get_size_t(p, &ctx->key_bits)) {
  152. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  153. return 0;
  154. }
  155. }
  156. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_ALG_ID);
  157. if (p != NULL) {
  158. ASN1_TYPE *type = NULL;
  159. long num = 0;
  160. const unsigned char *d = p->data;
  161. int ret = 1;
  162. unsigned char iv[16];
  163. if (p->data_type != OSSL_PARAM_OCTET_STRING
  164. || ctx->base.ivlen > sizeof(iv)
  165. || (type = d2i_ASN1_TYPE(NULL, &d, p->data_size)) == NULL
  166. || ((size_t)ASN1_TYPE_get_int_octetstring(type, &num, iv,
  167. ctx->base.ivlen)
  168. != ctx->base.ivlen)
  169. || !ossl_cipher_generic_initiv(&ctx->base, iv, ctx->base.ivlen)
  170. || (ctx->key_bits = rc2_magic_to_keybits(num)) == 0) {
  171. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  172. ret = 0;
  173. }
  174. ASN1_TYPE_free(type);
  175. if (ret == 0)
  176. return 0;
  177. /*
  178. * This code assumes that the caller will call
  179. * EVP_CipherInit_ex() with a non NULL key in order to setup a key that
  180. * uses the keylen and keybits that were set here.
  181. */
  182. ctx->base.keylen = ctx->key_bits / 8;
  183. }
  184. return 1;
  185. }
  186. CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_START(rc2)
  187. OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_RC2_KEYBITS, NULL),
  188. CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_END(rc2)
  189. CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_START(rc2)
  190. OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
  191. OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_RC2_KEYBITS, NULL),
  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. { 0, NULL } \
  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)