Class: Miscreant::AES::PMAC

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

Overview

The Parallel Message Authentication Code

Constant Summary collapse

PRECOMPUTED_BLOCKS =

Number of L blocks to precompute (i.e. ?? in the PMAC paper) TODO: dynamically compute these as needed

31

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ PMAC

Create a new PMAC instance

Parameters:

  • key (String)

    16-byte or 32-byte Encoding::BINARY cryptographic key



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/miscreant/aes/pmac.rb', line 16

def initialize(key)
  @cipher = Internals::AES::BlockCipher.new(key)

  # L is defined as follows (quoted from the PMAC paper):
  #
  # Equation 1:
  #
  #     a · x =
  #         a<<1 if firstbit(a)=0
  #         (a<<1) ⊕ 0¹²⁰10000111 if firstbit(a)=1
  #
  # Equation 2:
  #
  #     a · x⁻¹ =
  #         a>>1 if lastbit(a)=0
  #         (a>>1) ⊕ 10¹²⁰1000011 if lastbit(a)=1
  #
  # Let L(0) ← L. For i ∈ [1..µ], compute L(i) ← L(i − 1) · x by
  # Equation (1) using a shift and a conditional xor.
  #
  # Compute L(−1) ← L · x⁻¹ by Equation (2), using a shift and a
  # conditional xor.
  #
  # Save the values L(−1), L(0), L(1), L(2), ..., L(µ) in a table.
  # (Alternatively, [ed: as we have done in this codebase] defer computing
  # some or  all of these L(i) values until the value is actually needed.)
  @l = []
  tmp = Internals::Block.new
  tmp.encrypt(@cipher)

  PRECOMPUTED_BLOCKS.times.each do
    block = Internals::Block.new(tmp.data.dup)
    block.data.freeze
    block.freeze

    @l << block
    tmp.dbl
  end

  @l.freeze

  # Compute L(−1) ← L · x⁻¹:
  #
  #     a>>1 if lastbit(a)=0
  #     (a>>1) ⊕ 10¹²⁰1000011 if lastbit(a)=1
  #
  @l_inv = Internals::Block.new(@l[0].data.dup)
  last_bit = @l_inv[Internals::Block::SIZE - 1] & 0x01

  (Internals::Block::SIZE - 1).downto(1) do |i|
    carry = Internals::Util.ct_select(@l_inv[i - 1] & 1, 0x80, 0)
    @l_inv[i] = (@l_inv[i] >> 1) | carry
  end

  @l_inv[0] >>= 1
  @l_inv[0] ^= Internals::Util.ct_select(last_bit, 0x80, 0)
  @l_inv[Internals::Block::SIZE - 1] ^= Internals::Util.ct_select(last_bit, Internals::Block::R >> 1, 0)
  @l_inv.freeze
  @l_inv.data.freeze
end

Instance Method Details

#digest(message) ⇒ String

Compute the PMAC of the given input message in a single shot, outputting the MAC tag.

Unlike other PMAC implementations, this one does not support incremental processing/IUF operation. (Though that would enable slightly more efficient decryption for AES-SIV)

Parameters:

  • message (String)

    an Encoding::BINARY string to authenticate

Returns:

  • (String)

    PMAC tag



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/miscreant/aes/pmac.rb', line 94

def digest(message)
  Internals::Util.validate_bytestring("message", message)

  offset = Internals::Block.new
  tmp = Internals::Block.new
  tag = Internals::Block.new
  ctr = 0
  remaining = message.bytesize

  while remaining > Internals::Block::SIZE
    offset.xor_in_place(@l.fetch(Internals::Util.ctz(ctr + 1)))

    tmp.copy(offset)
    tmp.xor_in_place(message[ctr * Internals::Block::SIZE, Internals::Block::SIZE])
    tmp.encrypt(@cipher)
    tag.xor_in_place(tmp)

    ctr += 1
    remaining -= Internals::Block::SIZE
  end

  if remaining == Internals::Block::SIZE
    tag.xor_in_place(message[(ctr * Internals::Block::SIZE)..-1])
    tag.xor_in_place(@l_inv)
  else
    remaining.times { |i| tag[i] ^= message.getbyte((ctr * Internals::Block::SIZE) + i) }
    tag[remaining] ^= 0x80
  end

  tag.encrypt(@cipher)
  tag.data
end

#inspectString

Inspect this PMAC instance

Returns:

  • (String)

    description of this instance



80
81
82
# File 'lib/miscreant/aes/pmac.rb', line 80

def inspect
  to_s
end