Blowfish Algorithm
Blowfish Algorithm
Blowfish Algorithm
SEARCH IP NEWS INDUSTRY ARTICLES BLOGS VIDEOS SLIDES EVENTS Search Industry Articles
reprogrammed during normal use, enabling them to eliminate bugs and add Reducing Debug time for Scan pattern
new features as firmware upgrades become available. using Parallel Strobe Data (PSD) Flow
Most credible encryption algorithms are published and freely available for
analysis, because it's the security of the key that actually makes the E-mail This Article Printer-Friendly Page
algorithm secure. A good encryption algorithm is like a good bank vault:
even with complete plans for the vault, the best tools, and example vaults to
practice on, you won't get inside the real thing without the key.
Now let's say that a server wants to send a firmware upgrade to a device
and wants to be sure that the code isn't intercepted and modified during
transit. The firmware upgrade may be delivered over a network connection,
but could just as easily be delivered via a CD-ROM. In any case, the server
first encrypts the firmware upgrade with its private RSA key, and then sends
it to the device. The recipient decrypts the message with the server's public
key, which was perhaps programmed into the device during manufacture. If
the firmware upgrade is successfully decrypted, in other words a checksum
of the image equals a known value, or the machine instructions look valid,
the firmware upgrade is considered authentic.
Blowfish is public domain, and was designed by Bruce Schneier expressly for
use in performance-constrained environments such as embedded systems.
[3] It has been extensively analyzed and deemed "reasonably secure" by the
cryptographic community. Implementation examples are available from
several sources, including the one by Paul Kocher that's excerpted in this
article as Listing 1. (The complete code is available for download at
ftp://ftp.embedded.com/pub/2003/08blowfish.)
/*
Blowfish algorithm. Written 1997 by Paul Kocher
(paul@cryptography.com).
This code and the algorithm are in the0 public domain.
*/
typedef struct {
uint32_t P[16 + 2];
uint32_t S[4][256];
} BLOWFISH_CTX;
unsigned long
F(BLOWFISH_CTX *ctx, uint32_t x)
{
uint16_t a, b, c, d;
uint32_t y;
d = x & 0x00FF;
x >>= 8;
c = x & 0x00FF;
x >>= 8;
b = x & 0x00FF;
x >>= 8;
a = x & 0x00FF;
y = ctx->S[0][a] + ctx->S[1][b];
y = y ^ ctx->S[2][c];
y = y + ctx->S[3][d];
return y;
}
void
Blowfish_Encrypt(BLOWFISH_CTX *ctx, uint32_t *xl, uint32_t *xr)
{
uint32_t Xl;
uint32_t Xr;
uint32_t temp;
int ii;
Xl = *xl;
Xr = *xr;
temp = Xl;
Xl = Xr;
Xr = temp;
}
temp = Xl;
Xl = Xr;
Xr = temp;
Xr = Xr ^ ctx->P[N];
Xl = Xl ^ ctx->P[N + 1];
*xl = Xl;
*xr = Xr;
}
void
Blowfish_Decrypt(BLOWFISH_CTX *ctx, uint32_t *xl, uint32_t *xr)
{
uint32_t Xl;
uint32_t Xr;
uint32_t temp;
int ii;
Xl = *xl;
Xr = *xr;
temp = Xl;
Xl = Xr;
Xr = temp;
}
temp = Xl;
Xl = Xr;
Xr = temp;
Xr = Xr ^ ctx->P[1];
Xl = Xl ^ ctx->P[0];
*xl = Xl;
*xr = Xr;
}
void
Blowfish_Init(BLOWFISH_CTX *ctx, uint16_t *key, int KeyLen)
{
uint32_t Xl;
{
int i, j, k;
uint32_t data, datal, datar;
j = 0;
for (i = 0; i < N + 2; ++i)
{
data = 0x00000000;
for (k = 0; k < 4; ++k)
{
data = (data << 8) | key[j];
j = j + 1;
if (j >= keyLen) j = 0;
}
ctx->P[i] = ORIG_P[i] ^ data;
}
datal = 0x00000000;
datar = 0x00000000;
for (i = 0; i < N + 2; i += 2)
{
Blowfish_Encrypt(ctx, &datal, &datar);
ctx->P[i] = datal;
ctx->P[i + 1] = datar;
}
int
Blowfish_Test(BLOWFISH_CTX *ctx)
{
uint32_t L = 1, R = 2;
The P-array and S-array values used by Blowfish are precomputed based on
the user's key. In effect, the user's key is transformed into the P-array and
S-array; the key itself may be discarded after the transformation. The P-
array and S-array need not be recomputed (as long as the key doesn't
change), but must remain secret.
I'll refer you to the source code for computing the P and S arrays and only
briefly summarize the procedure as follows:
#include <stdio.h>
#include <string.h>
int
main (void)
{
BLOWFISH_CTX ctx;
int n;
uint8_t ciphertext_buffer[256];
uint8_t *ciphertext_string = &ciphertext_buffer[0];
int ciphertext_len = 0;
uint32_t message_left;
uint32_t message_right;
int block_len;
#if 1
/* sanity test, encrypts a known message */
n = Blowfish_Test(&ctx);
printf("Blowfish_Test returned: %d.%s\n", n, n ? " Abort." : "");
if (n) return n;
#endif
while (plaintext_len)
{
message_left = message_right = 0UL;
/* crack the message string into a 64-bit block (ok, really two 32-bit
blocks); pad with zeros if necessary */
for (block_len = 0; block_len < 4; block_len++)
{
message_left = message_left << 8;
if (plaintext_len)
{
message_left += *plaintext_string++;
plaintext_len--;
}
else message_left += 0;
}
for (block_len = 0; block_len < 4; block_len++)
{
message_right = message_right << 8;
if (plaintext_len)
{
message_right += *plaintext_string++;
plaintext_len--;
}
else message_right += 0;
}
/* encrypt and print the results */
Blowfish_Encrypt(&ctx, &message_left, &message_right);
printf("%lx%lx", message_left, message_right);
ciphertext_string = &ciphertext_buffer[0];
while(ciphertext_len)
{
message_left = message_right = 0UL;
printf("%c%c%c%c%c%c%c%c",
(int)(message_left >> 24), (int)(message_left >> 16),
(int)(message_left >> 8), (int)(message_left),
(int)(message_right >> 24), (int)(message_right >> 16),
(int)(message_right >> 8), (int)(message_right));
}
printf("\n");
return 0;
}
Now is a good time to start thinking about adding data integrity and privacy
capabilities to your embedded system. The Blowfish algorithm is an excellent
choice for encryption, since it's lightweight, public domain, and considered
secure even after extensive analysis.
End notes:
1. Not an actual log per se, but so-called ephemerides information that
allows the device to find GPS transmitters without doing a time-
consuming search of the entire GPS spectrum. Such information can
also be used to pinpoint the receiver's location at a previous point in
time. Because of this capability, GPS receivers are routinely collected
and analyzed during searches by law enforcement. A digital signature
would authenticate the ephimeride, verifying that it hadn't been
tampered with or rendered invalid before being used as evidence.
Back
2. In the U.S., commercial automotive systems do this to prevent warranty
claims for user-damaged hardware; in Europe, it's to prevent speeding.
Back
3. Schneier, Bruce. Applied Cryptography: Protocols, Algorithms, and
Source Code in C, Second Edition. New York, NY: John Wiley & Sons,
1995.
Back
Partner with us List your Products Design-Reuse.com © 2021 Design And Reuse
Suppliers, list your IPs for free. Contact Us All Rights Reserved.
About us
D&R Partner Program No portion of this site may be copied, retransmitted,
Advertise with Us reposted, duplicated or otherwise used without the
Partner with us List your Products Privacy Policy express written permission of Design And Reuse.