RSA引擎未使用struct中提到的函数

时间:2017-02-13 10:44:01

标签: c openssl openssl-engine

我不熟悉在openssl中制作引擎。基本上我想实现一个OpenSSL RSA引擎,它使用我在加密和解密时提到的功能。我的引擎编译并加载,但它似乎没有使用我希望它用于加密和解密的函数。

#include <openssl/opensslconf.h>
#include <stdio.h>
#include <string.h>
#include <openssl/crypto.h>
#include <openssl/buffer.h>
#include <openssl/engine.h>
#include <openssl/rsa.h>
#include <openssl/bn.h>
#include <openssl/err.h>

static int
eng_rsa_pub_enc (int flen, const unsigned char *from,
     unsigned char *to, RSA * rsa, int padding)
{

    printf ("Engine is encrypting using pub key \n");
    RSA_public_encrypt (flen, from, to, rsa, RSA_PKCS1_PADDING);
}

static int
eng_rsa_pub_dec (int flen, const unsigned char *from,
     unsigned char *to, RSA * rsa, int padding)
{

   printf ("Engine is decrypting using pub key \n");
   RSA_public_decrypt (flen, from, to, rsa, RSA_PKCS1_PADDING);
}

static int
eng_rsa_priv_enc (int flen, const unsigned char *from, unsigned char *to,
      RSA * rsa, int padding __attribute__ ((unused)))
{
   printf ("Engine is encrypting using priv key \n");
   RSA_private_encrypt (flen, from, to, rsa, RSA_PKCS1_PADDING);
}


static int
eng_rsa_priv_dec (int flen, const unsigned char *from, unsigned char *to,
      RSA * rsa, int padding __attribute__ ((unused)))
{
   printf ("Engine is decrypting using priv key \n");
   RSA_private_decrypt (flen, from, to, rsa, RSA_PKCS1_PADDING);
}

/* Constants used when creating the ENGINE */
static const char *engine_rsa_id = "rsa-engine 1";
static const char *engine_rsa_name = "engine for testing 1";



static RSA_METHOD struct_rsa = {
    "RSA engine for demo",
    eng_rsa_pub_enc,
    eng_rsa_pub_dec,
    eng_rsa_priv_enc,
    eng_rsa_priv_dec,
    NULL,
    NULL,
    NULL,
    NULL,
    RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE,
    NULL,
    NULL,
    NULL
};

static int bind (ENGINE * e, const char *id)
{
  printf ("%s\n", id);

  if (!ENGINE_set_id (e, engine_rsa_id) ||
      !ENGINE_set_name (e, engine_rsa_name) ||
      !ENGINE_set_RSA (e, &struct_rsa))
  return 0;

  return 1;
}

IMPLEMENT_DYNAMIC_BIND_FN (bind) 
IMPLEMENT_DYNAMIC_CHECK_FN ()

我正在使用以下命令编译代码。

gcc -fPIC -c rsa-engine.c
gcc -shared -o librsa_engine.so -lcrypto rsa-engine.o
openssl engine -t -c rsa_engine

此处引擎加载,但是当我尝试使用以下命令加密文本文件时

openssl pkeyutl -encrypt -in message.txt -pubin -inkey pubkey-B.pem -engine rsa_engine -out cipher.bin
openssl pkeyutl -decrypt -in cipher.bin -inkey privkey-B.pem -engine rsa_engine -out rec.txt

似乎没有使用我在struct_rsa中定义的功能。它也没有给出函数中printf的输出。

0 个答案:

没有答案