2 * Demo of how to construct your own engine and using it. The basis of this
3 * engine is RSAref, an old reference of the RSA algorithm which can still be
4 * found a little here and there.
9 #include "./source/global.h"
10 #include "./source/rsaref.h"
11 #include "./source/rsa.h"
12 #include "./source/des.h"
13 #include <openssl/err.h>
14 #define OPENSSL_NO_MD2
15 #define OPENSSL_NO_MD5
16 #include <openssl/evp.h>
17 #include <openssl/bn.h>
18 #include <openssl/engine.h>
20 #define RSAREF_LIB_NAME "rsaref engine"
21 #include "rsaref_err.c"
23 /*****************************************************************************
24 *** Function declarations and global variable definitions ***
25 *****************************************************************************/
27 /*****************************************************************************
28 * Constants used when creating the ENGINE
30 static const char *engine_rsaref_id = "rsaref";
31 static const char *engine_rsaref_name = "RSAref engine support";
33 /*****************************************************************************
34 * Functions to handle the engine
36 static int rsaref_destroy(ENGINE *e);
37 static int rsaref_init(ENGINE *e);
38 static int rsaref_finish(ENGINE *e);
40 /*****************************************************************************
43 static const ENGINE_CMD_DEFN rsaref_cmd_defns[] = {
47 /*****************************************************************************
50 static int rsaref_private_decrypt(int len, const unsigned char *from,
51 unsigned char *to, RSA *rsa, int padding);
52 static int rsaref_private_encrypt(int len, const unsigned char *from,
53 unsigned char *to, RSA *rsa, int padding);
54 static int rsaref_public_encrypt(int len, const unsigned char *from,
55 unsigned char *to, RSA *rsa, int padding);
56 static int rsaref_public_decrypt(int len, const unsigned char *from,
57 unsigned char *to, RSA *rsa, int padding);
58 static int bnref_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
59 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
60 static int rsaref_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa);
62 /*****************************************************************************
65 static RSA_METHOD rsaref_rsa = {
67 rsaref_public_encrypt,
68 rsaref_public_decrypt,
69 rsaref_private_encrypt,
70 rsaref_private_decrypt,
81 /*****************************************************************************
82 * Symetric cipher and digest function registrars
84 static int rsaref_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
85 const int **nids, int nid);
86 static int rsaref_digests(ENGINE *e, const EVP_MD **digest,
87 const int **nids, int nid);
89 static int rsaref_cipher_nids[] =
90 { NID_des_cbc, NID_des_ede3_cbc, NID_desx_cbc, 0 };
91 static int rsaref_digest_nids[] = { NID_md2, NID_md5, 0 };
93 /*****************************************************************************
96 static int cipher_des_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
97 const unsigned char *iv, int enc);
98 static int cipher_des_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
99 const unsigned char *in, unsigned int inl);
100 static int cipher_des_cbc_clean(EVP_CIPHER_CTX *);
101 static int cipher_des_ede3_cbc_init(EVP_CIPHER_CTX *ctx,
102 const unsigned char *key,
103 const unsigned char *iv, int enc);
104 static int cipher_des_ede3_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
105 const unsigned char *in,
107 static int cipher_des_ede3_cbc_clean(EVP_CIPHER_CTX *);
108 static int cipher_desx_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
109 const unsigned char *iv, int enc);
110 static int cipher_desx_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
111 const unsigned char *in, unsigned int inl);
112 static int cipher_desx_cbc_clean(EVP_CIPHER_CTX *);
114 /*****************************************************************************
117 static const EVP_CIPHER cipher_des_cbc = {
120 0 | EVP_CIPH_CBC_MODE,
123 cipher_des_cbc_clean,
131 static const EVP_CIPHER cipher_des_ede3_cbc = {
134 0 | EVP_CIPH_CBC_MODE,
135 cipher_des_ede3_cbc_init,
136 cipher_des_ede3_cbc_code,
137 cipher_des_ede3_cbc_clean,
138 sizeof(DES3_CBC_CTX),
145 static const EVP_CIPHER cipher_desx_cbc = {
148 0 | EVP_CIPH_CBC_MODE,
149 cipher_desx_cbc_init,
150 cipher_desx_cbc_code,
151 cipher_desx_cbc_clean,
152 sizeof(DESX_CBC_CTX),
159 /*****************************************************************************
162 static int digest_md2_init(EVP_MD_CTX *ctx);
163 static int digest_md2_update(EVP_MD_CTX *ctx, const void *data,
164 unsigned long count);
165 static int digest_md2_final(EVP_MD_CTX *ctx, unsigned char *md);
166 static int digest_md5_init(EVP_MD_CTX *ctx);
167 static int digest_md5_update(EVP_MD_CTX *ctx, const void *data,
168 unsigned long count);
169 static int digest_md5_final(EVP_MD_CTX *ctx, unsigned char *md);
171 /*****************************************************************************
174 static const EVP_MD digest_md2 = {
176 NID_md2WithRSAEncryption,
189 static const EVP_MD digest_md5 = {
191 NID_md5WithRSAEncryption,
204 /*****************************************************************************
205 *** Function definitions ***
206 *****************************************************************************/
208 /*****************************************************************************
209 * Functions to handle the engine
212 static int bind_rsaref(ENGINE *e)
214 const RSA_METHOD *meth1;
215 if (!ENGINE_set_id(e, engine_rsaref_id)
216 || !ENGINE_set_name(e, engine_rsaref_name)
217 || !ENGINE_set_RSA(e, &rsaref_rsa)
218 || !ENGINE_set_ciphers(e, rsaref_ciphers)
219 || !ENGINE_set_digests(e, rsaref_digests)
220 || !ENGINE_set_destroy_function(e, rsaref_destroy)
221 || !ENGINE_set_init_function(e, rsaref_init)
222 || !ENGINE_set_finish_function(e, rsaref_finish)
223 /* || !ENGINE_set_ctrl_function(e, rsaref_ctrl) */
225 * || !ENGINE_set_cmd_defns(e, rsaref_cmd_defns)
229 /* Ensure the rsaref error handling is set up */
230 ERR_load_RSAREF_strings();
234 #ifdef ENGINE_DYNAMIC_SUPPORT
235 static int bind_helper(ENGINE *e, const char *id)
237 if (id && (strcmp(id, engine_rsaref_id) != 0))
244 IMPLEMENT_DYNAMIC_CHECK_FN()
245 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
247 static ENGINE *engine_rsaref(void)
249 ENGINE *ret = ENGINE_new();
252 if (!bind_rsaref(ret)) {
259 void ENGINE_load_rsaref(void)
261 /* Copied from eng_[openssl|dyn].c */
262 ENGINE *toadd = engine_rsaref();
271 /* Initiator which is only present to make sure this engine looks available */
272 static int rsaref_init(ENGINE *e)
277 /* Finisher which is only present to make sure this engine looks available */
278 static int rsaref_finish(ENGINE *e)
283 /* Destructor (complements the "ENGINE_ncipher()" constructor) */
284 static int rsaref_destroy(ENGINE *e)
286 ERR_unload_RSAREF_strings();
290 /*****************************************************************************
294 static int rsaref_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
296 RSAREFerr(RSAREF_F_RSAREF_MOD_EXP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
300 static int bnref_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
301 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
303 RSAREFerr(RSAREF_F_BNREF_MOD_EXP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
307 /* unsigned char *to: [max] */
308 static int RSAref_bn2bin(BIGNUM *from, unsigned char *to, int max)
312 i = BN_num_bytes(from);
314 RSAREFerr(RSAREF_F_RSAREF_BN2BIN, RSAREF_R_LEN);
318 memset(to, 0, (unsigned int)max);
319 if (!BN_bn2bin(from, &(to[max - i])))
324 static int RSAref_Public_eay2ref(RSA *from, R_RSA_PUBLIC_KEY * to)
326 to->bits = BN_num_bits(from->n);
327 if (!RSAref_bn2bin(from->n, to->modulus, MAX_RSA_MODULUS_LEN))
329 if (!RSAref_bn2bin(from->e, to->exponent, MAX_RSA_MODULUS_LEN))
334 static int RSAref_Private_eay2ref(RSA *from, R_RSA_PRIVATE_KEY * to)
336 to->bits = BN_num_bits(from->n);
337 if (!RSAref_bn2bin(from->n, to->modulus, MAX_RSA_MODULUS_LEN))
339 if (!RSAref_bn2bin(from->e, to->publicExponent, MAX_RSA_MODULUS_LEN))
341 if (!RSAref_bn2bin(from->d, to->exponent, MAX_RSA_MODULUS_LEN))
343 if (!RSAref_bn2bin(from->p, to->prime[0], MAX_RSA_PRIME_LEN))
345 if (!RSAref_bn2bin(from->q, to->prime[1], MAX_RSA_PRIME_LEN))
347 if (!RSAref_bn2bin(from->dmp1, to->primeExponent[0], MAX_RSA_PRIME_LEN))
349 if (!RSAref_bn2bin(from->dmq1, to->primeExponent[1], MAX_RSA_PRIME_LEN))
351 if (!RSAref_bn2bin(from->iqmp, to->coefficient, MAX_RSA_PRIME_LEN))
356 static int rsaref_private_decrypt(int len, const unsigned char *from,
357 unsigned char *to, RSA *rsa, int padding)
360 R_RSA_PRIVATE_KEY RSAkey;
362 if (!RSAref_Private_eay2ref(rsa, &RSAkey))
365 RSAPrivateDecrypt(to, (unsigned int *)&outlen, (unsigned char *)from,
366 len, &RSAkey)) != 0) {
367 RSAREFerr(RSAREF_F_RSAREF_PRIVATE_DECRYPT, i);
371 memset(&RSAkey, 0, sizeof(RSAkey));
375 static int rsaref_private_encrypt(int len, const unsigned char *from,
376 unsigned char *to, RSA *rsa, int padding)
379 R_RSA_PRIVATE_KEY RSAkey;
381 if (padding != RSA_PKCS1_PADDING) {
382 RSAREFerr(RSAREF_F_RSAREF_PRIVATE_ENCRYPT,
383 RSA_R_UNKNOWN_PADDING_TYPE);
386 if (!RSAref_Private_eay2ref(rsa, &RSAkey))
389 RSAPrivateEncrypt(to, (unsigned int *)&outlen, (unsigned char *)from,
390 len, &RSAkey)) != 0) {
391 RSAREFerr(RSAREF_F_RSAREF_PRIVATE_ENCRYPT, i);
395 memset(&RSAkey, 0, sizeof(RSAkey));
399 static int rsaref_public_decrypt(int len, const unsigned char *from,
400 unsigned char *to, RSA *rsa, int padding)
403 R_RSA_PUBLIC_KEY RSAkey;
405 if (!RSAref_Public_eay2ref(rsa, &RSAkey))
408 RSAPublicDecrypt(to, (unsigned int *)&outlen, (unsigned char *)from,
409 len, &RSAkey)) != 0) {
410 RSAREFerr(RSAREF_F_RSAREF_PUBLIC_DECRYPT, i);
414 memset(&RSAkey, 0, sizeof(RSAkey));
418 static int rsaref_public_encrypt(int len, const unsigned char *from,
419 unsigned char *to, RSA *rsa, int padding)
423 R_RSA_PUBLIC_KEY RSAkey;
425 unsigned char buf[16];
427 if (padding != RSA_PKCS1_PADDING && padding != RSA_SSLV23_PADDING) {
428 RSAREFerr(RSAREF_F_RSAREF_PUBLIC_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
433 R_GetRandomBytesNeeded((unsigned int *)&i, &rnd);
435 if (RAND_bytes(buf, 16) <= 0)
437 R_RandomUpdate(&rnd, buf, (unsigned int)((i > 16) ? 16 : i));
441 if (!RSAref_Public_eay2ref(rsa, &RSAkey))
444 RSAPublicEncrypt(to, (unsigned int *)&outlen, (unsigned char *)from,
445 len, &RSAkey, &rnd)) != 0) {
446 RSAREFerr(RSAREF_F_RSAREF_PUBLIC_ENCRYPT, i);
451 memset(&RSAkey, 0, sizeof(RSAkey));
453 memset(&rnd, 0, sizeof(rnd));
457 /*****************************************************************************
458 * Symetric cipher and digest function registrars
460 static int rsaref_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
461 const int **nids, int nid)
465 /* We are returning a list of supported nids */
466 *nids = rsaref_cipher_nids;
467 return (sizeof(rsaref_cipher_nids) -
468 1) / sizeof(rsaref_cipher_nids[0]);
470 /* We are being asked for a specific cipher */
473 *cipher = &cipher_des_cbc;
475 case NID_des_ede3_cbc:
476 *cipher = &cipher_des_ede3_cbc;
479 *cipher = &cipher_desx_cbc;
489 static int rsaref_digests(ENGINE *e, const EVP_MD **digest,
490 const int **nids, int nid)
494 /* We are returning a list of supported nids */
495 *nids = rsaref_digest_nids;
496 return (sizeof(rsaref_digest_nids) -
497 1) / sizeof(rsaref_digest_nids[0]);
499 /* We are being asked for a specific digest */
502 *digest = &digest_md2;
505 *digest = &digest_md5;
515 /*****************************************************************************
519 #define data(ctx) ((DES_CBC_CTX *)(ctx)->cipher_data)
520 static int cipher_des_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
521 const unsigned char *iv, int enc)
523 DES_CBCInit(data(ctx), (unsigned char *)key, (unsigned char *)iv, enc);
527 static int cipher_des_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
528 const unsigned char *in, unsigned int inl)
530 int ret = DES_CBCUpdate(data(ctx), out, (unsigned char *)in, inl);
533 RSAREFerr(RSAREF_F_CIPHER_DES_CBC_CODE,
534 RSAREF_R_LENGTH_NOT_BLOCK_ALIGNED);
539 RSAREFerr(RSAREF_F_CIPHER_DES_CBC_CODE, RSAREF_R_UNKNOWN_FAULT);
544 static int cipher_des_cbc_clean(EVP_CIPHER_CTX *ctx)
546 memset(data(ctx), 0, ctx->cipher->ctx_size);
551 #define data(ctx) ((DES3_CBC_CTX *)(ctx)->cipher_data)
552 static int cipher_des_ede3_cbc_init(EVP_CIPHER_CTX *ctx,
553 const unsigned char *key,
554 const unsigned char *iv, int enc)
556 DES3_CBCInit(data(ctx), (unsigned char *)key, (unsigned char *)iv, enc);
560 static int cipher_des_ede3_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
561 const unsigned char *in, unsigned int inl)
563 int ret = DES3_CBCUpdate(data(ctx), out, (unsigned char *)in, inl);
566 RSAREFerr(RSAREF_F_CIPHER_DES_CBC_CODE,
567 RSAREF_R_LENGTH_NOT_BLOCK_ALIGNED);
572 RSAREFerr(RSAREF_F_CIPHER_DES_CBC_CODE, RSAREF_R_UNKNOWN_FAULT);
577 static int cipher_des_ede3_cbc_clean(EVP_CIPHER_CTX *ctx)
579 memset(data(ctx), 0, ctx->cipher->ctx_size);
584 #define data(ctx) ((DESX_CBC_CTX *)(ctx)->cipher_data)
585 static int cipher_desx_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
586 const unsigned char *iv, int enc)
588 DESX_CBCInit(data(ctx), (unsigned char *)key, (unsigned char *)iv, enc);
592 static int cipher_desx_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
593 const unsigned char *in, unsigned int inl)
595 int ret = DESX_CBCUpdate(data(ctx), out, (unsigned char *)in, inl);
598 RSAREFerr(RSAREF_F_CIPHER_DES_CBC_CODE,
599 RSAREF_R_LENGTH_NOT_BLOCK_ALIGNED);
604 RSAREFerr(RSAREF_F_CIPHER_DES_CBC_CODE, RSAREF_R_UNKNOWN_FAULT);
609 static int cipher_desx_cbc_clean(EVP_CIPHER_CTX *ctx)
611 memset(data(ctx), 0, ctx->cipher->ctx_size);
615 /*****************************************************************************
619 #define data(ctx) ((MD2_CTX *)(ctx)->md_data)
620 static int digest_md2_init(EVP_MD_CTX *ctx)
626 static int digest_md2_update(EVP_MD_CTX *ctx, const void *data,
629 MD2Update(data(ctx), (unsigned char *)data, (unsigned int)count);
633 static int digest_md2_final(EVP_MD_CTX *ctx, unsigned char *md)
635 MD2Final(md, data(ctx));
640 #define data(ctx) ((MD5_CTX *)(ctx)->md_data)
641 static int digest_md5_init(EVP_MD_CTX *ctx)
647 static int digest_md5_update(EVP_MD_CTX *ctx, const void *data,
650 MD5Update(data(ctx), (unsigned char *)data, (unsigned int)count);
654 static int digest_md5_final(EVP_MD_CTX *ctx, unsigned char *md)
656 MD5Final(md, data(ctx));