+++ /dev/null
-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 ;
-
--- /dev/null
+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 ;
+
+++ /dev/null
-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 ;
-
[ 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
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
+++ /dev/null
-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
-
+++ /dev/null
-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
+++ /dev/null
-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 ;
--- /dev/null
+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
--- /dev/null
+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 ;