*
*/
-#include "secnet.h"
+#include <stdint.h>
#include "serpent.h"
#include "serpentsboxes.h"
+#define GETPUT_CP(bytenum) \
+ (((basep) + (lenbytes) - (offset) - 4)[(bytenum)])
+
+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_makekey(struct keyInstance *key, int keyLen,
const uint8_t *keyMaterial)
{
uint32_t w[132],k[132];
for(i=0; i<keyLen/32; i++)
- w[i]=GET_32BIT_MSB_FIRST(keyMaterial + (keyLen/8 - i*4) - 4);
+ w[i]=serpent_get_32bit(keyMaterial, keyLen/8, i*4);
if(keyLen<256)
- w[i]=(GET_32BIT_MSB_FIRST(keyMaterial + (keyLen/8 - i*4) - 4)
+ w[i]=(serpent_get_32bit(keyMaterial, keyLen/8, i*4)
& ((1L<<((keyLen&31)))-1)) | (1L<<((keyLen&31)));
for(i++; i<8; i++)
w[i]=0;
register uint32_t x0, x1, x2, x3;
register uint32_t y0, y1, y2, y3;
- x0=GET_32BIT_MSB_FIRST(plaintext+12);
- x1=GET_32BIT_MSB_FIRST(plaintext+8);
- x2=GET_32BIT_MSB_FIRST(plaintext+4);
- x3=GET_32BIT_MSB_FIRST(plaintext);
+ 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]);
keying(x0, x1, x2, x3, key->subkeys[32]);
/* The ciphertext is now in x */
- PUT_32BIT_MSB_FIRST(ciphertext+12, x0);
- PUT_32BIT_MSB_FIRST(ciphertext+8, x1);
- PUT_32BIT_MSB_FIRST(ciphertext+4, x2);
- PUT_32BIT_MSB_FIRST(ciphertext, 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,
register uint32_t x0, x1, x2, x3;
register uint32_t y0, y1, y2, y3;
- x0=GET_32BIT_MSB_FIRST(ciphertext+12);
- x1=GET_32BIT_MSB_FIRST(ciphertext+8);
- x2=GET_32BIT_MSB_FIRST(ciphertext+4);
- x3=GET_32BIT_MSB_FIRST(ciphertext);
+ 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]);
keying(x0, x1, x2, x3, key->subkeys[ 0]);
/* The plaintext is now in x */
- PUT_32BIT_MSB_FIRST(plaintext+12, x0);
- PUT_32BIT_MSB_FIRST(plaintext+8, x1);
- PUT_32BIT_MSB_FIRST(plaintext+4, x2);
- PUT_32BIT_MSB_FIRST(plaintext, 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);
}
serpent_makekey(&ti->cryptkey,256,key);
serpent_makekey(&ti->mackey,256,key+32);
- ti->cryptiv=GET_32BIT_MSB_FIRST(key+64);
- ti->maciv=GET_32BIT_MSB_FIRST(key+68);
- ti->sendseq=GET_32BIT_MSB_FIRST(key+72);
+ ti->cryptiv=get_uint32(key+64);
+ ti->maciv=get_uint32(key+68);
+ ti->sendseq=get_uint32(key+72);
ti->lastrecvseq=ti->sendseq;
ti->keyed=True;
it we've have to add 16 bytes to each message, not 4, so that the
message stays a multiple of 16 bytes long.) */
memset(iv,0,16);
- PUT_32BIT_MSB_FIRST(iv, ti->maciv);
+ put_uint32(iv, ti->maciv);
serpent_encrypt(&ti->mackey,iv,macacc);
/* CBCMAC: encrypt in CBC mode. The MAC is the last encrypted
/* Serpent-CBC. We expand the ID as for CBCMAC, do the encryption,
and prepend the IV before increasing it. */
memset(iv,0,16);
- PUT_32BIT_MSB_FIRST(iv, ti->cryptiv);
+ put_uint32(iv, ti->cryptiv);
serpent_encrypt(&ti->cryptkey,iv,iv);
/* CBC: each block is XORed with the previous encrypted block (or the IV)
memset(iv,0,16);
{
uint32_t ivword = buf_unprepend_uint32(buf);
- PUT_32BIT_MSB_FIRST(iv, ivword);
+ put_uint32(iv, ivword);
}
/* Assert bufsize is multiple of blocksize */
if (buf->size&0xf) {
/* CBCMAC */
macexpected=buf_unappend(buf,16);
memset(iv,0,16);
- PUT_32BIT_MSB_FIRST(iv, ti->maciv);
+ put_uint32(iv, ti->maciv);
serpent_encrypt(&ti->mackey,iv,macacc);
/* CBCMAC: encrypt in CBC mode. The MAC is the last encrypted