Revert "Revert "and added files""
[bcm963xx.git] / userapps / opensource / sshd / libtomcrypt / noekeon.c
1 /* Implementation of the Noekeon block cipher by Tom St Denis */
2 #include "mycrypt.h"
3
4 #ifdef NOEKEON
5
6 const struct _cipher_descriptor noekeon_desc =
7 {
8     "noekeon",
9     16,
10     16, 16, 16, 16,
11     &noekeon_setup,
12     &noekeon_ecb_encrypt,
13     &noekeon_ecb_decrypt,
14     &noekeon_test,
15     &noekeon_keysize
16 };
17
18 static const unsigned long RC[] = {
19    0x00000080UL, 0x0000001bUL, 0x00000036UL, 0x0000006cUL,
20    0x000000d8UL, 0x000000abUL, 0x0000004dUL, 0x0000009aUL,
21    0x0000002fUL, 0x0000005eUL, 0x000000bcUL, 0x00000063UL,
22    0x000000c6UL, 0x00000097UL, 0x00000035UL, 0x0000006aUL,
23    0x000000d4UL 
24 };
25
26 static const unsigned long zero[] = { 0, 0, 0, 0 };
27
28 #define THETA(k, a, b, c, d)                               \
29     temp = a^c; temp = temp ^ ROL(temp, 8) ^ ROR(temp, 8); \
30     b ^= temp; d ^= temp;                                  \
31     a ^= k[0]; b ^= k[1];                                  \
32     c ^= k[2]; d ^= k[3];                                  \
33     temp = b^d; temp = temp ^ ROL(temp, 8) ^ ROR(temp, 8); \
34     a ^= temp; c ^= temp;
35     
36 #define GAMMA(a, b, c, d)     \
37     b ^= ~(d|c);              \
38     a ^= c&b;                 \
39     temp = d; d = a; a = temp;\
40     c ^= a ^ b ^ d;           \
41     b ^= ~(d|c);              \
42     a ^= c&b;
43     
44 #define PI1(a, b, c, d) \
45     a = ROL(a, 1); c = ROL(c, 5); d = ROL(d, 2);
46     
47 #define PI2(a, b, c, d) \
48     a = ROR(a, 1); c = ROR(c, 5); d = ROR(d, 2);
49     
50 int noekeon_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
51 {
52    unsigned long temp;
53    
54    _ARGCHK(key != NULL);
55    _ARGCHK(skey != NULL);
56    
57    if (keylen != 16) {
58       return CRYPT_INVALID_KEYSIZE;
59    }
60    
61    if (num_rounds != 16 && num_rounds != 0) {
62       return CRYPT_INVALID_ROUNDS;
63    }
64    
65    LOAD32L(skey->noekeon.K[0],&key[0]);
66    LOAD32L(skey->noekeon.K[1],&key[4]);
67    LOAD32L(skey->noekeon.K[2],&key[8]);
68    LOAD32L(skey->noekeon.K[3],&key[12]);
69    
70    LOAD32L(skey->noekeon.dK[0],&key[0]);
71    LOAD32L(skey->noekeon.dK[1],&key[4]);
72    LOAD32L(skey->noekeon.dK[2],&key[8]);
73    LOAD32L(skey->noekeon.dK[3],&key[12]);
74
75    THETA(zero, skey->noekeon.dK[0], skey->noekeon.dK[1], skey->noekeon.dK[2], skey->noekeon.dK[3]);
76
77    return CRYPT_OK;
78 }
79
80 #ifdef CLEAN_STACK
81 static void _noekeon_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key)
82 #else
83 void noekeon_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key)
84 #endif
85 {
86    unsigned long a,b,c,d,temp;
87    int r;
88
89    _ARGCHK(key != NULL);
90    _ARGCHK(pt != NULL);
91    _ARGCHK(ct != NULL);
92    
93    LOAD32L(a,&pt[0]); LOAD32L(b,&pt[4]);
94    LOAD32L(c,&pt[8]); LOAD32L(d,&pt[12]);
95    
96 #define ROUND(i) \
97        a ^= RC[r+i]; \
98        THETA(key->noekeon.K, a,b,c,d); \
99        PI1(a,b,c,d); \
100        GAMMA(a,b,c,d); \
101        PI2(a,b,c,d);
102
103    for (r = 0; r < 16; r += 2) {
104        ROUND(0);
105        ROUND(1);
106    }
107
108 #undef ROUND
109
110    a ^= RC[16];
111    THETA(key->noekeon.K, a, b, c, d);
112    
113    STORE32L(a,&ct[0]); STORE32L(b,&ct[4]);
114    STORE32L(c,&ct[8]); STORE32L(d,&ct[12]);
115 }
116
117 #ifdef CLEAN_STACK
118 void noekeon_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key)
119 {
120    _noekeon_ecb_encrypt(pt, ct, key);
121    burn_stack(sizeof(unsigned long) * 5 + sizeof(int));
122 }
123 #endif
124
125 #ifdef CLEAN_STACK
126 static void _noekeon_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key)
127 #else
128 void noekeon_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key)
129 #endif
130 {
131    unsigned long a,b,c,d, temp;
132    int r;
133
134    _ARGCHK(key != NULL);
135    _ARGCHK(pt != NULL);
136    _ARGCHK(ct != NULL);
137    
138    LOAD32L(a,&ct[0]); LOAD32L(b,&ct[4]);
139    LOAD32L(c,&ct[8]); LOAD32L(d,&ct[12]);
140    
141 #define ROUND(i) \
142        THETA(key->noekeon.dK, a,b,c,d); \
143        a ^= RC[r-i]; \
144        PI1(a,b,c,d); \
145        GAMMA(a,b,c,d); \
146        PI2(a,b,c,d); 
147        
148
149    for (r = 16; r > 0; r -= 2) {
150        ROUND(0);
151        ROUND(1);
152    }
153    
154 #undef ROUND
155
156    THETA(key->noekeon.dK, a,b,c,d);
157    a ^= RC[0];
158    STORE32L(a,&pt[0]); STORE32L(b, &pt[4]);
159    STORE32L(c,&pt[8]); STORE32L(d, &pt[12]);
160 }
161
162 #ifdef CLEAN_STACK
163 void noekeon_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key)
164 {
165    _noekeon_ecb_decrypt(ct, pt, key);
166    burn_stack(sizeof(unsigned long) * 5 + sizeof(int));
167 }
168 #endif
169
170 int noekeon_test(void)
171 {
172  #ifndef LTC_TEST
173     return CRYPT_NOP;
174  #else    
175    static const unsigned char
176           key[] = 
177              { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
178           pt[] = 
179              { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
180           ct[] = 
181              { 0x57, 0x9a, 0x6c, 0xe8, 0x91, 0x16, 0x52, 0x53,
182                0x32, 0x00, 0xca, 0x0a, 0x17, 0x5d, 0x28, 0x0e };
183    unsigned char tmp[2][16];
184    int err;
185    symmetric_key skey;
186    
187    if ((err = noekeon_setup(key, 16, 0, &skey)) != CRYPT_OK) {
188       return err;
189    }
190    
191    noekeon_ecb_encrypt(pt, tmp[0], &skey);
192    noekeon_ecb_decrypt(tmp[0], tmp[1], &skey);
193    
194    if (memcmp(tmp[0], ct, 16) != 0 || memcmp(tmp[1], pt, 16) != 0) {
195       return CRYPT_FAIL_TESTVECTOR;
196    }
197    
198    return CRYPT_OK;
199  #endif
200 }
201
202 int noekeon_keysize(int *desired_keysize)
203 {
204    _ARGCHK(desired_keysize != NULL);
205    if (*desired_keysize < 16) {
206       return CRYPT_INVALID_KEYSIZE;
207    } else {
208       *desired_keysize = 16;
209       return CRYPT_OK;
210    }
211 }
212
213 #endif
214