aboutsummaryrefslogtreecommitdiff
path: root/src/libsec/port/des3ECB.c
blob: cfd2ce966adde05328603a6b8cb61fbca9dd725e (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
#include "os.h"
#include <mp.h>
#include <libsec.h>

/* I wasn't sure what to do when the buffer was not */
/* a multiple of 8.  I did what lacy's cryptolib did */
/* to be compatible, but it looks dangerous to me */
/* since its encrypting plain text with the key. -- presotto */

void
des3ECBencrypt(uchar *p, int len, DES3state *s)
{
	int i;
	uchar tmp[8];

	for(; len >= 8; len -= 8){
		triple_block_cipher(s->expanded, p, DES3EDE);
		p += 8;
	}

	if(len > 0){
		for (i=0; i<8; i++)
			tmp[i] = i;
		triple_block_cipher(s->expanded, tmp, DES3EDE);
		for (i = 0; i < len; i++)
			p[i] ^= tmp[i];
	}
}

void
des3ECBdecrypt(uchar *p, int len, DES3state *s)
{
	int i;
	uchar tmp[8];

	for(; len >= 8; len -= 8){
		triple_block_cipher(s->expanded, p, DES3DED);
		p += 8;
	}

	if(len > 0){
		for (i=0; i<8; i++)
			tmp[i] = i;
		triple_block_cipher(s->expanded, tmp, DES3EDE);
		for (i = 0; i < len; i++)
			p[i] ^= tmp[i];
	}
}