X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?p=secnet.git;a=blobdiff_plain;f=transform-cbcmac.c;h=f74879293c8f49af9ad1bdca3807340c1731a678;hp=1e8a5e9b8acc4bc181f3b9cdbff5b32a2142abb9;hb=6c5889e68832bfd96fdd1de7e6df4309e7017372;hpb=92a7d254975db245c3320855515bffc1aebda9e4 diff --git a/transform-cbcmac.c b/transform-cbcmac.c index 1e8a5e9..f748792 100644 --- a/transform-cbcmac.c +++ b/transform-cbcmac.c @@ -18,29 +18,32 @@ /* Required key length in bytes */ #define REQUIRED_KEYLEN ((512+64+32)/8) +#include "transform-common.h" + +struct transform_params { + SEQNUM_PARAMS_FIELDS; +}; + struct transform { closure_t cl; struct transform_if ops; - uint32_t max_seq_skew; + struct transform_params p; }; struct transform_inst { struct transform_inst_if ops; + struct transform_params p; struct keyInstance cryptkey; struct keyInstance mackey; uint32_t cryptiv; uint32_t maciv; - uint32_t sendseq; - uint32_t lastrecvseq; - uint32_t max_skew; - bool_t keyed; + SEQNUM_KEYED_FIELDS; }; -#include "transform-common.h" - #define PKCS5_MASK 15 -static bool_t transform_setkey(void *sst, uint8_t *key, int32_t keylen) +static bool_t transform_setkey(void *sst, uint8_t *key, int32_t keylen, + bool_t direction) { struct transform_inst *ti=sst; @@ -62,9 +65,8 @@ static bool_t transform_setkey(void *sst, uint8_t *key, int32_t keylen) serpentbe_makekey(&ti->mackey,256,key+32); 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; + uint32_t firstseq=get_uint32(key+72); + SEQNUM_KEYED_INIT(firstseq,firstseq); return True; } @@ -113,7 +115,7 @@ static uint32_t transform_forward(void *sst, struct buffer_if *buf, bother sending the IV - it's the same each time. (If we wanted to send 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); + FILLZERO(iv); put_uint32(iv, ti->maciv); serpentbe_encrypt(&ti->mackey,iv,macacc); @@ -126,11 +128,11 @@ static uint32_t transform_forward(void *sst, struct buffer_if *buf, serpentbe_encrypt(&ti->mackey,macplain,macacc); } serpentbe_encrypt(&ti->mackey,macacc,macacc); - memcpy(buf_append(buf,16),macacc,16); + BUF_ADD_BYTES(append,buf,macacc,16); /* Serpent-CBC. We expand the ID as for CBCMAC, do the encryption, and prepend the IV before increasing it. */ - memset(iv,0,16); + FILLZERO(iv); put_uint32(iv, ti->cryptiv); serpentbe_encrypt(&ti->cryptkey,iv,iv); @@ -174,7 +176,7 @@ static uint32_t transform_reverse(void *sst, struct buffer_if *buf, } /* CBC */ - memset(iv,0,16); + FILLZERO(iv); { uint32_t ivword = buf_unprepend_uint32(buf); put_uint32(iv, ivword); @@ -192,12 +194,12 @@ static uint32_t transform_reverse(void *sst, struct buffer_if *buf, serpentbe_decrypt(&ti->cryptkey,n,n); for (i = 0; i < 16; i++) n[i] ^= iv[i]; - memcpy(iv, pct, 16); + COPY_OBJ(iv, pct); } /* CBCMAC */ macexpected=buf_unappend(buf,16); - memset(iv,0,16); + FILLZERO(iv); put_uint32(iv, ti->maciv); serpentbe_encrypt(&ti->mackey,iv,macacc); @@ -229,7 +231,7 @@ static uint32_t transform_reverse(void *sst, struct buffer_if *buf, /* Sequence number must be within max_skew of lastrecvseq; lastrecvseq is only allowed to increase. */ seqnum=buf_unprepend_uint32(buf); - SEQNUM_CHECK(seqnum, ti->max_skew); + SEQNUM_CHECK(seqnum, &ti->p); return 0; } @@ -242,7 +244,7 @@ static struct transform_inst_if *transform_create(void *sst) TRANSFORM_CREATE_CORE; - ti->max_skew=st->max_seq_skew; + ti->p=st->p; return &ti->ops; } @@ -254,15 +256,14 @@ static list_t *transform_apply(closure_t *self, struct cloc loc, item_t *item; dict_t *dict; - st=safe_malloc(sizeof(*st),"serpent"); + NEW(st); st->cl.description="serpent-cbc256"; st->cl.type=CL_TRANSFORM; st->cl.apply=NULL; st->cl.interface=&st->ops; st->ops.st=st; - st->ops.max_start_pad=28; /* 4byte seqnum, 16byte pad, 4byte MACIV, - 4byte IV */ - st->ops.max_end_pad=16; /* 16byte CBCMAC */ + update_max_start_pad(&transform_max_start_pad, 28); + /* 4byte seqnum, 16byte pad, 4byte MACIV, 4byte IV */ /* We need 256*2 bits for serpent keys, 32 bits for CBC-IV and 32 bits for CBCMAC-IV, and 32 bits for init sequence number */ @@ -275,8 +276,10 @@ static list_t *transform_apply(closure_t *self, struct cloc loc, cfgfatal(loc,"userv-ipif","parameter must be a dictionary\n"); dict=item->data.dict; - st->max_seq_skew=dict_read_number(dict, "max-sequence-skew", - False, "serpent-cbc256", loc, 10); + + SEQNUM_PARAMS_INIT(dict,&st->p,"serpent-cbc256",loc); + + SET_CAPAB_TRANSFORMNUM(CAPAB_TRANSFORMNUM_SERPENT256CBC); return new_closure(&st->cl); } @@ -334,7 +337,7 @@ void transform_cbcmac_module(dict_t *dict) const char *errmsg; int i; - tr = malloc(sizeof(struct transform)); + NEW(tr); tr->max_seq_skew = 20; ti = transform_create(tr); @@ -342,7 +345,7 @@ void transform_cbcmac_module(dict_t *dict) buf.base = malloc(4096); buffer_init(&buf, 2048); - memcpy(buf_append(&buf, sizeof(text)), text, sizeof(text)); + BUF_ADD_OBJ(append, buf, text, sizeof(text)); if (transform_forward(ti, &buf, &errmsg)) { fatal("transform_forward test: %s", errmsg); }