xref: /plan9/sys/src/cmd/ssh2/cipherblowfish.c (revision 63afb9a5d3f910047231762bcce0ee49fed3d07c)
1 #include <u.h>
2 #include <libc.h>
3 #include <mp.h>
4 #include <fcall.h>
5 #include <thread.h>
6 #include <9p.h>
7 #include <libsec.h>
8 #include "netssh.h"
9 
10 struct CipherState {
11 	BFstate	state;
12 };
13 
14 static CipherState*
initblowfish(Conn * c,int dir)15 initblowfish(Conn *c, int dir)
16 {
17 	int i;
18 	CipherState *cs;
19 
20 	if (debug > 1) {
21 		fprint(2, "initblowfish dir:%d\ns2cek: ", dir);
22 		for(i = 0; i < 16; ++i)
23 			fprint(2, "%02x", c->s2cek[i]);
24 		fprint(2, "\nc2sek: ");
25 		for(i = 0; i < 16; ++i)
26 			fprint(2, "%02x", c->c2sek[i]);
27 		fprint(2, "\ns2civ: ");
28 		for(i = 0; i < 8; ++i)
29 			fprint(2, "%02x", c->s2civ[i]);
30 		fprint(2, "\nc2siv: ");
31 		for(i = 0; i < 8; ++i)
32 			fprint(2, "%02x", c->c2siv[i]);
33 		fprint(2, "\n");
34 	}
35 	cs = emalloc9p(sizeof(CipherState));
36 	memset(cs, '\0', sizeof *cs);
37 	fprint(2, "cs: %p\n", cs);
38 	if(dir)
39 		setupBFstate(&cs->state, c->s2cek, 16, c->s2civ);
40 	else
41 		setupBFstate(&cs->state, c->c2sek, 16, c->c2siv);
42 	return cs;
43 }
44 
45 static void
encryptblowfish(CipherState * cs,uchar * buf,int nbuf)46 encryptblowfish(CipherState *cs, uchar *buf, int nbuf)
47 {
48 	bfCBCencrypt(buf, nbuf, &cs->state);
49 }
50 
51 static void
decryptblowfish(CipherState * cs,uchar * buf,int nbuf)52 decryptblowfish(CipherState *cs, uchar *buf, int nbuf)
53 {
54 fprint(2, "cs: %p, nb:%d\n", cs, nbuf);
55 fprint(2, "before decrypt: %02ux %02ux %02ux %02ux\n", buf[0], buf[1], buf[2], buf[3]);
56 	bfCBCdecrypt(buf, nbuf, &cs->state);
57 fprint(2, "after decrypt: %02ux %02ux %02ux %02ux\n", buf[0], buf[1], buf[2], buf[3]);
58 }
59 
60 Cipher cipherblowfish = {
61 	"blowfish-cbc",
62 	8,
63 	initblowfish,
64 	encryptblowfish,
65 	decryptblowfish,
66 };
67