]> gitweb.factorcode.org Git - factor.git/commitdiff
Fix RSA
authorDoug Coleman <erg@g.local>
Sun, 7 Oct 2007 04:01:26 +0000 (23:01 -0500)
committerDoug Coleman <erg@g.local>
Sun, 7 Oct 2007 04:01:26 +0000 (23:01 -0500)
Fix crypto unit tests

extra/crypto/rc4.factor [deleted file]
extra/crypto/rc4/rc4.factor [new file with mode: 0644]
extra/crypto/rsa.factor [deleted file]
extra/crypto/rsa/rsa-tests.factor
extra/crypto/rsa/rsa.factor
extra/crypto/test/rsa.factor [deleted file]
extra/crypto/test/xor.factor [deleted file]
extra/crypto/xor.factor [deleted file]
extra/crypto/xor/xor-tests.factor [new file with mode: 0644]
extra/crypto/xor/xor.factor [new file with mode: 0644]

diff --git a/extra/crypto/rc4.factor b/extra/crypto/rc4.factor
deleted file mode 100644 (file)
index 24f5231..0000000
+++ /dev/null
@@ -1,36 +0,0 @@
-USING: kernel math sequences namespaces math-contrib ;
-IN: crypto-internals
-
-! http://en.wikipedia.org/wiki/RC4_%28cipher%29
-
-SYMBOL: i
-SYMBOL: j
-SYMBOL: s
-SYMBOL: key
-SYMBOL: l
-
-
-! key scheduling algorithm, initialize s
-: ksa ( -- )
-    256 [ ] map s set
-    0 j set
-    256 [
-        dup s get nth j get + over l get mod key get nth + 255 bitand j set
-        dup j get s get exchange
-    ] repeat ;
-
-: generate ( -- n )
-    i get 1+ 255 bitand i set
-    j get i get s get nth + 255 bitand j set
-    i get j get s get exchange
-    i get s get nth j get s get nth + 255 bitand s get nth ;
-
-IN: crypto
-
-: rc4 ( key -- )
-    [ key set ] keep
-    length l set
-    ksa
-    0 i set
-    0 j set ;
-
diff --git a/extra/crypto/rc4/rc4.factor b/extra/crypto/rc4/rc4.factor
new file mode 100644 (file)
index 0000000..b730c4b
--- /dev/null
@@ -0,0 +1,39 @@
+USING: kernel math sequences namespaces ;
+IN: crypto.rc4
+
+! http://en.wikipedia.org/wiki/RC4_%28cipher%29
+
+<PRIVATE
+
+SYMBOL: i
+SYMBOL: j
+SYMBOL: s
+SYMBOL: key
+SYMBOL: l
+
+! key scheduling algorithm, initialize s
+: ksa ( -- )
+    256 [ ] map s set
+    0 j set
+    256 [
+        dup s get nth j get + over l get mod key get nth + 255 bitand j set
+        dup j get s get exchange drop
+    ] each ;
+
+: generate ( -- n )
+    i get 1+ 255 bitand i set
+    j get i get s get nth + 255 bitand j set
+    i get j get s get exchange
+    i get s get nth j get s get nth + 255 bitand s get nth ;
+
+PRIVATE>
+
+: rc4 ( key -- )
+    [
+        [ key set ] keep
+        length l set
+        ksa
+        0 i set
+        0 j set
+    ] with-scope ;
+
diff --git a/extra/crypto/rsa.factor b/extra/crypto/rsa.factor
deleted file mode 100644 (file)
index e082e43..0000000
+++ /dev/null
@@ -1,26 +0,0 @@
-USING: kernel math namespaces math-contrib errors ;
-
-IN: crypto
-SYMBOL: d
-SYMBOL: p
-SYMBOL: q
-SYMBOL: n
-SYMBOL: m
-SYMBOL: ee
-
-! e = public key, d = private key, n = public modulus
-TUPLE: rsa e d n ;
-
-! n bits
-: generate-rsa-keypair ( bitlen -- <rsa> )
-    [
-        2 /i generate-two-unique-primes [ q set p set ] 2keep [ * n set ] 2keep
-        [ 1- ] 2apply * m set
-        65537 ee set
-        m get ee get mod-inv m get + d set
-        ee get d get n get <rsa>
-    ] with-scope ;
-
-: rsa-encrypt ( message rsa -- encrypted ) [ rsa-e ] keep rsa-n ^mod ;
-: rsa-decrypt ( encrypted rsa -- message ) [ rsa-d ] keep rsa-n ^mod ;
-
index 10ff28a8b82b46e4811f280814b76f66c518a6a0..7de6bed76ffe83ca037a80c23b626b4517cb70de 100644 (file)
@@ -3,5 +3,5 @@ USING: kernel math namespaces crypto.rsa tools.test ;
 [ 123456789 ] [ 128 generate-rsa-keypair 123456789 over rsa-encrypt swap rsa-decrypt ] unit-test
 [ 123456789 ] [ 129 generate-rsa-keypair 123456789 over rsa-encrypt swap rsa-decrypt ] unit-test
 [ 123456789 ] [ 130 generate-rsa-keypair 123456789 over rsa-encrypt swap rsa-decrypt ] unit-test
