fn aes_gcm_encrypt<T: Serialize + DeserializeOwned>(
    algorithm: &'static aead::Algorithm,
    payload: &[u8],
    nonce: &[u8],
    aad: &[u8],
    key: &jwk::JWK<T>,
) -> Result<EncryptionResult, Error> {
    // JWA needs a 128 bit tag length. We need to assert that the algorithm has 128 bit tag length
    assert_eq!(algorithm.tag_len(), AES_GCM_TAG_SIZE);
    // Also the nonce (or initialization vector) needs to be 96 bits
    assert_eq!(algorithm.nonce_len(), AES_GCM_NONCE_LENGTH);

    let key = key.algorithm.octet_key()?;
    let key = aead::UnboundKey::new(algorithm, key)?;
    let sealing_key = aead::LessSafeKey::new(key);

    let mut in_out: Vec<u8> = payload.to_vec();
    let tag = sealing_key.seal_in_place_separate_tag(
        aead::Nonce::try_assume_unique_for_key(nonce)?,
        aead::Aad::from(aad),
        &mut in_out,
    )?;

    Ok(EncryptionResult {
        nonce: nonce.to_vec(),
        encrypted: in_out,
        tag: tag.as_ref().to_vec(),
        additional_data: aad.to_vec(),
    })
}
/// Encrypt a payload with AES GCM
Encrypt a payload with AES GCM
Encrypt a payload with AES GCM
fn aes_gcm_encrypt<T: Serialize + DeserializeOwned>(
    algorithm: &'static aead::Algorithm,
    payload: &[u8],
    nonce: &[u8],
    aad: &[u8],
    key: &jwk::JWK<T>,
) -> Result<EncryptionResult, Error> {
    // JWA needs a 128 bit tag length. We need to assert that the algorithm has 128 bit tag length
    assert_eq!(algorithm.tag_len(), AES_GCM_TAG_SIZE);
    // Also the nonce (or initialization vector) needs to be 96 bits
    assert_eq!(algorithm.nonce_len(), AES_GCM_NONCE_LENGTH);
    let key = key.algorithm.octet_key()?;
    let key = aead::UnboundKey::new(algorithm, key)?;
    let sealing_key = aead::LessSafeKey::new(key);
    let mut in_out: Vec<u8> = payload.to_vec();
    let tag = sealing_key.seal_in_place_separate_tag(
        aead::Nonce::try_assume_unique_for_key(nonce)?,
        aead::Aad::from(aad),
        &mut in_out,
    )?;
    Ok(EncryptionResult {
        nonce: nonce.to_vec(),
        encrypted: in_out,
        tag: tag.as_ref().to_vec(),
        additional_data: aad.to_vec(),
    })
}
fn aes_gcm_encrypt < T : Serialize + DeserializeOwned > ( algorithm : & ' static aead :: Algorithm , payload : & [ u8 ] , nonce : & [ u8 ] , aad : & [ u8 ] , key : & jwk :: JWK < T > , ) -> Result < EncryptionResult , Error > { // JWA needs a 128 bit tag length. We need to assert that the algorithm has 128 bit tag length assert_eq ! ( algorithm tag_len ( ) AES_GCM_TAG_SIZE ) ; // Also the nonce (or initialization vector) needs to be 96 bits assert_eq ! ( algorithm nonce_len ( ) AES_GCM_NONCE_LENGTH ) ; let key = key . algorithm . octet_key ( ) ? ; let key = aead :: UnboundKey :: new ( algorithm , key ) ? ; let sealing_key = aead :: LessSafeKey :: new ( key ) ; let mut in_out : Vec < u8 > = payload . to_vec ( ) ; let tag = sealing_key . seal_in_place_separate_tag ( aead :: Nonce :: try_assume_unique_for_key ( nonce ) ? , aead :: Aad :: from ( aad ) , & mut in_out , ) ? ; Ok ( EncryptionResult { nonce : nonce . to_vec ( ) , encrypted : in_out , tag : tag . as_ref ( ) . to_vec ( ) , additional_data : aad . to_vec ( ) , } ) }
Encrypt a payload with AES GCM
Encrypt a payload with AES GCM
algorithm
&'static aead::Algorithm
payload
&[u8]
nonce
&[u8]
aad
&[u8]
key
&jwk::JWK<T>
algorithm
&'static aead::Algorithm
payload
&[u8]
nonce
&[u8]
aad
&[u8]
key
&jwk::JWK<T>