summaryrefslogtreecommitdiff
path: root/appl/cmd/ssh/cipher3des.b
blob: e6f347f10822502b7e9d3e41516eea9c80aa7357 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
implement Cipher;

include "sys.m";

include "ipints.m";
	ipints: IPints;
	IPint: import ipints;

include "crypt.m";
	crypt: Crypt;
	DESstate: import crypt;

include "sshio.m";

Cipherstate: adt
{
	enc: array of ref DESstate;
	dec: array of ref DESstate;
};

cs: ref Cipherstate;

id(): int
{
	return SSH_CIPHER_3DES;
}

init(key: array of byte, nil: int)
{
	ipints = load IPints IPints->PATH;
	crypt = load Crypt Crypt->PATH;
	cs = ref Cipherstate(array[3] of ref DESstate, array[3] of ref DESstate);
	for(i := 0; i < 3; i++){
		cs.enc[i] = crypt->dessetup(key[i*8:], nil);
		cs.dec[i] = crypt->dessetup(key[i*8:], nil);
	}
}

encrypt(buf: array of byte, nbuf: int)
{
	crypt->descbc(cs.enc[0], buf, nbuf, Crypt->Encrypt);
	crypt->descbc(cs.enc[1], buf, nbuf, Crypt->Decrypt);
	crypt->descbc(cs.enc[2], buf, nbuf, Crypt->Encrypt);
}

decrypt(buf: array of byte, nbuf: int)
{
	crypt->descbc(cs.dec[2], buf, nbuf, Crypt->Decrypt);
	crypt->descbc(cs.dec[1], buf, nbuf, Crypt->Encrypt);
	crypt->descbc(cs.dec[0], buf, nbuf, Crypt->Decrypt);
}