1/*
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License").  You may not use
5 * this file except in compliance with the License.  You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10#define idea_mul(r,a,b,ul) \
11ul=(unsigned long)a*b; \
12if (ul != 0) \
13        { \
14        r=(ul&0xffff)-(ul>>16); \
15        r-=((r)>>16); \
16        } \
17else \
18        r=(-(int)a-b+1);        /* assuming a or b is 0 and in range */
19
20/* NOTE - c is not incremented as per n2l */
21#define n2ln(c,l1,l2,n) { \
22                        c+=n; \
23                        l1=l2=0; \
24                        switch (n) { \
25                        case 8: l2 =((unsigned long)(*(--(c))))    ; \
26                        /* fall thru */                              \
27                        case 7: l2|=((unsigned long)(*(--(c))))<< 8; \
28                        /* fall thru */                              \
29                        case 6: l2|=((unsigned long)(*(--(c))))<<16; \
30                        /* fall thru */                              \
31                        case 5: l2|=((unsigned long)(*(--(c))))<<24; \
32                        /* fall thru */                              \
33                        case 4: l1 =((unsigned long)(*(--(c))))    ; \
34                        /* fall thru */                              \
35                        case 3: l1|=((unsigned long)(*(--(c))))<< 8; \
36                        /* fall thru */                              \
37                        case 2: l1|=((unsigned long)(*(--(c))))<<16; \
38                        /* fall thru */                              \
39                        case 1: l1|=((unsigned long)(*(--(c))))<<24; \
40                                } \
41                        }
42
43/* NOTE - c is not incremented as per l2n */
44#define l2nn(l1,l2,c,n) { \
45                        c+=n; \
46                        switch (n) { \
47                        case 8: *(--(c))=(unsigned char)(((l2)    )&0xff); \
48                        /* fall thru */                                    \
49                        case 7: *(--(c))=(unsigned char)(((l2)>> 8)&0xff); \
50                        /* fall thru */                                    \
51                        case 6: *(--(c))=(unsigned char)(((l2)>>16)&0xff); \
52                        /* fall thru */                                    \
53                        case 5: *(--(c))=(unsigned char)(((l2)>>24)&0xff); \
54                        /* fall thru */                                    \
55                        case 4: *(--(c))=(unsigned char)(((l1)    )&0xff); \
56                        /* fall thru */                                    \
57                        case 3: *(--(c))=(unsigned char)(((l1)>> 8)&0xff); \
58                        /* fall thru */                                    \
59                        case 2: *(--(c))=(unsigned char)(((l1)>>16)&0xff); \
60                        /* fall thru */                                    \
61                        case 1: *(--(c))=(unsigned char)(((l1)>>24)&0xff); \
62                                } \
63                        }
64
65#undef n2l
66#define n2l(c,l)        (l =((unsigned long)(*((c)++)))<<24L, \
67                         l|=((unsigned long)(*((c)++)))<<16L, \
68                         l|=((unsigned long)(*((c)++)))<< 8L, \
69                         l|=((unsigned long)(*((c)++))))
70
71#undef l2n
72#define l2n(l,c)        (*((c)++)=(unsigned char)(((l)>>24L)&0xff), \
73                         *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
74                         *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
75                         *((c)++)=(unsigned char)(((l)     )&0xff))
76
77#undef s2n
78#define s2n(l,c)        (*((c)++)=(unsigned char)(((l)     )&0xff), \
79                         *((c)++)=(unsigned char)(((l)>> 8L)&0xff))
80
81#undef n2s
82#define n2s(c,l)        (l =((IDEA_INT)(*((c)++)))<< 8L, \
83                         l|=((IDEA_INT)(*((c)++)))      )
84
85
86#define E_IDEA(num) \
87        x1&=0xffff; \
88        idea_mul(x1,x1,*p,ul); p++; \
89        x2+= *(p++); \
90        x3+= *(p++); \
91        x4&=0xffff; \
92        idea_mul(x4,x4,*p,ul); p++; \
93        t0=(x1^x3)&0xffff; \
94        idea_mul(t0,t0,*p,ul); p++; \
95        t1=(t0+(x2^x4))&0xffff; \
96        idea_mul(t1,t1,*p,ul); p++; \
97        t0+=t1; \
98        x1^=t1; \
99        x4^=t0; \
100        ul=x2^t0; /* do the swap to x3 */ \
101        x2=x3^t1; \
102        x3=ul;
103