Commit 7892eae5 authored by bernd's avatar bernd
Browse files

checksums now covers all header bits, including flow control (ignored for cookie)

parent b8731073
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -48,7 +48,7 @@ UValue test# 0 to test#
: c:add-tag ( -- ) +addme
    net2o-code
      expect-reply
      s" DHT test" $, type cr get-ip
      log s" DHT test" $, type cr endwith get-ip
      pkc keysize 2* $, dht-id
      forever "test:tag" pkc keysize 2* gen-tag-del $, dht-tags-
      forever "test:tag" pkc keysize 2* gen-tag $, dht-tags+
+4 −4
Original line number Diff line number Diff line
@@ -44,16 +44,16 @@ object class
    \G Encrypt message in buffer addr u
    umethod c:decrypt ( addr u -- )
    \G Decrypt message in buffer addr u
    umethod c:encrypt+auth ( addr u -- )
    umethod c:encrypt+auth ( addr u tag -- )
    \G Encrypt message in buffer addr u
    umethod c:decrypt+auth ( addr u -- flag )
    umethod c:decrypt+auth ( addr u tag -- flag )
    \G Decrypt message in buffer addr u
    umethod c:hash ( addr u -- )
    \G Hash message in buffer addr u
    umethod c:prng ( addr u -- )
    \G Fill buffer addr u with PRNG sequence
    umethod c:checksum ( -- xd )
    umethod c:checksum ( tag -- xd )
    \G compute a 128 bit checksum
    umethod c:cookie ( -- x )
    \G compute a different checksum
    \G compute a different 64 bit checksum
end-class crypto
+15 −10
Original line number Diff line number Diff line
@@ -54,7 +54,7 @@ end-c-library

25 8 * Constant keccak#
128 Constant keccak#max
24 Constant keccak#cks
128 Constant keccak#cks

UValue @keccak

@@ -114,21 +114,23 @@ keccak-init
\    BEGIN  @keccak KeccakF  2dup keccak#max umin tuck -keccak
\    /string dup 0= UNTIL  2drop
; to c:decrypt ( addr u -- )
:noname ( addr u -- )
:noname ( addr u tag -- )
    \G Encrypt message in buffer addr u with auth
\    BEGIN  @keccak KeccakF  2dup keccak#max umin tuck +keccak
\    /string dup 0= UNTIL  drop
    @keccak -rot KeccakEncryptLoop
    { tag } @keccak -rot KeccakEncryptLoop
    @keccak KeccakF
    >r keccak-checksums keccak#cks keccak> keccak-checksums 128@ r> 128!
    >r keccak-checksums keccak#cks keccak>
    keccak-checksums tag 7 and 4 lshift + 128@ r> 128!
; to c:encrypt+auth ( addr u -- )
:noname ( addr u -- )
:noname ( addr u tag -- )
    \G Decrypt message in buffer addr u, with auth check
\    BEGIN  @keccak KeccakF  2dup keccak#max umin tuck -keccak
\    /string dup 0= UNTIL  drop
    @keccak -rot KeccakDecryptLoop
    { tag } @keccak -rot KeccakDecryptLoop
    @keccak KeccakF
    128@ keccak-checksums keccak#cks keccak> keccak-checksums 128@ 128=
    128@ keccak-checksums keccak#cks keccak>
    keccak-checksums tag 7 and 4 lshift + 128@ 128=
; to c:decrypt+auth ( addr u -- flag )
:noname ( addr u -- )
\G Hash message in buffer addr u
@@ -145,10 +147,13 @@ keccak-init
; to c:prng
\G Fill buffer addr u with PRNG sequence
:noname @keccak KeccakF
    keccak-checksums keccak#cks keccak> keccak-checksums 128@ ; to c:checksum ( -- xd )
    keccak-checksums keccak#cks keccak>
    7 and 4 lshift keccak-checksums + 128@ ; to c:checksum ( tag -- xd )
\G compute a 128 bit checksum
:noname keccak-checksums keccak#cks keccak> keccak-checksums $10 + 64@ ; to c:cookie ( -- x )
\G obtain a different 64 bit checksum part
:noname keccak-checksums keccak#cks keccak>
    64#0 keccak-checksums keccak#cks bounds ?DO
	I 64@ 64xor  8 +LOOP ; to c:cookie ( -- x )
