identity: Fix incorrect encoding P256 public key
Bug: 240359297
Change-Id: I8799f41765fe1acae2e11739806d4aedeb93976c
diff --git a/identity/util/src/java/com/android/security/identity/internal/Util.java b/identity/util/src/java/com/android/security/identity/internal/Util.java
index de7369a..ee12cd0 100644
--- a/identity/util/src/java/com/android/security/identity/internal/Util.java
+++ b/identity/util/src/java/com/android/security/identity/internal/Util.java
@@ -1150,22 +1150,24 @@
if (xBytes.length > 32) {
throw new RuntimeException("xBytes is " + xBytes.length + " which is unexpected");
}
- for (int n = 0; n < 32 - xBytes.length; n++) {
+ int numLeadingZeroBytes = 32 - xBytes.length;
+ for (int n = 0; n < numLeadingZeroBytes; n++) {
ret[n] = 0x00;
}
- for (int n = 32 - xBytes.length; n < xBytes.length; n++) {
- ret[n] = xBytes[n];
+ for (int n = 0; n < xBytes.length; n++) {
+ ret[numLeadingZeroBytes + n] = xBytes[n];
}
byte[] yBytes = stripLeadingZeroes(w.getAffineY().toByteArray());
if (yBytes.length > 32) {
throw new RuntimeException("yBytes is " + yBytes.length + " which is unexpected");
}
- for (int n = 0; n < 32 - yBytes.length; n++) {
+ numLeadingZeroBytes = 32 - yBytes.length;
+ for (int n = 0; n < numLeadingZeroBytes; n++) {
ret[32 + n] = 0x00;
}
- for (int n = 32 - yBytes.length; n < yBytes.length; n++) {
- ret[32 + n] = yBytes[n];
+ for (int n = 0; n < yBytes.length; n++) {
+ ret[32 + numLeadingZeroBytes + n] = yBytes[n];
}
return ret;