Class: PQCrypto::Seal::Format::HeaderCodec

Inherits:
Object
  • Object
show all
Defined in:
lib/pq_crypto/seal/format.rb

Class Method Summary collapse

Class Method Details

.build(payload_id:, payload_nonce:, recipient_capacity:, slot_size:, padded_inner_length:, public_metadata:, padding_policy_id:) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/pq_crypto/seal/format.rb', line 84

def build(payload_id:, payload_nonce:, recipient_capacity:, slot_size:,
          padded_inner_length:, public_metadata:, padding_policy_id:)
   = String().b
  validate_build_input!(
    payload_id, payload_nonce, , padding_policy_id
  )

  body = [
    Binary.u16(CONTENT_SUITE_AEGIS256),
    Binary.u8(LOOKUP_HINT),
    Binary.u16(FLAGS),
    Binary.u8(padding_policy_id),
    payload_id,
    payload_nonce,
    Binary.u16(recipient_capacity),
    Binary.u16(slot_size),
    Binary.u64(padded_inner_length),
    Binary.u32(.bytesize),
    
  ].join.b

  header = MAGIC + Binary.u8(VERSION) + Binary.u32(HEADER_PREFIX_BYTES + body.bytesize) + body
  raise InvalidConfigurationError, "header is too large" if header.bytesize > MAX_HEADER_BYTES

  header
end

.parse(bytes) ⇒ Object

Raises:



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/pq_crypto/seal/format.rb', line 111

def parse(bytes)
  source = String(bytes).b
  prefix = Binary::Reader.new(source)
  expect!(prefix.bytes(MAGIC.bytesize), MAGIC, "invalid envelope magic")
  expect!(prefix.u8, VERSION, "unsupported format version")
  header_length = prefix.u32
  validate_header_length!(header_length, prefix.offset)

  Binary.ensure_available!(source, 0, header_length)
  raw = source.byteslice(0, header_length).b
  fields = Binary::Reader.new(raw, prefix.offset)

  content_suite = fields.u16
  expect_suite!(content_suite, CONTENT_SUITE_AEGIS256, "content")
  lookup_mode = fields.u8
  expect_suite!(lookup_mode, LOOKUP_HINT, "lookup mode")
  flags = fields.u16
  expect!(flags, FLAGS, "unknown header flags")
  padding_policy_id = Format.validate_padding_policy_id!(fields.u8, error: FormatError)
  payload_id = fields.bytes(PAYLOAD_ID_BYTES)
  payload_nonce = fields.bytes(NONCE_BYTES)
  capacity = Format.validate_capacity!(fields.u16, error: FormatError)
  slot_size = Format.validate_slot_size!(fields.u16, error: FormatError)
  padded_inner_length = fields.u64
  raise FormatError, "padded inner frame is too short" if padded_inner_length < INNER_PREFIX_BYTES

  public_length = fields.u32
  raise FormatError, "public metadata is too large" if public_length > MAX_PUBLIC_METADATA_BYTES
   = fields.bytes(public_length)
  raise FormatError, "non-canonical header length" unless fields.finished?

  Header.new(
    raw: raw,
    content_suite_id: content_suite,
    lookup_mode: lookup_mode,
    flags: flags,
    padding_policy_id: padding_policy_id,
    payload_id: payload_id,
    payload_nonce: payload_nonce,
    recipient_capacity: capacity,
    slot_size: slot_size,
    padded_inner_length: padded_inner_length,
    public_metadata: 
  )
end