Commit 0b38a758 authored by Michael Raskin's avatar Michael Raskin

Syncing from Monotone at 20190502-024538. Last commit message was Fix empty...

Syncing from Monotone at 20190502-024538. Last commit message was Fix empty initialisation vector in AES
parent 498d5113
......@@ -3,7 +3,7 @@
(load-plugin "generic-readers.lisp")
(load-plugin "password-store.lisp")
(defun mkfile-add-encryption (password file-expression value-to-string)
(defun mkfile-add-encryption (password file-expression value-to-string &key (old t))
(let*
(
(macroexpanded-expression (macroexpand file-expression))
......@@ -45,7 +45,8 @@
(let* ((c (funcall ,old-content))
(c-str (and c ,(funcall value-to-string 'c)))
(p-str ,(funcall value-to-string password))
(ue (and c (unencrypt-aes c-str p-str)))
(ue (and c (if ,old (unencrypt-aes c-str p-str)
(unencrypt-aes-new c-str p-str))))
(ueo (and
c
(cl-fuse::string-to-octets
......@@ -62,9 +63,13 @@
`(lambda (x)
(funcall
,old-writer
(encrypt-aes
,(funcall value-to-string 'x)
,(funcall value-to-string password))))))
(if ,old
(encrypt-aes
,(funcall value-to-string 'x)
,(funcall value-to-string password))
(encrypt-aes-new
,(funcall value-to-string 'x)
,(funcall value-to-string password)))))))
)
(or
(when is-file
......
......@@ -33,6 +33,13 @@
)
full-string))
(defun random-buf (n)
(let* ((buf (make-array n :element-type '(unsigned-byte 8)
:initial-element 0)))
(with-open-file (f "/dev/urandom" :element-type '(unsigned-byte 8))
(read-sequence buf f))
buf))
(defun crypto-key (password)
(let* (
(hasher (make-instance 'ironclad:pbkdf2 :digest :sha1))
......@@ -74,3 +81,34 @@
output
))
(defun unencrypt-aes-new (x key)
(ignore-errors
(let* (
(byte-arr (base16-string-to-octet-array x))
(iv (subseq byte-arr 0 16))
(ciphertext (subseq byte-arr 16))
(cipher (ironclad:make-cipher :aes :key (crypto-key key) :mode :ctr
:initialization-vector iv))
(buffer (make-array (length ciphertext) :initial-element 32 :element-type '(unsigned-byte 8)))
(dummy (ironclad:decrypt cipher ciphertext buffer))
(output (cl-fuse::octets-to-string buffer :direct))
)
(declare (ignore dummy))
output
)))
(defun encrypt-aes-new (x key)
(let* (
(iv (random-buf 16))
(cipher (ironclad:make-cipher :aes :key (crypto-key key) :mode :ctr
:initialization-vector iv))
(byte-arr (cl-fuse::string-to-octets x :direct))
(buffer (make-array (length byte-arr) :initial-element 32 :element-type '(unsigned-byte 8)))
(dummy (ironclad:encrypt cipher byte-arr buffer))
(output (concatenate 'string
(octet-array-to-base16-string iv)
(octet-array-to-base16-string buffer)))
)
(declare (ignore dummy))
output
))
......@@ -496,11 +496,20 @@
,@(ast-eval (fifth x))))
(def-peg-fun
query-fs-sql2::encryptby (x)
query-fs-sql2::encryptbyold (x)
(mkfile-add-encryption
(ast-eval (third x))
(ast-eval (seventh x))
(lambda (x) `(sql-real-value ,x :encoding :direct))))
(lambda (x) `(sql-real-value ,x :encoding :direct))
:old t))
(def-peg-fun
query-fs-sql2::encryptbynew (x)
(mkfile-add-encryption
(ast-eval (third x))
(ast-eval (seventh x))
(lambda (x) `(sql-real-value ,x :encoding :direct))
:old nil))
(def-peg-fun
query-fs-sql2::sqlfor (x)
......
......@@ -5,7 +5,8 @@ StatementBody <- SetParam / ReadParam / IsolateDB
/ Proxy / Setter
/ Mkdir / OnCreateDir / OnCreateFile / WithFile
/ OnRemoveDir
/ EncryptBy
/ EncryptByNew
/ EncryptByOld
/ SQLFor / SQLGroupedFor
/ Template / CallTemplate
/ Symlink / OnSymlink
......@@ -39,7 +40,8 @@ OnWrite <- "on-write" S Identifier S SQLCommand
OnNonEmptyWrite <- "on-non-empty-write" S Identifier S SQLCommand
OnRemove <- "on-remove" S SQLCommand
EncryptBy <- "encrypt-by" S Value S "$(" S? (WithFile / Symlink) S? ")"
EncryptByOld <- "encrypt-by" S Value S "$(" S? (WithFile / Symlink) S? ")"
EncryptByNew <- "encrypt-by-new" S Value S "$(" S? (WithFile / Symlink) S? ")"
SQLFor <- "for" S Identifier S "in" S SQLQuery S StatementBody
SQLGroupedFor <- "grouped-for" S Identifier S "in" S SQLQuery S Block
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment