Skip to content

Commit

Permalink
Fix test_pkey_dsa.rb in FIPS.
Browse files Browse the repository at this point in the history
  • Loading branch information
junaruga committed Mar 12, 2024
1 parent 1e8e246 commit 07c83ed
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Rake::TestTask.new(:test_fips_internal) do |t|
'test/openssl/test_fips.rb',
'test/openssl/test_pkey.rb',
'test/openssl/test_pkey_dh.rb',
'test/openssl/test_pkey_dsa.rb',
'test/openssl/test_pkey_ec.rb',
]
t.warning = true
Expand Down
15 changes: 15 additions & 0 deletions test/openssl/fixtures/pkey/dsa2048.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-----BEGIN PRIVATE KEY-----
MIICXAIBADCCAjUGByqGSM44BAEwggIoAoIBAQDfuAlOk3spN7LjkZ0wwyBzejom
sDWJRv+jH4Ycpoq2NwWufAdSGv9DFgEPY5ENJS3fC67tY/0a1u2MUiQZqMIcbtvA
HTaaU9yD4W5oFeIknRIicY/xIw2uPW5HyeesIu56ESEjRZjiwCPQOmuStWUbzjyi
NSz1e/3XiSB6syJiLcY5QdR/fsUjn0NL/1KNDKUtvZD45ME7p3JBRCoSkTrIc13Z
IWMpJV94v8VMiRDS52TtPUDKPns41gl8pfixVFZ++KBRLmf23wRE7CNQqwiwE8Pf
ctqgO/tTAGLBSSJPZMhYWgrqP/U+xjd3gWbQ7b/UCZYt+KbBD9EeGsGHibDdAh0A
/XShdxKBFgLvURCX0mesjv9cY5J5rHu/zsJinQKCAQBuqt02aEn5fs8tqjhBtfB/
Pu+VxWG+LsizatE2+K07vCvH8SMc6tVERhnX9pvw8FW4WfsLxBu+9JWaBDCvzp2p
A+3Uz52rowDkSBqnpxr7ylUVis7pf0Zvh/Jjwx2rS46hVIJS6ygz8b3B/kXhT19v
1Hdw1vgNjEM6IusvfOs+djqGgd0rAEfwIiYeZAIrsPDekB5rCQqbTvJ+8TuWYb79
ROT9bK3oIBIjWPUBf7ZDgXdyuRNM0rWMA2JzTlPeAuriD58oteGw0XlebO6Wrb/7
hsBKHeTDMSaVmONxyTIws2UY9I2NbqCw5/v33W2R3F6X8Wg1gnU0Gj4ySOnD7oKr
BB4CHDQHDufeR9kwhCiPUjlwCaoplvYmEWe0PBRlxmk=
-----END PRIVATE KEY-----
34 changes: 21 additions & 13 deletions test/openssl/test_pkey_dsa.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ def test_new_break
def test_generate
# DSA.generate used to call DSA_generate_parameters_ex(), which adjusts the
# size of q according to the size of p
key1024 = OpenSSL::PKey::DSA.generate(1024)
assert_predicate key1024, :private?
assert_equal 1024, key1024.p.num_bits
assert_equal 160, key1024.q.num_bits

key2048 = OpenSSL::PKey::DSA.generate(2048)
assert_equal 2048, key2048.p.num_bits
assert_equal 256, key2048.q.num_bits
Expand All @@ -47,28 +42,41 @@ def test_generate
end
end

def test_generate_on_non_fips
# DSA with 1024 bits is invalid on FIPS 186-4.
# https://github.com/openssl/openssl/commit/49ed5ba8f62875074f04417189147fd3dda072ab
omit_on_fips

key1024 = OpenSSL::PKey::DSA.generate(1024)
assert_predicate key1024, :private?
assert_equal 1024, key1024.p.num_bits
assert_equal 160, key1024.q.num_bits
end

def test_sign_verify
dsa512 = Fixtures.pkey("dsa512")
# The DSA valid size is 2048 or 3072 on FIPS.
# https://github.com/openssl/openssl/blob/7649b5548e5c0352b91d9d3ed695e42a2ac1e99c/providers/common/securitycheck.c#L185-L188
dsa = Fixtures.pkey("dsa2048")
data = "Sign me!"
if defined?(OpenSSL::Digest::DSS1)
signature = dsa512.sign(OpenSSL::Digest.new('DSS1'), data)
assert_equal true, dsa512.verify(OpenSSL::Digest.new('DSS1'), signature, data)
signature = dsa.sign(OpenSSL::Digest.new('DSS1'), data)
assert_equal true, dsa.verify(OpenSSL::Digest.new('DSS1'), signature, data)
end

signature = dsa512.sign("SHA256", data)
assert_equal true, dsa512.verify("SHA256", signature, data)
signature = dsa.sign("SHA256", data)
assert_equal true, dsa.verify("SHA256", signature, data)

signature0 = (<<~'end;').unpack1("m")
MCwCFH5h40plgU5Fh0Z4wvEEpz0eE9SnAhRPbkRB8ggsN/vsSEYMXvJwjGg/
6g==
end;
assert_equal true, dsa512.verify("SHA256", signature0, data)
assert_equal true, dsa.verify("SHA256", signature0, data)
signature1 = signature0.succ
assert_equal false, dsa512.verify("SHA256", signature1, data)
assert_equal false, dsa.verify("SHA256", signature1, data)
end

def test_sign_verify_raw
key = Fixtures.pkey("dsa512")
key = Fixtures.pkey("dsa2048")
data = 'Sign me!'
digest = OpenSSL::Digest.digest('SHA1', data)

Expand Down

0 comments on commit 07c83ed

Please sign in to comment.