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 static int rsaref_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) ());
43 /*****************************************************************************
46 static const ENGINE_CMD_DEFN rsaref_cmd_defns[] = {
50 /*****************************************************************************
53 static int rsaref_private_decrypt(int len, const unsigned char *from,
54 unsigned char *to, RSA *rsa, int padding);
55 static int rsaref_private_encrypt(int len, const unsigned char *from,
56 unsigned char *to, RSA *rsa, int padding);
57 static int rsaref_public_encrypt(int len, const unsigned char *from,
58 unsigned char *to, RSA *rsa, int padding);
59 static int rsaref_public_decrypt(int len, const unsigned char *from,
60 unsigned char *to, RSA *rsa, int padding);
61 static int bnref_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
62 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
63 static int rsaref_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa);
65 /*****************************************************************************
68 static RSA_METHOD rsaref_rsa = {
70 rsaref_public_encrypt,
71 rsaref_public_decrypt,
72 rsaref_private_encrypt,
73 rsaref_private_decrypt,
84 /*****************************************************************************
85 * Symetric cipher and digest function registrars
87 static int rsaref_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
88 const int **nids, int nid);
89 static int rsaref_digests(ENGINE *e, const EVP_MD **digest,
90 const int **nids, int nid);
92 static int rsaref_cipher_nids[] =
93 { NID_des_cbc, NID_des_ede3_cbc, NID_desx_cbc, 0 };
94 static int rsaref_digest_nids[] = { NID_md2, NID_md5, 0 };
96 /*****************************************************************************
99 static int cipher_des_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
100 const unsigned char *iv, int enc);
101 static int cipher_des_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
102 const unsigned char *in, unsigned int inl);
103 static int cipher_des_cbc_clean(EVP_CIPHER_CTX *);
104 static int cipher_des_ede3_cbc_init(EVP_CIPHER_CTX *ctx,
105 const unsigned char *key,
106 const unsigned char *iv, int enc);
107 static int cipher_des_ede3_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
108 const unsigned char *in,
110 static int cipher_des_ede3_cbc_clean(EVP_CIPHER_CTX *);
111 static int cipher_desx_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
112 const unsigned char *iv, int enc);
113 static int cipher_desx_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
114 const unsigned char *in, unsigned int inl);
115 static int cipher_desx_cbc_clean(EVP_CIPHER_CTX *);
117 /*****************************************************************************
120 static const EVP_CIPHER cipher_des_cbc = {
123 0 | EVP_CIPH_CBC_MODE,
126 cipher_des_cbc_clean,
134 static const EVP_CIPHER cipher_des_ede3_cbc = {
137 0 | EVP_CIPH_CBC_MODE,
138 cipher_des_ede3_cbc_init,
139 cipher_des_ede3_cbc_code,
140 cipher_des_ede3_cbc_clean,
141 sizeof(DES3_CBC_CTX),
148 static const EVP_CIPHER cipher_desx_cbc = {
151 0 | EVP_CIPH_CBC_MODE,
152 cipher_desx_cbc_init,
153 cipher_desx_cbc_code,
154 cipher_desx_cbc_clean,
155 sizeof(DESX_CBC_CTX),
162 /*****************************************************************************
165 static int digest_md2_init(EVP_MD_CTX *ctx);
166 static int digest_md2_update(EVP_MD_CTX *ctx, const void *data,
167 unsigned long count);
168 static int digest_md2_final(EVP_MD_CTX *ctx, unsigned char *md);
169 static int digest_md5_init(EVP_MD_CTX *ctx);
170 static int digest_md5_update(EVP_MD_CTX *ctx, const void *data,
171 unsigned long count);
172 static int digest_md5_final(EVP_MD_CTX *ctx, unsigned char *md);
174 /*****************************************************************************
177 static const EVP_MD digest_md2 = {
179 NID_md2WithRSAEncryption,
192 static const EVP_MD digest_md5 = {
194 NID_md5WithRSAEncryption,
207 /*****************************************************************************
208 *** Function definitions ***
209 *****************************************************************************/
211 /*****************************************************************************
212 * Functions to handle the engine
215 static int bind_rsaref(ENGINE *e)
217 const RSA_METHOD *meth1;
218 if (!ENGINE_set_id(e, engine_rsaref_id)
219 || !ENGINE_set_name(e, engine_rsaref_name)
220 || !ENGINE_set_RSA(e, &rsaref_rsa)
221 || !ENGINE_set_ciphers(e, rsaref_ciphers)
222 || !ENGINE_set_digests(e, rsaref_digests)
223 || !ENGINE_set_destroy_function(e, rsaref_destroy)
224 || !ENGINE_set_init_function(e, rsaref_init)
225 || !ENGINE_set_finish_function(e, rsaref_finish)
226 /* || !ENGINE_set_ctrl_function(e, rsaref_ctrl) */
228 * || !ENGINE_set_cmd_defns(e, rsaref_cmd_defns)
232 /* Ensure the rsaref error handling is set up */
233 ERR_load_RSAREF_strings();
237 #ifdef ENGINE_DYNAMIC_SUPPORT
238 static int bind_helper(ENGINE *e, const char *id)
240 if (id && (strcmp(id, engine_rsaref_id) != 0))
247 IMPLEMENT_DYNAMIC_CHECK_FN()
248 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
250 static ENGINE *engine_rsaref(void)
252 ENGINE *ret = ENGINE_new();
255 if (!bind_rsaref(ret)) {
262 void ENGINE_load_rsaref(void)
264 /* Copied from eng_[openssl|dyn].c */
265 ENGINE *toadd = engine_rsaref();
274 /* Initiator which is only present to make sure this engine looks available */
275 static int rsaref_init(ENGINE *e)
280 /* Finisher which is only present to make sure this engine looks available */
281 static int rsaref_finish(ENGINE *e)
286 /* Destructor (complements the "ENGINE_ncipher()" constructor) */
287 static int rsaref_destroy(ENGINE *e)
289 ERR_unload_RSAREF_strings();
293 /*****************************************************************************
297 static int rsaref_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
299 RSAREFerr(RSAREF_F_RSAREF_MOD_EXP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
303 static int bnref_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
304 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
306 RSAREFerr(RSAREF_F_BNREF_MOD_EXP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
310 /* unsigned char *to: [max] */
311 static int RSAref_bn2bin(BIGNUM *from, unsigned char *to, int max)
315 i = BN_num_bytes(from);
317 RSAREFerr(RSAREF_F_RSAREF_BN2BIN, RSAREF_R_LEN);
321 memset(to, 0, (unsigned int)max);
322 if (!BN_bn2bin(from, &(to[max - i])))
328 /* unsigned char *from: [max] */
329 static BIGNUM *RSAref_bin2bn(unsigned char *from, BIGNUM *to, int max)
334 for (i = 0; i < max; i++)
338 ret = BN_bin2bn(&(from[i]), max - i, to);
342 static int RSAref_Public_ref2eay(RSArefPublicKey * from, RSA *to)
344 to->n = RSAref_bin2bn(from->m, NULL, RSAref_MAX_LEN);
345 to->e = RSAref_bin2bn(from->e, NULL, RSAref_MAX_LEN);
346 if ((to->n == NULL) || (to->e == NULL))
352 static int RSAref_Public_eay2ref(RSA *from, R_RSA_PUBLIC_KEY * to)
354 to->bits = BN_num_bits(from->n);
355 if (!RSAref_bn2bin(from->n, to->modulus, MAX_RSA_MODULUS_LEN))
357 if (!RSAref_bn2bin(from->e, to->exponent, MAX_RSA_MODULUS_LEN))
363 static int RSAref_Private_ref2eay(RSArefPrivateKey * from, RSA *to)
365 if ((to->n = RSAref_bin2bn(from->m, NULL, RSAref_MAX_LEN)) == NULL)
367 if ((to->e = RSAref_bin2bn(from->e, NULL, RSAref_MAX_LEN)) == NULL)
369 if ((to->d = RSAref_bin2bn(from->d, NULL, RSAref_MAX_LEN)) == NULL)
372 RSAref_bin2bn(from->prime[0], NULL, RSAref_MAX_PLEN)) == NULL)
375 RSAref_bin2bn(from->prime[1], NULL, RSAref_MAX_PLEN)) == NULL)
377 if ((to->dmp1 = RSAref_bin2bn(from->pexp[0], NULL, RSAref_MAX_PLEN))
380 if ((to->dmq1 = RSAref_bin2bn(from->pexp[1], NULL, RSAref_MAX_PLEN))
383 if ((to->iqmp = RSAref_bin2bn(from->coef, NULL, RSAref_MAX_PLEN)) == NULL)
389 static int RSAref_Private_eay2ref(RSA *from, R_RSA_PRIVATE_KEY * to)
391 to->bits = BN_num_bits(from->n);
392 if (!RSAref_bn2bin(from->n, to->modulus, MAX_RSA_MODULUS_LEN))
394 if (!RSAref_bn2bin(from->e, to->publicExponent, MAX_RSA_MODULUS_LEN))
396 if (!RSAref_bn2bin(from->d, to->exponent, MAX_RSA_MODULUS_LEN))
398 if (!RSAref_bn2bin(from->p, to->prime[0], MAX_RSA_PRIME_LEN))
400 if (!RSAref_bn2bin(from->q, to->prime[1], MAX_RSA_PRIME_LEN))
402 if (!RSAref_bn2bin(from->dmp1, to->primeExponent[0], MAX_RSA_PRIME_LEN))
404 if (!RSAref_bn2bin(from->dmq1, to->primeExponent[1], MAX_RSA_PRIME_LEN))
406 if (!RSAref_bn2bin(from->iqmp, to->coefficient, MAX_RSA_PRIME_LEN))
411 static int rsaref_private_decrypt(int len, const unsigned char *from,
412 unsigned char *to, RSA *rsa, int padding)
415 R_RSA_PRIVATE_KEY RSAkey;
417 if (!RSAref_Private_eay2ref(rsa, &RSAkey))
420 RSAPrivateDecrypt(to, (unsigned int *)&outlen, (unsigned char *)from,
421 len, &RSAkey)) != 0) {
422 RSAREFerr(RSAREF_F_RSAREF_PRIVATE_DECRYPT, i);
426 memset(&RSAkey, 0, sizeof(RSAkey));
430 static int rsaref_private_encrypt(int len, const unsigned char *from,
431 unsigned char *to, RSA *rsa, int padding)
434 R_RSA_PRIVATE_KEY RSAkey;
436 if (padding != RSA_PKCS1_PADDING) {
437 RSAREFerr(RSAREF_F_RSAREF_PRIVATE_ENCRYPT,
438 RSA_R_UNKNOWN_PADDING_TYPE);
441 if (!RSAref_Private_eay2ref(rsa, &RSAkey))
444 RSAPrivateEncrypt(to, (unsigned int *)&outlen, (unsigned char *)from,
445 len, &RSAkey)) != 0) {
446 RSAREFerr(RSAREF_F_RSAREF_PRIVATE_ENCRYPT, i);
450 memset(&RSAkey, 0, sizeof(RSAkey));
454 static int rsaref_public_decrypt(int len, const unsigned char *from,
455 unsigned char *to, RSA *rsa, int padding)
458 R_RSA_PUBLIC_KEY RSAkey;
460 if (!RSAref_Public_eay2ref(rsa, &RSAkey))
463 RSAPublicDecrypt(to, (unsigned int *)&outlen, (unsigned char *)from,
464 len, &RSAkey)) != 0) {
465 RSAREFerr(RSAREF_F_RSAREF_PUBLIC_DECRYPT, i);
469 memset(&RSAkey, 0, sizeof(RSAkey));
473 static int rsaref_public_encrypt(int len, const unsigned char *from,
474 unsigned char *to, RSA *rsa, int padding)
478 R_RSA_PUBLIC_KEY RSAkey;
480 unsigned char buf[16];
482 if (padding != RSA_PKCS1_PADDING && padding != RSA_SSLV23_PADDING) {
483 RSAREFerr(RSAREF_F_RSAREF_PUBLIC_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
488 R_GetRandomBytesNeeded((unsigned int *)&i, &rnd);
490 if (RAND_bytes(buf, 16) <= 0)
492 R_RandomUpdate(&rnd, buf, (unsigned int)((i > 16) ? 16 : i));
496 if (!RSAref_Public_eay2ref(rsa, &RSAkey))
499 RSAPublicEncrypt(to, (unsigned int *)&outlen, (unsigned char *)from,
500 len, &RSAkey, &rnd)) != 0) {
501 RSAREFerr(RSAREF_F_RSAREF_PUBLIC_ENCRYPT, i);
506 memset(&RSAkey, 0, sizeof(RSAkey));
508 memset(&rnd, 0, sizeof(rnd));
512 /*****************************************************************************
513 * Symetric cipher and digest function registrars
515 static int rsaref_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
516 const int **nids, int nid)
520 /* We are returning a list of supported nids */
521 *nids = rsaref_cipher_nids;
522 return (sizeof(rsaref_cipher_nids) -
523 1) / sizeof(rsaref_cipher_nids[0]);
525 /* We are being asked for a specific cipher */
528 *cipher = &cipher_des_cbc;
530 case NID_des_ede3_cbc:
531 *cipher = &cipher_des_ede3_cbc;
534 *cipher = &cipher_desx_cbc;
544 static int rsaref_digests(ENGINE *e, const EVP_MD **digest,
545 const int **nids, int nid)
549 /* We are returning a list of supported nids */
550 *nids = rsaref_digest_nids;
551 return (sizeof(rsaref_digest_nids) -
552 1) / sizeof(rsaref_digest_nids[0]);
554 /* We are being asked for a specific digest */
557 *digest = &digest_md2;
560 *digest = &digest_md5;
570 /*****************************************************************************
574 #define data(ctx) ((DES_CBC_CTX *)(ctx)->cipher_data)
575 static int cipher_des_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
576 const unsigned char *iv, int enc)
578 DES_CBCInit(data(ctx), (unsigned char *)key, (unsigned char *)iv, enc);
582 static int cipher_des_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
583 const unsigned char *in, unsigned int inl)
585 int ret = DES_CBCUpdate(data(ctx), out, (unsigned char *)in, inl);
588 RSAREFerr(RSAREF_F_CIPHER_DES_CBC_CODE,
589 RSAREF_R_LENGTH_NOT_BLOCK_ALIGNED);
594 RSAREFerr(RSAREF_F_CIPHER_DES_CBC_CODE, RSAREF_R_UNKNOWN_FAULT);
599 static int cipher_des_cbc_clean(EVP_CIPHER_CTX *ctx)
601 memset(data(ctx), 0, ctx->cipher->ctx_size);
606 #define data(ctx) ((DES3_CBC_CTX *)(ctx)->cipher_data)
607 static int cipher_des_ede3_cbc_init(EVP_CIPHER_CTX *ctx,
608 const unsigned char *key,
609 const unsigned char *iv, int enc)
611 DES3_CBCInit(data(ctx), (unsigned char *)key, (unsigned char *)iv, enc);
615 static int cipher_des_ede3_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
616 const unsigned char *in, unsigned int inl)
618 int ret = DES3_CBCUpdate(data(ctx), out, (unsigned char *)in, inl);
621 RSAREFerr(RSAREF_F_CIPHER_DES_CBC_CODE,
622 RSAREF_R_LENGTH_NOT_BLOCK_ALIGNED);
627 RSAREFerr(RSAREF_F_CIPHER_DES_CBC_CODE, RSAREF_R_UNKNOWN_FAULT);
632 static int cipher_des_ede3_cbc_clean(EVP_CIPHER_CTX *ctx)
634 memset(data(ctx), 0, ctx->cipher->ctx_size);
639 #define data(ctx) ((DESX_CBC_CTX *)(ctx)->cipher_data)
640 static int cipher_desx_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
641 const unsigned char *iv, int enc)
643 DESX_CBCInit(data(ctx), (unsigned char *)key, (unsigned char *)iv, enc);
647 static int cipher_desx_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
648 const unsigned char *in, unsigned int inl)
650 int ret = DESX_CBCUpdate(data(ctx), out, (unsigned char *)in, inl);
653 RSAREFerr(RSAREF_F_CIPHER_DES_CBC_CODE,
654 RSAREF_R_LENGTH_NOT_BLOCK_ALIGNED);
659 RSAREFerr(RSAREF_F_CIPHER_DES_CBC_CODE, RSAREF_R_UNKNOWN_FAULT);
664 static int cipher_desx_cbc_clean(EVP_CIPHER_CTX *ctx)
666 memset(data(ctx), 0, ctx->cipher->ctx_size);
670 /*****************************************************************************
674 #define data(ctx) ((MD2_CTX *)(ctx)->md_data)
675 static int digest_md2_init(EVP_MD_CTX *ctx)
681 static int digest_md2_update(EVP_MD_CTX *ctx, const void *data,
684 MD2Update(data(ctx), (unsigned char *)data, (unsigned int)count);
688 static int digest_md2_final(EVP_MD_CTX *ctx, unsigned char *md)
690 MD2Final(md, data(ctx));
695 #define data(ctx) ((MD5_CTX *)(ctx)->md_data)
696 static int digest_md5_init(EVP_MD_CTX *ctx)
702 static int digest_md5_update(EVP_MD_CTX *ctx, const void *data,
705 MD5Update(data(ctx), (unsigned char *)data, (unsigned int)count);
709 static int digest_md5_final(EVP_MD_CTX *ctx, unsigned char *md)
711 MD5Final(md, data(ctx));