172c33676SMaxim Ag /* $OpenBSD: i_cbc.c,v 1.3 2014/10/28 07:35:58 jsg Exp $ */
2f5b1c8a1SJohn Marino /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3f5b1c8a1SJohn Marino * All rights reserved.
4f5b1c8a1SJohn Marino *
5f5b1c8a1SJohn Marino * This package is an SSL implementation written
6f5b1c8a1SJohn Marino * by Eric Young (eay@cryptsoft.com).
7f5b1c8a1SJohn Marino * The implementation was written so as to conform with Netscapes SSL.
8f5b1c8a1SJohn Marino *
9f5b1c8a1SJohn Marino * This library is free for commercial and non-commercial use as long as
10f5b1c8a1SJohn Marino * the following conditions are aheared to. The following conditions
11f5b1c8a1SJohn Marino * apply to all code found in this distribution, be it the RC4, RSA,
12f5b1c8a1SJohn Marino * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13f5b1c8a1SJohn Marino * included with this distribution is covered by the same copyright terms
14f5b1c8a1SJohn Marino * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15f5b1c8a1SJohn Marino *
16f5b1c8a1SJohn Marino * Copyright remains Eric Young's, and as such any Copyright notices in
17f5b1c8a1SJohn Marino * the code are not to be removed.
18f5b1c8a1SJohn Marino * If this package is used in a product, Eric Young should be given attribution
19f5b1c8a1SJohn Marino * as the author of the parts of the library used.
20f5b1c8a1SJohn Marino * This can be in the form of a textual message at program startup or
21f5b1c8a1SJohn Marino * in documentation (online or textual) provided with the package.
22f5b1c8a1SJohn Marino *
23f5b1c8a1SJohn Marino * Redistribution and use in source and binary forms, with or without
24f5b1c8a1SJohn Marino * modification, are permitted provided that the following conditions
25f5b1c8a1SJohn Marino * are met:
26f5b1c8a1SJohn Marino * 1. Redistributions of source code must retain the copyright
27f5b1c8a1SJohn Marino * notice, this list of conditions and the following disclaimer.
28f5b1c8a1SJohn Marino * 2. Redistributions in binary form must reproduce the above copyright
29f5b1c8a1SJohn Marino * notice, this list of conditions and the following disclaimer in the
30f5b1c8a1SJohn Marino * documentation and/or other materials provided with the distribution.
31f5b1c8a1SJohn Marino * 3. All advertising materials mentioning features or use of this software
32f5b1c8a1SJohn Marino * must display the following acknowledgement:
33f5b1c8a1SJohn Marino * "This product includes cryptographic software written by
34f5b1c8a1SJohn Marino * Eric Young (eay@cryptsoft.com)"
35f5b1c8a1SJohn Marino * The word 'cryptographic' can be left out if the rouines from the library
36f5b1c8a1SJohn Marino * being used are not cryptographic related :-).
37f5b1c8a1SJohn Marino * 4. If you include any Windows specific code (or a derivative thereof) from
38f5b1c8a1SJohn Marino * the apps directory (application code) you must include an acknowledgement:
39f5b1c8a1SJohn Marino * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40f5b1c8a1SJohn Marino *
41f5b1c8a1SJohn Marino * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42f5b1c8a1SJohn Marino * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43f5b1c8a1SJohn Marino * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44f5b1c8a1SJohn Marino * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45f5b1c8a1SJohn Marino * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46f5b1c8a1SJohn Marino * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47f5b1c8a1SJohn Marino * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48f5b1c8a1SJohn Marino * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49f5b1c8a1SJohn Marino * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50f5b1c8a1SJohn Marino * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51f5b1c8a1SJohn Marino * SUCH DAMAGE.
52f5b1c8a1SJohn Marino *
53f5b1c8a1SJohn Marino * The licence and distribution terms for any publically available version or
54f5b1c8a1SJohn Marino * derivative of this code cannot be changed. i.e. this code cannot simply be
55f5b1c8a1SJohn Marino * copied and put under another distribution licence
56f5b1c8a1SJohn Marino * [including the GNU Public Licence.]
57f5b1c8a1SJohn Marino */
58f5b1c8a1SJohn Marino
59f5b1c8a1SJohn Marino #include <openssl/idea.h>
60f5b1c8a1SJohn Marino #include "idea_lcl.h"
61f5b1c8a1SJohn Marino
idea_cbc_encrypt(const unsigned char * in,unsigned char * out,long length,IDEA_KEY_SCHEDULE * ks,unsigned char * iv,int encrypt)62f5b1c8a1SJohn Marino void idea_cbc_encrypt(const unsigned char *in, unsigned char *out, long length,
63f5b1c8a1SJohn Marino IDEA_KEY_SCHEDULE *ks, unsigned char *iv, int encrypt)
64f5b1c8a1SJohn Marino {
65f5b1c8a1SJohn Marino unsigned long tin0,tin1;
66f5b1c8a1SJohn Marino unsigned long tout0,tout1,xor0,xor1;
67f5b1c8a1SJohn Marino long l=length;
68f5b1c8a1SJohn Marino unsigned long tin[2];
69f5b1c8a1SJohn Marino
70f5b1c8a1SJohn Marino if (encrypt)
71f5b1c8a1SJohn Marino {
72f5b1c8a1SJohn Marino n2l(iv,tout0);
73f5b1c8a1SJohn Marino n2l(iv,tout1);
74f5b1c8a1SJohn Marino iv-=8;
75f5b1c8a1SJohn Marino for (l-=8; l>=0; l-=8)
76f5b1c8a1SJohn Marino {
77f5b1c8a1SJohn Marino n2l(in,tin0);
78f5b1c8a1SJohn Marino n2l(in,tin1);
79f5b1c8a1SJohn Marino tin0^=tout0;
80f5b1c8a1SJohn Marino tin1^=tout1;
81f5b1c8a1SJohn Marino tin[0]=tin0;
82f5b1c8a1SJohn Marino tin[1]=tin1;
83f5b1c8a1SJohn Marino idea_encrypt(tin,ks);
84f5b1c8a1SJohn Marino tout0=tin[0]; l2n(tout0,out);
85f5b1c8a1SJohn Marino tout1=tin[1]; l2n(tout1,out);
86f5b1c8a1SJohn Marino }
87f5b1c8a1SJohn Marino if (l != -8)
88f5b1c8a1SJohn Marino {
89f5b1c8a1SJohn Marino n2ln(in,tin0,tin1,l+8);
90f5b1c8a1SJohn Marino tin0^=tout0;
91f5b1c8a1SJohn Marino tin1^=tout1;
92f5b1c8a1SJohn Marino tin[0]=tin0;
93f5b1c8a1SJohn Marino tin[1]=tin1;
94f5b1c8a1SJohn Marino idea_encrypt(tin,ks);
95f5b1c8a1SJohn Marino tout0=tin[0]; l2n(tout0,out);
96f5b1c8a1SJohn Marino tout1=tin[1]; l2n(tout1,out);
97f5b1c8a1SJohn Marino }
98f5b1c8a1SJohn Marino l2n(tout0,iv);
99f5b1c8a1SJohn Marino l2n(tout1,iv);
100f5b1c8a1SJohn Marino }
101f5b1c8a1SJohn Marino else
102f5b1c8a1SJohn Marino {
103f5b1c8a1SJohn Marino n2l(iv,xor0);
104f5b1c8a1SJohn Marino n2l(iv,xor1);
105f5b1c8a1SJohn Marino iv-=8;
106f5b1c8a1SJohn Marino for (l-=8; l>=0; l-=8)
107f5b1c8a1SJohn Marino {
108f5b1c8a1SJohn Marino n2l(in,tin0); tin[0]=tin0;
109f5b1c8a1SJohn Marino n2l(in,tin1); tin[1]=tin1;
110f5b1c8a1SJohn Marino idea_encrypt(tin,ks);
111f5b1c8a1SJohn Marino tout0=tin[0]^xor0;
112f5b1c8a1SJohn Marino tout1=tin[1]^xor1;
113f5b1c8a1SJohn Marino l2n(tout0,out);
114f5b1c8a1SJohn Marino l2n(tout1,out);
115f5b1c8a1SJohn Marino xor0=tin0;
116f5b1c8a1SJohn Marino xor1=tin1;
117f5b1c8a1SJohn Marino }
118f5b1c8a1SJohn Marino if (l != -8)
119f5b1c8a1SJohn Marino {
120f5b1c8a1SJohn Marino n2l(in,tin0); tin[0]=tin0;
121f5b1c8a1SJohn Marino n2l(in,tin1); tin[1]=tin1;
122f5b1c8a1SJohn Marino idea_encrypt(tin,ks);
123f5b1c8a1SJohn Marino tout0=tin[0]^xor0;
124f5b1c8a1SJohn Marino tout1=tin[1]^xor1;
125f5b1c8a1SJohn Marino l2nn(tout0,tout1,out,l+8);
126f5b1c8a1SJohn Marino xor0=tin0;
127f5b1c8a1SJohn Marino xor1=tin1;
128f5b1c8a1SJohn Marino }
129f5b1c8a1SJohn Marino l2n(xor0,iv);
130f5b1c8a1SJohn Marino l2n(xor1,iv);
131f5b1c8a1SJohn Marino }
132f5b1c8a1SJohn Marino tin0=tin1=tout0=tout1=xor0=xor1=0;
133f5b1c8a1SJohn Marino tin[0]=tin[1]=0;
134f5b1c8a1SJohn Marino }
135f5b1c8a1SJohn Marino
idea_encrypt(unsigned long * d,IDEA_KEY_SCHEDULE * key)136f5b1c8a1SJohn Marino void idea_encrypt(unsigned long *d, IDEA_KEY_SCHEDULE *key)
137f5b1c8a1SJohn Marino {
138f5b1c8a1SJohn Marino IDEA_INT *p;
139f5b1c8a1SJohn Marino unsigned long x1,x2,x3,x4,t0,t1,ul;
140f5b1c8a1SJohn Marino
141f5b1c8a1SJohn Marino x2=d[0];
142f5b1c8a1SJohn Marino x1=(x2>>16);
143f5b1c8a1SJohn Marino x4=d[1];
144f5b1c8a1SJohn Marino x3=(x4>>16);
145f5b1c8a1SJohn Marino
146f5b1c8a1SJohn Marino p= &(key->data[0][0]);
147f5b1c8a1SJohn Marino
148f5b1c8a1SJohn Marino E_IDEA(0);
149f5b1c8a1SJohn Marino E_IDEA(1);
150f5b1c8a1SJohn Marino E_IDEA(2);
151f5b1c8a1SJohn Marino E_IDEA(3);
152f5b1c8a1SJohn Marino E_IDEA(4);
153f5b1c8a1SJohn Marino E_IDEA(5);
154f5b1c8a1SJohn Marino E_IDEA(6);
155f5b1c8a1SJohn Marino E_IDEA(7);
156f5b1c8a1SJohn Marino
157f5b1c8a1SJohn Marino x1&=0xffff;
158f5b1c8a1SJohn Marino idea_mul(x1,x1,*p,ul); p++;
159f5b1c8a1SJohn Marino
160f5b1c8a1SJohn Marino t0= x3+ *(p++);
161f5b1c8a1SJohn Marino t1= x2+ *(p++);
162f5b1c8a1SJohn Marino
163f5b1c8a1SJohn Marino x4&=0xffff;
164f5b1c8a1SJohn Marino idea_mul(x4,x4,*p,ul);
165f5b1c8a1SJohn Marino
166f5b1c8a1SJohn Marino d[0]=(t0&0xffff)|((x1&0xffff)<<16);
167f5b1c8a1SJohn Marino d[1]=(x4&0xffff)|((t1&0xffff)<<16);
168f5b1c8a1SJohn Marino }
169