L1VM - libcrypto

There is a new crypto library for L1VM. It uses the “libsodium” “crypto_secretbox_easy()” function.
The include is in “include/crypto.l1h”. I did write a demo program which encrypts and decrypts a byte array: “crypto-lib.l1com”.

Here is the program:

// crypto-lib.l1com
//
// encrypt/decrypt demo
//
#include <intr.l1h>
(main func)
    (set int64 1 zero 0)
    (set int64 1 one 1)
    (set byte 1 zerob 0)
    (set byte 1 oneb 1)
    (set byte 256 inarray)
    (set byte 272 outarray)
    (set byte 272 decryptarray)
    (set byte 24 noncearray)
    (set byte 1 i 0)
    (set int64 1 size 256)
    (set int64 1 mode 0)
    (set int64 1 f 0)
    (set byte 1 c 0)
    (set const-int64 1 Pinarray 0)
    (set const-int64 1 Poutarray 0)
    (set const-int64 1 Pkeystr 0)
    (set const-int64 1 Pdecryptarray)
    (set const-int64 1 Pnoncearray)
    (set byte 256 keystr)
    (set string s okstr "crypt decrypt/encrypt ok!")
    (set string s errstr "crypt decrypt/encrypt ERROR!")
    (set int64 1 ret 0)
    (set int64 1 f 0)
    (inarray Pinarray pointer)
    (outarray Poutarray pointer)
    (keystr Pkeystr pointer)
    (decryptarray Pdecryptarray pointer)
    (noncearray Pnoncearray pointer)
    (zero :crypto_init !)
    (for-loop)
    (((i size <) f =) f for)
        (i inarray [ i ] =)
        ((i oneb +) i =)
    (next)
    // encrypt
    (Pinarray size Poutarray Pkeystr Pnoncearray mode :encrypt !)
    (ret stpopi)
    // decrypt
    (one mode =)
    (Poutarray size Pdecryptarray Pkeystr Pnoncearray mode :encrypt !)
    (ret stpopi)
    (((ret zero !=) f =) f if)
        print_s (errstr)
        print_n
        exit (one)
    (endif)
    // check array
    (zerob i =)
    (for-loop)
    (((i size <) f =) f for)
        (decryptarray [ i ] c =)
        (((c i !=) f =) f if)
            print_s (errstr)
            print_n
            exit (one)
        (endif)
        ((i oneb +) i =)
    (next)
    print_s (okstr)
    print_n
    exit (zero)
(funcend)
#include <crypto.l1h>

The “encrypt” function generates a key in “Pkeystr” and a nonce in “Pnoncearray” if encrypt mode is set to “0”. In the second “encrypt” call the mode is set to “1” for decrypt the byte array!