crypto-api.fs 1.92 KB
Newer Older
bernd's avatar
bernd committed
1 2 3
\ generic crypto api for net2o

require mini-oof2.fs
bernd's avatar
bernd committed
4
require user-object.fs
bernd's avatar
bernd committed
5

bernd's avatar
bernd committed
6 7 8 9 10 11 12 13 14 15 16 17
\ generic padding primitives

: >pad ( addr u u2 -- addr u2 ) \ u <= u2
    swap >r 2dup r@ safe/string r> fill ;
: >unpad ( addr u' -- addr u ) over + 1- c@ ;
: ?padded ( addr u' -- flag )
    2dup + 1- c@ dup >r safe/string r> skip nip 0= ;

: >padded { addr1 u1 addr2 u2 -- }
    addr1 addr2 u1 u2 umin move
    u1 u2 u< IF  addr2 u1 u2 >pad 2drop  THEN ;

bernd's avatar
bernd committed
18 19 20 21
\ For wurstkessel compatibility, the states are all 128 bytes
\ If the cryptosystem has more internal state, it may copy the key+iv there.
\ If it has less, it should use a suitable fraction of the key and the iv

bernd's avatar
bernd committed
22
User-o crypto-o
bernd's avatar
bernd committed
23

bernd's avatar
bernd committed
24
object class
bernd's avatar
bernd committed
25
    umethod c:init ( -- )
bernd's avatar
bernd committed
26
    \G initialize crypto function for a task
bernd's avatar
bernd committed
27 28
    umethod c:free ( -- )
    \G free crypto function for a task
bernd's avatar
bernd committed
29 30
    umethod c:0key ( -- )
    \G set zero key
bernd's avatar
bernd committed
31
    umethod c:key! ( addr -- )
bernd's avatar
bernd committed
32
    \G use addr as key storage
bernd's avatar
bernd committed
33
    umethod c:key@ ( -- addr )
bernd's avatar
bernd committed
34
    \G obtain the key storage
bernd's avatar
bernd committed
35
    umethod c:key# ( -- n )
bernd's avatar
bernd committed
36
    \G obtain key storage size
bernd's avatar
bernd committed
37
    umethod >c:key ( addr -- )
bernd's avatar
bernd committed
38
    \G move 128 bytes from addr to the state
bernd's avatar
bernd committed
39
    umethod c:key> ( addr -- )
bernd's avatar
bernd committed
40
    \G get 128 bytes from the state to addr
bernd's avatar
bernd committed
41
    umethod c:diffuse ( -- )
bernd's avatar
bernd committed
42
    \G perform a diffuse round
bernd's avatar
bernd committed
43
    umethod c:encrypt ( addr u -- )
bernd's avatar
bernd committed
44
    \G Encrypt message in buffer addr u
bernd's avatar
bernd committed
45
    umethod c:decrypt ( addr u -- )
bernd's avatar
bernd committed
46
    \G Decrypt message in buffer addr u
47
    umethod c:encrypt+auth ( addr u tag -- )
bernd's avatar
bernd committed
48
    \G Encrypt message in buffer addr u
49
    umethod c:decrypt+auth ( addr u tag -- flag )
bernd's avatar
bernd committed
50
    \G Decrypt message in buffer addr u
bernd's avatar
bernd committed
51
    umethod c:hash ( addr u -- )
bernd's avatar
bernd committed
52
    \G Hash message in buffer addr u
bernd's avatar
bernd committed
53
    umethod c:prng ( addr u -- )
bernd's avatar
bernd committed
54
    \G Fill buffer addr u with PRNG sequence
55
    umethod c:checksum ( tag -- xd )
bernd's avatar
bernd committed
56
    \G compute a 128 bit checksum
bernd's avatar
bernd committed
57
    umethod c:cookie ( -- x )
58
    \G compute a different 64 bit checksum
bernd's avatar
bernd committed
59 60
    umethod c:tweak! ( x128 -- )
    \G set 128 bit tweek (if tweekable)
bernd's avatar
bernd committed
61
end-class crypto