/src/openssl/crypto/modes/cfb128.c
Line | Count | Source |
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) % sizeof(size_t) != 0) |
51 | | break; |
52 | | #endif |
53 | 0 | while (len >= 16) { |
54 | 0 | (*block)(ivec, ivec, key); |
55 | 0 | for (; n < 16; n += sizeof(size_t)) { |
56 | 0 | *(size_t_aX *)(out + n) = *(size_t_aX *)(ivec + n) |
57 | 0 | ^= *(size_t_aX *)(in + n); |
58 | 0 | } |
59 | 0 | len -= 16; |
60 | 0 | out += 16; |
61 | 0 | in += 16; |
62 | 0 | n = 0; |
63 | 0 | } |
64 | 0 | if (len) { |
65 | 0 | (*block)(ivec, ivec, key); |
66 | 0 | while (len--) { |
67 | 0 | out[n] = ivec[n] ^= in[n]; |
68 | 0 | ++n; |
69 | 0 | } |
70 | 0 | } |
71 | 0 | *num = n; |
72 | 0 | return; |
73 | 0 | } while (0); |
74 | 0 | } |
75 | | /* the rest would be commonly eliminated by x86* compiler */ |
76 | 0 | #endif |
77 | 0 | while (l < len) { |
78 | 0 | if (n == 0) { |
79 | 0 | (*block)(ivec, ivec, key); |
80 | 0 | } |
81 | 0 | out[l] = ivec[n] ^= in[l]; |
82 | 0 | ++l; |
83 | 0 | n = (n + 1) % 16; |
84 | 0 | } |
85 | 0 | *num = n; |
86 | 0 | } else { |
87 | 0 | #if !defined(OPENSSL_SMALL_FOOTPRINT) |
88 | 0 | if (16 % sizeof(size_t) == 0) { /* always true actually */ |
89 | 0 | do { |
90 | 0 | while (n && len) { |
91 | 0 | unsigned char c; |
92 | 0 | *(out++) = ivec[n] ^ (c = *(in++)); |
93 | 0 | ivec[n] = c; |
94 | 0 | --len; |
95 | 0 | n = (n + 1) % 16; |
96 | 0 | } |
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 | 0 | while (len >= 16) { |
102 | 0 | (*block)(ivec, ivec, key); |
103 | 0 | for (; n < 16; n += sizeof(size_t)) { |
104 | 0 | size_t t = *(size_t_aX *)(in + n); |
105 | 0 | *(size_t_aX *)(out + n) |
106 | 0 | = *(size_t_aX *)(ivec + n) ^ t; |
107 | 0 | *(size_t_aX *)(ivec + n) = t; |
108 | 0 | } |
109 | 0 | len -= 16; |
110 | 0 | out += 16; |
111 | 0 | in += 16; |
112 | 0 | n = 0; |
113 | 0 | } |
114 | 0 | if (len) { |
115 | 0 | (*block)(ivec, ivec, key); |
116 | 0 | while (len--) { |
117 | 0 | unsigned char c; |
118 | 0 | out[n] = ivec[n] ^ (c = in[n]); |
119 | 0 | ivec[n] = c; |
120 | 0 | ++n; |
121 | 0 | } |
122 | 0 | } |
123 | 0 | *num = n; |
124 | 0 | return; |
125 | 0 | } while (0); |
126 | 0 | } |
127 | | /* the rest would be commonly eliminated by x86* compiler */ |
128 | 0 | #endif |
129 | 0 | while (l < len) { |
130 | 0 | unsigned char c; |
131 | 0 | if (n == 0) { |
132 | 0 | (*block)(ivec, ivec, key); |
133 | 0 | } |
134 | 0 | out[l] = ivec[n] ^ (c = in[l]); |
135 | 0 | ivec[n] = c; |
136 | 0 | ++l; |
137 | 0 | n = (n + 1) % 16; |
138 | 0 | } |
139 | 0 | *num = n; |
140 | 0 | } |
141 | 0 | } |
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 | | */ |
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 | 0 | { |
152 | 0 | int n, rem, num; |
153 | 0 | unsigned char ovec[16 * 2 + 1]; /* +1 because we dereference (but don't |
154 | | * use) one byte off the end */ |
155 | |
|
156 | 0 | if (nbits <= 0 || nbits > 128) |
157 | 0 | return; |
158 | | |
159 | | /* fill in the first half of the new IV with the current IV */ |
160 | 0 | memcpy(ovec, ivec, 16); |
161 | | /* construct the new IV */ |
162 | 0 | (*block)(ivec, ivec, key); |
163 | 0 | num = (nbits + 7) / 8; |
164 | 0 | if (enc) /* encrypt the input */ |
165 | 0 | for (n = 0; n < num; ++n) |
166 | 0 | out[n] = (ovec[16 + n] = in[n] ^ ivec[n]); |
167 | 0 | else /* decrypt the input */ |
168 | 0 | for (n = 0; n < num; ++n) |
169 | 0 | out[n] = (ovec[16 + n] = in[n]) ^ ivec[n]; |
170 | | /* shift ovec left... */ |
171 | 0 | rem = nbits % 8; |
172 | 0 | num = nbits / 8; |
173 | 0 | if (rem == 0) |
174 | 0 | memcpy(ivec, ovec + num, 16); |
175 | 0 | else |
176 | 0 | for (n = 0; n < 16; ++n) |
177 | 0 | 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 | 0 | } |
181 | | |
182 | | /* N.B. This expects the input to be packed, MS bit first */ |
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 | 0 | { |
188 | 0 | size_t n; |
189 | 0 | unsigned char c[1], d[1]; |
190 | |
|
191 | 0 | for (n = 0; n < bits; ++n) { |
192 | 0 | c[0] = (in[n / 8] & (1 << (7 - n % 8))) ? 0x80 : 0; |
193 | 0 | cfbr_encrypt_block(c, d, 1, key, ivec, enc, block); |
194 | 0 | out[n / 8] = (out[n / 8] & ~(1 << (unsigned int)(7 - n % 8))) | ((d[0] & 0x80) >> (unsigned int)(n % 8)); |
195 | 0 | } |
196 | 0 | } |
197 | | |
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 | 0 | { |
203 | 0 | size_t n; |
204 | |
|
205 | 0 | for (n = 0; n < length; ++n) |
206 | 0 | cfbr_encrypt_block(&in[n], &out[n], 8, key, ivec, enc, block); |
207 | 0 | } |