214 lines
7.3 KiB
Java
214 lines
7.3 KiB
Java
package org.bouncycastle.pqc.crypto.gmss;
|
|
|
|
import com.huawei.hms.android.HwBuildEx;
|
|
import org.bouncycastle.crypto.Digest;
|
|
import org.bouncycastle.pqc.crypto.gmss.util.GMSSRandom;
|
|
import org.bouncycastle.util.Arrays;
|
|
import org.bouncycastle.util.encoders.Hex;
|
|
|
|
/* loaded from: classes6.dex */
|
|
public class GMSSLeaf {
|
|
private byte[] concHashs;
|
|
private GMSSRandom gmssRandom;
|
|
private int i;
|
|
private int j;
|
|
private int keysize;
|
|
private byte[] leaf;
|
|
private int mdsize;
|
|
private Digest messDigestOTS;
|
|
byte[] privateKeyOTS;
|
|
private byte[] seed;
|
|
private int steps;
|
|
private int two_power_w;
|
|
private int w;
|
|
|
|
private int getLog(int i) {
|
|
int i2 = 1;
|
|
int i3 = 2;
|
|
while (i3 < i) {
|
|
i3 <<= 1;
|
|
i2++;
|
|
}
|
|
return i2;
|
|
}
|
|
|
|
public String toString() {
|
|
StringBuilder sb;
|
|
String str = "";
|
|
for (int i = 0; i < 4; i++) {
|
|
StringBuilder sb2 = new StringBuilder();
|
|
sb2.append(str);
|
|
sb2.append(getStatInt()[i]);
|
|
sb2.append(" ");
|
|
str = sb2.toString();
|
|
}
|
|
StringBuilder sb3 = new StringBuilder();
|
|
sb3.append(str);
|
|
sb3.append(" ");
|
|
sb3.append(this.mdsize);
|
|
sb3.append(" ");
|
|
sb3.append(this.keysize);
|
|
sb3.append(" ");
|
|
sb3.append(this.two_power_w);
|
|
sb3.append(" ");
|
|
String obj = sb3.toString();
|
|
byte[][] statByte = getStatByte();
|
|
for (int i2 = 0; i2 < 4; i2++) {
|
|
if (statByte[i2] != null) {
|
|
sb = new StringBuilder();
|
|
sb.append(obj);
|
|
sb.append(new String(Hex.encode(statByte[i2])));
|
|
sb.append(" ");
|
|
} else {
|
|
sb = new StringBuilder();
|
|
sb.append(obj);
|
|
sb.append("null ");
|
|
}
|
|
obj = sb.toString();
|
|
}
|
|
return obj;
|
|
}
|
|
|
|
/* JADX INFO: Access modifiers changed from: package-private */
|
|
public GMSSLeaf nextLeaf() {
|
|
GMSSLeaf gMSSLeaf = new GMSSLeaf(this);
|
|
gMSSLeaf.updateLeafCalc();
|
|
return gMSSLeaf;
|
|
}
|
|
|
|
/* JADX INFO: Access modifiers changed from: package-private */
|
|
public void initLeafCalc(byte[] bArr) {
|
|
this.i = 0;
|
|
this.j = 0;
|
|
byte[] bArr2 = new byte[this.mdsize];
|
|
System.arraycopy(bArr, 0, bArr2, 0, this.seed.length);
|
|
this.seed = this.gmssRandom.nextSeed(bArr2);
|
|
}
|
|
|
|
public int[] getStatInt() {
|
|
return new int[]{this.i, this.j, this.steps, this.w};
|
|
}
|
|
|
|
public byte[][] getStatByte() {
|
|
int i = this.mdsize;
|
|
byte[][] bArr = {new byte[i], new byte[i], new byte[this.keysize * i], new byte[i]};
|
|
bArr[0] = this.privateKeyOTS;
|
|
bArr[1] = this.seed;
|
|
bArr[2] = this.concHashs;
|
|
bArr[3] = this.leaf;
|
|
return bArr;
|
|
}
|
|
|
|
public byte[] getLeaf() {
|
|
return Arrays.clone(this.leaf);
|
|
}
|
|
|
|
private void updateLeafCalc() {
|
|
byte[] bArr = new byte[this.messDigestOTS.getDigestSize()];
|
|
for (int i = 0; i < this.steps + HwBuildEx.VersionCodes.CUR_DEVELOPMENT; i++) {
|
|
int i2 = this.i;
|
|
if (i2 == this.keysize && this.j == this.two_power_w - 1) {
|
|
Digest digest = this.messDigestOTS;
|
|
byte[] bArr2 = this.concHashs;
|
|
digest.update(bArr2, 0, bArr2.length);
|
|
byte[] bArr3 = new byte[this.messDigestOTS.getDigestSize()];
|
|
this.leaf = bArr3;
|
|
this.messDigestOTS.doFinal(bArr3, 0);
|
|
return;
|
|
}
|
|
if (i2 == 0 || this.j == this.two_power_w - 1) {
|
|
this.i = i2 + 1;
|
|
this.j = 0;
|
|
this.privateKeyOTS = this.gmssRandom.nextSeed(this.seed);
|
|
} else {
|
|
Digest digest2 = this.messDigestOTS;
|
|
byte[] bArr4 = this.privateKeyOTS;
|
|
digest2.update(bArr4, 0, bArr4.length);
|
|
this.privateKeyOTS = bArr;
|
|
this.messDigestOTS.doFinal(bArr, 0);
|
|
int i3 = this.j + 1;
|
|
this.j = i3;
|
|
if (i3 == this.two_power_w - 1) {
|
|
byte[] bArr5 = this.privateKeyOTS;
|
|
byte[] bArr6 = this.concHashs;
|
|
int i4 = this.mdsize;
|
|
System.arraycopy(bArr5, 0, bArr6, (this.i - 1) * i4, i4);
|
|
}
|
|
}
|
|
}
|
|
StringBuilder sb = new StringBuilder("unable to updateLeaf in steps: ");
|
|
sb.append(this.steps);
|
|
sb.append(" ");
|
|
sb.append(this.i);
|
|
sb.append(" ");
|
|
sb.append(this.j);
|
|
throw new IllegalStateException(sb.toString());
|
|
}
|
|
|
|
private GMSSLeaf(GMSSLeaf gMSSLeaf) {
|
|
this.messDigestOTS = gMSSLeaf.messDigestOTS;
|
|
this.mdsize = gMSSLeaf.mdsize;
|
|
this.keysize = gMSSLeaf.keysize;
|
|
this.gmssRandom = gMSSLeaf.gmssRandom;
|
|
this.leaf = Arrays.clone(gMSSLeaf.leaf);
|
|
this.concHashs = Arrays.clone(gMSSLeaf.concHashs);
|
|
this.i = gMSSLeaf.i;
|
|
this.j = gMSSLeaf.j;
|
|
this.two_power_w = gMSSLeaf.two_power_w;
|
|
this.w = gMSSLeaf.w;
|
|
this.steps = gMSSLeaf.steps;
|
|
this.seed = Arrays.clone(gMSSLeaf.seed);
|
|
this.privateKeyOTS = Arrays.clone(gMSSLeaf.privateKeyOTS);
|
|
}
|
|
|
|
public GMSSLeaf(Digest digest, byte[][] bArr, int[] iArr) {
|
|
this.i = iArr[0];
|
|
this.j = iArr[1];
|
|
this.steps = iArr[2];
|
|
this.w = iArr[3];
|
|
this.messDigestOTS = digest;
|
|
this.gmssRandom = new GMSSRandom(digest);
|
|
this.mdsize = this.messDigestOTS.getDigestSize();
|
|
this.keysize = ((int) Math.ceil((r9 << 3) / this.w)) + ((int) Math.ceil(getLog((r9 << this.w) + 1) / this.w));
|
|
this.two_power_w = 1 << this.w;
|
|
this.privateKeyOTS = bArr[0];
|
|
this.seed = bArr[1];
|
|
this.concHashs = bArr[2];
|
|
this.leaf = bArr[3];
|
|
}
|
|
|
|
public GMSSLeaf(Digest digest, int i, int i2, byte[] bArr) {
|
|
this.w = i;
|
|
this.messDigestOTS = digest;
|
|
this.gmssRandom = new GMSSRandom(digest);
|
|
this.mdsize = this.messDigestOTS.getDigestSize();
|
|
double d = i;
|
|
this.keysize = ((int) Math.ceil((r7 << 3) / d)) + ((int) Math.ceil(getLog((r7 << i) + 1) / d));
|
|
this.two_power_w = 1 << i;
|
|
this.steps = (int) Math.ceil(((((r8 - 1) * r7) + 1) + r7) / i2);
|
|
int i3 = this.mdsize;
|
|
this.seed = new byte[i3];
|
|
this.leaf = new byte[i3];
|
|
this.privateKeyOTS = new byte[i3];
|
|
this.concHashs = new byte[i3 * this.keysize];
|
|
initLeafCalc(bArr);
|
|
}
|
|
|
|
/* JADX INFO: Access modifiers changed from: package-private */
|
|
public GMSSLeaf(Digest digest, int i, int i2) {
|
|
this.w = i;
|
|
this.messDigestOTS = digest;
|
|
this.gmssRandom = new GMSSRandom(digest);
|
|
this.mdsize = this.messDigestOTS.getDigestSize();
|
|
double d = i;
|
|
this.keysize = ((int) Math.ceil((r7 << 3) / d)) + ((int) Math.ceil(getLog((r7 << i) + 1) / d));
|
|
this.two_power_w = 1 << i;
|
|
this.steps = (int) Math.ceil(((((r8 - 1) * r7) + 1) + r7) / i2);
|
|
int i3 = this.mdsize;
|
|
this.seed = new byte[i3];
|
|
this.leaf = new byte[i3];
|
|
this.privateKeyOTS = new byte[i3];
|
|
this.concHashs = new byte[i3 * this.keysize];
|
|
}
|
|
}
|