|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
|
|
package sun.security.provider; |
|
|
|
import java.math.BigInteger; |
|
import java.security.DigestException; |
|
import java.security.MessageDigest; |
|
import java.security.NoSuchAlgorithmException; |
|
import java.security.NoSuchProviderException; |
|
import java.security.SecureRandomParameters; |
|
import java.util.ArrayList; |
|
import java.util.Arrays; |
|
import java.util.List; |
|
|
|
public class HashDrbg extends AbstractHashDrbg { |
|
|
|
private static final byte[] ZERO = new byte[1]; |
|
private static final byte[] ONE = new byte[]{1}; |
|
|
|
private MessageDigest digest; |
|
|
|
private byte[] v; |
|
private byte[] c; |
|
|
|
public HashDrbg(SecureRandomParameters params) { |
|
mechName = "Hash_DRBG"; |
|
configure(params); |
|
} |
|
|
|
|
|
|
|
*/ |
|
@Override |
|
protected void initEngine() { |
|
try { |
|
|
|
|
|
|
|
*/ |
|
digest = MessageDigest.getInstance(algorithm, "SUN"); |
|
} catch (NoSuchProviderException | NoSuchAlgorithmException e) { |
|
|
|
try { |
|
digest = MessageDigest.getInstance(algorithm); |
|
} catch (NoSuchAlgorithmException exc) { |
|
throw new InternalError( |
|
"internal error: " + algorithm + " not available.", exc); |
|
} |
|
} |
|
} |
|
|
|
private byte[] hashDf(int requested, List<byte[]> inputs) { |
|
return hashDf(digest, outLen, requested, inputs); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public static byte[] hashDf(MessageDigest digest, int outLen, |
|
int requested, List<byte[]> inputs) { |
|
// 1. temp = the Null string. |
|
|
|
int len = (requested + outLen - 1) / outLen; |
|
byte[] temp = new byte[len * outLen]; |
|
|
|
int counter = 1; |
|
|
|
|
|
for (int i=0; i<len; i++) { |
|
// 4.1 temp = temp |
|
|
|
digest.update((byte) counter); |
|
digest.update((byte)(requested >> 21)); |
|
digest.update((byte)(requested >> 13)); |
|
digest.update((byte)(requested >> 5)); |
|
digest.update((byte)(requested << 3)); |
|
for (byte[] input : inputs) { |
|
digest.update(input); |
|
} |
|
try { |
|
digest.digest(temp, i * outLen, outLen); |
|
} catch (DigestException e) { |
|
throw new AssertionError("will not happen", e); |
|
} |
|
|
|
counter++; |
|
} |
|
|
|
return temp.length == requested? temp: Arrays.copyOf(temp, requested); |
|
// 6. Return |
|
} |
|
|
|
|
|
@Override |
|
protected final synchronized void hashReseedInternal(List<byte[]> inputs) { |
|
|
|
// 800-90Ar1 10.1.1.2: Instantiate Process. |
|
|
|
byte[] seed; |
|
|
|
|
|
if (v != null) { |
|
|
|
inputs.add(0, ONE); |
|
inputs.add(1, v); |
|
seed = hashDf(seedLen, inputs); |
|
} else { |
|
seed = hashDf(seedLen, inputs); |
|
} |
|
|
|
|
|
v = seed; |
|
|
|
|
|
inputs = new ArrayList<>(2); |
|
inputs.add(ZERO); |
|
inputs.add(v); |
|
c = hashDf(seedLen, inputs); |
|
|
|
|
|
reseedCounter = 1; |
|
|
|
//status(); |
|
|
|
// Step 6: Return |
|
} |
|
|
|
private void status() { |
|
if (debug != null) { |
|
debug.println(this, "V = " + hex(v)); |
|
debug.println(this, "C = " + hex(c)); |
|
debug.println(this, "reseed counter = " + reseedCounter); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private static void addBytes(byte[] out, int len, byte[]... data) { |
|
for (byte[] d: data) { |
|
int dlen = d.length; |
|
int carry = 0; |
|
for (int i = 0; i < len; i++) { |
|
int sum = (out[len - i - 1] & 0xff) + carry; |
|
if (i < dlen) { |
|
sum += (d[dlen - i - 1] & 0xff); |
|
} |
|
out[len - i - 1] = (byte) sum; |
|
carry = sum >> 8; |
|
if (i >= dlen - 1 && carry == 0) break; |
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
@Override |
|
public final synchronized void generateAlgorithm( |
|
byte[] result, byte[] additionalInput) { |
|
|
|
if (debug != null) { |
|
debug.println(this, "generateAlgorithm"); |
|
} |
|
|
|
// 800-90Ar1 10.1.1.4: Hash_DRBG_Generate Process |
|
|
|
// Step 1: Check reseed_counter. Will not fail. Already checked in |
|
// AbstractDrbg#engineNextBytes. |
|
|
|
|
|
if (additionalInput != null) { |
|
digest.update((byte)2); |
|
digest.update(v); |
|
digest.update(additionalInput); |
|
addBytes(v, seedLen, digest.digest()); |
|
} |
|
|
|
|
|
hashGen(result, v); |
|
|
|
|
|
digest.update((byte)3); |
|
digest.update(v); |
|
byte[] h = digest.digest(); |
|
|
|
|
|
byte[] rcBytes; |
|
if (reseedCounter < 256) { |
|
rcBytes = new byte[]{(byte)reseedCounter}; |
|
} else { |
|
rcBytes = BigInteger.valueOf(reseedCounter).toByteArray(); |
|
} |
|
addBytes(v, seedLen, h, c, rcBytes); |
|
|
|
|
|
reseedCounter++; |
|
|
|
//status(); |
|
|
|
// Step 7: Return. |
|
} |
|
|
|
|
|
private void hashGen(byte[] output, byte[] v) { |
|
|
|
|
|
byte[] data = v; |
|
|
|
// Step 3: W is output not filled |
|
|
|
|
|
int pos = 0; |
|
int len = output.length; |
|
|
|
while (len > 0) { |
|
if (len < outLen) { |
|
// Step 4.1 w = Hash (data). |
|
|
|
System.arraycopy(digest.digest(data), 0, output, pos, |
|
len); |
|
} else { |
|
try { |
|
|
|
digest.update(data); |
|
|
|
digest.digest(output, pos, outLen); |
|
} catch (DigestException e) { |
|
throw new AssertionError("will not happen", e); |
|
} |
|
} |
|
len -= outLen; |
|
if (len <= 0) { |
|
|
|
break; |
|
} |
|
|
|
if (data == v) { |
|
data = Arrays.copyOf(v, v.length); |
|
} |
|
addBytes(data, seedLen, ONE); |
|
pos += outLen; |
|
} |
|
|
|
// Step 5: No need to truncate |
|
// Step 6: Return |
|
} |
|
} |