crypto: introduce crypto infra
[vpp.git] / src / plugins / crypto_openssl / main.c
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2019 Cisco and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *------------------------------------------------------------------
16  */
17
18 #include <openssl/evp.h>
19 #include <openssl/hmac.h>
20 #include <openssl/rand.h>
21
22 #include <vlib/vlib.h>
23 #include <vnet/plugin/plugin.h>
24 #include <vnet/crypto/crypto.h>
25 #include <vpp/app/version.h>
26
27 typedef struct
28 {
29   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
30   EVP_CIPHER_CTX *evp_cipher_ctx;
31   HMAC_CTX *hmac_ctx;
32 #if OPENSSL_VERSION_NUMBER < 0x10100000L
33   HMAC_CTX _hmac_ctx;
34 #endif
35 } openssl_per_thread_data_t;
36
37 static openssl_per_thread_data_t *per_thread_data = 0;
38
39 #define foreach_openssl_evp_op \
40   _(DES_CBC, EVP_des_cbc) \
41   _(3DES_CBC, EVP_des_ede3_cbc) \
42   _(AES_128_CBC, EVP_aes_128_cbc) \
43   _(AES_192_CBC, EVP_aes_192_cbc) \
44   _(AES_256_CBC, EVP_aes_256_cbc)
45
46 #define foreach_openssl_hmac_op \
47   _(SHA1, EVP_sha1) \
48   _(SHA224, EVP_sha224) \
49   _(SHA256, EVP_sha256) \
50   _(SHA384, EVP_sha384) \
51   _(SHA512, EVP_sha512)
52
53 static_always_inline u32
54 openssl_ops_enc_cbc (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops,
55                      const EVP_CIPHER * cipher)
56 {
57   openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
58                                                      vm->thread_index);
59   EVP_CIPHER_CTX *ctx = ptd->evp_cipher_ctx;
60   u32 i;
61   for (i = 0; i < n_ops; i++)
62     {
63       vnet_crypto_op_t *op = ops[i];
64       int out_len;
65
66       if (op->flags & VNET_CRYPTO_OP_FLAG_INIT_IV)
67         RAND_bytes (op->iv, 16);
68
69       EVP_EncryptInit_ex (ctx, cipher, NULL, op->key, op->iv);
70       EVP_EncryptUpdate (ctx, op->dst, &out_len, op->src, op->len);
71       EVP_EncryptFinal_ex (ctx, op->dst + out_len, &out_len);
72       op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
73     }
74   return n_ops;
75 }
76
77 static_always_inline u32
78 openssl_ops_dec_cbc (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops,
79                      const EVP_CIPHER * cipher)
80 {
81   openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
82                                                      vm->thread_index);
83   EVP_CIPHER_CTX *ctx = ptd->evp_cipher_ctx;
84   u32 i;
85   for (i = 0; i < n_ops; i++)
86     {
87       vnet_crypto_op_t *op = ops[i];
88       int out_len;
89
90       EVP_DecryptInit_ex (ctx, cipher, NULL, op->key, op->iv);
91       EVP_DecryptUpdate (ctx, op->dst, &out_len, op->src, op->len);
92       EVP_DecryptFinal_ex (ctx, op->dst + out_len, &out_len);
93       op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
94     }
95   return n_ops;
96 }
97
98 static_always_inline u32
99 openssl_ops_hmac (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops,
100                   const EVP_MD * md)
101 {
102   openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
103                                                      vm->thread_index);
104   HMAC_CTX *ctx = ptd->hmac_ctx;
105   u32 i;
106   for (i = 0; i < n_ops; i++)
107     {
108       vnet_crypto_op_t *op = ops[i];
109       unsigned int out_len;
110
111       HMAC_Init_ex (ctx, op->key, op->key_len, md, NULL);
112       HMAC_Update (ctx, op->src, op->len);
113       HMAC_Final (ctx, op->dst, &out_len);
114       op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
115     }
116   return n_ops;
117 }
118
119 #define _(a, b) \
120 static u32 \
121 openssl_ops_enc_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops) \
122 { return openssl_ops_enc_cbc (vm, ops, n_ops, b ()); } \
123 \
124 u32 \
125 openssl_ops_dec_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops) \
126 { return openssl_ops_dec_cbc (vm, ops, n_ops, b ()); }
127
128 foreach_openssl_evp_op;
129 #undef _
130
131 #define _(a, b) \
132 static u32 \
133 openssl_ops_hmac_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops) \
134 { return openssl_ops_hmac (vm, ops, n_ops, b ()); } \
135
136 foreach_openssl_hmac_op;
137 #undef _
138
139
140 clib_error_t *
141 crypto_openssl_init (vlib_main_t * vm)
142 {
143   vlib_thread_main_t *tm = vlib_get_thread_main ();
144   openssl_per_thread_data_t *ptd;
145   u8 *seed_data = 0;
146   time_t t;
147   pid_t pid;
148
149   u32 eidx = vnet_crypto_register_engine (vm, "openssl", 50, "OpenSSL");
150   clib_error_t *error;
151
152   if ((error = vlib_call_init_function (vm, vnet_crypto_init)))
153     return error;
154
155 #define _(a, b) \
156   vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_ENC, \
157                                     openssl_ops_enc_##a); \
158   vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_DEC, \
159                                     openssl_ops_dec_##a);
160
161   foreach_openssl_evp_op;
162 #undef _
163
164 #define _(a, b) \
165   vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_HMAC, \
166                                     openssl_ops_hmac_##a); \
167
168   foreach_openssl_hmac_op;
169 #undef _
170
171   vec_validate_aligned (per_thread_data, tm->n_vlib_mains - 1,
172                         CLIB_CACHE_LINE_BYTES);
173
174   vec_foreach (ptd, per_thread_data)
175   {
176     ptd->evp_cipher_ctx = EVP_CIPHER_CTX_new ();
177 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
178     ptd->hmac_ctx = HMAC_CTX_new ();
179 #else
180     HMAC_CTX_init (&(ptd->_hmac_ctx));
181     ptd->hmac_ctx = &ptd->_hmac_ctx;
182 #endif
183   }
184
185   t = time (NULL);
186   pid = getpid ();
187   vec_add (seed_data, &t, sizeof (t));
188   vec_add (seed_data, &pid, sizeof (pid));
189   vec_add (seed_data, seed_data, sizeof (seed_data));
190
191   RAND_seed ((const void *) seed_data, vec_len (seed_data));
192
193   vec_free (seed_data);
194
195   return 0;
196 }
197
198 VLIB_INIT_FUNCTION (crypto_openssl_init);
199
200 /* *INDENT-OFF* */
201 VLIB_PLUGIN_REGISTER () = {
202   .version = VPP_BUILD_VER,
203   .description = "OpenSSL Crypto Engine Plugin",
204 };
205 /* *INDENT-ON* */
206
207 /*
208  * fd.io coding-style-patch-verification: ON
209  *
210  * Local Variables:
211  * eval: (c-set-style "gnu")
212  * End:
213  */