1 /* crypto/aes/aes_cfb.c -*- mode:C; c-file-style: "eay" -*- */
2 /* ====================================================================
3 * Copyright (c) 2002-2006 The OpenSSL Project. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * openssl-core@openssl.org.
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
31 * 6. Redistributions of any form whatsoever must retain the following
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
59 #include <openssl/aes.h>
63 #define STRICT_ALIGNMENT
64 #if defined(__i386) || defined(__i386__) || \
65 defined(__x86_64) || defined(__x86_64__) || \
66 defined(_M_IX86) || defined(_M_AMD64) || defined(_M_X64)
67 # undef STRICT_ALIGNMENT
70 /* The input and output encrypted as though 128bit cfb mode is being
71 * used. The extra state information to record how much of the
72 * 128bit block we have used is contained in *num;
75 void AES_cfb128_encrypt(const unsigned char *in, unsigned char *out,
76 unsigned long length, const AES_KEY *key,
77 unsigned char *ivec, int *num, const int enc) {
82 assert(in && out && key && ivec && num);
86 #if !defined(OPENSSL_SMALL_FOOTPRINT)
87 if (AES_BLOCK_SIZE%sizeof(size_t) == 0) { /* always true actually */
91 *(out++) = ivec[n] ^= *(in++);
93 if(!(n = (n + 1) % AES_BLOCK_SIZE))
97 #if defined(STRICT_ALIGNMENT)
98 if (((size_t)in|(size_t)out|(size_t)ivec)%sizeof(size_t) != 0)
101 while ((l + AES_BLOCK_SIZE) <= length) {
103 AES_encrypt(ivec, ivec, key);
104 for (i=0;i<AES_BLOCK_SIZE;i+=sizeof(size_t)) {
105 *(size_t*)(out+l+i) =
106 *(size_t*)(ivec+i) ^= *(size_t*)(in+l+i);
112 AES_encrypt(ivec, ivec, key);
113 do { out[l] = ivec[n] ^= in[l];
115 } while (l < length);
121 *(out++) = ivec[n] ^ (c = *(in++)); ivec[n] = c;
123 if(!(n = (n + 1) % AES_BLOCK_SIZE))
127 #if defined(STRICT_ALIGNMENT)
128 if (((size_t)in|(size_t)out|(size_t)ivec)%sizeof(size_t) != 0)
131 while (l + AES_BLOCK_SIZE <= length) {
133 AES_encrypt(ivec, ivec, key);
134 for (i=0;i<AES_BLOCK_SIZE;i+=sizeof(size_t)) {
135 size_t t = *(size_t*)(in+l+i);
136 *(size_t*)(out+l+i) = *(size_t*)(ivec+i) ^ t;
137 *(size_t*)(ivec+i) = t;
143 AES_encrypt(ivec, ivec, key);
144 do { unsigned char c;
145 out[l] = ivec[n] ^ (c = in[l]); ivec[n] = c;
147 } while (l < length);
155 /* this code would be commonly eliminated by x86* compiler */
157 #if defined(STRICT_ALIGNMENT) && !defined(OPENSSL_SMALL_FOOTPRINT)
162 AES_encrypt(ivec, ivec, key);
164 out[l] = ivec[n] ^= in[l];
166 n = (n+1) % AES_BLOCK_SIZE;
169 #if defined(STRICT_ALIGNMENT) && !defined(OPENSSL_SMALL_FOOTPRINT)
175 AES_encrypt(ivec, ivec, key);
177 out[l] = ivec[n] ^ (c = in[l]); ivec[n] = c;
179 n = (n+1) % AES_BLOCK_SIZE;
186 /* This expects a single block of size nbits for both in and out. Note that
187 it corrupts any extra bits in the last byte of out */
188 void AES_cfbr_encrypt_block(const unsigned char *in,unsigned char *out,
189 const int nbits,const AES_KEY *key,
190 unsigned char *ivec,const int enc)
193 unsigned char ovec[AES_BLOCK_SIZE*2];
195 if (nbits<=0 || nbits>128) return;
197 /* fill in the first half of the new IV with the current IV */
198 memcpy(ovec,ivec,AES_BLOCK_SIZE);
199 /* construct the new IV */
200 AES_encrypt(ivec,ivec,key);
202 if (enc) /* encrypt the input */
203 for(n=0 ; n < num ; ++n)
204 out[n] = (ovec[AES_BLOCK_SIZE+n] = in[n] ^ ivec[n]);
205 else /* decrypt the input */
206 for(n=0 ; n < num ; ++n)
207 out[n] = (ovec[AES_BLOCK_SIZE+n] = in[n]) ^ ivec[n];
208 /* shift ovec left... */
212 memcpy(ivec,ovec+num,AES_BLOCK_SIZE);
214 for(n=0 ; n < AES_BLOCK_SIZE ; ++n)
215 ivec[n] = ovec[n+num]<<rem | ovec[n+num+1]>>(8-rem);
217 /* it is not necessary to cleanse ovec, since the IV is not secret */
220 /* N.B. This expects the input to be packed, MS bit first */
221 void AES_cfb1_encrypt(const unsigned char *in, unsigned char *out,
222 const unsigned long length, const AES_KEY *key,
223 unsigned char *ivec, int *num, const int enc)
226 unsigned char c[1],d[1];
228 assert(in && out && key && ivec && num);
231 memset(out,0,(length+7)/8);
232 for(n=0 ; n < length ; ++n)
234 c[0]=(in[n/8]&(1 << (7-n%8))) ? 0x80 : 0;
235 AES_cfbr_encrypt_block(c,d,1,key,ivec,enc);
236 out[n/8]=(out[n/8]&~(1 << (7-n%8)))|((d[0]&0x80) >> (n%8));
240 void AES_cfb8_encrypt(const unsigned char *in, unsigned char *out,
241 const unsigned long length, const AES_KEY *key,
242 unsigned char *ivec, int *num, const int enc)
246 assert(in && out && key && ivec && num);
249 for(n=0 ; n < length ; ++n)
250 AES_cfbr_encrypt_block(&in[n],&out[n],8,key,ivec,enc);