38 #if defined(ELEPHANT_ALG_NAME)
40 #define ELEPHANT_CONCAT_INNER(name,suffix) name##suffix
41 #define ELEPHANT_CONCAT(name,suffix) ELEPHANT_CONCAT_INNER(name,suffix)
43 static int ELEPHANT_CONCAT(ELEPHANT_ALG_NAME,_aead_crypt)
44 (
unsigned char *c,
const unsigned char *m,
size_t mlen,
45 const unsigned char *ad,
size_t adlen,
46 const unsigned char *npub,
const unsigned char *k,
49 unsigned char *cstart = c;
52 unsigned char start[ELEPHANT_STATE_SIZE];
53 unsigned char mask1[ELEPHANT_STATE_SIZE];
54 unsigned char mask2[ELEPHANT_STATE_SIZE];
55 unsigned char mask3[ELEPHANT_STATE_SIZE];
56 unsigned char tag[ELEPHANT_STATE_SIZE];
58 unsigned char *m1 = mask1;
59 unsigned char *m2 = mask2;
60 unsigned char *m3 = mask3;
64 memcpy(state.B, k, ELEPHANT_KEY_SIZE);
65 memset(state.B + ELEPHANT_KEY_SIZE, 0,
sizeof(state.B) - ELEPHANT_KEY_SIZE);
66 ELEPHANT_PERMUTE(&state);
67 memcpy(start, state.B, ELEPHANT_STATE_SIZE);
70 memcpy(m1, start, ELEPHANT_STATE_SIZE);
71 ELEPHANT_LFSR(m2, m1);
72 ELEPHANT_LFSR(m3, m2);
75 memcpy(tag, npub, ELEPHANT_NONCE_SIZE);
76 if (adlen >= (ELEPHANT_STATE_SIZE - ELEPHANT_NONCE_SIZE)) {
77 memcpy(tag + ELEPHANT_NONCE_SIZE, ad,
78 ELEPHANT_STATE_SIZE - ELEPHANT_NONCE_SIZE);
79 ad += ELEPHANT_STATE_SIZE - ELEPHANT_NONCE_SIZE;
80 adlen -= ELEPHANT_STATE_SIZE - ELEPHANT_NONCE_SIZE;
83 memcpy(tag + ELEPHANT_NONCE_SIZE, ad, adlen);
84 tag[ELEPHANT_NONCE_SIZE + adlen] = 0x01;
85 memset(tag + ELEPHANT_NONCE_SIZE + adlen + 1, 0,
86 ELEPHANT_STATE_SIZE - ELEPHANT_NONCE_SIZE - (adlen + 1));
98 lw_xor_block_2_src(state.B, npub, m1, ELEPHANT_NONCE_SIZE);
99 memcpy(state.B + ELEPHANT_NONCE_SIZE, m1 + ELEPHANT_NONCE_SIZE,
100 ELEPHANT_STATE_SIZE - ELEPHANT_NONCE_SIZE);
101 lw_xor_block(state.B, m2, ELEPHANT_STATE_SIZE);
102 ELEPHANT_PERMUTE(&state);
103 lw_xor_block(state.B, m1, ELEPHANT_STATE_SIZE);
104 lw_xor_block(state.B, m2, ELEPHANT_STATE_SIZE);
105 if (mlen >= ELEPHANT_STATE_SIZE) {
106 lw_xor_block_2_dest(c, state.B, m, ELEPHANT_STATE_SIZE);
107 c += ELEPHANT_STATE_SIZE;
108 m += ELEPHANT_STATE_SIZE;
109 mlen -= ELEPHANT_STATE_SIZE;
111 lw_xor_block_2_dest(c, state.B, m, mlen);
112 state.B[mlen] = 0x01;
113 memset(state.B + mlen + 1, 0,
114 ELEPHANT_STATE_SIZE - mlen - 1);
122 lw_xor_block(state.B, m1, ELEPHANT_STATE_SIZE);
123 lw_xor_block(state.B, m3, ELEPHANT_STATE_SIZE);
124 ELEPHANT_PERMUTE(&state);
125 lw_xor_block(state.B, m1, ELEPHANT_STATE_SIZE);
126 lw_xor_block(state.B, m3, ELEPHANT_STATE_SIZE);
127 lw_xor_block(tag, state.B, ELEPHANT_STATE_SIZE);
130 if (mlen >= ELEPHANT_STATE_SIZE) {
131 lw_xor_block_2_src(state.B, m, m1, ELEPHANT_STATE_SIZE);
133 memcpy(state.B, m, mlen);
134 state.B[mlen] = 0x01;
135 memset(state.B + mlen + 1, 0,
136 ELEPHANT_STATE_SIZE - mlen - 1);
137 lw_xor_block(state.B, m1, ELEPHANT_STATE_SIZE);
139 lw_xor_block(state.B, m3, ELEPHANT_STATE_SIZE);
140 ELEPHANT_PERMUTE(&state);
141 lw_xor_block(state.B, m1, ELEPHANT_STATE_SIZE);
142 lw_xor_block(state.B, m3, ELEPHANT_STATE_SIZE);
143 lw_xor_block(tag, state.B, ELEPHANT_STATE_SIZE);
146 lw_xor_block_2_src(state.B, npub, m1, ELEPHANT_NONCE_SIZE);
147 memcpy(state.B + ELEPHANT_NONCE_SIZE, m1 + ELEPHANT_NONCE_SIZE,
148 ELEPHANT_STATE_SIZE - ELEPHANT_NONCE_SIZE);
149 lw_xor_block(state.B, m2, ELEPHANT_STATE_SIZE);
150 ELEPHANT_PERMUTE(&state);
151 lw_xor_block(state.B, m1, ELEPHANT_STATE_SIZE);
152 lw_xor_block(state.B, m2, ELEPHANT_STATE_SIZE);
153 if (mlen >= ELEPHANT_STATE_SIZE) {
154 lw_xor_block_2_src(c, state.B, m, ELEPHANT_STATE_SIZE);
155 c += ELEPHANT_STATE_SIZE;
156 m += ELEPHANT_STATE_SIZE;
157 mlen -= ELEPHANT_STATE_SIZE;
159 lw_xor_block_2_src(c, state.B, m, mlen);
170 if (adlen >= ELEPHANT_STATE_SIZE) {
171 lw_xor_block_2_src(state.B, ad, m2, ELEPHANT_STATE_SIZE);
172 ad += ELEPHANT_STATE_SIZE;
173 adlen -= ELEPHANT_STATE_SIZE;
175 memcpy(state.B, ad, adlen);
176 state.B[adlen] = 0x01;
177 memset(state.B + adlen + 1, 0, ELEPHANT_STATE_SIZE - adlen - 1);
178 lw_xor_block(state.B, m2, ELEPHANT_STATE_SIZE);
182 ELEPHANT_PERMUTE(&state);
183 lw_xor_block(state.B, m2, ELEPHANT_STATE_SIZE);
184 lw_xor_block(tag, state.B, ELEPHANT_STATE_SIZE);
188 if (!admore && !mmore)
196 ELEPHANT_LFSR(m3, m2);
200 lw_xor_block_2_src(state.B, tag, start, ELEPHANT_STATE_SIZE);
201 ELEPHANT_PERMUTE(&state);
203 lw_xor_block_2_src(c, state.B, start, ELEPHANT_TAG_SIZE);
206 lw_xor_block(state.B, start, ELEPHANT_TAG_SIZE);
207 return aead_check_tag(cstart, clen, state.B, m, ELEPHANT_TAG_SIZE);
211 int ELEPHANT_CONCAT(ELEPHANT_ALG_NAME,_aead_encrypt)
212 (
unsigned char *c,
size_t *clen,
213 const unsigned char *m,
size_t mlen,
214 const unsigned char *ad,
size_t adlen,
215 const unsigned char *npub,
216 const unsigned char *k)
219 *clen = mlen + ELEPHANT_TAG_SIZE;
222 return ELEPHANT_CONCAT(ELEPHANT_ALG_NAME,_aead_crypt)
223 (c, m, mlen, ad, adlen, npub, k, 1);
226 int ELEPHANT_CONCAT(ELEPHANT_ALG_NAME,_aead_decrypt)
227 (
unsigned char *m,
size_t *mlen,
228 const unsigned char *c,
size_t clen,
229 const unsigned char *ad,
size_t adlen,
230 const unsigned char *npub,
231 const unsigned char *k)
234 if (clen < ELEPHANT_TAG_SIZE)
236 clen -= ELEPHANT_TAG_SIZE;
240 return ELEPHANT_CONCAT(ELEPHANT_ALG_NAME,_aead_crypt)
241 (m, c, clen, ad, adlen, npub, k, 0);
248 #undef ELEPHANT_ALG_NAME
249 #undef ELEPHANT_STATE_SIZE
250 #undef ELEPHANT_STATE
251 #undef ELEPHANT_KEY_SIZE
252 #undef ELEPHANT_NONCE_SIZE
253 #undef ELEPHANT_TAG_SIZE
255 #undef ELEPHANT_PERMUTE
256 #undef ELEPHANT_CONCAT
257 #undef ELEPHANT_CONCAT_INNER