/* -*-c-*-
*
- * $Id: icrypt.c,v 1.2 1997/08/04 10:24:22 mdw Exp $
+ * $Id: icrypt.c,v 1.2.2.1 1997/09/26 09:08:07 mdw Exp $
*
- * Higher level IDEA encryption
+ * Higher level encryption functions
*
* (c) 1997 Mark Wooding
*/
/*----- Revision history --------------------------------------------------*
*
* $Log: icrypt.c,v $
+ * Revision 1.2.2.1 1997/09/26 09:08:07 mdw
+ * Use the Blowfish encryption algorithm instead of IDEA. This is partly
+ * because I prefer Blowfish (without any particularly strong evidence) but
+ * mainly because IDEA is patented and Blowfish isn't.
+ *
* Revision 1.2 1997/08/04 10:24:22 mdw
* Sources placed under CVS control.
*
/* --- Local headers --- */
+#include "blowfish.h"
#include "config.h"
#include "icrypt.h"
-#include "idea.h"
/*----- Main code ---------------------------------------------------------*/
*
* Arguments: @icrypt_job *j@ = pointer to job context block
* @unsigned char *k@ = pointer to key data
+ * @size_t sz@ = size of the 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)
+void icrypt_init(icrypt_job *j, unsigned char *k,
+ size_t sz, const unsigned char *iv)
{
- idea_ekeys(&j->k, k);
+ blowfish_setKey(&j->k, k, sz);
if (iv)
- memcpy(j->iv, iv, IDEA_BLKSIZE);
+ memcpy(j->iv, iv, BLOWFISH_BLKSIZE);
else
- memset(j->iv, 0, IDEA_BLKSIZE);
+ memset(j->iv, 0, BLOWFISH_BLKSIZE);
j->i = 8;
}
void icrypt_encrypt(icrypt_job *j, const void *src, void *dest, size_t sz)
{
- const char *s = src;
- char *d = dest;
+ const unsigned char *s = src;
+ unsigned char *d = dest;
int i;
/* --- First, use up bytes in the buffer --- */
- while (j->i < IDEA_BLKSIZE && sz > 0) {
+ while (j->i < BLOWFISH_BLKSIZE && sz > 0) {
*d++ = j->iv[j->i++] ^= *s++;
sz--;
}
/* --- Now encrypt larger chunks at a time --- */
- while (sz >= IDEA_BLKSIZE) {
+ while (sz >= BLOWFISH_BLKSIZE) {
/* --- Freshen the IV --- */
- idea_encrypt(&j->k, j->iv, j->iv);
+ blowfish_encrypt(&j->k, j->iv, j->iv);
/* --- Now encrypt some more bytes --- */
- for (i = 0; i < IDEA_BLKSIZE; i++) {
+ for (i = 0; i < BLOWFISH_BLKSIZE; i++) {
*d++ = j->iv[i] ^= *s++;
}
- sz -= IDEA_BLKSIZE;
+ sz -= BLOWFISH_BLKSIZE;
}
if (!sz) return;
/* --- Do the tail-end bits --- */
- idea_encrypt(&j->k, j->iv, j->iv);
+ blowfish_encrypt(&j->k, j->iv, j->iv);
j->i = 0;
while (sz) {
*d++ = j->iv[j->i++] ^= *s++;
void icrypt_decrypt(icrypt_job *j, const void *src, void *dest, size_t sz)
{
- const char *s = src;
- char *d = dest;
+ unsigned const char *s = src;
+ unsigned char *d = dest;
int i;
- char c;
+ unsigned char c;
/* --- First, use up bytes in the buffer --- */
- while (j->i < IDEA_BLKSIZE && sz > 0) {
+ while (j->i < BLOWFISH_BLKSIZE && sz > 0) {
c = *s++;
*d++ = j->iv[j->i] ^ c;
j->iv[j->i++] = c;
/* --- Now encrypt larger chunks at a time --- */
- while (sz >= IDEA_BLKSIZE) {
+ while (sz >= BLOWFISH_BLKSIZE) {
/* --- Freshen the IV --- */
- idea_encrypt(&j->k, j->iv, j->iv);
+ blowfish_encrypt(&j->k, j->iv, j->iv);
/* --- Now encrypt some more bytes --- */
- for (i = 0; i < IDEA_BLKSIZE; i++) {
+ for (i = 0; i < BLOWFISH_BLKSIZE; i++) {
c = *s++;
*d++ = j->iv[i] ^ c;
j->iv[i] = c;
}
- sz -= IDEA_BLKSIZE;
+ sz -= BLOWFISH_BLKSIZE;
}
if (!sz) return;
/* --- Do the tail-end bits --- */
- idea_encrypt(&j->k, j->iv, j->iv);
+ blowfish_encrypt(&j->k, j->iv, j->iv);
j->i = 0;
while (sz) {
c = *s++;
* Arguments: @icrypt_job *j@ = pointer to job context block
* @unsigned char *k@ = pointer to key data, or zero for
* no change
+ * @size_t sz@ = size of the key in bytes
* @const unsigned char *iv@ = pointer to IV, or zero
*
* Returns: ---
* of a session key, for example.
*/
-void icrypt_reset(icrypt_job *j, unsigned char *k, const unsigned char *iv)
+void icrypt_reset(icrypt_job *j, unsigned char *k,
+ size_t sz, const unsigned char *iv)
{
if (k)
- idea_ekeys(&j->k, k);
+ blowfish_setKey(&j->k, k, sz);
if (iv)
- memcpy(j->iv, iv, IDEA_BLKSIZE);
+ memcpy(j->iv, iv, BLOWFISH_BLKSIZE);
else {
- unsigned char b[IDEA_BLKSIZE];
- int n = j->i, o = IDEA_BLKSIZE - j->i;
+ unsigned char b[BLOWFISH_BLKSIZE];
+ int n = j->i, o = BLOWFISH_BLKSIZE - j->i;
memcpy(b, j->iv, sizeof(b));
memcpy(j->iv, b + n, o);
void icrypt_saveIV(icrypt_job *j, unsigned char *iv)
{
- int n = j->i, o = IDEA_BLKSIZE - j->i;
+ int n = j->i, o = BLOWFISH_BLKSIZE - j->i;
memcpy(j->iv, iv + n, o);
memcpy(j->iv + o, iv, n);
- idea_encrypt(&j->k, iv, iv);
+ blowfish_encrypt(&j->k, iv, iv);
}
/*----- Test rig ----------------------------------------------------------*/
#include <errno.h>
#include <pwd.h>
#include <unistd.h>
+#include "crypt.h"
#include "mdwopt.h"
#include "md5.h"
#include "utils.h"
{
char *pass = getpass("Password: ");
- unsigned char k[IDEA_KEYSIZE];
+ unsigned char k[BLOWFISH_KEYSIZE];
md5_init(&md);
md5_buffer(&md, pass, strlen(pass));
memset(pass, 0, strlen(pass));
md5_final(&md, k);
- icrypt_init(&j, k, 0);
+ icrypt_init(&j, k, BLOWFISH_KEYSIZE, 0);
}
if (optind >= argc)