|
1 | 1 | package org.bouncycastle.crypto.digests; |
2 | 2 |
|
3 | | - |
4 | 3 | import org.bouncycastle.crypto.CryptoServicePurpose; |
| 4 | +import org.bouncycastle.crypto.CryptoServicesRegistrar; |
| 5 | +import org.bouncycastle.crypto.SavableDigest; |
| 6 | +import org.bouncycastle.util.Memoable; |
| 7 | +import org.bouncycastle.util.Pack; |
5 | 8 |
|
6 | 9 | /** |
7 | 10 | * implementation of SHA-3 based on following KeccakNISTInterface.c from https://keccak.noekeon.org/ |
|
10 | 13 | */ |
11 | 14 | public class SHA3Digest |
12 | 15 | extends KeccakDigest |
| 16 | + implements SavableDigest |
13 | 17 | { |
14 | 18 | private static int checkBitLength(int bitLength) |
15 | 19 | { |
@@ -45,11 +49,51 @@ public SHA3Digest(int bitLength, CryptoServicePurpose purpose) |
45 | 49 | super(checkBitLength(bitLength), purpose); |
46 | 50 | } |
47 | 51 |
|
| 52 | + public SHA3Digest(byte[] encodedState) |
| 53 | + { |
| 54 | + super(getCryptoServicePurpose(encodedState[encodedState.length - 1])); |
| 55 | + |
| 56 | + Pack.bigEndianToLong(encodedState, 0, state, 0, state.length); |
| 57 | + int encOff = state.length * 8; |
| 58 | + System.arraycopy(encodedState, encOff, dataQueue, 0, dataQueue.length); |
| 59 | + encOff += dataQueue.length; |
| 60 | + rate = Pack.bigEndianToInt(encodedState, encOff); |
| 61 | + encOff += 4; |
| 62 | + bitsInQueue = Pack.bigEndianToInt(encodedState, encOff); |
| 63 | + encOff += 4; |
| 64 | + fixedOutputLength = Pack.bigEndianToInt(encodedState, encOff); |
| 65 | + encOff += 4; |
| 66 | + squeezing = encodedState[encOff] != 0; |
| 67 | + } |
| 68 | + |
| 69 | + private static CryptoServicePurpose getCryptoServicePurpose(byte b) |
| 70 | + { |
| 71 | + CryptoServicePurpose[] values = CryptoServicePurpose.values(); |
| 72 | + return values[b]; |
| 73 | + } |
| 74 | + |
48 | 75 | public SHA3Digest(SHA3Digest source) |
49 | 76 | { |
50 | 77 | super(source); |
51 | 78 | } |
52 | 79 |
|
| 80 | + private void copyIn(SHA3Digest source) |
| 81 | + { |
| 82 | + if (this.purpose != source.purpose) |
| 83 | + { |
| 84 | + throw new IllegalArgumentException("attempt to copy digest of different purpose"); |
| 85 | + } |
| 86 | + |
| 87 | + System.arraycopy(source.state, 0, this.state, 0, source.state.length); |
| 88 | + System.arraycopy(source.dataQueue, 0, this.dataQueue, 0, source.dataQueue.length); |
| 89 | + this.rate = source.rate; |
| 90 | + this.bitsInQueue = source.bitsInQueue; |
| 91 | + this.fixedOutputLength = source.fixedOutputLength; |
| 92 | + this.squeezing = source.squeezing; |
| 93 | + |
| 94 | + CryptoServicesRegistrar.checkConstraints(cryptoServiceProperties()); |
| 95 | + } |
| 96 | + |
53 | 97 | public String getAlgorithmName() |
54 | 98 | { |
55 | 99 | return "SHA3-" + fixedOutputLength; |
@@ -84,4 +128,41 @@ protected int doFinal(byte[] out, int outOff, byte partialByte, int partialBits) |
84 | 128 |
|
85 | 129 | return super.doFinal(out, outOff, (byte)finalInput, finalBits); |
86 | 130 | } |
| 131 | + |
| 132 | + public byte[] getEncodedState() |
| 133 | + { |
| 134 | + byte[] encState = new byte[state.length * 8 + dataQueue.length + 12 + 2]; |
| 135 | + |
| 136 | + for (int i = 0; i != state.length; i++) |
| 137 | + { |
| 138 | + Pack.longToBigEndian(state[i], encState, i * 8); |
| 139 | + } |
| 140 | + |
| 141 | + int sOff = state.length * 8; |
| 142 | + System.arraycopy(dataQueue, 0, encState, sOff, dataQueue.length); |
| 143 | + |
| 144 | + sOff += dataQueue.length; |
| 145 | + Pack.intToBigEndian(rate, encState, sOff); |
| 146 | + sOff += 4; |
| 147 | + Pack.intToBigEndian(bitsInQueue, encState, sOff); |
| 148 | + sOff += 4; |
| 149 | + Pack.intToBigEndian(fixedOutputLength, encState, sOff); |
| 150 | + sOff += 4; |
| 151 | + encState[sOff++] = squeezing ? (byte)1 : (byte)0; |
| 152 | + encState[sOff] = (byte)purpose.ordinal(); |
| 153 | + |
| 154 | + return encState; |
| 155 | + } |
| 156 | + |
| 157 | + public Memoable copy() |
| 158 | + { |
| 159 | + return new SHA3Digest(this); |
| 160 | + } |
| 161 | + |
| 162 | + public void reset(Memoable other) |
| 163 | + { |
| 164 | + SHA3Digest d = (SHA3Digest)other; |
| 165 | + |
| 166 | + copyIn(d); |
| 167 | + } |
87 | 168 | } |
0 commit comments