Subversion Repositories planix.SVN

Rev

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

Rev Author Line No. Line
2 - 1
/*
2
 * sha2 64-bit
3
 */
4
#include <u.h>
5
#include <libc.h>
6
#include <libsec.h>
7
 
8
static void encode32(uchar*, u32int*, ulong);
9
static DigestState* sha2_64(uchar *, ulong, uchar *, SHA2_256state *, int);
10
 
11
extern void _sha2block64(uchar*, ulong, u32int*);
12
 
13
/*
14
 *  for sha2_224 and sha2_256, len must be multiple of 64 for all but
15
 *  the last call.  There must be room in the input buffer to pad.
16
 *
17
 *  Note: sha2_224 calls sha2_256block as sha2_224, just uses different
18
 *  initial seed and produces a 224b hash result.  otherwise it's
19
 *  the same as sha2_256.
20
 */
21
 
22
SHA2_224state*
23
sha2_224(uchar *p, ulong len, uchar *digest, SHA2_224state *s)
24
{
25
	if(s == nil) {
26
		s = mallocz(sizeof(*s), 1);
27
		if(s == nil)
28
			return nil;
29
		s->malloced = 1;
30
	}
31
	if(s->seeded == 0){
32
		/*
33
		 * seed the state with the first 32 bits of the fractional
34
		 * parts of the square roots of the first 8 primes 2..19).
35
		 */
36
		s->state[0] = 0xc1059ed8;
37
		s->state[1] = 0x367cd507;
38
		s->state[2] = 0x3070dd17;
39
		s->state[3] = 0xf70e5939;
40
		s->state[4] = 0xffc00b31;
41
		s->state[5] = 0x68581511;
42
		s->state[6] = 0x64f98fa7;
43
		s->state[7] = 0xbefa4fa4;
44
		s->seeded = 1;
45
	}
46
	return sha2_64(p, len, digest, s, SHA2_224dlen);
47
}
48
 
49
SHA2_256state*
50
sha2_256(uchar *p, ulong len, uchar *digest, SHA2_256state *s)
51
{
52
	if(s == nil) {
53
		s = mallocz(sizeof(*s), 1);
54
		if(s == nil)
55
			return nil;
56
		s->malloced = 1;
57
	}
58
	if(s->seeded == 0){
59
		/*
60
		 * seed the state with the first 32 bits of the fractional
61
		 * parts of the square roots of the first 8 primes 2..19).
62
		 */
63
		s->state[0] = 0x6a09e667;
64
		s->state[1] = 0xbb67ae85;
65
		s->state[2] = 0x3c6ef372;
66
		s->state[3] = 0xa54ff53a;
67
		s->state[4] = 0x510e527f;
68
		s->state[5] = 0x9b05688c;
69
		s->state[6] = 0x1f83d9ab;
70
		s->state[7] = 0x5be0cd19;
71
		s->seeded = 1;
72
	}
73
	return sha2_64(p, len, digest, s, SHA2_256dlen);
74
}
75
 
76
/* common 64 byte block padding and count code for SHA2_224 and SHA2_256 */
77
static DigestState*
78
sha2_64(uchar *p, ulong len, uchar *digest, SHA2_256state *s, int dlen)
79
{
80
	int i;
81
	u32int x[16];
82
	uchar buf[128];
83
	uchar *e;
84
 
85
	/* fill out the partial 64 byte block from previous calls */
86
	if(s->blen){
87
		i = 64 - s->blen;
88
		if(len < i)
89
			i = len;
90
		memmove(s->buf + s->blen, p, i);
91
		len -= i;
92
		s->blen += i;
93
		p += i;
94
		if(s->blen == 64){
95
			_sha2block64(s->buf, s->blen, s->state);
96
			s->len += s->blen;
97
			s->blen = 0;
98
		}
99
	}
100
 
101
	/* do 64 byte blocks */
102
	i = len & ~(64-1);
103
	if(i){
104
		_sha2block64(p, i, s->state);
105
		s->len += i;
106
		len -= i;
107
		p += i;
108
	}
109
 
110
	/* save the left overs if not last call */
111
	if(digest == 0){
112
		if(len){
113
			memmove(s->buf, p, len);
114
			s->blen += len;
115
		}
116
		return s;
117
	}
118
 
119
	/*
120
	 *  this is the last time through, pad what's left with 0x80,
121
	 *  0's, and the input count to create a multiple of 64 bytes.
122
	 */
123
	if(s->blen){
124
		p = s->buf;
125
		len = s->blen;
126
	} else {
127
		memmove(buf, p, len);
128
		p = buf;
129
	}
130
	s->len += len;
131
	e = p + len;
132
	if(len < 56)
133
		i = 56 - len;
134
	else
135
		i = 120 - len;
136
	memset(e, 0, i);
137
	*e = 0x80;
138
	len += i;
139
 
140
	/* append the count */
141
	x[0] = s->len>>29;
142
	x[1] = s->len<<3;
143
	encode32(p+len, x, 8);
144
 
145
	/* digest the last part */
146
	_sha2block64(p, len+8, s->state);
147
	s->len += len+8;
148
 
149
	/* return result and free state */
150
	encode32(digest, s->state, dlen);
151
	if(s->malloced == 1)
152
		free(s);
153
	return nil;
154
}
155
 
156
/*
157
 * Encodes input (ulong) into output (uchar).
158
 * Assumes len is a multiple of 4.
159
 */
160
static void
161
encode32(uchar *output, u32int *input, ulong len)
162
{
163
	u32int x;
164
	uchar *e;
165
 
166
	for(e = output + len; output < e;) {
167
		x = *input++;
168
		*output++ = x >> 24;
169
		*output++ = x >> 16;
170
		*output++ = x >> 8;
171
		*output++ = x;
172
	}
173
}
174
 
175
DigestState*
176
hmac_sha2_224(uchar *p, ulong len, uchar *key, ulong klen, uchar *digest,
177
	DigestState *s)
178
{
179
	return hmac_x(p, len, key, klen, digest, s, sha2_224, SHA2_224dlen);
180
}
181
 
182
DigestState*
183
hmac_sha2_256(uchar *p, ulong len, uchar *key, ulong klen, uchar *digest,
184
	DigestState *s)
185
{
186
	return hmac_x(p, len, key, klen, digest, s, sha2_256, SHA2_256dlen);
187
}