Subversion Repositories planix.SVN

Rev

Rev 33 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
26 7u83 1
#include "os.h"
2
#include <libsec.h>
3
 
4
/*
5
 * Define by analogy with desCBCencrypt;  AES modes are not standardized yet.
6
 * Because of the way that non-multiple-of-16 buffers are handled,
7
 * the decryptor must be fed buffers of the same size as the encryptor.
8
 */
9
void
10
aesCBCencrypt(uchar *p, int len, AESstate *s)
11
{
12
	uchar *ip, *eip;
13
 
14
	if(((p-(uchar*)0) & 3) == 0){
15
		for(; len >= AESbsize; len -= AESbsize){
16
			ip = s->ivec;
17
			((u32int*)ip)[0] ^= ((u32int*)p)[0];
18
			((u32int*)ip)[1] ^= ((u32int*)p)[1];
19
			((u32int*)ip)[2] ^= ((u32int*)p)[2];
20
			((u32int*)ip)[3] ^= ((u32int*)p)[3];
21
 
22
			aes_encrypt(s->ekey, s->rounds, ip, ip);
23
 
24
			((u32int*)p)[0] = ((u32int*)ip)[0];
25
			((u32int*)p)[1] = ((u32int*)ip)[1];
26
			((u32int*)p)[2] = ((u32int*)ip)[2];
27
			((u32int*)p)[3] = ((u32int*)ip)[3];
28
			p += AESbsize;
29
		}
30
	} else {
31
		for(; len >= AESbsize; len -= AESbsize){
32
			ip = s->ivec;
33
			for(eip = ip+AESbsize; ip < eip; )
34
				*ip++ ^= *p++;
35
			aes_encrypt(s->ekey, s->rounds, s->ivec, s->ivec);
36
			memmove(p - AESbsize, s->ivec, AESbsize);
37
		}
38
	}
39
 
40
	if(len > 0){
41
		ip = s->ivec;
42
		aes_encrypt(s->ekey, s->rounds, ip, ip);
43
		for(eip = ip+len; ip < eip; )
44
			*p++ ^= *ip++;
45
	}
46
}
47
 
48
void
49
aesCBCdecrypt(uchar *p, int len, AESstate *s)
50
{
51
	uchar *ip, *eip, *tp;
52
	u32int t[4];
53
 
54
	if(((p-(uchar*)0) & 3) == 0){
55
		for(; len >= AESbsize; len -= AESbsize){
56
			t[0] = ((u32int*)p)[0];
57
			t[1] = ((u32int*)p)[1];
58
			t[2] = ((u32int*)p)[2];
59
			t[3] = ((u32int*)p)[3];
60
 
61
			aes_decrypt(s->dkey, s->rounds, p, p);
62
 
63
			ip = s->ivec;
64
			((u32int*)p)[0] ^= ((u32int*)ip)[0];
65
			((u32int*)p)[1] ^= ((u32int*)ip)[1];
66
			((u32int*)p)[2] ^= ((u32int*)ip)[2];
67
			((u32int*)p)[3] ^= ((u32int*)ip)[3];
68
			p += AESbsize;
69
 
70
			((u32int*)ip)[0] = t[0];
71
			((u32int*)ip)[1] = t[1];
72
			((u32int*)ip)[2] = t[2];
73
			((u32int*)ip)[3] = t[3];
74
		}
75
	} else {
76
		for(; len >= AESbsize; len -= AESbsize){
77
			tp = (uchar*)t;
78
			memmove(tp, p, AESbsize);
79
			aes_decrypt(s->dkey, s->rounds, p, p);
80
			ip = s->ivec;
81
			for(eip = ip+AESbsize; ip < eip; ){
82
				*p++ ^= *ip;
83
				*ip++ = *tp++;
84
			}
85
		}
86
	}
87
 
88
	if(len > 0){
89
		ip = s->ivec;
90
		aes_encrypt(s->ekey, s->rounds, ip, ip);
91
		for(eip = ip+len; ip < eip; )
92
			*p++ ^= *ip++;
93
	}
94
}