crypto-helper.scm - pee - Pee a password manager;Pee - because you have to...
 (HTM) git clone git://vernunftzentrum.de/pee.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) LICENSE
       ---
       crypto-helper.scm (1609B)
       ---
            1 (module crypto-helper (random-bytes hash-passphrase blake2s)
            2         (import (chicken base) scheme (chicken foreign))
            3         (import  (chicken blob) blob-hexadecimal srfi-4)
            4         (foreign-declare "#include \"blake2.h\"")
            5         (foreign-declare "#include \"blake2-impl.h\"")
            6         (foreign-declare "#include \"blake2s-ref.c\"")
            7 
            8         (define-foreign-variable blake2s-outbytes integer "BLAKE2S_OUTBYTES")
            9 
           10         (define blake2s
           11           (foreign-lambda*
           12            int
           13            ((c-string passphrase)
           14             ((scheme-pointer unsigned-char) out))
           15            "
           16            int res = 0;
           17            res = blake2s(out, passphrase, NULL, BLAKE2S_OUTBYTES, strlen(passphrase), 0);
           18            C_return(res);
           19            "))
           20 
           21         (define (hash-passphrase passphrase)
           22           (let ((out (make-blob blake2s-outbytes)))
           23             (if (not (zero? (blake2s passphrase out)))
           24                 (error "Unable to hash passphrase with blake2s")
           25                 out)))
           26 
           27         (define (random-bytes len)
           28           (cond-expand (openbsd:
           29                         (arc4random len))
           30                        (linux:
           31                         (with-input-from-file "/dev/urandom" (lambda () (read-u8vector len))))
           32                        (else
           33                         (with-input-from-file "/dev/random" (lambda () (read-u8vector len))))))
           34         #+openbsd
           35         (define (arc4random len)
           36           (let ((buf (make-blob len)))
           37             ((foreign-lambda
           38               void
           39               "arc4random_buf"
           40               (scheme-pointer void)
           41               size_t) buf len)
           42             (blob->u8vector buf)))
           43 )