Class: Miscreant::STREAM::Encryptor

Inherits:
Object
  • Object
show all
Defined in:
lib/miscreant/stream.rb

Overview

A STREAM encryptor

This corresponds to the ??? stream encryptor object as defined in the paper Online Authenticated-Encryption and its Nonce-Reuse Misuse-Resistance

Instance Method Summary collapse

Constructor Details

#initialize(alg, key, nonce) ⇒ Encryptor

Create a new STREAM encryptor.

Parameters:

  • alg ("AES-SIV", "AES-PMAC-SIV")

    cryptographic algorithm to use

  • key (String)

    32-byte or 64-byte random Encoding::BINARY secret key

  • nonce (String)

    8-byte nonce

Raises:

  • (TypeError)

    nonce is not a String

  • (ArgumentError)

    nonce is wrong length or not Encoding::BINARY



36
37
38
39
# File 'lib/miscreant/stream.rb', line 36

def initialize(alg, key, nonce)
  @aead = AEAD.new(alg, key)
  @nonce_encoder = NonceEncoder.new(nonce)
end

Instance Method Details

#inspectString

Inspect this STREAM encryptor instance

Returns:

  • (String)

    description of this instance



55
56
57
# File 'lib/miscreant/stream.rb', line 55

def inspect
  to_s
end

#seal(plaintext, ad: "", last_block: false) ⇒ String

Encrypt the next message in the stream

Parameters:

  • plaintext (String)

    plaintext message to encrypt

  • ad (String) (defaults to: "")

    (optional) associated data to authenticate

  • last_block (true, false) (defaults to: false)

    is this the last block in the STREAM?

Returns:

  • (String)

    ciphertext message



48
49
50
# File 'lib/miscreant/stream.rb', line 48

def seal(plaintext, ad: "", last_block: false)
  @aead.seal(plaintext, nonce: @nonce_encoder.next(last_block), ad: ad)
end