gost_eng.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /**********************************************************************
  2. * gost_eng.c *
  3. * Copyright (c) 2005-2006 Cryptocom LTD *
  4. * This file is distributed under the same license as OpenSSL *
  5. * *
  6. * Main file of GOST engine *
  7. * for OpenSSL *
  8. * Requires OpenSSL 0.9.9 for compilation *
  9. **********************************************************************/
  10. #include <string.h>
  11. #include <openssl/crypto.h>
  12. #include <openssl/err.h>
  13. #include <openssl/evp.h>
  14. #include <openssl/engine.h>
  15. #include <openssl/obj_mac.h>
  16. #include "e_gost_err.h"
  17. #include "gost_lcl.h"
  18. static const char *engine_gost_id = "gost";
  19. static const char *engine_gost_name = "Reference implementation of GOST engine";
  20. /* Symmetric cipher and digest function registrar */
  21. static int gost_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
  22. const int **nids, int nid);
  23. static int gost_digests(ENGINE *e, const EVP_MD **digest,
  24. const int **nids, int ind);
  25. static int gost_pkey_meths (ENGINE *e, EVP_PKEY_METHOD **pmeth,
  26. const int **nids, int nid);
  27. static int gost_pkey_asn1_meths (ENGINE *e, EVP_PKEY_ASN1_METHOD **ameth,
  28. const int **nids, int nid);
  29. static int gost_cipher_nids[] =
  30. {NID_id_Gost28147_89, NID_gost89_cnt,0};
  31. static int gost_digest_nids[] =
  32. {NID_id_GostR3411_94,NID_id_Gost28147_89_MAC, 0};
  33. static int gost_pkey_meth_nids[] =
  34. {NID_id_GostR3410_94,
  35. NID_id_GostR3410_2001, NID_id_Gost28147_89_MAC, 0};
  36. static EVP_PKEY_METHOD *pmeth_GostR3410_94 = NULL,
  37. *pmeth_GostR3410_2001 = NULL,
  38. *pmeth_Gost28147_MAC = NULL;
  39. static EVP_PKEY_ASN1_METHOD *ameth_GostR3410_94 = NULL,
  40. *ameth_GostR3410_2001 = NULL,
  41. *ameth_Gost28147_MAC = NULL;
  42. static int gost_engine_init(ENGINE *e)
  43. {
  44. return 1;
  45. }
  46. static int gost_engine_finish(ENGINE *e)
  47. {
  48. return 1;
  49. }
  50. static int gost_engine_destroy(ENGINE *e)
  51. {
  52. gost_param_free();
  53. return 1;
  54. }
  55. static int bind_gost (ENGINE *e,const char *id)
  56. {
  57. int ret = 0;
  58. if (id && strcmp(id, engine_gost_id)) return 0;
  59. if (!ENGINE_set_id(e, engine_gost_id))
  60. {
  61. printf("ENGINE_set_id failed\n");
  62. goto end;
  63. }
  64. if (!ENGINE_set_name(e, engine_gost_name))
  65. {
  66. printf("ENGINE_set_name failed\n");
  67. goto end;
  68. }
  69. if (!ENGINE_set_digests(e, gost_digests))
  70. {
  71. printf("ENGINE_set_digests failed\n");
  72. goto end;
  73. }
  74. if (! ENGINE_set_ciphers(e, gost_ciphers))
  75. {
  76. printf("ENGINE_set_ciphers failed\n");
  77. goto end;
  78. }
  79. if (! ENGINE_set_pkey_meths(e, gost_pkey_meths))
  80. {
  81. printf("ENGINE_set_pkey_meths failed\n");
  82. goto end;
  83. }
  84. if (! ENGINE_set_pkey_asn1_meths(e, gost_pkey_asn1_meths))
  85. {
  86. printf("ENGINE_set_pkey_asn1_meths failed\n");
  87. goto end;
  88. }
  89. /* Control function and commands */
  90. if (!ENGINE_set_cmd_defns(e,gost_cmds))
  91. {
  92. fprintf(stderr,"ENGINE_set_cmd_defns failed\n");
  93. goto end;
  94. }
  95. if (!ENGINE_set_ctrl_function(e,gost_control_func))
  96. {
  97. fprintf(stderr,"ENGINE_set_ctrl_func failed\n");
  98. goto end;
  99. }
  100. if ( ! ENGINE_set_destroy_function(e, gost_engine_destroy)
  101. || ! ENGINE_set_init_function(e,gost_engine_init)
  102. || ! ENGINE_set_finish_function(e,gost_engine_finish))
  103. {
  104. goto end;
  105. }
  106. if (!register_ameth_gost(NID_id_GostR3410_94, &ameth_GostR3410_94, "GOST94", "GOST R 34.10-94")) goto end;
  107. if (!register_ameth_gost(NID_id_GostR3410_2001, &ameth_GostR3410_2001, "GOST2001", "GOST R 34.10-2001")) goto end;
  108. if (!register_ameth_gost(NID_id_Gost28147_89_MAC, &ameth_Gost28147_MAC,
  109. "GOST-MAC", "GOST 28147-89 MAC")) goto end;
  110. if (!register_pmeth_gost(NID_id_GostR3410_94, &pmeth_GostR3410_94, 0)) goto end;
  111. if (!register_pmeth_gost(NID_id_GostR3410_2001, &pmeth_GostR3410_2001, 0)) goto end;
  112. if (!register_pmeth_gost(NID_id_Gost28147_89_MAC, &pmeth_Gost28147_MAC, 0))
  113. goto end;
  114. if ( ! ENGINE_register_ciphers(e)
  115. || ! ENGINE_register_digests(e)
  116. || ! ENGINE_register_pkey_meths(e)
  117. /* These two actually should go in LIST_ADD command */
  118. || ! EVP_add_cipher(&cipher_gost)
  119. || ! EVP_add_cipher(&cipher_gost_cpacnt)
  120. || ! EVP_add_digest(&digest_gost)
  121. || ! EVP_add_digest(&imit_gost_cpa)
  122. )
  123. {
  124. goto end;
  125. }
  126. ERR_load_GOST_strings();
  127. ret = 1;
  128. end:
  129. return ret;
  130. }
  131. #ifndef OPENSSL_NO_DYNAMIC_ENGINE
  132. IMPLEMENT_DYNAMIC_BIND_FN(bind_gost)
  133. IMPLEMENT_DYNAMIC_CHECK_FN()
  134. #endif /* ndef OPENSSL_NO_DYNAMIC_ENGINE */
  135. static int gost_digests(ENGINE *e, const EVP_MD **digest,
  136. const int **nids, int nid)
  137. {
  138. int ok =1 ;
  139. if (!digest)
  140. {
  141. *nids = gost_digest_nids;
  142. return 2;
  143. }
  144. /*printf("Digest no %d requested\n",nid);*/
  145. if(nid == NID_id_GostR3411_94)
  146. {
  147. *digest = &digest_gost;
  148. }
  149. else if (nid == NID_id_Gost28147_89_MAC)
  150. {
  151. *digest = &imit_gost_cpa;
  152. }
  153. else
  154. {
  155. ok =0;
  156. *digest = NULL;
  157. }
  158. return ok;
  159. }
  160. static int gost_ciphers (ENGINE *e,const EVP_CIPHER **cipher,
  161. const int **nids, int nid)
  162. {
  163. int ok = 1;
  164. if (!cipher)
  165. {
  166. *nids = gost_cipher_nids;
  167. return 2; /* two ciphers are supported */
  168. }
  169. if(nid == NID_id_Gost28147_89)
  170. {
  171. *cipher = &cipher_gost;
  172. }
  173. else if (nid == NID_gost89_cnt)
  174. {
  175. *cipher = &cipher_gost_cpacnt;
  176. }
  177. else
  178. {
  179. ok = 0;
  180. *cipher = NULL;
  181. }
  182. return ok;
  183. }
  184. static int gost_pkey_meths (ENGINE *e, EVP_PKEY_METHOD **pmeth,
  185. const int **nids, int nid)
  186. {
  187. if (!pmeth)
  188. {
  189. *nids = gost_pkey_meth_nids;
  190. return 3;
  191. }
  192. switch (nid)
  193. {
  194. case NID_id_GostR3410_94: *pmeth = pmeth_GostR3410_94; return 1;
  195. case NID_id_GostR3410_2001: *pmeth = pmeth_GostR3410_2001; return 1;
  196. case NID_id_Gost28147_89_MAC: *pmeth = pmeth_Gost28147_MAC; return 1;
  197. default:;
  198. }
  199. *pmeth = NULL;
  200. return 0;
  201. }
  202. static int gost_pkey_asn1_meths (ENGINE *e, EVP_PKEY_ASN1_METHOD **ameth,
  203. const int **nids, int nid)
  204. {
  205. if (!ameth)
  206. {
  207. *nids = gost_pkey_meth_nids;
  208. return 3;
  209. }
  210. switch (nid)
  211. {
  212. case NID_id_GostR3410_94: *ameth = ameth_GostR3410_94; return 1;
  213. case NID_id_GostR3410_2001: *ameth = ameth_GostR3410_2001; return 1;
  214. case NID_id_Gost28147_89_MAC: *ameth = ameth_Gost28147_MAC; return 1;
  215. default:;
  216. }
  217. *ameth = NULL;
  218. return 0;
  219. }
  220. #ifdef OPENSSL_NO_DYNAMIC_ENGINE
  221. static ENGINE *engine_gost(void)
  222. {
  223. ENGINE *ret = ENGINE_new();
  224. if (!ret)
  225. return NULL;
  226. if (!bind_gost(ret,engine_gost_id))
  227. {
  228. ENGINE_free(ret);
  229. return NULL;
  230. }
  231. return ret;
  232. }
  233. void ENGINE_load_gost(void)
  234. {
  235. ENGINE *toadd =engine_gost();
  236. if (!toadd) return;
  237. ENGINE_add(toadd);
  238. ENGINE_free(toadd);
  239. ERR_clear_error();
  240. }
  241. #endif