add aes to tls Reference: /n/patches.lsub.org/patch/tlsaes Date: Sun Oct 28 18:09:17 CET 2012 Signed-off-by: quanstro@quanstro.net --- /sys/src/nix/port/devtls.c Thu Apr 12 12:26:28 2012 +++ /sys/src/nix/port/devtls.c Thu Jan 5 18:39:14 2012 @@ -234,6 +234,8 @@ static int rc4enc(Secret *sec, uchar *buf, int n); static int des3enc(Secret *sec, uchar *buf, int n); static int des3dec(Secret *sec, uchar *buf, int n); +static int aesenc(Secret *sec, uchar *buf, int n); +static int aesdec(Secret *sec, uchar *buf, int n); static int noenc(Secret *sec, uchar *buf, int n); static int sslunpad(uchar *buf, int n, int block); static int tlsunpad(uchar *buf, int n, int block); @@ -1429,6 +1431,16 @@ } static void +initAESkey(Encalg *ea, Secret *s, uchar *p, uchar *iv) +{ + s->enckey = smalloc(sizeof(AESstate)); + s->enc = aesenc; + s->dec = aesdec; + s->block = 16; + setupAESstate(s->enckey, p, ea->keylen, iv); +} + +static void initclearenc(Encalg *, Secret *s, uchar *, uchar *) { s->enc = noenc; @@ -1441,6 +1453,8 @@ { "clear", 0, 0, initclearenc }, { "rc4_128", 128/8, 0, initRC4key }, { "3des_ede_cbc", 3 * 8, 8, initDES3key }, + { "aes_128_cbc", 128/8, 16, initAESkey }, + { "aes_256_cbc", 256/8, 16, initAESkey }, { 0 } }; @@ -1698,7 +1712,8 @@ static int already; if(!already){ -// fmtinstall('H', encodefmt); + extern int encodefmt(Fmt*); + fmtinstall('H', encodefmt); already = 1; } @@ -2016,6 +2031,22 @@ des3CBCdecrypt(buf, n, sec->enckey); return (*sec->unpad)(buf, n, 8); } + +static int +aesenc(Secret *sec, uchar *buf, int n) +{ + n = blockpad(buf, n, 16); + aesCBCencrypt(buf, n, sec->enckey); + return n; +} + +static int +aesdec(Secret *sec, uchar *buf, int n) +{ + aesCBCdecrypt(buf, n, sec->enckey); + return (*sec->unpad)(buf, n, 16); +} + static DigestState* nomac(uchar *, ulong, uchar *, ulong, uchar *, DigestState *) {