]>
Commit | Line | Data |
---|---|---|
d2e9e320 RS |
1 | /* |
2 | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. | |
0f113f3e | 3 | * |
d2e9e320 RS |
4 | * Licensed under the OpenSSL license (the "License"). You may not use |
5 | * this file except in compliance with the License. You can obtain a copy | |
6 | * in the file LICENSE in the source distribution or at | |
7 | * https://www.openssl.org/source/license.html | |
d02b48c6 RE |
8 | */ |
9 | ||
10 | #include "des_locl.h" | |
1a4b8e7c | 11 | #include "e_os.h" |
d02b48c6 | 12 | |
0f113f3e MC |
13 | /* |
14 | * The input and output encrypted as though 64bit cfb mode is being used. | |
15 | * The extra state information to record how much of the 64bit block we have | |
16 | * used is contained in *num; | |
d02b48c6 RE |
17 | */ |
18 | ||
c2e4f17c | 19 | void DES_ede3_cfb64_encrypt(const unsigned char *in, unsigned char *out, |
0f113f3e MC |
20 | long length, DES_key_schedule *ks1, |
21 | DES_key_schedule *ks2, DES_key_schedule *ks3, | |
22 | DES_cblock *ivec, int *num, int enc) | |
23 | { | |
24 | register DES_LONG v0, v1; | |
25 | register long l = length; | |
26 | register int n = *num; | |
27 | DES_LONG ti[2]; | |
28 | unsigned char *iv, c, cc; | |
d02b48c6 | 29 | |
0f113f3e MC |
30 | iv = &(*ivec)[0]; |
31 | if (enc) { | |
32 | while (l--) { | |
33 | if (n == 0) { | |
34 | c2l(iv, v0); | |
35 | c2l(iv, v1); | |
d02b48c6 | 36 | |
0f113f3e MC |
37 | ti[0] = v0; |
38 | ti[1] = v1; | |
39 | DES_encrypt3(ti, ks1, ks2, ks3); | |
40 | v0 = ti[0]; | |
41 | v1 = ti[1]; | |
d02b48c6 | 42 | |
0f113f3e MC |
43 | iv = &(*ivec)[0]; |
44 | l2c(v0, iv); | |
45 | l2c(v1, iv); | |
46 | iv = &(*ivec)[0]; | |
47 | } | |
48 | c = *(in++) ^ iv[n]; | |
49 | *(out++) = c; | |
50 | iv[n] = c; | |
51 | n = (n + 1) & 0x07; | |
52 | } | |
53 | } else { | |
54 | while (l--) { | |
55 | if (n == 0) { | |
56 | c2l(iv, v0); | |
57 | c2l(iv, v1); | |
d02b48c6 | 58 | |
0f113f3e MC |
59 | ti[0] = v0; |
60 | ti[1] = v1; | |
61 | DES_encrypt3(ti, ks1, ks2, ks3); | |
62 | v0 = ti[0]; | |
63 | v1 = ti[1]; | |
d02b48c6 | 64 | |
0f113f3e MC |
65 | iv = &(*ivec)[0]; |
66 | l2c(v0, iv); | |
67 | l2c(v1, iv); | |
68 | iv = &(*ivec)[0]; | |
69 | } | |
70 | cc = *(in++); | |
71 | c = iv[n]; | |
72 | iv[n] = cc; | |
73 | *(out++) = c ^ cc; | |
74 | n = (n + 1) & 0x07; | |
75 | } | |
76 | } | |
77 | v0 = v1 = ti[0] = ti[1] = c = cc = 0; | |
78 | *num = n; | |
79 | } | |
d02b48c6 | 80 | |
0f113f3e MC |
81 | /* |
82 | * This is compatible with the single key CFB-r for DES, even thought that's | |
8c6336b0 AP |
83 | * not what EVP needs. |
84 | */ | |
85 | ||
0f113f3e MC |
86 | void DES_ede3_cfb_encrypt(const unsigned char *in, unsigned char *out, |
87 | int numbits, long length, DES_key_schedule *ks1, | |
88 | DES_key_schedule *ks2, DES_key_schedule *ks3, | |
89 | DES_cblock *ivec, int enc) | |
90 | { | |
91 | register DES_LONG d0, d1, v0, v1; | |
92 | register unsigned long l = length, n = ((unsigned int)numbits + 7) / 8; | |
93 | register int num = numbits, i; | |
94 | DES_LONG ti[2]; | |
95 | unsigned char *iv; | |
96 | unsigned char ovec[16]; | |
8c6336b0 | 97 | |
0f113f3e MC |
98 | if (num > 64) |
99 | return; | |
100 | iv = &(*ivec)[0]; | |
101 | c2l(iv, v0); | |
102 | c2l(iv, v1); | |
103 | if (enc) { | |
104 | while (l >= n) { | |
105 | l -= n; | |
106 | ti[0] = v0; | |
107 | ti[1] = v1; | |
108 | DES_encrypt3(ti, ks1, ks2, ks3); | |
109 | c2ln(in, d0, d1, n); | |
110 | in += n; | |
111 | d0 ^= ti[0]; | |
112 | d1 ^= ti[1]; | |
113 | l2cn(d0, d1, out, n); | |
114 | out += n; | |
115 | /* | |
116 | * 30-08-94 - eay - changed because l>>32 and l<<32 are bad under | |
117 | * gcc :-( | |
118 | */ | |
119 | if (num == 32) { | |
120 | v0 = v1; | |
121 | v1 = d0; | |
122 | } else if (num == 64) { | |
123 | v0 = d0; | |
124 | v1 = d1; | |
125 | } else { | |
126 | iv = &ovec[0]; | |
127 | l2c(v0, iv); | |
128 | l2c(v1, iv); | |
129 | l2c(d0, iv); | |
130 | l2c(d1, iv); | |
131 | /* shift ovec left most of the bits... */ | |
132 | memmove(ovec, ovec + num / 8, 8 + (num % 8 ? 1 : 0)); | |
133 | /* now the remaining bits */ | |
134 | if (num % 8 != 0) | |
135 | for (i = 0; i < 8; ++i) { | |
136 | ovec[i] <<= num % 8; | |
137 | ovec[i] |= ovec[i + 1] >> (8 - num % 8); | |
138 | } | |
139 | iv = &ovec[0]; | |
140 | c2l(iv, v0); | |
141 | c2l(iv, v1); | |
142 | } | |
143 | } | |
144 | } else { | |
145 | while (l >= n) { | |
146 | l -= n; | |
147 | ti[0] = v0; | |
148 | ti[1] = v1; | |
149 | DES_encrypt3(ti, ks1, ks2, ks3); | |
150 | c2ln(in, d0, d1, n); | |
151 | in += n; | |
152 | /* | |
153 | * 30-08-94 - eay - changed because l>>32 and l<<32 are bad under | |
154 | * gcc :-( | |
155 | */ | |
156 | if (num == 32) { | |
157 | v0 = v1; | |
158 | v1 = d0; | |
159 | } else if (num == 64) { | |
160 | v0 = d0; | |
161 | v1 = d1; | |
162 | } else { | |
163 | iv = &ovec[0]; | |
164 | l2c(v0, iv); | |
165 | l2c(v1, iv); | |
166 | l2c(d0, iv); | |
167 | l2c(d1, iv); | |
168 | /* shift ovec left most of the bits... */ | |
169 | memmove(ovec, ovec + num / 8, 8 + (num % 8 ? 1 : 0)); | |
170 | /* now the remaining bits */ | |
171 | if (num % 8 != 0) | |
172 | for (i = 0; i < 8; ++i) { | |
173 | ovec[i] <<= num % 8; | |
174 | ovec[i] |= ovec[i + 1] >> (8 - num % 8); | |
175 | } | |
176 | iv = &ovec[0]; | |
177 | c2l(iv, v0); | |
178 | c2l(iv, v1); | |
179 | } | |
180 | d0 ^= ti[0]; | |
181 | d1 ^= ti[1]; | |
182 | l2cn(d0, d1, out, n); | |
183 | out += n; | |
184 | } | |
185 | } | |
186 | iv = &(*ivec)[0]; | |
187 | l2c(v0, iv); | |
188 | l2c(v1, iv); | |
189 | v0 = v1 = d0 = d1 = ti[0] = ti[1] = 0; | |
190 | } |