Class: Noise::State::CipherState
- Inherits:
-
Object
- Object
- Noise::State::CipherState
- Defined in:
- lib/noise/state/cipher_state.rb
Overview
A CipherState can encrypt and decrypt data based on its k and n variables:
- k: A cipher key of 32 bytes (which may be empty). Empty is a special value which indicates k has not yet been initialized.
- n: An 8-byte (64-bit) unsigned integer nonce.
Constant Summary collapse
- MAX_NONCE =
2**64 - 1
- TAG_LENGTH =
16
Instance Attribute Summary collapse
-
#k ⇒ Object
readonly
Returns the value of attribute k.
-
#n ⇒ Object
readonly
Returns the value of attribute n.
Instance Method Summary collapse
- #decrypt_with_ad(ad, ciphertext) ⇒ Object
-
#encrypt_with_ad(ad, plaintext) ⇒ Object
@return [String] ENCRYPT(k, n++, ad, plaintext) if k is non-empty, otherwise returns plaintext.
-
#initialize(cipher:) ⇒ CipherState
constructor
A new instance of CipherState.
- #initialize_key(key) ⇒ Object
-
#key? ⇒ Boolean
True if k is non-empty, false otherwise.
- #nonce=(nonce) ⇒ Object
- #rekey ⇒ Object
Constructor Details
#initialize(cipher:) ⇒ CipherState
Returns a new instance of CipherState.
17 18 19 |
# File 'lib/noise/state/cipher_state.rb', line 17 def initialize(cipher:) @cipher = cipher end |
Instance Attribute Details
#k ⇒ Object (readonly)
Returns the value of attribute k.
15 16 17 |
# File 'lib/noise/state/cipher_state.rb', line 15 def k @k end |
#n ⇒ Object (readonly)
Returns the value of attribute n.
15 16 17 |
# File 'lib/noise/state/cipher_state.rb', line 15 def n @n end |
Instance Method Details
#decrypt_with_ad(ad, ciphertext) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/noise/state/cipher_state.rb', line 47 def decrypt_with_ad(ad, ciphertext) return ciphertext unless key? raise Noise::Exceptions::MaxNonceError if @n == MAX_NONCE # Without this the ciphers slice a nil authentication tag out of the truncated input. raise Noise::Exceptions::DecryptError, 'Ciphertext is shorter than the tag.' if ciphertext.bytesize < TAG_LENGTH plaintext = @cipher.decrypt(@k, @n, ad, ciphertext) @n += 1 plaintext end |
#encrypt_with_ad(ad, plaintext) ⇒ Object
@return [String] ENCRYPT(k, n++, ad, plaintext) if k is non-empty, otherwise returns plaintext.
37 38 39 40 41 42 43 44 |
# File 'lib/noise/state/cipher_state.rb', line 37 def encrypt_with_ad(ad, plaintext) return plaintext unless key? raise Noise::Exceptions::MaxNonceError if @n == MAX_NONCE ciphertext = @cipher.encrypt(@k, @n, ad, plaintext) @n += 1 ciphertext end |
#initialize_key(key) ⇒ Object
22 23 24 25 |
# File 'lib/noise/state/cipher_state.rb', line 22 def initialize_key(key) @k = key @n = 0 end |
#key? ⇒ Boolean
Returns true if k is non-empty, false otherwise.
28 29 30 |
# File 'lib/noise/state/cipher_state.rb', line 28 def key? !@k.nil? end |
#nonce=(nonce) ⇒ Object
32 33 34 |
# File 'lib/noise/state/cipher_state.rb', line 32 def nonce=(nonce) @n = nonce end |
#rekey ⇒ Object
59 60 61 |
# File 'lib/noise/state/cipher_state.rb', line 59 def rekey @k = @cipher.rekey(@k) end |