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.



258
259
260
261
262
263
264
265
# File 'lib/pq_crypto/kem.rb', line 258

def initialize(algorithm, bytes, seed: nil)
  @algorithm = algorithm
  @bytes = Internal.binary_string(bytes)
  @seed = seed.nil? ? nil : Internal.binary_string(seed)
  validate_length!
  validate_seed_length! if @seed
  validate_structure!
end

Instance Attribute Details

#algorithmObject (readonly)

Returns the value of attribute algorithm.



256
257
258
# File 'lib/pq_crypto/kem.rb', line 256

def algorithm
  @algorithm
end

Class Method Details

.from_seed(algorithm, seed) ⇒ Object



273
274
275
276
277
278
279
# File 'lib/pq_crypto/kem.rb', line 273

def self.from_seed(algorithm, seed)
  seed_bytes = Internal.binary_string(seed)
  _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?



313
314
315
316
# File 'lib/pq_crypto/kem.rb', line 313

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

#decapsulate(ciphertext) ⇒ Object



301
302
303
304
305
# File 'lib/pq_crypto/kem.rb', line 301

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

#hashObject



320
321
322
# File 'lib/pq_crypto/kem.rb', line 320

def hash
  object_id.hash
end

#inspectObject



324
325
326
# File 'lib/pq_crypto/kem.rb', line 324

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

#to_bytesObject



281
282
283
# File 'lib/pq_crypto/kem.rb', line 281

def to_bytes
  @bytes.dup
end

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



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

def to_pkcs8_der(format: :expanded, passphrase: nil, iterations: PKCS8::ENCRYPTED_PKCS8_DEFAULT_ITERATIONS)
  PKCS8.encode_der(@algorithm, pkcs8_material(format), format: format, passphrase: passphrase, iterations: iterations)
end

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



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

def to_pkcs8_pem(format: :expanded, passphrase: nil, iterations: PKCS8::ENCRYPTED_PKCS8_DEFAULT_ITERATIONS)
  PKCS8.encode_pem(@algorithm, pkcs8_material(format), format: format, passphrase: passphrase, iterations: iterations)
end

#to_pqc_container_derObject



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

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

#to_pqc_container_pemObject



289
290
291
# File 'lib/pq_crypto/kem.rb', line 289

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

#valid?Boolean

Returns:

  • (Boolean)


267
268
269
270
271
# File 'lib/pq_crypto/kem.rb', line 267

def valid?
  return true unless KEM.send(:checkable?, @algorithm)

  PQCrypto.__send__(KEM.send(:native_method_for, @algorithm, :check_secret_key), @bytes)
end

#wipe!Object



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

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