X-Git-Url: https://git.distorted.org.uk/~mdw/become/blobdiff_plain/03f996bd8a0d6391518979cdab3dbe38cba0bf83..f60a34341fee6aafd5b878dce23b80af7c60064d:/src/icrypt.c diff --git a/src/icrypt.c b/src/icrypt.c deleted file mode 100644 index 589c5a5..0000000 --- a/src/icrypt.c +++ /dev/null @@ -1,321 +0,0 @@ -/* -*-c-*- - * - * $Id: icrypt.c,v 1.2 1997/08/04 10:24:22 mdw Exp $ - * - * Higher level IDEA encryption - * - * (c) 1997 Mark Wooding - */ - -/*----- Licensing notice --------------------------------------------------* - * - * This file is part of `become' - * - * `Become' is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * `Become' is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with `become'; if not, write to the Free Software Foundation, - * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - */ - -/*----- Revision history --------------------------------------------------* - * - * $Log: icrypt.c,v $ - * Revision 1.2 1997/08/04 10:24:22 mdw - * Sources placed under CVS control. - * - * Revision 1.1 1997/07/21 13:47:49 mdw - * Initial revision - * - */ - -/*----- Header files ------------------------------------------------------*/ - -/* --- ANSI headers --- */ - -#include -#include -#include - -/* --- Local headers --- */ - -#include "config.h" -#include "icrypt.h" -#include "idea.h" - -/*----- Main code ---------------------------------------------------------*/ - -/* --- @icrypt_init@ --- * - * - * Arguments: @icrypt_job *j@ = pointer to job context block - * @unsigned char *k@ = pointer to key data - * @const unsigned char *iv@ = pointer to IV - * - * Returns: --- - * - * Use: Primes the context block ready for encryption. - */ - -void icrypt_init(icrypt_job *j, unsigned char *k, const unsigned char *iv) -{ - idea_ekeys(&j->k, k); - if (iv) - memcpy(j->iv, iv, IDEA_BLKSIZE); - else - memset(j->iv, 0, IDEA_BLKSIZE); - j->i = 8; -} - -/* --- @icrypt_encrypt@ --- * - * - * Arguments: @icrypt_job *j@ = job handle - * @const void *src@ = pointer to source buffer - * @void *dest@ = pointer to destination buffer - * @size_t sz@ = size of buffers to handle - * - * Returns: --- - * - * Use: Encrypts data from the source to the destination, using the - * key attached to the job handle. - */ - -void icrypt_encrypt(icrypt_job *j, const void *src, void *dest, size_t sz) -{ - const char *s = src; - char *d = dest; - int i; - - /* --- First, use up bytes in the buffer --- */ - - while (j->i < IDEA_BLKSIZE && sz > 0) { - *d++ = j->iv[j->i++] ^= *s++; - sz--; - } - if (!sz) return; - - /* --- Now encrypt larger chunks at a time --- */ - - while (sz >= IDEA_BLKSIZE) { - - /* --- Freshen the IV --- */ - - idea_encrypt(&j->k, j->iv, j->iv); - - /* --- Now encrypt some more bytes --- */ - - for (i = 0; i < IDEA_BLKSIZE; i++) { - *d++ = j->iv[i] ^= *s++; - } - sz -= IDEA_BLKSIZE; - } - if (!sz) return; - - /* --- Do the tail-end bits --- */ - - idea_encrypt(&j->k, j->iv, j->iv); - j->i = 0; - while (sz) { - *d++ = j->iv[j->i++] ^= *s++; - sz--; - } -} - -/* --- @icrypt_decrypt@ --- * - * - * Arguments: @icrypt_job *j@ = job handle - * @const void *src@ = pointer to source buffer - * @void *dest@ = pointer to destination buffer - * @size_t sz@ = size of buffers to handle - * - * Returns: --- - * - * Use: Decrypts data from the source to the destination, using - * the key attached to the job handle. - */ - -void icrypt_decrypt(icrypt_job *j, const void *src, void *dest, size_t sz) -{ - const char *s = src; - char *d = dest; - int i; - char c; - - /* --- First, use up bytes in the buffer --- */ - - while (j->i < IDEA_BLKSIZE && sz > 0) { - c = *s++; - *d++ = j->iv[j->i] ^ c; - j->iv[j->i++] = c; - sz--; - } - if (!sz) return; - - /* --- Now encrypt larger chunks at a time --- */ - - while (sz >= IDEA_BLKSIZE) { - - /* --- Freshen the IV --- */ - - idea_encrypt(&j->k, j->iv, j->iv); - - /* --- Now encrypt some more bytes --- */ - - for (i = 0; i < IDEA_BLKSIZE; i++) { - c = *s++; - *d++ = j->iv[i] ^ c; - j->iv[i] = c; - } - sz -= IDEA_BLKSIZE; - } - if (!sz) return; - - /* --- Do the tail-end bits --- */ - - idea_encrypt(&j->k, j->iv, j->iv); - j->i = 0; - while (sz) { - c = *s++; - *d++ = j->iv[j->i] ^ c; - j->iv[j->i++] = c; - sz--; - } -} - -/* --- @icrypt_reset@ --- * - * - * Arguments: @icrypt_job *j@ = pointer to job context block - * @unsigned char *k@ = pointer to key data, or zero for - * no change - * @const unsigned char *iv@ = pointer to IV, or zero - * - * Returns: --- - * - * Use: Alters the context block. This can be used after recovery - * of a session key, for example. - */ - -void icrypt_reset(icrypt_job *j, unsigned char *k, const unsigned char *iv) -{ - if (k) - idea_ekeys(&j->k, k); - if (iv) - memcpy(j->iv, iv, IDEA_BLKSIZE); - else { - unsigned char b[IDEA_BLKSIZE]; - int n = j->i, o = IDEA_BLKSIZE - j->i; - - memcpy(b, j->iv, sizeof(b)); - memcpy(j->iv, b + n, o); - memcpy(j->iv + o, b, n); - } - j->i = 8; -} - -/* --- @icrypt_saveIV@ --- * - * - * Arguments: @icrypt_job *j@ = pointer to job context block - * @unsigned char *iv@ = where to store the IV - * - * Returns: --- - * - * Use: Writes out the job's IV after munging it a little. - */ - -void icrypt_saveIV(icrypt_job *j, unsigned char *iv) -{ - int n = j->i, o = IDEA_BLKSIZE - j->i; - - memcpy(j->iv, iv + n, o); - memcpy(j->iv + o, iv, n); - idea_encrypt(&j->k, iv, iv); -} - -/*----- Test rig ----------------------------------------------------------*/ - -#ifdef TEST_RIG - -#include -#include -#include -#include "mdwopt.h" -#include "md5.h" -#include "utils.h" - -void icrypt(icrypt_job *j, - void (*proc)(icrypt_job *j, - const void *src, - void *dest, - size_t sz), - FILE *fp) -{ - char buff[71]; - size_t r; - - for (;;) { - r = fread(buff, 1, sizeof(buff), fp); - if (!r) break; - proc(j, buff, buff, r); - fwrite(buff, 1, r, stdout); - } -} - -int main(int argc, char *argv[]) -{ - icrypt_job j; - md5 md; - void (*proc)(icrypt_job *j, - const void *src, - void *dest, - size_t sz) = icrypt_encrypt; - - ego(argv[0]); - - for (;;) { - int i = getopt(argc, argv, "d"); - if (i < 0) - break; - if (i == 'd') - proc = icrypt_decrypt; - } - - { - char *pass = getpass("Password: "); - unsigned char k[IDEA_KEYSIZE]; - md5_init(&md); - md5_buffer(&md, pass, strlen(pass)); - memset(pass, 0, strlen(pass)); - md5_final(&md, k); - - icrypt_init(&j, k, 0); - } - - if (optind >= argc) - icrypt(&j, proc, stdin); - - while (optind < argc) { - char *p = argv[optind++]; - if (strcmp(p, "-") == 0) - icrypt(&j, proc, stdin); - else { - FILE *fp = fopen(p, "rb"); - if (!fp) - die("couldn't open `%s': %s", p, strerror(errno)); - icrypt(&j, proc, fp); - fclose(fp); - } - } - - return (0); -} - -#endif - -/*----- That's all, folks -------------------------------------------------*/