]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/modes/cfb128.c
f9c3c6053698f0a2813aca120b97aeb01ac5fed4
2 * Copyright 2008-2020 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include <openssl/crypto.h>
12 #include "crypto/modes.h"
14 #if defined(__GNUC__) && !defined(STRICT_ALIGNMENT)
15 typedef size_t size_t_aX
__attribute((__aligned__(1)));
17 typedef size_t size_t_aX
;
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;
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
)
34 /* There is no good way to signal an error return from here */
41 #if !defined(OPENSSL_SMALL_FOOTPRINT)
42 if (16 % sizeof(size_t) == 0) { /* always true actually */
45 *(out
++) = ivec
[n
] ^= *(in
++);
49 # if defined(STRICT_ALIGNMENT)
50 if (((size_t)in
| (size_t)out
| (size_t)ivec
) %
55 (*block
) (ivec
, ivec
, key
);
56 for (; n
< 16; n
+= sizeof(size_t)) {
57 *(size_t_aX
*)(out
+ n
) =
58 *(size_t_aX
*)(ivec
+ n
)
59 ^= *(size_t_aX
*)(in
+ n
);
67 (*block
) (ivec
, ivec
, key
);
69 out
[n
] = ivec
[n
] ^= in
[n
];
77 /* the rest would be commonly eliminated by x86* compiler */
81 (*block
) (ivec
, ivec
, key
);
83 out
[l
] = ivec
[n
] ^= in
[l
];
89 #if !defined(OPENSSL_SMALL_FOOTPRINT)
90 if (16 % sizeof(size_t) == 0) { /* always true actually */
94 *(out
++) = ivec
[n
] ^ (c
= *(in
++));
99 # if defined(STRICT_ALIGNMENT)
100 if (((size_t)in
| (size_t)out
| (size_t)ivec
) %
105 (*block
) (ivec
, ivec
, key
);
106 for (; n
< 16; n
+= sizeof(size_t)) {
107 size_t t
= *(size_t_aX
*)(in
+ n
);
108 *(size_t_aX
*)(out
+ n
)
109 = *(size_t_aX
*)(ivec
+ n
) ^ t
;
110 *(size_t_aX
*)(ivec
+ n
) = t
;
118 (*block
) (ivec
, ivec
, key
);
121 out
[n
] = ivec
[n
] ^ (c
= in
[n
]);
130 /* the rest would be commonly eliminated by x86* compiler */
135 (*block
) (ivec
, ivec
, key
);
137 out
[l
] = ivec
[n
] ^ (c
= in
[l
]);
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
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
,
156 unsigned char ovec
[16 * 2 + 1]; /* +1 because we dereference (but don't
157 * use) one byte off the end */
159 if (nbits
<= 0 || nbits
> 128)
162 /* fill in the first half of the new IV with the current IV */
163 memcpy(ovec
, ivec
, 16);
164 /* construct the new IV */
165 (*block
) (ivec
, ivec
, key
);
166 num
= (nbits
+ 7) / 8;
167 if (enc
) /* encrypt the input */
168 for (n
= 0; n
< num
; ++n
)
169 out
[n
] = (ovec
[16 + n
] = in
[n
] ^ ivec
[n
]);
170 else /* decrypt the input */
171 for (n
= 0; n
< num
; ++n
)
172 out
[n
] = (ovec
[16 + n
] = in
[n
]) ^ ivec
[n
];
173 /* shift ovec left... */
177 memcpy(ivec
, ovec
+ num
, 16);
179 for (n
= 0; n
< 16; ++n
)
180 ivec
[n
] = ovec
[n
+ num
] << rem
| ovec
[n
+ num
+ 1] >> (8 - rem
);
182 /* it is not necessary to cleanse ovec, since the IV is not secret */
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
)
192 unsigned char c
[1], d
[1];
194 for (n
= 0; n
< bits
; ++n
) {
195 c
[0] = (in
[n
/ 8] & (1 << (7 - n
% 8))) ? 0x80 : 0;
196 cfbr_encrypt_block(c
, d
, 1, key
, ivec
, enc
, block
);
197 out
[n
/ 8] = (out
[n
/ 8] & ~(1 << (unsigned int)(7 - n
% 8))) |
198 ((d
[0] & 0x80) >> (unsigned int)(n
% 8));
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
)
209 for (n
= 0; n
< length
; ++n
)
210 cfbr_encrypt_block(&in
[n
], &out
[n
], 8, key
, ivec
, enc
, block
);