File src/blowfish.c

blowfish.c: C implementation of the Blowfish algorithm.


Copyright (C) 1997 by Paul Kocher


This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA








COMMENTS ON USING THIS CODE:


Normal usage is as follows:
[1] Allocate a BLOWFISH_CTX. (It may be too big for the stack.)
[2] Call Blowfish_Init with a pointer to your BLOWFISH_CTX, a pointer to
the key, and the number of bytes in the key.
[3] To encrypt a 64-bit block, call Blowfish_Encrypt with a pointer to
BLOWFISH_CTX, a pointer to the 32-bit left half of the plaintext
and a pointer to the 32-bit right half. The plaintext will be
overwritten with the ciphertext.
[4] Decryption is the same as encryption except that the plaintext and
ciphertext are reversed.


Warning #1: The code does not check key lengths. (Caveat encryptor.)
Warning #2: Beware that Blowfish keys repeat such that "ab" = "abab".
Warning #3: It is normally a good idea to zeroize the BLOWFISH_CTX before
freeing it.
Warning #4: Endianness conversions are the responsibility of the caller.
(To encrypt bytes on a little-endian platforms, you'll probably want
to swap bytes around instead of just casting.)
Warning #5: Make sure to use a reasonable mode of operation for your
application. (If you don't know what CBC mode is, see Warning #7.)
Warning #6: This code is susceptible to timing attacks.
Warning #7: Security engineering is risky and non-intuitive. Have someone
check your work. If you don't know what you are doing, get help.




This is code is fast enough for most applications, but is not optimized for
speed.


If you require this code under a license other than LGPL, please ask. (I
can be located using your favorite search engine.) Unfortunately, I do not
have time to provide unpaid support for everyone who uses this code.


-- Paul Kocher


Included Files


Preprocessor definitions

#define _LONGLONG 1

#define N 16


Local Variables

ORIG_P
static const unsigned long ORIG_P[16+2]
Used in: Blowfish_Init()  

ORIG_S
static const unsigned long ORIG_S[4][256]

Used in: Blowfish_Init()  

Global Function Blowfish_Decrypt()

void Blowfish_Decrypt ( BLOWFISH_CTX* ctx, unsigned long* xl, unsigned long* xr )
Prototyped in: src/blowfish.h
Calls: F() src/blowfish.c
Called by: decrypt_fish() src/dinoex_jobs.c

Global Function Blowfish_Encrypt()

void Blowfish_Encrypt ( BLOWFISH_CTX* ctx, unsigned long* xl, unsigned long* xr )
Prototyped in: src/blowfish.h
Calls: F() src/blowfish.c
Called by: Blowfish_Init() src/blowfish.c
  encrypt_fish() src/dinoex_jobs.c

Global Function Blowfish_Init()

void Blowfish_Init ( BLOWFISH_CTX* ctx, const unsigned char* key, int keyLen )
Prototyped in: src/blowfish.h
Calls: Blowfish_Encrypt() src/blowfish.c
Called by: decrypt_fish() src/dinoex_jobs.c
  encrypt_fish() src/dinoex_jobs.c
References Variables: ORIG_P src/blowfish.c
  ORIG_S src/blowfish.c

Local Function F()

static unsigned long F ( BLOWFISH_CTX* ctx, unsigned long x )
Called by: Blowfish_Decrypt() src/blowfish.c
  Blowfish_Encrypt() src/blowfish.c