| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.security; |
| 7 |
|
| 8 |
import java.security.MessageDigest; |
| 9 |
import java.util.Arrays; |
| 10 |
|
| 11 |
import org.h2.util.Bits; |
| 12 |
|
| 13 |
/** |
| 14 |
* SHA-3 message digest family. |
| 15 |
*/ |
| 16 |
public final class SHA3 extends MessageDigest { |
| 17 |
|
| 18 |
private static final long[] ROUND_CONSTANTS; |
| 19 |
|
| 20 |
static { |
| 21 |
long[] rc = new long[24]; |
| 22 |
byte l = 1; |
| 23 |
for (int i = 0; i < 24; i++) { |
| 24 |
rc[i] = 0; |
| 25 |
for (int j = 0; j < 7; j++) { |
| 26 |
byte t = l; |
| 27 |
l = (byte) (t < 0 ? t << 1 ^ 0x71 : t << 1); |
| 28 |
if ((t & 1) != 0) { |
| 29 |
rc[i] ^= 1L << (1 << j) - 1; |
| 30 |
} |
| 31 |
} |
| 32 |
} |
| 33 |
ROUND_CONSTANTS = rc; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Returns a new instance of SHA3-224 message digest. |
| 38 |
* |
| 39 |
* @return SHA3-224 message digest |
| 40 |
*/ |
| 41 |
public static SHA3 getSha3_224() { |
| 42 |
return new SHA3("SHA3-224", 28); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Returns a new instance of SHA3-256 message digest. |
| 47 |
* |
| 48 |
* @return SHA3-256 message digest |
| 49 |
*/ |
| 50 |
public static SHA3 getSha3_256() { |
| 51 |
return new SHA3("SHA3-256", 32); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Returns a new instance of SHA3-384 message digest. |
| 56 |
* |
| 57 |
* @return SHA3-384 message digest |
| 58 |
*/ |
| 59 |
public static SHA3 getSha3_384() { |
| 60 |
return new SHA3("SHA3-384", 48); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Returns a new instance of SHA3-512 message digest. |
| 65 |
* |
| 66 |
* @return SHA3-512 message digest |
| 67 |
*/ |
| 68 |
public static SHA3 getSha3_512() { |
| 69 |
return new SHA3("SHA3-512", 64); |
| 70 |
} |
| 71 |
|
| 72 |
private final int digestLength; |
| 73 |
|
| 74 |
private final int rate; |
| 75 |
|
| 76 |
private long state00, state01, state02, state03, state04, state05, state06, state07, state08, state09, // |
| 77 |
state10, state11, state12, state13, state14, state15, state16, state17, state18, state19, // |
| 78 |
state20, state21, state22, state23, state24; |
| 79 |
|
| 80 |
private final byte[] buf; |
| 81 |
|
| 82 |
private int bufcnt; |
| 83 |
|
| 84 |
private SHA3(String algorithm, int digestLength) { |
| 85 |
super(algorithm); |
| 86 |
this.digestLength = digestLength; |
| 87 |
buf = new byte[this.rate = 200 - digestLength * 2]; |
| 88 |
} |
| 89 |
|
| 90 |
@Override |
| 91 |
protected byte[] engineDigest() { |
| 92 |
buf[bufcnt] = 0b110; |
| 93 |
Arrays.fill(buf, bufcnt + 1, rate, (byte) 0); |
| 94 |
buf[rate - 1] |= 0x80; |
| 95 |
absorbQueue(); |
| 96 |
byte[] r = new byte[digestLength]; |
| 97 |
switch (digestLength) { |
| 98 |
case 64: |
| 99 |
Bits.writeLongLE(r, 56, state07); |
| 100 |
Bits.writeLongLE(r, 48, state06); |
| 101 |
//$FALL-THROUGH$ |
| 102 |
case 48: |
| 103 |
Bits.writeLongLE(r, 40, state05); |
| 104 |
Bits.writeLongLE(r, 32, state04); |
| 105 |
//$FALL-THROUGH$ |
| 106 |
case 32: |
| 107 |
Bits.writeLongLE(r, 24, state03); |
| 108 |
break; |
| 109 |
case 28: |
| 110 |
Bits.writeIntLE(r, 24, (int) state03); |
| 111 |
} |
| 112 |
Bits.writeLongLE(r, 16, state02); |
| 113 |
Bits.writeLongLE(r, 8, state01); |
| 114 |
Bits.writeLongLE(r, 0, state00); |
| 115 |
engineReset(); |
| 116 |
return r; |
| 117 |
} |
| 118 |
|
| 119 |
@Override |
| 120 |
protected int engineGetDigestLength() { |
| 121 |
return digestLength; |
| 122 |
} |
| 123 |
|
| 124 |
@Override |
| 125 |
protected void engineReset() { |
| 126 |
state24 = state23 = state22 = state21 = state20 // |
| 127 |
= state19 = state18 = state17 = state16 = state15 // |
| 128 |
= state14 = state13 = state12 = state11 = state10 // |
| 129 |
= state09 = state08 = state07 = state06 = state05 // |
| 130 |
= state04 = state03 = state02 = state01 = state00 = 0L; |
| 131 |
Arrays.fill(buf, (byte) 0); |
| 132 |
bufcnt = 0; |
| 133 |
} |
| 134 |
|
| 135 |
@Override |
| 136 |
protected void engineUpdate(byte input) { |
| 137 |
buf[bufcnt++] = input; |
| 138 |
if (bufcnt == rate) { |
| 139 |
absorbQueue(); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
@Override |
| 144 |
protected void engineUpdate(byte[] input, int offset, int len) { |
| 145 |
while (len > 0) { |
| 146 |
if (bufcnt == 0 && len >= rate) { |
| 147 |
do { |
| 148 |
absorb(input, offset); |
| 149 |
offset += rate; |
| 150 |
len -= rate; |
| 151 |
} while (len >= rate); |
| 152 |
} else { |
| 153 |
int partialBlock = Math.min(len, rate - bufcnt); |
| 154 |
System.arraycopy(input, offset, buf, bufcnt, partialBlock); |
| 155 |
bufcnt += partialBlock; |
| 156 |
offset += partialBlock; |
| 157 |
len -= partialBlock; |
| 158 |
if (bufcnt == rate) { |
| 159 |
absorbQueue(); |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
private void absorbQueue() { |
| 166 |
absorb(buf, 0); |
| 167 |
bufcnt = 0; |
| 168 |
} |
| 169 |
|
| 170 |
private void absorb(byte[] data, int offset) { |
| 171 |
/* |
| 172 |
* There is no need to copy 25 state* fields into local variables, |
| 173 |
* because so large number of local variables only hurts performance. |
| 174 |
*/ |
| 175 |
switch (digestLength) { |
| 176 |
case 28: |
| 177 |
state17 ^= Bits.readLongLE(data, offset + 136); |
| 178 |
//$FALL-THROUGH$ |
| 179 |
case 32: |
| 180 |
state13 ^= Bits.readLongLE(data, offset + 104); |
| 181 |
state14 ^= Bits.readLongLE(data, offset + 112); |
| 182 |
state15 ^= Bits.readLongLE(data, offset + 120); |
| 183 |
state16 ^= Bits.readLongLE(data, offset + 128); |
| 184 |
//$FALL-THROUGH$ |
| 185 |
case 48: |
| 186 |
state09 ^= Bits.readLongLE(data, offset + 72); |
| 187 |
state10 ^= Bits.readLongLE(data, offset + 80); |
| 188 |
state11 ^= Bits.readLongLE(data, offset + 88); |
| 189 |
state12 ^= Bits.readLongLE(data, offset + 96); |
| 190 |
} |
| 191 |
state00 ^= Bits.readLongLE(data, offset); |
| 192 |
state01 ^= Bits.readLongLE(data, offset + 8); |
| 193 |
state02 ^= Bits.readLongLE(data, offset + 16); |
| 194 |
state03 ^= Bits.readLongLE(data, offset + 24); |
| 195 |
state04 ^= Bits.readLongLE(data, offset + 32); |
| 196 |
state05 ^= Bits.readLongLE(data, offset + 40); |
| 197 |
state06 ^= Bits.readLongLE(data, offset + 48); |
| 198 |
state07 ^= Bits.readLongLE(data, offset + 56); |
| 199 |
state08 ^= Bits.readLongLE(data, offset + 64); |
| 200 |
for (int i = 0; i < 24; i++) { |
| 201 |
long c0 = state00 ^ state05 ^ state10 ^ state15 ^ state20; |
| 202 |
long c1 = state01 ^ state06 ^ state11 ^ state16 ^ state21; |
| 203 |
long c2 = state02 ^ state07 ^ state12 ^ state17 ^ state22; |
| 204 |
long c3 = state03 ^ state08 ^ state13 ^ state18 ^ state23; |
| 205 |
long c4 = state04 ^ state09 ^ state14 ^ state19 ^ state24; |
| 206 |
long dX = (c1 << 1 | c1 >>> 63) ^ c4; |
| 207 |
state00 ^= dX; |
| 208 |
state05 ^= dX; |
| 209 |
state10 ^= dX; |
| 210 |
state15 ^= dX; |
| 211 |
state20 ^= dX; |
| 212 |
dX = (c2 << 1 | c2 >>> 63) ^ c0; |
| 213 |
state01 ^= dX; |
| 214 |
state06 ^= dX; |
| 215 |
state11 ^= dX; |
| 216 |
state16 ^= dX; |
| 217 |
state21 ^= dX; |
| 218 |
dX = (c3 << 1 | c3 >>> 63) ^ c1; |
| 219 |
state02 ^= dX; |
| 220 |
state07 ^= dX; |
| 221 |
state12 ^= dX; |
| 222 |
state17 ^= dX; |
| 223 |
state22 ^= dX; |
| 224 |
dX = (c4 << 1 | c4 >>> 63) ^ c2; |
| 225 |
state03 ^= dX; |
| 226 |
state08 ^= dX; |
| 227 |
state13 ^= dX; |
| 228 |
state18 ^= dX; |
| 229 |
state23 ^= dX; |
| 230 |
dX = (c0 << 1 | c0 >>> 63) ^ c3; |
| 231 |
state04 ^= dX; |
| 232 |
state09 ^= dX; |
| 233 |
state14 ^= dX; |
| 234 |
state19 ^= dX; |
| 235 |
state24 ^= dX; |
| 236 |
long s00 = state00; |
| 237 |
long s01 = state06 << 44 | state06 >>> 20; |
| 238 |
long s02 = state12 << 43 | state12 >>> 21; |
| 239 |
long s03 = state18 << 21 | state18 >>> 43; |
| 240 |
long s04 = state24 << 14 | state24 >>> 50; |
| 241 |
long s05 = state03 << 28 | state03 >>> 36; |
| 242 |
long s06 = state09 << 20 | state09 >>> 44; |
| 243 |
long s07 = state10 << 3 | state10 >>> 61; |
| 244 |
long s08 = state16 << 45 | state16 >>> 19; |
| 245 |
long s09 = state22 << 61 | state22 >>> 3; |
| 246 |
long s10 = state01 << 1 | state01 >>> 63; |
| 247 |
long s11 = state07 << 6 | state07 >>> 58; |
| 248 |
long s12 = state13 << 25 | state13 >>> 39; |
| 249 |
long s13 = state19 << 8 | state19 >>> 56; |
| 250 |
long s14 = state20 << 18 | state20 >>> 46; |
| 251 |
long s15 = state04 << 27 | state04 >>> 37; |
| 252 |
long s16 = state05 << 36 | state05 >>> 28; |
| 253 |
long s17 = state11 << 10 | state11 >>> 54; |
| 254 |
long s18 = state17 << 15 | state17 >>> 49; |
| 255 |
long s19 = state23 << 56 | state23 >>> 8; |
| 256 |
long s20 = state02 << 62 | state02 >>> 2; |
| 257 |
long s21 = state08 << 55 | state08 >>> 9; |
| 258 |
long s22 = state14 << 39 | state14 >>> 25; |
| 259 |
long s23 = state15 << 41 | state15 >>> 23; |
| 260 |
long s24 = state21 << 2 | state21 >>> 62; |
| 261 |
state00 = s00 ^ ~s01 & s02 ^ ROUND_CONSTANTS[i]; |
| 262 |
state01 = s01 ^ ~s02 & s03; |
| 263 |
state02 = s02 ^ ~s03 & s04; |
| 264 |
state03 = s03 ^ ~s04 & s00; |
| 265 |
state04 = s04 ^ ~s00 & s01; |
| 266 |
state05 = s05 ^ ~s06 & s07; |
| 267 |
state06 = s06 ^ ~s07 & s08; |
| 268 |
state07 = s07 ^ ~s08 & s09; |
| 269 |
state08 = s08 ^ ~s09 & s05; |
| 270 |
state09 = s09 ^ ~s05 & s06; |
| 271 |
state10 = s10 ^ ~s11 & s12; |
| 272 |
state11 = s11 ^ ~s12 & s13; |
| 273 |
state12 = s12 ^ ~s13 & s14; |
| 274 |
state13 = s13 ^ ~s14 & s10; |
| 275 |
state14 = s14 ^ ~s10 & s11; |
| 276 |
state15 = s15 ^ ~s16 & s17; |
| 277 |
state16 = s16 ^ ~s17 & s18; |
| 278 |
state17 = s17 ^ ~s18 & s19; |
| 279 |
state18 = s18 ^ ~s19 & s15; |
| 280 |
state19 = s19 ^ ~s15 & s16; |
| 281 |
state20 = s20 ^ ~s21 & s22; |
| 282 |
state21 = s21 ^ ~s22 & s23; |
| 283 |
state22 = s22 ^ ~s23 & s24; |
| 284 |
state23 = s23 ^ ~s24 & s20; |
| 285 |
state24 = s24 ^ ~s20 & s21; |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
} |