|
| 1 | +/*[INCLUDE-IF CRIU_SUPPORT]*/ |
| 2 | +/* |
| 3 | + * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved. |
| 4 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 5 | + * |
| 6 | + * This code is free software; you can redistribute it and/or modify it |
| 7 | + * under the terms of the GNU General Public License version 2 only, as |
| 8 | + * published by the Free Software Foundation. Oracle designates this |
| 9 | + * particular file as subject to the "Classpath" exception as provided |
| 10 | + * by Oracle in the LICENSE file that accompanied this code. |
| 11 | + * |
| 12 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 13 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 15 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 16 | + * accompanied this code). |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU General Public License version |
| 19 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 20 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | + * |
| 22 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 23 | + * or visit www.oracle.com if you need additional information or have any |
| 24 | + * questions. |
| 25 | + */ |
| 26 | + |
| 27 | +/* |
| 28 | + * =========================================================================== |
| 29 | + * (c) Copyright IBM Corp. 2022, 2022 All Rights Reserved |
| 30 | + * =========================================================================== |
| 31 | + */ |
| 32 | + |
| 33 | +package openj9.internal.criu; |
| 34 | + |
| 35 | +import java.io.InputStream; |
| 36 | +import java.io.IOException; |
| 37 | +import java.security.MessageDigest; |
| 38 | +import java.security.NoSuchAlgorithmException; |
| 39 | +import java.security.NoSuchProviderException; |
| 40 | +import java.util.Arrays; |
| 41 | + |
| 42 | +/** |
| 43 | + * <p>This class provides a crytpographically strong pseudo-random number |
| 44 | + * generator based on the SHA-1 hash algorithm. |
| 45 | + * |
| 46 | + * <p>Seed must be provided externally. |
| 47 | + * |
| 48 | + * <p>Also note that when a random object is deserialized, |
| 49 | + * <a href="#engineNextBytes(byte[])">engineNextBytes</a> invoked on the |
| 50 | + * restored random object will yield the exact same (random) bytes as the |
| 51 | + * original object. If this behaviour is not desired, the restored random |
| 52 | + * object should be seeded, using |
| 53 | + * <a href="#engineSetSeed(byte[])">engineSetSeed</a>. |
| 54 | + * |
| 55 | + * @author Benjamin Renaud |
| 56 | + * @author Josh Bloch |
| 57 | + * @author Gadi Guy |
| 58 | + */ |
| 59 | + |
| 60 | +public final class SHA1PRNG implements java.io.Serializable { |
| 61 | + |
| 62 | + private static final long serialVersionUID = 3581829991155417889L; |
| 63 | + |
| 64 | + // SHA-1 Digest yields 160-bit hashes which require 20 bytes of space. |
| 65 | + private static final int DIGEST_SIZE = 20; |
| 66 | + private transient MessageDigest digest; |
| 67 | + private byte[] state; |
| 68 | + private byte[] remainder; |
| 69 | + private int remCount; |
| 70 | + |
| 71 | + // This class is a modified version of the SHA1PRNG SecureRandom implementation |
| 72 | + // that is found at sun.security.provider.SecureRandom. |
| 73 | + // It was modified to be used by CRIUSEC NativePRNG as a mixing data source. |
| 74 | + // Auto-seeding was removed, it is always seeded by NativePRNG from a |
| 75 | + // blocking entropy source. |
| 76 | + |
| 77 | + private SHA1PRNG(byte[] seed) { |
| 78 | + init(seed); |
| 79 | + } |
| 80 | + |
| 81 | + static SHA1PRNG seedFrom(InputStream in) throws IOException { |
| 82 | + byte[] seed = new byte[DIGEST_SIZE]; |
| 83 | + if (in.readNBytes(seed, 0, DIGEST_SIZE) != DIGEST_SIZE) { |
| 84 | + throw new IOException("Could not read seed"); |
| 85 | + } |
| 86 | + return new SHA1PRNG(seed); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * This call, used by the constructor, instantiates the SHA digest |
| 91 | + * and sets the seed. |
| 92 | + */ |
| 93 | + private void init(byte[] seed) { |
| 94 | + if (seed == null) { |
| 95 | + throw new InternalError("internal error: no seed available."); |
| 96 | + } |
| 97 | + |
| 98 | + try { |
| 99 | + digest = MessageDigest.getInstance("SHA-1", "CRIUSEC"); |
| 100 | + } catch (NoSuchProviderException | NoSuchAlgorithmException e) { |
| 101 | + throw new InternalError("internal error: SHA-1 not available.", e); |
| 102 | + } |
| 103 | + |
| 104 | + engineSetSeed(seed); |
| 105 | + } |
| 106 | + |
| 107 | + |
| 108 | + /** |
| 109 | + * Reseeds this random object. The given seed supplements, rather than |
| 110 | + * replaces, the existing seed. Thus, repeated calls are guaranteed |
| 111 | + * never to reduce randomness. |
| 112 | + * |
| 113 | + * @param seed the seed. |
| 114 | + */ |
| 115 | + public synchronized void engineSetSeed(byte[] seed) { |
| 116 | + if (state != null) { |
| 117 | + digest.update(state); |
| 118 | + for (int i = 0; i < state.length; i++) { |
| 119 | + state[i] = 0; |
| 120 | + } |
| 121 | + } |
| 122 | + state = digest.digest(seed); |
| 123 | + remCount = 0; |
| 124 | + } |
| 125 | + |
| 126 | + private static void updateState(byte[] state, byte[] output) { |
| 127 | + int carry = 1; |
| 128 | + boolean collision = true; |
| 129 | + |
| 130 | + // state(n + 1) = (state(n) + output(n) + 1) % 2^160; |
| 131 | + for (int i = 0; i < state.length; i++) { |
| 132 | + // Add two bytes. |
| 133 | + int stateCalc = (state[i] & 0xFF) + (output[i] & 0xFF) + carry; |
| 134 | + // Result is lower 8 bits. |
| 135 | + byte newState = (byte)stateCalc; |
| 136 | + // Store result. Check for state collision. |
| 137 | + collision &= (state[i] == newState); |
| 138 | + state[i] = newState; |
| 139 | + // High 8 bits are carry. Store for next iteration. |
| 140 | + carry = stateCalc >>> 8; |
| 141 | + } |
| 142 | + |
| 143 | + // Make sure at least one bit changes. |
| 144 | + if (collision) { |
| 145 | + state[0]++; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + |
| 150 | + /** |
| 151 | + * Generates a user-specified number of random bytes. |
| 152 | + * |
| 153 | + * @param result the array to be filled in with random bytes. |
| 154 | + */ |
| 155 | + public synchronized void engineNextBytes(byte[] result) { |
| 156 | + int index = 0; |
| 157 | + byte[] output = remainder; |
| 158 | + |
| 159 | + // Use remainder from last time. |
| 160 | + int r = remCount; |
| 161 | + if (r > 0) { |
| 162 | + // Compute how many bytes to be copied. |
| 163 | + int todo = Math.min(result.length - index, DIGEST_SIZE - r); |
| 164 | + // Copy the bytes, zero the buffer. |
| 165 | + for (int i = 0; i < todo; i++) { |
| 166 | + result[i] = output[r]; |
| 167 | + output[r++] = 0; |
| 168 | + } |
| 169 | + remCount += todo; |
| 170 | + index += todo; |
| 171 | + } |
| 172 | + |
| 173 | + // If we need more bytes, make them. |
| 174 | + while (index < result.length) { |
| 175 | + // Step the state. |
| 176 | + digest.update(state); |
| 177 | + output = digest.digest(); |
| 178 | + updateState(state, output); |
| 179 | + |
| 180 | + // Compute how many bytes to be copied. |
| 181 | + int todo = Math.min(result.length - index, DIGEST_SIZE); |
| 182 | + // Copy the bytes, zero the buffer. |
| 183 | + for (int i = 0; i < todo; i++) { |
| 184 | + result[index++] = output[i]; |
| 185 | + output[i] = 0; |
| 186 | + } |
| 187 | + remCount += todo; |
| 188 | + } |
| 189 | + |
| 190 | + // Store remainder for next time. |
| 191 | + remainder = output; |
| 192 | + remCount %= DIGEST_SIZE; |
| 193 | + } |
| 194 | + |
| 195 | + void clearState() { |
| 196 | + Arrays.fill(state, (byte) 0x00); |
| 197 | + Arrays.fill(remainder, (byte) 0x00); |
| 198 | + remCount = 0; |
| 199 | + } |
| 200 | +} |
0 commit comments