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

276 lines
7.9 KiB
Java
Raw Permalink Normal View History

2024-07-27 18:17:47 +07:00
package org.bouncycastle.crypto.digests;
import com.google.common.primitives.UnsignedBytes;
import org.bouncycastle.util.Memoable;
import org.bouncycastle.util.Pack;
/* loaded from: classes6.dex */
public class SM3Digest extends GeneralDigest {
private static final int BLOCK_SIZE = 16;
private static final int DIGEST_LENGTH = 32;
private static final int[] T = new int[64];
private int[] V;
private int[] W;
private int[] W1;
private int[] inwords;
private int xOff;
private int FF0(int i, int i2, int i3) {
return (i ^ i2) ^ i3;
}
private int FF1(int i, int i2, int i3) {
return (i & i2) | (i & i3) | (i2 & i3);
}
private int GG0(int i, int i2, int i3) {
return (i ^ i2) ^ i3;
}
private int GG1(int i, int i2, int i3) {
return (i & i2) | (i3 & (~i));
}
private int P0(int i) {
return ((i >>> 15) | (i << 17)) ^ (((i << 9) | (i >>> 23)) ^ i);
}
private int P1(int i) {
return ((i >>> 9) | (i << 23)) ^ (((i << 15) | (i >>> 17)) ^ i);
}
@Override // org.bouncycastle.crypto.Digest
public int getDigestSize() {
return 32;
}
@Override // org.bouncycastle.util.Memoable
public void reset(Memoable memoable) {
SM3Digest sM3Digest = (SM3Digest) memoable;
super.copyIn((GeneralDigest) sM3Digest);
copyIn(sM3Digest);
}
@Override // org.bouncycastle.crypto.digests.GeneralDigest, org.bouncycastle.crypto.Digest
public void reset() {
super.reset();
int[] iArr = this.V;
iArr[0] = 1937774191;
iArr[1] = 1226093241;
iArr[2] = 388252375;
iArr[3] = -628488704;
iArr[4] = -1452330820;
iArr[5] = 372324522;
iArr[6] = -477237683;
iArr[7] = -1325724082;
this.xOff = 0;
}
@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.inwords;
int i2 = this.xOff;
iArr[i2] = (b4 & UnsignedBytes.MAX_VALUE) | ((b & UnsignedBytes.MAX_VALUE) << 24) | ((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) {
int i = this.xOff;
if (i > 14) {
this.inwords[i] = 0;
this.xOff = i + 1;
processBlock();
}
while (true) {
int i2 = this.xOff;
if (i2 >= 14) {
int[] iArr = this.inwords;
iArr[i2] = (int) (j >>> 32);
this.xOff = i2 + 2;
iArr[i2 + 1] = (int) j;
return;
}
this.inwords[i2] = 0;
this.xOff = i2 + 1;
}
}
@Override // org.bouncycastle.crypto.digests.GeneralDigest
protected void processBlock() {
int i;
int i2;
int i3 = 0;
while (true) {
i = 16;
if (i3 >= 16) {
break;
}
this.W[i3] = this.inwords[i3];
i3++;
}
for (int i4 = 16; i4 < 68; i4++) {
int[] iArr = this.W;
int i5 = iArr[i4 - 3];
int i6 = iArr[i4 - 13];
iArr[i4] = (((i6 << 7) | (i6 >>> 25)) ^ P1(((i5 << 15) | (i5 >>> 17)) ^ (iArr[i4 - 16] ^ iArr[i4 - 9]))) ^ this.W[i4 - 6];
}
int i7 = 0;
while (true) {
i2 = 64;
if (i7 >= 64) {
break;
}
int[] iArr2 = this.W1;
int[] iArr3 = this.W;
iArr2[i7] = iArr3[i7 + 4] ^ iArr3[i7];
i7++;
}
int[] iArr4 = this.V;
int i8 = iArr4[0];
int i9 = iArr4[1];
int i10 = iArr4[2];
int i11 = iArr4[3];
int i12 = iArr4[4];
int i13 = iArr4[5];
int i14 = iArr4[6];
int i15 = iArr4[7];
int i16 = 0;
int i17 = i13;
int i18 = i14;
while (i16 < i) {
int i19 = (i8 << 12) | (i8 >>> 20);
int i20 = i19 + i12 + T[i16];
int i21 = (i20 << 7) | (i20 >>> 25);
int FF0 = FF0(i8, i9, i10);
int i22 = this.W1[i16];
int P0 = P0(GG0(i12, i17, i18) + i15 + i21 + this.W[i16]);
i16++;
int i23 = FF0 + i11 + (i21 ^ i19) + i22;
i11 = i10;
i10 = (i9 >>> 23) | (i9 << 9);
i9 = i8;
i8 = i23;
i = 16;
int i24 = i12;
i12 = P0;
i15 = i18;
i18 = (i17 << 19) | (i17 >>> 13);
i17 = i24;
}
int i25 = i15;
int i26 = i11;
int i27 = i18;
int i28 = i10;
int i29 = i9;
int i30 = i8;
int i31 = i17;
int i32 = 16;
while (i32 < i2) {
int i33 = (i30 << 12) | (i30 >>> 20);
int i34 = i33 + i12 + T[i32];
int i35 = (i34 << 7) | (i34 >>> 25);
int FF1 = FF1(i30, i29, i28);
int i36 = this.W1[i32];
int P02 = P0(GG1(i12, i31, i27) + i25 + i35 + this.W[i32]);
i32++;
int i37 = FF1 + i26 + (i35 ^ i33) + i36;
i26 = i28;
i28 = (i29 << 9) | (i29 >>> 23);
i29 = i30;
i30 = i37;
i2 = 64;
i25 = i27;
i27 = (i31 << 19) | (i31 >>> 13);
i31 = i12;
i12 = P02;
}
int[] iArr5 = this.V;
iArr5[0] = iArr5[0] ^ i30;
iArr5[1] = iArr5[1] ^ i29;
iArr5[2] = iArr5[2] ^ i28;
iArr5[3] = iArr5[3] ^ i26;
iArr5[4] = iArr5[4] ^ i12;
iArr5[5] = i31 ^ iArr5[5];
iArr5[6] = i27 ^ iArr5[6];
iArr5[7] = i25 ^ iArr5[7];
this.xOff = 0;
}
@Override // org.bouncycastle.crypto.Digest
public String getAlgorithmName() {
return "SM3";
}
@Override // org.bouncycastle.crypto.Digest
public int doFinal(byte[] bArr, int i) {
finish();
Pack.intToBigEndian(this.V[0], bArr, i);
Pack.intToBigEndian(this.V[1], bArr, i + 4);
Pack.intToBigEndian(this.V[2], bArr, i + 8);
Pack.intToBigEndian(this.V[3], bArr, i + 12);
Pack.intToBigEndian(this.V[4], bArr, i + 16);
Pack.intToBigEndian(this.V[5], bArr, i + 20);
Pack.intToBigEndian(this.V[6], bArr, i + 24);
Pack.intToBigEndian(this.V[7], bArr, i + 28);
reset();
return 32;
}
@Override // org.bouncycastle.util.Memoable
public Memoable copy() {
return new SM3Digest(this);
}
private void copyIn(SM3Digest sM3Digest) {
int[] iArr = sM3Digest.V;
int[] iArr2 = this.V;
System.arraycopy(iArr, 0, iArr2, 0, iArr2.length);
int[] iArr3 = sM3Digest.inwords;
int[] iArr4 = this.inwords;
System.arraycopy(iArr3, 0, iArr4, 0, iArr4.length);
this.xOff = sM3Digest.xOff;
}
public SM3Digest(SM3Digest sM3Digest) {
super(sM3Digest);
this.V = new int[8];
this.inwords = new int[16];
this.W = new int[68];
this.W1 = new int[64];
copyIn(sM3Digest);
}
public SM3Digest() {
this.V = new int[8];
this.inwords = new int[16];
this.W = new int[68];
this.W1 = new int[64];
reset();
}
static {
int i;
int i2 = 0;
while (true) {
if (i2 >= 16) {
break;
}
T[i2] = (2043430169 >>> (32 - i2)) | (2043430169 << i2);
i2++;
}
for (i = 16; i < 64; i++) {
int i3 = i % 32;
T[i] = (2055708042 << i3) | (2055708042 >>> (32 - i3));
}
}
}