xref: /dpdk/drivers/crypto/openssl/compat.h (revision 17d5bc6135afdb38ddf02595bfa15aa5142d80b1)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Cavium Networks
3  */
4 
5 #ifndef __RTA_COMPAT_H__
6 #define __RTA_COMPAT_H__
7 
8 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
9 static __rte_always_inline void
free_hmac_ctx(EVP_MAC_CTX * ctx)10 free_hmac_ctx(EVP_MAC_CTX *ctx)
11 {
12 	EVP_MAC_CTX_free(ctx);
13 }
14 
15 static __rte_always_inline void
free_cmac_ctx(EVP_MAC_CTX * ctx)16 free_cmac_ctx(EVP_MAC_CTX *ctx)
17 {
18 	EVP_MAC_CTX_free(ctx);
19 }
20 #else
21 static __rte_always_inline void
free_hmac_ctx(HMAC_CTX * ctx)22 free_hmac_ctx(HMAC_CTX *ctx)
23 {
24 	HMAC_CTX_free(ctx);
25 }
26 
27 static __rte_always_inline void
free_cmac_ctx(CMAC_CTX * ctx)28 free_cmac_ctx(CMAC_CTX *ctx)
29 {
30 	CMAC_CTX_free(ctx);
31 }
32 #endif
33 
34 #if (OPENSSL_VERSION_NUMBER < 0x10100000L)
35 
36 static __rte_always_inline int
set_rsa_params(RSA * rsa,BIGNUM * p,BIGNUM * q)37 set_rsa_params(RSA *rsa, BIGNUM *p, BIGNUM *q)
38 {
39 	rsa->p = p;
40 	rsa->q = q;
41 	return 0;
42 }
43 
44 static __rte_always_inline int
set_rsa_crt_params(RSA * rsa,BIGNUM * dmp1,BIGNUM * dmq1,BIGNUM * iqmp)45 set_rsa_crt_params(RSA *rsa, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
46 {
47 	rsa->dmp1 = dmp1;
48 	rsa->dmq1 = dmq1;
49 	rsa->iqmp = iqmp;
50 	return 0;
51 }
52 
53 static __rte_always_inline int
set_rsa_keys(RSA * rsa,BIGNUM * n,BIGNUM * e,BIGNUM * d)54 set_rsa_keys(RSA *rsa, BIGNUM *n, BIGNUM *e, BIGNUM *d)
55 {
56 	rsa->n = n;
57 	rsa->e = e;
58 	rsa->d = d;
59 	return 0;
60 }
61 
62 static __rte_always_inline int
set_dh_params(DH * dh,BIGNUM * p,BIGNUM * g)63 set_dh_params(DH *dh, BIGNUM *p, BIGNUM *g)
64 {
65 	dh->p = p;
66 	dh->q = NULL;
67 	dh->g = g;
68 	return 0;
69 }
70 
71 static __rte_always_inline int
set_dh_priv_key(DH * dh,BIGNUM * priv_key)72 set_dh_priv_key(DH *dh, BIGNUM *priv_key)
73 {
74 	dh->priv_key = priv_key;
75 	return 0;
76 }
77 
78 static __rte_always_inline int
set_dsa_params(DSA * dsa,BIGNUM * p,BIGNUM * q,BIGNUM * g)79 set_dsa_params(DSA *dsa, BIGNUM *p, BIGNUM *q, BIGNUM *g)
80 {
81 	dsa->p = p;
82 	dsa->q = q;
83 	dsa->g = g;
84 	return 0;
85 }
86 
87 static __rte_always_inline void
get_dh_pub_key(DH * dh,const BIGNUM ** pub_key)88 get_dh_pub_key(DH *dh, const BIGNUM **pub_key)
89 {
90 	*pub_key = dh->pub_key;
91 }
92 
93 static __rte_always_inline void
get_dh_priv_key(DH * dh,const BIGNUM ** priv_key)94 get_dh_priv_key(DH *dh, const BIGNUM **priv_key)
95 {
96 	*priv_key = dh->priv_key;
97 }
98 
99 static __rte_always_inline void
set_dsa_sign(DSA_SIG * sign,BIGNUM * r,BIGNUM * s)100 set_dsa_sign(DSA_SIG *sign, BIGNUM *r, BIGNUM *s)
101 {
102 	sign->r = r;
103 	sign->s = s;
104 }
105 
106 static __rte_always_inline void
get_dsa_sign(DSA_SIG * sign,const BIGNUM ** r,const BIGNUM ** s)107 get_dsa_sign(DSA_SIG *sign, const BIGNUM **r, const BIGNUM **s)
108 {
109 	*r = sign->r;
110 	*s = sign->s;
111 }
112 
113 static __rte_always_inline int
set_dsa_keys(DSA * dsa,BIGNUM * pub,BIGNUM * priv)114 set_dsa_keys(DSA *dsa, BIGNUM *pub, BIGNUM *priv)
115 {
116 	dsa->pub_key = pub;
117 	dsa->priv_key = priv;
118 	return 0;
119 }
120 
121 static __rte_always_inline void
set_dsa_pub_key(DSA * dsa,BIGNUM * pub)122 set_dsa_pub_key(DSA *dsa, BIGNUM *pub)
123 {
124 	dsa->pub_key = pub;
125 }
126 
127 static __rte_always_inline void
get_dsa_priv_key(DSA * dsa,BIGNUM ** priv_key)128 get_dsa_priv_key(DSA *dsa, BIGNUM **priv_key)
129 {
130 	*priv_key = dsa->priv_key;
131 }
132 
133 #elif (OPENSSL_VERSION_NUMBER >= 0x30000000L)
134 static __rte_always_inline void
set_dsa_sign(DSA_SIG * sign,BIGNUM * r,BIGNUM * s)135 set_dsa_sign(DSA_SIG *sign, BIGNUM *r, BIGNUM *s)
136 {
137 	DSA_SIG_set0(sign, r, s);
138 }
139 
140 static __rte_always_inline void
get_dsa_sign(DSA_SIG * sign,const BIGNUM ** r,const BIGNUM ** s)141 get_dsa_sign(DSA_SIG *sign, const BIGNUM **r, const BIGNUM **s)
142 {
143 	DSA_SIG_get0(sign, r, s);
144 }
145 #else
146 
147 static __rte_always_inline int
set_rsa_params(RSA * rsa,BIGNUM * p,BIGNUM * q)148 set_rsa_params(RSA *rsa, BIGNUM *p, BIGNUM *q)
149 {
150 	return !(RSA_set0_factors(rsa, p, q));
151 }
152 
153 static __rte_always_inline int
set_rsa_crt_params(RSA * rsa,BIGNUM * dmp1,BIGNUM * dmq1,BIGNUM * iqmp)154 set_rsa_crt_params(RSA *rsa, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
155 {
156 	return !(RSA_set0_crt_params(rsa, dmp1, dmq1, iqmp));
157 }
158 
159 /* n, e must be non-null, d can be NULL */
160 
161 static __rte_always_inline  int
set_rsa_keys(RSA * rsa,BIGNUM * n,BIGNUM * e,BIGNUM * d)162 set_rsa_keys(RSA *rsa, BIGNUM *n, BIGNUM *e, BIGNUM *d)
163 {
164 	return !(RSA_set0_key(rsa, n, e, d));
165 }
166 
167 static __rte_always_inline int
set_dh_params(DH * dh,BIGNUM * p,BIGNUM * g)168 set_dh_params(DH *dh, BIGNUM *p, BIGNUM *g)
169 {
170 	return !(DH_set0_pqg(dh, p, NULL, g));
171 }
172 
173 static __rte_always_inline int
set_dh_priv_key(DH * dh,BIGNUM * priv_key)174 set_dh_priv_key(DH *dh, BIGNUM *priv_key)
175 {
176 	return !(DH_set0_key(dh, NULL, priv_key));
177 }
178 
179 static __rte_always_inline void
get_dh_pub_key(DH * dh_key,const BIGNUM ** pub_key)180 get_dh_pub_key(DH *dh_key, const BIGNUM **pub_key)
181 {
182 	DH_get0_key(dh_key, pub_key, NULL);
183 }
184 
185 static __rte_always_inline void
get_dh_priv_key(DH * dh_key,const BIGNUM ** priv_key)186 get_dh_priv_key(DH *dh_key, const BIGNUM **priv_key)
187 {
188 	DH_get0_key(dh_key, NULL, priv_key);
189 }
190 
191 static __rte_always_inline int
set_dsa_params(DSA * dsa,BIGNUM * p,BIGNUM * q,BIGNUM * g)192 set_dsa_params(DSA *dsa, BIGNUM *p, BIGNUM *q, BIGNUM *g)
193 {
194 	return !(DSA_set0_pqg(dsa, p, q, g));
195 }
196 
197 static __rte_always_inline void
set_dsa_priv_key(DSA * dsa,BIGNUM * priv_key)198 set_dsa_priv_key(DSA *dsa, BIGNUM *priv_key)
199 {
200 	DSA_set0_key(dsa, NULL, priv_key);
201 }
202 
203 static __rte_always_inline void
set_dsa_sign(DSA_SIG * sign,BIGNUM * r,BIGNUM * s)204 set_dsa_sign(DSA_SIG *sign, BIGNUM *r, BIGNUM *s)
205 {
206 	DSA_SIG_set0(sign, r, s);
207 }
208 
209 static __rte_always_inline void
get_dsa_sign(DSA_SIG * sign,const BIGNUM ** r,const BIGNUM ** s)210 get_dsa_sign(DSA_SIG *sign, const BIGNUM **r, const BIGNUM **s)
211 {
212 	DSA_SIG_get0(sign, r, s);
213 }
214 
215 static __rte_always_inline int
set_dsa_keys(DSA * dsa,BIGNUM * pub,BIGNUM * priv)216 set_dsa_keys(DSA *dsa, BIGNUM *pub, BIGNUM *priv)
217 {
218 	return !(DSA_set0_key(dsa, pub, priv));
219 }
220 
221 static __rte_always_inline void
set_dsa_pub_key(DSA * dsa,BIGNUM * pub_key)222 set_dsa_pub_key(DSA *dsa, BIGNUM *pub_key)
223 {
224 	DSA_set0_key(dsa, pub_key, NULL);
225 }
226 
227 static __rte_always_inline void
get_dsa_priv_key(DSA * dsa,const BIGNUM ** priv_key)228 get_dsa_priv_key(DSA *dsa, const BIGNUM **priv_key)
229 {
230 	DSA_get0_key(dsa, NULL, priv_key);
231 }
232 
233 #endif /* version < 10100000 */
234 
235 #endif /* __RTA_COMPAT_H__ */
236