-[ 123 ] [ 17 2753 3233 <rsa> 123 over rsa-encrypt swap rsa-decrypt ] unit-test
+[ 123 ] [ 3233 2753 17 <rsa> 123 over rsa-encrypt swap rsa-decrypt ] unit-test
 
index ad5822b24c779948b7b0e4910ac2755be10db69b..ffb2a64b763a4da4a46b6ce41a9e7f96a0528032 100644 (file)
@@ -2,28 +2,44 @@ USING: math.miller-rabin kernel math math.functions namespaces
 sequences ;
 IN: crypto.rsa
 
-SYMBOL: d
-SYMBOL: p
-SYMBOL: q
-SYMBOL: n
-SYMBOL: m
-SYMBOL: ee
+! The private key is the only secret.
 
-! e = public key, d = private key, n = public modulus
-TUPLE: rsa e d n ;
+! p,q are two random primes of numbits/2
+! phi = (p-1)(q-1)
+! modulus = p*q
+! public = 65537
+! private = public modinv phi
+
+TUPLE: rsa modulus private-key public-key ;
 
 C: <rsa> rsa
 
-! n bits
+<PRIVATE
+
+: public-key 65537 ; inline
+
+: rsa-primes ( numbits -- p q )
+    2/ 2 unique-primes first2 ;
+
+: modulus-phi ( numbits -- n phi ) 
+    #! Loop until phi is not divisible by the public key.
+    dup rsa-primes [ * ] 2keep
+    [ 1- ] 2apply *
+    dup public-key gcd nip 1 = [
+        rot drop
+    ] [
+        2drop modulus-phi
+    ] if ;
+
+PRIVATE>
+
 : generate-rsa-keypair ( numbits -- <rsa> )
-    [
-        2 /i 2 unique-primes first2 [ q set p set ] 2keep [ * n set ] 2keep
-        [ 1- ] 2apply * m set
-        65537 ee set
-        m get ee get mod-inv m get + d set
-        ee get d get n get <rsa>
-    ] with-scope ;
+    modulus-phi
+    public-key over mod-inv +
+    public-key <rsa> ;
 
-: rsa-encrypt ( message rsa -- encrypted ) [ rsa-e ] keep rsa-n ^mod ;
-: rsa-decrypt ( encrypted rsa -- message ) [ rsa-d ] keep rsa-n ^mod ;
+: rsa-encrypt ( message rsa -- encrypted )
+    [ rsa-public-key ] keep rsa-modulus ^mod ;
 
