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