xref: /src/crypto/openssl/crypto/modes/cfb128.c (revision f25b8c9fb4f58cf61adb47d7570abe7caa6d385d)
1 /*
2  * Copyright 2008-2021 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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 <string.h>
11 #include <openssl/crypto.h>
12 #include "crypto/modes.h"
13 
14 #if defined(__GNUC__) && !defined(STRICT_ALIGNMENT)
15 typedef size_t size_t_aX __attribute((__aligned__(1)));
16 #else
17 typedef size_t size_t_aX;
18 #endif
19 
20 /*
21  * The input and output encrypted as though 128bit cfb mode is being used.
22  * The extra state information to record how much of the 128bit block we have
23  * used is contained in *num;
24  */
CRYPTO_cfb128_encrypt(const unsigned char * in,unsigned char * out,size_t len,const void * key,unsigned char ivec[16],int * num,int enc,block128_f block)25 void CRYPTO_cfb128_encrypt(const unsigned char *in, unsigned char *out,
26     size_t len, const void *key,
27     unsigned char ivec[16], int *num,
28     int enc, block128_f block)
29 {
30     unsigned int n;
31     size_t l = 0;
32 
33     if (*num < 0) {
34         /* There is no good way to signal an error return from here */
35         *num = -1;
36         return;
37     }
38     n = *num;
39 
40     if (enc) {
41 #if !defined(OPENSSL_SMALL_FOOTPRINT)
42         if (16 % sizeof(size_t) == 0) { /* always true actually */
43             do {
44                 while (n && len) {
45                     *(out++) = ivec[n] ^= *(in++);
46                     --len;
47                     n = (n + 1) % 16;
48                 }
49 #if defined(STRICT_ALIGNMENT)
50                 if (((size_t)in | (size_t)out | (size_t)ivec) % sizeof(size_t) != 0)
51                     break;
52 #endif
53                 while (len >= 16) {
54                     (*block)(ivec, ivec, key);
55                     for (; n < 16; n += sizeof(size_t)) {
56                         *(size_t_aX *)(out + n) = *(size_t_aX *)(ivec + n)
57                             ^= *(size_t_aX *)(in + n);
58                     }
59                     len -= 16;
60                     out += 16;
61                     in += 16;
62                     n = 0;
63                 }
64                 if (len) {
65                     (*block)(ivec, ivec, key);
66                     while (len--) {
67                         out[n] = ivec[n] ^= in[n];
68                         ++n;
69                     }
70                 }
71                 *num = n;
72                 return;
73             } while (0);
74         }
75         /* the rest would be commonly eliminated by x86* compiler */
76 #endif
77         while (l < len) {
78             if (n == 0) {
79                 (*block)(ivec, ivec, key);
80             }
81             out[l] = ivec[n] ^= in[l];
82             ++l;
83             n = (n + 1) % 16;
84         }
85         *num = n;
86     } else {
87 #if !defined(OPENSSL_SMALL_FOOTPRINT)
88         if (16 % sizeof(size_t) == 0) { /* always true actually */
89             do {
90                 while (n && len) {
91                     unsigned char c;
92                     *(out++) = ivec[n] ^ (c = *(in++));
93                     ivec[n] = c;
94                     --len;
95                     n = (n + 1) % 16;
96                 }
97 #if defined(STRICT_ALIGNMENT)
98                 if (((size_t)in | (size_t)out | (size_t)ivec) % sizeof(size_t) != 0)
99                     break;
100 #endif
101                 while (len >= 16) {
102                     (*block)(ivec, ivec, key);
103                     for (; n < 16; n += sizeof(size_t)) {
104                         size_t t = *(size_t_aX *)(in + n);
105                         *(size_t_aX *)(out + n)
106                             = *(size_t_aX *)(ivec + n) ^ t;
107                         *(size_t_aX *)(ivec + n) = t;
108                     }
109                     len -= 16;
110                     out += 16;
111                     in += 16;
112                     n = 0;
113                 }
114                 if (len) {
115                     (*block)(ivec, ivec, key);
116                     while (len--) {
117                         unsigned char c;
118                         out[n] = ivec[n] ^ (c = in[n]);
119                         ivec[n] = c;
120                         ++n;
121                     }
122                 }
123                 *num = n;
124                 return;
125             } while (0);
126         }
127         /* the rest would be commonly eliminated by x86* compiler */
128 #endif
129         while (l < len) {
130             unsigned char c;
131             if (n == 0) {
132                 (*block)(ivec, ivec, key);
133             }
134             out[l] = ivec[n] ^ (c = in[l]);
135             ivec[n] = c;
136             ++l;
137             n = (n + 1) % 16;
138         }
139         *num = n;
140     }
141 }
142 
143 /*
144  * This expects a single block of size nbits for both in and out. Note that
145  * it corrupts any extra bits in the last byte of out
146  */
cfbr_encrypt_block(const unsigned char * in,unsigned char * out,int nbits,const void * key,unsigned char ivec[16],int enc,block128_f block)147 static void cfbr_encrypt_block(const unsigned char *in, unsigned char *out,
148     int nbits, const void *key,
149     unsigned char ivec[16], int enc,
150     block128_f block)
151 {
152     int n, rem, num;
153     unsigned char ovec[16 * 2 + 1]; /* +1 because we dereference (but don't
154                                      * use) one byte off the end */
155 
156     if (nbits <= 0 || nbits > 128)
157         return;
158 
159     /* fill in the first half of the new IV with the current IV */
160     memcpy(ovec, ivec, 16);
161     /* construct the new IV */
162     (*block)(ivec, ivec, key);
163     num = (nbits + 7) / 8;
164     if (enc) /* encrypt the input */
165         for (n = 0; n < num; ++n)
166             out[n] = (ovec[16 + n] = in[n] ^ ivec[n]);
167     else /* decrypt the input */
168         for (n = 0; n < num; ++n)
169             out[n] = (ovec[16 + n] = in[n]) ^ ivec[n];
170     /* shift ovec left... */
171     rem = nbits % 8;
172     num = nbits / 8;
173     if (rem == 0)
174         memcpy(ivec, ovec + num, 16);
175     else
176         for (n = 0; n < 16; ++n)
177             ivec[n] = ovec[n + num] << rem | ovec[n + num + 1] >> (8 - rem);
178 
179     /* it is not necessary to cleanse ovec, since the IV is not secret */
180 }
181 
182 /* N.B. This expects the input to be packed, MS bit first */
CRYPTO_cfb128_1_encrypt(const unsigned char * in,unsigned char * out,size_t bits,const void * key,unsigned char ivec[16],int * num,int enc,block128_f block)183 void CRYPTO_cfb128_1_encrypt(const unsigned char *in, unsigned char *out,
184     size_t bits, const void *key,
185     unsigned char ivec[16], int *num,
186     int enc, block128_f block)
187 {
188     size_t n;
189     unsigned char c[1], d[1];
190 
191     for (n = 0; n < bits; ++n) {
192         c[0] = (in[n / 8] & (1 << (7 - n % 8))) ? 0x80 : 0;
193         cfbr_encrypt_block(c, d, 1, key, ivec, enc, block);
194         out[n / 8] = (out[n / 8] & ~(1 << (unsigned int)(7 - n % 8))) | ((d[0] & 0x80) >> (unsigned int)(n % 8));
195     }
196 }
197 
CRYPTO_cfb128_8_encrypt(const unsigned char * in,unsigned char * out,size_t length,const void * key,unsigned char ivec[16],int * num,int enc,block128_f block)198 void CRYPTO_cfb128_8_encrypt(const unsigned char *in, unsigned char *out,
199     size_t length, const void *key,
200     unsigned char ivec[16], int *num,
201     int enc, block128_f block)
202 {
203     size_t n;
204 
205     for (n = 0; n < length; ++n)
206         cfbr_encrypt_block(&in[n], &out[n], 8, key, ivec, enc, block);
207 }
208