-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvimcrypt.el
153 lines (129 loc) · 5.55 KB
/
vimcrypt.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
;;; vimcrypt.el --- encrypt and decrypt vimcrypt files
;;
;; Copyright (c) 2017 Lorenzo Veronese
;;
;; Author: Lorenzo Veronese <wert310>
;; Created: 2017-02-03
;; Version: 0.1
;; Keywords: crypto
;;
;; This is free software; you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be
;; useful, but WITHOUT ANY WARRANTY; without even the implied
;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
;; PURPOSE. See the GNU General Public License for more details.
;; You should have received a copy of the GNU General Public
;; License along with this program; if not, write to the Free
;; Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
;; MA 02111-1307 USA
;;
;;; Commentary:
;;
;; Provides functions to encrypt and decrypt vim encrypted files
;;
;;; Code:
(require 'cl)
(require 'blowfish)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Utils
(defun vimcrypt-swap-endianness (data)
(apply #'concat
(loop for i from 0 to (1- (/ (length data) 4))
collecting (reverse (substring data (* i 4) (+ (* i 4) 4))))))
(defun vimcrypt-swapped-encrypt (bf data)
(vimcrypt-swap-endianness
(blowfish-encrypt bf (vimcrypt-swap-endianness data))))
(defun vimcrypt-zero-pad (data)
(let ((qty (mod (length data) 8)))
(cond ((zerop qty) data)
(t (concat data (make-string (- 8 qty) 0))))))
(defun vimcrypt-derive-key (password salt)
(let ((key (secure-hash 'sha256 (concat password salt))))
(dotimes (i 1000)
(setf key (secure-hash 'sha256 (concat key salt))))
(blowfish-decode-hex key)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; blowfish CFB
(defstruct vimcrypt-fixed-cfb
cipher iv)
(defstruct vimcrypt-bad-cfb
cipher iv)
(defmethod vimcrypt-cfb-decrypt ((cfb vimcrypt-bad-cfb) data)
(loop with plain = nil
with xor = (vimcrypt-swapped-encrypt (vimcrypt-bad-cfb-cipher cfb)
(vimcrypt-bad-cfb-iv cfb))
for i from 0 to (1- (length data))
if (and (>= i 64) (zerop (mod i 8)))
do (setf xor (vimcrypt-swapped-encrypt (vimcrypt-bad-cfb-cipher cfb)
(substring data (- i 64) (+ 8 (- i 64)))))
collect (logxor (aref xor (mod i 8)) (aref data i)) into plain
finally (return (apply #'string plain))))
(defmethod vimcrypt-cfb-decrypt ((cfb vimcrypt-fixed-cfb) data)
(loop with plain = nil
with xor = nil
for i from 0 to (1- (length data))
if (zerop (mod i 8))
do (progn
(setf xor (vimcrypt-swapped-encrypt (vimcrypt-fixed-cfb-cipher cfb)
(vimcrypt-fixed-cfb-iv cfb)))
(setf (vimcrypt-fixed-cfb-iv cfb) (substring data i (+ i 8))))
collect (logxor (aref xor (mod i 8)) (aref data i)) into plain
finally (return (apply #'string plain))))
(cl-defun vimcrypt-bf-decrypt (in-passwd in-data &key (version 'bf2))
(let* ((passwd (string-to-unibyte in-passwd))
(data (string-to-unibyte in-data))
(salt (substring data 0 8))
(iv (substring data 8 16))
(ciphertext (substring data 16))
(key (vimcrypt-derive-key passwd salt))
(cipher (blowfish-init key))
(cfb (case version
((bf1) (make-vimcrypt-bad-cfb :cipher cipher :iv iv))
((bf2) (make-vimcrypt-fixed-cfb :cipher cipher :iv iv))
(t (error "Invalid BF version!")))))
(substring
(vimcrypt-cfb-decrypt cfb (vimcrypt-zero-pad ciphertext))
0 (length ciphertext))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Toplevel Functions
(defun vimcrypt-decrypt (in-passwd in-data)
(let ((magic (substring in-data 0 12)))
(cond ((equal magic "VimCrypt~03!")
(vimcrypt-bf-decrypt in-passwd (substring in-data 12) :version 'bf2))
((equal magic "VimCrypt~02!")
(vimcrypt-bf-decrypt in-passwd (substring in-data 12) :version 'bf1))
((equal (substring magic 0 9) "VimCrypt~")
(error "Invalid vimcrypt method '%s'!" magic))
(t (error "Invalid file: not vimcrypt encrypted!")))))
(defun vimcrypt-decrypt-buffer (buffer)
"Decrypt the content of BUFFER in place"
(interactive "bBuffer: ")
(with-current-buffer (get-buffer buffer)
(let ((password (read-passwd "Password: ")))
(message "Decrypting...")
(let* ((string (buffer-string))
(plain (vimcrypt-decrypt password string)))
(message "Done.")
(erase-buffer)
(insert plain)
(goto-char (point-min))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; TEST
(let (bb)
(setq bb (blowfish-init (vimcrypt-derive-key "password" "salt")))
(blowfish-encode-hex
(blowfish-encrypt bb "plaintxt")) ; 72503b38106022a7
(blowfish-encode-hex
(vimcrypt-swapped-encrypt bb "plaintxt"))) ; ad3dfa7fe8ea40f6
(let (b1 d1 e1)
(setq b1 (blowfish-init
(blowfish-decode-hex
"64645ae6b151959a4ea2a778e22f3c32030aa8a5c3917dc43bc9b3229d331d16")))
(setq d1 (blowfish-decode-hex "ecc3a9dc3d4b0f8d"))
(setq e1 (vimcrypt-swapped-encrypt b1 (blowfish-decode-hex "c2fa00d1be7d851c")))
(apply #'string (loop for i from 0 to 7 collect (logxor (aref d1 i) (aref e1 i)))))
(provide 'vimcrypt)
;; vimcrypt.el ends here