Class: OpenSSL::CMAC

Inherits:
Object
  • Object
show all
Defined in:
lib/openssl/cmac.rb,
lib/openssl/cmac/version.rb

Overview

Abstract from tools.ietf.org/html/rfc4493:

The National Institute of Standards and Technology (NIST) has recently specified the Cipher-based Message Authentication Code (CMAC), which is equivalent to the One-Key CBC MAC1 (OMAC1) submitted by Iwata and Kurosawa. This memo specifies an authentication algorithm based on CMAC with the 128-bit Advanced Encryption Standard (AES). This new authentication algorithm is named AES-CMAC. The purpose of this document is to make the AES-CMAC algorithm conveniently available to the Internet Community.

tools.ietf.org/html/rfc4494 reduces the length of the result from 16 to 12 Byte.

tools.ietf.org/html/rfc4615 allows to use variable key sizes.

Constant Summary collapse

VERSION =
'2.1.0'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cipher, key = '') ⇒ Object

Returns an instance of OpenSSL::CMAC set with the cipher algorithm and key to be used. The instance represents the initial state of the message authentication code before any data has been processed. To process data with it, use the instance method update with your data as an argument.

Parameters:

  • cipher (String)

    entry of OpenSSL::CMAC.ciphers

  • key (String) (defaults to: '')

    binary key string

Raises:



60
61
62
63
64
65
66
67
68
# File 'lib/openssl/cmac.rb', line 60

def initialize(cipher, key = '')
  raise CMACError, "unsupported cipher algorithm (#{cipher})" unless CMAC.ciphers.include?(cipher.upcase)

  @keys = []
  @buffer = String.new.force_encoding('ASCII-8BIT')
  @cipher = OpenSSL::Cipher.new("#{cipher.upcase}-128-CBC")

  self.key = key unless key == ''
end

Class Method Details

.ciphers[String]

Searches for supported algorithms within OpenSSL

Returns:

  • ([String])

    supported algorithms



34
35
36
# File 'lib/openssl/cmac.rb', line 34

def self.ciphers
  @ciphers ||= OpenSSL::Cipher.ciphers.select { |c| c.match(/-128-CBC$/i) }.map { |e| e[0..-9].upcase }.uniq
end

.digest(cipher, key, data, length = 16) ⇒ String

Returns the authentication code as a binary string. The cipher parameter must be an entry of OpenSSL::CMAC.ciphers.

Parameters:

  • cipher (String)

    entry of OpenSSL::CMAC.ciphers

  • key (String)

    binary key string

  • data (String)

    binary data string

  • length (Number) (defaults to: 16)

    length of the authentication code

Returns:

  • (String)

    authentication code



47
48
49
# File 'lib/openssl/cmac.rb', line 47

def self.digest(cipher, key, data, length = 16)
  CMAC.new(cipher, key).update(data).digest(length)
end

Instance Method Details

#<<(data) ⇒ Object

Alias for: update



84
85
86
# File 'lib/openssl/cmac.rb', line 84

def <<(data)
  update(data)
end

#block_lengthNumber

Returns the block length of the used cipher algorithm.

Returns:

  • (Number)

    length of the used cipher algorithm



91
92
93
# File 'lib/openssl/cmac.rb', line 91

def block_length
  16
end

#digest(length = 16) ⇒ Object

Returns the authentication code an instance represents as a binary string.

Parameters:

  • length (Number) (defaults to: 16)

    length of the authentication code

Raises:



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/openssl/cmac.rb', line 134

def digest(length = 16)
  raise CMACError, 'no key is set' if @keys[0].nil?
  raise CMACError, 'no key is set' unless length.between?(1, 16)

  block = @buffer.bytes
  k = @keys[block.length == 16 ? 1 : 2].dup
  i = block.length.times { |t| k[t] ^= block[t] }
  k[i] ^= 0x80 if i < 16
  mac = @cipher.update(k.pack('C*')) + @cipher.final
  reset_with_key(@keys[0])
  # Each block is 16-bytes and the last block will always be PKCS#7 padding
  # which we want to discard.  Take the last block prior to the padding for
  # the MAC.
  mac[-32...(-32 + length)]
end

#digest_max_lengthNumber

Returns the maximum length of the resulting digest.

Returns:

  • (Number)

    maximum length of the resulting digest



98
99
100
# File 'lib/openssl/cmac.rb', line 98

def digest_max_length
  16
end

#key=(key) ⇒ Object

Returns self as it was when it was first initialized with new key, with all processed data cleared from it.

Parameters:

  • key (String)

    binary key string



74
75
76
77
78
79
80
81
# File 'lib/openssl/cmac.rb', line 74

def key=(key)
  reset
  key = CMAC.digest('AES', "\x00" * 16, key, 16) unless key.b.length == 16

  @keys[0] = key.dup
  @cipher.key = @keys[0]
  generate_subkey
end

#nameString

Returns the name of the used authentication code algorithm.

Returns:

  • (String)

    name of the used authentication code algorithm



105
106
107
# File 'lib/openssl/cmac.rb', line 105

def name
  "CMAC with #{@cipher.name[0..-9]}"
end

#resetObject

Returns self as it was when it was first initialized, with all processed data cleared from it.

Returns:

  • (Object)

    self with initial state



113
114
115
# File 'lib/openssl/cmac.rb', line 113

def reset
  reset_with_key
end

#update(data) ⇒ Object

Returns self updated with the message to be authenticated. Can be called repeatedly with chunks of the message.

Parameters:

  • data (String)

    binary data string

Returns:

  • (Object)

    self with new state

Raises:



123
124
125
126
127
128
129
# File 'lib/openssl/cmac.rb', line 123

def update(data)
  raise CMACError, 'no key is set' if @keys[0].nil?

  @buffer += data
  @cipher.update(@buffer.slice!(0...16)) while @buffer.length > 16
  self
end