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