diff options
Diffstat (limited to '')
| -rw-r--r-- | src/lib/libcrypto/evp/e_rc2.c | 169 |
1 files changed, 161 insertions, 8 deletions
diff --git a/src/lib/libcrypto/evp/e_rc2.c b/src/lib/libcrypto/evp/e_rc2.c index b07fafddfd..d91b86dd03 100644 --- a/src/lib/libcrypto/evp/e_rc2.c +++ b/src/lib/libcrypto/evp/e_rc2.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: e_rc2.c,v 1.14 2022/01/20 11:31:37 inoguchi Exp $ */ | 1 | /* $OpenBSD: e_rc2.c,v 1.15 2022/09/03 19:59:32 jsing Exp $ */ |
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 3 | * All rights reserved. | 3 | * All rights reserved. |
| 4 | * | 4 | * |
| @@ -84,13 +84,166 @@ typedef struct { | |||
| 84 | 84 | ||
| 85 | #define data(ctx) ((EVP_RC2_KEY *)(ctx)->cipher_data) | 85 | #define data(ctx) ((EVP_RC2_KEY *)(ctx)->cipher_data) |
| 86 | 86 | ||
| 87 | IMPLEMENT_BLOCK_CIPHER(rc2, ks, RC2, EVP_RC2_KEY, NID_rc2, | 87 | static int |
| 88 | 8, | 88 | rc2_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) |
| 89 | RC2_KEY_LENGTH, 8, 64, | 89 | { |
| 90 | EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT, | 90 | while (inl >= EVP_MAXCHUNK) { |
| 91 | rc2_init_key, NULL, | 91 | RC2_cbc_encrypt(in, out, (long)EVP_MAXCHUNK, &((EVP_RC2_KEY *)ctx->cipher_data)->ks, ctx->iv, ctx->encrypt); |
| 92 | rc2_set_asn1_type_and_iv, rc2_get_asn1_type_and_iv, | 92 | inl -= EVP_MAXCHUNK; |
| 93 | rc2_ctrl) | 93 | in += EVP_MAXCHUNK; |
| 94 | out += EVP_MAXCHUNK; | ||
| 95 | } | ||
| 96 | |||
| 97 | if (inl) | ||
| 98 | RC2_cbc_encrypt(in, out, (long)inl, &((EVP_RC2_KEY *)ctx->cipher_data)->ks, ctx->iv, ctx->encrypt); | ||
| 99 | |||
| 100 | return 1; | ||
| 101 | } | ||
| 102 | |||
| 103 | static int | ||
| 104 | rc2_cfb64_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) | ||
| 105 | { | ||
| 106 | size_t chunk = EVP_MAXCHUNK; | ||
| 107 | |||
| 108 | if (64 == 1) | ||
| 109 | chunk >>= 3; | ||
| 110 | |||
| 111 | if (inl < chunk) | ||
| 112 | chunk = inl; | ||
| 113 | |||
| 114 | while (inl && inl >= chunk) { | ||
| 115 | 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); | ||
| 116 | inl -= chunk; | ||
| 117 | in += chunk; | ||
| 118 | out += chunk; | ||
| 119 | if (inl < chunk) | ||
| 120 | chunk = inl; | ||
| 121 | } | ||
| 122 | |||
| 123 | return 1; | ||
| 124 | } | ||
| 125 | |||
| 126 | static int | ||
| 127 | rc2_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) | ||
| 128 | { | ||
| 129 | size_t i, bl; | ||
| 130 | |||
| 131 | bl = ctx->cipher->block_size; | ||
| 132 | |||
| 133 | if (inl < bl) | ||
| 134 | return 1; | ||
| 135 | |||
| 136 | inl -= bl; | ||
| 137 | |||
| 138 | for (i = 0; i <= inl; i += bl) | ||
| 139 | RC2_ecb_encrypt(in + i, out + i, &((EVP_RC2_KEY *)ctx->cipher_data)->ks, ctx->encrypt); | ||
| 140 | |||
| 141 | return 1; | ||
| 142 | } | ||
| 143 | |||
| 144 | static int | ||
| 145 | rc2_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) | ||
| 146 | { | ||
| 147 | while (inl >= EVP_MAXCHUNK) { | ||
| 148 | RC2_ofb64_encrypt(in, out, (long)EVP_MAXCHUNK, &((EVP_RC2_KEY *)ctx->cipher_data)->ks, ctx->iv, &ctx->num); | ||
| 149 | inl -= EVP_MAXCHUNK; | ||
| 150 | in += EVP_MAXCHUNK; | ||
| 151 | out += EVP_MAXCHUNK; | ||
| 152 | } | ||
| 153 | |||
| 154 | if (inl) | ||
| 155 | RC2_ofb64_encrypt(in, out, (long)inl, &((EVP_RC2_KEY *)ctx->cipher_data)->ks, ctx->iv, &ctx->num); | ||
| 156 | |||
| 157 | return 1; | ||
| 158 | } | ||
| 159 | |||
| 160 | static const EVP_CIPHER rc2_cbc = { | ||
| 161 | .nid = NID_rc2_cbc, | ||
| 162 | .block_size = 8, | ||
| 163 | .key_len = RC2_KEY_LENGTH, | ||
| 164 | .iv_len = 8, | ||
| 165 | .flags = EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT | EVP_CIPH_CBC_MODE, | ||
| 166 | .init = rc2_init_key, | ||
| 167 | .do_cipher = rc2_cbc_cipher, | ||
| 168 | .cleanup = NULL, | ||
| 169 | .ctx_size = sizeof(EVP_RC2_KEY), | ||
| 170 | .set_asn1_parameters = rc2_set_asn1_type_and_iv, | ||
| 171 | .get_asn1_parameters = rc2_get_asn1_type_and_iv, | ||
| 172 | .ctrl = rc2_ctrl, | ||
| 173 | .app_data = NULL, | ||
| 174 | }; | ||
| 175 | |||
| 176 | const EVP_CIPHER * | ||
| 177 | EVP_rc2_cbc(void) | ||
| 178 | { | ||
| 179 | return &rc2_cbc; | ||
| 180 | } | ||
| 181 | |||
| 182 | static const EVP_CIPHER rc2_cfb64 = { | ||
| 183 | .nid = NID_rc2_cfb64, | ||
| 184 | .block_size = 1, | ||
| 185 | .key_len = RC2_KEY_LENGTH, | ||
| 186 | .iv_len = 8, | ||
| 187 | .flags = EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT | EVP_CIPH_CFB_MODE, | ||
| 188 | .init = rc2_init_key, | ||
| 189 | .do_cipher = rc2_cfb64_cipher, | ||
| 190 | .cleanup = NULL, | ||
| 191 | .ctx_size = sizeof(EVP_RC2_KEY), | ||
| 192 | .set_asn1_parameters = rc2_set_asn1_type_and_iv, | ||
| 193 | .get_asn1_parameters = rc2_get_asn1_type_and_iv, | ||
| 194 | .ctrl = rc2_ctrl, | ||
| 195 | .app_data = NULL, | ||
| 196 | }; | ||
| 197 | |||
| 198 | const EVP_CIPHER * | ||
| 199 | EVP_rc2_cfb64(void) | ||
| 200 | { | ||
| 201 | return &rc2_cfb64; | ||
| 202 | } | ||
| 203 | |||
| 204 | static const EVP_CIPHER rc2_ofb = { | ||
| 205 | .nid = NID_rc2_ofb64, | ||
| 206 | .block_size = 1, | ||
| 207 | .key_len = RC2_KEY_LENGTH, | ||
| 208 | .iv_len = 8, | ||
| 209 | .flags = EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT | EVP_CIPH_OFB_MODE, | ||
| 210 | .init = rc2_init_key, | ||
| 211 | .do_cipher = rc2_ofb_cipher, | ||
| 212 | .cleanup = NULL, | ||
| 213 | .ctx_size = sizeof(EVP_RC2_KEY), | ||
| 214 | .set_asn1_parameters = rc2_set_asn1_type_and_iv, | ||
| 215 | .get_asn1_parameters = rc2_get_asn1_type_and_iv, | ||
| 216 | .ctrl = rc2_ctrl, | ||
| 217 | .app_data = NULL, | ||
| 218 | }; | ||
| 219 | |||
| 220 | const EVP_CIPHER * | ||
| 221 | EVP_rc2_ofb(void) | ||
| 222 | { | ||
| 223 | return &rc2_ofb; | ||
| 224 | } | ||
| 225 | |||
| 226 | static const EVP_CIPHER rc2_ecb = { | ||
| 227 | .nid = NID_rc2_ecb, | ||
| 228 | .block_size = 8, | ||
| 229 | .key_len = RC2_KEY_LENGTH, | ||
| 230 | .iv_len = 0, | ||
| 231 | .flags = EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT | EVP_CIPH_ECB_MODE, | ||
| 232 | .init = rc2_init_key, | ||
| 233 | .do_cipher = rc2_ecb_cipher, | ||
| 234 | .cleanup = NULL, | ||
| 235 | .ctx_size = sizeof(EVP_RC2_KEY), | ||
| 236 | .set_asn1_parameters = rc2_set_asn1_type_and_iv, | ||
| 237 | .get_asn1_parameters = rc2_get_asn1_type_and_iv, | ||
| 238 | .ctrl = rc2_ctrl, | ||
| 239 | .app_data = NULL, | ||
| 240 | }; | ||
| 241 | |||
| 242 | const EVP_CIPHER * | ||
| 243 | EVP_rc2_ecb(void) | ||
| 244 | { | ||
| 245 | return &rc2_ecb; | ||
| 246 | } | ||
| 94 | 247 | ||
| 95 | #define RC2_40_MAGIC 0xa0 | 248 | #define RC2_40_MAGIC 0xa0 |
| 96 | #define RC2_64_MAGIC 0x78 | 249 | #define RC2_64_MAGIC 0x78 |
