needs tests/harness num/chacha fs/sh testbegin create PLAINTEXT 400 allot create CIPHERTEXT 400 allot \test quarter round on simple code rowqround ( a -- ) 0 1 2 3 qround@, drop, exit, create TST_VECTOR $11111111 , $01020304 , $9b8d6f43 , $01234567 , create expected $ea2a92f4 , $cb1cf8ce , $4581472e , $5881c4bb , TST_VECTOR rowqround TST_VECTOR expected 4 []= #true \test newChaCha20 newChaCha20 const CHACHA CHACHA 3 and 0 #eq \ address is aligned to 4 CHACHA cntlo 1 #eq \ counter is set to 1 CHACHA rounds 20 #eq \ rounds is set to 20 CHACHA remaining 0 #eq \ there's no remaining byte yet CHACHA void? #true \ void? is set \test ChaCha$ create NONCE map< c, $00 $00 $00 $09 $00 $00 $00 $4a $00 $00 $00 $00 create EXP_NONCE map< , $09000000 $4a000000 $00000000 create KEY map< c, $00 $01 $02 $03 $04 $05 $06 $07 $08 $09 $0a $0b $0c $0d $0e $0f \ $10 $11 $12 $13 $14 $15 $16 $17 $18 $19 $1a $1b $1c $1d $1e $1f create EXP_KEY map< , $03020100 $07060504 $0b0a0908 $0f0e0d0c \ $13121110 $17161514 $1b1a1918 $1f1e1d1c create COPY_NONCE 12 allot create COPY_KEY 32 allot NONCE COPY_NONCE 12 cmove KEY COPY_KEY 32 cmove NONCE KEY CHACHA ChaCha$ create zeroes 16 4* allot0 NONCE zeroes 3 []= #true KEY zeroes 8 []= #true EXP_NONCE CHACHA nonce 3 []= #true EXP_KEY CHACHA bstate 16 + 8 []= #true COPY_KEY KEY 32 cmove \ note: CHACHA has been set with the correct keys and nonce before \test loadstate CHACHA loadstate drop create PREV_STATE map< , $61707865 $3320646e $79622d32 $6b206574 \ $03020100 $07060504 $0b0a0908 $0f0e0d0c \ $13121110 $17161514 $1b1a1918 $1f1e1d1c \ $00000001 $09000000 $4a000000 $00000000 PREV_STATE CHACHA wstate 16 []= #true \test round create THEN_STATE map< , $837778ab $e238d763 $a67ae21e $5950bb2f \ $c4f2d0c7 $fc62bb2f $8fa018fc $3f5ec7b7 \ $335271c2 $f29489f3 $eabda8fc $82e46ebd \ $d19c12b4 $b04e16de $9e83d0cb $4e3c50a2 CHACHA round THEN_STATE CHACHA wstate 16 []= #true \test ChaCha20 block function \ We directly store the result of the block function in little-endian create THEN_STATE map< le, $e4e7f110 $15593bd1 $1fdd0f50 $c47120a3 \ $c7f4d1c7 $0368c033 $9aaa2204 $4e6cd4c3 \ $466482d2 $09aa9f07 $05d7c214 $a2028bd9 \ $d19c12b5 $b94e16de $e883d0cb $4e3c50a2 CHACHA loadstate blockfn THEN_STATE CHACHA wstate 16 []= #true \test ChaCha discard CHACHA discard \ check the inner state of CHACHA zeroes CHACHA wstate 16 []= #true zeroes CHACHA bstate 4 4* + 8 []= #true zeroes CHACHA nonce 3 []= #true CHACHA cntlo 1 #eq CHACHA remaining 0 #eq \test ChaCha20 cipher : readallclose<< ( dst "name" -- len ) -1 word openpath r! read r> close ; create NONCE map< c, $00 $00 $00 $00 $00 $00 $00 $4a $00 $00 $00 $00 PLAINTEXT readallclose<< data/tests/chacha/plain1.txt const TEXT_LEN CIPHERTEXT readallclose<< data/tests/chacha/cipher1.bin TEXT_LEN #eq newChaCha20 const CHACHA NONCE KEY CHACHA ChaCha$ PLAINTEXT TEXT_LEN CHACHA apply drop PLAINTEXT CIPHERTEXT TEXT_LEN c[]= #true create KEY map< c, $00 $00 $00 $00 $00 $00 $00 $00 $00 $00 $00 $00 $00 $00 $00 $00 \ $00 $00 $00 $00 $00 $00 $00 $00 $00 $00 $00 $00 $00 $00 $00 $01 create NONCE map< c, $00 $00 $00 $00 $00 $00 $00 $00 $00 $00 $00 $02 PLAINTEXT readallclose<< data/tests/chacha/plain2.txt const TEXT_LEN CIPHERTEXT readallclose<< data/tests/chacha/cipher2.bin TEXT_LEN #eq CHACHA discard 0 CHACHA to void? NONCE KEY CHACHA ChaCha$ PLAINTEXT TEXT_LEN CHACHA apply drop PLAINTEXT CIPHERTEXT TEXT_LEN c[]= #true CHACHA discard \test ChaCha streaming capabilities 0 CHACHA to void? NONCE KEY CHACHA ChaCha$ PLAINTEXT 308 CHACHA apply drop CHACHA discard NONCE KEY CHACHA ChaCha$ CIPHERTEXT 70 CHACHA apply 14 CHACHA apply 82 CHACHA apply 23 CHACHA apply 53 CHACHA apply 66 CHACHA apply drop \ assert that we've read the same quantity of data 70 14 + 82 + 23 + 53 + 66 + 308 #eq PLAINTEXT CIPHERTEXT 308 c[]= #true \test XChaCha construction create KEY map< c, $00 $01 $02 $03 $04 $05 $06 $07 $08 $09 $0a $0b $0c $0d $0e $0f \ $10 $11 $12 $13 $14 $15 $16 $17 $18 $19 $1a $1b $1c $1d $1e $1f create NONCE map< c, $00 $00 $00 $09 $00 $00 $00 $4a $00 $00 $00 $00 $31 $41 $59 $27 \ $40 $41 $42 $43 $44 $45 $46 $47 create EXP_STATE map< , $61707865 $3320646e $79622d32 $6b206574 \ $423b4182 $fe7bb227 $50420ed3 $737d878a \ $d5e4f9a0 $53a8748a $13c42ec1 $dcecd326 \ $00000001 $00000000 $43424140 $47464544 newChaCha20 const CHACHA XChaCha CHACHA to variant NONCE KEY CHACHA ChaCha$ EXP_STATE CHACHA bstate 16 []= #true KEY zeroes 8 []= #true NONCE zeroes 6 []= #true \test XChaCha20 cipher create KEY map< c, $80 $81 $82 $83 $84 $85 $86 $87 $88 $89 $8a $8b $8c $8d $8e $8f \ $90 $91 $92 $93 $94 $95 $96 $97 $98 $99 $9a $9b $9c $9d $9e $9f create NONCE map< c, $40 $41 $42 $43 $44 $45 $46 $47 $48 $49 $4a $4b $4c $4d $4e $4f \ $50 $51 $52 $53 $54 $55 $56 $58 PLAINTEXT readallclose<< data/tests/chacha/plainx.txt const TEXT_LEN CIPHERTEXT readallclose<< data/tests/chacha/cipherx.bin TEXT_LEN #eq NONCE KEY CHACHA ChaCha$ PLAINTEXT TEXT_LEN CHACHA apply drop PLAINTEXT CIPHERTEXT TEXT_LEN c[]= #true \test 64-bit counter -1 CHACHA to cntlo CHACHA counter1+ drop CHACHA cntlo 0 #eq CHACHA cnthi 1 #eq testend