+: rsa-decrypt ( encrypted rsa -- message )
+    [ rsa-private-key ] keep rsa-modulus ^mod ;
\ No newline at end of file
diff --git a/extra/crypto/test/rsa.factor b/extra/crypto/test/rsa.factor
deleted file mode 100644 (file)
index cddad58..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-USING: kernel math test namespaces crypto ;
-
-[ 123456789 ] [ 128 generate-rsa-keypair 123456789 over rsa-encrypt swap rsa-decrypt ] unit-test
-[ 123456789 ] [ 129 generate-rsa-keypair 123456789 over rsa-encrypt swap rsa-decrypt ] unit-test
-[ 123456789 ] [ 130 generate-rsa-keypair 123456789 over rsa-encrypt swap rsa-decrypt ] unit-test
-[ 123 ] [ 17 2753 3233 <rsa> 123 over rsa-encrypt swap rsa-decrypt ] unit-test
-
diff --git a/extra/crypto/test/xor.factor b/extra/crypto/test/xor.factor
deleted file mode 100644 (file)
index 2a77cf0..0000000
+++ /dev/null
@@ -1,23 +0,0 @@
-USING: crypto errors kernel test strings ;
-
-! No key
-[ T{ no-xor-key f } ] [ [ "" dup xor-crypt ] catch ] unit-test
-[ T{ no-xor-key f } ] [ [ { } dup xor-crypt ] catch ] unit-test
-[ T{ no-xor-key f } ] [ [ V{ } dup xor-crypt ] catch ] unit-test
-[ T{ no-xor-key f } ] [ [ "" "asdf" dupd xor-crypt xor-crypt ] catch ] unit-test
-
-! a xor a = 0
-[ { 0 0 0 0 0 0 0 } ] [ "abcdefg" dup xor-crypt ] unit-test
-
-[ { 15 15 15 15 } ] [ { 10 10 10 10 } { 5 5 5 5 } xor-crypt ] unit-test
-
-[ "asdf" ] [ "key" "asdf" dupd xor-crypt xor-crypt >string ] unit-test
-[ "" ] [ "key" "" xor-crypt >string ] unit-test
-[ "a longer message...!" ] [
-    "."
-    "a longer message...!" dupd xor-crypt xor-crypt >string
-] unit-test
-[ "a longer message...!" ] [
-    "a very long key, longer than the message even."
-    "a longer message...!" dupd xor-crypt xor-crypt >string
-] unit-test
diff --git a/extra/crypto/xor.factor b/extra/crypto/xor.factor
deleted file mode 100644 (file)
index a2b3161..0000000
+++ /dev/null
@@ -1,9 +0,0 @@
-USING: errors kernel math sequences ;
-IN: crypto
-
-TUPLE: no-xor-key ;
-
-: xor-crypt ( key seq -- seq )
-    over empty? [ <no-xor-key> throw ] when
-    [ length ] keep
-    [ >r over mod-nth r> bitxor ] 2map nip ;
diff --git a/extra/crypto/xor/xor-tests.factor b/extra/crypto/xor/xor-tests.factor
new file mode 100644 (file)
index 0000000..a0b764c
--- /dev/null
@@ -0,0 +1,24 @@
+USING: continuations crypto.xor kernel strings tools.test ;
+IN: temporary
+
+! No key
+[ T{ no-xor-key f } ] [ [ "" dup xor-crypt ] catch ] unit-test
+[ T{ no-xor-key f } ] [ [ { } dup xor-crypt ] catch ] unit-test
+[ T{ no-xor-key f } ] [ [ V{ } dup xor-crypt ] catch ] unit-test
+[ T{ no-xor-key f } ] [ [ "" "asdf" dupd xor-crypt xor-crypt ] catch ] unit-test
+
+! a xor a = 0
+[ "\0\0\0\0\0\0\0" ] [ "abcdefg" dup xor-crypt ] unit-test
+
+[ { 15 15 15 15 } ] [ { 10 10 10 10 } { 5 5 5 5 } xor-crypt ] unit-test
+
+[ "asdf" ] [ "key" "asdf" dupd xor-crypt xor-crypt >string ] unit-test
+[ "" ] [ "key" "" xor-crypt >string ] unit-test
+[ "a longer message...!" ] [
+    "."
+    "a longer message...!" dupd xor-crypt xor-crypt >string
+] unit-test
+[ "a longer message...!" ] [
+    "a very long key, longer than the message even."
+    "a longer message...!" dupd xor-crypt xor-crypt >string
+] unit-test
diff --git a/extra/crypto/xor/xor.factor b/extra/crypto/xor/xor.factor
new file mode 100644 (file)
index 0000000..0713e19
--- /dev/null
@@ -0,0 +1,8 @@
+USING: crypto.common kernel math sequences ;
+IN: crypto.xor
+
+TUPLE: no-xor-key ;
+
+: xor-crypt ( key seq -- seq )
+    over empty? [ no-xor-key construct-empty throw ] when
+    dup length rot [ mod-nth bitxor ] curry 2map ;