/* IDEA(International Data Encryption Algorithm), refer to http://www.quadibloc.com/crypto/co040302.htm
* IDEA.c, an IDEA encryption and decryption program.
* Author shenyang
* Mar. 4th, 2011
* TODO: Fault analysis on IDEA, defence of fault analysis on IDEA.
*/
#ifndef IDEA_H
#include "IDEA.h"
#endif
#include
#include
/* define operation */
staticuint16_t add_mod(uint16_t a, uint16_t b);
staticuint16_t mp_mod(uint16_t a,uint16_t b);
staticuint16_t XOR(uint16_t a, uint16_t b);
staticstatus_t left_shift(uint16_t key[8],intnum);
staticvoidswap(uint16_t *a, uint16_t *b);
/* addition and mod 65536 */
staticuint16_t add_mod(uint16_t a, uint16_t b)
{
uint32_t tmp = a+b;
uint16_t ret = tmp % IDEA_ADD_MODULAR;
returnret;
}
/* multiply and mod 65537 */
staticuint16_t mp_mod(uint16_t a,uint16_t b)
{
/* Note: In IDEA, for purposes of multiplication, a 16 bit word containing all zeroes is considered to represent the number 65,536;
* other numbers are represented in conventional unsigned notation, and multiplication is modulo the prime number 65,537
*/
uint64_t tmp, tmp_a, tmp_b; //if both a and b are 2^16, the result will be 2^32 which will exceed a 32-bit int
tmp_a = a==0 ? (1<<16) : a;
tmp_b = b==0 ? (1<<16) : b;
tmp = (tmp_a * tmp_b) % IDEA_MP_MODULAR;
return(uint16_t)(tmp);
}
/* XOR */
staticuint16_t XOR(uint16_t a, uint16_t b)
{
returna^b;
}
staticvoidswap(uint16_t *a, uint16_t *b)
{
uint16_t c = 0;
c = *a;
*a = *b;
*b = c;
}
/* IDEA encryption */
status_t idea_encrypt(uint64_t