blob: f848cdd46ae5c10d8607b7dc3debac331ea7518f [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2002-2006 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package sun.security.provider;
27
28import static sun.security.provider.ByteArrayAccess.*;
29
30/**
31 * This class implements the Secure Hash Algorithm SHA-256 developed by
32 * the National Institute of Standards and Technology along with the
33 * National Security Agency.
34 *
35 * <p>It implements java.security.MessageDigestSpi, and can be used
36 * through Java Cryptography Architecture (JCA), as a pluggable
37 * MessageDigest implementation.
38 *
39 * @since 1.4.2
40 * @author Valerie Peng
41 * @author Andreas Sterbenz
42 */
43public final class SHA2 extends DigestBase {
44
45 private static final int ITERATION = 64;
46 // Constants for each round
47 private static final int[] ROUND_CONSTS = {
48 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
49 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
50 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
51 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
52 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
53 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
54 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
55 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
56 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
57 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
58 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
59 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
60 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
61 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
62 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
63 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
64 };
65
66 // buffer used by implCompress()
67 private final int[] W;
68
69 // state of this object
70 private final int[] state;
71
72 /**
73 * Creates a new SHA object.
74 */
75 public SHA2() {
76 super("SHA-256", 32, 64);
77 state = new int[8];
78 W = new int[64];
79 implReset();
80 }
81
82 /**
83 * Creates a SHA2 object.with state (for cloning)
84 */
85 private SHA2(SHA2 base) {
86 super(base);
87 this.state = base.state.clone();
88 this.W = new int[64];
89 }
90
91 public Object clone() {
92 return new SHA2(this);
93 }
94
95 /**
96 * Resets the buffers and hash value to start a new hash.
97 */
98 void implReset() {
99 state[0] = 0x6a09e667;
100 state[1] = 0xbb67ae85;
101 state[2] = 0x3c6ef372;
102 state[3] = 0xa54ff53a;
103 state[4] = 0x510e527f;
104 state[5] = 0x9b05688c;
105 state[6] = 0x1f83d9ab;
106 state[7] = 0x5be0cd19;
107 }
108
109 void implDigest(byte[] out, int ofs) {
110 long bitsProcessed = bytesProcessed << 3;
111
112 int index = (int)bytesProcessed & 0x3f;
113 int padLen = (index < 56) ? (56 - index) : (120 - index);
114 engineUpdate(padding, 0, padLen);
115
116 i2bBig4((int)(bitsProcessed >>> 32), buffer, 56);
117 i2bBig4((int)bitsProcessed, buffer, 60);
118 implCompress(buffer, 0);
119
120 i2bBig(state, 0, out, ofs, 32);
121 }
122
123 /**
124 * logical function ch(x,y,z) as defined in spec:
125 * @return (x and y) xor ((complement x) and z)
126 * @param x int
127 * @param y int
128 * @param z int
129 */
130 private static int lf_ch(int x, int y, int z) {
131 return (x & y) ^ ((~x) & z);
132 }
133
134 /**
135 * logical function maj(x,y,z) as defined in spec:
136 * @return (x and y) xor (x and z) xor (y and z)
137 * @param x int
138 * @param y int
139 * @param z int
140 */
141 private static int lf_maj(int x, int y, int z) {
142 return (x & y) ^ (x & z) ^ (y & z);
143 }
144
145 /**
146 * logical function R(x,s) - right shift
147 * @return x right shift for s times
148 * @param x int
149 * @param s int
150 */
151 private static int lf_R( int x, int s ) {
152 return (x >>> s);
153 }
154
155 /**
156 * logical function S(x,s) - right rotation
157 * @return x circular right shift for s times
158 * @param x int
159 * @param s int
160 */
161 private static int lf_S(int x, int s) {
162 return (x >>> s) | (x << (32 - s));
163 }
164
165 /**
166 * logical function sigma0(x) - xor of results of right rotations
167 * @return S(x,2) xor S(x,13) xor S(x,22)
168 * @param x int
169 */
170 private static int lf_sigma0(int x) {
171 return lf_S(x, 2) ^ lf_S(x, 13) ^ lf_S(x, 22);
172 }
173
174 /**
175 * logical function sigma1(x) - xor of results of right rotations
176 * @return S(x,6) xor S(x,11) xor S(x,25)
177 * @param x int
178 */
179 private static int lf_sigma1(int x) {
180 return lf_S( x, 6 ) ^ lf_S( x, 11 ) ^ lf_S( x, 25 );
181 }
182
183 /**
184 * logical function delta0(x) - xor of results of right shifts/rotations
185 * @return int
186 * @param x int
187 */
188 private static int lf_delta0(int x) {
189 return lf_S(x, 7) ^ lf_S(x, 18) ^ lf_R(x, 3);
190 }
191
192 /**
193 * logical function delta1(x) - xor of results of right shifts/rotations
194 * @return int
195 * @param x int
196 */
197 private static int lf_delta1(int x) {
198 return lf_S(x, 17) ^ lf_S(x, 19) ^ lf_R(x, 10);
199 }
200
201 /**
202 * Process the current block to update the state variable state.
203 */
204 void implCompress(byte[] buf, int ofs) {
205 b2iBig64(buf, ofs, W);
206
207 // The first 16 ints are from the byte stream, compute the rest of
208 // the W[]'s
209 for (int t = 16; t < ITERATION; t++) {
210 W[t] = lf_delta1(W[t-2]) + W[t-7] + lf_delta0(W[t-15])
211 + W[t-16];
212 }
213
214 int a = state[0];
215 int b = state[1];
216 int c = state[2];
217 int d = state[3];
218 int e = state[4];
219 int f = state[5];
220 int g = state[6];
221 int h = state[7];
222
223 for (int i = 0; i < ITERATION; i++) {
224 int T1 = h + lf_sigma1(e) + lf_ch(e,f,g) + ROUND_CONSTS[i] + W[i];
225 int T2 = lf_sigma0(a) + lf_maj(a,b,c);
226 h = g;
227 g = f;
228 f = e;
229 e = d + T1;
230 d = c;
231 c = b;
232 b = a;
233 a = T1 + T2;
234 }
235 state[0] += a;
236 state[1] += b;
237 state[2] += c;
238 state[3] += d;
239 state[4] += e;
240 state[5] += f;
241 state[6] += g;
242 state[7] += h;
243 }
244
245}