1*95e1ffb1Schristos /* $NetBSD: bf_cbc.c,v 1.12 2005/12/11 12:20:48 christos Exp $ */
2e77423d9Sthorpej
36b3afc53Stls /* crypto/bf/bf_cbc.c */
46b3afc53Stls /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
56b3afc53Stls * All rights reserved.
66b3afc53Stls *
76b3afc53Stls * This package is an SSL implementation written
86b3afc53Stls * by Eric Young (eay@cryptsoft.com).
96b3afc53Stls * The implementation was written so as to conform with Netscapes SSL.
106b3afc53Stls *
116b3afc53Stls * This library is free for commercial and non-commercial use as long as
126b3afc53Stls * the following conditions are aheared to. The following conditions
136b3afc53Stls * apply to all code found in this distribution, be it the RC4, RSA,
146b3afc53Stls * lhash, DES, etc., code; not just the SSL code. The SSL documentation
156b3afc53Stls * included with this distribution is covered by the same copyright terms
166b3afc53Stls * except that the holder is Tim Hudson (tjh@cryptsoft.com).
176b3afc53Stls *
186b3afc53Stls * Copyright remains Eric Young's, and as such any Copyright notices in
196b3afc53Stls * the code are not to be removed.
206b3afc53Stls * If this package is used in a product, Eric Young should be given attribution
216b3afc53Stls * as the author of the parts of the library used.
226b3afc53Stls * This can be in the form of a textual message at program startup or
236b3afc53Stls * in documentation (online or textual) provided with the package.
246b3afc53Stls *
256b3afc53Stls * Redistribution and use in source and binary forms, with or without
266b3afc53Stls * modification, are permitted provided that the following conditions
276b3afc53Stls * are met:
286b3afc53Stls * 1. Redistributions of source code must retain the copyright
296b3afc53Stls * notice, this list of conditions and the following disclaimer.
306b3afc53Stls * 2. Redistributions in binary form must reproduce the above copyright
316b3afc53Stls * notice, this list of conditions and the following disclaimer in the
326b3afc53Stls * documentation and/or other materials provided with the distribution.
336b3afc53Stls * 3. All advertising materials mentioning features or use of this software
346b3afc53Stls * must display the following acknowledgement:
356b3afc53Stls * "This product includes cryptographic software written by
366b3afc53Stls * Eric Young (eay@cryptsoft.com)"
376b3afc53Stls * The word 'cryptographic' can be left out if the rouines from the library
386b3afc53Stls * being used are not cryptographic related :-).
396b3afc53Stls * 4. If you include any Windows specific code (or a derivative thereof) from
406b3afc53Stls * the apps directory (application code) you must include an acknowledgement:
416b3afc53Stls * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
426b3afc53Stls *
436b3afc53Stls * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
446b3afc53Stls * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
456b3afc53Stls * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
466b3afc53Stls * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
476b3afc53Stls * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
486b3afc53Stls * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
496b3afc53Stls * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
506b3afc53Stls * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
516b3afc53Stls * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
526b3afc53Stls * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
536b3afc53Stls * SUCH DAMAGE.
546b3afc53Stls *
556b3afc53Stls * The licence and distribution terms for any publically available version or
566b3afc53Stls * derivative of this code cannot be changed. i.e. this code cannot simply be
576b3afc53Stls * copied and put under another distribution licence
586b3afc53Stls * [including the GNU Public Licence.]
596b3afc53Stls */
606b3afc53Stls
614f2ad952Slukem #include <sys/cdefs.h>
62*95e1ffb1Schristos __KERNEL_RCSID(0, "$NetBSD: bf_cbc.c,v 1.12 2005/12/11 12:20:48 christos Exp $");
637fbd07ccSelric
647fbd07ccSelric #include <sys/types.h>
654f2ad952Slukem
6629db5af4Selric #include <crypto/blowfish/blowfish.h>
676b3afc53Stls #include "bf_locl.h"
686b3afc53Stls
BF_cbc_encrypt(const unsigned char * in,unsigned char * out,long length,const BF_KEY * schedule,unsigned char * ivec,int encrypt)696b3afc53Stls void BF_cbc_encrypt(const unsigned char *in, unsigned char *out, long length,
706b3afc53Stls const BF_KEY *schedule, unsigned char *ivec, int encrypt)
716b3afc53Stls {
726b3afc53Stls register BF_LONG tin0,tin1;
736b3afc53Stls register BF_LONG tout0,tout1,xor0,xor1;
746b3afc53Stls register long l=length;
756b3afc53Stls BF_LONG tin[2];
766b3afc53Stls
776b3afc53Stls if (encrypt)
786b3afc53Stls {
796b3afc53Stls n2l(ivec,tout0);
806b3afc53Stls n2l(ivec,tout1);
816b3afc53Stls ivec-=8;
826b3afc53Stls for (l-=8; l>=0; l-=8)
836b3afc53Stls {
846b3afc53Stls n2l(in,tin0);
856b3afc53Stls n2l(in,tin1);
866b3afc53Stls tin0^=tout0;
876b3afc53Stls tin1^=tout1;
886b3afc53Stls tin[0]=tin0;
896b3afc53Stls tin[1]=tin1;
900a86a6b0Schristos BF_encrypt(tin,(const BF_KEY *)schedule);
916b3afc53Stls tout0=tin[0];
926b3afc53Stls tout1=tin[1];
936b3afc53Stls l2n(tout0,out);
946b3afc53Stls l2n(tout1,out);
956b3afc53Stls }
966b3afc53Stls if (l != -8)
976b3afc53Stls {
986b3afc53Stls n2ln(in,tin0,tin1,l+8);
996b3afc53Stls tin0^=tout0;
1006b3afc53Stls tin1^=tout1;
1016b3afc53Stls tin[0]=tin0;
1026b3afc53Stls tin[1]=tin1;
1030a86a6b0Schristos BF_encrypt(tin,(const BF_KEY *)schedule);
1046b3afc53Stls tout0=tin[0];
1056b3afc53Stls tout1=tin[1];
1066b3afc53Stls l2n(tout0,out);
1076b3afc53Stls l2n(tout1,out);
1086b3afc53Stls }
1096b3afc53Stls l2n(tout0,ivec);
1106b3afc53Stls l2n(tout1,ivec);
1116b3afc53Stls }
1126b3afc53Stls else
1136b3afc53Stls {
1146b3afc53Stls n2l(ivec,xor0);
1156b3afc53Stls n2l(ivec,xor1);
1166b3afc53Stls ivec-=8;
1176b3afc53Stls for (l-=8; l>=0; l-=8)
1186b3afc53Stls {
1196b3afc53Stls n2l(in,tin0);
1206b3afc53Stls n2l(in,tin1);
1216b3afc53Stls tin[0]=tin0;
1226b3afc53Stls tin[1]=tin1;
1230a86a6b0Schristos BF_decrypt(tin,(const BF_KEY *)schedule);
1246b3afc53Stls tout0=tin[0]^xor0;
1256b3afc53Stls tout1=tin[1]^xor1;
1266b3afc53Stls l2n(tout0,out);
1276b3afc53Stls l2n(tout1,out);
1286b3afc53Stls xor0=tin0;
1296b3afc53Stls xor1=tin1;
1306b3afc53Stls }
1316b3afc53Stls if (l != -8)
1326b3afc53Stls {
1336b3afc53Stls n2l(in,tin0);
1346b3afc53Stls n2l(in,tin1);
1356b3afc53Stls tin[0]=tin0;
1366b3afc53Stls tin[1]=tin1;
1370a86a6b0Schristos BF_decrypt(tin,(const BF_KEY *)schedule);
1386b3afc53Stls tout0=tin[0]^xor0;
1396b3afc53Stls tout1=tin[1]^xor1;
1406b3afc53Stls l2nn(tout0,tout1,out,l+8);
1416b3afc53Stls xor0=tin0;
1426b3afc53Stls xor1=tin1;
1436b3afc53Stls }
1446b3afc53Stls l2n(xor0,ivec);
1456b3afc53Stls l2n(xor1,ivec);
1466b3afc53Stls }
1476b3afc53Stls tin0=tin1=tout0=tout1=xor0=xor1=0;
1486b3afc53Stls tin[0]=tin[1]=0;
1496b3afc53Stls }
1506b3afc53Stls
151