]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/modes/cfb128.c
2 * Copyright 2008-2016 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <openssl/crypto.h>
11 #include "modes_lcl.h"
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;
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
)
30 #if !defined(OPENSSL_SMALL_FOOTPRINT)
31 if (16 % sizeof(size_t) == 0) { /* always true actually */
34 *(out
++) = ivec
[n
] ^= *(in
++);
38 # if defined(STRICT_ALIGNMENT)
39 if (((size_t)in
| (size_t)out
| (size_t)ivec
) %
44 (*block
) (ivec
, ivec
, key
);
45 for (; n
< 16; n
+= sizeof(size_t)) {
46 *(size_t *)(out
+ n
) =
47 *(size_t *)(ivec
+ n
) ^= *(size_t *)(in
+ n
);
55 (*block
) (ivec
, ivec
, key
);
57 out
[n
] = ivec
[n
] ^= in
[n
];
65 /* the rest would be commonly eliminated by x86* compiler */
69 (*block
) (ivec
, ivec
, key
);
71 out
[l
] = ivec
[n
] ^= in
[l
];
77 #if !defined(OPENSSL_SMALL_FOOTPRINT)
78 if (16 % sizeof(size_t) == 0) { /* always true actually */
82 *(out
++) = ivec
[n
] ^ (c
= *(in
++));
87 # if defined(STRICT_ALIGNMENT)
88 if (((size_t)in
| (size_t)out
| (size_t)ivec
) %
93 (*block
) (ivec
, ivec
, key
);
94 for (; n
< 16; n
+= sizeof(size_t)) {
95 size_t t
= *(size_t *)(in
+ n
);
96 *(size_t *)(out
+ n
) = *(size_t *)(ivec
+ n
) ^ t
;
97 *(size_t *)(ivec
+ n
) = t
;
105 (*block
) (ivec
, ivec
, key
);
108 out
[n
] = ivec
[n
] ^ (c
= in
[n
]);
117 /* the rest would be commonly eliminated by x86* compiler */
122 (*block
) (ivec
, ivec
, key
);
124 out
[l
] = ivec
[n
] ^ (c
= in
[l
]);
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
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
,
143 unsigned char ovec
[16 * 2 + 1]; /* +1 because we dereference (but don't
144 * use) one byte off the end */
146 if (nbits
<= 0 || nbits
> 128)
149 /* fill in the first half of the new IV with the current IV */
150 memcpy(ovec
, ivec
, 16);
151 /* construct the new IV */
152 (*block
) (ivec
, ivec
, key
);
153 num
= (nbits
+ 7) / 8;
154 if (enc
) /* encrypt the input */
155 for (n
= 0; n
< num
; ++n
)
156 out
[n
] = (ovec
[16 + n
] = in
[n
] ^ ivec
[n
]);
157 else /* decrypt the input */
158 for (n
= 0; n
< num
; ++n
)
159 out
[n
] = (ovec
[16 + n
] = in
[n
]) ^ ivec
[n
];
160 /* shift ovec left... */
164 memcpy(ivec
, ovec
+ num
, 16);
166 for (n
= 0; n
< 16; ++n
)
167 ivec
[n
] = ovec
[n
+ num
] << rem
| ovec
[n
+ num
+ 1] >> (8 - rem
);
169 /* it is not necessary to cleanse ovec, since the IV is not secret */
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
)
179 unsigned char c
[1], d
[1];
181 for (n
= 0; n
< bits
; ++n
) {
182 c
[0] = (in
[n
/ 8] & (1 << (7 - n
% 8))) ? 0x80 : 0;
183 cfbr_encrypt_block(c
, d
, 1, key
, ivec
, enc
, block
);
184 out
[n
/ 8] = (out
[n
/ 8] & ~(1 << (unsigned int)(7 - n
% 8))) |
185 ((d
[0] & 0x80) >> (unsigned int)(n
% 8));
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
)
196 for (n
= 0; n
< length
; ++n
)
197 cfbr_encrypt_block(&in
[n
], &out
[n
], 8, key
, ivec
, enc
, block
);