Module: PQCrypto::Seal::Padding

Defined in:
lib/pq_crypto/seal/padding.rb

Defined Under Namespace

Classes: BucketsPolicy, FixedPolicy, NoRequirement, NonePolicy, PadmePolicy, Policy, Verification

Constant Summary collapse

NONE =
NonePolicy.new
PADME =
PadmePolicy.new
NO_REQUIREMENT =
NoRequirement.new.freeze
SIMPLE_POLICIES =
{
  nil => NONE,
  false => NO_REQUIREMENT,
  none: NONE,
  padme: PADME
}.freeze
HEADER_POLICIES =
{
  Format::PADDING_NONE => NONE,
  Format::PADDING_PADME => PADME
}.freeze

Class Method Summary collapse

Class Method Details

.for_encryption(value) ⇒ Object



148
149
150
151
152
153
# File 'lib/pq_crypto/seal/padding.rb', line 148

def for_encryption(value)
  return SIMPLE_POLICIES.fetch(value) if SIMPLE_POLICIES.key?(value) && value != false
  return policy_from_hash(value) if value.is_a?(Hash)
  raise InvalidConfigurationError, "padding: :preserve is only valid for rotate_dek" if value == :preserve
  raise InvalidConfigurationError, "unsupported padding policy: #{value.inspect}"
end

.padme_target(length) ⇒ Object

Raises:

  • (ArgumentError)


135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/pq_crypto/seal/padding.rb', line 135

def padme_target(length)
  value = Integer(length)
  raise ArgumentError, "length must be non-negative" if value.negative?
  return value if value <= 1

  exponent = value.bit_length - 1
  low_bits = exponent - exponent.bit_length
  return value unless low_bits.positive?

  mask = (1 << low_bits) - 1
  (value + mask) & ~mask
end

.policy_id(policy) ⇒ Object



177
178
179
# File 'lib/pq_crypto/seal/padding.rb', line 177

def policy_id(policy)
  for_encryption(policy).id
end

.requirement(value, header_policy_id:) ⇒ Object



155
156
157
158
159
160
# File 'lib/pq_crypto/seal/padding.rb', line 155

def requirement(value, header_policy_id:)
  return NO_REQUIREMENT if value.nil? || value == false
  return policy_from_header(header_policy_id) if value == :from_header

  for_encryption(value)
end

.target(base_length, policy) ⇒ Object



173
174
175
# File 'lib/pq_crypto/seal/padding.rb', line 173

def target(base_length, policy)
  for_encryption(policy).target(base_length)
end

.verify!(required, header:, envelope_bytes:, content_bytes:, metadata_bytes:) ⇒ Object



162
163
164
165
166
167
168
169
170
171
# File 'lib/pq_crypto/seal/padding.rb', line 162

def verify!(required, header:, envelope_bytes:, content_bytes:, metadata_bytes:)
  requirement(required, header_policy_id: header.padding_policy_id).verify!(
    Verification.new(
      header: header,
      envelope_bytes: envelope_bytes,
      content_bytes: content_bytes,
      metadata_bytes: 
    )
  )
end