xref: /plan9/sys/src/cmd/unix/drawterm/libsec/desECB.c (revision 9b943567965ba040fd275927fbe088656eb8ce4f)
1 #include "../lib9.h"
2 #include "../libsec/libsec.h"
3 
4 // I wasn't sure what to do when the buffer was not
5 // a multiple of 8.  I did what lacy's cryptolib did
6 // to be compatible, but it looks dangerous to me
7 // since its encrypting plain text with the key. -- presotto
8 
9 void
10 desECBencrypt(uchar *p, int len, DESstate *s)
11 {
12 	int i;
13 	uchar tmp[8];
14 
15 	for(; len >= 8; len -= 8){
16 		block_cipher(s->expanded, p, 0);
17 		p += 8;
18 	}
19 
20 	if(len > 0){
21 		for (i=0; i<8; i++)
22 			tmp[i] = i;
23 		block_cipher(s->expanded, tmp, 0);
24 		for (i = 0; i < len; i++)
25 			p[i] ^= tmp[i];
26 	}
27 }
28 
29 void
30 desECBdecrypt(uchar *p, int len, DESstate *s)
31 {
32 	int i;
33 	uchar tmp[8];
34 
35 	for(; len >= 8; len -= 8){
36 		block_cipher(s->expanded, p, 1);
37 		p += 8;
38 	}
39 
40 	if(len > 0){
41 		for (i=0; i<8; i++)
42 			tmp[i] = i;
43 		block_cipher(s->expanded, tmp, 0);
44 		for (i = 0; i < len; i++)
45 			p[i] ^= tmp[i];
46 	}
47 }
48