2 * Copyright (c) 2019 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
15 #include <vlib/vlib.h>
16 #include <vppinfra/time.h>
17 #include <vppinfra/cache.h>
18 #include <vppinfra/error.h>
19 #include <vnet/crypto/crypto.h>
20 #include <unittest/crypto/crypto.h>
22 crypto_test_main_t crypto_test_main;
25 sort_registrations (void *a0, void *a1)
27 unittest_crypto_test_registration_t **r0 = a0;
28 unittest_crypto_test_registration_t **r1 = a1;
30 return (strncmp (r0[0]->name, r1[0]->name, 256));
34 test_crypto (vlib_main_t * vm, crypto_test_main_t * tm)
36 vnet_crypto_main_t *cm = &crypto_main;
37 unittest_crypto_test_registration_t *r = tm->test_registrations;
38 unittest_crypto_test_registration_t **rv = 0;
39 vnet_crypto_alg_data_t *ad;
40 vnet_crypto_op_t *ops = 0, *op;
41 u8 *computed_data = 0, *s = 0, *err = 0;
42 u32 computed_data_total_len = 0, n_ops = 0;
45 /* construct registration vector */
49 ad = vec_elt_at_index (cm->algs, r->alg);
51 for (i = 0; i < VNET_CRYPTO_OP_N_TYPES; i++)
53 vnet_crypto_op_id_t id = ad->op_by_type[i];
60 case VNET_CRYPTO_OP_TYPE_ENCRYPT:
61 case VNET_CRYPTO_OP_TYPE_DECRYPT:
62 case VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT:
63 computed_data_total_len += r->ciphertext.length;
66 case VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT:
67 computed_data_total_len += r->ciphertext.length;
68 computed_data_total_len += r->tag.length;
71 case VNET_CRYPTO_OP_TYPE_HMAC:
72 computed_data_total_len += r->digest.length;
84 vec_sort_with_function (rv, sort_registrations);
86 vec_validate_aligned (computed_data, computed_data_total_len - 1,
87 CLIB_CACHE_LINE_BYTES);
88 vec_validate_aligned (ops, n_ops - 1, CLIB_CACHE_LINE_BYTES);
89 computed_data_total_len = 0;
93 vec_foreach_index (i, rv)
97 ad = vec_elt_at_index (cm->algs, r->alg);
98 for (t = 0; t < VNET_CRYPTO_OP_N_TYPES; t++)
100 vnet_crypto_op_id_t id = ad->op_by_type[t];
105 vnet_crypto_op_init (op, id);
109 case VNET_CRYPTO_OP_TYPE_ENCRYPT:
110 case VNET_CRYPTO_OP_TYPE_DECRYPT:
112 op->iv_len = r->iv.length;
113 op->key = r->key.data;
114 op->key_len = r->key.length;
115 op->len = r->plaintext.length;
116 op->src = t == VNET_CRYPTO_OP_TYPE_ENCRYPT ?
117 r->plaintext.data : r->ciphertext.data;
118 op->dst = computed_data + computed_data_total_len;
119 computed_data_total_len += r->ciphertext.length;
121 case VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT:
122 case VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT:
124 op->iv_len = r->iv.length;
125 op->key = r->key.data;
126 op->key_len = r->key.length;
127 op->aad = r->aad.data;
128 op->aad_len = r->aad.length;
129 op->len = r->plaintext.length;
130 op->dst = computed_data + computed_data_total_len;
131 computed_data_total_len += r->ciphertext.length;
132 if (t == VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT)
134 op->src = r->plaintext.data;
135 op->tag = computed_data + computed_data_total_len;
136 computed_data_total_len += r->tag.length;
140 op->src = r->ciphertext.data;
141 op->tag = r->tag.data;
143 op->tag_len = r->tag.length;
145 case VNET_CRYPTO_OP_TYPE_HMAC:
146 op->key = r->key.data;
147 op->key_len = r->key.length;
148 op->src = r->plaintext.data;
149 op->len = r->plaintext.length;
150 op->digest_len = r->digest.length;
151 op->digest = computed_data + computed_data_total_len;
152 computed_data_total_len += r->digest.length;
166 vnet_crypto_process_ops (vm, ops, vec_len (ops));
169 vec_foreach (op, ops)
172 r = rv[op->user_data];
173 unittest_crypto_test_data_t *exp_pt = 0, *exp_ct = 0;
174 unittest_crypto_test_data_t *exp_digest = 0, *exp_tag = 0;
176 switch (vnet_crypto_get_op_type (op->op))
178 case VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT:
180 case VNET_CRYPTO_OP_TYPE_ENCRYPT:
181 exp_ct = &r->ciphertext;
183 case VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT:
184 case VNET_CRYPTO_OP_TYPE_DECRYPT:
185 exp_pt = &r->plaintext;
187 case VNET_CRYPTO_OP_TYPE_HMAC:
188 exp_digest = &r->digest;
194 vec_reset_length (err);
196 if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED)
197 err = format (err, "%sengine error: %U", vec_len (err) ? ", " : "",
198 format_vnet_crypto_op_status, op->status);
200 if (exp_ct && memcmp (op->dst, exp_ct->data, exp_ct->length) != 0)
201 err = format (err, "%sciphertext mismatch",
202 vec_len (err) ? ", " : "");
204 if (exp_pt && memcmp (op->dst, exp_pt->data, exp_pt->length) != 0)
205 err = format (err, "%splaintext mismatch", vec_len (err) ? ", " : "");
207 if (exp_tag && memcmp (op->tag, exp_tag->data, exp_tag->length) != 0)
208 err = format (err, "%stag mismatch", vec_len (err) ? ", " : "");
211 memcmp (op->digest, exp_digest->data, exp_digest->length) != 0)
212 err = format (err, "%sdigest mismatch", vec_len (err) ? ", " : "");
214 vec_reset_length (s);
215 s = format (s, "%s (%U)", r->name, format_vnet_crypto_op, op->op);
220 vlib_cli_output (vm, "%-60v%s%v", s, vec_len (err) ? "FAIL: " : "OK",
224 if (tm->verbose == 2)
228 vlib_cli_output (vm, "Expected ciphertext:\n%U"
229 "\nCalculated ciphertext:\n%U",
230 format_hexdump, exp_ct->data, exp_ct->length,
231 format_hexdump, op->dst, exp_ct->length);
233 vlib_cli_output (vm, "Expected plaintext:\n%U"
234 "\nCalculated plaintext:\n%U",
235 format_hexdump, exp_pt->data, exp_pt->length,
236 format_hexdump, op->dst, exp_pt->length);
237 if (r->tag.length && fail)
238 vlib_cli_output (vm, "Expected tag:\n%U"
239 "\nCalculated tag:\n%U",
240 format_hexdump, r->tag.data, r->tag.length,
241 format_hexdump, op->tag, op->tag_len);
242 if (exp_digest && fail)
243 vlib_cli_output (vm, "Expected digest:\n%U"
244 "\nCalculated Digest:\n%U",
245 format_hexdump, exp_digest->data,
246 exp_digest->length, format_hexdump, op->digest,
252 vec_free (computed_data);
260 static clib_error_t *
261 test_crypto_command_fn (vlib_main_t * vm,
262 unformat_input_t * input, vlib_cli_command_t * cmd)
264 crypto_test_main_t *tm = &crypto_test_main;
266 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
268 if (unformat (input, "verbose"))
270 else if (unformat (input, "detail"))
273 return clib_error_return (0, "unknown input '%U'",
274 format_unformat_error, input);
277 return test_crypto (vm, tm);
281 VLIB_CLI_COMMAND (test_crypto_command, static) =
283 .path = "test crypto",
284 .short_help = "test crypto",
285 .function = test_crypto_command_fn,
289 static clib_error_t *
290 crypto_test_init (vlib_main_t * vm)
295 VLIB_INIT_FUNCTION (crypto_test_init);
298 * fd.io coding-style-patch-verification: ON
301 * eval: (c-set-style "gnu")