| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.security.keystore2; |
| 18 |
|
| 19 |
import static org.junit.Assert.assertEquals; |
| 20 |
import static org.junit.Assert.assertThrows; |
| 21 |
|
| 22 |
import android.security.KeyStoreSecurityLevel; |
| 23 |
import android.system.keystore2.Authorization; |
| 24 |
import android.system.keystore2.Domain; |
| 25 |
import android.system.keystore2.KeyDescriptor; |
| 26 |
import android.system.keystore2.KeyMetadata; |
| 27 |
|
| 28 |
import androidx.test.runner.AndroidJUnit4; |
| 29 |
|
| 30 |
import org.junit.Test; |
| 31 |
import org.junit.runner.RunWith; |
| 32 |
import org.mockito.Mock; |
| 33 |
|
| 34 |
import java.math.BigInteger; |
| 35 |
import java.util.Base64; |
| 36 |
|
| 37 |
@RunWith(AndroidJUnit4.class) |
| 38 |
public class AndroidKeyStoreEdECPublicKeyTest { |
| 39 |
private static KeyDescriptor descriptor() { |
| 40 |
final KeyDescriptor keyDescriptor = new KeyDescriptor(); |
| 41 |
keyDescriptor.alias = "key"; |
| 42 |
keyDescriptor.blob = null; |
| 43 |
keyDescriptor.domain = Domain.APP; |
| 44 |
keyDescriptor.nspace = -1; |
| 45 |
return keyDescriptor; |
| 46 |
} |
| 47 |
|
| 48 |
private static KeyMetadata metadata(byte[] cert, byte[] certChain) { |
| 49 |
KeyMetadata metadata = new KeyMetadata(); |
| 50 |
metadata.authorizations = new Authorization[0]; |
| 51 |
metadata.certificate = cert; |
| 52 |
metadata.certificateChain = certChain; |
| 53 |
metadata.key = descriptor(); |
| 54 |
metadata.modificationTimeMs = 0; |
| 55 |
metadata.keySecurityLevel = 1; |
| 56 |
return metadata; |
| 57 |
} |
| 58 |
|
| 59 |
@Mock |
| 60 |
private KeyStoreSecurityLevel mKeystoreSecurityLevel; |
| 61 |
|
| 62 |
private static class EdECTestVector { |
| 63 |
public final byte[] encodedKeyBytes; |
| 64 |
public final boolean isOdd; |
| 65 |
public final BigInteger yValue; |
| 66 |
|
| 67 |
EdECTestVector(String b64KeyBytes, boolean isOdd, String yValue) { |
| 68 |
this.encodedKeyBytes = Base64.getDecoder().decode(b64KeyBytes); |
| 69 |
this.isOdd = isOdd; |
| 70 |
this.yValue = new BigInteger(yValue); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
private static final EdECTestVector[] ED_EC_TEST_VECTORS = new EdECTestVector[]{ |
| 75 |
new EdECTestVector("MCowBQYDK2VwAyEADE+wvQqNHxaERPhAZ0rCFlgFbfWLs/YonPXdSTw0VSo=", |
| 76 |
false, |
| 77 |
"19147682157189290216699341180089409126316261024914226007941553249095116672780" |
| 78 |
), |
| 79 |
new EdECTestVector("MCowBQYDK2VwAyEA/0E1IRNzGj85Ot/TPeXqifkqTkdk4voleH0hIq59D9w=", |
| 80 |
true, |
| 81 |
"41640152188550647350742178040529506688513911269563908889464821205156322689535" |
| 82 |
), |
| 83 |
new EdECTestVector("MCowBQYDK2VwAyEAunOvGuenetl9GQSXGVo5L3RIr4OOIpFIv/Zre8qTc/8=", |
| 84 |
true, |
| 85 |
"57647939198144376128225770417635248407428273266444593100194116168980378907578" |
| 86 |
), |
| 87 |
new EdECTestVector("MCowBQYDK2VwAyEA2hHqaZ5IolswN1Yd58Y4hzhmUMCCqc4PW5A/SFLmTX8=", |
| 88 |
false, |
| 89 |
"57581368614046789120409806291852629847774713088410311752049592044694364885466" |
| 90 |
), |
| 91 |
}; |
| 92 |
|
| 93 |
@Test |
| 94 |
public void testParsingOfValidKeys() { |
| 95 |
for (EdECTestVector testVector : ED_EC_TEST_VECTORS) { |
| 96 |
AndroidKeyStoreEdECPublicKey pkey = new AndroidKeyStoreEdECPublicKey(descriptor(), |
| 97 |
metadata(null, null), "EdDSA", mKeystoreSecurityLevel, |
| 98 |
testVector.encodedKeyBytes); |
| 99 |
|
| 100 |
assertEquals(pkey.getPoint().isXOdd(), testVector.isOdd); |
| 101 |
assertEquals(pkey.getPoint().getY(), testVector.yValue); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
@Test |
| 106 |
public void testFailedParsingOfKeysWithDifferentOid() { |
| 107 |
final byte[] testVectorWithIncorrectOid = Base64.getDecoder().decode( |
| 108 |
"MCowBQYDLGVwAyEADE+wvQqNHxaERPhAZ0rCFlgFbfWLs/YonPXdSTw0VSo="); |
| 109 |
assertThrows("OID should be unrecognized", IllegalArgumentException.class, |
| 110 |
() -> new AndroidKeyStoreEdECPublicKey(descriptor(), metadata(null, null), "EdDSA", |
| 111 |
mKeystoreSecurityLevel, testVectorWithIncorrectOid)); |
| 112 |
} |
| 113 |
|
| 114 |
@Test |
| 115 |
public void testFailedParsingOfKeysWithWrongSize() { |
| 116 |
final byte[] testVectorWithIncorrectKeySize = Base64.getDecoder().decode( |
| 117 |
"MCwwBQYDK2VwAyMADE+wvQqNHxaERPhAZ0rCFlgFbfWLs/YonPXdSTw0VSrOzg=="); |
| 118 |
assertThrows("Key length should be invalid", IllegalArgumentException.class, |
| 119 |
() -> new AndroidKeyStoreEdECPublicKey(descriptor(), metadata(null, null), "EdDSA", |
| 120 |
mKeystoreSecurityLevel, testVectorWithIncorrectKeySize)); |
| 121 |
} |
| 122 |
} |
| 123 |
|