Module: RbNaCl::SelfTest
- Defined in:
- lib/rbnacl/self_test.rb
Overview
Self-test performed at startup
Class Method Summary collapse
- .box_common_test(box) ⇒ Object
- .box_test ⇒ Object
- .digital_signature_test ⇒ Object
- .hmac_test(klass, tag) ⇒ Object
- .secret_box_test ⇒ Object
- .sha256_test ⇒ Object
- .vector(name) ⇒ Object
Class Method Details
.box_common_test(box) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/rbnacl/self_test.rb', line 30 def box_common_test(box) nonce = vector :box_nonce = vector :box_message ciphertext = vector :box_ciphertext fail SelfTestFailure, "failed to generate correct ciphertext" unless box.encrypt(nonce, ) == ciphertext fail SelfTestFailure, "failed to decrypt ciphertext correctly" unless box.decrypt(nonce, ciphertext) == begin passed = false corrupt_ct = ciphertext.dup corrupt_ct[23] = " " box.decrypt(nonce, corrupt_ct) rescue CryptoError passed = true ensure passed || fail(SelfTestFailure, "failed to detect corrupt ciphertext") end end |
.box_test ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/rbnacl/self_test.rb', line 17 def box_test alicepk = RbNaCl::PublicKey.new(vector(:alice_public)) bobsk = RbNaCl::PrivateKey.new(vector(:bob_private)) box = RbNaCl::Box.new(alicepk, bobsk) box_common_test(box) end |
.digital_signature_test ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/rbnacl/self_test.rb', line 50 def digital_signature_test signing_key = SigningKey.new(vector(:sign_private)) verify_key = signing_key.verify_key unless verify_key.to_s == vector(:sign_public) #:nocov: fail SelfTestFailure, "failed to generate verify key correctly" #:nocov: end = vector :sign_message signature = signing_key.sign() unless signature == vector(:sign_signature) #:nocov: fail SelfTestFailure, "failed to generate correct signature" #:nocov: end unless verify_key.verify(signature, ) #:nocov: fail SelfTestFailure, "failed to verify a valid signature" #:nocov: end begin passed = false bad_signature = signature[0, 63] + "0" verify_key.verify(bad_signature, ) rescue CryptoError passed = true ensure passed || fail(SelfTestFailure, "failed to detect corrupt ciphertext") end end |
.hmac_test(klass, tag) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/rbnacl/self_test.rb', line 93 def hmac_test(klass, tag) authenticator = klass.new(vector(:auth_key)) = vector :auth_message fail SelfTestFailure, "#{klass} failed to generate correct authentication tag" unless authenticator.auth() == vector(tag) fail SelfTestFailure, "#{klass} failed to verify correct authentication tag" unless authenticator.verify(vector(tag), ) begin passed = false authenticator.verify(vector(tag), + " ") rescue CryptoError passed = true ensure passed || fail(SelfTestFailure, "failed to detect corrupt ciphertext") end end |
.secret_box_test ⇒ Object
25 26 27 28 |
# File 'lib/rbnacl/self_test.rb', line 25 def secret_box_test box = SecretBox.new(vector(:secret_key)) box_common_test(box) end |
.sha256_test ⇒ Object
86 87 88 89 90 91 |
# File 'lib/rbnacl/self_test.rb', line 86 def sha256_test = vector :sha256_message digest = vector :sha256_digest fail SelfTestFailure, "failed to generate a correct SHA256 digest" unless RbNaCl::Hash.sha256() == digest end |
.vector(name) ⇒ Object
13 14 15 |
# File 'lib/rbnacl/self_test.rb', line 13 def vector(name) [TEST_VECTORS[name]].pack("H*") end |