PrfSetWrapper implements PrimitiveWrapper<PrfSet, PrfSet> { @Override public PrfSet wrap(PrimitiveSet<PrfSet> set) throws GeneralSecurityException { return new WrappedPrfSet(set); }  @Override PrfSet wrap(PrimitiveSet<PrfSet> set); @Override Class<PrfSet> getPrimitiveClass(); @Override Class<PrfSet> getInputPrimitiveClass(); static void register();  }
@Test public void testSmallPlaintextWithRawKey() throws Exception { byte[] keyValue = Random.randBytes(KEY_SIZE); Keyset.Key primary = TestUtil.createKey( TestUtil.createPrfKeyData(keyValue), 5, KeyStatusType.ENABLED, OutputPrefixType.RAW); KeysetHandle keysetHandle = TestUtil.createKeysetHandle(TestUtil.createKeyset(primary)); byte[] plaintext = "blah".getBytes(UTF_8); PrfSet prfSet = new PrfSetWrapper().wrap(Registry.getPrimitives(keysetHandle, PrfSet.class)); byte[] prs = prfSet.computePrimary(plaintext, 12); byte[] prs2 = prfSet.getPrfs().get(5).compute(plaintext, 12); assertEquals(5, prfSet.getPrimaryId()); assertThat(prfSet.getPrfs()).hasSize(1); assertThat(prs).hasLength(12); assertArrayEquals(prs2, prs); }
@Test public void testSmallPlaintextWithMultipleKeys() throws Exception { byte[] primaryKeyValue = Random.randBytes(KEY_SIZE); Keyset.Key primary = TestUtil.createKey( TestUtil.createPrfKeyData(primaryKeyValue), 5, KeyStatusType.ENABLED, OutputPrefixType.RAW); byte[] secondaryKeyValue = Random.randBytes(KEY_SIZE); Keyset.Key secondary = TestUtil.createKey( TestUtil.createPrfKeyData(secondaryKeyValue), 6, KeyStatusType.ENABLED, OutputPrefixType.RAW); KeysetHandle keysetHandle = TestUtil.createKeysetHandle(TestUtil.createKeyset(primary, secondary)); byte[] plaintext = "blah".getBytes(UTF_8); PrfSet prfSet = new PrfSetWrapper().wrap(Registry.getPrimitives(keysetHandle, PrfSet.class)); byte[] prsPrimary = prfSet.computePrimary(plaintext, 12); byte[] prs5 = prfSet.getPrfs().get(5).compute(plaintext, 12); byte[] prs6 = prfSet.getPrfs().get(6).compute(plaintext, 12); assertEquals(5, prfSet.getPrimaryId()); assertThat(prfSet.getPrfs()).hasSize(2); assertThat(prsPrimary).hasLength(12); assertArrayEquals(prs5, prsPrimary); assertThat(prsPrimary).isNotEqualTo(prs6); }
@Test public void testWrapEmptyThrows() throws Exception { final PrimitiveSet<PrfSet> primitiveSet = PrimitiveSet.newPrimitiveSet(PrfSet.class); assertThrows( GeneralSecurityException.class, new ThrowingRunnable() { @Override public void run() throws Throwable { new PrfSetWrapper().wrap(primitiveSet); } }); }
@Test public void testWrapNoPrimaryThrows() throws Exception { byte[] primaryKeyValue = Random.randBytes(KEY_SIZE); Keyset.Key primary = TestUtil.createKey( TestUtil.createPrfKeyData(primaryKeyValue), 5, KeyStatusType.ENABLED, OutputPrefixType.RAW); final Prf unusedPrf = new Prf() { @Override public byte[] compute(byte[] input, int outputLength) throws GeneralSecurityException { return new byte[0]; } }; final PrimitiveSet<PrfSet> primitiveSet = PrimitiveSet.newPrimitiveSet(PrfSet.class); primitiveSet.addPrimitive( new PrfSet() { @Override public int getPrimaryId() { return 0; } @Override public Map<Integer, Prf> getPrfs() throws GeneralSecurityException { HashMap<Integer, Prf> prfMap = new HashMap<>(); prfMap.put(0, unusedPrf); return prfMap; } }, primary); assertThrows( GeneralSecurityException.class, new ThrowingRunnable() { @Override public void run() throws Throwable { new PrfSetWrapper().wrap(primitiveSet); } }); }
@Test public void testMultiplePrfsIntoWrapperThrows() throws Exception { byte[] primaryKeyValue = Random.randBytes(KEY_SIZE); Keyset.Key primary = TestUtil.createKey( TestUtil.createPrfKeyData(primaryKeyValue), 5, KeyStatusType.ENABLED, OutputPrefixType.RAW); final Prf unusedPrf = new Prf() { @Override public byte[] compute(byte[] input, int outputLength) throws GeneralSecurityException { return new byte[0]; } }; final PrimitiveSet<PrfSet> primitiveSet = PrimitiveSet.newPrimitiveSet(PrfSet.class); primitiveSet.setPrimary( primitiveSet.addPrimitive( new PrfSet() { @Override public int getPrimaryId() { return 0; } @Override public Map<Integer, Prf> getPrfs() throws GeneralSecurityException { HashMap<Integer, Prf> prfMap = new HashMap<>(); prfMap.put(0, unusedPrf); prfMap.put(1, unusedPrf); return prfMap; } }, primary)); assertThrows( GeneralSecurityException.class, new ThrowingRunnable() { @Override public void run() throws Throwable { new PrfSetWrapper().wrap(primitiveSet); } }); }