Class: Noise::Functions::Cipher::AesGcm
- Inherits:
-
Object
- Object
- Noise::Functions::Cipher::AesGcm
- Defined in:
- lib/noise/functions/cipher/aes_gcm.rb
Constant Summary collapse
- MAX_NONCE =
2**64 - 1
Instance Method Summary collapse
- #decrypt(k, n, ad, ciphertext) ⇒ Object
- #encrypt(k, n, ad, plaintext) ⇒ Object
-
#nonce_to_bytes(n) ⇒ Object
4 zero bytes followed by n as a big-endian 64 bit integer.
-
#rekey(k) ⇒ Object
Returns a new 32-byte cipher key as a pseudorandom function of k.
Instance Method Details
#decrypt(k, n, ad, ciphertext) ⇒ Object
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/noise/functions/cipher/aes_gcm.rb', line 19 def decrypt(k, n, ad, ciphertext) cipher = OpenSSL::Cipher.new('aes-256-gcm').decrypt cipher.key = k cipher.iv = nonce_to_bytes(n) cipher.auth_data = ad cipher.auth_tag = ciphertext[-16..] update(cipher, ciphertext[0...-16]) + cipher.final rescue OpenSSL::Cipher::CipherError => e raise Noise::Exceptions::DecryptError, "Decrpyt failed. #{e.}", e.backtrace end |
#encrypt(k, n, ad, plaintext) ⇒ Object
9 10 11 12 13 14 15 16 17 |
# File 'lib/noise/functions/cipher/aes_gcm.rb', line 9 def encrypt(k, n, ad, plaintext) cipher = OpenSSL::Cipher.new('aes-256-gcm').encrypt cipher.key = k cipher.iv = nonce_to_bytes(n) cipher.auth_data = ad update(cipher, plaintext) + cipher.final + cipher.auth_tag rescue OpenSSL::Cipher::CipherError => e raise Noise::Exceptions::EncryptError, "Encrypt failed. #{e.}", e.backtrace end |
#nonce_to_bytes(n) ⇒ Object
4 zero bytes followed by n as a big-endian 64 bit integer.
31 32 33 |
# File 'lib/noise/functions/cipher/aes_gcm.rb', line 31 def nonce_to_bytes(n) "\x00" * 4 + [n].pack('Q>') end |
#rekey(k) ⇒ Object
Returns a new 32-byte cipher key as a pseudorandom function of k. If this function is not specifically defined for some set of cipher functions, then it defaults to returning the first 32 bytes from ENCRYPT(k,maxnonce, zerolen, zeros), where maxnonce equals 264-1, zerolen is a zero-length byte sequence, and zeros is a sequence of 32 bytes filled with zeros.
41 42 43 |
# File 'lib/noise/functions/cipher/aes_gcm.rb', line 41 def rekey(k) encrypt(k, MAX_NONCE, '', "\x00" * 32)[0...32] end |