xref: /plan9-contrib/sys/src/cmd/unix/drawterm/libsec/desCBC.c (revision 9b943567965ba040fd275927fbe088656eb8ce4f)
1 #include "../lib9.h"
2 #include "../libsec/libsec.h"
3 
4 // Because of the way that non multiple of 8
5 // buffers are handled, the decryptor must
6 // be fed buffers of the same size as the
7 // encryptor
8 
9 
10 // If the length is not a multiple of 8, I encrypt
11 // the overflow to be compatible with lacy's cryptlib
12 void
13 desCBCencrypt(uchar *p, int len, DESstate *s)
14 {
15 	uchar *p2, *ip, *eip;
16 
17 	for(; len >= 8; len -= 8){
18 		p2 = p;
19 		ip = s->ivec;
20 		for(eip = ip+8; ip < eip; )
21 			*p2++ ^= *ip++;
22 		block_cipher(s->expanded, p, 0);
23 		memmove(s->ivec, p, 8);
24 		p += 8;
25 	}
26 
27 	if(len > 0){
28 		ip = s->ivec;
29 		block_cipher(s->expanded, ip, 0);
30 		for(eip = ip+len; ip < eip; )
31 			*p++ ^= *ip++;
32 	}
33 }
34 
35 void
36 desCBCdecrypt(uchar *p, int len, DESstate *s)
37 {
38 	uchar *ip, *eip, *tp;
39 	uchar tmp[8];
40 
41 	for(; len >= 8; len -= 8){
42 		memmove(tmp, p, 8);
43 		block_cipher(s->expanded, p, 1);
44 		tp = tmp;
45 		ip = s->ivec;
46 		for(eip = ip+8; ip < eip; ){
47 			*p++ ^= *ip;
48 			*ip++ = *tp++;
49 		}
50 	}
51 
52 	if(len > 0){
53 		ip = s->ivec;
54 		block_cipher(s->expanded, ip, 0);
55 		for(eip = ip+len; ip < eip; )
56 			*p++ ^= *ip++;
57 	}
58 }
59