what-the-bank/sources/org/bouncycastle/crypto/digests/SHA256Digest.java

262 lines
9.3 KiB
Java
Raw Permalink Normal View History

2024-07-27 18:17:47 +07:00
package org.bouncycastle.crypto.digests;
import com.google.common.base.Ascii;
import com.google.common.primitives.UnsignedBytes;
import org.bouncycastle.pqc.jcajce.spec.McElieceCCA2KeyGenParameterSpec;
import org.bouncycastle.util.Memoable;
import org.bouncycastle.util.Pack;
/* loaded from: classes6.dex */
public class SHA256Digest extends GeneralDigest implements EncodableDigest {
private static final int DIGEST_LENGTH = 32;
static final int[] K = {1116352408, 1899447441, -1245643825, -373957723, 961987163, 1508970993, -1841331548, -1424204075, -670586216, 310598401, 607225278, 1426881987, 1925078388, -2132889090, -1680079193, -1046744716, -459576895, -272742522, 264347078, 604807628, 770255983, 1249150122, 1555081692, 1996064986, -1740746414, -1473132947, -1341970488, -1084653625, -958395405, -710438585, 113926993, 338241895, 666307205, 773529912, 1294757372, 1396182291, 1695183700, 1986661051, -2117940946, -1838011259, -1564481375, -1474664885, -1035236496, -949202525, -778901479, -694614492, -200395387, 275423344, 430227734, 506948616, 659060556, 883997877, 958139571, 1322822218, 1537002063, 1747873779, 1955562222, 2024104815, -2067236844, -1933114872, -1866530822, -1538233109, -1090935817, -965641998};
private int H1;
private int H2;
private int H3;
private int H4;
private int H5;
private int H6;
private int H7;
private int H8;
private int[] X;
private int xOff;
private int Ch(int i, int i2, int i3) {
return (i & i2) ^ (i3 & (~i));
}
private int Maj(int i, int i2, int i3) {
return ((i & i2) ^ (i & i3)) ^ (i2 & i3);
}
private int Sum0(int i) {
return (((i << 19) | (i >>> 13)) ^ ((i >>> 2) | (i << 30))) ^ ((i << 10) | (i >>> 22));
}
private int Sum1(int i) {
return (((i << 21) | (i >>> 11)) ^ ((i >>> 6) | (i << 26))) ^ ((i << 7) | (i >>> 25));
}
private int Theta0(int i) {
return (((i << 14) | (i >>> 18)) ^ ((i >>> 7) | (i << 25))) ^ (i >>> 3);
}
private int Theta1(int i) {
return (((i << 13) | (i >>> 19)) ^ ((i >>> 17) | (i << 15))) ^ (i >>> 10);
}
@Override // org.bouncycastle.crypto.Digest
public int getDigestSize() {
return 32;
}
@Override // org.bouncycastle.util.Memoable
public void reset(Memoable memoable) {
copyIn((SHA256Digest) memoable);
}
@Override // org.bouncycastle.crypto.digests.GeneralDigest, org.bouncycastle.crypto.Digest
public void reset() {
super.reset();
this.H1 = 1779033703;
this.H2 = -1150833019;
this.H3 = 1013904242;
this.H4 = -1521486534;
this.H5 = 1359893119;
this.H6 = -1694144372;
this.H7 = 528734635;
this.H8 = 1541459225;
this.xOff = 0;
int i = 0;
while (true) {
int[] iArr = this.X;
if (i == iArr.length) {
return;
}
iArr[i] = 0;
i++;
}
}
@Override // org.bouncycastle.crypto.digests.GeneralDigest
protected void processWord(byte[] bArr, int i) {
byte b = bArr[i];
byte b2 = bArr[i + 1];
byte b3 = bArr[i + 2];
byte b4 = bArr[i + 3];
int[] iArr = this.X;
int i2 = this.xOff;
iArr[i2] = (b4 & UnsignedBytes.MAX_VALUE) | (b << Ascii.CAN) | ((b2 & UnsignedBytes.MAX_VALUE) << 16) | ((b3 & UnsignedBytes.MAX_VALUE) << 8);
int i3 = i2 + 1;
this.xOff = i3;
if (i3 == 16) {
processBlock();
}
}
@Override // org.bouncycastle.crypto.digests.GeneralDigest
protected void processLength(long j) {
if (this.xOff > 14) {
processBlock();
}
int[] iArr = this.X;
iArr[14] = (int) (j >>> 32);
iArr[15] = (int) j;
}
@Override // org.bouncycastle.crypto.digests.GeneralDigest
protected void processBlock() {
for (int i = 16; i <= 63; i++) {
int[] iArr = this.X;
int Theta1 = Theta1(iArr[i - 2]);
int[] iArr2 = this.X;
iArr[i] = Theta1 + iArr2[i - 7] + Theta0(iArr2[i - 15]) + this.X[i - 16];
}
int i2 = this.H1;
int i3 = this.H2;
int i4 = this.H3;
int i5 = this.H4;
int i6 = this.H5;
int i7 = this.H6;
int i8 = this.H7;
int i9 = this.H8;
int i10 = 0;
for (int i11 = 0; i11 < 8; i11++) {
int Sum1 = Sum1(i6);
int Ch = Ch(i6, i7, i8);
int[] iArr3 = K;
int i12 = i9 + Sum1 + Ch + iArr3[i10] + this.X[i10];
int i13 = i5 + i12;
int Sum0 = i12 + Sum0(i2) + Maj(i2, i3, i4);
int i14 = i10 + 1;
int Sum12 = i8 + Sum1(i13) + Ch(i13, i6, i7) + iArr3[i14] + this.X[i14];
int i15 = i4 + Sum12;
int Sum02 = Sum12 + Sum0(Sum0) + Maj(Sum0, i2, i3);
int i16 = i10 + 2;
int Sum13 = i7 + Sum1(i15) + Ch(i15, i13, i6) + iArr3[i16] + this.X[i16];
int i17 = i3 + Sum13;
int Sum03 = Sum13 + Sum0(Sum02) + Maj(Sum02, Sum0, i2);
int i18 = i10 + 3;
int Sum14 = i6 + Sum1(i17) + Ch(i17, i15, i13) + iArr3[i18] + this.X[i18];
int i19 = i2 + Sum14;
int Sum04 = Sum14 + Sum0(Sum03) + Maj(Sum03, Sum02, Sum0);
int i20 = i10 + 4;
int Sum15 = i13 + Sum1(i19) + Ch(i19, i17, i15) + iArr3[i20] + this.X[i20];
i9 = Sum0 + Sum15;
i5 = Sum15 + Sum0(Sum04) + Maj(Sum04, Sum03, Sum02);
int i21 = i10 + 5;
int Sum16 = i15 + Sum1(i9) + Ch(i9, i19, i17) + iArr3[i21] + this.X[i21];
i8 = Sum02 + Sum16;
i4 = Sum16 + Sum0(i5) + Maj(i5, Sum04, Sum03);
int i22 = i10 + 6;
int Sum17 = i17 + Sum1(i8) + Ch(i8, i9, i19) + iArr3[i22] + this.X[i22];
i7 = Sum03 + Sum17;
i3 = Sum17 + Sum0(i4) + Maj(i4, i5, Sum04);
int i23 = i10 + 7;
int Sum18 = i19 + Sum1(i7) + Ch(i7, i8, i9) + iArr3[i23] + this.X[i23];
i6 = Sum04 + Sum18;
i2 = Sum18 + Sum0(i3) + Maj(i3, i4, i5);
i10 += 8;
}
this.H1 += i2;
this.H2 += i3;
this.H3 += i4;
this.H4 += i5;
this.H5 += i6;
this.H6 += i7;
this.H7 += i8;
this.H8 += i9;
this.xOff = 0;
for (int i24 = 0; i24 < 16; i24++) {
this.X[i24] = 0;
}
}
@Override // org.bouncycastle.crypto.digests.EncodableDigest
public byte[] getEncodedState() {
byte[] bArr = new byte[(this.xOff << 2) + 52];
super.populateState(bArr);
Pack.intToBigEndian(this.H1, bArr, 16);
Pack.intToBigEndian(this.H2, bArr, 20);
Pack.intToBigEndian(this.H3, bArr, 24);
Pack.intToBigEndian(this.H4, bArr, 28);
Pack.intToBigEndian(this.H5, bArr, 32);
Pack.intToBigEndian(this.H6, bArr, 36);
Pack.intToBigEndian(this.H7, bArr, 40);
Pack.intToBigEndian(this.H8, bArr, 44);
Pack.intToBigEndian(this.xOff, bArr, 48);
for (int i = 0; i != this.xOff; i++) {
Pack.intToBigEndian(this.X[i], bArr, (i << 2) + 52);
}
return bArr;
}
@Override // org.bouncycastle.crypto.Digest
public String getAlgorithmName() {
return McElieceCCA2KeyGenParameterSpec.SHA256;
}
@Override // org.bouncycastle.crypto.Digest
public int doFinal(byte[] bArr, int i) {
finish();
Pack.intToBigEndian(this.H1, bArr, i);
Pack.intToBigEndian(this.H2, bArr, i + 4);
Pack.intToBigEndian(this.H3, bArr, i + 8);
Pack.intToBigEndian(this.H4, bArr, i + 12);
Pack.intToBigEndian(this.H5, bArr, i + 16);
Pack.intToBigEndian(this.H6, bArr, i + 20);
Pack.intToBigEndian(this.H7, bArr, i + 24);
Pack.intToBigEndian(this.H8, bArr, i + 28);
reset();
return 32;
}
@Override // org.bouncycastle.util.Memoable
public Memoable copy() {
return new SHA256Digest(this);
}
private void copyIn(SHA256Digest sHA256Digest) {
super.copyIn((GeneralDigest) sHA256Digest);
this.H1 = sHA256Digest.H1;
this.H2 = sHA256Digest.H2;
this.H3 = sHA256Digest.H3;
this.H4 = sHA256Digest.H4;
this.H5 = sHA256Digest.H5;
this.H6 = sHA256Digest.H6;
this.H7 = sHA256Digest.H7;
this.H8 = sHA256Digest.H8;
int[] iArr = sHA256Digest.X;
System.arraycopy(iArr, 0, this.X, 0, iArr.length);
this.xOff = sHA256Digest.xOff;
}
public SHA256Digest(byte[] bArr) {
super(bArr);
this.X = new int[64];
this.H1 = Pack.bigEndianToInt(bArr, 16);
this.H2 = Pack.bigEndianToInt(bArr, 20);
this.H3 = Pack.bigEndianToInt(bArr, 24);
this.H4 = Pack.bigEndianToInt(bArr, 28);
this.H5 = Pack.bigEndianToInt(bArr, 32);
this.H6 = Pack.bigEndianToInt(bArr, 36);
this.H7 = Pack.bigEndianToInt(bArr, 40);
this.H8 = Pack.bigEndianToInt(bArr, 44);
this.xOff = Pack.bigEndianToInt(bArr, 48);
for (int i = 0; i != this.xOff; i++) {
this.X[i] = Pack.bigEndianToInt(bArr, (i << 2) + 52);
}
}
public SHA256Digest(SHA256Digest sHA256Digest) {
super(sHA256Digest);
this.X = new int[64];
copyIn(sHA256Digest);
}
public SHA256Digest() {
this.X = new int[64];
reset();
}
}