\G obtain a 64 bit checksum

keccak ' new static-a with-allocater Constant keccak-o

+5 −1
Original line number Diff line number Diff line
@@ -557,6 +557,7 @@ $20 net2o: tmpnest ( $:string -- ) \ nested (temporary encrypted) command
    ELSE  ." don't store key: o=" o hex. .nnb F cr  THEN ;

+net2o: map-request ( addrs ucode udata -- ) \ request mapping
    knocked? 0= IF   64drop 64drop 64drop un-cmd  EXIT  THEN
    2*64>n
    nest[
    ?new-mykey ticker 64@ lit, set-cookie
@@ -636,15 +637,18 @@ net2o-base
    update-key all-ivs ;

+net2o: gen-reply ( -- ) \ generate a key request reply reply
    own-crypt? 0= ?EXIT
    own-crypt? knocked? or 0= ?EXIT
    [: crypt( ." Reply key: " tmpkey@ .nnb F cr )
      reply-key, cookie+request time-offset! ]tmpnest
      push-cmd ;]  IS expect-reply? ;
+net2o: gen-punch-reply ( -- )  o? \ generate a key request reply reply
    knocked? 0= ?EXIT
    [: crypt( ." Reply key: " tmpkey@ .nnb F cr )
      reply-key, gen-punchload gen-punch time-offset! ]tmpnest
      push-cmd ;]  IS expect-reply? ;

+net2o: knock ( $:challenge -- ) $> net2o:knock knock-val and validated or! ;

gen-table $freeze

\ everything that follows here can assume to have a connection context
+16 −6
Original line number Diff line number Diff line
@@ -127,10 +127,10 @@ User last-ivskey
    2>r over >r  rng@ rng@ r> 128! 2r> crypt-key-init ;

: encrypt$ ( addr u1 key u2 -- )
    crypt-key-setup  2 64s - c:encrypt+auth ;
    crypt-key-setup  2 64s - 0 c:encrypt+auth ;

: decrypt$ ( addr u1 key u2 -- addr' u' flag )
    crypt-key-init 2 64s - 2dup c:decrypt+auth ;
    crypt-key-init 2 64s - 2dup 0 c:decrypt+auth ;

\ passphraese encryption needs to diffuse a lot after mergin in the salt

@@ -145,10 +145,10 @@ User last-ivskey
    drop c@ $F and 2* $100 swap lshift ;

: encrypt-pw$ ( addr u1 key u2 n -- )
    crypt-pw-setup  pw-diffuse  2 64s - c:encrypt+auth ;
    crypt-pw-setup  pw-diffuse  2 64s - 0 c:encrypt+auth ;

: decrypt-pw$ ( addr u1 key u2 -- addr' u' flag )  2over pw-setup >r
    crypt-key-init   r> pw-diffuse  2 64s - 2dup c:decrypt+auth ;
    crypt-key-init   r> pw-diffuse  2 64s - 2dup 0 c:decrypt+auth ;

\ encrypt with own key

@@ -160,10 +160,12 @@ User last-ivskey
    $>align oldmykey state# decrypt$ +enc ;

: outbuf-encrypt ( map -- ) +calc
    crypt-buf-init outbuf packet-data +cryptsu c:encrypt+auth +enc ;
    crypt-buf-init outbuf packet-data +cryptsu
    outbuf 1+ c@ c:encrypt+auth +enc ;

: inbuf-decrypt ( map -- flag2 ) +calc
    crypt-buf-init inbuf packet-data +cryptsu c:decrypt+auth +enc ;
    crypt-buf-init inbuf packet-data +cryptsu
    inbuf 1+ c@ c:decrypt+auth +enc ;

\ IVS

@@ -322,6 +324,14 @@ Defer search-key \ search if that is one of our pubkeys
    THEN
    2drop ;

\ port knocking

Variable knocks

: net2o:knock ( addr u -- flag )
    0 -rot knocks [: 2over 2swap decrypt$ nip nip -rot 2>r or 2r>
    ;] $[]map 2drop ;

0 [IF]
Local Variables:
forth-local-words:
Loading