/** @return all primitives using RAW prefix. */ public List<Entry<P>> getRawPrimitives() throws GeneralSecurityException { return getPrimitive(CryptoFormat.RAW_PREFIX); }
/** @return the entries with primitives identified by the ciphertext prefix of {@code key}. */ protected List<Entry<P>> getPrimitive(Keyset.Key key) throws GeneralSecurityException { return getPrimitive(CryptoFormat.getOutputPrefix(key)); }
@Override public byte[] decrypt(final byte[] ciphertext, final byte[] contextInfo) throws GeneralSecurityException { if (ciphertext.length > CryptoFormat.NON_RAW_PREFIX_SIZE) { byte[] prefix = Arrays.copyOfRange(ciphertext, 0, CryptoFormat.NON_RAW_PREFIX_SIZE); byte[] ciphertextNoPrefix = Arrays.copyOfRange(ciphertext, CryptoFormat.NON_RAW_PREFIX_SIZE, ciphertext.length); List<PrimitiveSet.Entry<HybridDecrypt>> entries = primitives.getPrimitive(prefix); for (PrimitiveSet.Entry<HybridDecrypt> entry : entries) { try { return entry.getPrimitive().decrypt(ciphertextNoPrefix, contextInfo); } catch (GeneralSecurityException e) { logger.info("ciphertext prefix matches a key, but cannot decrypt: " + e.toString()); continue; } } } // Let's try all RAW keys. List<PrimitiveSet.Entry<HybridDecrypt>> entries = primitives.getRawPrimitives(); for (PrimitiveSet.Entry<HybridDecrypt> entry : entries) { try { return entry.getPrimitive().decrypt(ciphertext, contextInfo); } catch (GeneralSecurityException e) { continue; } } // nothing works. throw new GeneralSecurityException("decryption failed"); } };
@Override public byte[] decrypt(final byte[] ciphertext, final byte[] associatedData) throws GeneralSecurityException { if (ciphertext.length > CryptoFormat.NON_RAW_PREFIX_SIZE) { byte[] prefix = Arrays.copyOfRange(ciphertext, 0, CryptoFormat.NON_RAW_PREFIX_SIZE); byte[] ciphertextNoPrefix = Arrays.copyOfRange(ciphertext, CryptoFormat.NON_RAW_PREFIX_SIZE, ciphertext.length); List<PrimitiveSet.Entry<Aead>> entries = pset.getPrimitive(prefix); for (PrimitiveSet.Entry<Aead> entry : entries) { try { return entry.getPrimitive().decrypt(ciphertextNoPrefix, associatedData); } catch (GeneralSecurityException e) { logger.info("ciphertext prefix matches a key, but cannot decrypt: " + e.toString()); continue; } } } // Let's try all RAW keys. List<PrimitiveSet.Entry<Aead>> entries = pset.getRawPrimitives(); for (PrimitiveSet.Entry<Aead> entry : entries) { try { return entry.getPrimitive().decrypt(ciphertext, associatedData); } catch (GeneralSecurityException e) { continue; } } // nothing works. throw new GeneralSecurityException("decryption failed"); } };
byte[] ciphertextNoPrefix = Arrays.copyOfRange(ciphertext, CryptoFormat.NON_RAW_PREFIX_SIZE, ciphertext.length); List<PrimitiveSet.Entry<DeterministicAead>> entries = primitives.getPrimitive(prefix); for (PrimitiveSet.Entry<DeterministicAead> entry : entries) { try {
List<PrimitiveSet.Entry<Mac>> entries = primitives.getPrimitive(prefix); for (PrimitiveSet.Entry<Mac> entry : entries) { try {
byte[] sigNoPrefix = Arrays.copyOfRange(signature, CryptoFormat.NON_RAW_PREFIX_SIZE, signature.length); List<PrimitiveSet.Entry<PublicKeyVerify>> entries = primitives.getPrimitive(prefix); for (PrimitiveSet.Entry<PublicKeyVerify> entry : entries) { try {