Class: Noise::Functions::Cipher::ChaChaPoly
- Inherits:
-
Object
- Object
- Noise::Functions::Cipher::ChaChaPoly
- Defined in:
- lib/noise/functions/cipher/cha_cha_poly.rb
Constant Summary collapse
- MAX_NONCE =
2**64 - 1
- ALGORITHM =
The name OpenSSL knows the AEAD construction of RFC 8439 by. It takes a 12 byte nonce and produces a 16 byte authentication tag, which is what the Noise specification requires of the ChaChaPoly cipher functions.
'chacha20-poly1305'- TAGLEN =
Length in bytes of the authentication tag that encrypt appends to the ciphertext.
16
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 little-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
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/noise/functions/cipher/cha_cha_poly.rb', line 27 def decrypt(k, n, ad, ciphertext) cipher = OpenSSL::Cipher.new(ALGORITHM).decrypt cipher.key = k cipher.iv = nonce_to_bytes(n) cipher.auth_data = ad cipher.auth_tag = ciphertext[-TAGLEN..] update(cipher, ciphertext[0...-TAGLEN]) + cipher.final rescue OpenSSL::Cipher::CipherError => e raise Noise::Exceptions::DecryptError, "Decrpyt failed. #{e.}", e.backtrace end |
#encrypt(k, n, ad, plaintext) ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/noise/functions/cipher/cha_cha_poly.rb', line 17 def encrypt(k, n, ad, plaintext) cipher = OpenSSL::Cipher.new(ALGORITHM).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 little-endian 64 bit integer.
39 40 41 |
# File 'lib/noise/functions/cipher/cha_cha_poly.rb', line 39 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 2**64-1, zerolen is a zero-length byte sequence, and zeros is a sequence of 32 bytes filled with zeros.
49 50 51 |
# File 'lib/noise/functions/cipher/cha_cha_poly.rb', line 49 def rekey(k) encrypt(k, MAX_NONCE, '', "\x00" * 32)[0...32] end |