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")
36 ;; Those two autoloads are needed since T-gnus won't load password.el
37 ;; by way of pgg.el at the compile time.
38 (autoload 'password-read-and-add "password")
39 (autoload 'password-cache-remove "password"))
42 "File encryption configuration.")
44 (defcustom encrypt-password-cache-expiry 200
45 "Encryption password timeout.
46 When set, directly sets password-cache-expiry"
49 :set (lambda (symbol value)
51 (setq password-cache-expiry value)))
53 (defcustom encrypt-file-alist nil
54 "List of file names or regexes matched with encryptions.
59 (encrypt-xor \"Semi-Secret\")))"
62 (list :tag "Encryption entry"
63 (radio :tag "What to encrypt"
64 (file :tag "Filename")
65 (regexp :tag "Regular expression match"))
66 (radio :tag "How to encrypt it"
69 (const :tag "GPG Program" gpg)
70 (radio :tag "Choose a cipher"
71 (const :tag "3DES Encryption" "3DES")
72 (const :tag "CAST5 Encryption" "CAST5")
73 (const :tag "Blowfish Encryption" "BLOWFISH")
74 (const :tag "AES Encryption" "AES")
75 (const :tag "AES192 Encryption" "AES192")
76 (const :tag "AES256 Encryption" "AES256")
77 (const :tag "Twofish Encryption" "TWOFISH")
78 (string :tag "Cipher Name")))
80 :tag "Built-in simple XOR"
81 (const :tag "XOR Encryption" encrypt-xor)
82 (string :tag "XOR Cipher Value (seed value)")))))
85 ;; TODO: now, load gencrypt.el and if successful, modify the
86 ;; custom-type of encrypt-file-alist to add the gencrypt.el options
88 ;; (plist-get (symbol-plist 'encrypt-file-alist) 'custom-type)
91 (defcustom encrypt-gpg-path (executable-find "gpg")
92 "Path to the GPG program."
94 (file :tag "Location of the GPG executable")
95 (const :tag "GPG is not installed" nil))
98 (defvar encrypt-temp-prefix "encrypt"
99 "Prefix for temporary filenames")
102 (defun encrypt-find-model (filename)
103 "Given a filename, find a encrypt-file-alist entry"
104 (dolist (entry encrypt-file-alist)
105 (let ((match (nth 0 entry))
106 (model (nth 1 entry)))
107 (when (or (eq match filename)
108 (string-match match filename))
112 (defun encrypt-insert-file-contents (file &optional model)
113 "Decrypt FILE into the current buffer."
114 (interactive "fFile to insert: ")
115 (let* ((model (or model (encrypt-find-model file)))
116 (method (nth 0 model))
117 (cipher (nth 1 model))
118 (password-key (format "encrypt-password-%s-%s %s"
119 (symbol-name method) cipher file))
121 (password-read-and-add
122 (format "%s password for cipher %s? "
123 (symbol-name method) cipher)
125 (buffer-file-coding-system 'binary)
126 (coding-system-for-read 'binary)
129 ;; note we only insert-file-contents if the method is known to be valid
132 (insert-file-contents file)
133 (setq outdata (encrypt-gpg-decode-buffer passphrase cipher)))
134 ((eq method 'encrypt-xor)
135 (insert-file-contents file)
136 (setq outdata (encrypt-xor-decode-buffer passphrase cipher))))
140 (gnus-message 9 "%s was decrypted with %s (cipher %s)"
141 file (symbol-name method) cipher)
142 (delete-region (point-min) (point-max))
143 (goto-char (point-min))
145 ;; the decryption failed, alas
146 (password-cache-remove password-key)
147 (gnus-error 5 "%s was NOT decrypted with %s (cipher %s)"
148 file (symbol-name method) cipher))))
150 (defun encrypt-get-file-contents (file &optional model)
151 "Decrypt FILE and return the contents."
152 (interactive "fFile to decrypt: ")
154 (encrypt-insert-file-contents file model)
157 (defun encrypt-put-file-contents (file data &optional model)
158 "Encrypt the DATA to FILE, then continue normally."
161 (encrypt-write-file-contents file model)))
163 (defun encrypt-write-file-contents (file &optional model)
164 "Encrypt the current buffer to FILE, then continue normally."
165 (interactive "fFile to write: ")
166 (let* ((model (or model (encrypt-find-model file)))
167 (method (nth 0 model))
168 (cipher (nth 1 model))
169 (password-key (format "encrypt-password-%s-%s %s"
170 (symbol-name method) cipher file))
173 (format "%s password for cipher %s? "
174 (symbol-name method) cipher)
180 (setq outdata (encrypt-gpg-encode-buffer passphrase cipher)))
181 ((eq method 'encrypt-xor)
182 (setq outdata (encrypt-xor-encode-buffer passphrase cipher))))
186 (gnus-message 9 "%s was encrypted with %s (cipher %s)"
187 file (symbol-name method) cipher)
188 (delete-region (point-min) (point-max))
189 (goto-char (point-min))
191 ;; do not confirm overwrites
192 (write-file file nil))
193 ;; the decryption failed, alas
194 (password-cache-remove password-key)
195 (gnus-error 5 "%s was NOT encrypted with %s (cipher %s)"
196 file (symbol-name method) cipher))))
198 (defun encrypt-xor-encode-buffer (passphrase cipher)
199 (encrypt-xor-process-buffer passphrase cipher t))
201 (defun encrypt-xor-decode-buffer (passphrase cipher)
202 (encrypt-xor-process-buffer passphrase cipher nil))
204 (defun encrypt-xor-process-buffer (passphrase
207 "Given PASSPHRASE, xor-encode or decode the contents of the current buffer."
208 (let* ((bs (buffer-substring-no-properties (point-min) (point-max)))
209 ;; passphrase-sum is a simple additive checksum of the
210 ;; passphrase and the cipher
212 (when (stringp passphrase)
213 (apply '+ (append cipher passphrase nil))))
219 (dolist (x (append bs nil))
220 (setq new-list (cons (logxor x passphrase-sum) new-list)))
223 (insert (format "%d " x))))
225 (setq new-list (reverse (split-string bs)))
227 (setq x (string-to-int x))
228 (insert (format "%c" (logxor x passphrase-sum))))))
229 (buffer-substring-no-properties (point-min) (point-max)))))
231 (defun encrypt-gpg-encode-buffer (passphrase cipher)
232 (encrypt-gpg-process-buffer passphrase cipher t))
234 (defun encrypt-gpg-decode-buffer (passphrase cipher)
235 (encrypt-gpg-process-buffer passphrase cipher nil))
237 (defun encrypt-gpg-process-buffer (passphrase
240 "With PASSPHRASE, use GPG to encode or decode the current buffer."
241 (let* ((program encrypt-gpg-path)
242 (input (buffer-substring-no-properties (point-min) (point-max)))
243 (temp-maker (if (fboundp 'make-temp-file)
246 (temp-file (funcall temp-maker encrypt-temp-prefix))
247 (default-enable-multibyte-characters nil)
248 (args `("--cipher-algo" ,cipher
251 "--passphrase-fd" "0"
253 exit-status exit-data)
264 (insert passphrase "\n"))
267 (apply #'call-process-region (point-min) (point-max) program
268 t `(t ,temp-file) nil args))
269 (if (equal exit-status 0)
271 (buffer-substring-no-properties (point-min) (point-max)))
273 (when (file-exists-p temp-file)
274 (insert-file-contents temp-file))
275 (gnus-error 5 (format "%s exited abnormally: '%s' [%s]"
276 program exit-status (buffer-string)))))
277 (delete-file temp-file))
278 (gnus-error 5 "GPG is not installed."))
282 ;;; encrypt.el ends here