1 ;;; encrypt.el --- file encryption routines
2 ;; Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
4 ;; Author: Teodor Zlatanov <tzz@lifelogs.com>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;;; This module addresses data encryption. Page breaks are used for
28 ;;; grouping declarations and documentation relating to each
29 ;;; particular aspect.
35 (autoload 'password-read "password"))
38 "File encryption configuration.")
40 (defcustom encrypt-password-cache-expiry 200
41 "Encryption password timeout.
42 When set, directly sets password-cache-expiry"
45 :set (lambda (symbol value)
47 (setq password-cache-expiry value)))
49 (defcustom encrypt-file-alist nil
50 "List of file names or regexes matched with encryptions.
55 (encrypt-xor \"Semi-Secret\")))"
58 (list :tag "Encryption entry"
59 (radio :tag "What to encrypt"
60 (file :tag "Filename")
61 (regexp :tag "Regular expression match"))
62 (radio :tag "How to encrypt it"
65 (const :tag "GPG Program" gpg)
66 (radio :tag "Choose a cipher"
67 (const :tag "3DES Encryption" "3DES")
68 (const :tag "CAST5 Encryption" "CAST5")
69 (const :tag "Blowfish Encryption" "BLOWFISH")
70 (const :tag "AES Encryption" "AES")
71 (const :tag "AES192 Encryption" "AES192")
72 (const :tag "AES256 Encryption" "AES256")
73 (const :tag "Twofish Encryption" "TWOFISH")
74 (string :tag "Cipher Name")))
76 :tag "Built-in simple XOR"
77 (const :tag "XOR Encryption" encrypt-xor)
78 (string :tag "XOR Cipher Value (seed value)")))))
81 ;; TODO: now, load gencrypt.el and if successful, modify the
82 ;; custom-type of encrypt-file-alist to add the gencrypt.el options
84 ;; (plist-get (symbol-plist 'encrypt-file-alist) 'custom-type)
87 (defcustom encrypt-gpg-path (executable-find "gpg")
88 "Path to the GPG program."
90 (file :tag "Location of the GPG executable")
91 (const :tag "GPG is not installed" nil))
94 (defvar encrypt-temp-prefix "encrypt"
95 "Prefix for temporary filenames")
98 (defun encrypt-find-model (filename)
99 "Given a filename, find a encrypt-file-alist entry"
100 (dolist (entry encrypt-file-alist)
101 (let ((match (nth 0 entry))
102 (model (nth 1 entry)))
103 (when (or (eq match filename)
104 (string-match match filename))
108 (defun encrypt-insert-file-contents (file &optional model)
109 "Decrypt FILE into the current buffer."
110 (interactive "fFile to insert: ")
111 (let* ((model (or model (encrypt-find-model file)))
112 (method (nth 0 model))
113 (cipher (nth 1 model))
114 (password-key (format "encrypt-password-%s-%s %s"
115 (symbol-name method) cipher file))
117 (password-read-and-add
118 (format "%s password for cipher %s? "
119 (symbol-name method) cipher)
121 (buffer-file-coding-system 'binary)
122 (coding-system-for-read 'binary)
125 ;; note we only insert-file-contents if the method is known to be valid
128 (insert-file-contents file)
129 (setq outdata (encrypt-gpg-decode-buffer passphrase cipher)))
130 ((eq method 'encrypt-xor)
131 (insert-file-contents file)
132 (setq outdata (encrypt-xor-decode-buffer passphrase cipher))))
136 (gnus-message 9 "%s was decrypted with %s (cipher %s)"
137 file (symbol-name method) cipher)
138 (delete-region (point-min) (point-max))
139 (goto-char (point-min))
141 ;; the decryption failed, alas
142 (password-cache-remove password-key)
143 (gnus-error 5 "%s was NOT decrypted with %s (cipher %s)"
144 file (symbol-name method) cipher))))
146 (defun encrypt-get-file-contents (file &optional model)
147 "Decrypt FILE and return the contents."
148 (interactive "fFile to decrypt: ")
150 (encrypt-insert-file-contents file model)
153 (defun encrypt-put-file-contents (file data &optional model)
154 "Encrypt the DATA to FILE, then continue normally."
157 (encrypt-write-file-contents file model)))
159 (defun encrypt-write-file-contents (file &optional model)
160 "Encrypt the current buffer to FILE, then continue normally."
161 (interactive "fFile to write: ")
162 (let* ((model (or model (encrypt-find-model file)))
163 (method (nth 0 model))
164 (cipher (nth 1 model))
165 (password-key (format "encrypt-password-%s-%s %s"
166 (symbol-name method) cipher file))
169 (format "%s password for cipher %s? "
170 (symbol-name method) cipher)
176 (setq outdata (encrypt-gpg-encode-buffer passphrase cipher)))
177 ((eq method 'encrypt-xor)
178 (setq outdata (encrypt-xor-encode-buffer passphrase cipher))))
182 (gnus-message 9 "%s was encrypted with %s (cipher %s)"
183 file (symbol-name method) cipher)
184 (delete-region (point-min) (point-max))
185 (goto-char (point-min))
187 ;; do not confirm overwrites
188 (write-file file nil))
189 ;; the decryption failed, alas
190 (password-cache-remove password-key)
191 (gnus-error 5 "%s was NOT encrypted with %s (cipher %s)"
192 file (symbol-name method) cipher))))
194 (defun encrypt-xor-encode-buffer (passphrase cipher)
195 (encrypt-xor-process-buffer passphrase cipher t))
197 (defun encrypt-xor-decode-buffer (passphrase cipher)
198 (encrypt-xor-process-buffer passphrase cipher nil))
200 (defun encrypt-xor-process-buffer (passphrase
203 "Given PASSPHRASE, xor-encode or decode the contents of the current buffer."
204 (let* ((bs (buffer-substring-no-properties (point-min) (point-max)))
205 ;; passphrase-sum is a simple additive checksum of the
206 ;; passphrase and the cipher
208 (when (stringp passphrase)
209 (apply '+ (append cipher passphrase nil))))
215 (dolist (x (append bs nil))
216 (setq new-list (cons (logxor x passphrase-sum) new-list)))
219 (insert (format "%d " x))))
221 (setq new-list (reverse (split-string bs)))
223 (setq x (string-to-int x))
224 (insert (format "%c" (logxor x passphrase-sum))))))
225 (buffer-substring-no-properties (point-min) (point-max)))))
227 (defun encrypt-gpg-encode-buffer (passphrase cipher)
228 (encrypt-gpg-process-buffer passphrase cipher t))
230 (defun encrypt-gpg-decode-buffer (passphrase cipher)
231 (encrypt-gpg-process-buffer passphrase cipher nil))
233 (defun encrypt-gpg-process-buffer (passphrase
236 "With PASSPHRASE, use GPG to encode or decode the current buffer."
237 (let* ((program encrypt-gpg-path)
238 (input (buffer-substring-no-properties (point-min) (point-max)))
239 (temp-maker (if (fboundp 'make-temp-file)
242 (temp-file (funcall temp-maker encrypt-temp-prefix))
243 (default-enable-multibyte-characters nil)
244 (args `("--cipher-algo" ,cipher
247 "--passphrase-fd" "0"
249 exit-status exit-data)
260 (insert passphrase "\n"))
263 (apply #'call-process-region (point-min) (point-max) program
264 t `(t ,temp-file) nil args))
265 (if (equal exit-status 0)
267 (buffer-substring-no-properties (point-min) (point-max)))
269 (when (file-exists-p temp-file)
270 (insert-file-contents temp-file))
271 (gnus-error 5 (format "%s exited abnormally: '%s' [%s]"
272 program exit-status (buffer-string)))))
273 (delete-file temp-file))
274 (gnus-error 5 "GPG is not installed."))
278 ;;; encrypt.el ends here
280 ;; arch-tag: d907e4f1-71b5-42b1-a180-fc7b84ff0648