| |
|
| #include "ecrypt-sync.h" |
#include "ecrypt-sync.h" |
| |
|
| void genrand_tuple_int32(u32 rand_tuple[], s32 len); |
void genrand_tuple_int32(ECRYPT_ctx* ctx, u32 rand_tuple[], s32 len); |
| void init_by_array(u32 init_key[], int key_length); |
void init_by_array(ECRYPT_ctx* ctx, u32 init_key[], int key_length); |
| |
|
| |
#ifdef ECRYPT_API |
| |
#include "mt-fubuki.c" |
| |
#endif |
| |
|
| /* This is a stream cipher. One word means a 32 bit word. */ |
/* This is a stream cipher. One word means a 32 bit word. */ |
| /* Tuple words will be gather to make one Block. */ |
/* Tuple words will be gather to make one Block. */ |
| #define Log_Add_Size 5 /* Log of Add_Size */ |
#define Log_Add_Size 5 /* Log of Add_Size */ |
| #define Add_Size 32 /* number of constant adders */ |
#define Add_Size 32 /* number of constant adders */ |
| |
|
| u32 multi_table[Multi_Size], inv_table[Multi_Size]; |
|
| u32 add_table[Add_Size]; |
|
| s32 jump; |
|
| |
|
| /* for debug */ |
/* for debug */ |
| void print_block(u32 block[]) { |
void print_block(u32 block[]) { |
| s32 i; |
s32 i; |
| /**********************************************/ |
/**********************************************/ |
| |
|
| /* Compute Multiplication Constants: 3 mod 8, 7 mod 16 */ |
/* Compute Multiplication Constants: 3 mod 8, 7 mod 16 */ |
| void prepare_multi(void) { |
void prepare_multi(ECRYPT_ctx* ctx) { |
| s32 i; |
s32 i; |
| for (i=0; i<Multi_Size; i+=4) { |
for (i=0; i<Multi_Size; i+=4) { |
| genrand_tuple_int32(&multi_table[i], 4); |
genrand_tuple_int32(ctx, &ctx->multi_table[i], 4); |
| } |
} |
| for (i=0; i< Multi_Size; i+=2) { |
for (i=0; i< Multi_Size; i+=2) { |
| multi_table[i] = (multi_table[i] & U32C(0xfffffff8)) | U32C(0x3); |
ctx->multi_table[i] = (ctx->multi_table[i] & U32C(0xfffffff8)) | U32C(0x3); |
| multi_table[i] |= (U32C(0x80000000) >> (i % 8)); |
ctx->multi_table[i] |= (U32C(0x80000000) >> (i % 8)); |
| multi_table[i] &= ~(U32C(0x40000000) >> (i % 8)); |
ctx->multi_table[i] &= ~(U32C(0x40000000) >> (i % 8)); |
| |
|
| multi_table[i+1] = (multi_table[i+1] & U32C(0xfffffff0)) | U32C(0x7); |
ctx->multi_table[i+1] = (ctx->multi_table[i+1] & U32C(0xfffffff0)) | U32C(0x7); |
| multi_table[i+1] |= (U32C(0x80000000) >> (i+1 % 8)); |
ctx->multi_table[i+1] |= (U32C(0x80000000) >> (i+1 % 8)); |
| multi_table[i+1] &= ~(U32C(0x40000000) >> (i+1 % 8)); |
ctx->multi_table[i+1] &= ~(U32C(0x40000000) >> (i+1 % 8)); |
| } |
} |
| } |
} |
| |
|
| |
|
| |
|
| /* prepare the table of inverses */ |
/* prepare the table of inverses */ |
| void prepare_multi_inv(void) { |
void prepare_multi_inv(ECRYPT_ctx* ctx) { |
| s32 i; |
s32 i; |
| for (i=0; i< Multi_Size; i++) { |
for (i=0; i< Multi_Size; i++) { |
| inv_table[i] = inv_mod_32(multi_table[i]); |
ctx->inv_table[i] = inv_mod_32(ctx->multi_table[i]); |
| } |
} |
| } |
} |
| |
|
| /* Compute addition constants */ |
/* Compute addition constants */ |
| void prepare_add_table(void) { |
void prepare_add_table(ECRYPT_ctx* ctx) { |
| s32 i; |
s32 i; |
| |
|
| genrand_tuple_int32(add_table, Add_Size); |
genrand_tuple_int32(ctx, ctx->add_table, Add_Size); |
| |
|
| for (i=0; i< Add_Size; i++) { |
for (i=0; i< Add_Size; i++) { |
| u32 s; |
u32 s; |
| s = (i * 1103515245 + 12345) & (Add_Size - 1); |
s = (i * 1103515245 + 12345) & (Add_Size - 1); |
| s ^= (s >> (Log_Add_Size / 2)); |
s ^= (s >> (Log_Add_Size / 2)); |
| add_table[i] <<= Log_Add_Size; |
ctx->add_table[i] <<= Log_Add_Size; |
| add_table[i] |= s; |
ctx->add_table[i] |= s; |
| } |
} |
| } |
} |
| |
|
| |
|
| /* word wise encrypt by Exor Mult table-Plus Rotate */ |
/* word wise encrypt by Exor Mult table-Plus Rotate */ |
| /* rotate number is between 16 - 23 */ |
/* rotate number is between 16 - 23 */ |
| void crypt_empr(u32 block[Tuple]) |
void crypt_empr(ECRYPT_ctx* ctx, u32 block[Tuple]) |
| { |
{ |
| s32 i, s; |
s32 i, s; |
| u32 param[Tuple]; |
u32 param[Tuple]; |
| genrand_tuple_int32(param, Tuple); |
genrand_tuple_int32(ctx, param, Tuple); |
| for (i=0; i<Tuple; i++) { |
for (i=0; i<Tuple; i++) { |
| s = ((param[i] >> (32 - 4)) | 0x10 ) & 0x17; |
s = ((param[i] >> (32 - 4)) | 0x10 ) & 0x17; |
| block[i] ^= param[i]; |
block[i] ^= param[i]; |
| block[i] *= multi_table[param[(i+1) % Tuple]>> (32 - 5)]; |
block[i] *= ctx->multi_table[param[(i+1) % Tuple]>> (32 - 5)]; |
| block[(i+jump) & Low_Mask] += add_table[block[i] >> (32 - Log_Add_Size)]; |
block[(i+ctx->jump) & Low_Mask] += ctx->add_table[block[i] >> (32 - Log_Add_Size)]; |
| block[i] = ((~block[i]) << (32 - s)) | (block[i] >> s); |
block[i] = ((~block[i]) << (32 - s)) | (block[i] >> s); |
| } |
} |
| } |
} |
| |
|
| void crypt_empr_inv(u32 block[Tuple], u32 param[Tuple]) |
void crypt_empr_inv(ECRYPT_ctx* ctx, u32 block[Tuple], u32 param[Tuple]) |
| { |
{ |
| s32 i, s; |
s32 i, s; |
| for (i=Tuple-1; i>=0; i--) { |
for (i=Tuple-1; i>=0; i--) { |
| s = ((param[i] >> (32 - 4)) | 0x10 ) & 0x17; |
s = ((param[i] >> (32 - 4)) | 0x10 ) & 0x17; |
| block[i] = ((~block[i]) >> (32 - s)) | (block[i] << s); |
block[i] = ((~block[i]) >> (32 - s)) | (block[i] << s); |
| block[(i+jump) & Low_Mask] -= add_table[block[i] >> (32 - Log_Add_Size)]; |
block[(i+ctx->jump) & Low_Mask] -= ctx->add_table[block[i] >> (32 - Log_Add_Size)]; |
| block[i] *= inv_table[param[(i+1) % Tuple]>> (32 - 5)]; |
block[i] *= ctx->inv_table[param[(i+1) % Tuple]>> (32 - 5)]; |
| block[i] ^= param[i]; |
block[i] ^= param[i]; |
| } |
} |
| } |
} |
| |
|
| /* word wise encrypt by Exor Mult table-Exor Rotate */ |
/* word wise encrypt by Exor Mult table-Exor Rotate */ |
| /* rotate number is between 16 - 23 */ |
/* rotate number is between 16 - 23 */ |
| void crypt_emer(u32 block[Tuple]) |
void crypt_emer(ECRYPT_ctx* ctx, u32 block[Tuple]) |
| { |
{ |
| s32 i, s; |
s32 i, s; |
| u32 param[Tuple]; |
u32 param[Tuple]; |
| genrand_tuple_int32(param, Tuple); |
genrand_tuple_int32(ctx, param, Tuple); |
| for (i=0; i<Tuple; i++) { |
for (i=0; i<Tuple; i++) { |
| s = ((param[i] >> (32 - 4)) | 0x10 ) & 0x17; |
s = ((param[i] >> (32 - 4)) | 0x10 ) & 0x17; |
| block[i] ^= param[i]; |
block[i] ^= param[i]; |
| block[i] *= multi_table[param[(i+2) % Tuple]>> (32 - 5)]; |
block[i] *= ctx->multi_table[param[(i+2) % Tuple]>> (32 - 5)]; |
| block[(i+jump) & Low_Mask] ^= add_table[block[i] >> (32 - Log_Add_Size)]; |
block[(i+ctx->jump) & Low_Mask] ^= ctx->add_table[block[i] >> (32 - Log_Add_Size)]; |
| block[i] = ((~block[i]) << (32 - s)) | (block[i] >> s); |
block[i] = ((~block[i]) << (32 - s)) | (block[i] >> s); |
| } |
} |
| } |
} |
| |
|
| void crypt_emer_inv(u32 block[Tuple], u32 param[Tuple]) |
void crypt_emer_inv(ECRYPT_ctx* ctx, u32 block[Tuple], u32 param[Tuple]) |
| { |
{ |
| s32 i, s; |
s32 i, s; |
| for (i=Tuple-1; i>=0; i--) { |
for (i=Tuple-1; i>=0; i--) { |
| s = ((param[i] >> (32 - 4)) | 0x10 ) & 0x17; |
s = ((param[i] >> (32 - 4)) | 0x10 ) & 0x17; |
| block[i] = ((~block[i]) >> (32 - s)) | (block[i] << s); |
block[i] = ((~block[i]) >> (32 - s)) | (block[i] << s); |
| block[(i+jump) & Low_Mask] ^= add_table[block[i] >> (32 - Log_Add_Size)]; |
block[(i+ctx->jump) & Low_Mask] ^= ctx->add_table[block[i] >> (32 - Log_Add_Size)]; |
| block[i] *= inv_table[param[(i+2) % Tuple]>> (32 - 5)]; |
block[i] *= ctx->inv_table[param[(i+2) % Tuple]>> (32 - 5)]; |
| block[i] ^= param[i]; |
block[i] ^= param[i]; |
| } |
} |
| } |
} |
| |
|
| /* word wise encrypt by Exor Mult table-Plus Shift */ |
/* word wise encrypt by Exor Mult table-Plus Shift */ |
| /* Shift number is betwee 16 - 23 */ |
/* Shift number is betwee 16 - 23 */ |
| void crypt_emps(u32 block[Tuple]) |
void crypt_emps(ECRYPT_ctx* ctx, u32 block[Tuple]) |
| { |
{ |
| s32 i, s; |
s32 i, s; |
| u32 param[Tuple]; |
u32 param[Tuple]; |
| genrand_tuple_int32(param, Tuple); |
genrand_tuple_int32(ctx, param, Tuple); |
| for (i=0; i<Tuple; i++) { |
for (i=0; i<Tuple; i++) { |
| s = ((param[i] >> (32 - 4)) | 0x10 ) & 0x17; |
s = ((param[i] >> (32 - 4)) | 0x10 ) & 0x17; |
| block[i] ^= param[i]; |
block[i] ^= param[i]; |
| block[i] *= multi_table[param[(i+2) % Tuple]>> (32 - 5)]; |
block[i] *= ctx->multi_table[param[(i+2) % Tuple]>> (32 - 5)]; |
| block[(i+jump) & Low_Mask] += add_table[block[i] >> (32 - Log_Add_Size)]; |
block[(i+ctx->jump) & Low_Mask] += ctx->add_table[block[i] >> (32 - Log_Add_Size)]; |
| block[i] ^= ((~block[i]) >> s); |
block[i] ^= ((~block[i]) >> s); |
| } |
} |
| } |
} |
| |
|
| void crypt_emps_inv(u32 block[Tuple], u32 param[Tuple]) |
void crypt_emps_inv(ECRYPT_ctx* ctx, u32 block[Tuple], u32 param[Tuple]) |
| { |
{ |
| s32 i, s; |
s32 i, s; |
| for (i=Tuple-1; i>=0; i--) { |
for (i=Tuple-1; i>=0; i--) { |
| s = ((param[i] >> (32 - 4)) | 0x10 ) & 0x17; |
s = ((param[i] >> (32 - 4)) | 0x10 ) & 0x17; |
| block[i] ^= ((~block[i]) >> s); |
block[i] ^= ((~block[i]) >> s); |
| block[(i+jump) & Low_Mask] -= add_table[block[i] >> (32 - Log_Add_Size)]; |
block[(i+ctx->jump) & Low_Mask] -= ctx->add_table[block[i] >> (32 - Log_Add_Size)]; |
| block[i] *= inv_table[param[(i+2) % Tuple]>> (32 - 5)]; |
block[i] *= ctx->inv_table[param[(i+2) % Tuple]>> (32 - 5)]; |
| block[i] ^= param[i]; |
block[i] ^= param[i]; |
| } |
} |
| } |
} |
| |
|
| /* word wise encrypt by Exor Mult table-Exor Shift */ |
/* word wise encrypt by Exor Mult table-Exor Shift */ |
| /* Shift number is betwee 16 - 23 */ |
/* Shift number is betwee 16 - 23 */ |
| void crypt_emes(u32 block[Tuple]) |
void crypt_emes(ECRYPT_ctx* ctx, u32 block[Tuple]) |
| { |
{ |
| s32 i, s; |
s32 i, s; |
| u32 param[Tuple]; |
u32 param[Tuple]; |
| genrand_tuple_int32(param, Tuple); |
genrand_tuple_int32(ctx, param, Tuple); |
| for (i=0; i<Tuple; i++) { |
for (i=0; i<Tuple; i++) { |
| s = ((param[i] >> (32 - 4)) | 0x10 ) & 0x17; |
s = ((param[i] >> (32 - 4)) | 0x10 ) & 0x17; |
| block[i] ^= param[i]; |
block[i] ^= param[i]; |
| block[i] *= multi_table[param[(i+3) % Tuple]>> (32 - 5)]; |
block[i] *= ctx->multi_table[param[(i+3) % Tuple]>> (32 - 5)]; |
| block[(i+jump) & Low_Mask] ^= add_table[block[i] >> (32 - Log_Add_Size)]; |
block[(i+ctx->jump) & Low_Mask] ^= ctx->add_table[block[i] >> (32 - Log_Add_Size)]; |
| block[i] ^= ((~block[i]) >> s); |
block[i] ^= ((~block[i]) >> s); |
| } |
} |
| } |
} |
| |
|
| void crypt_emes_inv(u32 block[Tuple], u32 param[Tuple]) |
void crypt_emes_inv(ECRYPT_ctx* ctx, u32 block[Tuple], u32 param[Tuple]) |
| { |
{ |
| s32 i, s; |
s32 i, s; |
| for (i=Tuple-1; i>=0; i--) { |
for (i=Tuple-1; i>=0; i--) { |
| s = ((param[i] >> (32 - 4)) | 0x10 ) & 0x17; |
s = ((param[i] >> (32 - 4)) | 0x10 ) & 0x17; |
| block[i] ^= ((~block[i]) >> s); |
block[i] ^= ((~block[i]) >> s); |
| block[(i+jump) & Low_Mask] ^= add_table[block[i] >> (32 - Log_Add_Size)]; |
block[(i+ctx->jump) & Low_Mask] ^= ctx->add_table[block[i] >> (32 - Log_Add_Size)]; |
| block[i] *= inv_table[param[(i+3) % Tuple]>> (32 - 5)]; |
block[i] *= ctx->inv_table[param[(i+3) % Tuple]>> (32 - 5)]; |
| block[i] ^= param[i]; |
block[i] ^= param[i]; |
| } |
} |
| } |
} |
| |
|
| /* Inter-word operations */ |
/* Inter-word operations */ |
| /* multiply to one and add to the other */ |
/* multiply to one and add to the other */ |
| void crypt_ma(u32 block[Tuple]) |
void crypt_ma(ECRYPT_ctx* ctx, u32 block[Tuple]) |
| { |
{ |
| s32 i, j, s; |
s32 i, j, s; |
| u32 param[Tuple]; |
u32 param[Tuple]; |
| genrand_tuple_int32(param, Tuple); |
genrand_tuple_int32(ctx, param, Tuple); |
| |
|
| for (i=0; i<Tuple; i++) { |
for (i=0; i<Tuple; i++) { |
| j = (i - jump) & Low_Mask; |
j = (i - ctx->jump) & Low_Mask; |
| s = ((param[j] >> (32 - 4)) | 0x10 ) & 0x17; |
s = ((param[j] >> (32 - 4)) | 0x10 ) & 0x17; |
| block[i] += (block[j]*param[i]); |
block[i] += (block[j]*param[i]); |
| block[i] ^= ((~block[i]) >> s); |
block[i] ^= ((~block[i]) >> s); |
| } |
} |
| } |
} |
| |
|
| void crypt_ma_inv(u32 block[Tuple], u32 param[Tuple]) |
void crypt_ma_inv(ECRYPT_ctx* ctx, u32 block[Tuple], u32 param[Tuple]) |
| { |
{ |
| s32 i, j, s; |
s32 i, j, s; |
| |
|
| for (i=Tuple-1; i>=0; i--) { |
for (i=Tuple-1; i>=0; i--) { |
| j = (i - jump) & Low_Mask; |
j = (i - ctx->jump) & Low_Mask; |
| s = ((param[j] >> (32 - 4)) | 0x10 ) & 0x17; |
s = ((param[j] >> (32 - 4)) | 0x10 ) & 0x17; |
| block[i] ^= ((~block[i]) >> s); |
block[i] ^= ((~block[i]) >> s); |
| block[i] -= (block[j]*param[i]); |
block[i] -= (block[j]*param[i]); |
| } |
} |
| |
|
| /* multiply two words, exor to another words, and minus */ |
/* multiply two words, exor to another words, and minus */ |
| void crypt_mem(u32 block[Tuple]) |
void crypt_mem(ECRYPT_ctx* ctx, u32 block[Tuple]) |
| { |
{ |
| s32 i, j, k; |
s32 i, j, k; |
| u32 param[Tuple]; |
u32 param[Tuple]; |
| genrand_tuple_int32(param, Tuple); |
genrand_tuple_int32(ctx, param, Tuple); |
| |
|
| for (i=0; i<Tuple; i++) { |
for (i=0; i<Tuple; i++) { |
| j = (i - jump) & Low_Mask; |
j = (i - ctx->jump) & Low_Mask; |
| k = param[j] >> (32 - Log_Tuple); |
k = param[j] >> (32 - Log_Tuple); |
| if (k==i) k=(k-1) & Low_Mask; |
if (k==i) k=(k-1) & Low_Mask; |
| block[i] ^= (block[j]*block[k]); |
block[i] ^= (block[j]*block[k]); |
| } |
} |
| } |
} |
| |
|
| void crypt_mem_inv(u32 block[Tuple], u32 param[Tuple]) |
void crypt_mem_inv(ECRYPT_ctx* ctx, u32 block[Tuple], u32 param[Tuple]) |
| { |
{ |
| s32 i, j, k; |
s32 i, j, k; |
| |
|
| for (i=Tuple-1; i>=0; i--) { |
for (i=Tuple-1; i>=0; i--) { |
| j = (i - jump) & Low_Mask; |
j = (i - ctx->jump) & Low_Mask; |
| k = param[j] >> (32 - Log_Tuple); |
k = param[j] >> (32 - Log_Tuple); |
| if (k==i) k=(k-1) & Low_Mask; |
if (k==i) k=(k-1) & Low_Mask; |
| block[i] ^= (block[i] >> 16); |
block[i] ^= (block[i] >> 16); |
| } |
} |
| |
|
| /* (one word OR param) times another word) is EXORed to another */ |
/* (one word OR param) times another word) is EXORed to another */ |
| void crypt_ome(u32 block[Tuple]) |
void crypt_ome(ECRYPT_ctx* ctx, u32 block[Tuple]) |
| { |
{ |
| s32 i, j, k; |
s32 i, j, k; |
| u32 param[Tuple]; |
u32 param[Tuple]; |
| genrand_tuple_int32(param, Tuple); |
genrand_tuple_int32(ctx, param, Tuple); |
| |
|
| for (i=0; i<Tuple; i++) { |
for (i=0; i<Tuple; i++) { |
| j = (i - jump) & Low_Mask; |
j = (i - ctx->jump) & Low_Mask; |
| k = param[j] >> (32 - Log_Tuple); |
k = param[j] >> (32 - Log_Tuple); |
| if (k==i) k=(k-1) & Low_Mask; |
if (k==i) k=(k-1) & Low_Mask; |
| block[i] ^= (block[k]|param[i])*block[j]; |
block[i] ^= (block[k]|param[i])*block[j]; |
| } |
} |
| } |
} |
| |
|
| void crypt_ome_inv(u32 block[Tuple], u32 param[Tuple]) |
void crypt_ome_inv(ECRYPT_ctx* ctx, u32 block[Tuple], u32 param[Tuple]) |
| { |
{ |
| s32 i, j, k; |
s32 i, j, k; |
| |
|
| for (i=Tuple-1; i>=0; i--) { |
for (i=Tuple-1; i>=0; i--) { |
| j = (i - jump) & Low_Mask; |
j = (i - ctx->jump) & Low_Mask; |
| k = param[j] >> (32 - Log_Tuple); |
k = param[j] >> (32 - Log_Tuple); |
| if (k==i) k=(k-1) & Low_Mask; |
if (k==i) k=(k-1) & Low_Mask; |
| block[i] ^= (block[i] >> 16); |
block[i] ^= (block[i] >> 16); |
| |
|
| /* (one word EXOR param) times another word) is EXORed to another */ |
/* (one word EXOR param) times another word) is EXORed to another */ |
| |
|
| void crypt_eme(u32 block[Tuple]) |
void crypt_eme(ECRYPT_ctx* ctx, u32 block[Tuple]) |
| { |
{ |
| s32 i, j, k; |
s32 i, j, k; |
| u32 param[Tuple]; |
u32 param[Tuple]; |
| genrand_tuple_int32(param, Tuple); |
genrand_tuple_int32(ctx, param, Tuple); |
| |
|
| for (i=0; i<Tuple; i++) { |
for (i=0; i<Tuple; i++) { |
| j = (i - jump) & Low_Mask; |
j = (i - ctx->jump) & Low_Mask; |
| k = param[j] >> (32 - Log_Tuple); |
k = param[j] >> (32 - Log_Tuple); |
| if (k==i) k=(k-1) & Low_Mask; |
if (k==i) k=(k-1) & Low_Mask; |
| block[i] ^= (block[k]^param[i])*block[j]; |
block[i] ^= (block[k]^param[i])*block[j]; |
| } |
} |
| } |
} |
| |
|
| void crypt_eme_inv(u32 block[Tuple], u32 param[Tuple]) |
void crypt_eme_inv(ECRYPT_ctx* ctx, u32 block[Tuple], u32 param[Tuple]) |
| { |
{ |
| s32 i, j, k; |
s32 i, j, k; |
| |
|
| for (i=Tuple-1; i>=0; i--) { |
for (i=Tuple-1; i>=0; i--) { |
| j = (i - jump) & Low_Mask; |
j = (i - ctx->jump) & Low_Mask; |
| k = param[j] >> (32 - Log_Tuple); |
k = param[j] >> (32 - Log_Tuple); |
| if (k==i) k=(k-1) & Low_Mask; |
if (k==i) k=(k-1) & Low_Mask; |
| block[i] ^= (block[i] >> 17); |
block[i] ^= (block[i] >> 17); |
| } |
} |
| |
|
| /* vertical partial rotation with bit inversion*/ |
/* vertical partial rotation with bit inversion*/ |
| void crypt_vert_rotate(u32 block[Tuple]) |
void crypt_vert_rotate(ECRYPT_ctx* ctx, u32 block[Tuple]) |
| { |
{ |
| u32 key, rkey, s; |
u32 key, rkey, s; |
| s32 i, j, jump_odd; |
s32 i, j, jump_odd; |
| u32 param[Tuple]; |
u32 param[Tuple]; |
| |
|
| jump_odd = (jump - 1) | 0x1; |
jump_odd = (ctx->jump - 1) | 0x1; |
| |
|
| genrand_tuple_int32(param, Tuple); |
genrand_tuple_int32(ctx, param, Tuple); |
| |
|
| key = ((param[0]+param[Tuple-1])<< 2) + 1; |
key = ((param[0]+param[Tuple-1])<< 2) + 1; |
| rkey = ~key; |
rkey = ~key; |
| } |
} |
| } |
} |
| |
|
| void crypt_vert_rotate_inv(u32 block[Tuple], u32 param[Tuple]) |
void crypt_vert_rotate_inv(ECRYPT_ctx* ctx, u32 block[Tuple], u32 param[Tuple]) |
| { |
{ |
| u32 key, rkey, s; |
u32 key, rkey, s; |
| s32 i, j, jump_odd; |
s32 i, j, jump_odd; |
| |
|
| jump_odd = (jump - 1)| 0x1; |
jump_odd = (ctx->jump - 1)| 0x1; |
| |
|
| for (i=0; i<Tuple; i++) { |
for (i=0; i<Tuple; i++) { |
| block[i] -= param[i]; |
block[i] -= param[i]; |
| } |
} |
| |
|
| |
|
| void hmnencode(const u8* plaintext, u8* ciphertext, u32 msglen) /* Message length in bytes. */ |
void hmnencode(ECRYPT_ctx* ctx, const u8* plaintext, u8* ciphertext, u32 msglen) /* Message length in bytes. */ |
| |
|
| { |
{ |
| s32 i, j, repeat; |
s32 i, j, repeat; |
| set_buf(msgbuf, plaintext, cinpos, msglen); |
set_buf(msgbuf, plaintext, cinpos, msglen); |
| cinpos += 4*Tuple; |
cinpos += 4*Tuple; |
| |
|
| genrand_tuple_int32(func_choice, 4); |
genrand_tuple_int32(ctx, func_choice, 4); |
| |
|
| func_choice[2] *= (func_choice[0] | 0x1UL); |
func_choice[2] *= (func_choice[0] | 0x1UL); |
| func_choice[3] *= (func_choice[1] | 0x1UL); |
func_choice[3] *= (func_choice[1] | 0x1UL); |
| func_choice[0] ^= (func_choice[3] >> 5); |
func_choice[0] ^= (func_choice[3] >> 5); |
| func_choice[1] ^= (func_choice[2] >> 5); |
func_choice[1] ^= (func_choice[2] >> 5); |
| |
|
| jump = 1; |
ctx->jump = 1; |
| for (j=0; j< 2*Iteration;) { |
for (j=0; j< 2*Iteration;) { |
| s32 c, t; |
s32 c, t; |
| |
|
| t = j >> 4; |
t = j >> 4; |
| c = (func_choice[t] >> ((j++ & 0xfUL) * 2)) & 0x3UL; |
c = (func_choice[t] >> ((j++ & 0xfUL) * 2)) & 0x3UL; |
| switch (c) { |
switch (c) { |
| case 0: crypt_empr(msgbuf); break; |
case 0: crypt_empr(ctx, msgbuf); break; |
| case 1: crypt_emer(msgbuf); break; |
case 1: crypt_emer(ctx, msgbuf); break; |
| case 2: crypt_emps(msgbuf); break; |
case 2: crypt_emps(ctx, msgbuf); break; |
| case 3: crypt_emes(msgbuf); break; |
case 3: crypt_emes(ctx, msgbuf); break; |
| } |
} |
| |
|
| |
|
| if ((jump <<= 1) >= Tuple) jump = 1; |
if ((ctx->jump <<= 1) >= Tuple) ctx->jump = 1; |
| |
|
| t = j >> 4; |
t = j >> 4; |
| c = (func_choice[t] >> ((j++ & 0xfUL) * 2)) & 0x3UL; |
c = (func_choice[t] >> ((j++ & 0xfUL) * 2)) & 0x3UL; |
| |
|
| switch (c) { |
switch (c) { |
| case 0: crypt_ma(msgbuf); break; |
case 0: crypt_ma(ctx, msgbuf); break; |
| case 1: crypt_mem(msgbuf); break; |
case 1: crypt_mem(ctx, msgbuf); break; |
| case 2: crypt_ome(msgbuf); break; |
case 2: crypt_ome(ctx, msgbuf); break; |
| case 3: crypt_eme(msgbuf); break; |
case 3: crypt_eme(ctx, msgbuf); break; |
| } |
} |
| if ((jump <<= 1) >= Tuple) jump = 1; |
if ((ctx->jump <<= 1) >= Tuple) ctx->jump = 1; |
| |
|
| crypt_vert_rotate(msgbuf); |
crypt_vert_rotate(ctx, msgbuf); |
| if ((jump <<= 1) >= Tuple) jump = 1; |
if ((ctx->jump <<= 1) >= Tuple) ctx->jump = 1; |
| } |
} |
| |
|
| set_array(msgbuf, ciphertext, coutpos); |
set_array(msgbuf, ciphertext, coutpos); |
| } |
} |
| } |
} |
| |
|
| void hmndecode(const u8* ciphertext, u8* plaintext, u32 msglen) /* Message length in bytes. */ |
void hmndecode(ECRYPT_ctx* ctx, const u8* ciphertext, u8* plaintext, u32 msglen) /* Message length in bytes. */ |
| { |
{ |
| s32 i, j, k, repeat; |
s32 i, j, k, repeat; |
| u32 temp_rand[3*Iteration][Tuple]; |
u32 temp_rand[3*Iteration][Tuple]; |
| set_buf(msgbuf, ciphertext, cinpos, msglen); |
set_buf(msgbuf, ciphertext, cinpos, msglen); |
| cinpos += 4*Tuple; |
cinpos += 4*Tuple; |
| |
|
| genrand_tuple_int32(func_choice, 4); |
genrand_tuple_int32(ctx, func_choice, 4); |
| |
|
| func_choice[2] *= (func_choice[0] | 0x1UL); |
func_choice[2] *= (func_choice[0] | 0x1UL); |
| func_choice[3] *= (func_choice[1] | 0x1UL); |
func_choice[3] *= (func_choice[1] | 0x1UL); |
| func_choice[1] ^= (func_choice[2] >> 5); |
func_choice[1] ^= (func_choice[2] >> 5); |
| |
|
| for (k=0; k< 3*Iteration; k++) |
for (k=0; k< 3*Iteration; k++) |
| genrand_tuple_int32(temp_rand[k], Tuple); |
genrand_tuple_int32(ctx, temp_rand[k], Tuple); |
| |
|
| jump = 1 << ((3*Iteration-1) % Log_Tuple); |
ctx->jump = 1 << ((3*Iteration-1) % Log_Tuple); |
| |
|
| for (j=2*Iteration -1; j>=0;) { |
for (j=2*Iteration -1; j>=0;) { |
| s32 c, t; |
s32 c, t; |
| t = j >> 4; |
t = j >> 4; |
| c = (func_choice[t] >> ((j-- & 0xfUL) * 2)) & 0x3UL; |
c = (func_choice[t] >> ((j-- & 0xfUL) * 2)) & 0x3UL; |
| |
|
| crypt_vert_rotate_inv(msgbuf,temp_rand[--k]); |
crypt_vert_rotate_inv(ctx, msgbuf,temp_rand[--k]); |
| if ((jump >>= 1) == 0) jump = Tuple >> 1; |
if ((ctx->jump >>= 1) == 0) ctx->jump = Tuple >> 1; |
| |
|
| switch (c) { |
switch (c) { |
| case 0: crypt_ma_inv(msgbuf,temp_rand[--k]); break; |
case 0: crypt_ma_inv(ctx, msgbuf,temp_rand[--k]); break; |
| case 1: crypt_mem_inv(msgbuf,temp_rand[--k]); break; |
case 1: crypt_mem_inv(ctx, msgbuf,temp_rand[--k]); break; |
| case 2: crypt_ome_inv(msgbuf,temp_rand[--k]); break; |
case 2: crypt_ome_inv(ctx, msgbuf,temp_rand[--k]); break; |
| case 3: crypt_eme_inv(msgbuf,temp_rand[--k]); break; |
case 3: crypt_eme_inv(ctx, msgbuf,temp_rand[--k]); break; |
| } |
} |
| if ((jump >>= 1) == 0) jump = Tuple >> 1; |
if ((ctx->jump >>= 1) == 0) ctx->jump = Tuple >> 1; |
| |
|
| t = j >> 4; |
t = j >> 4; |
| c = (func_choice[t] >> ((j-- & 0xfUL) * 2)) & 0x3UL; |
c = (func_choice[t] >> ((j-- & 0xfUL) * 2)) & 0x3UL; |
| |
|
| switch (c) { |
switch (c) { |
| case 0: crypt_empr_inv(msgbuf,temp_rand[--k]); break; |
case 0: crypt_empr_inv(ctx, msgbuf,temp_rand[--k]); break; |
| case 1: crypt_emer_inv(msgbuf,temp_rand[--k]); break; |
case 1: crypt_emer_inv(ctx, msgbuf,temp_rand[--k]); break; |
| case 2: crypt_emps_inv(msgbuf,temp_rand[--k]); break; |
case 2: crypt_emps_inv(ctx, msgbuf,temp_rand[--k]); break; |
| case 3: crypt_emes_inv(msgbuf,temp_rand[--k]); break; |
case 3: crypt_emes_inv(ctx, msgbuf,temp_rand[--k]); break; |
| } |
} |
| if ((jump >>= 1) == 0) jump = Tuple >> 1; |
if ((ctx->jump >>= 1) == 0) ctx->jump = Tuple >> 1; |
| } |
} |
| |
|
| set_array(msgbuf, plaintext, coutpos); |
set_array(msgbuf, plaintext, coutpos); |
| } |
} |
| init_array[t+(s++)] = x; |
init_array[t+(s++)] = x; |
| } |
} |
| init_by_array(init_array, t+s); |
init_by_array(ctx, init_array, t+s); |
| |
|
| prepare_multi(); |
prepare_multi(ctx); |
| prepare_multi_inv(); |
prepare_multi_inv(ctx); |
| prepare_add_table(); |
prepare_add_table(ctx); |
| } |
} |
| |
|
| void ECRYPT_keystream_bytes( |
void ECRYPT_keystream_bytes( |
| |
|
| for (j=0; j<Tuple; j++) msgbuf[j] = 0; |
for (j=0; j<Tuple; j++) msgbuf[j] = 0; |
| |
|
| genrand_tuple_int32(func_choice, 4); |
genrand_tuple_int32(ctx, func_choice, 4); |
| |
|
| func_choice[2] *= (func_choice[0] | 0x1UL); |
func_choice[2] *= (func_choice[0] | 0x1UL); |
| func_choice[3] *= (func_choice[1] | 0x1UL); |
func_choice[3] *= (func_choice[1] | 0x1UL); |
| t = j >> 4; |
t = j >> 4; |
| c = (func_choice[t] >> ((j++ & 0xfUL) * 2)) & 0x3UL; |
c = (func_choice[t] >> ((j++ & 0xfUL) * 2)) & 0x3UL; |
| switch (c) { |
switch (c) { |
| case 0: crypt_empr(msgbuf); break; |
case 0: crypt_empr(ctx, msgbuf); break; |
| case 1: crypt_emer(msgbuf); break; |
case 1: crypt_emer(ctx, msgbuf); break; |
| case 2: crypt_emps(msgbuf); break; |
case 2: crypt_emps(ctx, msgbuf); break; |
| case 3: crypt_emes(msgbuf); break; |
case 3: crypt_emes(ctx, msgbuf); break; |
| } |
} |
| |
|
| t = j >> 4; |
t = j >> 4; |
| c = (func_choice[t] >> ((j++ & 0xfUL) * 2)) & 0x3UL; |
c = (func_choice[t] >> ((j++ & 0xfUL) * 2)) & 0x3UL; |
| |
|
| switch (c) { |
switch (c) { |
| case 0: crypt_ma(msgbuf); break; |
case 0: crypt_ma(ctx, msgbuf); break; |
| case 1: crypt_mem(msgbuf); break; |
case 1: crypt_mem(ctx, msgbuf); break; |
| case 2: crypt_ome(msgbuf); break; |
case 2: crypt_ome(ctx, msgbuf); break; |
| case 3: crypt_eme(msgbuf); break; |
case 3: crypt_eme(ctx, msgbuf); break; |
| } |
} |
| crypt_vert_rotate(msgbuf); |
crypt_vert_rotate(ctx, msgbuf); |
| } |
} |
| |
|
| set_array(msgbuf, keystream, coutpos); |
set_array(msgbuf, keystream, coutpos); |
| u8* ciphertext, |
u8* ciphertext, |
| u32 msglen) /* Message length in bytes. */ |
u32 msglen) /* Message length in bytes. */ |
| { |
{ |
| hmnencode(plaintext, ciphertext, msglen); |
hmnencode(ctx, plaintext, ciphertext, msglen); |
| } |
} |
| |
|
| void ECRYPT_decrypt_bytes( |
void ECRYPT_decrypt_bytes( |
| printf("ECRYPT_decrypt_bytes: msglen should be multiple of %d.\n", 4*Tuple); |
printf("ECRYPT_decrypt_bytes: msglen should be multiple of %d.\n", 4*Tuple); |
| return; |
return; |
| } |
} |
| hmndecode(ciphertext, plaintext, msglen); |
hmndecode(ctx, ciphertext, plaintext, msglen); |
| } |
} |
| |
|
| |
#ifndef ECRYPT_API |
| |
|
| int main(void) |
int main(void) |
| { |
{ |
| |
|
| return 0; |
return 0; |
| } |
} |
| |
|
| |
#endif |