3172cd817088568efe10d729b35cefe362cdc78b
[strongswan.git] / src / pluto / alg / ike_alg_blowfish.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stddef.h>
4 #include <sys/types.h>
5 #include <freeswan.h>
6
7 #include "constants.h"
8 #include "defs.h"
9 #include "log.h"
10 #include "libblowfish/blowfish.h"
11 #include "alg_info.h"
12 #include "ike_alg.h"
13
14 #define BLOWFISH_CBC_BLOCK_SIZE 8 /* block size */
15 #define BLOWFISH_KEY_MIN_LEN 128
16 #define BLOWFISH_KEY_MAX_LEN 448
17
18
19 /**
20 * Blowfish CBC encryption test vectors
21 */
22
23 /**
24 * Test vector by Eric Young
25 */
26
27 static const u_char bf_test0_key[] = {
28 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
29 0xF0, 0xE1, 0xD2, 0xC3, 0xB4, 0xA5, 0x96, 0x87
30 };
31
32 static const u_char bf_test0_iv[] = {
33 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
34 };
35
36 static const u_char bf_test0_plain[] = {
37 /* "7654321 Now is the time for " */
38 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31, 0x20,
39 0x4E, 0x6F, 0x77, 0x20, 0x69, 0x73, 0x20, 0x74,
40 0x68, 0x65, 0x20, 0x74, 0x69, 0x6D, 0x65, 0x20,
41 0x66, 0x6F, 0x72, 0x20, 0x00, 0x00, 0x00, 0x00
42 };
43
44 static const u_char bf_test0_cipher[] = {
45 0x6B, 0x77, 0xB4, 0xD6, 0x30, 0x06, 0xDE, 0xE6,
46 0x05, 0xB1, 0x56, 0xE2, 0x74, 0x03, 0x97, 0x93,
47 0x58, 0xDE, 0xB9, 0xE7, 0x15, 0x46, 0x16, 0xD9,
48 0x59, 0xF1, 0x65, 0x2B, 0xD5, 0xFF, 0x92, 0xCC
49 };
50
51 /**
52 * Test vector by Chilkat Software
53 * (www.chilkatsoft.com/p/php_blowfish.asp)
54 */
55
56 static const u_char bf_test1_key[] = {
57 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
58 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
59 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
60 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50
61 };
62
63 static const u_char bf_test1_iv[] = {
64 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38
65 };
66
67 static const u_char bf_test1_plain[] = {
68 /* "The quick brown fox jumped over the lazy dog" */
69 0x54, 0x68, 0x65, 0x20, 0x71, 0x75, 0x69, 0x63,
70 0x6b, 0x20, 0x62, 0x72, 0x6f, 0x77, 0x6e, 0x20,
71 0x66, 0x6f, 0x78, 0x20, 0x6a, 0x75, 0x6d, 0x70,
72 0x65, 0x64, 0x20, 0x6f, 0x76, 0x65, 0x72, 0x20,
73 0x74, 0x68, 0x65, 0x20, 0x6c, 0x61, 0x7a, 0x79,
74 0x20, 0x64, 0x6f, 0x67, 0x00, 0x00, 0x00, 0x00
75 };
76
77 static const u_char bf_test1_cipher[] = {
78 0x27, 0x68, 0x55, 0xca, 0x6c, 0x0d, 0x60, 0xf7,
79 0xd9, 0x70, 0x82, 0x10, 0x44, 0x0c, 0x10, 0x72,
80 0xe0, 0x5d, 0x07, 0x8e, 0x73, 0x3b, 0x34, 0xb4,
81 0x19, 0x8d, 0x60, 0x9d, 0xc2, 0xfc, 0xc2, 0xf0,
82 0xc3, 0x09, 0x26, 0xcd, 0xef, 0x3b, 0x6d, 0x52,
83 0xba, 0xf6, 0xe3, 0x45, 0xaa, 0x03, 0xf8, 0x3e
84 };
85
86 static const enc_testvector_t bf_enc_testvectors[] = {
87 { sizeof(bf_test0_key), bf_test0_key, bf_test0_iv,
88 sizeof(bf_test0_plain), bf_test0_plain, bf_test0_cipher },
89 { sizeof(bf_test1_key), bf_test1_key, bf_test1_iv,
90 sizeof(bf_test1_plain), bf_test1_plain, bf_test1_cipher },
91 { 0, NULL, NULL, 0, NULL, NULL }
92 };
93
94 static void
95 do_blowfish(u_int8_t *buf, size_t buf_len, u_int8_t *key, size_t key_size, u_int8_t *iv, bool enc)
96 {
97 BF_KEY bf_ctx;
98
99 BF_set_key(&bf_ctx, key_size , key);
100 BF_cbc_encrypt(buf, buf, buf_len, &bf_ctx, iv, enc);
101 }
102
103 struct encrypt_desc algo_blowfish =
104 {
105 algo_type: IKE_ALG_ENCRYPT,
106 algo_id: OAKLEY_BLOWFISH_CBC,
107 algo_next: NULL,
108 enc_ctxsize: sizeof(BF_KEY),
109 enc_blocksize: BLOWFISH_CBC_BLOCK_SIZE,
110 keyminlen: BLOWFISH_KEY_MIN_LEN,
111 keydeflen: BLOWFISH_KEY_MIN_LEN,
112 keymaxlen: BLOWFISH_KEY_MAX_LEN,
113 do_crypt: do_blowfish,
114 enc_testvectors: bf_enc_testvectors,
115 };
116
117 int ike_alg_blowfish_init(void);
118
119 int
120 ike_alg_blowfish_init(void)
121 {
122 int ret = ike_alg_register_enc(&algo_blowfish);
123
124 return ret;
125 }
126 /*
127 IKE_ALG_INIT_NAME: ike_alg_blowfish_init
128 */