-/* $OpenBSD: e_rc2.c,v 1.14 2022/01/20 11:31:37 inoguchi Exp $ */
+/* $OpenBSD: e_rc2.c,v 1.15 2022/09/03 19:59:32 jsing Exp $ */
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
* All rights reserved.
*
#define data(ctx) ((EVP_RC2_KEY *)(ctx)->cipher_data)
-IMPLEMENT_BLOCK_CIPHER(rc2, ks, RC2, EVP_RC2_KEY, NID_rc2,
- 8,
- RC2_KEY_LENGTH, 8, 64,
- EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT,
- rc2_init_key, NULL,
- rc2_set_asn1_type_and_iv, rc2_get_asn1_type_and_iv,
- rc2_ctrl)
+static int
+rc2_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl)
+{
+ while (inl >= EVP_MAXCHUNK) {
+ RC2_cbc_encrypt(in, out, (long)EVP_MAXCHUNK, &((EVP_RC2_KEY *)ctx->cipher_data)->ks, ctx->iv, ctx->encrypt);
+ inl -= EVP_MAXCHUNK;
+ in += EVP_MAXCHUNK;
+ out += EVP_MAXCHUNK;
+ }
+
+ if (inl)
+ RC2_cbc_encrypt(in, out, (long)inl, &((EVP_RC2_KEY *)ctx->cipher_data)->ks, ctx->iv, ctx->encrypt);
+
+ return 1;
+}
+
+static int
+rc2_cfb64_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl)
+{
+ size_t chunk = EVP_MAXCHUNK;
+
+ if (64 == 1)
+ chunk >>= 3;
+
+ if (inl < chunk)
+ chunk = inl;
+
+ while (inl && inl >= chunk) {
+ RC2_cfb64_encrypt(in, out, (long)((64 == 1) && !(ctx->flags & EVP_CIPH_FLAG_LENGTH_BITS) ? inl * 8 : inl), &((EVP_RC2_KEY *)ctx->cipher_data)->ks, ctx->iv, &ctx->num, ctx->encrypt);
+ inl -= chunk;
+ in += chunk;
+ out += chunk;
+ if (inl < chunk)
+ chunk = inl;
+ }
+
+ return 1;
+}
+
+static int
+rc2_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl)
+{
+ size_t i, bl;
+
+ bl = ctx->cipher->block_size;
+
+ if (inl < bl)
+ return 1;
+
+ inl -= bl;
+
+ for (i = 0; i <= inl; i += bl)
+ RC2_ecb_encrypt(in + i, out + i, &((EVP_RC2_KEY *)ctx->cipher_data)->ks, ctx->encrypt);
+
+ return 1;
+}
+
+static int
+rc2_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl)
+{
+ while (inl >= EVP_MAXCHUNK) {
+ RC2_ofb64_encrypt(in, out, (long)EVP_MAXCHUNK, &((EVP_RC2_KEY *)ctx->cipher_data)->ks, ctx->iv, &ctx->num);
+ inl -= EVP_MAXCHUNK;
+ in += EVP_MAXCHUNK;
+ out += EVP_MAXCHUNK;
+ }
+
+ if (inl)
+ RC2_ofb64_encrypt(in, out, (long)inl, &((EVP_RC2_KEY *)ctx->cipher_data)->ks, ctx->iv, &ctx->num);
+
+ return 1;
+}
+
+static const EVP_CIPHER rc2_cbc = {
+ .nid = NID_rc2_cbc,
+ .block_size = 8,
+ .key_len = RC2_KEY_LENGTH,
+ .iv_len = 8,
+ .flags = EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT | EVP_CIPH_CBC_MODE,
+ .init = rc2_init_key,
+ .do_cipher = rc2_cbc_cipher,
+ .cleanup = NULL,
+ .ctx_size = sizeof(EVP_RC2_KEY),
+ .set_asn1_parameters = rc2_set_asn1_type_and_iv,
+ .get_asn1_parameters = rc2_get_asn1_type_and_iv,
+ .ctrl = rc2_ctrl,
+ .app_data = NULL,
+};
+
+const EVP_CIPHER *
+EVP_rc2_cbc(void)
+{
+ return &rc2_cbc;
+}
+
+static const EVP_CIPHER rc2_cfb64 = {
+ .nid = NID_rc2_cfb64,
+ .block_size = 1,
+ .key_len = RC2_KEY_LENGTH,
+ .iv_len = 8,
+ .flags = EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT | EVP_CIPH_CFB_MODE,
+ .init = rc2_init_key,
+ .do_cipher = rc2_cfb64_cipher,
+ .cleanup = NULL,
+ .ctx_size = sizeof(EVP_RC2_KEY),
+ .set_asn1_parameters = rc2_set_asn1_type_and_iv,
+ .get_asn1_parameters = rc2_get_asn1_type_and_iv,
+ .ctrl = rc2_ctrl,
+ .app_data = NULL,
+};
+
+const EVP_CIPHER *
+EVP_rc2_cfb64(void)
+{
+ return &rc2_cfb64;
+}
+
+static const EVP_CIPHER rc2_ofb = {
+ .nid = NID_rc2_ofb64,
+ .block_size = 1,
+ .key_len = RC2_KEY_LENGTH,
+ .iv_len = 8,
+ .flags = EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT | EVP_CIPH_OFB_MODE,
+ .init = rc2_init_key,
+ .do_cipher = rc2_ofb_cipher,
+ .cleanup = NULL,
+ .ctx_size = sizeof(EVP_RC2_KEY),
+ .set_asn1_parameters = rc2_set_asn1_type_and_iv,
+ .get_asn1_parameters = rc2_get_asn1_type_and_iv,
+ .ctrl = rc2_ctrl,
+ .app_data = NULL,
+};
+
+const EVP_CIPHER *
+EVP_rc2_ofb(void)
+{
+ return &rc2_ofb;
+}
+
+static const EVP_CIPHER rc2_ecb = {
+ .nid = NID_rc2_ecb,
+ .block_size = 8,
+ .key_len = RC2_KEY_LENGTH,
+ .iv_len = 0,
+ .flags = EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT | EVP_CIPH_ECB_MODE,
+ .init = rc2_init_key,
+ .do_cipher = rc2_ecb_cipher,
+ .cleanup = NULL,
+ .ctx_size = sizeof(EVP_RC2_KEY),
+ .set_asn1_parameters = rc2_set_asn1_type_and_iv,
+ .get_asn1_parameters = rc2_get_asn1_type_and_iv,
+ .ctrl = rc2_ctrl,
+ .app_data = NULL,
+};
+
+const EVP_CIPHER *
+EVP_rc2_ecb(void)
+{
+ return &rc2_ecb;
+}
#define RC2_40_MAGIC 0xa0
#define RC2_64_MAGIC 0x78