| 1 | /********************************************************************* |
| 2 | * Filename: blowfish_test.c |
| 3 | * Author: Brad Conte (brad AT bradconte.com) |
| 4 | * Copyright: |
| 5 | * Disclaimer: This code is presented "as is" without any guarantees. |
| 6 | * Details: Performs known-answer tests on the corresponding Blowfish |
| 7 | implementation. These tests do not encompass the full |
| 8 | range of available test vectors, however, if the tests |
| 9 | pass it is very, very likely that the code is correct |
| 10 | and was compiled properly. This code also serves as |
| 11 | example usage of the functions. |
| 12 | *********************************************************************/ |
| 13 | |
| 14 | /*************************** HEADER FILES ***************************/ |
| 15 | #include <stdio.h> |
| 16 | #include <memory.h> |
| 17 | #include "blowfish.h" |
| 18 | |
| 19 | /*********************** FUNCTION DEFINITIONS ***********************/ |
| 20 | int blowfish_test() |
| 21 | { |
| 22 | BYTE key1[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; |
| 23 | BYTE key2[8] = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff}; |
| 24 | BYTE key3[24] = {0xF0,0xE1,0xD2,0xC3,0xB4,0xA5,0x96,0x87, |
| 25 | 0x78,0x69,0x5A,0x4B,0x3C,0x2D,0x1E,0x0F, |
| 26 | 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77}; |
| 27 | BYTE p1[BLOWFISH_BLOCK_SIZE] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; |
| 28 | BYTE p2[BLOWFISH_BLOCK_SIZE] = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff}; |
| 29 | BYTE p3[BLOWFISH_BLOCK_SIZE] = {0xFE,0xDC,0xBA,0x98,0x76,0x54,0x32,0x10}; |
| 30 | |
| 31 | BYTE c1[BLOWFISH_BLOCK_SIZE] = {0x4e,0xf9,0x97,0x45,0x61,0x98,0xdd,0x78}; |
| 32 | BYTE c2[BLOWFISH_BLOCK_SIZE] = {0x51,0x86,0x6f,0xd5,0xb8,0x5e,0xcb,0x8a}; |
| 33 | BYTE c3[BLOWFISH_BLOCK_SIZE] = {0x05,0x04,0x4b,0x62,0xfa,0x52,0xd0,0x80}; |
| 34 | |
| 35 | BYTE enc_buf[BLOWFISH_BLOCK_SIZE]; |
| 36 | BLOWFISH_KEY key; |
| 37 | int pass = 1; |
| 38 | |
| 39 | // Test vector 1. |
| 40 | blowfish_key_setup(key1, &key, BLOWFISH_BLOCK_SIZE); |
| 41 | blowfish_encrypt(p1, enc_buf, &key); |
| 42 | pass = pass && !memcmp(c1, enc_buf, BLOWFISH_BLOCK_SIZE); |
| 43 | blowfish_decrypt(c1, enc_buf, &key); |
| 44 | pass = pass && !memcmp(p1, enc_buf, BLOWFISH_BLOCK_SIZE); |
| 45 | |
| 46 | // Test vector 2. |
| 47 | blowfish_key_setup(key2, &key, BLOWFISH_BLOCK_SIZE); |
| 48 | blowfish_encrypt(p2, enc_buf, &key); |
| 49 | pass = pass && !memcmp(c2, enc_buf, BLOWFISH_BLOCK_SIZE); |
| 50 | blowfish_decrypt(c2, enc_buf, &key); |
| 51 | pass = pass && !memcmp(p2, enc_buf, BLOWFISH_BLOCK_SIZE); |
| 52 | |
| 53 | // Test vector 3. |
| 54 | blowfish_key_setup(key3, &key, 24); |
| 55 | blowfish_encrypt(p3, enc_buf, &key); |
| 56 | pass = pass && !memcmp(c3, enc_buf, BLOWFISH_BLOCK_SIZE); |
| 57 | blowfish_decrypt(c3, enc_buf, &key); |
| 58 | pass = pass && !memcmp(p3, enc_buf, BLOWFISH_BLOCK_SIZE); |
| 59 | |
| 60 | return(pass); |
| 61 | } |
| 62 | |
| 63 | int main() |
| 64 | { |
| 65 | printf("Blowfish tests: %s\n", blowfish_test() ? "SUCCEEDED" : "FAILED"); |
| 66 | |
| 67 | return(0); |
| 68 | } |