e_bf.c revision 1.13
1/* $OpenBSD: e_bf.c,v 1.13 2022/09/10 17:39:47 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 <limits.h>
60#include <stdio.h>
61
62#include <openssl/opensslconf.h>
63
64#ifndef OPENSSL_NO_BF
65
66#include <openssl/blowfish.h>
67#include <openssl/evp.h>
68#include <openssl/objects.h>
69
70#include "evp_locl.h"
71
72typedef struct {
73	BF_KEY ks;
74} EVP_BF_KEY;
75
76#define data(ctx)	((EVP_BF_KEY *)(ctx)->cipher_data)
77
78static int
79bf_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
80    const unsigned char *iv, int enc)
81{
82	BF_set_key(&data(ctx)->ks, EVP_CIPHER_CTX_key_length(ctx), key);
83	return 1;
84}
85
86static int
87bf_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl)
88{
89	if (inl > LONG_MAX)
90		return 0;
91
92	while (inl >= EVP_MAXCHUNK) {
93		BF_cbc_encrypt(in, out, (long)EVP_MAXCHUNK, &((EVP_BF_KEY *)ctx->cipher_data)->ks, ctx->iv, ctx->encrypt);
94		inl -= EVP_MAXCHUNK;
95		in += EVP_MAXCHUNK;
96		out += EVP_MAXCHUNK;
97	}
98
99	if (inl)
100		BF_cbc_encrypt(in, out, (long)inl, &((EVP_BF_KEY *)ctx->cipher_data)->ks, ctx->iv, ctx->encrypt);
101
102	return 1;
103}
104
105static int
106bf_cfb64_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl)
107{
108	size_t chunk = EVP_MAXCHUNK;
109
110	if (inl > LONG_MAX)
111		return 0;
112
113	if (inl < chunk)
114		chunk = inl;
115
116	while (inl && inl >= chunk) {
117		BF_cfb64_encrypt(in, out, (long)chunk, &((EVP_BF_KEY *)ctx->cipher_data)->ks, ctx->iv, &ctx->num, ctx->encrypt);
118		inl -= chunk;
119		in += chunk;
120		out += chunk;
121		if (inl < chunk)
122			chunk = inl;
123	}
124
125	return 1;
126}
127
128static int
129bf_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl)
130{
131	size_t i, bl;
132
133	if (inl > LONG_MAX)
134		return 0;
135
136	bl = ctx->cipher->block_size;
137
138	if (inl < bl)
139		return 1;
140
141	inl -= bl;
142
143	for (i = 0; i <= inl; i += bl)
144		BF_ecb_encrypt(in + i, out + i, &((EVP_BF_KEY *)ctx->cipher_data)->ks, ctx->encrypt);
145
146	return 1;
147}
148
149static int
150bf_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl)
151{
152	if (inl > LONG_MAX)
153		return 0;
154
155	while (inl >= EVP_MAXCHUNK) {
156		BF_ofb64_encrypt(in, out, (long)EVP_MAXCHUNK, &((EVP_BF_KEY *)ctx->cipher_data)->ks, ctx->iv, &ctx->num);
157		inl -= EVP_MAXCHUNK;
158		in += EVP_MAXCHUNK;
159		out += EVP_MAXCHUNK;
160	}
161
162	if (inl)
163		BF_ofb64_encrypt(in, out, (long)inl, &((EVP_BF_KEY *)ctx->cipher_data)->ks, ctx->iv, &ctx->num);
164
165	return 1;
166}
167
168static const EVP_CIPHER bf_cbc = {
169	.nid = NID_bf_cbc,
170	.block_size = 8,
171	.key_len = 16,
172	.iv_len = 8,
173	.flags = EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CBC_MODE,
174	.init = bf_init_key,
175	.do_cipher = bf_cbc_cipher,
176	.cleanup = NULL,
177	.ctx_size = sizeof(EVP_BF_KEY),
178	.set_asn1_parameters = EVP_CIPHER_set_asn1_iv,
179	.get_asn1_parameters = EVP_CIPHER_get_asn1_iv,
180	.ctrl = NULL,
181	.app_data = NULL,
182};
183
184const EVP_CIPHER *
185EVP_bf_cbc(void)
186{
187	return &bf_cbc;
188}
189
190static const EVP_CIPHER bf_cfb64 = {
191	.nid = NID_bf_cfb64,
192	.block_size = 1,
193	.key_len = 16,
194	.iv_len = 8,
195	.flags = EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CFB_MODE,
196	.init = bf_init_key,
197	.do_cipher = bf_cfb64_cipher,
198	.cleanup = NULL,
199	.ctx_size = sizeof(EVP_BF_KEY),
200	.set_asn1_parameters = EVP_CIPHER_set_asn1_iv,
201	.get_asn1_parameters = EVP_CIPHER_get_asn1_iv,
202	.ctrl = NULL,
203	.app_data = NULL,
204};
205
206const EVP_CIPHER *
207EVP_bf_cfb64(void)
208{
209	return &bf_cfb64;
210}
211
212static const EVP_CIPHER bf_ofb = {
213	.nid = NID_bf_ofb64,
214	.block_size = 1,
215	.key_len = 16,
216	.iv_len = 8,
217	.flags = EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_OFB_MODE,
218	.init = bf_init_key,
219	.do_cipher = bf_ofb_cipher,
220	.cleanup = NULL,
221	.ctx_size = sizeof(EVP_BF_KEY),
222	.set_asn1_parameters = EVP_CIPHER_set_asn1_iv,
223	.get_asn1_parameters = EVP_CIPHER_get_asn1_iv,
224	.ctrl = NULL,
225	.app_data = NULL,
226};
227
228const EVP_CIPHER *
229EVP_bf_ofb(void)
230{
231	return &bf_ofb;
232}
233
234static const EVP_CIPHER bf_ecb = {
235	.nid = NID_bf_ecb,
236	.block_size = 8,
237	.key_len = 16,
238	.iv_len = 0,
239	.flags = EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_ECB_MODE,
240	.init = bf_init_key,
241	.do_cipher = bf_ecb_cipher,
242	.cleanup = NULL,
243	.ctx_size = sizeof(EVP_BF_KEY),
244	.set_asn1_parameters = EVP_CIPHER_set_asn1_iv,
245	.get_asn1_parameters = EVP_CIPHER_get_asn1_iv,
246	.ctrl = NULL,
247	.app_data = NULL,
248};
249
250const EVP_CIPHER *
251EVP_bf_ecb(void)
252{
253	return &bf_ecb;
254}
255#endif
256