* encrypt.el: add autoload tags
[gnus] / lisp / encrypt.el
1 ;;; encrypt.el --- file encryption routines
2 ;; Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
3
4 ;; Author: Teodor Zlatanov <tzz@lifelogs.com>
5 ;; Created: 2003/01/24
6 ;; Keywords: files
7
8 ;; This file is part of GNU Emacs.
9
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)
13 ;; any later version.
14
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.
19
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.
24
25 ;;; Commentary:
26
27 ;;; This module addresses data encryption.  Page breaks are used for
28 ;;; grouping declarations and documentation relating to each
29 ;;; particular aspect.
30
31 ;;; Code:
32
33 ;; autoload password
34 (eval-and-compile
35   (autoload 'password-read "password"))
36
37 (defgroup encrypt nil
38   "File encryption configuration.")
39
40 (defcustom encrypt-password-cache-expiry 200
41   "Encryption password timeout.
42 When set, directly sets password-cache-expiry"
43   :type 'integer
44   :group 'encrypt
45   :set (lambda (symbol value)
46          (set symbol value)
47          (setq password-cache-expiry value)))
48
49 (defcustom encrypt-file-alist nil
50   "List of file names or regexes matched with encryptions.
51 Format example:
52  '((\"beta\"
53     (gpg \"AES\"))
54    (\"/home/tzz/alpha\"
55     (encrypt-xor \"Semi-Secret\")))"
56
57   :type '(repeat
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"
63                   (list
64                    :tag "GPG Encryption"
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")))
75                   (list
76                    :tag "Built-in simple XOR"
77                    (const :tag "XOR Encryption" encrypt-xor)
78                    (string :tag "XOR Cipher Value (seed value)")))))
79   :group 'encrypt)
80
81 ;; TODO: now, load gencrypt.el and if successful, modify the
82 ;; custom-type of encrypt-file-alist to add the gencrypt.el options
83
84 ;; (plist-get (symbol-plist 'encrypt-file-alist) 'custom-type)
85 ;; then use plist-put
86
87 (defcustom encrypt-gpg-path (executable-find "gpg")
88   "Path to the GPG program."
89   :type '(radio
90           (file :tag "Location of the GPG executable")
91           (const :tag "GPG is not installed" nil))
92   :group 'encrypt)
93
94 (defvar encrypt-temp-prefix "encrypt"
95   "Prefix for temporary filenames")
96
97 ;;;###autoload
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))
105         (return model)))))
106
107 ;;;###autoload
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))
116          (passphrase
117           (password-read-and-add
118            (format "%s password for cipher %s? "
119                    (symbol-name method) cipher)
120            password-key))
121           (buffer-file-coding-system 'binary)
122          (coding-system-for-read 'binary)
123          outdata)
124
125     ;; note we only insert-file-contents if the method is known to be valid
126     (cond
127      ((eq method 'gpg)
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))))
133
134     (if outdata
135         (progn
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))
140           (insert outdata))
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))))
145
146 (defun encrypt-get-file-contents (file &optional model)
147   "Decrypt FILE and return the contents."
148   (interactive "fFile to decrypt: ")
149   (with-temp-buffer
150     (encrypt-insert-file-contents file model)
151     (buffer-string)))
152
153 (defun encrypt-put-file-contents (file data &optional model)
154   "Encrypt the DATA to FILE, then continue normally."
155   (with-temp-buffer
156     (insert data)
157     (encrypt-write-file-contents file model)))
158
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))
167          (passphrase
168           (password-read
169            (format "%s password for cipher %s? "
170                    (symbol-name method) cipher)
171            password-key))
172          outdata)
173
174     (cond
175      ((eq method 'gpg)
176       (setq outdata (encrypt-gpg-encode-buffer passphrase cipher)))
177      ((eq method 'encrypt-xor)
178       (setq outdata (encrypt-xor-encode-buffer passphrase cipher))))
179
180     (if outdata
181         (progn
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))
186           (insert outdata)
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))))
193
194 (defun encrypt-xor-encode-buffer (passphrase cipher)
195   (encrypt-xor-process-buffer passphrase cipher t))
196
197 (defun encrypt-xor-decode-buffer (passphrase cipher)
198   (encrypt-xor-process-buffer passphrase cipher nil))
199
200 (defun encrypt-xor-process-buffer (passphrase
201                                         cipher
202                                         &optional encode)
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
207         (passphrase-sum
208          (when (stringp passphrase)
209            (apply '+ (append cipher passphrase nil))))
210         new-list)
211
212     (with-temp-buffer
213       (if encode
214           (progn
215             (dolist (x (append bs nil))
216               (setq new-list (cons (logxor x passphrase-sum) new-list)))
217
218             (dolist (x new-list)
219               (insert (format "%d " x))))
220         (progn
221           (setq new-list (reverse (split-string bs)))
222           (dolist (x new-list)
223             (setq x (string-to-int x))
224             (insert (format "%c" (logxor x passphrase-sum))))))
225       (buffer-substring-no-properties (point-min) (point-max)))))
226
227 (defun encrypt-gpg-encode-buffer (passphrase cipher)
228   (encrypt-gpg-process-buffer passphrase cipher t))
229
230 (defun encrypt-gpg-decode-buffer (passphrase cipher)
231   (encrypt-gpg-process-buffer passphrase cipher nil))
232
233 (defun encrypt-gpg-process-buffer (passphrase 
234                                         cipher 
235                                         &optional encode)
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) 
240                          'make-temp-file 
241                        'make-temp-name))
242          (temp-file (funcall temp-maker encrypt-temp-prefix))
243          (default-enable-multibyte-characters nil)
244          (args `("--cipher-algo" ,cipher
245                  "--status-fd" "2"
246                  "--logger-fd" "2"
247                  "--passphrase-fd" "0"
248                  "--no-tty"))
249          exit-status exit-data)
250     
251     (when encode
252       (setq args
253             (append args
254                     '("--symmetric"
255                       "--armor"))))
256
257     (if program
258         (with-temp-buffer
259           (when passphrase
260             (insert passphrase "\n"))
261           (insert input)
262           (setq exit-status
263                 (apply #'call-process-region (point-min) (point-max) program
264                        t `(t ,temp-file) nil args))
265           (if (equal exit-status 0)
266               (setq exit-data
267                     (buffer-substring-no-properties (point-min) (point-max)))
268             (with-temp-buffer
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."))
275     exit-data))
276
277 (provide 'encrypt)
278 ;;; encrypt.el ends here
279
280 ;; arch-tag: d907e4f1-71b5-42b1-a180-fc7b84ff0648