e_idea.c revision 1.13
1/* $OpenBSD: e_idea.c,v 1.13 2022/09/04 13:17:18 jsing Exp $ */ 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3 * All rights reserved. 4 * 5 * This package is an SSL implementation written 6 * by Eric Young (eay@cryptsoft.com). 7 * The implementation was written so as to conform with Netscapes SSL. 8 * 9 * This library is free for commercial and non-commercial use as long as 10 * the following conditions are aheared to. The following conditions 11 * apply to all code found in this distribution, be it the RC4, RSA, 12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13 * included with this distribution is covered by the same copyright terms 14 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15 * 16 * Copyright remains Eric Young's, and as such any Copyright notices in 17 * the code are not to be removed. 18 * If this package is used in a product, Eric Young should be given attribution 19 * as the author of the parts of the library used. 20 * This can be in the form of a textual message at program startup or 21 * in documentation (online or textual) provided with the package. 22 * 23 * Redistribution and use in source and binary forms, with or without 24 * modification, are permitted provided that the following conditions 25 * are met: 26 * 1. Redistributions of source code must retain the copyright 27 * notice, this list of conditions and the following disclaimer. 28 * 2. Redistributions in binary form must reproduce the above copyright 29 * notice, this list of conditions and the following disclaimer in the 30 * documentation and/or other materials provided with the distribution. 31 * 3. All advertising materials mentioning features or use of this software 32 * must display the following acknowledgement: 33 * "This product includes cryptographic software written by 34 * Eric Young (eay@cryptsoft.com)" 35 * The word 'cryptographic' can be left out if the rouines from the library 36 * being used are not cryptographic related :-). 37 * 4. If you include any Windows specific code (or a derivative thereof) from 38 * the apps directory (application code) you must include an acknowledgement: 39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40 * 41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51 * SUCH DAMAGE. 52 * 53 * The licence and distribution terms for any publically available version or 54 * derivative of this code cannot be changed. i.e. this code cannot simply be 55 * copied and put under another distribution licence 56 * [including the GNU Public Licence.] 57 */ 58 59#include <stdio.h> 60#include <string.h> 61 62#include <openssl/opensslconf.h> 63 64#ifndef OPENSSL_NO_IDEA 65 66#include <openssl/evp.h> 67#include <openssl/idea.h> 68#include <openssl/objects.h> 69 70#include "evp_locl.h" 71 72/* NB idea_ecb_encrypt doesn't take an 'encrypt' argument so we treat it as a special 73 * case 74 */ 75 76static int 77idea_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, 78 const unsigned char *iv, int enc) 79{ 80 if (!enc) { 81 if (EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_OFB_MODE) 82 enc = 1; 83 else if (EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_CFB_MODE) 84 enc = 1; 85 } 86 if (enc) 87 idea_set_encrypt_key(key, ctx->cipher_data); 88 else { 89 IDEA_KEY_SCHEDULE tmp; 90 91 idea_set_encrypt_key(key, &tmp); 92 idea_set_decrypt_key(&tmp, ctx->cipher_data); 93 explicit_bzero((unsigned char *)&tmp, 94 sizeof(IDEA_KEY_SCHEDULE)); 95 } 96 return 1; 97} 98 99static int 100idea_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, 101 const unsigned char *in, size_t inl) 102{ 103 size_t i, bl; 104 105 bl = ctx->cipher->block_size; 106 107 if (inl < bl) 108 return 1; 109 110 inl -= bl; 111 112 for (i = 0; i <= inl; i += bl) 113 idea_ecb_encrypt(in + i, out + i, ctx->cipher_data); 114 return 1; 115} 116 117typedef struct { 118 IDEA_KEY_SCHEDULE ks; 119} EVP_IDEA_KEY; 120 121static int 122idea_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) 123{ 124 while (inl >= EVP_MAXCHUNK) { 125 idea_cbc_encrypt(in, out, (long)EVP_MAXCHUNK, &((EVP_IDEA_KEY *)ctx->cipher_data)->ks, ctx->iv, ctx->encrypt); 126 inl -= EVP_MAXCHUNK; 127 in += EVP_MAXCHUNK; 128 out += EVP_MAXCHUNK; 129 } 130 131 if (inl) 132 idea_cbc_encrypt(in, out, (long)inl, &((EVP_IDEA_KEY *)ctx->cipher_data)->ks, ctx->iv, ctx->encrypt); 133 134 return 1; 135} 136 137static int 138idea_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) 139{ 140 while (inl >= EVP_MAXCHUNK) { 141 idea_ofb64_encrypt(in, out, (long)EVP_MAXCHUNK, &((EVP_IDEA_KEY *)ctx->cipher_data)->ks, ctx->iv, &ctx->num); 142 inl -= EVP_MAXCHUNK; 143 in += EVP_MAXCHUNK; 144 out += EVP_MAXCHUNK; 145 } 146 147 if (inl) 148 idea_ofb64_encrypt(in, out, (long)inl, &((EVP_IDEA_KEY *)ctx->cipher_data)->ks, ctx->iv, &ctx->num); 149 150 return 1; 151} 152 153static int 154idea_cfb64_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) 155{ 156 size_t chunk = EVP_MAXCHUNK; 157 158 if (64 == 1) 159 chunk >>= 3; 160 161 if (inl < chunk) 162 chunk = inl; 163 164 while (inl && inl >= chunk) { 165 idea_cfb64_encrypt(in, out, (long)((64 == 1) && !(ctx->flags & EVP_CIPH_FLAG_LENGTH_BITS) ? inl * 8 : inl), &((EVP_IDEA_KEY *)ctx->cipher_data)->ks, ctx->iv, &ctx->num, ctx->encrypt); 166 inl -= chunk; 167 in += chunk; 168 out += chunk; 169 if (inl < chunk) 170 chunk = inl; 171 } 172 173 return 1; 174} 175 176 177static const EVP_CIPHER idea_cbc = { 178 .nid = NID_idea_cbc, 179 .block_size = 8, 180 .key_len = 16, 181 .iv_len = 8, 182 .flags = 0 | EVP_CIPH_CBC_MODE, 183 .init = idea_init_key, 184 .do_cipher = idea_cbc_cipher, 185 .cleanup = NULL, 186 .ctx_size = sizeof(IDEA_KEY_SCHEDULE), 187 .set_asn1_parameters = EVP_CIPHER_set_asn1_iv, 188 .get_asn1_parameters = EVP_CIPHER_get_asn1_iv, 189 .ctrl = NULL, 190 .app_data = NULL, 191}; 192 193const EVP_CIPHER * 194EVP_idea_cbc(void) 195{ 196 return &idea_cbc; 197} 198 199static const EVP_CIPHER idea_cfb64 = { 200 .nid = NID_idea_cfb64, 201 .block_size = 1, 202 .key_len = 16, 203 .iv_len = 8, 204 .flags = 0 | EVP_CIPH_CFB_MODE, 205 .init = idea_init_key, 206 .do_cipher = idea_cfb64_cipher, 207 .cleanup = NULL, 208 .ctx_size = sizeof(IDEA_KEY_SCHEDULE), 209 .set_asn1_parameters = EVP_CIPHER_set_asn1_iv, 210 .get_asn1_parameters = EVP_CIPHER_get_asn1_iv, 211 .ctrl = NULL, 212 .app_data = NULL, 213}; 214 215const EVP_CIPHER * 216EVP_idea_cfb64(void) 217{ 218 return &idea_cfb64; 219} 220 221static const EVP_CIPHER idea_ofb = { 222 .nid = NID_idea_ofb64, 223 .block_size = 1, 224 .key_len = 16, 225 .iv_len = 8, 226 .flags = 0 | EVP_CIPH_OFB_MODE, 227 .init = idea_init_key, 228 .do_cipher = idea_ofb_cipher, 229 .cleanup = NULL, 230 .ctx_size = sizeof(IDEA_KEY_SCHEDULE), 231 .set_asn1_parameters = EVP_CIPHER_set_asn1_iv, 232 .get_asn1_parameters = EVP_CIPHER_get_asn1_iv, 233 .ctrl = NULL, 234 .app_data = NULL, 235}; 236 237const EVP_CIPHER * 238EVP_idea_ofb(void) 239{ 240 return &idea_ofb; 241} 242 243static const EVP_CIPHER idea_ecb = { 244 .nid = NID_idea_ecb, 245 .block_size = 8, 246 .key_len = 16, 247 .iv_len = 0, 248 .flags = 0 | EVP_CIPH_ECB_MODE, 249 .init = idea_init_key, 250 .do_cipher = idea_ecb_cipher, 251 .cleanup = NULL, 252 .ctx_size = sizeof(IDEA_KEY_SCHEDULE), 253 .set_asn1_parameters = EVP_CIPHER_set_asn1_iv, 254 .get_asn1_parameters = EVP_CIPHER_get_asn1_iv, 255 .ctrl = NULL, 256 .app_data = NULL, 257}; 258 259const EVP_CIPHER * 260EVP_idea_ecb(void) 261{ 262 return &idea_ecb; 263} 264#endif 265