xref: /openssl/crypto/idea/idea_local.h (revision 7ed6de99)
1 /*
2  * Copyright 1995-2024 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) \
11 ul=(unsigned long)a*b; \
12 if (ul != 0) { \
13         r=(ul&0xffff)-(ul>>16); \
14         r-=((r)>>16); \
15 } else { \
16         r=(-(int)a-b+1);        /* assuming a or b is 0 and in range */ \
17 }
18 
19 /* NOTE - c is not incremented as per n2l */
20 #define n2ln(c,l1,l2,n) { \
21                         c+=n; \
22                         l1=l2=0; \
23                         switch (n) { \
24                         case 8: l2 =((unsigned long)(*(--(c))))    ; \
25                         /* fall through */                              \
26                         case 7: l2|=((unsigned long)(*(--(c))))<< 8; \
27                         /* fall through */                              \
28                         case 6: l2|=((unsigned long)(*(--(c))))<<16; \
29                         /* fall through */                              \
30                         case 5: l2|=((unsigned long)(*(--(c))))<<24; \
31                         /* fall through */                              \
32                         case 4: l1 =((unsigned long)(*(--(c))))    ; \
33                         /* fall through */                              \
34                         case 3: l1|=((unsigned long)(*(--(c))))<< 8; \
35                         /* fall through */                              \
36                         case 2: l1|=((unsigned long)(*(--(c))))<<16; \
37                         /* fall through */                              \
38                         case 1: l1|=((unsigned long)(*(--(c))))<<24; \
39                                 } \
40                         }
41 
42 /* NOTE - c is not incremented as per l2n */
43 #define l2nn(l1,l2,c,n) { \
44                         c+=n; \
45                         switch (n) { \
46                         case 8: *(--(c))=(unsigned char)(((l2)    )&0xff); \
47                         /* fall through */                                    \
48                         case 7: *(--(c))=(unsigned char)(((l2)>> 8)&0xff); \
49                         /* fall through */                                    \
50                         case 6: *(--(c))=(unsigned char)(((l2)>>16)&0xff); \
51                         /* fall through */                                    \
52                         case 5: *(--(c))=(unsigned char)(((l2)>>24)&0xff); \
53                         /* fall through */                                    \
54                         case 4: *(--(c))=(unsigned char)(((l1)    )&0xff); \
55                         /* fall through */                                    \
56                         case 3: *(--(c))=(unsigned char)(((l1)>> 8)&0xff); \
57                         /* fall through */                                    \
58                         case 2: *(--(c))=(unsigned char)(((l1)>>16)&0xff); \
59                         /* fall through */                                    \
60                         case 1: *(--(c))=(unsigned char)(((l1)>>24)&0xff); \
61                                 } \
62                         }
63 
64 #undef n2l
65 #define n2l(c,l)        (l =((unsigned long)(*((c)++)))<<24L, \
66                          l|=((unsigned long)(*((c)++)))<<16L, \
67                          l|=((unsigned long)(*((c)++)))<< 8L, \
68                          l|=((unsigned long)(*((c)++))))
69 
70 #undef l2n
71 #define l2n(l,c)        (*((c)++)=(unsigned char)(((l)>>24L)&0xff), \
72                          *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
73                          *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
74                          *((c)++)=(unsigned char)(((l)     )&0xff))
75 
76 #undef s2n
77 #define s2n(l,c)        (*((c)++)=(unsigned char)(((l)     )&0xff), \
78                          *((c)++)=(unsigned char)(((l)>> 8L)&0xff))
79 
80 #undef n2s
81 #define n2s(c,l)        (l =((IDEA_INT)(*((c)++)))<< 8L, \
82                          l|=((IDEA_INT)(*((c)++)))      )
83 
84 
85 #define E_IDEA(num) \
86         x1&=0xffff; \
87         idea_mul(x1,x1,*p,ul); p++; \
88         x2+= *(p++); \
89         x3+= *(p++); \
90         x4&=0xffff; \
91         idea_mul(x4,x4,*p,ul); p++; \
92         t0=(x1^x3)&0xffff; \
93         idea_mul(t0,t0,*p,ul); p++; \
94         t1=(t0+(x2^x4))&0xffff; \
95         idea_mul(t1,t1,*p,ul); p++; \
96         t0+=t1; \
97         x1^=t1; \
98         x4^=t0; \
99         ul=x2^t0; /* do the swap to x3 */ \
100         x2=x3^t1; \
101         x3=ul;
102