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])))
327 static int RSAref_Public_eay2ref(RSA *from, R_RSA_PUBLIC_KEY * to)
329 to->bits = BN_num_bits(from->n);
330 if (!RSAref_bn2bin(from->n, to->modulus, MAX_RSA_MODULUS_LEN))
332 if (!RSAref_bn2bin(from->e, to->exponent, MAX_RSA_MODULUS_LEN))
337 static int RSAref_Private_eay2ref(RSA *from, R_RSA_PRIVATE_KEY * to)
339 to->bits = BN_num_bits(from->n);
340 if (!RSAref_bn2bin(from->n, to->modulus, MAX_RSA_MODULUS_LEN))
342 if (!RSAref_bn2bin(from->e, to->publicExponent, MAX_RSA_MODULUS_LEN))
344 if (!RSAref_bn2bin(from->d, to->exponent, MAX_RSA_MODULUS_LEN))
346 if (!RSAref_bn2bin(from->p, to->prime[0], MAX_RSA_PRIME_LEN))
348 if (!RSAref_bn2bin(from->q, to->prime[1], MAX_RSA_PRIME_LEN))
350 if (!RSAref_bn2bin(from->dmp1, to->primeExponent[0], MAX_RSA_PRIME_LEN))
352 if (!RSAref_bn2bin(from->dmq1, to->primeExponent[1], MAX_RSA_PRIME_LEN))
354 if (!RSAref_bn2bin(from->iqmp, to->coefficient, MAX_RSA_PRIME_LEN))
359 static int rsaref_private_decrypt(int len, const unsigned char *from,
360 unsigned char *to, RSA *rsa, int padding)
363 R_RSA_PRIVATE_KEY RSAkey;
365 if (!RSAref_Private_eay2ref(rsa, &RSAkey))
368 RSAPrivateDecrypt(to, (unsigned int *)&outlen, (unsigned char *)from,
369 len, &RSAkey)) != 0) {
370 RSAREFerr(RSAREF_F_RSAREF_PRIVATE_DECRYPT, i);
374 memset(&RSAkey, 0, sizeof(RSAkey));
378 static int rsaref_private_encrypt(int len, const unsigned char *from,
379 unsigned char *to, RSA *rsa, int padding)
382 R_RSA_PRIVATE_KEY RSAkey;
384 if (padding != RSA_PKCS1_PADDING) {
385 RSAREFerr(RSAREF_F_RSAREF_PRIVATE_ENCRYPT,
386 RSA_R_UNKNOWN_PADDING_TYPE);
389 if (!RSAref_Private_eay2ref(rsa, &RSAkey))
392 RSAPrivateEncrypt(to, (unsigned int *)&outlen, (unsigned char *)from,
393 len, &RSAkey)) != 0) {
394 RSAREFerr(RSAREF_F_RSAREF_PRIVATE_ENCRYPT, i);
398 memset(&RSAkey, 0, sizeof(RSAkey));
402 static int rsaref_public_decrypt(int len, const unsigned char *from,
403 unsigned char *to, RSA *rsa, int padding)
406 R_RSA_PUBLIC_KEY RSAkey;
408 if (!RSAref_Public_eay2ref(rsa, &RSAkey))
411 RSAPublicDecrypt(to, (unsigned int *)&outlen, (unsigned char *)from,
412 len, &RSAkey)) != 0) {
413 RSAREFerr(RSAREF_F_RSAREF_PUBLIC_DECRYPT, i);
417 memset(&RSAkey, 0, sizeof(RSAkey));
421 static int rsaref_public_encrypt(int len, const unsigned char *from,
422 unsigned char *to, RSA *rsa, int padding)
426 R_RSA_PUBLIC_KEY RSAkey;
428 unsigned char buf[16];
430 if (padding != RSA_PKCS1_PADDING && padding != RSA_SSLV23_PADDING) {
431 RSAREFerr(RSAREF_F_RSAREF_PUBLIC_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
436 R_GetRandomBytesNeeded((unsigned int *)&i, &rnd);
438 if (RAND_bytes(buf, 16) <= 0)
440 R_RandomUpdate(&rnd, buf, (unsigned int)((i > 16) ? 16 : i));
444 if (!RSAref_Public_eay2ref(rsa, &RSAkey))
447 RSAPublicEncrypt(to, (unsigned int *)&outlen, (unsigned char *)from,
448 len, &RSAkey, &rnd)) != 0) {
449 RSAREFerr(RSAREF_F_RSAREF_PUBLIC_ENCRYPT, i);
454 memset(&RSAkey, 0, sizeof(RSAkey));
456 memset(&rnd, 0, sizeof(rnd));
460 /*****************************************************************************
461 * Symetric cipher and digest function registrars
463 static int rsaref_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
464 const int **nids, int nid)
468 /* We are returning a list of supported nids */
469 *nids = rsaref_cipher_nids;
470 return (sizeof(rsaref_cipher_nids) -
471 1) / sizeof(rsaref_cipher_nids[0]);
473 /* We are being asked for a specific cipher */
476 *cipher = &cipher_des_cbc;
478 case NID_des_ede3_cbc:
479 *cipher = &cipher_des_ede3_cbc;
482 *cipher = &cipher_desx_cbc;
492 static int rsaref_digests(ENGINE *e, const EVP_MD **digest,
493 const int **nids, int nid)
497 /* We are returning a list of supported nids */
498 *nids = rsaref_digest_nids;
499 return (sizeof(rsaref_digest_nids) -
500 1) / sizeof(rsaref_digest_nids[0]);
502 /* We are being asked for a specific digest */
505 *digest = &digest_md2;
508 *digest = &digest_md5;
518 /*****************************************************************************
522 #define data(ctx) ((DES_CBC_CTX *)(ctx)->cipher_data)
523 static int cipher_des_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
524 const unsigned char *iv, int enc)
526 DES_CBCInit(data(ctx), (unsigned char *)key, (unsigned char *)iv, enc);
530 static int cipher_des_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
531 const unsigned char *in, unsigned int inl)
533 int ret = DES_CBCUpdate(data(ctx), out, (unsigned char *)in, inl);
536 RSAREFerr(RSAREF_F_CIPHER_DES_CBC_CODE,
537 RSAREF_R_LENGTH_NOT_BLOCK_ALIGNED);
542 RSAREFerr(RSAREF_F_CIPHER_DES_CBC_CODE, RSAREF_R_UNKNOWN_FAULT);
547 static int cipher_des_cbc_clean(EVP_CIPHER_CTX *ctx)
549 memset(data(ctx), 0, ctx->cipher->ctx_size);
554 #define data(ctx) ((DES3_CBC_CTX *)(ctx)->cipher_data)
555 static int cipher_des_ede3_cbc_init(EVP_CIPHER_CTX *ctx,
556 const unsigned char *key,
557 const unsigned char *iv, int enc)
559 DES3_CBCInit(data(ctx), (unsigned char *)key, (unsigned char *)iv, enc);
563 static int cipher_des_ede3_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
564 const unsigned char *in, unsigned int inl)
566 int ret = DES3_CBCUpdate(data(ctx), out, (unsigned char *)in, inl);
569 RSAREFerr(RSAREF_F_CIPHER_DES_CBC_CODE,
570 RSAREF_R_LENGTH_NOT_BLOCK_ALIGNED);
575 RSAREFerr(RSAREF_F_CIPHER_DES_CBC_CODE, RSAREF_R_UNKNOWN_FAULT);
580 static int cipher_des_ede3_cbc_clean(EVP_CIPHER_CTX *ctx)
582 memset(data(ctx), 0, ctx->cipher->ctx_size);
587 #define data(ctx) ((DESX_CBC_CTX *)(ctx)->cipher_data)
588 static int cipher_desx_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
589 const unsigned char *iv, int enc)
591 DESX_CBCInit(data(ctx), (unsigned char *)key, (unsigned char *)iv, enc);
595 static int cipher_desx_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
596 const unsigned char *in, unsigned int inl)
598 int ret = DESX_CBCUpdate(data(ctx), out, (unsigned char *)in, inl);
601 RSAREFerr(RSAREF_F_CIPHER_DES_CBC_CODE,
602 RSAREF_R_LENGTH_NOT_BLOCK_ALIGNED);
607 RSAREFerr(RSAREF_F_CIPHER_DES_CBC_CODE, RSAREF_R_UNKNOWN_FAULT);
612 static int cipher_desx_cbc_clean(EVP_CIPHER_CTX *ctx)
614 memset(data(ctx), 0, ctx->cipher->ctx_size);
618 /*****************************************************************************
622 #define data(ctx) ((MD2_CTX *)(ctx)->md_data)
623 static int digest_md2_init(EVP_MD_CTX *ctx)
629 static int digest_md2_update(EVP_MD_CTX *ctx, const void *data,
632 MD2Update(data(ctx), (unsigned char *)data, (unsigned int)count);
636 static int digest_md2_final(EVP_MD_CTX *ctx, unsigned char *md)
638 MD2Final(md, data(ctx));
643 #define data(ctx) ((MD5_CTX *)(ctx)->md_data)
644 static int digest_md5_init(EVP_MD_CTX *ctx)
650 static int digest_md5_update(EVP_MD_CTX *ctx, const void *data,
653 MD5Update(data(ctx), (unsigned char *)data, (unsigned int)count);
657 static int digest_md5_final(EVP_MD_CTX *ctx, unsigned char *md)
659 MD5Final(md, data(ctx));