]> gitweb.factorcode.org Git - factor.git/blobdiff - extra/ecdsa/ecdsa.factor
factor: trim using lists
[factor.git] / extra / ecdsa / ecdsa.factor
index 78b528d4ad2404f34994ab57d1e7303548c8ba3f..c01550e62877fa507ddfb95d9204fa8029143551 100644 (file)
@@ -2,38 +2,28 @@
 ! See http://factorcode.org/license.txt for BSD license.
 
 USING: kernel accessors sequences sequences.private destructors math namespaces
-       locals openssl openssl.libcrypto byte-arrays bit-arrays.private
-       alien.c-types ;
+       openssl openssl.libcrypto byte-arrays bit-arrays.private
+       alien.c-types alien.destructors alien.data ;
 
 IN: ecdsa
 
 <PRIVATE
 
-TUPLE: openssl-object handle ;
+TUPLE: ec-key handle ;
 
-GENERIC# free-handle 1 ( obj handle -- obj )
-
-M: openssl-object dispose
-    dup [ free-handle f ] change-handle 2drop ;
-
-TUPLE: ec-key < openssl-object ;
-
-M: ec-key free-handle EC_KEY_free ;
+M: ec-key dispose
+    [ EC_KEY_free f ] change-handle drop ;
 
 : <ec-key> ( curve -- key )
     OBJ_sn2nid dup zero? [ "Unknown curve name" throw ] when
     EC_KEY_new_by_curve_name dup ssl-error ec-key boa ;
 
 : ec-key-handle ( -- handle )
-    ec-key get dup handle>> [ nip ] [ already-disposed ] if* ;
-
-TUPLE: openssl-bignum < openssl-object ;
-
-M: openssl-bignum free-handle BN_clear_free ;
+    ec-key get dup handle>> [ ] [ already-disposed ] ?if ;
 
-TUPLE: ec-point < openssl-object ;
+DESTRUCTOR: BN_clear_free
 
-M: ec-point free-handle EC_POINT_clear_free ;
+DESTRUCTOR: EC_POINT_clear_free
 
 PRIVATE>
 
@@ -45,30 +35,29 @@ PRIVATE>
 
 : set-private-key ( bin -- )
     ec-key-handle swap
-    dup length f BN_bin2bn dup ssl-error dup openssl-bignum boa
-    [ drop EC_KEY_set_private_key ssl-error ] with-disposal ;
+    dup length f BN_bin2bn dup ssl-error
+    [ &BN_clear_free EC_KEY_set_private_key ssl-error ] with-destructors ;
 
 :: set-public-key ( BIN -- )
     ec-key-handle :> KEY
     KEY EC_KEY_get0_group :> GROUP
-    GROUP EC_POINT_new dup ssl-error :> POINT
-    POINT ec-point boa
+    GROUP EC_POINT_new dup ssl-error
     [
-        drop
+        &EC_POINT_clear_free :> POINT
         GROUP POINT BIN dup length f EC_POINT_oct2point ssl-error
         KEY POINT EC_KEY_set_public_key ssl-error
-    ] with-disposal ;
+    ] with-destructors ;
 
 : get-private-key ( -- bin/f )
     ec-key-handle EC_KEY_get0_private_key
-    dup [ dup BN_num_bits bits>bytes <byte-array> tuck BN_bn2bin drop ] when ;
+    dup [ dup BN_num_bits bits>bytes <byte-array> [ BN_bn2bin drop ] keep ] when ;
 
 :: get-public-key ( -- bin/f )
     ec-key-handle :> KEY
-    KEY EC_KEY_get0_public_key dup 
+    KEY EC_KEY_get0_public_key dup
     [| PUB |
         KEY EC_KEY_get0_group :> GROUP
-        GROUP EC_GROUP_get_degree bits>bytes 1+ :> LEN
+        GROUP EC_GROUP_get_degree bits>bytes 1 + :> LEN
         LEN <byte-array> :> BIN
         GROUP PUB POINT_CONVERSION_COMPRESSED BIN LEN f
         EC_POINT_point2oct ssl-error
@@ -78,9 +67,9 @@ PRIVATE>
 :: ecdsa-sign ( DGST -- sig )
     ec-key-handle :> KEY
     KEY ECDSA_size dup ssl-error <byte-array> :> SIG
-    "uint" <c-object> :> LEN
+    0 uint <ref> :> LEN
     0 DGST dup length SIG LEN KEY ECDSA_sign ssl-error
-    LEN *uint SIG resize ;
+    LEN uint deref SIG resize ;
 
 : ecdsa-verify ( dgst sig -- ? )
-    ec-key-handle [ 0 -rot [ dup length ] bi@ ] dip ECDSA_verify 0 > ;
\ No newline at end of file
+    ec-key-handle [ 0 -rot [ dup length ] bi@ ] dip ECDSA_verify 0 > ;