Class: PQCrypto::KEM::SecretKey

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

Direct Known Subclasses

HybridKEM::SecretKey

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(algorithm, bytes, seed: nil) ⇒ SecretKey

Returns a new instance of SecretKey.



227
228
229
230
231
232
233
# File 'lib/pq_crypto/kem.rb', line 227

def initialize(algorithm, bytes, seed: nil)
  @algorithm = algorithm
  @bytes = String(bytes).b
  @seed = seed.nil? ? nil : String(seed).b
  validate_length!
  validate_seed_length! if @seed
end

Instance Attribute Details

#algorithmObject (readonly)

Returns the value of attribute algorithm.



225
226
227
# File 'lib/pq_crypto/kem.rb', line 225

def algorithm
  @algorithm
end

Class Method Details

.from_seed(algorithm, seed) ⇒ Object



235
236
237
238
239
240
241
# File 'lib/pq_crypto/kem.rb', line 235

def self.from_seed(algorithm, seed)
  seed_bytes = String(seed).b
  _public_key, expanded = PQCrypto.__send__(KEM.send(:native_method_for, algorithm, :keypair_from_seed), seed_bytes)
  new(algorithm, expanded, seed: seed_bytes)
rescue ArgumentError => e
  raise InvalidKeyError, e.message
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



297
298
299
300
# File 'lib/pq_crypto/kem.rb', line 297

def ==(other)
  return false unless other.is_a?(SecretKey) && other.algorithm == algorithm
  PQCrypto.__send__(:native_ct_equals, other.send(:bytes_for_native), @bytes)
end

#decapsulate(ciphertext) ⇒ Object



285
286
287
288
289
# File 'lib/pq_crypto/kem.rb', line 285

def decapsulate(ciphertext)
  PQCrypto.__send__(KEM.send(:native_method_for, @algorithm, :decapsulate), String(ciphertext).b, @bytes)
rescue ArgumentError => e
  raise InvalidCiphertextError, e.message
end

#hashObject



304
305
306
# File 'lib/pq_crypto/kem.rb', line 304

def hash
  object_id.hash
end

#inspectObject



308
309
310
# File 'lib/pq_crypto/kem.rb', line 308

def inspect
  "#<#{self.class}:0x#{object_id.to_s(16)} algorithm=#{algorithm.inspect}>"
end

#to_bytesObject



243
244
245
# File 'lib/pq_crypto/kem.rb', line 243

def to_bytes
  @bytes.dup
end

#to_pkcs8_der(format: :expanded, passphrase: nil, iterations: PKCS8::ENCRYPTED_PKCS8_DEFAULT_ITERATIONS) ⇒ Object



255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/pq_crypto/kem.rb', line 255

def to_pkcs8_der(format: :expanded, passphrase: nil, iterations: PKCS8::ENCRYPTED_PKCS8_DEFAULT_ITERATIONS)
  case format
  when :expanded
    PKCS8.encode_der(@algorithm, @bytes, format: :expanded, passphrase: passphrase, iterations: iterations)
  when :seed
    ensure_seed_available!(format)
    PKCS8.encode_der(@algorithm, @seed, format: :seed, passphrase: passphrase, iterations: iterations)
  when :both
    ensure_seed_available!(format)
    PKCS8.encode_der(@algorithm, [@seed, @bytes], format: :both, passphrase: passphrase, iterations: iterations)
  else
    raise SerializationError, "Unsupported PKCS#8 private key format: #{format.inspect}"
  end
end

#to_pkcs8_pem(format: :expanded, passphrase: nil, iterations: PKCS8::ENCRYPTED_PKCS8_DEFAULT_ITERATIONS) ⇒ Object



270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/pq_crypto/kem.rb', line 270

def to_pkcs8_pem(format: :expanded, passphrase: nil, iterations: PKCS8::ENCRYPTED_PKCS8_DEFAULT_ITERATIONS)
  case format
  when :expanded
    PKCS8.encode_pem(@algorithm, @bytes, format: :expanded, passphrase: passphrase, iterations: iterations)
  when :seed
    ensure_seed_available!(format)
    PKCS8.encode_pem(@algorithm, @seed, format: :seed, passphrase: passphrase, iterations: iterations)
  when :both
    ensure_seed_available!(format)
    PKCS8.encode_pem(@algorithm, [@seed, @bytes], format: :both, passphrase: passphrase, iterations: iterations)
  else
    raise SerializationError, "Unsupported PKCS#8 private key format: #{format.inspect}"
  end
end

#to_pqc_container_derObject



247
248
249
# File 'lib/pq_crypto/kem.rb', line 247

def to_pqc_container_der
  Serialization.secret_key_to_pqc_container_der(@algorithm, @bytes)
end

#to_pqc_container_pemObject



251
252
253
# File 'lib/pq_crypto/kem.rb', line 251

def to_pqc_container_pem
  Serialization.secret_key_to_pqc_container_pem(@algorithm, @bytes)
end

#wipe!Object



291
292
293
294
295
# File 'lib/pq_crypto/kem.rb', line 291

def wipe!
  PQCrypto.secure_wipe(@bytes)
  PQCrypto.secure_wipe(@seed) if @seed
  self
end