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