/** @param serializedKey serialized {@code AesGcmKey} proto */ @Override public Aead getPrimitive(ByteString serializedKey) throws GeneralSecurityException { try { AesGcmKey keyProto = AesGcmKey.parseFrom(serializedKey); return getPrimitive(keyProto); } catch (InvalidProtocolBufferException e) { throw new GeneralSecurityException("expected AesGcmKey proto"); } }
/** * @param serializedKeyFormat serialized {@code AesGcmKeyFormat} proto * @return new {@code AesGcmKey} proto */ @Override public MessageLite newKey(ByteString serializedKeyFormat) throws GeneralSecurityException { try { AesGcmKeyFormat format = AesGcmKeyFormat.parseFrom(serializedKeyFormat); return newKey(format); } catch (InvalidProtocolBufferException e) { throw new GeneralSecurityException("expected serialized AesGcmKeyFormat proto", e); } }
/** @param key {@code AesGcmKey} proto */ @Override public Aead getPrimitive(MessageLite key) throws GeneralSecurityException { if (!(key instanceof AesGcmKey)) { throw new GeneralSecurityException("expected AesGcmKey proto"); } AesGcmKey keyProto = (AesGcmKey) key; validate(keyProto); return new AesGcmJce(keyProto.getKeyValue().toByteArray()); }
private KeyManager<Aead> aeadKeyManager(String typeUrl) throws GeneralSecurityException { switch (typeUrl) { case AesCtrHmacAeadKeyManager.TYPE_URL: return new AesCtrHmacAeadKeyManager(); case AesEaxKeyManager.TYPE_URL: return new AesEaxKeyManager(); case AesGcmKeyManager.TYPE_URL: return new AesGcmKeyManager(); case ChaCha20Poly1305KeyManager.TYPE_URL: return new ChaCha20Poly1305KeyManager(); case KmsAeadKeyManager.TYPE_URL: return new KmsAeadKeyManager(); case KmsEnvelopeAeadKeyManager.TYPE_URL: return new KmsEnvelopeAeadKeyManager(); default: throw new GeneralSecurityException( String.format("No support for primitive 'Aead' with key type '%s'.", typeUrl)); } } }
/** * @param keyFormat {@code AesGcmKeyFormat} proto * @return new {@code AesGcmKey} proto */ @Override public MessageLite newKey(MessageLite keyFormat) throws GeneralSecurityException { if (!(keyFormat instanceof AesGcmKeyFormat)) { throw new GeneralSecurityException("expected AesGcmKeyFormat proto"); } AesGcmKeyFormat format = (AesGcmKeyFormat) keyFormat; validate(format); return AesGcmKey.newBuilder() .setKeyValue(ByteString.copyFrom(Random.randBytes(format.getKeySize()))) .setVersion(VERSION) .build(); }
/** * @param serializedKeyFormat serialized {@code AesGcmKeyFormat} proto * @return {@code KeyData} proto with a new {@code AesGcmKey} proto */ @Override public KeyData newKeyData(ByteString serializedKeyFormat) throws GeneralSecurityException { AesGcmKey key = (AesGcmKey) newKey(serializedKeyFormat); return KeyData.newBuilder() .setTypeUrl(TYPE_URL) .setValue(key.toByteString()) .setKeyMaterialType(KeyData.KeyMaterialType.SYMMETRIC) .build(); }