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