dipto181

r_engine.c

Apr 11th, 2021
266
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define _GNU_SOURCE /* See feature_test_macros(7) */
  2. #include <sched.h>
  3. #include <openssl/opensslconf.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <openssl/crypto.h>
  8. #include <openssl/buffer.h>
  9. #include <openssl/engine.h>
  10. #include <openssl/rsa.h>
  11. #include <openssl/bn.h>
  12. #include <openssl/err.h>
  13. #include <openssl/ossl_typ.h>
  14. #include <immintrin.h>
  15. #include <sys/wait.h>
  16. #include <sys/types.h>
  17. #include <sys/stat.h>
  18. #include <fcntl.h>
  19. #include <errno.h>
  20. #include <stdlib.h>
  21.  
  22.  
  23. /* Declared already in ossl_typ.h */
  24. /* typedef struct rsa_st RSA; */
  25. /* typedef struct rsa_meth_st RSA_METHOD; */
  26.  
  27.  
  28. struct rsa_meth_st {
  29.  
  30. const char *name;
  31. int (*rsa_pub_enc) (int flen, const unsigned char *from, unsigned char *to, RSA *rsa, int padding);
  32. int (*rsa_pub_dec) (int flen, const unsigned char *from, unsigned char *to, RSA *rsa, int padding);
  33. int (*rsa_priv_enc) (int flen, const unsigned char *from, unsigned char *to, RSA *rsa, int padding);
  34. int (*rsa_priv_dec) (int flen, const unsigned char *from, unsigned char *to, RSA *rsa, int padding);
  35.  
  36. int (*rsa_mod_exp) (BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx);
  37.  
  38. int (*bn_mod_exp) (BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
  39.  
  40. int (*init) (RSA *rsa);
  41.  
  42. int (*finish) (RSA *rsa);
  43.  
  44. int flags;
  45.  
  46. char *app_data;
  47.  
  48. int (*rsa_sign) (int type, const unsigned char *m, unsigned int m_length, unsigned char *sigret, unsigned int *siglen, const RSA *rsa);
  49.  
  50. int (*rsa_verify) (int dtype, const unsigned char *m, unsigned int m_length, const unsigned char *sigbuf, unsigned int siglen, const RSA *rsa);
  51.  
  52. int (*rsa_keygen) (RSA *rsa, int bits, BIGNUM *e, BN_GENCB *cb);
  53.  
  54. };
  55.  
  56. // RSA Public key operation
  57. static int eng_rsa_pub_enc (int flen, const unsigned char *from, unsigned char *to, RSA * rsa, int padding){
  58. printf ("Engine is encrypting using pub key \n");
  59. }
  60.  
  61. static int eng_rsa_pub_dec (int flen, const unsigned char *from, unsigned char *to, RSA * rsa, int padding){
  62.  
  63. printf ("Engine is decrypting using pub key \n");
  64. }
  65.  
  66. static int eng_rsa_priv_enc (int flen, const unsigned char *from, unsigned char *to, RSA * rsa, int padding __attribute__ ((unused))){
  67.  
  68. printf ("Engine is encrypting using priv key \n");
  69. }
  70.  
  71. static int eng_rsa_priv_dec (int flen, unsigned char *from, unsigned char *to, RSA * rsa, int padding __attribute__ ((unused))){
  72. printf ("Engine is decrypting using priv key \n");
  73. }
  74.  
  75. /* Constants used when creating the ENGINE */
  76. static const char *engine_rsa_id = "rsa-engine-new";
  77. static const char *engine_rsa_name = "Demo engine";
  78.  
  79. struct rsa_meth_st suse_rsa =
  80. {
  81. "demo RSA Engine",
  82. eng_rsa_pub_enc,
  83. eng_rsa_pub_dec,
  84. eng_rsa_priv_enc,
  85. eng_rsa_priv_dec,
  86. NULL,
  87. NULL,
  88. NULL,
  89. NULL,
  90. RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE,
  91. NULL,
  92. NULL,
  93. NULL,
  94. NULL
  95. };
  96.  
  97. static int bind (ENGINE * e, const char *id){
  98. printf ("%s\n", id);
  99.  
  100. if (!ENGINE_set_id (e, engine_rsa_id) ||
  101.  
  102. !ENGINE_set_name (e, engine_rsa_name) ||
  103.  
  104. !ENGINE_set_RSA (e, &suse_rsa))
  105.  
  106. return 0;
  107.  
  108. return 1;
  109.  
  110. }
  111.  
  112. IMPLEMENT_DYNAMIC_BIND_FN (bind)
  113. IMPLEMENT_DYNAMIC_CHECK_FN ()
  114.  
RAW Paste Data