gost_eng.c 7.6 KB

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