1require_relative 'utils' 2 3class OpenSSL::TestPKCS5 < Test::Unit::TestCase 4 5 def test_pbkdf2_hmac_sha1_rfc6070_c_1_len_20 6 p ="password" 7 s = "salt" 8 c = 1 9 dk_len = 20 10 raw = %w{ 0c 60 c8 0f 96 1f 0e 71 11 f3 a9 b5 24 af 60 12 06 12 2f e0 37 a6 } 13 expected = [raw.join('')].pack('H*') 14 value = OpenSSL::PKCS5.pbkdf2_hmac_sha1(p, s, c, dk_len) 15 assert_equal(expected, value) 16 end 17 18 def test_pbkdf2_hmac_sha1_rfc6070_c_2_len_20 19 p ="password" 20 s = "salt" 21 c = 2 22 dk_len = 20 23 raw = %w{ ea 6c 01 4d c7 2d 6f 8c 24 cd 1e d9 2a ce 1d 41 f0 25 d8 de 89 57 } 26 expected = [raw.join('')].pack('H*') 27 value = OpenSSL::PKCS5.pbkdf2_hmac_sha1(p, s, c, dk_len) 28 assert_equal(expected, value) 29 end 30 31 def test_pbkdf2_hmac_sha1_rfc6070_c_4096_len_20 32 p ="password" 33 s = "salt" 34 c = 4096 35 dk_len = 20 36 raw = %w{ 4b 00 79 01 b7 65 48 9a 37 be ad 49 d9 26 f7 21 d0 38 65 a4 29 c1 } 39 expected = [raw.join('')].pack('H*') 40 value = OpenSSL::PKCS5.pbkdf2_hmac_sha1(p, s, c, dk_len) 41 assert_equal(expected, value) 42 end 43 44# takes too long! 45# def test_pbkdf2_hmac_sha1_rfc6070_c_16777216_len_20 46# p ="password" 47# s = "salt" 48# c = 16777216 49# dk_len = 20 50# raw = %w{ ee fe 3d 61 cd 4d a4 e4 51# e9 94 5b 3d 6b a2 15 8c 52# 26 34 e9 84 } 53# expected = [raw.join('')].pack('H*') 54# value = OpenSSL::PKCS5.pbkdf2_hmac_sha1(p, s, c, dk_len) 55# assert_equal(expected, value) 56# end 57 58 def test_pbkdf2_hmac_sha1_rfc6070_c_4096_len_25 59 p ="passwordPASSWORDpassword" 60 s = "saltSALTsaltSALTsaltSALTsaltSALTsalt" 61 c = 4096 62 dk_len = 25 63 64 raw = %w{ 3d 2e ec 4f e4 1c 84 9b 65 80 c8 d8 36 62 c0 e4 4a 66 8b 29 1a 96 4c f2 f0 70 67 38 } 68 expected = [raw.join('')].pack('H*') 69 value = OpenSSL::PKCS5.pbkdf2_hmac_sha1(p, s, c, dk_len) 70 assert_equal(expected, value) 71 end 72 73 def test_pbkdf2_hmac_sha1_rfc6070_c_4096_len_16 74 p ="pass\0word" 75 s = "sa\0lt" 76 c = 4096 77 dk_len = 16 78 raw = %w{ 56 fa 6a a7 55 48 09 9d 79 cc 37 d7 f0 34 25 e0 c3 } 80 expected = [raw.join('')].pack('H*') 81 value = OpenSSL::PKCS5.pbkdf2_hmac_sha1(p, s, c, dk_len) 82 assert_equal(expected, value) 83 end 84 85 def test_pbkdf2_hmac_sha256_c_20000_len_32 86 #unfortunately no official test vectors available yet for SHA-2 87 p ="password" 88 s = OpenSSL::Random.random_bytes(16) 89 c = 20000 90 dk_len = 32 91 digest = OpenSSL::Digest::SHA256.new 92 value1 = OpenSSL::PKCS5.pbkdf2_hmac(p, s, c, dk_len, digest) 93 value2 = OpenSSL::PKCS5.pbkdf2_hmac(p, s, c, dk_len, digest) 94 assert_equal(value1, value2) 95 end if OpenSSL::PKCS5.respond_to?(:pbkdf2_hmac) 96 97end if defined?(OpenSSL) 98