diff options
Diffstat (limited to 'src/lib/libcrypto/des/cfb64ede.c')
-rw-r--r-- | src/lib/libcrypto/des/cfb64ede.c | 292 |
1 files changed, 144 insertions, 148 deletions
diff --git a/src/lib/libcrypto/des/cfb64ede.c b/src/lib/libcrypto/des/cfb64ede.c index f2b2fe2db5..700a36620f 100644 --- a/src/lib/libcrypto/des/cfb64ede.c +++ b/src/lib/libcrypto/des/cfb64ede.c | |||
@@ -1,25 +1,25 @@ | |||
1 | /* $OpenBSD: cfb64ede.c,v 1.10 2022/11/26 16:08:51 tb Exp $ */ | 1 | /* $OpenBSD: cfb64ede.c,v 1.11 2023/07/08 07:11:07 beck 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 | * |
5 | * This package is an SSL implementation written | 5 | * This package is an SSL implementation written |
6 | * by Eric Young (eay@cryptsoft.com). | 6 | * by Eric Young (eay@cryptsoft.com). |
7 | * The implementation was written so as to conform with Netscapes SSL. | 7 | * The implementation was written so as to conform with Netscapes SSL. |
8 | * | 8 | * |
9 | * This library is free for commercial and non-commercial use as long as | 9 | * This library is free for commercial and non-commercial use as long as |
10 | * the following conditions are aheared to. The following conditions | 10 | * the following conditions are aheared to. The following conditions |
11 | * apply to all code found in this distribution, be it the RC4, RSA, | 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 | 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 | 13 | * included with this distribution is covered by the same copyright terms |
14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
15 | * | 15 | * |
16 | * Copyright remains Eric Young's, and as such any Copyright notices in | 16 | * Copyright remains Eric Young's, and as such any Copyright notices in |
17 | * the code are not to be removed. | 17 | * the code are not to be removed. |
18 | * If this package is used in a product, Eric Young should be given attribution | 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. | 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 | 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. | 21 | * in documentation (online or textual) provided with the package. |
22 | * | 22 | * |
23 | * Redistribution and use in source and binary forms, with or without | 23 | * Redistribution and use in source and binary forms, with or without |
24 | * modification, are permitted provided that the following conditions | 24 | * modification, are permitted provided that the following conditions |
25 | * are met: | 25 | * are met: |
@@ -34,10 +34,10 @@ | |||
34 | * Eric Young (eay@cryptsoft.com)" | 34 | * Eric Young (eay@cryptsoft.com)" |
35 | * The word 'cryptographic' can be left out if the rouines from the library | 35 | * The word 'cryptographic' can be left out if the rouines from the library |
36 | * being used are not cryptographic related :-). | 36 | * being used are not cryptographic related :-). |
37 | * 4. If you include any Windows specific code (or a derivative thereof) from | 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: | 38 | * the apps directory (application code) you must include an acknowledgement: |
39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
40 | * | 40 | * |
41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
@@ -49,7 +49,7 @@ | |||
49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 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 | 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
51 | * SUCH DAMAGE. | 51 | * SUCH DAMAGE. |
52 | * | 52 | * |
53 | * The licence and distribution terms for any publically available version or | 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 | 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
55 | * copied and put under another distribution licence | 55 | * copied and put under another distribution licence |
@@ -63,182 +63,178 @@ | |||
63 | * 64bit block we have used is contained in *num; | 63 | * 64bit block we have used is contained in *num; |
64 | */ | 64 | */ |
65 | 65 | ||
66 | void DES_ede3_cfb64_encrypt(const unsigned char *in, unsigned char *out, | 66 | void |
67 | long length, DES_key_schedule *ks1, | 67 | DES_ede3_cfb64_encrypt(const unsigned char *in, unsigned char *out, |
68 | DES_key_schedule *ks2, DES_key_schedule *ks3, | 68 | long length, DES_key_schedule *ks1, |
69 | DES_cblock *ivec, int *num, int enc) | 69 | DES_key_schedule *ks2, DES_key_schedule *ks3, |
70 | { | 70 | DES_cblock *ivec, int *num, int enc) |
71 | DES_LONG v0,v1; | 71 | { |
72 | long l=length; | 72 | DES_LONG v0, v1; |
73 | int n= *num; | 73 | long l = length; |
74 | int n = *num; | ||
74 | DES_LONG ti[2]; | 75 | DES_LONG ti[2]; |
75 | unsigned char *iv,c,cc; | 76 | unsigned char *iv, c, cc; |
76 | 77 | ||
77 | iv=&(*ivec)[0]; | 78 | iv = &(*ivec)[0]; |
78 | if (enc) | 79 | if (enc) { |
79 | { | 80 | while (l--) { |
80 | while (l--) | 81 | if (n == 0) { |
81 | { | 82 | c2l(iv, v0); |
82 | if (n == 0) | 83 | c2l(iv, v1); |
83 | { | ||
84 | c2l(iv,v0); | ||
85 | c2l(iv,v1); | ||
86 | 84 | ||
87 | ti[0]=v0; | 85 | ti[0] = v0; |
88 | ti[1]=v1; | 86 | ti[1] = v1; |
89 | DES_encrypt3(ti,ks1,ks2,ks3); | 87 | DES_encrypt3(ti, ks1, ks2, ks3); |
90 | v0=ti[0]; | 88 | v0 = ti[0]; |
91 | v1=ti[1]; | 89 | v1 = ti[1]; |
92 | 90 | ||
93 | iv = &(*ivec)[0]; | 91 | iv = &(*ivec)[0]; |
94 | l2c(v0,iv); | 92 | l2c(v0, iv); |
95 | l2c(v1,iv); | 93 | l2c(v1, iv); |
96 | iv = &(*ivec)[0]; | 94 | iv = &(*ivec)[0]; |
97 | } | ||
98 | c= *(in++)^iv[n]; | ||
99 | *(out++)=c; | ||
100 | iv[n]=c; | ||
101 | n=(n+1)&0x07; | ||
102 | } | 95 | } |
96 | c = *(in++) ^ iv[n]; | ||
97 | *(out++) = c; | ||
98 | iv[n] = c; | ||
99 | n = (n + 1) & 0x07; | ||
103 | } | 100 | } |
104 | else | 101 | } else { |
105 | { | 102 | while (l--) { |
106 | while (l--) | 103 | if (n == 0) { |
107 | { | 104 | c2l(iv, v0); |
108 | if (n == 0) | 105 | c2l(iv, v1); |
109 | { | ||
110 | c2l(iv,v0); | ||
111 | c2l(iv,v1); | ||
112 | 106 | ||
113 | ti[0]=v0; | 107 | ti[0] = v0; |
114 | ti[1]=v1; | 108 | ti[1] = v1; |
115 | DES_encrypt3(ti,ks1,ks2,ks3); | 109 | DES_encrypt3(ti, ks1, ks2, ks3); |
116 | v0=ti[0]; | 110 | v0 = ti[0]; |
117 | v1=ti[1]; | 111 | v1 = ti[1]; |
118 | 112 | ||
119 | iv = &(*ivec)[0]; | 113 | iv = &(*ivec)[0]; |
120 | l2c(v0,iv); | 114 | l2c(v0, iv); |
121 | l2c(v1,iv); | 115 | l2c(v1, iv); |
122 | iv = &(*ivec)[0]; | 116 | iv = &(*ivec)[0]; |
123 | } | ||
124 | cc= *(in++); | ||
125 | c=iv[n]; | ||
126 | iv[n]=cc; | ||
127 | *(out++)=c^cc; | ||
128 | n=(n+1)&0x07; | ||
129 | } | 117 | } |
118 | cc = *(in++); | ||
119 | c = iv[n]; | ||
120 | iv[n] = cc; | ||
121 | *(out++) = c ^ cc; | ||
122 | n = (n + 1) & 0x07; | ||
130 | } | 123 | } |
131 | v0=v1=ti[0]=ti[1]=c=cc=0; | ||
132 | *num=n; | ||
133 | } | 124 | } |
125 | v0 = v1 = ti[0] = ti[1] = c = cc = 0; | ||
126 | *num = n; | ||
127 | } | ||
134 | 128 | ||
135 | /* This is compatible with the single key CFB-r for DES, even thought that's | 129 | /* This is compatible with the single key CFB-r for DES, even thought that's |
136 | * not what EVP needs. | 130 | * not what EVP needs. |
137 | */ | 131 | */ |
138 | 132 | ||
139 | void DES_ede3_cfb_encrypt(const unsigned char *in,unsigned char *out, | 133 | void |
140 | int numbits,long length,DES_key_schedule *ks1, | 134 | DES_ede3_cfb_encrypt(const unsigned char *in, unsigned char *out, |
141 | DES_key_schedule *ks2,DES_key_schedule *ks3, | 135 | int numbits, long length, DES_key_schedule *ks1, |
142 | DES_cblock *ivec,int enc) | 136 | DES_key_schedule *ks2, DES_key_schedule *ks3, |
143 | { | 137 | DES_cblock *ivec, int enc) |
144 | DES_LONG d0,d1,v0,v1; | 138 | { |
145 | unsigned long l=length,n=((unsigned int)numbits+7)/8; | 139 | DES_LONG d0, d1, v0, v1; |
146 | int num=numbits,i; | 140 | unsigned long l = length, n = ((unsigned int)numbits + 7)/8; |
141 | int num = numbits, i; | ||
147 | DES_LONG ti[2]; | 142 | DES_LONG ti[2]; |
148 | unsigned char *iv; | 143 | unsigned char *iv; |
149 | unsigned char ovec[16]; | 144 | unsigned char ovec[16]; |
150 | 145 | ||
151 | if (num > 64) return; | 146 | if (num > 64) |
147 | return; | ||
152 | iv = &(*ivec)[0]; | 148 | iv = &(*ivec)[0]; |
153 | c2l(iv,v0); | 149 | c2l(iv, v0); |
154 | c2l(iv,v1); | 150 | c2l(iv, v1); |
155 | if (enc) | 151 | if (enc) { |
156 | { | 152 | while (l >= n) { |
157 | while (l >= n) | 153 | l -= n; |
158 | { | 154 | ti[0] = v0; |
159 | l-=n; | 155 | ti[1] = v1; |
160 | ti[0]=v0; | 156 | DES_encrypt3(ti, ks1, ks2, ks3); |
161 | ti[1]=v1; | 157 | c2ln(in, d0, d1, n); |
162 | DES_encrypt3(ti,ks1,ks2,ks3); | 158 | in += n; |
163 | c2ln(in,d0,d1,n); | 159 | d0 ^= ti[0]; |
164 | in+=n; | 160 | d1 ^= ti[1]; |
165 | d0^=ti[0]; | 161 | l2cn(d0, d1, out, n); |
166 | d1^=ti[1]; | 162 | out += n; |
167 | l2cn(d0,d1,out,n); | ||
168 | out+=n; | ||
169 | /* 30-08-94 - eay - changed because l>>32 and | 163 | /* 30-08-94 - eay - changed because l>>32 and |
170 | * l<<32 are bad under gcc :-( */ | 164 | * l<<32 are bad under gcc :-( */ |
171 | if (num == 32) | 165 | if (num == 32) { |
172 | { v0=v1; v1=d0; } | 166 | v0 = v1; |
173 | else if (num == 64) | 167 | v1 = d0; |
174 | { v0=d0; v1=d1; } | 168 | } else if (num == 64) { |
175 | else | 169 | v0 = d0; |
176 | { | 170 | v1 = d1; |
177 | iv=&ovec[0]; | 171 | } else { |
178 | l2c(v0,iv); | 172 | iv = &ovec[0]; |
179 | l2c(v1,iv); | 173 | l2c(v0, iv); |
180 | l2c(d0,iv); | 174 | l2c(v1, iv); |
181 | l2c(d1,iv); | 175 | l2c(d0, iv); |
176 | l2c(d1, iv); | ||
182 | /* shift ovec left most of the bits... */ | 177 | /* shift ovec left most of the bits... */ |
183 | memmove(ovec,ovec+num/8,8+(num%8 ? 1 : 0)); | 178 | memmove(ovec, ovec + num/8, |
179 | 8 + (num % 8 ? 1 : 0)); | ||
184 | /* now the remaining bits */ | 180 | /* now the remaining bits */ |
185 | if(num%8 != 0) | 181 | if (num % 8 != 0) |
186 | for(i=0 ; i < 8 ; ++i) | 182 | for (i = 0; i < 8; ++i) |
187 | { | 183 | { |
188 | ovec[i]<<=num%8; | 184 | ovec[i] <<= num % 8; |
189 | ovec[i]|=ovec[i+1]>>(8-num%8); | 185 | ovec[i] |= ovec[i + 1] >> |
190 | } | 186 | (8 - num % 8); |
191 | iv=&ovec[0]; | 187 | } |
192 | c2l(iv,v0); | 188 | iv = &ovec[0]; |
193 | c2l(iv,v1); | 189 | c2l(iv, v0); |
194 | } | 190 | c2l(iv, v1); |
195 | } | 191 | } |
196 | } | 192 | } |
197 | else | 193 | } else { |
198 | { | 194 | while (l >= n) { |
199 | while (l >= n) | 195 | l -= n; |
200 | { | 196 | ti[0] = v0; |
201 | l-=n; | 197 | ti[1] = v1; |
202 | ti[0]=v0; | 198 | DES_encrypt3(ti, ks1, ks2, ks3); |
203 | ti[1]=v1; | 199 | c2ln(in, d0, d1, n); |
204 | DES_encrypt3(ti,ks1,ks2,ks3); | 200 | in += n; |
205 | c2ln(in,d0,d1,n); | ||
206 | in+=n; | ||
207 | /* 30-08-94 - eay - changed because l>>32 and | 201 | /* 30-08-94 - eay - changed because l>>32 and |
208 | * l<<32 are bad under gcc :-( */ | 202 | * l<<32 are bad under gcc :-( */ |
209 | if (num == 32) | 203 | if (num == 32) { |
210 | { v0=v1; v1=d0; } | 204 | v0 = v1; |
211 | else if (num == 64) | 205 | v1 = d0; |
212 | { v0=d0; v1=d1; } | 206 | } else if (num == 64) { |
213 | else | 207 | v0 = d0; |
214 | { | 208 | v1 = d1; |
215 | iv=&ovec[0]; | 209 | } else { |
216 | l2c(v0,iv); | 210 | iv = &ovec[0]; |
217 | l2c(v1,iv); | 211 | l2c(v0, iv); |
218 | l2c(d0,iv); | 212 | l2c(v1, iv); |
219 | l2c(d1,iv); | 213 | l2c(d0, iv); |
214 | l2c(d1, iv); | ||
220 | /* shift ovec left most of the bits... */ | 215 | /* shift ovec left most of the bits... */ |
221 | memmove(ovec,ovec+num/8,8+(num%8 ? 1 : 0)); | 216 | memmove(ovec, ovec + num/8, |
217 | 8 + (num % 8 ? 1 : 0)); | ||
222 | /* now the remaining bits */ | 218 | /* now the remaining bits */ |
223 | if(num%8 != 0) | 219 | if (num % 8 != 0) |
224 | for(i=0 ; i < 8 ; ++i) | 220 | for (i = 0; i < 8; ++i) |
225 | { | 221 | { |
226 | ovec[i]<<=num%8; | 222 | ovec[i] <<= num % 8; |
227 | ovec[i]|=ovec[i+1]>>(8-num%8); | 223 | ovec[i] |= ovec[i + 1] >> |
228 | } | 224 | (8 - num % 8); |
229 | iv=&ovec[0]; | 225 | } |
230 | c2l(iv,v0); | 226 | iv = &ovec[0]; |
231 | c2l(iv,v1); | 227 | c2l(iv, v0); |
232 | } | 228 | c2l(iv, v1); |
233 | d0^=ti[0]; | ||
234 | d1^=ti[1]; | ||
235 | l2cn(d0,d1,out,n); | ||
236 | out+=n; | ||
237 | } | 229 | } |
230 | d0 ^= ti[0]; | ||
231 | d1 ^= ti[1]; | ||
232 | l2cn(d0, d1, out, n); | ||
233 | out += n; | ||
238 | } | 234 | } |
239 | iv = &(*ivec)[0]; | ||
240 | l2c(v0,iv); | ||
241 | l2c(v1,iv); | ||
242 | v0=v1=d0=d1=ti[0]=ti[1]=0; | ||
243 | } | 235 | } |
244 | 236 | iv = &(*ivec)[0]; | |
237 | l2c(v0, iv); | ||
238 | l2c(v1, iv); | ||
239 | v0 = v1 = d0 = d1 = ti[0] = ti[1] = 0; | ||
240 | } | ||