Kwp implements KeyWrap { @Override public byte[] wrap(final byte[] data) throws GeneralSecurityException { if (data.length < MIN_WRAP_KEY_SIZE) { throw new GeneralSecurityException("Key size of key to wrap too small"); } if (data.length > MAX_WRAP_KEY_SIZE) { throw new GeneralSecurityException("Key size of key to wrap too large"); } byte[] iv = new byte[8]; System.arraycopy(PREFIX, 0, iv, 0, PREFIX.length); for (int i = 0; i < 4; i++) { iv[4 + i] = (byte) ((data.length >> (8 * (3 - i))) & 0xff); } return computeW(iv, data); } Kwp(final byte[] key); @Override byte[] wrap(final byte[] data); @Override byte[] unwrap(final byte[] data);  }
@Test public void testInvalidWrappingSizes() throws Exception { byte[] wrapKey = Random.randBytes(16); KeyWrap wrapper = new Kwp(wrapKey); for (int wrappedSize = 0; wrappedSize < 16; wrappedSize++) { try { wrapper.wrap(new byte[wrappedSize]); fail("Should not wrap short keys"); } catch (GeneralSecurityException ex) { } } }