Annotation of OpenXM/src/ox_ntl/crypt/sha1/sha1.c, Revision 1.2
1.2 ! iwane 1: /* $OpenXM: OpenXM/src/ox_ntl/crypt/sha1/sha1.c,v 1.1 2004/01/12 13:16:28 iwane Exp $ */
! 2: /* RFC 3174 - SHA-1 (US Secure Hash Algorithm 1 (SHA1))*/
1.1 iwane 3:
1.2 ! iwane 4: #include "sha1.h"
1.1 iwane 5:
6: /* Global Constant */
7: static const unsigned int K[4] = {0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6};
8: static const unsigned int H[5] = {0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0};
9:
10: #define BLOCK (512 / 8)
11:
12:
13: static inline unsigned int lshift32(unsigned int x, int n)
14: {
15: return ((x << n) | (x >> (32 - n)));
16: };
17:
18:
19:
20: /**
21: * sizeof(buf) >= 512 * ((len * 8 + 1) / 64)
22: * len * 8 < 2^64 ==> len < 2^61
23: */
24: static unsigned int
25: padding(unsigned char *buf, const unsigned char *msg, int length)
26: {
27: int n;
28: int i;
29: int len = length % BLOCK;
30:
31: if (len == 0)
32: len = BLOCK;
33:
34: memcpy(buf, msg, len);
35:
36: buf[len++] = 0x80;
37:
38: /* 56 < len % 64 */
39: if (BLOCK - 8 < len % BLOCK) {
40: /* too long */
41: n = BLOCK - (len % BLOCK) + BLOCK - 8;
42: } else {
43: n = BLOCK - 8 - (len % BLOCK);
44: }
45:
46: memset(buf + len, 0x00, n);
47:
48: n += len;
49:
50: for (i = 0; i < 4; i++) {
51: buf[n] = 0x00;
52: buf[n + 4] = ((length * 8) >> (8 * (3 - i))) & 0xff;
53: n++;
54: }
55:
56: return ((n + 4) / BLOCK);
57: }
58:
59:
60:
61:
62: static unsigned int
63: f(unsigned int t, unsigned int b, unsigned int c, unsigned int d)
64: {
65: if (t < 20) {
66: return ((b & c) | ((~b) & d));
67: }
68: if (t < 40 || t >= 60) {
69: return (b ^ c ^ d);
70: }
71:
72: if (t < 60) {
73: return ((b & c) | (b & d) | (c & d));
74: }
75:
1.2 ! iwane 76: /* Invalid Parameter. */
1.1 iwane 77: return (0);
78: }
79:
80:
81: /* sizeof(msg) == 512 / 8.
82: * padding.
83: */
84: static void
85: md(unsigned int *h, const unsigned char *msg)
86: {
87: int t;
88: unsigned int a, b, c, d, e, temp;
89: int i;
90: unsigned int w[80];
91:
1.2 ! iwane 92: /* ... */
1.1 iwane 93: for (t = 0; t < 16; t++) {
94: w[t] = 0;
95: for (i = 0; i < 4; i++) {
96: w[t] |= (msg[i + 4 * t] & 0xff) << ((3 - i) * 8);
97: }
98: }
99:
100: for (t = 16; t < 80; t++) {
101: w[t] = lshift32(w[t - 3] ^ w[t - 8] ^ w[t - 14] ^ w[t - 16], 1);
102: }
103:
104: a = h[0];
105: b = h[1];
106: c = h[2];
107: d = h[3];
108: e = h[4];
109:
110: for (t = 0; t < 80; t++) {
111: temp = lshift32(a, 5) + f(t, b, c, d) + e + w[t] + K[t / 20];
112: e = d;
113: d = c;
114: c = lshift32(b, 30);
115: b = a;
116: a = temp;
117:
118: }
119:
120: h[0] += a;
121: h[1] += b;
122: h[2] += c;
123: h[3] += d;
124: h[4] += e;
125: }
126:
127: int
1.2 ! iwane 128: sha1_h(unsigned char *Ph, const unsigned char *msg, int len, const unsigned int *hp)
1.1 iwane 129: {
130: int i, j, cnt, l = len;
131: unsigned char buf[1024];
1.2 ! iwane 132: unsigned int h[sizeof(H) / sizeof(H[0])];
! 133:
! 134: memcpy(h, hp, sizeof(h));
1.1 iwane 135:
136: while (l > BLOCK) {
137: md(h, msg);
138: msg += BLOCK;
139: l -= BLOCK;
140: }
141:
142: cnt = padding(buf, msg, len);
143: for (i = 0; i < cnt; i++) {
144: md(h, buf + BLOCK * i);
145: }
146:
147: memset(Ph, 0x00, sizeof(H));
148: for (i = 0; i < sizeof(H) / sizeof(H[0]); i++) {
149: for (j = 0; j < 32; j++) {
150: Ph[4 * i + j / 8] |= ((h[i] >> (31 - j)) & 1) << (7 - j % 8);
151: }
152: }
153:
154:
155: return (0);
156: }
157:
158:
159: int
160: sha1(unsigned char *Ph, const unsigned char *msg, int len)
161: {
1.2 ! iwane 162: return (sha1_h(Ph, msg, len, H));
1.1 iwane 163: }
164:
165:
166: #ifdef SHA_DEBUG
167: /* debug */
168: #include <stdio.h>
169:
170: int
171: main()
172: {
173: char *a;
174: int m, i;
175: unsigned char h[32 * 5];
176: char b[10000000];
177:
178: for (i = 0; i < 1000000; i++)
179: b[i] = 'a';
180: b[i] = '\0';
181:
182: a = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
183: a = b;
184: a = "abc";
185:
186: m = sha1(h, a, strlen(a));
187:
188: for (i = 0; i < 160 / 8; i++) {
189: printf("%02x", h[i] & 0xff);
190: if (i % 4 == 3)
191: printf(" ");
192: }
193: printf("\n");
194:
195:
196: return (0);
197: }
198:
199: #endif
200:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>