3 * $Id: icrypt.c,v 1.3 1997/09/26 09:14:58 mdw Exp $
5 * Higher level encryption functions
7 * (c) 1997 Mark Wooding
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of `become'
14 * `Become' is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * `Become' is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with `become'; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.3 1997/09/26 09:14:58 mdw
33 * Merged blowfish branch into trunk.
35 * Revision 1.2.2.1 1997/09/26 09:08:07 mdw
36 * Use the Blowfish encryption algorithm instead of IDEA. This is partly
37 * because I prefer Blowfish (without any particularly strong evidence) but
38 * mainly because IDEA is patented and Blowfish isn't.
40 * Revision 1.2 1997/08/04 10:24:22 mdw
41 * Sources placed under CVS control.
43 * Revision 1.1 1997/07/21 13:47:49 mdw
48 /*----- Header files ------------------------------------------------------*/
50 /* --- ANSI headers --- */
56 /* --- Local headers --- */
62 /*----- Main code ---------------------------------------------------------*/
64 /* --- @icrypt_init@ --- *
66 * Arguments: @icrypt_job *j@ = pointer to job context block
67 * @unsigned char *k@ = pointer to key data
68 * @size_t sz@ = size of the key data
69 * @const unsigned char *iv@ = pointer to IV
73 * Use: Primes the context block ready for encryption.
76 void icrypt_init(icrypt_job *j, unsigned char *k,
77 size_t sz, const unsigned char *iv)
79 blowfish_setKey(&j->k, k, sz);
81 memcpy(j->iv, iv, BLOWFISH_BLKSIZE);
83 memset(j->iv, 0, BLOWFISH_BLKSIZE);
87 /* --- @icrypt_encrypt@ --- *
89 * Arguments: @icrypt_job *j@ = job handle
90 * @const void *src@ = pointer to source buffer
91 * @void *dest@ = pointer to destination buffer
92 * @size_t sz@ = size of buffers to handle
96 * Use: Encrypts data from the source to the destination, using the
97 * key attached to the job handle.
100 void icrypt_encrypt(icrypt_job *j, const void *src, void *dest, size_t sz)
102 const unsigned char *s = src;
103 unsigned char *d = dest;
106 /* --- First, use up bytes in the buffer --- */
108 while (j->i < BLOWFISH_BLKSIZE && sz > 0) {
109 *d++ = j->iv[j->i++] ^= *s++;
114 /* --- Now encrypt larger chunks at a time --- */
116 while (sz >= BLOWFISH_BLKSIZE) {
118 /* --- Freshen the IV --- */
120 blowfish_encrypt(&j->k, j->iv, j->iv);
122 /* --- Now encrypt some more bytes --- */
124 for (i = 0; i < BLOWFISH_BLKSIZE; i++) {
125 *d++ = j->iv[i] ^= *s++;
127 sz -= BLOWFISH_BLKSIZE;
131 /* --- Do the tail-end bits --- */
133 blowfish_encrypt(&j->k, j->iv, j->iv);
136 *d++ = j->iv[j->i++] ^= *s++;
141 /* --- @icrypt_decrypt@ --- *
143 * Arguments: @icrypt_job *j@ = job handle
144 * @const void *src@ = pointer to source buffer
145 * @void *dest@ = pointer to destination buffer
146 * @size_t sz@ = size of buffers to handle
150 * Use: Decrypts data from the source to the destination, using
151 * the key attached to the job handle.
154 void icrypt_decrypt(icrypt_job *j, const void *src, void *dest, size_t sz)
156 unsigned const char *s = src;
157 unsigned char *d = dest;
161 /* --- First, use up bytes in the buffer --- */
163 while (j->i < BLOWFISH_BLKSIZE && sz > 0) {
165 *d++ = j->iv[j->i] ^ c;
171 /* --- Now encrypt larger chunks at a time --- */
173 while (sz >= BLOWFISH_BLKSIZE) {
175 /* --- Freshen the IV --- */
177 blowfish_encrypt(&j->k, j->iv, j->iv);
179 /* --- Now encrypt some more bytes --- */
181 for (i = 0; i < BLOWFISH_BLKSIZE; i++) {
186 sz -= BLOWFISH_BLKSIZE;
190 /* --- Do the tail-end bits --- */
192 blowfish_encrypt(&j->k, j->iv, j->iv);
196 *d++ = j->iv[j->i] ^ c;
202 /* --- @icrypt_reset@ --- *
204 * Arguments: @icrypt_job *j@ = pointer to job context block
205 * @unsigned char *k@ = pointer to key data, or zero for
207 * @size_t sz@ = size of the key in bytes
208 * @const unsigned char *iv@ = pointer to IV, or zero
212 * Use: Alters the context block. This can be used after recovery
213 * of a session key, for example.
216 void icrypt_reset(icrypt_job *j, unsigned char *k,
217 size_t sz, const unsigned char *iv)
220 blowfish_setKey(&j->k, k, sz);
222 memcpy(j->iv, iv, BLOWFISH_BLKSIZE);
224 unsigned char b[BLOWFISH_BLKSIZE];
225 int n = j->i, o = BLOWFISH_BLKSIZE - j->i;
227 memcpy(b, j->iv, sizeof(b));
228 memcpy(j->iv, b + n, o);
229 memcpy(j->iv + o, b, n);
234 /* --- @icrypt_saveIV@ --- *
236 * Arguments: @icrypt_job *j@ = pointer to job context block
237 * @unsigned char *iv@ = where to store the IV
241 * Use: Writes out the job's IV after munging it a little.
244 void icrypt_saveIV(icrypt_job *j, unsigned char *iv)
246 int n = j->i, o = BLOWFISH_BLKSIZE - j->i;
248 memcpy(j->iv, iv + n, o);
249 memcpy(j->iv + o, iv, n);
250 blowfish_encrypt(&j->k, iv, iv);
253 /*----- Test rig ----------------------------------------------------------*/
265 void icrypt(icrypt_job *j,
266 void (*proc)(icrypt_job *j,
276 r = fread(buff, 1, sizeof(buff), fp);
278 proc(j, buff, buff, r);
279 fwrite(buff, 1, r, stdout);
283 int main(int argc, char *argv[])
287 void (*proc)(icrypt_job *j,
290 size_t sz) = icrypt_encrypt;
295 int i = getopt(argc, argv, "d");
299 proc = icrypt_decrypt;
303 char *pass = getpass("Password: ");
304 unsigned char k[BLOWFISH_KEYSIZE];
306 md5_buffer(&md, pass, strlen(pass));
307 memset(pass, 0, strlen(pass));
310 icrypt_init(&j, k, BLOWFISH_KEYSIZE, 0);
314 icrypt(&j, proc, stdin);
316 while (optind < argc) {
317 char *p = argv[optind++];
318 if (strcmp(p, "-") == 0)
319 icrypt(&j, proc, stdin);
321 FILE *fp = fopen(p, "rb");
323 die("couldn't open `%s': %s", p, strerror(errno));
324 icrypt(&j, proc, fp);
334 /*----- That's all, folks -------------------------------------------------*/