Class: Miscreant::Internals::AES::BlockCipher

Inherits:
Object
  • Object
show all
Defined in:
lib/miscreant/internals/aes/block_cipher.rb

Overview

The AES cipher in a raw block mode (a.k.a. ECB mode)

NOTE: The only valid use of ECB mode is constructing higher-level cryptographic primitives. This library uses this class to implement the CMAC and PMAC message authentication codes.

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ BlockCipher

Create a new block cipher instance

Parameters:

  • key (String)

    a random 16-byte or 32-byte Encoding::BINARY encryption key

Raises:

  • (TypeError)

    the key was not a String

  • (ArgumentError)

    the key was the wrong length or encoding



19
20
21
22
23
24
25
26
# File 'lib/miscreant/internals/aes/block_cipher.rb', line 19

def initialize(key)
  Util.validate_bytestring("key", key, length: [16, 32])

  @cipher = OpenSSL::Cipher.new("AES-#{key.length * 8}-ECB")
  @cipher.encrypt
  @cipher.padding = 0
  @cipher.key = key
end

Instance Method Details

#encrypt(message) ⇒ Object

Encrypt the given AES block-sized message

Parameters:

  • message (String)

    a 16-byte Encoding::BINARY message to encrypt

Raises:

  • (TypeError)

    the message was not a String

  • (ArgumentError)

    the message was the wrong length



41
42
43
44
# File 'lib/miscreant/internals/aes/block_cipher.rb', line 41

def encrypt(message)
  Util.validate_bytestring("message", message, length: Block::SIZE)
  @cipher.update(message) + @cipher.final
end

#inspectString

Inspect this AES block cipher instance

Returns:

  • (String)

    description of this instance



31
32
33
# File 'lib/miscreant/internals/aes/block_cipher.rb', line 31

def inspect
  to_s
end