2727import static org .conscrypt .HpkeFixture .DEFAULT_SUITE_NAME ;
2828import static org .conscrypt .HpkeFixture .createDefaultHpkeContextRecipient ;
2929import static org .conscrypt .HpkeFixture .createDefaultHpkeContextSender ;
30- import static org .conscrypt .HpkeTestVectorsTest .getHpkeEncryptionRecords ;
3130import static org .conscrypt .TestUtils .encodeHex ;
3231import static org .junit .Assert .assertArrayEquals ;
3332import static org .junit .Assert .assertEquals ;
3635import static org .junit .Assert .assertThrows ;
3736import static org .junit .Assert .assertTrue ;
3837
38+ import org .conscrypt .java .security .DefaultKeys ;
39+ import org .junit .After ;
40+ import org .junit .Before ;
41+ import org .junit .Test ;
42+ import org .junit .runner .RunWith ;
43+ import org .junit .runners .JUnit4 ;
44+
3945import java .nio .charset .StandardCharsets ;
4046import java .security .GeneralSecurityException ;
4147import java .security .InvalidKeyException ;
4450import java .security .Provider ;
4551import java .security .PublicKey ;
4652import java .security .Security ;
47- import java .util .List ;
48-
49- import org .conscrypt .HpkeTestVectorsTest .HpkeData ;
50- import org .conscrypt .HpkeTestVectorsTest .HpkeEncryptionData ;
51- import org .conscrypt .java .security .DefaultKeys ;
52- import org .junit .After ;
53- import org .junit .Before ;
54- import org .junit .Test ;
55- import org .junit .runner .RunWith ;
56- import org .junit .runners .JUnit4 ;
5753
5854/**
5955 * Tests for DuckTypedHpkeSpiTest. Essentially the same as the tests for HpkeContext but
@@ -134,14 +130,6 @@ public void export() throws Exception {
134130 assertNotEquals (encodeHex (export1 ), encodeHex (export2 ));
135131 }
136132
137- @ Test
138- public void vectors () throws Exception {
139- final List <HpkeData > records = getHpkeEncryptionRecords ();
140- for (HpkeData record : records ) {
141- testHpkeEncryption (record );
142- }
143- }
144-
145133 @ Test
146134 public void initInvalidKeys () throws Exception {
147135 HpkeContextSender sender = HpkeContextSender .getInstance (DEFAULT_SUITE_NAME );
@@ -167,67 +155,37 @@ public void initInvalidKeys() throws Exception {
167155 () -> recipient .init (DEFAULT_ENC , invalidKey , DEFAULT_INFO ));
168156 }
169157
170- @ Test
171- public void testSeal_missingRequiredParameters_throwNullException () throws Exception {
172- HpkeContextSender ctxSender = HpkeContextSender .getInstance (DEFAULT_SUITE_NAME );
173- ctxSender .init (DEFAULT_PK , DEFAULT_INFO );
174- assertThrows (NullPointerException .class ,
175- () -> ctxSender .seal (/* plaintext= */ null , DEFAULT_AAD ));
176- }
177-
178- @ Test
179- public void testExport_lowerEdgeLength () throws Exception {
180- final HpkeContextSender ctxSender = createDefaultHpkeContextSender ();
181- final byte [] enc = ctxSender .getEncapsulated ();
182- final byte [] export = ctxSender .export (/* length= */ 0 , DEFAULT_EXPORTER_CONTEXT );
183- assertNotNull (enc );
184- assertNotNull (export );
185- assertThrows (IllegalArgumentException .class ,
186- () -> ctxSender .export (/* length= */ -1 , DEFAULT_EXPORTER_CONTEXT ));
187- }
188-
189-
190- @ Test
191- public void testInitUnsupportedModes () throws Exception {
192- HpkeContextSender sender = HpkeContextSender .getInstance (DEFAULT_SUITE_NAME );
193- byte [] psk = "Shhh! Secret!" .getBytes (StandardCharsets .UTF_8 );
194- byte [] pskId = "id" .getBytes (StandardCharsets .UTF_8 );
195-
196- assertThrows (UnsupportedOperationException .class , () ->
197- sender .init (DEFAULT_PK , DEFAULT_INFO , DEFAULT_SK ));
198- assertThrows (UnsupportedOperationException .class , () ->
199- sender .init (DEFAULT_PK , DEFAULT_INFO , psk , pskId ));
200- assertThrows (UnsupportedOperationException .class , () ->
201- sender .init (DEFAULT_PK , DEFAULT_INFO , DEFAULT_SK , psk , pskId ));
202- }
203-
204- // Copied from HpkeTestVectorsTest but with extra checks to ensure we are operating on
205- // duck typed instances.
206- private void testHpkeEncryption (HpkeData record ) throws Exception {
207- final byte [] enc = record .pkEm ;
208-
209- // Encryption
210- final HpkeContextSender contextSender =
211- setupBaseForTesting (record .hpkeSuite , record .pkRm , record .info , record .skEm );
212- assertForeign (contextSender );
213- final byte [] encResult = contextSender .getEncapsulated ();
214- assertArrayEquals ("Failed encryption 'enc' " + encodeHex (enc ), enc , encResult );
215- for (HpkeEncryptionData encryption : record .encryptions ) {
216- final byte [] ciphertext = contextSender .seal (encryption .pt , encryption .aad );
217- assertArrayEquals ("Failed encryption 'ciphertext' on data : " + encryption ,
218- encryption .ct , ciphertext );
219- }
158+ @ Test
159+ public void testSeal_missingRequiredParameters_throwNullException () throws Exception {
160+ HpkeContextSender ctxSender = HpkeContextSender .getInstance (DEFAULT_SUITE_NAME );
161+ ctxSender .init (DEFAULT_PK , DEFAULT_INFO );
162+ assertThrows (NullPointerException .class ,
163+ () -> ctxSender .seal (/* plaintext= */ null , DEFAULT_AAD ));
164+ }
220165
221- // Decryption
222- final HpkeContextRecipient contextRecipient =
223- HpkeContextRecipient .getInstance (record .hpkeSuite .name ());
224- assertForeign (contextRecipient );
225- contextRecipient .init (enc , record .skRm , record .info );
226- for (HpkeEncryptionData encryption : record .encryptions ) {
227- final byte [] plaintext = contextRecipient .open (encryption .ct , encryption .aad );
228- assertArrayEquals (
229- "Failed decryption on data : " + encryption , encryption .pt , plaintext );
230- }
166+ @ Test
167+ public void testExport_lowerEdgeLength () throws Exception {
168+ final HpkeContextSender ctxSender = createDefaultHpkeContextSender ();
169+ final byte [] enc = ctxSender .getEncapsulated ();
170+ final byte [] export = ctxSender .export (/* length= */ 0 , DEFAULT_EXPORTER_CONTEXT );
171+ assertNotNull (enc );
172+ assertNotNull (export );
173+ assertThrows (IllegalArgumentException .class ,
174+ () -> ctxSender .export (/* length= */ -1 , DEFAULT_EXPORTER_CONTEXT ));
175+ }
176+
177+ @ Test
178+ public void testInitUnsupportedModes () throws Exception {
179+ HpkeContextSender sender = HpkeContextSender .getInstance (DEFAULT_SUITE_NAME );
180+ byte [] psk = "Shhh! Secret!" .getBytes (StandardCharsets .UTF_8 );
181+ byte [] pskId = "id" .getBytes (StandardCharsets .UTF_8 );
182+
183+ assertThrows (UnsupportedOperationException .class ,
184+ () -> sender .init (DEFAULT_PK , DEFAULT_INFO , DEFAULT_SK ));
185+ assertThrows (UnsupportedOperationException .class ,
186+ () -> sender .init (DEFAULT_PK , DEFAULT_INFO , psk , pskId ));
187+ assertThrows (UnsupportedOperationException .class ,
188+ () -> sender .init (DEFAULT_PK , DEFAULT_INFO , DEFAULT_SK , psk , pskId ));
231189 }
232190
233191 private HpkeContextSender setupBaseForTesting (
0 commit comments