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