crypto: introduce crypto infra
[vpp.git] / src / plugins / unittest / crypto_test.c
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
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>
21
22 crypto_test_main_t crypto_test_main;
23
24 static int
25 sort_registrations (void *a0, void *a1)
26 {
27   unittest_crypto_test_registration_t **r0 = a0;
28   unittest_crypto_test_registration_t **r1 = a1;
29
30   return (r0[0]->op > r1[0]->op);
31 }
32
33 static clib_error_t *
34 test_crypto (vlib_main_t * vm, crypto_test_main_t * tm)
35 {
36   unittest_crypto_test_registration_t *r = tm->test_registrations;
37   unittest_crypto_test_registration_t **rv = 0;
38   vnet_crypto_op_t *ops = 0, *op;
39   u8 *computed_data = 0, *s = 0;
40   u32 computed_data_total_len = 0, n_tests = 0;
41   u32 i;
42
43   /* construct registration vector */
44   while (r)
45     {
46       vec_add1 (rv, r);
47       computed_data_total_len += r->data.length;
48       n_tests += 1;
49       /* next */
50       r = r->next;
51     }
52
53   vec_sort_with_function (rv, sort_registrations);
54
55   vec_validate_aligned (computed_data, computed_data_total_len - 1,
56                         CLIB_CACHE_LINE_BYTES);
57   vec_validate_aligned (ops, n_tests - 1, CLIB_CACHE_LINE_BYTES);
58   computed_data_total_len = 0;
59
60   /* *INDENT-OFF* */
61   vec_foreach_index (i, rv)
62     {
63       r = rv[i];
64       op  = ops + i;
65       op->op = r->op;
66       op->iv = r->iv.data;
67       op->key = r->key.data;
68       op->src = r->data.data;
69       op->dst = computed_data + computed_data_total_len;
70       op->len = r->data.length;
71       op->key_len = r->key.length;
72       computed_data_total_len += r->expected.length;
73       /* next */
74       r = r->next;
75     }
76   /* *INDENT-ON* */
77
78   vnet_crypto_process_ops (vm, ops, vec_len (ops));
79
80   /* *INDENT-OFF* */
81   vec_foreach_index (i, rv)
82     {
83       int fail = 0;
84       r = rv[i];
85       op  = ops + i;
86
87       if (memcmp (op->dst, r->expected.data, r->expected.length) != 0)
88         fail = 1;
89
90       vec_reset_length (s);
91       s = format (s, "%s (%U)", r->name,
92                        format_vnet_crypto_op, r->op);
93
94       vlib_cli_output (vm, "%-60v%s", s, fail ? "FAIL" : "OK");
95       if (fail & tm->verbose)
96         {
97            vlib_cli_output (vm, "Expected:\n%U\nCalculated:\n%U",
98                             format_hexdump, r->expected, r->expected.length,
99                             format_hexdump, op->dst, r->expected.length);
100         }
101     }
102   /* *INDENT-ON* */
103
104   vec_free (computed_data);
105   vec_free (ops);
106   vec_free (rv);
107   vec_free (s);
108   return 0;
109 }
110
111 static clib_error_t *
112 test_crypto_command_fn (vlib_main_t * vm,
113                         unformat_input_t * input, vlib_cli_command_t * cmd)
114 {
115   crypto_test_main_t *tm = &crypto_test_main;
116
117   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
118     {
119       if (unformat (input, "verbose %d", &tm->verbose))
120         ;
121       else
122         return clib_error_return (0, "unknown input '%U'",
123                                   format_unformat_error, input);
124     }
125
126   return test_crypto (vm, tm);
127 }
128
129 /* *INDENT-OFF* */
130 VLIB_CLI_COMMAND (test_crypto_command, static) =
131 {
132   .path = "test crypto",
133   .short_help = "test crypto",
134   .function = test_crypto_command_fn,
135 };
136 /* *INDENT-ON* */
137
138 static clib_error_t *
139 crypto_test_init (vlib_main_t * vm)
140 {
141   return (0);
142 }
143
144 VLIB_INIT_FUNCTION (crypto_test_init);
145
146 /*
147  * fd.io coding-style-patch-verification: ON
148  *
149  * Local Variables:
150  * eval: (c-set-style "gnu")
151  * End:
152  */