Skip to content

Commit

Permalink
SHA256 self-test
Browse files Browse the repository at this point in the history
  • Loading branch information
tarcieri committed Mar 7, 2013
1 parent b58ada0 commit 8a0eab8
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions lib/rbnacl/self_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ def secret_box_test
end

def box_common_test(box)
nonce = Encoder[:hex].decode TestVectors[:box_nonce]
message = Encoder[:hex].decode TestVectors[:box_message]
ciphertext = Encoder[:hex].decode TestVectors[:box_ciphertext]
nonce = hexdecode_vector :box_nonce
message = hexdecode_vector :box_message
ciphertext = hexdecode_vector :box_ciphertext

unless box.encrypt(nonce, message) == ciphertext
raise SelfTestFailure, "failed to generate correct ciphertext"
Expand Down Expand Up @@ -52,7 +52,7 @@ def digital_signature_test
raise SelfTestFailure, "failed to generate verify key correctly"
end

message = Encoder[:hex].decode TestVectors[:sign_message]
message = hexdecode_vector :sign_message
signature = signing_key.sign(message, :hex)

unless signature == TestVectors[:sign_signature]
Expand All @@ -70,10 +70,19 @@ def digital_signature_test
end
end

def sha256_test
message = hexdecode_vector :sha256_message
digest = hexdecode_vector :sha256_digest

unless Crypto::Hash.sha256(message) == digest
raise SelfTestFailure, "failed to generate a correct SHA256 digest"
end
end

def hmac_test(klass, tag)
authenticator = klass.new(TestVectors[:auth_key], :hex)

message = Encoder[:hex].decode TestVectors[:auth_message]
message = hexdecode_vector :auth_message

unless authenticator.auth(message, :hex) == TestVectors[tag]
raise SelfTestFailure, "#{klass} failed to generate correct authentication tag"
Expand All @@ -87,12 +96,21 @@ def hmac_test(klass, tag)
raise SelfTestFailure, "#{klass} failed to detect invalid authentication tag"
end
end

def hexdecode(string)
Encoder[:hex].decode(string)
end

def hexdecode_vector(name)
hexdecode TestVectors[name]
end
end
end

Crypto::SelfTest.box_test
Crypto::SelfTest.secret_box_test
Crypto::SelfTest.digital_signature_test
Crypto::SelfTest.sha256_test
Crypto::SelfTest.hmac_test Crypto::HMAC::SHA256, :auth_hmacsha256
Crypto::SelfTest.hmac_test Crypto::HMAC::SHA512256, :auth_hmacsha512256
Crypto::SelfTest.hmac_test Crypto::Auth::OneTime, :auth_onetime
Expand Down

0 comments on commit 8a0eab8

Please sign in to comment.