130 lines
4.6 KiB
Java
130 lines
4.6 KiB
Java
package com.google.android.gms.internal.p002firebaseauthapi;
|
|
|
|
import com.google.common.primitives.UnsignedBytes;
|
|
import java.security.GeneralSecurityException;
|
|
import java.util.Arrays;
|
|
import javax.crypto.AEADBadTagException;
|
|
import javax.crypto.BadPaddingException;
|
|
import javax.crypto.Cipher;
|
|
import javax.crypto.IllegalBlockSizeException;
|
|
import javax.crypto.spec.IvParameterSpec;
|
|
import javax.crypto.spec.SecretKeySpec;
|
|
|
|
/* loaded from: classes2.dex */
|
|
public final class zzpm implements zzap {
|
|
private static final ThreadLocal zza = new zzpk();
|
|
private static final ThreadLocal zzb = new zzpl();
|
|
private final byte[] zzc;
|
|
private final byte[] zzd;
|
|
private final SecretKeySpec zze;
|
|
private final int zzf;
|
|
|
|
public zzpm(byte[] bArr, int i) throws GeneralSecurityException {
|
|
if (!zzdv.zza(1)) {
|
|
throw new GeneralSecurityException("Can not use AES-EAX in FIPS-mode.");
|
|
}
|
|
if (i == 12 || i == 16) {
|
|
this.zzf = i;
|
|
zzqs.zzb(bArr.length);
|
|
SecretKeySpec secretKeySpec = new SecretKeySpec(bArr, "AES");
|
|
this.zze = secretKeySpec;
|
|
Cipher cipher = (Cipher) zza.get();
|
|
cipher.init(1, secretKeySpec);
|
|
byte[] zzc = zzc(cipher.doFinal(new byte[16]));
|
|
this.zzc = zzc;
|
|
this.zzd = zzc(zzc);
|
|
return;
|
|
}
|
|
throw new IllegalArgumentException("IV size should be either 12 or 16 bytes");
|
|
}
|
|
|
|
private static byte[] zzc(byte[] bArr) {
|
|
byte[] bArr2 = new byte[16];
|
|
int i = 0;
|
|
while (i < 15) {
|
|
byte b = bArr[i];
|
|
int i2 = i + 1;
|
|
bArr2[i] = (byte) (((b + b) ^ ((bArr[i2] & UnsignedBytes.MAX_VALUE) >>> 7)) & 255);
|
|
i = i2;
|
|
}
|
|
byte b2 = bArr[15];
|
|
bArr2[15] = (byte) (((bArr[0] >> 7) & 135) ^ (b2 + b2));
|
|
return bArr2;
|
|
}
|
|
|
|
private final byte[] zzd(Cipher cipher, int i, byte[] bArr, int i2, int i3) throws IllegalBlockSizeException, BadPaddingException {
|
|
int length;
|
|
byte[] bArr2;
|
|
byte[] bArr3 = new byte[16];
|
|
bArr3[15] = (byte) i;
|
|
if (i3 == 0) {
|
|
return cipher.doFinal(zze(bArr3, this.zzc));
|
|
}
|
|
byte[] doFinal = cipher.doFinal(bArr3);
|
|
int i4 = 0;
|
|
int i5 = 0;
|
|
while (i3 - i5 > 16) {
|
|
for (int i6 = 0; i6 < 16; i6++) {
|
|
doFinal[i6] = (byte) (doFinal[i6] ^ bArr[(i2 + i5) + i6]);
|
|
}
|
|
doFinal = cipher.doFinal(doFinal);
|
|
i5 += 16;
|
|
}
|
|
byte[] copyOfRange = Arrays.copyOfRange(bArr, i5 + i2, i2 + i3);
|
|
if (copyOfRange.length == 16) {
|
|
bArr2 = zze(copyOfRange, this.zzc);
|
|
} else {
|
|
byte[] copyOf = Arrays.copyOf(this.zzd, 16);
|
|
while (true) {
|
|
length = copyOfRange.length;
|
|
if (i4 >= length) {
|
|
break;
|
|
}
|
|
copyOf[i4] = (byte) (copyOf[i4] ^ copyOfRange[i4]);
|
|
i4++;
|
|
}
|
|
copyOf[length] = (byte) (copyOf[length] ^ 128);
|
|
bArr2 = copyOf;
|
|
}
|
|
return cipher.doFinal(zze(doFinal, bArr2));
|
|
}
|
|
|
|
private static byte[] zze(byte[] bArr, byte[] bArr2) {
|
|
int length = bArr.length;
|
|
byte[] bArr3 = new byte[length];
|
|
for (int i = 0; i < length; i++) {
|
|
bArr3[i] = (byte) (bArr[i] ^ bArr2[i]);
|
|
}
|
|
return bArr3;
|
|
}
|
|
|
|
@Override // com.google.android.gms.internal.p002firebaseauthapi.zzap
|
|
public final byte[] zza(byte[] bArr, byte[] bArr2) throws GeneralSecurityException {
|
|
int length = bArr.length;
|
|
int i = (length - this.zzf) - 16;
|
|
if (i < 0) {
|
|
throw new GeneralSecurityException("ciphertext too short");
|
|
}
|
|
Cipher cipher = (Cipher) zza.get();
|
|
cipher.init(1, this.zze);
|
|
byte[] zzd = zzd(cipher, 0, bArr, 0, this.zzf);
|
|
byte[] zzd2 = zzd(cipher, 1, bArr2, 0, 0);
|
|
byte[] zzd3 = zzd(cipher, 2, bArr, this.zzf, i);
|
|
byte b = 0;
|
|
for (int i2 = 0; i2 < 16; i2++) {
|
|
b = (byte) (b | (((bArr[(length - 16) + i2] ^ zzd2[i2]) ^ zzd[i2]) ^ zzd3[i2]));
|
|
}
|
|
if (b != 0) {
|
|
throw new AEADBadTagException("tag mismatch");
|
|
}
|
|
Cipher cipher2 = (Cipher) zzb.get();
|
|
cipher2.init(1, this.zze, new IvParameterSpec(zzd));
|
|
return cipher2.doFinal(bArr, this.zzf, i);
|
|
}
|
|
|
|
@Override // com.google.android.gms.internal.p002firebaseauthapi.zzap
|
|
public final byte[] zzb(byte[] bArr, byte[] bArr2) throws GeneralSecurityException {
|
|
throw null;
|
|
}
|
|
}
|