001: package org.bouncycastle.crypto.digests;
002:
003: import org.bouncycastle.crypto.digests.GeneralDigest;
004:
005: /**
006: * SHA-224 as described in RFC 3874
007: * <pre>
008: * block word digest
009: * SHA-1 512 32 160
010: * SHA-224 512 32 224
011: * SHA-256 512 32 256
012: * SHA-384 1024 64 384
013: * SHA-512 1024 64 512
014: * </pre>
015: */
016: public class SHA224Digest extends GeneralDigest {
017: private static final int DIGEST_LENGTH = 28;
018:
019: private int H1, H2, H3, H4, H5, H6, H7, H8;
020:
021: private int[] X = new int[64];
022: private int xOff;
023:
024: /**
025: * Standard constructor
026: */
027: public SHA224Digest() {
028: reset();
029: }
030:
031: /**
032: * Copy constructor. This will copy the state of the provided
033: * message digest.
034: */
035: public SHA224Digest(SHA224Digest t) {
036: super (t);
037:
038: H1 = t.H1;
039: H2 = t.H2;
040: H3 = t.H3;
041: H4 = t.H4;
042: H5 = t.H5;
043: H6 = t.H6;
044: H7 = t.H7;
045: H8 = t.H8;
046:
047: System.arraycopy(t.X, 0, X, 0, t.X.length);
048: xOff = t.xOff;
049: }
050:
051: public String getAlgorithmName() {
052: return "SHA-224";
053: }
054:
055: public int getDigestSize() {
056: return DIGEST_LENGTH;
057: }
058:
059: protected void processWord(byte[] in, int inOff) {
060: X[xOff++] = ((in[inOff] & 0xff) << 24)
061: | ((in[inOff + 1] & 0xff) << 16)
062: | ((in[inOff + 2] & 0xff) << 8)
063: | ((in[inOff + 3] & 0xff));
064:
065: if (xOff == 16) {
066: processBlock();
067: }
068: }
069:
070: private void unpackWord(int word, byte[] out, int outOff) {
071: out[outOff] = (byte) (word >>> 24);
072: out[outOff + 1] = (byte) (word >>> 16);
073: out[outOff + 2] = (byte) (word >>> 8);
074: out[outOff + 3] = (byte) word;
075: }
076:
077: protected void processLength(long bitLength) {
078: if (xOff > 14) {
079: processBlock();
080: }
081:
082: X[14] = (int) (bitLength >>> 32);
083: X[15] = (int) (bitLength & 0xffffffff);
084: }
085:
086: public int doFinal(byte[] out, int outOff) {
087: finish();
088:
089: unpackWord(H1, out, outOff);
090: unpackWord(H2, out, outOff + 4);
091: unpackWord(H3, out, outOff + 8);
092: unpackWord(H4, out, outOff + 12);
093: unpackWord(H5, out, outOff + 16);
094: unpackWord(H6, out, outOff + 20);
095: unpackWord(H7, out, outOff + 24);
096:
097: reset();
098:
099: return DIGEST_LENGTH;
100: }
101:
102: /**
103: * reset the chaining variables
104: */
105: public void reset() {
106: super .reset();
107:
108: /* SHA-224 initial hash value
109: */
110:
111: H1 = 0xc1059ed8;
112: H2 = 0x367cd507;
113: H3 = 0x3070dd17;
114: H4 = 0xf70e5939;
115: H5 = 0xffc00b31;
116: H6 = 0x68581511;
117: H7 = 0x64f98fa7;
118: H8 = 0xbefa4fa4;
119:
120: xOff = 0;
121: for (int i = 0; i != X.length; i++) {
122: X[i] = 0;
123: }
124: }
125:
126: protected void processBlock() {
127: //
128: // expand 16 word block into 64 word blocks.
129: //
130: for (int t = 16; t <= 63; t++) {
131: X[t] = Theta1(X[t - 2]) + X[t - 7] + Theta0(X[t - 15])
132: + X[t - 16];
133: }
134:
135: //
136: // set up working variables.
137: //
138: int a = H1;
139: int b = H2;
140: int c = H3;
141: int d = H4;
142: int e = H5;
143: int f = H6;
144: int g = H7;
145: int h = H8;
146:
147: int t = 0;
148: for (int i = 0; i < 8; i++) {
149: // t = 8 * i
150: h += Sum1(e) + Ch(e, f, g) + K[t] + X[t++];
151: d += h;
152: h += Sum0(a) + Maj(a, b, c);
153:
154: // t = 8 * i + 1
155: g += Sum1(d) + Ch(d, e, f) + K[t] + X[t++];
156: c += g;
157: g += Sum0(h) + Maj(h, a, b);
158:
159: // t = 8 * i + 2
160: f += Sum1(c) + Ch(c, d, e) + K[t] + X[t++];
161: b += f;
162: f += Sum0(g) + Maj(g, h, a);
163:
164: // t = 8 * i + 3
165: e += Sum1(b) + Ch(b, c, d) + K[t] + X[t++];
166: a += e;
167: e += Sum0(f) + Maj(f, g, h);
168:
169: // t = 8 * i + 4
170: d += Sum1(a) + Ch(a, b, c) + K[t] + X[t++];
171: h += d;
172: d += Sum0(e) + Maj(e, f, g);
173:
174: // t = 8 * i + 5
175: c += Sum1(h) + Ch(h, a, b) + K[t] + X[t++];
176: g += c;
177: c += Sum0(d) + Maj(d, e, f);
178:
179: // t = 8 * i + 6
180: b += Sum1(g) + Ch(g, h, a) + K[t] + X[t++];
181: f += b;
182: b += Sum0(c) + Maj(c, d, e);
183:
184: // t = 8 * i + 7
185: a += Sum1(f) + Ch(f, g, h) + K[t] + X[t++];
186: e += a;
187: a += Sum0(b) + Maj(b, c, d);
188: }
189:
190: H1 += a;
191: H2 += b;
192: H3 += c;
193: H4 += d;
194: H5 += e;
195: H6 += f;
196: H7 += g;
197: H8 += h;
198:
199: //
200: // reset the offset and clean out the word buffer.
201: //
202: xOff = 0;
203: for (int i = 0; i < 16; i++) {
204: X[i] = 0;
205: }
206: }
207:
208: /* SHA-224 functions */
209: private int Ch(int x, int y, int z) {
210: return ((x & y) ^ ((~x) & z));
211: }
212:
213: private int Maj(int x, int y, int z) {
214: return ((x & y) ^ (x & z) ^ (y & z));
215: }
216:
217: private int Sum0(int x) {
218: return ((x >>> 2) | (x << 30)) ^ ((x >>> 13) | (x << 19))
219: ^ ((x >>> 22) | (x << 10));
220: }
221:
222: private int Sum1(int x) {
223: return ((x >>> 6) | (x << 26)) ^ ((x >>> 11) | (x << 21))
224: ^ ((x >>> 25) | (x << 7));
225: }
226:
227: private int Theta0(int x) {
228: return ((x >>> 7) | (x << 25)) ^ ((x >>> 18) | (x << 14))
229: ^ (x >>> 3);
230: }
231:
232: private int Theta1(int x) {
233: return ((x >>> 17) | (x << 15)) ^ ((x >>> 19) | (x << 13))
234: ^ (x >>> 10);
235: }
236:
237: /* SHA-224 Constants
238: * (represent the first 32 bits of the fractional parts of the
239: * cube roots of the first sixty-four prime numbers)
240: */
241: static final int K[] = { 0x428a2f98, 0x71374491, 0xb5c0fbcf,
242: 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
243: 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74,
244: 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
245: 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc,
246: 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
247: 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85,
248: 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb,
249: 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70,
250: 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
251: 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3,
252: 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f,
253: 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7,
254: 0xc67178f2 };
255: }
|