Ejemplo n.º 1
0
int safer_ecb_decrypt(const unsigned char *block_in,
                             unsigned char *block_out,
                             symmetric_key *skey)
#endif
{   unsigned char a, b, c, d, e, f, g, h, t;
    unsigned int round;
    unsigned char *key;

    LTC_ARGCHK(block_in != NULL);
    LTC_ARGCHK(block_out != NULL);
    LTC_ARGCHK(skey != NULL);

    key = skey->safer.key;
    a = block_in[0]; b = block_in[1]; c = block_in[2]; d = block_in[3];
    e = block_in[4]; f = block_in[5]; g = block_in[6]; h = block_in[7];
    if (LTC_SAFER_MAX_NOF_ROUNDS < (round = *key)) round = LTC_SAFER_MAX_NOF_ROUNDS;
    key += LTC_SAFER_BLOCK_LEN * (1 + 2 * round);
    h ^= *key; g -= *--key; f -= *--key; e ^= *--key;
    d ^= *--key; c -= *--key; b -= *--key; a ^= *--key;
    while (round--)
    {
        t = e; e = b; b = c; c = t; t = f; f = d; d = g; g = t;
        IPHT(a, e); IPHT(b, f); IPHT(c, g); IPHT(d, h);
        IPHT(a, c); IPHT(e, g); IPHT(b, d); IPHT(f, h);
        IPHT(a, b); IPHT(c, d); IPHT(e, f); IPHT(g, h);
        h -= *--key; g ^= *--key; f ^= *--key; e -= *--key;
        d -= *--key; c ^= *--key; b ^= *--key; a -= *--key;
        h = LOG(h) ^ *--key; g = EXP(g) - *--key;
        f = EXP(f) - *--key; e = LOG(e) ^ *--key;
        d = LOG(d) ^ *--key; c = EXP(c) - *--key;
        b = EXP(b) - *--key; a = LOG(a) ^ *--key;
    }
    block_out[0] = a & 0xFF; block_out[1] = b & 0xFF;
    block_out[2] = c & 0xFF; block_out[3] = d & 0xFF;
    block_out[4] = e & 0xFF; block_out[5] = f & 0xFF;
    block_out[6] = g & 0xFF; block_out[7] = h & 0xFF;
    return CRYPT_OK;
}
Ejemplo n.º 2
0
void SAFER::Decrypt(const byte *block_in, byte *block_out) const
{
    byte a, b, c, d, e, f, g, h, t;
    unsigned int round = keySchedule[0];
    const byte *key = keySchedule + BLOCKSIZE * (1 + 2 * round) - 7;

    a = block_in[0];
    b = block_in[1];
    c = block_in[2];
    d = block_in[3];
    e = block_in[4];
    f = block_in[5];
    g = block_in[6];
    h = block_in[7];
    h ^= key[7];
    g -= key[6];
    f -= key[5];
    e ^= key[4];
    d ^= key[3];
    c -= key[2];
    b -= key[1];
    a ^= key[0];
    while (round--)
    {
        key -= 16;
        t = e;
        e = b;
        b = c;
        c = t;
        t = f;
        f = d;
        d = g;
        g = t;
        IPHT(a, e);
        IPHT(b, f);
        IPHT(c, g);
        IPHT(d, h);
        IPHT(a, c);
        IPHT(e, g);
        IPHT(b, d);
        IPHT(f, h);
        IPHT(a, b);
        IPHT(c, d);
        IPHT(e, f);
        IPHT(g, h);
        h -= key[15];
        g ^= key[14];
        f ^= key[13];
        e -= key[12];
        d -= key[11];
        c ^= key[10];
        b ^= key[9];
        a -= key[8];
        h = LOG(h) ^ key[7];
        g = EXP(g) - key[6];
        f = EXP(f) - key[5];
        e = LOG(e) ^ key[4];
        d = LOG(d) ^ key[3];
        c = EXP(c) - key[2];
        b = EXP(b) - key[1];
        a = LOG(a) ^ key[0];
    }
    block_out[0] = a & 0xFF;
    block_out[1] = b & 0xFF;
    block_out[2] = c & 0xFF;
    block_out[3] = d & 0xFF;
    block_out[4] = e & 0xFF;
    block_out[5] = f & 0xFF;
    block_out[6] = g & 0xFF;
    block_out[7] = h & 0xFF;
}
Ejemplo n.º 3
0
Archivo: safer.cpp Proyecto: c3d/tao-3D
void SAFER::Dec::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
{
	byte a, b, c, d, e, f, g, h, t;
	unsigned int round = keySchedule[0];
	const byte *key = keySchedule + BLOCKSIZE * (1 + 2 * round) - 7;

	Block::Get(inBlock)(a)(b)(c)(d)(e)(f)(g)(h);
	h ^= key[7]; g -= key[6]; f -= key[5]; e ^= key[4];
	d ^= key[3]; c -= key[2]; b -= key[1]; a ^= key[0];
	while (round--)
	{
		key -= 16;
		t = e; e = b; b = c; c = t; t = f; f = d; d = g; g = t;
		IPHT(a, e); IPHT(b, f); IPHT(c, g); IPHT(d, h);
		IPHT(a, c); IPHT(e, g); IPHT(b, d); IPHT(f, h);
		IPHT(a, b); IPHT(c, d); IPHT(e, f); IPHT(g, h);
		h -= key[15]; g ^= key[14]; f ^= key[13]; e -= key[12];
		d -= key[11]; c ^= key[10]; b ^= key[9]; a -= key[8];
		h = LOG(h) ^ key[7]; g = EXP(g) - key[6];
		f = EXP(f) - key[5]; e = LOG(e) ^ key[4];
		d = LOG(d) ^ key[3]; c = EXP(c) - key[2];
		b = EXP(b) - key[1]; a = LOG(a) ^ key[0];
	}
	Block::Put(xorBlock, outBlock)(a)(b)(c)(d)(e)(f)(g)(h);
}
Ejemplo n.º 4
0
WIN32DLL_DEFINE
    void _mcrypt_decrypt(const safer_key_t * key, safer_block_t * block_in)
{
	safer_block_t a, b, c, d, e, f, g, h, t;
	unsigned int round;
	a = block_in[0];
	b = block_in[1];
	c = block_in[2];
	d = block_in[3];
	e = block_in[4];
	f = block_in[5];
	g = block_in[6];
	h = block_in[7];
	if (SAFER_MAX_NOF_ROUNDS < (round = *key))
		round = SAFER_MAX_NOF_ROUNDS;
	key += SAFER_BLOCK_LEN * (1 + 2 * round);
	h ^= *key;
	g -= *--key;
	f -= *--key;
	e ^= *--key;
	d ^= *--key;
	c -= *--key;
	b -= *--key;
	a ^= *--key;
	while (round--) {
		t = e;
		e = b;
		b = c;
		c = t;
		t = f;
		f = d;
		d = g;
		g = t;
		IPHT(a, e);
		IPHT(b, f);
		IPHT(c, g);
		IPHT(d, h);
		IPHT(a, c);
		IPHT(e, g);
		IPHT(b, d);
		IPHT(f, h);
		IPHT(a, b);
		IPHT(c, d);
		IPHT(e, f);
		IPHT(g, h);
		h -= *--key;
		g ^= *--key;
		f ^= *--key;
		e -= *--key;
		d -= *--key;
		c ^= *--key;
		b ^= *--key;
		a -= *--key;
		h = LOG(h) ^ *--key;
		g = EXP(g) - *--key;
		f = EXP(f) - *--key;
		e = LOG(e) ^ *--key;
		d = LOG(d) ^ *--key;
		c = EXP(c) - *--key;
		b = EXP(b) - *--key;
		a = LOG(a) ^ *--key;
	}
	block_in[0] = a & 0xFF;
	block_in[1] = b & 0xFF;
	block_in[2] = c & 0xFF;
	block_in[3] = d & 0xFF;
	block_in[4] = e & 0xFF;
	block_in[5] = f & 0xFF;
	block_in[6] = g & 0xFF;
	block_in[7] = h & 0xFF;
}				/* Safer_Decrypt_Block */