| | |
| | | @Test |
| | | public void testAnotherPrivateKeyCannotUnwrap() throws Exception |
| | | { |
| | | KeyPair other; |
| | | do |
| | | { |
| | | other = rsaKeyPair(2048); |
| | | } |
| | | while (modulus(other).compareTo(modulus(keyPair)) <= 0); |
| | | final KeyPair otherKeyPair = other; |
| | | final KeyPair secondKeyPair = rsaKeyPair(2048); |
| | | final int modulusOrder = modulus(keyPair).compareTo(modulus(secondKeyPair)); |
| | | assertThat(modulusOrder).as("the two RSA moduli must differ").isNotZero(); |
| | | final KeyPair wrappingKeyPair = modulusOrder < 0 ? keyPair : secondKeyPair; |
| | | final KeyPair wrongKeyPair = modulusOrder < 0 ? secondKeyPair : keyPair; |
| | | final SecretKey key = new SecretKeySpec(bytes(16, 0x3C), "AES"); |
| | | inProviders(BC_FIPS, () -> |
| | | { |
| | | final byte[] wrapped = RsaSveKeyTransport.wrap(keyPair.getPublic(), key, KEY_ID, random); |
| | | assertThatThrownBy(() -> RsaSveKeyTransport.unwrap(otherKeyPair, wrapped, "AES", KEY_ID)) |
| | | .isInstanceOf(GeneralSecurityException.class); |
| | | final byte[] wrapped = RsaSveKeyTransport.wrap(wrappingKeyPair.getPublic(), key, KEY_ID, random); |
| | | assertThatThrownBy(() -> RsaSveKeyTransport.unwrap(wrongKeyPair, wrapped, "AES", KEY_ID)) |
| | | .isInstanceOf(InvalidKeyException.class) |
| | | .hasMessage(RsaSveKeyTransport.NAME + " cannot unwrap the key"); |
| | | return null; |
| | | }); |
| | | } |