chiark / gitweb /
subdirmk: Change stub force target to `run-main.mk'
[secnet.git] / transform-cbcmac.c
index 724a59eeefa576ce73855406153db4ee181c9f2d..b481346068833c12eefdc1463b20cf489a97286d 100644 (file)
@@ -1,5 +1,24 @@
 /* Transform module - bulk data transformation */
 
+/*
+ * This file is part of secnet.
+ * See README for full list of copyright holders.
+ *
+ * secnet is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ * 
+ * secnet is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * version 3 along with secnet; if not, see
+ * https://www.gnu.org/licenses/gpl.html.
+ */
+
 /* For now it's hard-coded to do sequence
    number/pkcs5/serpent-cbcmac/serpent with a 256 bit key for each
    instance of serpent. We also require key material for the IVs for
 /* Required key length in bytes */
 #define REQUIRED_KEYLEN ((512+64+32)/8)
 
+#include "transform-common.h"
+
 struct transform_params {
-    uint32_t max_seq_skew;
+    SEQNUM_PARAMS_FIELDS;
 };
 
 struct transform {
@@ -35,13 +56,9 @@ struct transform_inst {
     struct keyInstance mackey;
     uint32_t cryptiv;
     uint32_t maciv;
-    uint32_t sendseq;
-    uint32_t lastrecvseq;
-    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,
@@ -67,9 +84,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;
 }
@@ -85,8 +101,8 @@ static void transform_delkey(void *sst)
     ti->keyed=False;
 }
 
-static uint32_t transform_forward(void *sst, struct buffer_if *buf,
-                                 const char **errmsg)
+static transform_apply_return transform_forward(void *sst,
+            struct buffer_if *buf, const char **errmsg)
 {
     struct transform_inst *ti=sst;
     uint8_t *padp;
@@ -156,8 +172,8 @@ static uint32_t transform_forward(void *sst, struct buffer_if *buf,
     return 0;
 }
 
-static uint32_t transform_reverse(void *sst, struct buffer_if *buf,
-                                 const char **errmsg)
+static transform_apply_return transform_reverse(void *sst,
+                struct buffer_if *buf, const char **errmsg)
 {
     struct transform_inst *ti=sst;
     uint8_t *padp;
@@ -175,7 +191,7 @@ static uint32_t transform_reverse(void *sst, struct buffer_if *buf,
 
     if (buf->size < 4 + 16 + 16) {
        *errmsg="msg too short";
-       return 1;
+       return transform_apply_err;
     }
 
     /* CBC */
@@ -187,7 +203,7 @@ static uint32_t transform_reverse(void *sst, struct buffer_if *buf,
     /* Assert bufsize is multiple of blocksize */
     if (buf->size&0xf) {
        *errmsg="msg not multiple of cipher blocksize";
-       return 1;
+       return transform_apply_err;
     }
     serpentbe_encrypt(&ti->cryptkey,iv,iv);
     for (n=buf->start; n<buf->start+buf->size; n+=16)
@@ -215,9 +231,9 @@ static uint32_t transform_reverse(void *sst, struct buffer_if *buf,
        serpentbe_encrypt(&ti->mackey,macplain,macacc);
     }
     serpentbe_encrypt(&ti->mackey,macacc,macacc);
-    if (!consttime_memeq(macexpected,macacc,16)!=0) {
+    if (!consttime_memeq(macexpected,macacc,16)) {
        *errmsg="invalid MAC";
-       return 1;
+       return transform_apply_err;
     }
 
     /* PKCS5, stolen from IWJ */
@@ -226,7 +242,7 @@ static uint32_t transform_reverse(void *sst, struct buffer_if *buf,
     padlen=*padp;
     if (!padlen || (padlen > PKCS5_MASK+1)) {
        *errmsg="pkcs5: invalid length";
-       return 1;
+       return transform_apply_err;
     }
 
     buf_unappend(buf,padlen-1);
@@ -234,7 +250,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->p.max_seq_skew);
+    SEQNUM_CHECK(seqnum, &ti->p);
     
     return 0;
 }
@@ -259,7 +275,7 @@ 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;
@@ -276,13 +292,13 @@ static list_t *transform_apply(closure_t *self, struct cloc loc,
     /* First parameter must be a dict */
     item=list_elem(args,0);
     if (!item || item->type!=t_dict)
-       cfgfatal(loc,"userv-ipif","parameter must be a dictionary\n");
+       cfgfatal(loc,"serpent256-cbc","parameter must be a dictionary\n");
     
     dict=item->data.dict;
-    st->p.max_seq_skew=dict_read_number(dict, "max-sequence-skew",
-                                       False, "serpent-cbc256", loc, 10);
 
-    SET_CAPAB_TRANSFORMNUM(CAPAB_TRANSFORMNUM_SERPENT256CBC);
+    SEQNUM_PARAMS_INIT(dict,&st->p,"serpent-cbc256",loc);
+
+    SET_CAPAB_BIT(CAPAB_BIT_SERPENT256CBC);
 
     return new_closure(&st->cl);
 }
@@ -340,7 +356,7 @@ void transform_cbcmac_module(dict_t *dict)
        const char *errmsg;
        int i;
 
-       tr = safe_malloc(sizeof(struct transform),"test transform");
+       NEW(tr);
        tr->max_seq_skew = 20;
        ti = transform_create(tr);