[ prog / sol / mona ]

prog


SchemeBBS [part 2]

4 2020-06-19 11:47

Here is an upgrade of >>3 with selectable padding scheme and the addition of PKCS#7 padding.

$ cat test.scm
(define (simple-mcrypt algo mode padunit ivlen padin padout)
   (lambda (text key encrypt?)
      (let ((out (with-input-from-string
                    (if encrypt?
                        (padin text padunit)
                        text)
                    (lambda ()
                       (call-with-output-string
                          (lambda (port)
                             (mcrypt-encrypt-port
                                algo
                                mode
                                (current-input-port)
                                port
                                key
                                (make-string ivlen #\NUL)
                                encrypt?)))))))
         (if encrypt?
             out
             (padout out padunit)))))

(define (test-simple-mcrypt engine text key)
   (let* ((hidden   (engine text   key #t))
          (revealed (engine hidden key #f)))
      (string=? text revealed)))

(define (padin-nul s unit)
   (let* ((slen (string-length s))
          (mod  (modulo slen unit)))
      (if (= mod 0)
          s
          (string-pad-right s (+ slen (- unit mod)) #\NUL))))

(define (padout-nul)
   (define trimcs (char-set-invert (char-set #\NUL)))
   (lambda (s unit)
      (string-trim-right s trimcs)))

(define (padin-pkcs7 s unit)
   (let* ((slen (string-length s))
          (mod  (modulo slen unit))
          (used (- unit mod)))
      (string-pad-right s (+ slen used) (ascii->char used))))

(define (padout-pkcs7 s unit)
   (let* ((slen (string-length s))
          (used (char->ascii (string-ref s (- slen 1)))))
      (string-head s (- slen used))))

Usage example with cbc AES-256. Strings of NULs are no problem with PKCS#7.

$ mit-scheme --load test.scm
[...]
  Release 9.1.1     || Microcode 15.3 || Runtime 15.7 || SF 4.41
  LIAR/x86-64 4.118 || Edwin 3.116
;Loading "test.scm"... done

1 ]=> (mcrypt-available?)
;Loading "/usr/lib/x86_64-linux-gnu/mit-scheme/lib/prmcrypt.so"... done
;Value: #t
1 ]=> (define aes (simple-mcrypt "rijndael-256" "cbc" 16 16 padin-nul (padout-nul)))
;Value: aes
1 ]=> (define key (string-append (md5-string "secret key") (md5-string "part two")))
;Loading "/usr/lib/x86_64-linux-gnu/mit-scheme/lib/prmhash.so"... done
;Value: key
1 ]=> (define text "How can I run my own instance of this")
;Value: text
1 ]=> (test-simple-mcrypt aes text key)
;Value: #t

1 ]=> (define aes (simple-mcrypt "rijndael-256" "cbc" 16 16 padin-pkcs7 padout-pkcs7))
;Value: aes
1 ]=> (test-simple-mcrypt aes text key)
;Value: #t
1 ]=> (define text (make-string 55 #\NUL))
;Value: text
1 ]=> text
;Value 13: "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
1 ]=> (test-simple-mcrypt aes text key)
;Value: #t
1 ]=> 
112


VIP:

do not edit these