X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?p=secnet.git;a=blobdiff_plain;f=serpent.c;h=6407c5776624eef4801685aab34864b238b19614;hp=51b27ba688b9581121c36e562f541f969dc7a93a;hb=af43f0b77a10716921d13c047d1d3c39570cae17;hpb=2fe58dfd10216a37f1ece081f926971882de112e diff --git a/serpent.c b/serpent.c index 51b27ba..6407c57 100644 --- a/serpent.c +++ b/serpent.c @@ -20,21 +20,56 @@ * */ -#include "secnet.h" +#include #include "serpent.h" #include "serpentsboxes.h" -void serpent_makekey(struct keyInstance *key, int keyLen, - uint8_t *keyMaterial) +#ifdef SERPENT_BIGENDIAN + +#define GETPUT_CP(bytenum) \ + (((basep) + (lenbytes) - (offset) - 4)[(bytenum)]) + +#define SERPENT_DECORATE(func) serpentbe_##func + +#else /* !defined(SERPENT_BIGENDIAN) */ + +#define GETPUT_CP(bytenum) \ + (((basep) + (offset))[3-(bytenum)]) + +#define SERPENT_DECORATE(func) serpent_##func + +#endif /* !defined(SERPENT_BIGENDIAN) */ + +static uint32_t serpent_get_32bit(const uint8_t *basep, + int lenbytes, int offset) +{ + return (((uint32_t)GETPUT_CP(0) << 24) | + ((uint32_t)GETPUT_CP(1) << 16) | + ((uint32_t)GETPUT_CP(2) << +8) | + ((uint32_t)GETPUT_CP(3))); +} + +static void serpent_put_32bit(uint8_t *basep, int lenbytes, int offset, uint32_t value) +{ + GETPUT_CP(0) = (char)((value) >> 24); + GETPUT_CP(1) = (char)((value) >> 16); + GETPUT_CP(2) = (char)((value) >> 8); + GETPUT_CP(3) = (char)(value); +} + +void SERPENT_DECORATE(makekey)(struct keyInstance *key, int keyLen, + const uint8_t *keyMaterial) { - uint32_t i,j; + int i; + uint32_t j; uint32_t w[132],k[132]; for(i=0; isubkeys[i][j] = k[4*i+j]; } -void serpent_encrypt(struct keyInstance *key, - uint32_t plaintext[4], - uint32_t ciphertext[4]) +void SERPENT_DECORATE(encrypt)(struct keyInstance *key, + const uint8_t plaintext[16], + uint8_t ciphertext[16]) { register uint32_t x0, x1, x2, x3; register uint32_t y0, y1, y2, y3; - x0=plaintext[0]; - x1=plaintext[1]; - x2=plaintext[2]; - x3=plaintext[3]; + x0=serpent_get_32bit(plaintext,16,+0); + x1=serpent_get_32bit(plaintext,16,+4); + x2=serpent_get_32bit(plaintext,16,+8); + x3=serpent_get_32bit(plaintext,16,12); /* Start to encrypt the plaintext x */ keying(x0, x1, x2, x3, key->subkeys[ 0]); @@ -195,23 +230,23 @@ void serpent_encrypt(struct keyInstance *key, keying(x0, x1, x2, x3, key->subkeys[32]); /* The ciphertext is now in x */ - ciphertext[0] = x0; - ciphertext[1] = x1; - ciphertext[2] = x2; - ciphertext[3] = x3; + serpent_put_32bit(ciphertext,16,+0, x0); + serpent_put_32bit(ciphertext,16,+4, x1); + serpent_put_32bit(ciphertext,16,+8, x2); + serpent_put_32bit(ciphertext,16,12, x3); } -void serpent_decrypt(struct keyInstance *key, - uint32_t ciphertext[4], - uint32_t plaintext[4]) +void SERPENT_DECORATE(decrypt)(struct keyInstance *key, + const uint8_t ciphertext[16], + uint8_t plaintext[16]) { register uint32_t x0, x1, x2, x3; register uint32_t y0, y1, y2, y3; - x0=ciphertext[0]; - x1=ciphertext[1]; - x2=ciphertext[2]; - x3=ciphertext[3]; + x0=serpent_get_32bit(ciphertext,16,+0); + x1=serpent_get_32bit(ciphertext,16,+4); + x2=serpent_get_32bit(ciphertext,16,+8); + x3=serpent_get_32bit(ciphertext,16,12); /* Start to decrypt the ciphertext x */ keying(x0, x1, x2, x3, key->subkeys[32]); @@ -313,8 +348,8 @@ void serpent_decrypt(struct keyInstance *key, keying(x0, x1, x2, x3, key->subkeys[ 0]); /* The plaintext is now in x */ - plaintext[0] = x0; - plaintext[1] = x1; - plaintext[2] = x2; - plaintext[3] = x3; + serpent_put_32bit(plaintext,16,+0, x0); + serpent_put_32bit(plaintext,16,+4, x1); + serpent_put_32bit(plaintext,16,+8, x2); + serpent_put_32bit(plaintext,16,12, x3); }