X-Git-Url: https://git.distorted.org.uk/~mdw/secnet/blobdiff_plain/1caa23ff879cec7f8f36b32a987f0610291ef177..4a1a591973e0be6f33a55b8f1fc5abc827f6969d:/transform.c diff --git a/transform.c b/transform.c index 5497711..08ddad6 100644 --- a/transform.c +++ b/transform.c @@ -19,7 +19,6 @@ struct transform { closure_t cl; - uint32_t line; struct transform_if ops; uint32_t max_seq_skew; }; @@ -60,21 +59,28 @@ static bool_t transform_setkey(void *sst, uint8_t *key, int32_t keylen) 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; return True; } +static bool_t transform_valid(void *sst) +{ + struct transform_inst *ti=sst; + + return ti->keyed; +} + static void transform_delkey(void *sst) { struct transform_inst *ti=sst; - memset(&ti->cryptkey,0,sizeof(ti->cryptkey)); - memset(&ti->mackey,0,sizeof(ti->mackey)); + FILLZERO(ti->cryptkey); + FILLZERO(ti->mackey); ti->keyed=False; } @@ -115,7 +121,7 @@ static uint32_t transform_forward(void *sst, struct buffer_if *buf, 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 @@ -132,7 +138,7 @@ static uint32_t transform_forward(void *sst, struct buffer_if *buf, /* 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) @@ -172,16 +178,21 @@ static uint32_t transform_reverse(void *sst, struct buffer_if *buf, return 1; } + if (buf->size < 4 + 16 + 16) { + *errmsg="msg too short"; + return 1; + } /* CBC */ 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) { *errmsg="msg not multiple of cipher blocksize"; + return 1; } serpent_encrypt(&ti->cryptkey,iv,iv); for (n=buf->start; nstart+buf->size; n+=16) @@ -197,7 +208,7 @@ static uint32_t transform_reverse(void *sst, struct buffer_if *buf, /* 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 @@ -209,7 +220,7 @@ static uint32_t transform_reverse(void *sst, struct buffer_if *buf, serpent_encrypt(&ti->mackey,macplain,macacc); } serpent_encrypt(&ti->mackey,macacc,macacc); - if (memcmp(macexpected,macacc,16)!=0) { + if (!consttime_memeq(macexpected,macacc,16)!=0) { *errmsg="invalid MAC"; return 1; } @@ -223,13 +234,7 @@ static uint32_t transform_reverse(void *sst, struct buffer_if *buf, return 1; } - padp=buf_unappend(buf,padlen-1); - for (i=0; iops.st=ti; ti->ops.setkey=transform_setkey; + ti->ops.valid=transform_valid; ti->ops.delkey=transform_delkey; ti->ops.forwards=transform_forward; ti->ops.reverse=transform_reverse;