Fix link for ASN1_generate_nconf
[openssl.git] / crypto / aes / aes_cfb.c
1 /* crypto/aes/aes_cfb.c -*- mode:C; c-file-style: "eay" -*- */
2 /* ====================================================================
3  * Copyright (c) 2002-2006 The OpenSSL Project.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer. 
11  *
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
15  *    distribution.
16  *
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/)"
21  *
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.
26  *
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.
30  *
31  * 6. Redistributions of any form whatsoever must retain the following
32  *    acknowledgment:
33  *    "This product includes software developed by the OpenSSL Project
34  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35  *
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  * ====================================================================
49  *
50  */
51
52 #ifndef AES_DEBUG
53 # ifndef NDEBUG
54 #  define NDEBUG
55 # endif
56 #endif
57 #include <assert.h>
58
59 #include <openssl/aes.h>
60 #include "aes_locl.h"
61 #include "e_os.h"
62
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
68 #endif
69
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;
73  */
74
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) {
78
79     unsigned int n;
80     unsigned long l = 0;
81
82     assert(in && out && key && ivec && num);
83
84     n = *num;
85
86 #if !defined(OPENSSL_SMALL_FOOTPRINT)
87     if (AES_BLOCK_SIZE%sizeof(size_t) == 0) {   /* always true actually */
88         if (enc) {
89                 if (n) {
90                         while (length) {
91                                 *(out++) = ivec[n] ^= *(in++);
92                                 length--;
93                                 if(!(n = (n + 1) % AES_BLOCK_SIZE))
94                                         break;
95                         }
96                 }
97 #if defined(STRICT_ALIGNMENT)
98                 if (((size_t)in|(size_t)out|(size_t)ivec)%sizeof(size_t) != 0)
99                         goto enc_unaligned;
100 #endif
101                 while ((l + AES_BLOCK_SIZE) <= length) {
102                         unsigned int i;
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);
107                         }
108                         l += AES_BLOCK_SIZE;
109                 }
110
111                 if (l < length) {
112                         AES_encrypt(ivec, ivec, key);
113                         do {    out[l] = ivec[n] ^= in[l];
114                                 l++; n++;
115                         } while (l < length);
116                 }
117         } else {
118                 if (n) {
119                         while (length) {
120                                 unsigned char c;
121                                 *(out++) = ivec[n] ^ (c = *(in++)); ivec[n] = c;
122                                 length--;
123                                 if(!(n = (n + 1) % AES_BLOCK_SIZE))
124                                         break;
125                         }
126                 }
127 #if defined(STRICT_ALIGNMENT)
128                 if (((size_t)in|(size_t)out|(size_t)ivec)%sizeof(size_t) != 0)
129                         goto dec_unaligned;
130 #endif
131                 while (l + AES_BLOCK_SIZE <= length) {
132                         unsigned int i;
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;
138                         }
139                         l += AES_BLOCK_SIZE;
140                 }
141
142                 if (l < length) {
143                         AES_encrypt(ivec, ivec, key);
144                         do {    unsigned char c;
145                                 out[l] = ivec[n] ^ (c = in[l]); ivec[n] = c;
146                                 l++; n++;
147                         } while (l < length);
148                 }
149         }
150         *num = n;
151         return;
152     }
153 #endif
154
155     /* this code would be commonly eliminated by x86* compiler */
156         if (enc) {
157 #if defined(STRICT_ALIGNMENT) && !defined(OPENSSL_SMALL_FOOTPRINT)
158             enc_unaligned:
159 #endif
160                 while (l<length) {
161                         if (n == 0) {
162                                 AES_encrypt(ivec, ivec, key);
163                         }
164                         out[l] = ivec[n] ^= in[l];
165                         l++;
166                         n = (n+1) % AES_BLOCK_SIZE;
167                 }
168         } else {
169 #if defined(STRICT_ALIGNMENT) && !defined(OPENSSL_SMALL_FOOTPRINT)
170             dec_unaligned:
171 #endif
172                 while (l<length) {
173                         unsigned char c;
174                         if (n == 0) {
175                                 AES_encrypt(ivec, ivec, key);
176                         }
177                         out[l] = ivec[n] ^ (c = in[l]); ivec[n] = c;
178                         l++;
179                         n = (n+1) % AES_BLOCK_SIZE;
180                 }
181         }
182
183         *num=n;
184 }
185
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)
191     {
192     int n,rem,num;
193     unsigned char ovec[AES_BLOCK_SIZE*2];
194
195     if (nbits<=0 || nbits>128) return;
196
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);
201         num = (nbits+7)/8;
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... */
209         rem = nbits%8;
210         num = nbits/8;
211         if(rem==0)
212             memcpy(ivec,ovec+num,AES_BLOCK_SIZE);
213         else
214             for(n=0 ; n < AES_BLOCK_SIZE ; ++n)
215                 ivec[n] = ovec[n+num]<<rem | ovec[n+num+1]>>(8-rem);
216
217     /* it is not necessary to cleanse ovec, since the IV is not secret */
218     }
219
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)
224     {
225     unsigned int n;
226     unsigned char c[1],d[1];
227
228     assert(in && out && key && ivec && num);
229     assert(*num == 0);
230
231     memset(out,0,(length+7)/8);
232     for(n=0 ; n < length ; ++n)
233         {
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));
237         }
238     }
239
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)
243     {
244     unsigned int n;
245
246     assert(in && out && key && ivec && num);
247     assert(*num == 0);
248
249     for(n=0 ; n < length ; ++n)
250         AES_cfbr_encrypt_block(&in[n],&out[n],8,key,ivec,enc);
251     }
252