3 * $Id: icrypt.c,v 1.2 1997/08/04 10:24:22 mdw Exp $
5 * Higher level IDEA encryption
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.2 1997/08/04 10:24:22 mdw
33 * Sources placed under CVS control.
35 * Revision 1.1 1997/07/21 13:47:49 mdw
40 /*----- Header files ------------------------------------------------------*/
42 /* --- ANSI headers --- */
48 /* --- Local headers --- */
54 /*----- Main code ---------------------------------------------------------*/
56 /* --- @icrypt_init@ --- *
58 * Arguments: @icrypt_job *j@ = pointer to job context block
59 * @unsigned char *k@ = pointer to key data
60 * @const unsigned char *iv@ = pointer to IV
64 * Use: Primes the context block ready for encryption.
67 void icrypt_init(icrypt_job
*j
, unsigned char *k
, const unsigned char *iv
)
71 memcpy(j
->iv
, iv
, IDEA_BLKSIZE
);
73 memset(j
->iv
, 0, IDEA_BLKSIZE
);
77 /* --- @icrypt_encrypt@ --- *
79 * Arguments: @icrypt_job *j@ = job handle
80 * @const void *src@ = pointer to source buffer
81 * @void *dest@ = pointer to destination buffer
82 * @size_t sz@ = size of buffers to handle
86 * Use: Encrypts data from the source to the destination, using the
87 * key attached to the job handle.
90 void icrypt_encrypt(icrypt_job
*j
, const void *src
, void *dest
, size_t sz
)
96 /* --- First, use up bytes in the buffer --- */
98 while (j
->i
< IDEA_BLKSIZE
&& sz
> 0) {
99 *d
++ = j
->iv
[j
->i
++] ^= *s
++;
104 /* --- Now encrypt larger chunks at a time --- */
106 while (sz
>= IDEA_BLKSIZE
) {
108 /* --- Freshen the IV --- */
110 idea_encrypt(&j
->k
, j
->iv
, j
->iv
);
112 /* --- Now encrypt some more bytes --- */
114 for (i
= 0; i
< IDEA_BLKSIZE
; i
++) {
115 *d
++ = j
->iv
[i
] ^= *s
++;
121 /* --- Do the tail-end bits --- */
123 idea_encrypt(&j
->k
, j
->iv
, j
->iv
);
126 *d
++ = j
->iv
[j
->i
++] ^= *s
++;
131 /* --- @icrypt_decrypt@ --- *
133 * Arguments: @icrypt_job *j@ = job handle
134 * @const void *src@ = pointer to source buffer
135 * @void *dest@ = pointer to destination buffer
136 * @size_t sz@ = size of buffers to handle
140 * Use: Decrypts data from the source to the destination, using
141 * the key attached to the job handle.
144 void icrypt_decrypt(icrypt_job
*j
, const void *src
, void *dest
, size_t sz
)
151 /* --- First, use up bytes in the buffer --- */
153 while (j
->i
< IDEA_BLKSIZE
&& sz
> 0) {
155 *d
++ = j
->iv
[j
->i
] ^ c
;
161 /* --- Now encrypt larger chunks at a time --- */
163 while (sz
>= IDEA_BLKSIZE
) {
165 /* --- Freshen the IV --- */
167 idea_encrypt(&j
->k
, j
->iv
, j
->iv
);
169 /* --- Now encrypt some more bytes --- */
171 for (i
= 0; i
< IDEA_BLKSIZE
; i
++) {
180 /* --- Do the tail-end bits --- */
182 idea_encrypt(&j
->k
, j
->iv
, j
->iv
);
186 *d
++ = j
->iv
[j
->i
] ^ c
;
192 /* --- @icrypt_reset@ --- *
194 * Arguments: @icrypt_job *j@ = pointer to job context block
195 * @unsigned char *k@ = pointer to key data, or zero for
197 * @const unsigned char *iv@ = pointer to IV, or zero
201 * Use: Alters the context block. This can be used after recovery
202 * of a session key, for example.
205 void icrypt_reset(icrypt_job
*j
, unsigned char *k
, const unsigned char *iv
)
208 idea_ekeys(&j
->k
, k
);
210 memcpy(j
->iv
, iv
, IDEA_BLKSIZE
);
212 unsigned char b
[IDEA_BLKSIZE
];
213 int n
= j
->i
, o
= IDEA_BLKSIZE
- j
->i
;
215 memcpy(b
, j
->iv
, sizeof(b
));
216 memcpy(j
->iv
, b
+ n
, o
);
217 memcpy(j
->iv
+ o
, b
, n
);
222 /* --- @icrypt_saveIV@ --- *
224 * Arguments: @icrypt_job *j@ = pointer to job context block
225 * @unsigned char *iv@ = where to store the IV
229 * Use: Writes out the job's IV after munging it a little.
232 void icrypt_saveIV(icrypt_job
*j
, unsigned char *iv
)
234 int n
= j
->i
, o
= IDEA_BLKSIZE
- j
->i
;
236 memcpy(j
->iv
, iv
+ n
, o
);
237 memcpy(j
->iv
+ o
, iv
, n
);
238 idea_encrypt(&j
->k
, iv
, iv
);
241 /*----- Test rig ----------------------------------------------------------*/
252 void icrypt(icrypt_job
*j
,
253 void (*proc
)(icrypt_job
*j
,
263 r
= fread(buff
, 1, sizeof(buff
), fp
);
265 proc(j
, buff
, buff
, r
);
266 fwrite(buff
, 1, r
, stdout
);
270 int main(int argc
, char *argv
[])
274 void (*proc
)(icrypt_job
*j
,
277 size_t sz
) = icrypt_encrypt
;
282 int i
= getopt(argc
, argv
, "d");
286 proc
= icrypt_decrypt
;
290 char *pass
= getpass("Password: ");
291 unsigned char k
[IDEA_KEYSIZE
];
293 md5_buffer(&md
, pass
, strlen(pass
));
294 memset(pass
, 0, strlen(pass
));
297 icrypt_init(&j
, k
, 0);
301 icrypt(&j
, proc
, stdin
);
303 while (optind
< argc
) {
304 char *p
= argv
[optind
++];
305 if (strcmp(p
, "-") == 0)
306 icrypt(&j
, proc
, stdin
);
308 FILE *fp
= fopen(p
, "rb");
310 die("couldn't open `%s': %s", p
, strerror(errno
));
311 icrypt(&j
, proc
, fp
);
321 /*----- That's all, folks -------------------------------------------------*/