Sha256: 98b6fc3344ac7cab0ab26ef229221841af974e515a10e9d0c04252b683473d3b

Contents?: true

Size: 1.56 KB

Versions: 2

Compression:

Stored size: 1.56 KB

Contents

# encoding: binary
# frozen_string_literal: true

module Miscreant
  module Internals
    module AES # :nodoc:
      # 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.
      class BlockCipher # :nodoc:
        # Create a new block cipher instance
        #
        # @param key [String] a random 16-byte or 32-byte Encoding::BINARY encryption key
        #
        # @raise [TypeError] the key was not a String
        # @raise [ArgumentError] the key was the wrong length or encoding
        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

        # Inspect this AES block cipher instance
        #
        # @return [String] description of this instance
        def inspect
          to_s
        end

        # Encrypt the given AES block-sized message
        #
        # @param message [String] a 16-byte Encoding::BINARY message to encrypt
        #
        # @raise [TypeError] the message was not a String
        # @raise [ArgumentError] the message was the wrong length
        def encrypt(message)
          Util.validate_bytestring("message", message, length: Block::SIZE)
          @cipher.update(message) + @cipher.final
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
miscreant-0.3.0 lib/miscreant/internals/aes/block_cipher.rb
miscreant-0.2.0 lib/miscreant/internals/aes/block_cipher.rb