7017fd96ff47a5d762c759064d7c1d98a0daf8a1
[openssl.git] / crypto / des / cfb64ede.c
1 /*
2  * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 #include "des_locl.h"
11 #include "e_os.h"
12
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;
17  */
18
19 void DES_ede3_cfb64_encrypt(const unsigned char *in, unsigned char *out,
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;
29
30     iv = &(*ivec)[0];
31     if (enc) {
32         while (l--) {
33             if (n == 0) {
34                 c2l(iv, v0);
35                 c2l(iv, v1);
36
37                 ti[0] = v0;
38                 ti[1] = v1;
39                 DES_encrypt3(ti, ks1, ks2, ks3);
40                 v0 = ti[0];
41                 v1 = ti[1];
42
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);
58
59                 ti[0] = v0;
60                 ti[1] = v1;
61                 DES_encrypt3(ti, ks1, ks2, ks3);
62                 v0 = ti[0];
63                 v1 = ti[1];
64
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 }
80
81 /*
82  * This is compatible with the single key CFB-r for DES, even thought that's
83  * not what EVP needs.
84  */
85
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];
97
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 }