Module: PQCrypto::Seal::Padding
- Defined in:
- lib/pq_crypto/seal/padding.rb
Defined Under Namespace
Classes: BucketsPolicy, FixedPolicy, NoRequirement, NonePolicy, PadmePolicy, Policy, PolicyIdRequirement, 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
{
Format::PADDING_NONE => NONE,
Format::PADDING_PADME => PADME,
Format::PADDING_FIXED => PolicyIdRequirement.new(Format::PADDING_FIXED),
Format::PADDING_BUCKETS => PolicyIdRequirement.new(Format::PADDING_BUCKETS)
}.freeze
Class Method Summary
collapse
Class Method Details
.for_encryption(value) ⇒ Object
165
166
167
168
169
170
|
# File 'lib/pq_crypto/seal/padding.rb', line 165
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
152
153
154
155
156
157
158
159
160
161
162
163
|
# File 'lib/pq_crypto/seal/padding.rb', line 152
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
194
195
196
|
# File 'lib/pq_crypto/seal/padding.rb', line 194
def policy_id(policy)
for_encryption(policy).id
end
|
.requirement(value, header_policy_id:) ⇒ Object
172
173
174
175
176
177
|
# File 'lib/pq_crypto/seal/padding.rb', line 172
def requirement(value, header_policy_id:)
return NO_REQUIREMENT if value.nil? || value == false
return () if value == :from_header
for_encryption(value)
end
|
.target(base_length, policy) ⇒ Object
190
191
192
|
# File 'lib/pq_crypto/seal/padding.rb', line 190
def target(base_length, policy)
for_encryption(policy).target(base_length)
end
|
.verify!(required, header:, envelope_bytes:, content_bytes:, metadata_bytes:) ⇒ Object
179
180
181
182
183
184
185
186
187
188
|
# File 'lib/pq_crypto/seal/padding.rb', line 179
def verify!(required, header:, envelope_bytes:, content_bytes:, metadata_bytes:)
requirement(required, header_policy_id: .padding_policy_id).verify!(
Verification.new(
header: ,
envelope_bytes: envelope_bytes,
content_bytes: content_bytes,
metadata_bytes: metadata_bytes
)
)
end
|