• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/* BEGIN_HEADER */
2#include "mbedtls/blowfish.h"
3/* END_HEADER */
4
5/* BEGIN_DEPENDENCIES
6 * depends_on:MBEDTLS_BLOWFISH_C
7 * END_DEPENDENCIES
8 */
9
10/* BEGIN_CASE */
11void blowfish_valid_param()
12{
13    TEST_VALID_PARAM(mbedtls_blowfish_free(NULL));
14}
15/* END_CASE */
16
17/* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
18void blowfish_invalid_param()
19{
20    mbedtls_blowfish_context ctx;
21    unsigned char buf[16] = { 0 };
22    size_t const valid_keylength = sizeof(buf) * 8;
23    size_t valid_mode = MBEDTLS_BLOWFISH_ENCRYPT;
24    size_t invalid_mode = 42;
25    size_t off;
26    ((void) off);
27
28    TEST_INVALID_PARAM(mbedtls_blowfish_init(NULL));
29    TEST_VALID_PARAM(mbedtls_blowfish_free(NULL));
30
31    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
32                           mbedtls_blowfish_setkey(NULL,
33                                                   buf,
34                                                   valid_keylength));
35    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
36                           mbedtls_blowfish_setkey(&ctx,
37                                                   NULL,
38                                                   valid_keylength));
39
40    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
41                           mbedtls_blowfish_crypt_ecb(NULL,
42                                                      valid_mode,
43                                                      buf, buf));
44    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
45                           mbedtls_blowfish_crypt_ecb(&ctx,
46                                                      invalid_mode,
47                                                      buf, buf));
48    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
49                           mbedtls_blowfish_crypt_ecb(&ctx,
50                                                      valid_mode,
51                                                      NULL, buf));
52    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
53                           mbedtls_blowfish_crypt_ecb(&ctx,
54                                                      valid_mode,
55                                                      buf, NULL));
56
57#if defined(MBEDTLS_CIPHER_MODE_CBC)
58    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
59                           mbedtls_blowfish_crypt_cbc(NULL,
60                                                      valid_mode,
61                                                      sizeof(buf),
62                                                      buf, buf, buf));
63    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
64                           mbedtls_blowfish_crypt_cbc(&ctx,
65                                                      invalid_mode,
66                                                      sizeof(buf),
67                                                      buf, buf, buf));
68    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
69                           mbedtls_blowfish_crypt_cbc(&ctx,
70                                                      valid_mode,
71                                                      sizeof(buf),
72                                                      NULL, buf, buf));
73    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
74                           mbedtls_blowfish_crypt_cbc(&ctx,
75                                                      valid_mode,
76                                                      sizeof(buf),
77                                                      buf, NULL, buf));
78    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
79                           mbedtls_blowfish_crypt_cbc(&ctx,
80                                                      valid_mode,
81                                                      sizeof(buf),
82                                                      buf, buf, NULL));
83#endif /* MBEDTLS_CIPHER_MODE_CBC */
84
85#if defined(MBEDTLS_CIPHER_MODE_CFB)
86    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
87                           mbedtls_blowfish_crypt_cfb64(NULL,
88                                                        valid_mode,
89                                                        sizeof(buf),
90                                                        &off, buf,
91                                                        buf, buf));
92    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
93                           mbedtls_blowfish_crypt_cfb64(&ctx,
94                                                        invalid_mode,
95                                                        sizeof(buf),
96                                                        &off, buf,
97                                                        buf, buf));
98    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
99                           mbedtls_blowfish_crypt_cfb64(&ctx,
100                                                        valid_mode,
101                                                        sizeof(buf),
102                                                        NULL, buf,
103                                                        buf, buf));
104    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
105                           mbedtls_blowfish_crypt_cfb64(&ctx,
106                                                        valid_mode,
107                                                        sizeof(buf),
108                                                        &off, NULL,
109                                                        buf, buf));
110    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
111                           mbedtls_blowfish_crypt_cfb64(&ctx,
112                                                        valid_mode,
113                                                        sizeof(buf),
114                                                        &off, buf,
115                                                        NULL, buf));
116    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
117                           mbedtls_blowfish_crypt_cfb64(&ctx,
118                                                        valid_mode,
119                                                        sizeof(buf),
120                                                        &off, buf,
121                                                        buf, NULL));
122#endif /* MBEDTLS_CIPHER_MODE_CFB */
123
124#if defined(MBEDTLS_CIPHER_MODE_CTR)
125    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
126                           mbedtls_blowfish_crypt_ctr(NULL,
127                                                      sizeof(buf),
128                                                      &off,
129                                                      buf, buf,
130                                                      buf, buf));
131    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
132                           mbedtls_blowfish_crypt_ctr(&ctx,
133                                                      sizeof(buf),
134                                                      NULL,
135                                                      buf, buf,
136                                                      buf, buf));
137    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
138                           mbedtls_blowfish_crypt_ctr(&ctx,
139                                                      sizeof(buf),
140                                                      &off,
141                                                      NULL, buf,
142                                                      buf, buf));
143    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
144                           mbedtls_blowfish_crypt_ctr(&ctx,
145                                                      sizeof(buf),
146                                                      &off,
147                                                      buf, NULL,
148                                                      buf, buf));
149    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
150                           mbedtls_blowfish_crypt_ctr(&ctx,
151                                                      sizeof(buf),
152                                                      &off,
153                                                      buf, buf,
154                                                      NULL, buf));
155    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
156                           mbedtls_blowfish_crypt_ctr(&ctx,
157                                                      sizeof(buf),
158                                                      &off,
159                                                      buf, buf,
160                                                      buf, NULL));
161#endif /* MBEDTLS_CIPHER_MODE_CTR */
162
163exit:
164    return;
165}
166/* END_CASE */
167
168/* BEGIN_CASE */
169void blowfish_encrypt_ecb(data_t *key_str, data_t *src_str,
170                          data_t *dst, int setkey_result)
171{
172    unsigned char output[100];
173    mbedtls_blowfish_context ctx;
174
175    memset(output, 0x00, 100);
176    mbedtls_blowfish_init(&ctx);
177
178
179    TEST_ASSERT(mbedtls_blowfish_setkey(&ctx, key_str->x, key_str->len * 8) == setkey_result);
180    if (setkey_result == 0) {
181        TEST_ASSERT(mbedtls_blowfish_crypt_ecb(&ctx, MBEDTLS_BLOWFISH_ENCRYPT, src_str->x,
182                                               output) == 0);
183
184        TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, 8, dst->len) == 0);
185    }
186
187exit:
188    mbedtls_blowfish_free(&ctx);
189}
190/* END_CASE */
191
192/* BEGIN_CASE */
193void blowfish_decrypt_ecb(data_t *key_str, data_t *src_str,
194                          data_t *dst, int setkey_result)
195{
196    unsigned char output[100];
197    mbedtls_blowfish_context ctx;
198
199    memset(output, 0x00, 100);
200    mbedtls_blowfish_init(&ctx);
201
202
203    TEST_ASSERT(mbedtls_blowfish_setkey(&ctx, key_str->x, key_str->len * 8) == setkey_result);
204    if (setkey_result == 0) {
205        TEST_ASSERT(mbedtls_blowfish_crypt_ecb(&ctx, MBEDTLS_BLOWFISH_DECRYPT, src_str->x,
206                                               output) == 0);
207
208        TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, 8, dst->len) == 0);
209    }
210
211exit:
212    mbedtls_blowfish_free(&ctx);
213}
214/* END_CASE */
215
216/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
217void blowfish_encrypt_cbc(data_t *key_str, data_t *iv_str,
218                          data_t *src_str, data_t *dst,
219                          int cbc_result)
220{
221    unsigned char output[100];
222    mbedtls_blowfish_context ctx;
223
224    memset(output, 0x00, 100);
225    mbedtls_blowfish_init(&ctx);
226
227
228    mbedtls_blowfish_setkey(&ctx, key_str->x, key_str->len * 8);
229
230    TEST_ASSERT(mbedtls_blowfish_crypt_cbc(&ctx, MBEDTLS_BLOWFISH_ENCRYPT, src_str->len, iv_str->x,
231                                           src_str->x, output) == cbc_result);
232    if (cbc_result == 0) {
233
234        TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x,
235                                        src_str->len, dst->len) == 0);
236    }
237
238exit:
239    mbedtls_blowfish_free(&ctx);
240}
241/* END_CASE */
242
243/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
244void blowfish_decrypt_cbc(data_t *key_str, data_t *iv_str,
245                          data_t *src_str, data_t *dst,
246                          int cbc_result)
247{
248    unsigned char output[100];
249    mbedtls_blowfish_context ctx;
250
251    memset(output, 0x00, 100);
252    mbedtls_blowfish_init(&ctx);
253
254
255    mbedtls_blowfish_setkey(&ctx, key_str->x, key_str->len * 8);
256    TEST_ASSERT(mbedtls_blowfish_crypt_cbc(&ctx, MBEDTLS_BLOWFISH_DECRYPT, src_str->len, iv_str->x,
257                                           src_str->x, output) == cbc_result);
258    if (cbc_result == 0) {
259
260        TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, src_str->len,
261                                        dst->len) == 0);
262    }
263
264exit:
265    mbedtls_blowfish_free(&ctx);
266}
267/* END_CASE */
268
269/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
270void blowfish_encrypt_cfb64(data_t *key_str, data_t *iv_str,
271                            data_t *src_str, data_t *dst)
272{
273    unsigned char output[100];
274    mbedtls_blowfish_context ctx;
275    size_t iv_offset = 0;
276
277    memset(output, 0x00, 100);
278    mbedtls_blowfish_init(&ctx);
279
280
281    mbedtls_blowfish_setkey(&ctx, key_str->x, key_str->len * 8);
282    TEST_ASSERT(mbedtls_blowfish_crypt_cfb64(&ctx, MBEDTLS_BLOWFISH_ENCRYPT, src_str->len,
283                                             &iv_offset, iv_str->x, src_str->x, output) == 0);
284
285    TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, src_str->len,
286                                    dst->len) == 0);
287
288exit:
289    mbedtls_blowfish_free(&ctx);
290}
291/* END_CASE */
292
293/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
294void blowfish_decrypt_cfb64(data_t *key_str, data_t *iv_str,
295                            data_t *src_str, data_t *dst)
296{
297    unsigned char output[100];
298    mbedtls_blowfish_context ctx;
299    size_t iv_offset = 0;
300
301    memset(output, 0x00, 100);
302    mbedtls_blowfish_init(&ctx);
303
304
305    mbedtls_blowfish_setkey(&ctx, key_str->x, key_str->len * 8);
306    TEST_ASSERT(mbedtls_blowfish_crypt_cfb64(&ctx, MBEDTLS_BLOWFISH_DECRYPT, src_str->len,
307                                             &iv_offset, iv_str->x, src_str->x, output) == 0);
308
309    TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, src_str->len,
310                                    dst->len) == 0);
311
312exit:
313    mbedtls_blowfish_free(&ctx);
314}
315/* END_CASE */
316
317/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CTR */
318void blowfish_encrypt_ctr(data_t *key_str, data_t *iv_str,
319                          data_t *src_str, data_t *dst)
320{
321    unsigned char stream_str[100];
322    unsigned char output[100];
323    mbedtls_blowfish_context ctx;
324    size_t iv_offset = 0;
325
326    memset(stream_str, 0x00, 100);
327    memset(output, 0x00, 100);
328    mbedtls_blowfish_init(&ctx);
329
330
331    mbedtls_blowfish_setkey(&ctx, key_str->x, key_str->len * 8);
332    TEST_ASSERT(mbedtls_blowfish_crypt_ctr(&ctx, src_str->len, &iv_offset, iv_str->x, stream_str,
333                                           src_str->x, output) == 0);
334
335    TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, src_str->len,
336                                    dst->len) == 0);
337
338exit:
339    mbedtls_blowfish_free(&ctx);
340}
341/* END_CASE */
342