X-Git-Url: https://git.cryptolib.org/?a=blobdiff_plain;f=serpent.c;h=6506b2ad2b70495db93c40054bbf997ecf1bf2d3;hb=17332291e15183d71d88ed868275e3cb53917180;hp=c90b099e36848421b2b1b53d00ccf884c401ad39;hpb=c1553054f9455c2f88d6b3cb44de1cc25674a528;p=avr-crypto-lib.git diff --git a/serpent.c b/serpent.c index c90b099..6506b2a 100644 --- a/serpent.c +++ b/serpent.c @@ -1,6 +1,6 @@ /* serpent.c */ /* - This file is part of the Crypto-avr-lib/microcrypt-lib. + This file is part of the This file is part of the AVR-Crypto-Lib. Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de) This program is free software: you can redistribute it and/or modify @@ -47,7 +47,7 @@ uint32_t rotr32(uint32_t a, uint8_t n){ #define X2 (((uint32_t*)b)[2]) #define X3 (((uint32_t*)b)[3]) -static void lt(uint8_t *b){ +static void serpent_lt(uint8_t *b){ X0 = rotl32(X0, 13); X2 = rotl32(X2, 3); X1 ^= X0 ^ X2; @@ -60,7 +60,7 @@ static void lt(uint8_t *b){ X2 = rotr32(X2, 10); } -static void inv_lt(uint8_t *b){ +static void serpent_inv_lt(uint8_t *b){ X2 = rotl32(X2, 10); X0 = rotr32(X0, 5); X2 ^= X3 ^ (X1 << 7); @@ -75,29 +75,28 @@ static void inv_lt(uint8_t *b){ #define GOLDEN_RATIO 0x9e3779b9l -static uint32_t gen_w(uint32_t * b, uint8_t i){ +static uint32_t serpent_gen_w(uint32_t * b, uint8_t i){ uint32_t ret; ret = b[0] ^ b[3] ^ b[5] ^ b[7] ^ GOLDEN_RATIO ^ (uint32_t)i; ret = rotl32(ret, 11); return ret; } -/* key must be 256bit (32 byte) large! */ -void serpent_init(const void* key, uint16_t keysize, serpent_ctx_t* ctx){ +void serpent_init(const void* key, uint16_t keysize_b, serpent_ctx_t* ctx){ uint32_t buffer[8]; uint8_t i,j; - if(keysize<256){ + if(keysize_b<256){ /* keysize is less than 256 bit, padding needed */ memset(buffer, 0, 32); - memcpy(buffer, key, (keysize+7)/8); - ((uint8_t*)buffer)[keysize/8] |= 1<<(keysize%8); + memcpy(buffer, key, (keysize_b+7)/8); + ((uint8_t*)buffer)[keysize_b/8] |= 1<<(keysize_b%8); } else { /* keysize is 256 bit */ memcpy(buffer, key, 32); } for(i=0; i<33; ++i){ for(j=0; j<4; ++j){ - ctx->k[i][j] = gen_w(buffer, i*4+j); + ctx->k[i][j] = serpent_gen_w(buffer, i*4+j); memmove(buffer, &(buffer[1]), 7*4); /* shift buffer one to the "left" */ buffer[7] = ctx->k[i][j]; } @@ -107,13 +106,12 @@ void serpent_init(const void* key, uint16_t keysize, serpent_ctx_t* ctx){ } } - void serpent_enc(void* buffer, const serpent_ctx_t* ctx){ uint8_t i; for(i=0; i<31; ++i){ memxor(buffer, ctx->k[i], 16); sbox128(buffer, i); - lt((uint8_t*)buffer); + serpent_lt((uint8_t*)buffer); } memxor(buffer, ctx->k[i], 16); sbox128(buffer, i); @@ -130,7 +128,7 @@ void serpent_dec(void* buffer, const serpent_ctx_t* ctx){ memxor((uint8_t*)buffer, ctx->k[i], 16); --i; for(; i>=0; --i){ - inv_lt(buffer); + serpent_inv_lt(buffer); inv_sbox128(buffer, i); memxor(buffer, ctx->k[i], 16); } @@ -140,4 +138,3 @@ void serpent_dec(void* buffer, const serpent_ctx_t* ctx){ -