1 ;;; encrypt.el --- file encryption routines
2 ;; Copyright (C) 2002, 2003, 2004, 2005 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 ;; The following section is 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")
40 (defvar password-cache-expiry))
42 (defgroup encrypt '((password-cache custom-variable)
43 (password-cache-expiry custom-variable))
44 "File encryption configuration.")
46 (defcustom encrypt-file-alist nil
47 "List of file names or regexes matched with encryptions.
52 (encrypt-xor \"Semi-Secret\")))"
55 (list :tag "Encryption entry"
56 (radio :tag "What to encrypt"
57 (file :tag "Filename")
58 (regexp :tag "Regular expression match"))
59 (radio :tag "How to encrypt it"
62 (const :tag "GPG Program" gpg)
63 (radio :tag "Choose a cipher"
64 (const :tag "3DES Encryption" "3DES")
65 (const :tag "CAST5 Encryption" "CAST5")
66 (const :tag "Blowfish Encryption" "BLOWFISH")
67 (const :tag "AES Encryption" "AES")
68 (const :tag "AES192 Encryption" "AES192")
69 (const :tag "AES256 Encryption" "AES256")
70 (const :tag "Twofish Encryption" "TWOFISH")
71 (string :tag "Cipher Name")))
73 :tag "Built-in simple XOR"
74 (const :tag "XOR Encryption" encrypt-xor)
75 (string :tag "XOR Cipher Value (seed value)")))))
78 ;; TODO: now, load gencrypt.el and if successful, modify the
79 ;; custom-type of encrypt-file-alist to add the gencrypt.el options
81 ;; (plist-get (symbol-plist 'encrypt-file-alist) 'custom-type)
84 (defcustom encrypt-gpg-path (executable-find "gpg")
85 "Path to the GPG program."
87 (file :tag "Location of the GPG executable")
88 (const :tag "GPG is not installed" nil))
91 (defvar encrypt-temp-prefix "encrypt"
92 "Prefix for temporary filenames")
95 (defun encrypt-find-model (filename)
96 "Given a filename, find a encrypt-file-alist entry"
97 (dolist (entry encrypt-file-alist)
98 (let ((match (nth 0 entry))
99 (model (nth 1 entry)))
100 (when (or (eq match filename)
101 (string-match match filename))
105 (defun encrypt-insert-file-contents (file &optional model)
106 "Decrypt FILE into the current buffer."
107 (interactive "fFile to insert: ")
108 (let* ((model (or model (encrypt-find-model file)))
109 (method (nth 0 model))
110 (cipher (nth 1 model))
111 (password-key (format "encrypt-password-%s-%s %s"
112 (symbol-name method) cipher file))
114 (password-read-and-add
115 (format "%s password for cipher %s? "
116 (symbol-name method) cipher)
118 (buffer-file-coding-system 'binary)
119 (coding-system-for-read 'binary)
122 ;; note we only insert-file-contents if the method is known to be valid
125 (insert-file-contents file)
126 (setq outdata (encrypt-gpg-decode-buffer passphrase cipher)))
127 ((eq method 'encrypt-xor)
128 (insert-file-contents file)
129 (setq outdata (encrypt-xor-decode-buffer passphrase cipher))))
133 (gnus-message 9 "%s was decrypted with %s (cipher %s)"
134 file (symbol-name method) cipher)
135 (delete-region (point-min) (point-max))
136 (goto-char (point-min))
138 ;; the decryption failed, alas
139 (password-cache-remove password-key)
140 (gnus-error 5 "%s was NOT decrypted with %s (cipher %s)"
141 file (symbol-name method) cipher))))
143 (defun encrypt-get-file-contents (file &optional model)
144 "Decrypt FILE and return the contents."
145 (interactive "fFile to decrypt: ")
147 (encrypt-insert-file-contents file model)
150 (defun encrypt-put-file-contents (file data &optional model)
151 "Encrypt the DATA to FILE, then continue normally."
154 (encrypt-write-file-contents file model)))
156 (defun encrypt-write-file-contents (file &optional model)
157 "Encrypt the current buffer to FILE, then continue normally."
158 (interactive "fFile to write: ")
159 (let* ((model (or model (encrypt-find-model file)))
160 (method (nth 0 model))
161 (cipher (nth 1 model))
162 (password-key (format "encrypt-password-%s-%s %s"
163 (symbol-name method) cipher file))
166 (format "%s password for cipher %s? "
167 (symbol-name method) cipher)
173 (setq outdata (encrypt-gpg-encode-buffer passphrase cipher)))
174 ((eq method 'encrypt-xor)
175 (setq outdata (encrypt-xor-encode-buffer passphrase cipher))))
179 (gnus-message 9 "%s was encrypted with %s (cipher %s)"
180 file (symbol-name method) cipher)
181 (delete-region (point-min) (point-max))
182 (goto-char (point-min))
184 ;; do not confirm overwrites
185 (write-file file nil))
186 ;; the decryption failed, alas
187 (password-cache-remove password-key)
188 (gnus-error 5 "%s was NOT encrypted with %s (cipher %s)"
189 file (symbol-name method) cipher))))
191 (defun encrypt-xor-encode-buffer (passphrase cipher)
192 (encrypt-xor-process-buffer passphrase cipher t))
194 (defun encrypt-xor-decode-buffer (passphrase cipher)
195 (encrypt-xor-process-buffer passphrase cipher nil))
197 (defun encrypt-xor-process-buffer (passphrase
200 "Given PASSPHRASE, xor-encode or decode the contents of the current buffer."
201 (let* ((bs (buffer-substring-no-properties (point-min) (point-max)))
202 ;; passphrase-sum is a simple additive checksum of the
203 ;; passphrase and the cipher
205 (when (stringp passphrase)
206 (apply '+ (append cipher passphrase nil))))
212 (dolist (x (append bs nil))
213 (setq new-list (cons (logxor x passphrase-sum) new-list)))
216 (insert (format "%d " x))))
218 (setq new-list (reverse (split-string bs)))
220 (setq x (string-to-int x))
221 (insert (format "%c" (logxor x passphrase-sum))))))
222 (buffer-substring-no-properties (point-min) (point-max)))))
224 (defun encrypt-gpg-encode-buffer (passphrase cipher)
225 (encrypt-gpg-process-buffer passphrase cipher t))
227 (defun encrypt-gpg-decode-buffer (passphrase cipher)
228 (encrypt-gpg-process-buffer passphrase cipher nil))
230 (defun encrypt-gpg-process-buffer (passphrase
233 "With PASSPHRASE, use GPG to encode or decode the current buffer."
234 (let* ((program encrypt-gpg-path)
235 (input (buffer-substring-no-properties (point-min) (point-max)))
236 (temp-maker (if (fboundp 'make-temp-file)
239 (temp-file (funcall temp-maker encrypt-temp-prefix))
240 (default-enable-multibyte-characters nil)
241 (args `("--cipher-algo" ,cipher
244 "--passphrase-fd" "0"
246 exit-status exit-data)
257 (insert passphrase "\n"))
260 (apply #'call-process-region (point-min) (point-max) program
261 t `(t ,temp-file) nil args))
262 (if (equal exit-status 0)
264 (buffer-substring-no-properties (point-min) (point-max)))
266 (when (file-exists-p temp-file)
267 (insert-file-contents temp-file))
268 (gnus-error 5 (format "%s exited abnormally: '%s' [%s]"
269 program exit-status (buffer-string)))))
270 (delete-file temp-file))
271 (gnus-error 5 "GPG is not installed."))
275 ;;; encrypt.el ends here