2020import static org .junit .Assert .assertThrows ;
2121
2222import com .google .crypto .tink .hybrid .HpkeParameters ;
23+ import com .google .crypto .tink .hybrid .HpkePrivateKey ;
2324import com .google .crypto .tink .hybrid .HpkePublicKey ;
25+ import com .google .crypto .tink .hybrid .internal .testing .HpkeTestUtil ;
26+ import com .google .crypto .tink .hybrid .internal .testing .HybridTestVector ;
2427import com .google .crypto .tink .subtle .EllipticCurves ;
2528import com .google .crypto .tink .subtle .EllipticCurves .CurveType ;
2629import com .google .crypto .tink .subtle .EllipticCurves .PointFormatType ;
2932import java .math .BigInteger ;
3033import java .security .GeneralSecurityException ;
3134import java .security .spec .ECPoint ;
35+ import java .util .Arrays ;
3236import org .junit .Test ;
3337import org .junit .runner .RunWith ;
3438import org .junit .runners .JUnit4 ;
@@ -39,6 +43,32 @@ public final class HpkeHelperForAndroidKeystoreTest {
3943 @ Test
4044 public void decryptUnauthenticatedWithEncapsulatedKeyAndP256SharedSecret_success ()
4145 throws Exception {
46+ HybridTestVector vector = HpkeTestUtil .createTestVector0 ();
47+ HpkePrivateKey privateKey = (HpkePrivateKey ) vector .getPrivateKey ();
48+ // The shared secret is needed for decryption with
49+ // decryptUnauthenticatedWithEncapsulatedKeyAndP256SharedSecret -- we logged it to enable this
50+ // code.
51+ byte [] dhSharedSecret =
52+ Hex .decode ("c47e13b026cac2b065b83c5985cc03f683382ed027448b3432fa51d34e54f7e6" );
53+
54+ // Variant NO_PREFIX, DHKEM_P256_HKDF_SHA256 -> the first 65 bytes are the encapsulated key.
55+ byte [] encapsulatedKey = Arrays .copyOf (vector .getCiphertext (), 65 );
56+ HpkeHelperForAndroidKeystore helper =
57+ HpkeHelperForAndroidKeystore .create (privateKey .getPublicKey ());
58+ assertThat (
59+ helper .decryptUnauthenticatedWithEncapsulatedKeyAndP256SharedSecret (
60+ encapsulatedKey ,
61+ dhSharedSecret ,
62+ vector .getCiphertext (),
63+ 65 ,
64+ vector .getContextInfo ()))
65+ .isEqualTo (vector .getPlaintext ());
66+ }
67+
68+ /** A second test with a test vector. Here only due to history (and it doesn't seem to hurt). */
69+ @ Test
70+ public void decryptUnauthenticatedWithEncapsulatedKeyAndP256SharedSecret_testVector2_success ()
71+ throws Exception {
4272 HpkeParameters params =
4373 HpkeParameters .builder ()
4474 .setVariant (HpkeParameters .Variant .NO_PREFIX )
0 commit comments