10Sstevel@tonic-gate /* crypto/aes/aes_cfb.c -*- mode:C; c-file-style: "eay" -*- */
20Sstevel@tonic-gate /* ====================================================================
30Sstevel@tonic-gate * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
40Sstevel@tonic-gate *
50Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
60Sstevel@tonic-gate * modification, are permitted provided that the following conditions
70Sstevel@tonic-gate * are met:
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
100Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
110Sstevel@tonic-gate *
120Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
130Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in
140Sstevel@tonic-gate * the documentation and/or other materials provided with the
150Sstevel@tonic-gate * distribution.
160Sstevel@tonic-gate *
170Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this
180Sstevel@tonic-gate * software must display the following acknowledgment:
190Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
200Sstevel@tonic-gate * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
210Sstevel@tonic-gate *
220Sstevel@tonic-gate * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
230Sstevel@tonic-gate * endorse or promote products derived from this software without
240Sstevel@tonic-gate * prior written permission. For written permission, please contact
250Sstevel@tonic-gate * openssl-core@openssl.org.
260Sstevel@tonic-gate *
270Sstevel@tonic-gate * 5. Products derived from this software may not be called "OpenSSL"
280Sstevel@tonic-gate * nor may "OpenSSL" appear in their names without prior written
290Sstevel@tonic-gate * permission of the OpenSSL Project.
300Sstevel@tonic-gate *
310Sstevel@tonic-gate * 6. Redistributions of any form whatsoever must retain the following
320Sstevel@tonic-gate * acknowledgment:
330Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
340Sstevel@tonic-gate * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
350Sstevel@tonic-gate *
360Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
370Sstevel@tonic-gate * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
380Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
390Sstevel@tonic-gate * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
400Sstevel@tonic-gate * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
410Sstevel@tonic-gate * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
420Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
430Sstevel@tonic-gate * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
440Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
450Sstevel@tonic-gate * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
460Sstevel@tonic-gate * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
470Sstevel@tonic-gate * OF THE POSSIBILITY OF SUCH DAMAGE.
480Sstevel@tonic-gate * ====================================================================
490Sstevel@tonic-gate *
500Sstevel@tonic-gate */
510Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
520Sstevel@tonic-gate * All rights reserved.
530Sstevel@tonic-gate *
540Sstevel@tonic-gate * This package is an SSL implementation written
550Sstevel@tonic-gate * by Eric Young (eay@cryptsoft.com).
560Sstevel@tonic-gate * The implementation was written so as to conform with Netscapes SSL.
570Sstevel@tonic-gate *
580Sstevel@tonic-gate * This library is free for commercial and non-commercial use as long as
590Sstevel@tonic-gate * the following conditions are aheared to. The following conditions
600Sstevel@tonic-gate * apply to all code found in this distribution, be it the RC4, RSA,
610Sstevel@tonic-gate * lhash, DES, etc., code; not just the SSL code. The SSL documentation
620Sstevel@tonic-gate * included with this distribution is covered by the same copyright terms
630Sstevel@tonic-gate * except that the holder is Tim Hudson (tjh@cryptsoft.com).
640Sstevel@tonic-gate *
650Sstevel@tonic-gate * Copyright remains Eric Young's, and as such any Copyright notices in
660Sstevel@tonic-gate * the code are not to be removed.
670Sstevel@tonic-gate * If this package is used in a product, Eric Young should be given attribution
680Sstevel@tonic-gate * as the author of the parts of the library used.
690Sstevel@tonic-gate * This can be in the form of a textual message at program startup or
700Sstevel@tonic-gate * in documentation (online or textual) provided with the package.
710Sstevel@tonic-gate *
720Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
730Sstevel@tonic-gate * modification, are permitted provided that the following conditions
740Sstevel@tonic-gate * are met:
750Sstevel@tonic-gate * 1. Redistributions of source code must retain the copyright
760Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
770Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
780Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
790Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
800Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
810Sstevel@tonic-gate * must display the following acknowledgement:
820Sstevel@tonic-gate * "This product includes cryptographic software written by
830Sstevel@tonic-gate * Eric Young (eay@cryptsoft.com)"
840Sstevel@tonic-gate * The word 'cryptographic' can be left out if the rouines from the library
850Sstevel@tonic-gate * being used are not cryptographic related :-).
860Sstevel@tonic-gate * 4. If you include any Windows specific code (or a derivative thereof) from
870Sstevel@tonic-gate * the apps directory (application code) you must include an acknowledgement:
880Sstevel@tonic-gate * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
890Sstevel@tonic-gate *
900Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
910Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
920Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
930Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
940Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
950Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
960Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
970Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
980Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
990Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1000Sstevel@tonic-gate * SUCH DAMAGE.
1010Sstevel@tonic-gate *
1020Sstevel@tonic-gate * The licence and distribution terms for any publically available version or
1030Sstevel@tonic-gate * derivative of this code cannot be changed. i.e. this code cannot simply be
1040Sstevel@tonic-gate * copied and put under another distribution licence
1050Sstevel@tonic-gate * [including the GNU Public Licence.]
1060Sstevel@tonic-gate */
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate #ifndef AES_DEBUG
1090Sstevel@tonic-gate # ifndef NDEBUG
1100Sstevel@tonic-gate # define NDEBUG
1110Sstevel@tonic-gate # endif
1120Sstevel@tonic-gate #endif
1130Sstevel@tonic-gate #include <assert.h>
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate #include <openssl/aes.h>
1160Sstevel@tonic-gate #include "aes_locl.h"
117*2139Sjp161948 #include "e_os.h"
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate /* The input and output encrypted as though 128bit cfb mode is being
1200Sstevel@tonic-gate * used. The extra state information to record how much of the
1210Sstevel@tonic-gate * 128bit block we have used is contained in *num;
1220Sstevel@tonic-gate */
1230Sstevel@tonic-gate
AES_cfb128_encrypt(const unsigned char * in,unsigned char * out,const unsigned long length,const AES_KEY * key,unsigned char * ivec,int * num,const int enc)1240Sstevel@tonic-gate void AES_cfb128_encrypt(const unsigned char *in, unsigned char *out,
1250Sstevel@tonic-gate const unsigned long length, const AES_KEY *key,
1260Sstevel@tonic-gate unsigned char *ivec, int *num, const int enc) {
1270Sstevel@tonic-gate
1280Sstevel@tonic-gate unsigned int n;
1290Sstevel@tonic-gate unsigned long l = length;
1300Sstevel@tonic-gate unsigned char c;
1310Sstevel@tonic-gate
1320Sstevel@tonic-gate assert(in && out && key && ivec && num);
1330Sstevel@tonic-gate
1340Sstevel@tonic-gate n = *num;
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate if (enc) {
1370Sstevel@tonic-gate while (l--) {
1380Sstevel@tonic-gate if (n == 0) {
1390Sstevel@tonic-gate AES_encrypt(ivec, ivec, key);
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate ivec[n] = *(out++) = *(in++) ^ ivec[n];
1420Sstevel@tonic-gate n = (n+1) % AES_BLOCK_SIZE;
1430Sstevel@tonic-gate }
1440Sstevel@tonic-gate } else {
1450Sstevel@tonic-gate while (l--) {
1460Sstevel@tonic-gate if (n == 0) {
1470Sstevel@tonic-gate AES_encrypt(ivec, ivec, key);
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate c = *(in);
1500Sstevel@tonic-gate *(out++) = *(in++) ^ ivec[n];
1510Sstevel@tonic-gate ivec[n] = c;
1520Sstevel@tonic-gate n = (n+1) % AES_BLOCK_SIZE;
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate }
1550Sstevel@tonic-gate
1560Sstevel@tonic-gate *num=n;
1570Sstevel@tonic-gate }
1580Sstevel@tonic-gate
159*2139Sjp161948 /* This expects a single block of size nbits for both in and out. Note that
160*2139Sjp161948 it corrupts any extra bits in the last byte of out */
AES_cfbr_encrypt_block(const unsigned char * in,unsigned char * out,const int nbits,const AES_KEY * key,unsigned char * ivec,const int enc)161*2139Sjp161948 void AES_cfbr_encrypt_block(const unsigned char *in,unsigned char *out,
162*2139Sjp161948 const int nbits,const AES_KEY *key,
163*2139Sjp161948 unsigned char *ivec,const int enc)
164*2139Sjp161948 {
165*2139Sjp161948 int n,rem,num;
166*2139Sjp161948 unsigned char ovec[AES_BLOCK_SIZE*2];
167*2139Sjp161948
168*2139Sjp161948 if (nbits<=0 || nbits>128) return;
169*2139Sjp161948
170*2139Sjp161948 /* fill in the first half of the new IV with the current IV */
171*2139Sjp161948 memcpy(ovec,ivec,AES_BLOCK_SIZE);
172*2139Sjp161948 /* construct the new IV */
173*2139Sjp161948 AES_encrypt(ivec,ivec,key);
174*2139Sjp161948 num = (nbits+7)/8;
175*2139Sjp161948 if (enc) /* encrypt the input */
176*2139Sjp161948 for(n=0 ; n < num ; ++n)
177*2139Sjp161948 out[n] = (ovec[AES_BLOCK_SIZE+n] = in[n] ^ ivec[n]);
178*2139Sjp161948 else /* decrypt the input */
179*2139Sjp161948 for(n=0 ; n < num ; ++n)
180*2139Sjp161948 out[n] = (ovec[AES_BLOCK_SIZE+n] = in[n]) ^ ivec[n];
181*2139Sjp161948 /* shift ovec left... */
182*2139Sjp161948 rem = nbits%8;
183*2139Sjp161948 num = nbits/8;
184*2139Sjp161948 if(rem==0)
185*2139Sjp161948 memcpy(ivec,ovec+num,AES_BLOCK_SIZE);
186*2139Sjp161948 else
187*2139Sjp161948 for(n=0 ; n < AES_BLOCK_SIZE ; ++n)
188*2139Sjp161948 ivec[n] = ovec[n+num]<<rem | ovec[n+num+1]>>(8-rem);
189*2139Sjp161948
190*2139Sjp161948 /* it is not necessary to cleanse ovec, since the IV is not secret */
191*2139Sjp161948 }
192*2139Sjp161948
193*2139Sjp161948 /* N.B. This expects the input to be packed, MS bit first */
AES_cfb1_encrypt(const unsigned char * in,unsigned char * out,const unsigned long length,const AES_KEY * key,unsigned char * ivec,int * num,const int enc)194*2139Sjp161948 void AES_cfb1_encrypt(const unsigned char *in, unsigned char *out,
195*2139Sjp161948 const unsigned long length, const AES_KEY *key,
196*2139Sjp161948 unsigned char *ivec, int *num, const int enc)
197*2139Sjp161948 {
198*2139Sjp161948 unsigned int n;
199*2139Sjp161948 unsigned char c[1],d[1];
200*2139Sjp161948
201*2139Sjp161948 assert(in && out && key && ivec && num);
202*2139Sjp161948 assert(*num == 0);
203*2139Sjp161948
204*2139Sjp161948 memset(out,0,(length+7)/8);
205*2139Sjp161948 for(n=0 ; n < length ; ++n)
206*2139Sjp161948 {
207*2139Sjp161948 c[0]=(in[n/8]&(1 << (7-n%8))) ? 0x80 : 0;
208*2139Sjp161948 AES_cfbr_encrypt_block(c,d,1,key,ivec,enc);
209*2139Sjp161948 out[n/8]=(out[n/8]&~(1 << (7-n%8)))|((d[0]&0x80) >> (n%8));
210*2139Sjp161948 }
211*2139Sjp161948 }
212*2139Sjp161948
AES_cfb8_encrypt(const unsigned char * in,unsigned char * out,const unsigned long length,const AES_KEY * key,unsigned char * ivec,int * num,const int enc)213*2139Sjp161948 void AES_cfb8_encrypt(const unsigned char *in, unsigned char *out,
214*2139Sjp161948 const unsigned long length, const AES_KEY *key,
215*2139Sjp161948 unsigned char *ivec, int *num, const int enc)
216*2139Sjp161948 {
217*2139Sjp161948 unsigned int n;
218*2139Sjp161948
219*2139Sjp161948 assert(in && out && key && ivec && num);
220*2139Sjp161948 assert(*num == 0);
221*2139Sjp161948
222*2139Sjp161948 for(n=0 ; n < length ; ++n)
223*2139Sjp161948 AES_cfbr_encrypt_block(&in[n],&out[n],8,key,ivec,enc);
224*2139Sjp161948 }
225*2139Sjp161948
226