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