1*0a6a1f1dSLionel Sambuc /*
2*0a6a1f1dSLionel Sambuc  * Demo of how to construct your own engine and using it.  The basis of this
3*0a6a1f1dSLionel Sambuc  * engine is RSAref, an old reference of the RSA algorithm which can still be
4*0a6a1f1dSLionel Sambuc  * found a little here and there.
5*0a6a1f1dSLionel Sambuc  */
6ebfedea0SLionel Sambuc 
7ebfedea0SLionel Sambuc #include <stdio.h>
8ebfedea0SLionel Sambuc #include <string.h>
9ebfedea0SLionel Sambuc #include "./source/global.h"
10ebfedea0SLionel Sambuc #include "./source/rsaref.h"
11ebfedea0SLionel Sambuc #include "./source/rsa.h"
12ebfedea0SLionel Sambuc #include "./source/des.h"
13ebfedea0SLionel Sambuc #include <openssl/err.h>
14ebfedea0SLionel Sambuc #define OPENSSL_NO_MD2
15ebfedea0SLionel Sambuc #define OPENSSL_NO_MD5
16ebfedea0SLionel Sambuc #include <openssl/evp.h>
17ebfedea0SLionel Sambuc #include <openssl/bn.h>
18ebfedea0SLionel Sambuc #include <openssl/engine.h>
19ebfedea0SLionel Sambuc 
20ebfedea0SLionel Sambuc #define RSAREF_LIB_NAME "rsaref engine"
21ebfedea0SLionel Sambuc #include "rsaref_err.c"
22ebfedea0SLionel Sambuc 
23ebfedea0SLionel Sambuc /*****************************************************************************
24ebfedea0SLionel Sambuc  *** Function declarations and global variable definitions                 ***
25ebfedea0SLionel Sambuc  *****************************************************************************/
26ebfedea0SLionel Sambuc 
27ebfedea0SLionel Sambuc /*****************************************************************************
28ebfedea0SLionel Sambuc  * Constants used when creating the ENGINE
29ebfedea0SLionel Sambuc  **/
30ebfedea0SLionel Sambuc static const char *engine_rsaref_id = "rsaref";
31ebfedea0SLionel Sambuc static const char *engine_rsaref_name = "RSAref engine support";
32ebfedea0SLionel Sambuc 
33ebfedea0SLionel Sambuc /*****************************************************************************
34ebfedea0SLionel Sambuc  * Functions to handle the engine
35ebfedea0SLionel Sambuc  **/
36ebfedea0SLionel Sambuc static int rsaref_destroy(ENGINE *e);
37ebfedea0SLionel Sambuc static int rsaref_init(ENGINE *e);
38ebfedea0SLionel Sambuc static int rsaref_finish(ENGINE *e);
39ebfedea0SLionel Sambuc #if 0
40ebfedea0SLionel Sambuc static int rsaref_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) ());
41ebfedea0SLionel Sambuc #endif
42ebfedea0SLionel Sambuc 
43ebfedea0SLionel Sambuc /*****************************************************************************
44ebfedea0SLionel Sambuc  * Engine commands
45ebfedea0SLionel Sambuc  **/
46ebfedea0SLionel Sambuc static const ENGINE_CMD_DEFN rsaref_cmd_defns[] = {
47ebfedea0SLionel Sambuc     {0, NULL, NULL, 0}
48ebfedea0SLionel Sambuc };
49ebfedea0SLionel Sambuc 
50ebfedea0SLionel Sambuc /*****************************************************************************
51ebfedea0SLionel Sambuc  * RSA functions
52ebfedea0SLionel Sambuc  **/
53ebfedea0SLionel Sambuc static int rsaref_private_decrypt(int len, const unsigned char *from,
54ebfedea0SLionel Sambuc                                   unsigned char *to, RSA *rsa, int padding);
55ebfedea0SLionel Sambuc static int rsaref_private_encrypt(int len, const unsigned char *from,
56ebfedea0SLionel Sambuc                                   unsigned char *to, RSA *rsa, int padding);
57ebfedea0SLionel Sambuc static int rsaref_public_encrypt(int len, const unsigned char *from,
58ebfedea0SLionel Sambuc                                  unsigned char *to, RSA *rsa, int padding);
59ebfedea0SLionel Sambuc static int rsaref_public_decrypt(int len, const unsigned char *from,
60ebfedea0SLionel Sambuc                                  unsigned char *to, RSA *rsa, int padding);
61*0a6a1f1dSLionel Sambuc static int bnref_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
62*0a6a1f1dSLionel Sambuc                          const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
63ebfedea0SLionel Sambuc static int rsaref_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa);
64ebfedea0SLionel Sambuc 
65ebfedea0SLionel Sambuc /*****************************************************************************
66ebfedea0SLionel Sambuc  * Our RSA method
67ebfedea0SLionel Sambuc  **/
68*0a6a1f1dSLionel Sambuc static RSA_METHOD rsaref_rsa = {
69ebfedea0SLionel Sambuc     "RSAref PKCS#1 RSA",
70ebfedea0SLionel Sambuc     rsaref_public_encrypt,
71ebfedea0SLionel Sambuc     rsaref_public_decrypt,
72ebfedea0SLionel Sambuc     rsaref_private_encrypt,
73ebfedea0SLionel Sambuc     rsaref_private_decrypt,
74ebfedea0SLionel Sambuc     rsaref_mod_exp,
75ebfedea0SLionel Sambuc     bnref_mod_exp,
76ebfedea0SLionel Sambuc     NULL,
77ebfedea0SLionel Sambuc     NULL,
78ebfedea0SLionel Sambuc     0,
79ebfedea0SLionel Sambuc     NULL,
80ebfedea0SLionel Sambuc     NULL,
81ebfedea0SLionel Sambuc     NULL
82ebfedea0SLionel Sambuc };
83ebfedea0SLionel Sambuc 
84ebfedea0SLionel Sambuc /*****************************************************************************
85ebfedea0SLionel Sambuc  * Symetric cipher and digest function registrars
86ebfedea0SLionel Sambuc  **/
87ebfedea0SLionel Sambuc static int rsaref_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
88ebfedea0SLionel Sambuc                           const int **nids, int nid);
89ebfedea0SLionel Sambuc static int rsaref_digests(ENGINE *e, const EVP_MD **digest,
90ebfedea0SLionel Sambuc                           const int **nids, int nid);
91ebfedea0SLionel Sambuc 
92ebfedea0SLionel Sambuc static int rsaref_cipher_nids[] =
93ebfedea0SLionel Sambuc     { NID_des_cbc, NID_des_ede3_cbc, NID_desx_cbc, 0 };
94*0a6a1f1dSLionel Sambuc static int rsaref_digest_nids[] = { NID_md2, NID_md5, 0 };
95ebfedea0SLionel Sambuc 
96ebfedea0SLionel Sambuc /*****************************************************************************
97ebfedea0SLionel Sambuc  * DES functions
98ebfedea0SLionel Sambuc  **/
99ebfedea0SLionel Sambuc static int cipher_des_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
100ebfedea0SLionel Sambuc                                const unsigned char *iv, int enc);
101ebfedea0SLionel Sambuc static int cipher_des_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
102ebfedea0SLionel Sambuc                                const unsigned char *in, unsigned int inl);
103ebfedea0SLionel Sambuc static int cipher_des_cbc_clean(EVP_CIPHER_CTX *);
104*0a6a1f1dSLionel Sambuc static int cipher_des_ede3_cbc_init(EVP_CIPHER_CTX *ctx,
105*0a6a1f1dSLionel Sambuc                                     const unsigned char *key,
106ebfedea0SLionel Sambuc                                     const unsigned char *iv, int enc);
107ebfedea0SLionel Sambuc static int cipher_des_ede3_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
108*0a6a1f1dSLionel Sambuc                                     const unsigned char *in,
109*0a6a1f1dSLionel Sambuc                                     unsigned int inl);
110ebfedea0SLionel Sambuc static int cipher_des_ede3_cbc_clean(EVP_CIPHER_CTX *);
111ebfedea0SLionel Sambuc static int cipher_desx_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
112ebfedea0SLionel Sambuc                                 const unsigned char *iv, int enc);
113ebfedea0SLionel Sambuc static int cipher_desx_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
114ebfedea0SLionel Sambuc                                 const unsigned char *in, unsigned int inl);
115ebfedea0SLionel Sambuc static int cipher_desx_cbc_clean(EVP_CIPHER_CTX *);
116ebfedea0SLionel Sambuc 
117ebfedea0SLionel Sambuc /*****************************************************************************
118ebfedea0SLionel Sambuc  * Our DES ciphers
119ebfedea0SLionel Sambuc  **/
120*0a6a1f1dSLionel Sambuc static const EVP_CIPHER cipher_des_cbc = {
121ebfedea0SLionel Sambuc     NID_des_cbc,
122ebfedea0SLionel Sambuc     8, 8, 8,
123ebfedea0SLionel Sambuc     0 | EVP_CIPH_CBC_MODE,
124ebfedea0SLionel Sambuc     cipher_des_cbc_init,
125ebfedea0SLionel Sambuc     cipher_des_cbc_code,
126ebfedea0SLionel Sambuc     cipher_des_cbc_clean,
127ebfedea0SLionel Sambuc     sizeof(DES_CBC_CTX),
128ebfedea0SLionel Sambuc     NULL,
129ebfedea0SLionel Sambuc     NULL,
130ebfedea0SLionel Sambuc     NULL,
131ebfedea0SLionel Sambuc     NULL
132ebfedea0SLionel Sambuc };
133ebfedea0SLionel Sambuc 
134*0a6a1f1dSLionel Sambuc static const EVP_CIPHER cipher_des_ede3_cbc = {
135ebfedea0SLionel Sambuc     NID_des_ede3_cbc,
136ebfedea0SLionel Sambuc     8, 24, 8,
137ebfedea0SLionel Sambuc     0 | EVP_CIPH_CBC_MODE,
138ebfedea0SLionel Sambuc     cipher_des_ede3_cbc_init,
139ebfedea0SLionel Sambuc     cipher_des_ede3_cbc_code,
140ebfedea0SLionel Sambuc     cipher_des_ede3_cbc_clean,
141ebfedea0SLionel Sambuc     sizeof(DES3_CBC_CTX),
142ebfedea0SLionel Sambuc     NULL,
143ebfedea0SLionel Sambuc     NULL,
144ebfedea0SLionel Sambuc     NULL,
145ebfedea0SLionel Sambuc     NULL
146ebfedea0SLionel Sambuc };
147ebfedea0SLionel Sambuc 
148*0a6a1f1dSLionel Sambuc static const EVP_CIPHER cipher_desx_cbc = {
149ebfedea0SLionel Sambuc     NID_desx_cbc,
150ebfedea0SLionel Sambuc     8, 24, 8,
151ebfedea0SLionel Sambuc     0 | EVP_CIPH_CBC_MODE,
152ebfedea0SLionel Sambuc     cipher_desx_cbc_init,
153ebfedea0SLionel Sambuc     cipher_desx_cbc_code,
154ebfedea0SLionel Sambuc     cipher_desx_cbc_clean,
155ebfedea0SLionel Sambuc     sizeof(DESX_CBC_CTX),
156ebfedea0SLionel Sambuc     NULL,
157ebfedea0SLionel Sambuc     NULL,
158ebfedea0SLionel Sambuc     NULL,
159ebfedea0SLionel Sambuc     NULL
160ebfedea0SLionel Sambuc };
161ebfedea0SLionel Sambuc 
162ebfedea0SLionel Sambuc /*****************************************************************************
163ebfedea0SLionel Sambuc  * MD functions
164ebfedea0SLionel Sambuc  **/
165ebfedea0SLionel Sambuc static int digest_md2_init(EVP_MD_CTX *ctx);
166ebfedea0SLionel Sambuc static int digest_md2_update(EVP_MD_CTX *ctx, const void *data,
167ebfedea0SLionel Sambuc                              unsigned long count);
168ebfedea0SLionel Sambuc static int digest_md2_final(EVP_MD_CTX *ctx, unsigned char *md);
169ebfedea0SLionel Sambuc static int digest_md5_init(EVP_MD_CTX *ctx);
170ebfedea0SLionel Sambuc static int digest_md5_update(EVP_MD_CTX *ctx, const void *data,
171ebfedea0SLionel Sambuc                              unsigned long count);
172ebfedea0SLionel Sambuc static int digest_md5_final(EVP_MD_CTX *ctx, unsigned char *md);
173ebfedea0SLionel Sambuc 
174ebfedea0SLionel Sambuc /*****************************************************************************
175ebfedea0SLionel Sambuc  * Our MD digests
176ebfedea0SLionel Sambuc  **/
177*0a6a1f1dSLionel Sambuc static const EVP_MD digest_md2 = {
178ebfedea0SLionel Sambuc     NID_md2,
179ebfedea0SLionel Sambuc     NID_md2WithRSAEncryption,
180ebfedea0SLionel Sambuc     16,
181ebfedea0SLionel Sambuc     0,
182ebfedea0SLionel Sambuc     digest_md2_init,
183ebfedea0SLionel Sambuc     digest_md2_update,
184ebfedea0SLionel Sambuc     digest_md2_final,
185ebfedea0SLionel Sambuc     NULL,
186ebfedea0SLionel Sambuc     NULL,
187ebfedea0SLionel Sambuc     EVP_PKEY_RSA_method,
188ebfedea0SLionel Sambuc     16,
189ebfedea0SLionel Sambuc     sizeof(MD2_CTX)
190ebfedea0SLionel Sambuc };
191ebfedea0SLionel Sambuc 
192*0a6a1f1dSLionel Sambuc static const EVP_MD digest_md5 = {
193ebfedea0SLionel Sambuc     NID_md5,
194ebfedea0SLionel Sambuc     NID_md5WithRSAEncryption,
195ebfedea0SLionel Sambuc     16,
196ebfedea0SLionel Sambuc     0,
197ebfedea0SLionel Sambuc     digest_md5_init,
198ebfedea0SLionel Sambuc     digest_md5_update,
199ebfedea0SLionel Sambuc     digest_md5_final,
200ebfedea0SLionel Sambuc     NULL,
201ebfedea0SLionel Sambuc     NULL,
202ebfedea0SLionel Sambuc     EVP_PKEY_RSA_method,
203ebfedea0SLionel Sambuc     64,
204ebfedea0SLionel Sambuc     sizeof(MD5_CTX)
205ebfedea0SLionel Sambuc };
206ebfedea0SLionel Sambuc 
207ebfedea0SLionel Sambuc /*****************************************************************************
208ebfedea0SLionel Sambuc  *** Function definitions                                                  ***
209ebfedea0SLionel Sambuc  *****************************************************************************/
210ebfedea0SLionel Sambuc 
211ebfedea0SLionel Sambuc /*****************************************************************************
212ebfedea0SLionel Sambuc  * Functions to handle the engine
213ebfedea0SLionel Sambuc  **/
214ebfedea0SLionel Sambuc 
bind_rsaref(ENGINE * e)215ebfedea0SLionel Sambuc static int bind_rsaref(ENGINE *e)
216ebfedea0SLionel Sambuc {
217ebfedea0SLionel Sambuc     const RSA_METHOD *meth1;
218ebfedea0SLionel Sambuc     if (!ENGINE_set_id(e, engine_rsaref_id)
219ebfedea0SLionel Sambuc         || !ENGINE_set_name(e, engine_rsaref_name)
220ebfedea0SLionel Sambuc         || !ENGINE_set_RSA(e, &rsaref_rsa)
221ebfedea0SLionel Sambuc         || !ENGINE_set_ciphers(e, rsaref_ciphers)
222ebfedea0SLionel Sambuc         || !ENGINE_set_digests(e, rsaref_digests)
223ebfedea0SLionel Sambuc         || !ENGINE_set_destroy_function(e, rsaref_destroy)
224ebfedea0SLionel Sambuc         || !ENGINE_set_init_function(e, rsaref_init)
225ebfedea0SLionel Sambuc         || !ENGINE_set_finish_function(e, rsaref_finish)
226ebfedea0SLionel Sambuc         /* || !ENGINE_set_ctrl_function(e, rsaref_ctrl) */
227*0a6a1f1dSLionel Sambuc         /*
228*0a6a1f1dSLionel Sambuc          * || !ENGINE_set_cmd_defns(e, rsaref_cmd_defns)
229*0a6a1f1dSLionel Sambuc          */ )
230ebfedea0SLionel Sambuc         return 0;
231ebfedea0SLionel Sambuc 
232ebfedea0SLionel Sambuc     /* Ensure the rsaref error handling is set up */
233ebfedea0SLionel Sambuc     ERR_load_RSAREF_strings();
234ebfedea0SLionel Sambuc     return 1;
235ebfedea0SLionel Sambuc }
236ebfedea0SLionel Sambuc 
237ebfedea0SLionel Sambuc #ifdef ENGINE_DYNAMIC_SUPPORT
bind_helper(ENGINE * e,const char * id)238ebfedea0SLionel Sambuc static int bind_helper(ENGINE *e, const char *id)
239ebfedea0SLionel Sambuc {
240ebfedea0SLionel Sambuc     if (id && (strcmp(id, engine_rsaref_id) != 0))
241ebfedea0SLionel Sambuc         return 0;
242ebfedea0SLionel Sambuc     if (!bind_rsaref(e))
243ebfedea0SLionel Sambuc         return 0;
244ebfedea0SLionel Sambuc     return 1;
245ebfedea0SLionel Sambuc }
246*0a6a1f1dSLionel Sambuc 
247ebfedea0SLionel Sambuc IMPLEMENT_DYNAMIC_CHECK_FN()
IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)248ebfedea0SLionel Sambuc     IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
249ebfedea0SLionel Sambuc #else
250ebfedea0SLionel Sambuc static ENGINE *engine_rsaref(void)
251ebfedea0SLionel Sambuc {
252ebfedea0SLionel Sambuc     ENGINE *ret = ENGINE_new();
253ebfedea0SLionel Sambuc     if (!ret)
254ebfedea0SLionel Sambuc         return NULL;
255*0a6a1f1dSLionel Sambuc     if (!bind_rsaref(ret)) {
256ebfedea0SLionel Sambuc         ENGINE_free(ret);
257ebfedea0SLionel Sambuc         return NULL;
258ebfedea0SLionel Sambuc     }
259ebfedea0SLionel Sambuc     return ret;
260ebfedea0SLionel Sambuc }
261ebfedea0SLionel Sambuc 
262ebfedea0SLionel Sambuc void ENGINE_load_rsaref(void)
263ebfedea0SLionel Sambuc {
264ebfedea0SLionel Sambuc     /* Copied from eng_[openssl|dyn].c */
265ebfedea0SLionel Sambuc     ENGINE *toadd = engine_rsaref();
266*0a6a1f1dSLionel Sambuc     if (!toadd)
267*0a6a1f1dSLionel Sambuc         return;
268ebfedea0SLionel Sambuc     ENGINE_add(toadd);
269ebfedea0SLionel Sambuc     ENGINE_free(toadd);
270ebfedea0SLionel Sambuc     ERR_clear_error();
271ebfedea0SLionel Sambuc }
272ebfedea0SLionel Sambuc #endif
273ebfedea0SLionel Sambuc 
274ebfedea0SLionel Sambuc /* Initiator which is only present to make sure this engine looks available */
275ebfedea0SLionel Sambuc static int rsaref_init(ENGINE *e)
276ebfedea0SLionel Sambuc {
277ebfedea0SLionel Sambuc     return 1;
278ebfedea0SLionel Sambuc }
279ebfedea0SLionel Sambuc 
280ebfedea0SLionel Sambuc /* Finisher which is only present to make sure this engine looks available */
rsaref_finish(ENGINE * e)281ebfedea0SLionel Sambuc static int rsaref_finish(ENGINE *e)
282ebfedea0SLionel Sambuc {
283ebfedea0SLionel Sambuc     return 1;
284ebfedea0SLionel Sambuc }
285ebfedea0SLionel Sambuc 
286ebfedea0SLionel Sambuc /* Destructor (complements the "ENGINE_ncipher()" constructor) */
rsaref_destroy(ENGINE * e)287ebfedea0SLionel Sambuc static int rsaref_destroy(ENGINE *e)
288ebfedea0SLionel Sambuc {
289ebfedea0SLionel Sambuc     ERR_unload_RSAREF_strings();
290ebfedea0SLionel Sambuc     return 1;
291ebfedea0SLionel Sambuc }
292ebfedea0SLionel Sambuc 
293ebfedea0SLionel Sambuc /*****************************************************************************
294ebfedea0SLionel Sambuc  * RSA functions
295ebfedea0SLionel Sambuc  **/
296ebfedea0SLionel Sambuc 
rsaref_mod_exp(BIGNUM * r0,const BIGNUM * I,RSA * rsa)297ebfedea0SLionel Sambuc static int rsaref_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
298ebfedea0SLionel Sambuc {
299ebfedea0SLionel Sambuc     RSAREFerr(RSAREF_F_RSAREF_MOD_EXP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
300ebfedea0SLionel Sambuc     return (0);
301ebfedea0SLionel Sambuc }
302ebfedea0SLionel Sambuc 
bnref_mod_exp(BIGNUM * r,const BIGNUM * a,const BIGNUM * p,const BIGNUM * m,BN_CTX * ctx,BN_MONT_CTX * m_ctx)303ebfedea0SLionel Sambuc static int bnref_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
304ebfedea0SLionel Sambuc                          const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
305ebfedea0SLionel Sambuc {
306ebfedea0SLionel Sambuc     RSAREFerr(RSAREF_F_BNREF_MOD_EXP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
307ebfedea0SLionel Sambuc     return (0);
308ebfedea0SLionel Sambuc }
309ebfedea0SLionel Sambuc 
310ebfedea0SLionel Sambuc /* unsigned char *to:  [max]    */
RSAref_bn2bin(BIGNUM * from,unsigned char * to,int max)311ebfedea0SLionel Sambuc static int RSAref_bn2bin(BIGNUM *from, unsigned char *to, int max)
312ebfedea0SLionel Sambuc {
313ebfedea0SLionel Sambuc     int i;
314ebfedea0SLionel Sambuc 
315ebfedea0SLionel Sambuc     i = BN_num_bytes(from);
316*0a6a1f1dSLionel Sambuc     if (i > max) {
317ebfedea0SLionel Sambuc         RSAREFerr(RSAREF_F_RSAREF_BN2BIN, RSAREF_R_LEN);
318ebfedea0SLionel Sambuc         return (0);
319ebfedea0SLionel Sambuc     }
320ebfedea0SLionel Sambuc 
321ebfedea0SLionel Sambuc     memset(to, 0, (unsigned int)max);
322ebfedea0SLionel Sambuc     if (!BN_bn2bin(from, &(to[max - i])))
323ebfedea0SLionel Sambuc         return (0);
324ebfedea0SLionel Sambuc     return (1);
325ebfedea0SLionel Sambuc }
326ebfedea0SLionel Sambuc 
327ebfedea0SLionel Sambuc #ifdef undef
328ebfedea0SLionel Sambuc /* unsigned char *from:  [max]    */
RSAref_bin2bn(unsigned char * from,BIGNUM * to,int max)329ebfedea0SLionel Sambuc static BIGNUM *RSAref_bin2bn(unsigned char *from, BIGNUM *to, int max)
330ebfedea0SLionel Sambuc {
331ebfedea0SLionel Sambuc     int i;
332ebfedea0SLionel Sambuc     BIGNUM *ret;
333ebfedea0SLionel Sambuc 
334ebfedea0SLionel Sambuc     for (i = 0; i < max; i++)
335*0a6a1f1dSLionel Sambuc         if (from[i])
336*0a6a1f1dSLionel Sambuc             break;
337ebfedea0SLionel Sambuc 
338ebfedea0SLionel Sambuc     ret = BN_bin2bn(&(from[i]), max - i, to);
339ebfedea0SLionel Sambuc     return (ret);
340ebfedea0SLionel Sambuc }
341ebfedea0SLionel Sambuc 
RSAref_Public_ref2eay(RSArefPublicKey * from,RSA * to)342ebfedea0SLionel Sambuc static int RSAref_Public_ref2eay(RSArefPublicKey * from, RSA *to)
343ebfedea0SLionel Sambuc {
344ebfedea0SLionel Sambuc     to->n = RSAref_bin2bn(from->m, NULL, RSAref_MAX_LEN);
345ebfedea0SLionel Sambuc     to->e = RSAref_bin2bn(from->e, NULL, RSAref_MAX_LEN);
346*0a6a1f1dSLionel Sambuc     if ((to->n == NULL) || (to->e == NULL))
347*0a6a1f1dSLionel Sambuc         return (0);
348ebfedea0SLionel Sambuc     return (1);
349ebfedea0SLionel Sambuc }
350ebfedea0SLionel Sambuc #endif
351ebfedea0SLionel Sambuc 
RSAref_Public_eay2ref(RSA * from,R_RSA_PUBLIC_KEY * to)352ebfedea0SLionel Sambuc static int RSAref_Public_eay2ref(RSA *from, R_RSA_PUBLIC_KEY * to)
353ebfedea0SLionel Sambuc {
354ebfedea0SLionel Sambuc     to->bits = BN_num_bits(from->n);
355*0a6a1f1dSLionel Sambuc     if (!RSAref_bn2bin(from->n, to->modulus, MAX_RSA_MODULUS_LEN))
356*0a6a1f1dSLionel Sambuc         return (0);
357*0a6a1f1dSLionel Sambuc     if (!RSAref_bn2bin(from->e, to->exponent, MAX_RSA_MODULUS_LEN))
358*0a6a1f1dSLionel Sambuc         return (0);
359ebfedea0SLionel Sambuc     return (1);
360ebfedea0SLionel Sambuc }
361ebfedea0SLionel Sambuc 
362ebfedea0SLionel Sambuc #ifdef undef
RSAref_Private_ref2eay(RSArefPrivateKey * from,RSA * to)363ebfedea0SLionel Sambuc static int RSAref_Private_ref2eay(RSArefPrivateKey * from, RSA *to)
364ebfedea0SLionel Sambuc {
365ebfedea0SLionel Sambuc     if ((to->n = RSAref_bin2bn(from->m, NULL, RSAref_MAX_LEN)) == NULL)
366ebfedea0SLionel Sambuc         return (0);
367ebfedea0SLionel Sambuc     if ((to->e = RSAref_bin2bn(from->e, NULL, RSAref_MAX_LEN)) == NULL)
368ebfedea0SLionel Sambuc         return (0);
369ebfedea0SLionel Sambuc     if ((to->d = RSAref_bin2bn(from->d, NULL, RSAref_MAX_LEN)) == NULL)
370ebfedea0SLionel Sambuc         return (0);
371*0a6a1f1dSLionel Sambuc     if ((to->p =
372*0a6a1f1dSLionel Sambuc          RSAref_bin2bn(from->prime[0], NULL, RSAref_MAX_PLEN)) == NULL)
373ebfedea0SLionel Sambuc         return (0);
374*0a6a1f1dSLionel Sambuc     if ((to->q =
375*0a6a1f1dSLionel Sambuc          RSAref_bin2bn(from->prime[1], NULL, RSAref_MAX_PLEN)) == NULL)
376ebfedea0SLionel Sambuc         return (0);
377ebfedea0SLionel Sambuc     if ((to->dmp1 = RSAref_bin2bn(from->pexp[0], NULL, RSAref_MAX_PLEN))
378ebfedea0SLionel Sambuc         == NULL)
379ebfedea0SLionel Sambuc         return (0);
380ebfedea0SLionel Sambuc     if ((to->dmq1 = RSAref_bin2bn(from->pexp[1], NULL, RSAref_MAX_PLEN))
381ebfedea0SLionel Sambuc         == NULL)
382ebfedea0SLionel Sambuc         return (0);
383ebfedea0SLionel Sambuc     if ((to->iqmp = RSAref_bin2bn(from->coef, NULL, RSAref_MAX_PLEN)) == NULL)
384ebfedea0SLionel Sambuc         return (0);
385ebfedea0SLionel Sambuc     return (1);
386ebfedea0SLionel Sambuc }
387ebfedea0SLionel Sambuc #endif
388ebfedea0SLionel Sambuc 
RSAref_Private_eay2ref(RSA * from,R_RSA_PRIVATE_KEY * to)389ebfedea0SLionel Sambuc static int RSAref_Private_eay2ref(RSA *from, R_RSA_PRIVATE_KEY * to)
390ebfedea0SLionel Sambuc {
391ebfedea0SLionel Sambuc     to->bits = BN_num_bits(from->n);
392*0a6a1f1dSLionel Sambuc     if (!RSAref_bn2bin(from->n, to->modulus, MAX_RSA_MODULUS_LEN))
393*0a6a1f1dSLionel Sambuc         return (0);
394*0a6a1f1dSLionel Sambuc     if (!RSAref_bn2bin(from->e, to->publicExponent, MAX_RSA_MODULUS_LEN))
395*0a6a1f1dSLionel Sambuc         return (0);
396*0a6a1f1dSLionel Sambuc     if (!RSAref_bn2bin(from->d, to->exponent, MAX_RSA_MODULUS_LEN))
397*0a6a1f1dSLionel Sambuc         return (0);
398*0a6a1f1dSLionel Sambuc     if (!RSAref_bn2bin(from->p, to->prime[0], MAX_RSA_PRIME_LEN))
399*0a6a1f1dSLionel Sambuc         return (0);
400*0a6a1f1dSLionel Sambuc     if (!RSAref_bn2bin(from->q, to->prime[1], MAX_RSA_PRIME_LEN))
401*0a6a1f1dSLionel Sambuc         return (0);
402*0a6a1f1dSLionel Sambuc     if (!RSAref_bn2bin(from->dmp1, to->primeExponent[0], MAX_RSA_PRIME_LEN))
403*0a6a1f1dSLionel Sambuc         return (0);
404*0a6a1f1dSLionel Sambuc     if (!RSAref_bn2bin(from->dmq1, to->primeExponent[1], MAX_RSA_PRIME_LEN))
405*0a6a1f1dSLionel Sambuc         return (0);
406*0a6a1f1dSLionel Sambuc     if (!RSAref_bn2bin(from->iqmp, to->coefficient, MAX_RSA_PRIME_LEN))
407*0a6a1f1dSLionel Sambuc         return (0);
408ebfedea0SLionel Sambuc     return (1);
409ebfedea0SLionel Sambuc }
410ebfedea0SLionel Sambuc 
rsaref_private_decrypt(int len,const unsigned char * from,unsigned char * to,RSA * rsa,int padding)411*0a6a1f1dSLionel Sambuc static int rsaref_private_decrypt(int len, const unsigned char *from,
412*0a6a1f1dSLionel Sambuc                                   unsigned char *to, RSA *rsa, int padding)
413ebfedea0SLionel Sambuc {
414ebfedea0SLionel Sambuc     int i, outlen = -1;
415ebfedea0SLionel Sambuc     R_RSA_PRIVATE_KEY RSAkey;
416ebfedea0SLionel Sambuc 
417ebfedea0SLionel Sambuc     if (!RSAref_Private_eay2ref(rsa, &RSAkey))
418ebfedea0SLionel Sambuc         goto err;
419*0a6a1f1dSLionel Sambuc     if ((i =
420*0a6a1f1dSLionel Sambuc          RSAPrivateDecrypt(to, (unsigned int *)&outlen, (unsigned char *)from,
421*0a6a1f1dSLionel Sambuc                            len, &RSAkey)) != 0) {
422ebfedea0SLionel Sambuc         RSAREFerr(RSAREF_F_RSAREF_PRIVATE_DECRYPT, i);
423ebfedea0SLionel Sambuc         outlen = -1;
424ebfedea0SLionel Sambuc     }
425ebfedea0SLionel Sambuc  err:
426ebfedea0SLionel Sambuc     memset(&RSAkey, 0, sizeof(RSAkey));
427ebfedea0SLionel Sambuc     return (outlen);
428ebfedea0SLionel Sambuc }
429ebfedea0SLionel Sambuc 
rsaref_private_encrypt(int len,const unsigned char * from,unsigned char * to,RSA * rsa,int padding)430*0a6a1f1dSLionel Sambuc static int rsaref_private_encrypt(int len, const unsigned char *from,
431*0a6a1f1dSLionel Sambuc                                   unsigned char *to, RSA *rsa, int padding)
432ebfedea0SLionel Sambuc {
433ebfedea0SLionel Sambuc     int i, outlen = -1;
434ebfedea0SLionel Sambuc     R_RSA_PRIVATE_KEY RSAkey;
435ebfedea0SLionel Sambuc 
436*0a6a1f1dSLionel Sambuc     if (padding != RSA_PKCS1_PADDING) {
437*0a6a1f1dSLionel Sambuc         RSAREFerr(RSAREF_F_RSAREF_PRIVATE_ENCRYPT,
438*0a6a1f1dSLionel Sambuc                   RSA_R_UNKNOWN_PADDING_TYPE);
439ebfedea0SLionel Sambuc         goto err;
440ebfedea0SLionel Sambuc     }
441ebfedea0SLionel Sambuc     if (!RSAref_Private_eay2ref(rsa, &RSAkey))
442ebfedea0SLionel Sambuc         goto err;
443*0a6a1f1dSLionel Sambuc     if ((i =
444*0a6a1f1dSLionel Sambuc          RSAPrivateEncrypt(to, (unsigned int *)&outlen, (unsigned char *)from,
445*0a6a1f1dSLionel Sambuc                            len, &RSAkey)) != 0) {
446ebfedea0SLionel Sambuc         RSAREFerr(RSAREF_F_RSAREF_PRIVATE_ENCRYPT, i);
447ebfedea0SLionel Sambuc         outlen = -1;
448ebfedea0SLionel Sambuc     }
449ebfedea0SLionel Sambuc  err:
450ebfedea0SLionel Sambuc     memset(&RSAkey, 0, sizeof(RSAkey));
451ebfedea0SLionel Sambuc     return (outlen);
452ebfedea0SLionel Sambuc }
453ebfedea0SLionel Sambuc 
rsaref_public_decrypt(int len,const unsigned char * from,unsigned char * to,RSA * rsa,int padding)454*0a6a1f1dSLionel Sambuc static int rsaref_public_decrypt(int len, const unsigned char *from,
455*0a6a1f1dSLionel Sambuc                                  unsigned char *to, RSA *rsa, int padding)
456ebfedea0SLionel Sambuc {
457ebfedea0SLionel Sambuc     int i, outlen = -1;
458ebfedea0SLionel Sambuc     R_RSA_PUBLIC_KEY RSAkey;
459ebfedea0SLionel Sambuc 
460ebfedea0SLionel Sambuc     if (!RSAref_Public_eay2ref(rsa, &RSAkey))
461ebfedea0SLionel Sambuc         goto err;
462*0a6a1f1dSLionel Sambuc     if ((i =
463*0a6a1f1dSLionel Sambuc          RSAPublicDecrypt(to, (unsigned int *)&outlen, (unsigned char *)from,
464*0a6a1f1dSLionel Sambuc                           len, &RSAkey)) != 0) {
465ebfedea0SLionel Sambuc         RSAREFerr(RSAREF_F_RSAREF_PUBLIC_DECRYPT, i);
466ebfedea0SLionel Sambuc         outlen = -1;
467ebfedea0SLionel Sambuc     }
468ebfedea0SLionel Sambuc  err:
469ebfedea0SLionel Sambuc     memset(&RSAkey, 0, sizeof(RSAkey));
470ebfedea0SLionel Sambuc     return (outlen);
471ebfedea0SLionel Sambuc }
472ebfedea0SLionel Sambuc 
rsaref_public_encrypt(int len,const unsigned char * from,unsigned char * to,RSA * rsa,int padding)473*0a6a1f1dSLionel Sambuc static int rsaref_public_encrypt(int len, const unsigned char *from,
474*0a6a1f1dSLionel Sambuc                                  unsigned char *to, RSA *rsa, int padding)
475ebfedea0SLionel Sambuc {
476ebfedea0SLionel Sambuc     int outlen = -1;
477ebfedea0SLionel Sambuc     int i;
478ebfedea0SLionel Sambuc     R_RSA_PUBLIC_KEY RSAkey;
479ebfedea0SLionel Sambuc     R_RANDOM_STRUCT rnd;
480ebfedea0SLionel Sambuc     unsigned char buf[16];
481ebfedea0SLionel Sambuc 
482*0a6a1f1dSLionel Sambuc     if (padding != RSA_PKCS1_PADDING && padding != RSA_SSLV23_PADDING) {
483ebfedea0SLionel Sambuc         RSAREFerr(RSAREF_F_RSAREF_PUBLIC_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
484ebfedea0SLionel Sambuc         goto err;
485ebfedea0SLionel Sambuc     }
486ebfedea0SLionel Sambuc 
487ebfedea0SLionel Sambuc     R_RandomInit(&rnd);
488ebfedea0SLionel Sambuc     R_GetRandomBytesNeeded((unsigned int *)&i, &rnd);
489*0a6a1f1dSLionel Sambuc     while (i > 0) {
490ebfedea0SLionel Sambuc         if (RAND_bytes(buf, 16) <= 0)
491ebfedea0SLionel Sambuc             goto err;
492ebfedea0SLionel Sambuc         R_RandomUpdate(&rnd, buf, (unsigned int)((i > 16) ? 16 : i));
493ebfedea0SLionel Sambuc         i -= 16;
494ebfedea0SLionel Sambuc     }
495ebfedea0SLionel Sambuc 
496ebfedea0SLionel Sambuc     if (!RSAref_Public_eay2ref(rsa, &RSAkey))
497ebfedea0SLionel Sambuc         goto err;
498*0a6a1f1dSLionel Sambuc     if ((i =
499*0a6a1f1dSLionel Sambuc          RSAPublicEncrypt(to, (unsigned int *)&outlen, (unsigned char *)from,
500*0a6a1f1dSLionel Sambuc                           len, &RSAkey, &rnd)) != 0) {
501ebfedea0SLionel Sambuc         RSAREFerr(RSAREF_F_RSAREF_PUBLIC_ENCRYPT, i);
502ebfedea0SLionel Sambuc         outlen = -1;
503ebfedea0SLionel Sambuc         goto err;
504ebfedea0SLionel Sambuc     }
505ebfedea0SLionel Sambuc  err:
506ebfedea0SLionel Sambuc     memset(&RSAkey, 0, sizeof(RSAkey));
507ebfedea0SLionel Sambuc     R_RandomFinal(&rnd);
508ebfedea0SLionel Sambuc     memset(&rnd, 0, sizeof(rnd));
509ebfedea0SLionel Sambuc     return (outlen);
510ebfedea0SLionel Sambuc }
511ebfedea0SLionel Sambuc 
512ebfedea0SLionel Sambuc /*****************************************************************************
513ebfedea0SLionel Sambuc  * Symetric cipher and digest function registrars
514ebfedea0SLionel Sambuc  **/
rsaref_ciphers(ENGINE * e,const EVP_CIPHER ** cipher,const int ** nids,int nid)515ebfedea0SLionel Sambuc static int rsaref_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
516ebfedea0SLionel Sambuc                           const int **nids, int nid)
517ebfedea0SLionel Sambuc {
518ebfedea0SLionel Sambuc     int ok = 1;
519*0a6a1f1dSLionel Sambuc     if (!cipher) {
520ebfedea0SLionel Sambuc         /* We are returning a list of supported nids */
521ebfedea0SLionel Sambuc         *nids = rsaref_cipher_nids;
522*0a6a1f1dSLionel Sambuc         return (sizeof(rsaref_cipher_nids) -
523*0a6a1f1dSLionel Sambuc                 1) / sizeof(rsaref_cipher_nids[0]);
524ebfedea0SLionel Sambuc     }
525ebfedea0SLionel Sambuc     /* We are being asked for a specific cipher */
526*0a6a1f1dSLionel Sambuc     switch (nid) {
527ebfedea0SLionel Sambuc     case NID_des_cbc:
528*0a6a1f1dSLionel Sambuc         *cipher = &cipher_des_cbc;
529*0a6a1f1dSLionel Sambuc         break;
530ebfedea0SLionel Sambuc     case NID_des_ede3_cbc:
531*0a6a1f1dSLionel Sambuc         *cipher = &cipher_des_ede3_cbc;
532*0a6a1f1dSLionel Sambuc         break;
533ebfedea0SLionel Sambuc     case NID_desx_cbc:
534*0a6a1f1dSLionel Sambuc         *cipher = &cipher_desx_cbc;
535*0a6a1f1dSLionel Sambuc         break;
536ebfedea0SLionel Sambuc     default:
537ebfedea0SLionel Sambuc         ok = 0;
538ebfedea0SLionel Sambuc         *cipher = NULL;
539ebfedea0SLionel Sambuc         break;
540ebfedea0SLionel Sambuc     }
541ebfedea0SLionel Sambuc     return ok;
542ebfedea0SLionel Sambuc }
543*0a6a1f1dSLionel Sambuc 
rsaref_digests(ENGINE * e,const EVP_MD ** digest,const int ** nids,int nid)544ebfedea0SLionel Sambuc static int rsaref_digests(ENGINE *e, const EVP_MD **digest,
545ebfedea0SLionel Sambuc                           const int **nids, int nid)
546ebfedea0SLionel Sambuc {
547ebfedea0SLionel Sambuc     int ok = 1;
548*0a6a1f1dSLionel Sambuc     if (!digest) {
549ebfedea0SLionel Sambuc         /* We are returning a list of supported nids */
550ebfedea0SLionel Sambuc         *nids = rsaref_digest_nids;
551*0a6a1f1dSLionel Sambuc         return (sizeof(rsaref_digest_nids) -
552*0a6a1f1dSLionel Sambuc                 1) / sizeof(rsaref_digest_nids[0]);
553ebfedea0SLionel Sambuc     }
554ebfedea0SLionel Sambuc     /* We are being asked for a specific digest */
555*0a6a1f1dSLionel Sambuc     switch (nid) {
556ebfedea0SLionel Sambuc     case NID_md2:
557*0a6a1f1dSLionel Sambuc         *digest = &digest_md2;
558*0a6a1f1dSLionel Sambuc         break;
559ebfedea0SLionel Sambuc     case NID_md5:
560*0a6a1f1dSLionel Sambuc         *digest = &digest_md5;
561*0a6a1f1dSLionel Sambuc         break;
562ebfedea0SLionel Sambuc     default:
563ebfedea0SLionel Sambuc         ok = 0;
564ebfedea0SLionel Sambuc         *digest = NULL;
565ebfedea0SLionel Sambuc         break;
566ebfedea0SLionel Sambuc     }
567ebfedea0SLionel Sambuc     return ok;
568ebfedea0SLionel Sambuc }
569ebfedea0SLionel Sambuc 
570ebfedea0SLionel Sambuc /*****************************************************************************
571ebfedea0SLionel Sambuc  * DES functions
572ebfedea0SLionel Sambuc  **/
573ebfedea0SLionel Sambuc #undef data
574ebfedea0SLionel Sambuc #define data(ctx) ((DES_CBC_CTX *)(ctx)->cipher_data)
cipher_des_cbc_init(EVP_CIPHER_CTX * ctx,const unsigned char * key,const unsigned char * iv,int enc)575ebfedea0SLionel Sambuc static int cipher_des_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
576ebfedea0SLionel Sambuc                                const unsigned char *iv, int enc)
577ebfedea0SLionel Sambuc {
578ebfedea0SLionel Sambuc     DES_CBCInit(data(ctx), (unsigned char *)key, (unsigned char *)iv, enc);
579ebfedea0SLionel Sambuc     return 1;
580ebfedea0SLionel Sambuc }
581*0a6a1f1dSLionel Sambuc 
cipher_des_cbc_code(EVP_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,unsigned int inl)582ebfedea0SLionel Sambuc static int cipher_des_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
583ebfedea0SLionel Sambuc                                const unsigned char *in, unsigned int inl)
584ebfedea0SLionel Sambuc {
585ebfedea0SLionel Sambuc     int ret = DES_CBCUpdate(data(ctx), out, (unsigned char *)in, inl);
586*0a6a1f1dSLionel Sambuc     switch (ret) {
587ebfedea0SLionel Sambuc     case RE_LEN:
588*0a6a1f1dSLionel Sambuc         RSAREFerr(RSAREF_F_CIPHER_DES_CBC_CODE,
589*0a6a1f1dSLionel Sambuc                   RSAREF_R_LENGTH_NOT_BLOCK_ALIGNED);
590ebfedea0SLionel Sambuc         break;
591ebfedea0SLionel Sambuc     case 0:
592ebfedea0SLionel Sambuc         break;
593ebfedea0SLionel Sambuc     default:
594ebfedea0SLionel Sambuc         RSAREFerr(RSAREF_F_CIPHER_DES_CBC_CODE, RSAREF_R_UNKNOWN_FAULT);
595ebfedea0SLionel Sambuc     }
596ebfedea0SLionel Sambuc     return !ret;
597ebfedea0SLionel Sambuc }
598*0a6a1f1dSLionel Sambuc 
cipher_des_cbc_clean(EVP_CIPHER_CTX * ctx)599ebfedea0SLionel Sambuc static int cipher_des_cbc_clean(EVP_CIPHER_CTX *ctx)
600ebfedea0SLionel Sambuc {
601ebfedea0SLionel Sambuc     memset(data(ctx), 0, ctx->cipher->ctx_size);
602ebfedea0SLionel Sambuc     return 1;
603ebfedea0SLionel Sambuc }
604ebfedea0SLionel Sambuc 
605ebfedea0SLionel Sambuc #undef data
606ebfedea0SLionel Sambuc #define data(ctx) ((DES3_CBC_CTX *)(ctx)->cipher_data)
cipher_des_ede3_cbc_init(EVP_CIPHER_CTX * ctx,const unsigned char * key,const unsigned char * iv,int enc)607*0a6a1f1dSLionel Sambuc static int cipher_des_ede3_cbc_init(EVP_CIPHER_CTX *ctx,
608*0a6a1f1dSLionel Sambuc                                     const unsigned char *key,
609ebfedea0SLionel Sambuc                                     const unsigned char *iv, int enc)
610ebfedea0SLionel Sambuc {
611*0a6a1f1dSLionel Sambuc     DES3_CBCInit(data(ctx), (unsigned char *)key, (unsigned char *)iv, enc);
612ebfedea0SLionel Sambuc     return 1;
613ebfedea0SLionel Sambuc }
614*0a6a1f1dSLionel Sambuc 
cipher_des_ede3_cbc_code(EVP_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,unsigned int inl)615ebfedea0SLionel Sambuc static int cipher_des_ede3_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
616ebfedea0SLionel Sambuc                                     const unsigned char *in, unsigned int inl)
617ebfedea0SLionel Sambuc {
618ebfedea0SLionel Sambuc     int ret = DES3_CBCUpdate(data(ctx), out, (unsigned char *)in, inl);
619*0a6a1f1dSLionel Sambuc     switch (ret) {
620ebfedea0SLionel Sambuc     case RE_LEN:
621*0a6a1f1dSLionel Sambuc         RSAREFerr(RSAREF_F_CIPHER_DES_CBC_CODE,
622*0a6a1f1dSLionel Sambuc                   RSAREF_R_LENGTH_NOT_BLOCK_ALIGNED);
623ebfedea0SLionel Sambuc         break;
624ebfedea0SLionel Sambuc     case 0:
625ebfedea0SLionel Sambuc         break;
626ebfedea0SLionel Sambuc     default:
627ebfedea0SLionel Sambuc         RSAREFerr(RSAREF_F_CIPHER_DES_CBC_CODE, RSAREF_R_UNKNOWN_FAULT);
628ebfedea0SLionel Sambuc     }
629ebfedea0SLionel Sambuc     return !ret;
630ebfedea0SLionel Sambuc }
631*0a6a1f1dSLionel Sambuc 
cipher_des_ede3_cbc_clean(EVP_CIPHER_CTX * ctx)632ebfedea0SLionel Sambuc static int cipher_des_ede3_cbc_clean(EVP_CIPHER_CTX *ctx)
633ebfedea0SLionel Sambuc {
634ebfedea0SLionel Sambuc     memset(data(ctx), 0, ctx->cipher->ctx_size);
635ebfedea0SLionel Sambuc     return 1;
636ebfedea0SLionel Sambuc }
637ebfedea0SLionel Sambuc 
638ebfedea0SLionel Sambuc #undef data
639ebfedea0SLionel Sambuc #define data(ctx) ((DESX_CBC_CTX *)(ctx)->cipher_data)
cipher_desx_cbc_init(EVP_CIPHER_CTX * ctx,const unsigned char * key,const unsigned char * iv,int enc)640ebfedea0SLionel Sambuc static int cipher_desx_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
641ebfedea0SLionel Sambuc                                 const unsigned char *iv, int enc)
642ebfedea0SLionel Sambuc {
643*0a6a1f1dSLionel Sambuc     DESX_CBCInit(data(ctx), (unsigned char *)key, (unsigned char *)iv, enc);
644ebfedea0SLionel Sambuc     return 1;
645ebfedea0SLionel Sambuc }
646*0a6a1f1dSLionel Sambuc 
cipher_desx_cbc_code(EVP_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,unsigned int inl)647ebfedea0SLionel Sambuc static int cipher_desx_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
648ebfedea0SLionel Sambuc                                 const unsigned char *in, unsigned int inl)
649ebfedea0SLionel Sambuc {
650ebfedea0SLionel Sambuc     int ret = DESX_CBCUpdate(data(ctx), out, (unsigned char *)in, inl);
651*0a6a1f1dSLionel Sambuc     switch (ret) {
652ebfedea0SLionel Sambuc     case RE_LEN:
653*0a6a1f1dSLionel Sambuc         RSAREFerr(RSAREF_F_CIPHER_DES_CBC_CODE,
654*0a6a1f1dSLionel Sambuc                   RSAREF_R_LENGTH_NOT_BLOCK_ALIGNED);
655ebfedea0SLionel Sambuc         break;
656ebfedea0SLionel Sambuc     case 0:
657ebfedea0SLionel Sambuc         break;
658ebfedea0SLionel Sambuc     default:
659ebfedea0SLionel Sambuc         RSAREFerr(RSAREF_F_CIPHER_DES_CBC_CODE, RSAREF_R_UNKNOWN_FAULT);
660ebfedea0SLionel Sambuc     }
661ebfedea0SLionel Sambuc     return !ret;
662ebfedea0SLionel Sambuc }
663*0a6a1f1dSLionel Sambuc 
cipher_desx_cbc_clean(EVP_CIPHER_CTX * ctx)664ebfedea0SLionel Sambuc static int cipher_desx_cbc_clean(EVP_CIPHER_CTX *ctx)
665ebfedea0SLionel Sambuc {
666ebfedea0SLionel Sambuc     memset(data(ctx), 0, ctx->cipher->ctx_size);
667ebfedea0SLionel Sambuc     return 1;
668ebfedea0SLionel Sambuc }
669ebfedea0SLionel Sambuc 
670ebfedea0SLionel Sambuc /*****************************************************************************
671ebfedea0SLionel Sambuc  * MD functions
672ebfedea0SLionel Sambuc  **/
673ebfedea0SLionel Sambuc #undef data
674ebfedea0SLionel Sambuc #define data(ctx) ((MD2_CTX *)(ctx)->md_data)
digest_md2_init(EVP_MD_CTX * ctx)675ebfedea0SLionel Sambuc static int digest_md2_init(EVP_MD_CTX *ctx)
676ebfedea0SLionel Sambuc {
677ebfedea0SLionel Sambuc     MD2Init(data(ctx));
678ebfedea0SLionel Sambuc     return 1;
679ebfedea0SLionel Sambuc }
680*0a6a1f1dSLionel Sambuc 
digest_md2_update(EVP_MD_CTX * ctx,const void * data,unsigned long count)681ebfedea0SLionel Sambuc static int digest_md2_update(EVP_MD_CTX *ctx, const void *data,
682ebfedea0SLionel Sambuc                              unsigned long count)
683ebfedea0SLionel Sambuc {
684ebfedea0SLionel Sambuc     MD2Update(data(ctx), (unsigned char *)data, (unsigned int)count);
685ebfedea0SLionel Sambuc     return 1;
686ebfedea0SLionel Sambuc }
687*0a6a1f1dSLionel Sambuc 
digest_md2_final(EVP_MD_CTX * ctx,unsigned char * md)688ebfedea0SLionel Sambuc static int digest_md2_final(EVP_MD_CTX *ctx, unsigned char *md)
689ebfedea0SLionel Sambuc {
690ebfedea0SLionel Sambuc     MD2Final(md, data(ctx));
691ebfedea0SLionel Sambuc     return 1;
692ebfedea0SLionel Sambuc }
693ebfedea0SLionel Sambuc 
694ebfedea0SLionel Sambuc #undef data
695ebfedea0SLionel Sambuc #define data(ctx) ((MD5_CTX *)(ctx)->md_data)
digest_md5_init(EVP_MD_CTX * ctx)696ebfedea0SLionel Sambuc static int digest_md5_init(EVP_MD_CTX *ctx)
697ebfedea0SLionel Sambuc {
698ebfedea0SLionel Sambuc     MD5Init(data(ctx));
699ebfedea0SLionel Sambuc     return 1;
700ebfedea0SLionel Sambuc }
701*0a6a1f1dSLionel Sambuc 
digest_md5_update(EVP_MD_CTX * ctx,const void * data,unsigned long count)702ebfedea0SLionel Sambuc static int digest_md5_update(EVP_MD_CTX *ctx, const void *data,
703ebfedea0SLionel Sambuc                              unsigned long count)
704ebfedea0SLionel Sambuc {
705ebfedea0SLionel Sambuc     MD5Update(data(ctx), (unsigned char *)data, (unsigned int)count);
706ebfedea0SLionel Sambuc     return 1;
707ebfedea0SLionel Sambuc }
708*0a6a1f1dSLionel Sambuc 
digest_md5_final(EVP_MD_CTX * ctx,unsigned char * md)709ebfedea0SLionel Sambuc static int digest_md5_final(EVP_MD_CTX *ctx, unsigned char *md)
710ebfedea0SLionel Sambuc {
711ebfedea0SLionel Sambuc     MD5Final(md, data(ctx));
712ebfedea0SLionel Sambuc     return 1;
713ebfedea0SLionel Sambuc }
714