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