@@ -120,14 +120,9 @@ def test_ECPrivateKey
120120 asn1 = OpenSSL ::ASN1 ::Sequence ( [
121121 OpenSSL ::ASN1 ::Integer ( 1 ) ,
122122 OpenSSL ::ASN1 ::OctetString ( p256 . private_key . to_s ( 2 ) ) ,
123- OpenSSL ::ASN1 ::ASN1Data . new (
124- [ OpenSSL ::ASN1 ::ObjectId ( "prime256v1" ) ] ,
125- 0 , :CONTEXT_SPECIFIC
126- ) ,
127- OpenSSL ::ASN1 ::ASN1Data . new (
128- [ OpenSSL ::ASN1 ::BitString ( p256 . public_key . to_bn . to_s ( 2 ) ) ] ,
129- 1 , :CONTEXT_SPECIFIC
130- )
123+ OpenSSL ::ASN1 ::ObjectId ( "prime256v1" , 0 , :EXPLICIT ) ,
124+ OpenSSL ::ASN1 ::BitString ( p256 . public_key . to_octet_string ( :uncompressed ) ,
125+ 1 , :EXPLICIT )
131126 ] )
132127 key = OpenSSL ::PKey ::EC . new ( asn1 . to_der )
133128 assert_predicate key , :private?
@@ -181,7 +176,7 @@ def test_PUBKEY
181176 OpenSSL ::ASN1 ::ObjectId ( "prime256v1" )
182177 ] ) ,
183178 OpenSSL ::ASN1 ::BitString (
184- p256 . public_key . to_bn . to_s ( 2 )
179+ p256 . public_key . to_octet_string ( :uncompressed )
185180 )
186181 ] )
187182 key = OpenSSL ::PKey ::EC . new ( asn1 . to_der )
@@ -224,7 +219,8 @@ def test_ec_group
224219 assert_equal :uncompressed , group4 . point_conversion_form
225220 assert_equal group1 , group4
226221 assert_equal group1 . curve_name , group4 . curve_name
227- assert_equal group1 . generator . to_bn , group4 . generator . to_bn
222+ assert_equal group1 . generator . to_octet_string ( :uncompressed ) ,
223+ group4 . generator . to_octet_string ( :uncompressed )
228224 assert_equal group1 . order , group4 . order
229225 assert_equal group1 . cofactor , group4 . cofactor
230226 assert_equal group1 . seed , group4 . seed
@@ -239,34 +235,57 @@ def test_ec_point
239235 point2 = OpenSSL ::PKey ::EC ::Point . new ( group , point . to_bn )
240236 assert_equal point , point2
241237 assert_equal point . to_bn , point2 . to_bn
238+ assert_equal point . to_octet_string ( :uncompressed ) ,
239+ point2 . to_octet_string ( :uncompressed )
240+
241+ point3 = OpenSSL ::PKey ::EC ::Point . new ( group ,
242+ point . to_octet_string ( :uncompressed ) )
243+ assert_equal point , point3
244+ assert_equal point . to_bn , point3 . to_bn
245+ assert_equal point . to_octet_string ( :uncompressed ) ,
246+ point3 . to_octet_string ( :uncompressed )
247+
242248 point2 . invert!
243- assert_not_equal point . to_bn , point2 . to_bn
249+ point3 . invert!
250+ assert_not_equal point . to_octet_string ( :uncompressed ) ,
251+ point2 . to_octet_string ( :uncompressed )
252+ assert_equal point2 . to_octet_string ( :uncompressed ) ,
253+ point3 . to_octet_string ( :uncompressed )
244254
245255 begin
246256 group = OpenSSL ::PKey ::EC ::Group . new ( :GFp , 17 , 2 , 2 )
247257 group . point_conversion_form = :uncompressed
248- generator = OpenSSL ::PKey ::EC ::Point . new ( group , 0x040501 . to_bn )
258+ generator = OpenSSL ::PKey ::EC ::Point . new ( group , B ( %w{ 04 05 01 } ) )
249259 group . set_generator ( generator , 19 , 1 )
250- point = OpenSSL ::PKey ::EC ::Point . new ( group , 0x040603 . to_bn )
260+ point = OpenSSL ::PKey ::EC ::Point . new ( group , B ( %w{ 04 06 03 } ) )
251261 rescue OpenSSL ::PKey ::EC ::Group ::Error
252262 pend "Patched OpenSSL rejected curve" if /unsupported field/ =~ $!. message
253263 raise
254264 end
255265
266+ assert_equal 0x040603 . to_bn , point . to_bn
256267 assert_equal 0x040603 . to_bn , point . to_bn ( :uncompressed )
257268 assert_equal 0x0306 . to_bn , point . to_bn ( :compressed )
258269 assert_equal 0x070603 . to_bn , point . to_bn ( :hybrid )
259270
260- assert_equal 0x040603 . to_bn , point . to_bn
271+ group2 = group . dup ; group2 . point_conversion_form = :compressed
272+ point2 = OpenSSL ::PKey ::EC ::Point . new ( group2 , B ( %w{ 04 06 03 } ) )
273+ assert_equal 0x0306 . to_bn , point2 . to_bn
274+
275+ assert_equal B ( %w{ 04 06 03 } ) , point . to_octet_string ( :uncompressed )
276+ assert_equal B ( %w{ 03 06 } ) , point . to_octet_string ( :compressed )
277+ assert_equal B ( %w{ 07 06 03 } ) , point . to_octet_string ( :hybrid )
278+
261279 assert_equal true , point . on_curve?
262280 point . invert! # 8.5
263- assert_equal 0x04060E . to_bn , point . to_bn
281+ assert_equal B ( %w{ 04 06 0E } ) , point . to_octet_string ( :uncompressed )
264282 assert_equal true , point . on_curve?
265283
266284 assert_equal false , point . infinity?
267285 point . set_to_infinity!
268286 assert_equal true , point . infinity?
269287 assert_equal 0 . to_bn , point . to_bn
288+ assert_equal B ( %w{ 00 } ) , point . to_octet_string ( :uncompressed )
270289 assert_equal true , point . on_curve?
271290 end
272291
@@ -276,25 +295,25 @@ def test_ec_point_mul
276295 # generator is (5, 1)
277296 group = OpenSSL ::PKey ::EC ::Group . new ( :GFp , 17 , 2 , 2 )
278297 group . point_conversion_form = :uncompressed
279- gen = OpenSSL ::PKey ::EC ::Point . new ( group , OpenSSL :: BN . new ( "040501" , 16 ) )
298+ gen = OpenSSL ::PKey ::EC ::Point . new ( group , B ( %w{ 04 05 01 } ) )
280299 group . set_generator ( gen , 19 , 1 )
281300
282301 # 3 * (6, 3) = (16, 13)
283- point_a = OpenSSL ::PKey ::EC ::Point . new ( group , OpenSSL :: BN . new ( "040603" , 16 ) )
302+ point_a = OpenSSL ::PKey ::EC ::Point . new ( group , B ( %w{ 04 06 03 } ) )
284303 result_a1 = point_a . mul ( 3 )
285- assert_equal ( "04100D" , result_a1 . to_bn . to_s ( 16 ) )
304+ assert_equal B ( %w{ 04 10 0D } ) , result_a1 . to_octet_string ( :uncompressed )
286305 # 3 * (6, 3) + 3 * (5, 1) = (7, 6)
287306 result_a2 = point_a . mul ( 3 , 3 )
288- assert_equal ( "040706" , result_a2 . to_bn . to_s ( 16 ) )
307+ assert_equal B ( %w{ 04 07 06 } ) , result_a2 . to_octet_string ( :uncompressed )
289308 # 3 * point_a = 3 * (6, 3) = (16, 13)
290309 result_b1 = point_a . mul ( [ 3 ] , [ ] )
291- assert_equal ( "04100D" , result_b1 . to_bn . to_s ( 16 ) )
310+ assert_equal B ( %w{ 04 10 0D } ) , result_b1 . to_octet_string ( :uncompressed )
292311 # 3 * point_a + 2 * point_a = 3 * (6, 3) + 2 * (6, 3) = (7, 11)
293312 result_b1 = point_a . mul ( [ 3 , 2 ] , [ point_a ] )
294- assert_equal ( "04070B" , result_b1 . to_bn . to_s ( 16 ) )
313+ assert_equal B ( %w{ 04 07 0B } ) , result_b1 . to_octet_string ( :uncompressed )
295314 # 3 * point_a + 5 * point_a.group.generator = 3 * (6, 3) + 5 * (5, 1) = (13, 10)
296315 result_b1 = point_a . mul ( [ 3 ] , [ ] , 5 )
297- assert_equal ( "040D0A" , result_b1 . to_bn . to_s ( 16 ) )
316+ assert_equal B ( %w{ 04 0D 0A } ) , result_b1 . to_octet_string ( :uncompressed )
298317 rescue OpenSSL ::PKey ::EC ::Group ::Error
299318 # CentOS patches OpenSSL to reject curves defined over Fp where p < 256 bits
300319 raise if $!. message !~ /unsupported field/
@@ -316,6 +335,10 @@ def test_ec_point_mul
316335
317336 private
318337
338+ def B ( ary )
339+ [ Array ( ary ) . join ] . pack ( "H*" )
340+ end
341+
319342 def assert_same_ec ( expected , key )
320343 check_component ( expected , key , [ :group , :public_key , :private_key ] )
321344 end
0 commit comments