rsaref.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. /*
  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.
  5. */
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include "./source/global.h"
  9. #include "./source/rsaref.h"
  10. #include "./source/rsa.h"
  11. #include "./source/des.h"
  12. #include <openssl/err.h>
  13. #define OPENSSL_NO_MD2
  14. #define OPENSSL_NO_MD5
  15. #include <openssl/evp.h>
  16. #include <openssl/bn.h>
  17. #include <openssl/engine.h>
  18. #define RSAREF_LIB_NAME "rsaref engine"
  19. #include "rsaref_err.c"
  20. /*****************************************************************************
  21. *** Function declarations and global variable definitions ***
  22. *****************************************************************************/
  23. /*****************************************************************************
  24. * Constants used when creating the ENGINE
  25. **/
  26. static const char *engine_rsaref_id = "rsaref";
  27. static const char *engine_rsaref_name = "RSAref engine support";
  28. /*****************************************************************************
  29. * Functions to handle the engine
  30. **/
  31. static int rsaref_destroy(ENGINE *e);
  32. static int rsaref_init(ENGINE *e);
  33. static int rsaref_finish(ENGINE *e);
  34. #if 0
  35. static int rsaref_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) ());
  36. #endif
  37. /*****************************************************************************
  38. * Engine commands
  39. **/
  40. static const ENGINE_CMD_DEFN rsaref_cmd_defns[] = {
  41. {0, NULL, NULL, 0}
  42. };
  43. /*****************************************************************************
  44. * RSA functions
  45. **/
  46. static int rsaref_private_decrypt(int len, const unsigned char *from,
  47. unsigned char *to, RSA *rsa, int padding);
  48. static int rsaref_private_encrypt(int len, const unsigned char *from,
  49. unsigned char *to, RSA *rsa, int padding);
  50. static int rsaref_public_encrypt(int len, const unsigned char *from,
  51. unsigned char *to, RSA *rsa, int padding);
  52. static int rsaref_public_decrypt(int len, const unsigned char *from,
  53. unsigned char *to, RSA *rsa, int padding);
  54. static int bnref_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  55. const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
  56. static int rsaref_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa);
  57. /*****************************************************************************
  58. * Our RSA method
  59. **/
  60. static RSA_METHOD rsaref_rsa = {
  61. "RSAref PKCS#1 RSA",
  62. rsaref_public_encrypt,
  63. rsaref_public_decrypt,
  64. rsaref_private_encrypt,
  65. rsaref_private_decrypt,
  66. rsaref_mod_exp,
  67. bnref_mod_exp,
  68. NULL,
  69. NULL,
  70. 0,
  71. NULL,
  72. NULL,
  73. NULL
  74. };
  75. /*****************************************************************************
  76. * Symetric cipher and digest function registrars
  77. **/
  78. static int rsaref_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
  79. const int **nids, int nid);
  80. static int rsaref_digests(ENGINE *e, const EVP_MD **digest,
  81. const int **nids, int nid);
  82. static int rsaref_cipher_nids[] =
  83. { NID_des_cbc, NID_des_ede3_cbc, NID_desx_cbc, 0 };
  84. static int rsaref_digest_nids[] = { NID_md2, NID_md5, 0 };
  85. /*****************************************************************************
  86. * DES functions
  87. **/
  88. static int cipher_des_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  89. const unsigned char *iv, int enc);
  90. static int cipher_des_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
  91. const unsigned char *in, unsigned int inl);
  92. static int cipher_des_cbc_clean(EVP_CIPHER_CTX *);
  93. static int cipher_des_ede3_cbc_init(EVP_CIPHER_CTX *ctx,
  94. const unsigned char *key,
  95. const unsigned char *iv, int enc);
  96. static int cipher_des_ede3_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
  97. const unsigned char *in,
  98. unsigned int inl);
  99. static int cipher_des_ede3_cbc_clean(EVP_CIPHER_CTX *);
  100. static int cipher_desx_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  101. const unsigned char *iv, int enc);
  102. static int cipher_desx_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
  103. const unsigned char *in, unsigned int inl);
  104. static int cipher_desx_cbc_clean(EVP_CIPHER_CTX *);
  105. /*****************************************************************************
  106. * Our DES ciphers
  107. **/
  108. static const EVP_CIPHER cipher_des_cbc = {
  109. NID_des_cbc,
  110. 8, 8, 8,
  111. 0 | EVP_CIPH_CBC_MODE,
  112. cipher_des_cbc_init,
  113. cipher_des_cbc_code,
  114. cipher_des_cbc_clean,
  115. sizeof(DES_CBC_CTX),
  116. NULL,
  117. NULL,
  118. NULL,
  119. NULL
  120. };
  121. static const EVP_CIPHER cipher_des_ede3_cbc = {
  122. NID_des_ede3_cbc,
  123. 8, 24, 8,
  124. 0 | EVP_CIPH_CBC_MODE,
  125. cipher_des_ede3_cbc_init,
  126. cipher_des_ede3_cbc_code,
  127. cipher_des_ede3_cbc_clean,
  128. sizeof(DES3_CBC_CTX),
  129. NULL,
  130. NULL,
  131. NULL,
  132. NULL
  133. };
  134. static const EVP_CIPHER cipher_desx_cbc = {
  135. NID_desx_cbc,
  136. 8, 24, 8,
  137. 0 | EVP_CIPH_CBC_MODE,
  138. cipher_desx_cbc_init,
  139. cipher_desx_cbc_code,
  140. cipher_desx_cbc_clean,
  141. sizeof(DESX_CBC_CTX),
  142. NULL,
  143. NULL,
  144. NULL,
  145. NULL
  146. };
  147. /*****************************************************************************
  148. * MD functions
  149. **/
  150. static int digest_md2_init(EVP_MD_CTX *ctx);
  151. static int digest_md2_update(EVP_MD_CTX *ctx, const void *data,
  152. unsigned long count);
  153. static int digest_md2_final(EVP_MD_CTX *ctx, unsigned char *md);
  154. static int digest_md5_init(EVP_MD_CTX *ctx);
  155. static int digest_md5_update(EVP_MD_CTX *ctx, const void *data,
  156. unsigned long count);
  157. static int digest_md5_final(EVP_MD_CTX *ctx, unsigned char *md);
  158. /*****************************************************************************
  159. * Our MD digests
  160. **/
  161. static const EVP_MD digest_md2 = {
  162. NID_md2,
  163. NID_md2WithRSAEncryption,
  164. 16,
  165. 0,
  166. digest_md2_init,
  167. digest_md2_update,
  168. digest_md2_final,
  169. NULL,
  170. NULL,
  171. EVP_PKEY_RSA_method,
  172. 16,
  173. sizeof(MD2_CTX)
  174. };
  175. static const EVP_MD digest_md5 = {
  176. NID_md5,
  177. NID_md5WithRSAEncryption,
  178. 16,
  179. 0,
  180. digest_md5_init,
  181. digest_md5_update,
  182. digest_md5_final,
  183. NULL,
  184. NULL,
  185. EVP_PKEY_RSA_method,
  186. 64,
  187. sizeof(MD5_CTX)
  188. };
  189. /*****************************************************************************
  190. *** Function definitions ***
  191. *****************************************************************************/
  192. /*****************************************************************************
  193. * Functions to handle the engine
  194. **/
  195. static int bind_rsaref(ENGINE *e)
  196. {
  197. const RSA_METHOD *meth1;
  198. if (!ENGINE_set_id(e, engine_rsaref_id)
  199. || !ENGINE_set_name(e, engine_rsaref_name)
  200. || !ENGINE_set_RSA(e, &rsaref_rsa)
  201. || !ENGINE_set_ciphers(e, rsaref_ciphers)
  202. || !ENGINE_set_digests(e, rsaref_digests)
  203. || !ENGINE_set_destroy_function(e, rsaref_destroy)
  204. || !ENGINE_set_init_function(e, rsaref_init)
  205. || !ENGINE_set_finish_function(e, rsaref_finish)
  206. /* || !ENGINE_set_ctrl_function(e, rsaref_ctrl) */
  207. /*
  208. * || !ENGINE_set_cmd_defns(e, rsaref_cmd_defns)
  209. */ )
  210. return 0;
  211. /* Ensure the rsaref error handling is set up */
  212. ERR_load_RSAREF_strings();
  213. return 1;
  214. }
  215. #ifdef ENGINE_DYNAMIC_SUPPORT
  216. static int bind_helper(ENGINE *e, const char *id)
  217. {
  218. if (id && (strcmp(id, engine_rsaref_id) != 0))
  219. return 0;
  220. if (!bind_rsaref(e))
  221. return 0;
  222. return 1;
  223. }
  224. IMPLEMENT_DYNAMIC_CHECK_FN()
  225. IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
  226. #else
  227. static ENGINE *engine_rsaref(void)
  228. {
  229. ENGINE *ret = ENGINE_new();
  230. if (!ret)
  231. return NULL;
  232. if (!bind_rsaref(ret)) {
  233. ENGINE_free(ret);
  234. return NULL;
  235. }
  236. return ret;
  237. }
  238. void ENGINE_load_rsaref(void)
  239. {
  240. /* Copied from eng_[openssl|dyn].c */
  241. ENGINE *toadd = engine_rsaref();
  242. if (!toadd)
  243. return;
  244. ENGINE_add(toadd);
  245. ENGINE_free(toadd);
  246. ERR_clear_error();
  247. }
  248. #endif
  249. /* Initiator which is only present to make sure this engine looks available */
  250. static int rsaref_init(ENGINE *e)
  251. {
  252. return 1;
  253. }
  254. /* Finisher which is only present to make sure this engine looks available */
  255. static int rsaref_finish(ENGINE *e)
  256. {
  257. return 1;
  258. }
  259. /* Destructor (complements the "ENGINE_ncipher()" constructor) */
  260. static int rsaref_destroy(ENGINE *e)
  261. {
  262. ERR_unload_RSAREF_strings();
  263. return 1;
  264. }
  265. /*****************************************************************************
  266. * RSA functions
  267. **/
  268. static int rsaref_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
  269. {
  270. RSAREFerr(RSAREF_F_RSAREF_MOD_EXP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  271. return (0);
  272. }
  273. static int bnref_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  274. const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
  275. {
  276. RSAREFerr(RSAREF_F_BNREF_MOD_EXP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  277. return (0);
  278. }
  279. /* unsigned char *to: [max] */
  280. static int RSAref_bn2bin(BIGNUM *from, unsigned char *to, int max)
  281. {
  282. int i;
  283. i = BN_num_bytes(from);
  284. if (i > max) {
  285. RSAREFerr(RSAREF_F_RSAREF_BN2BIN, RSAREF_R_LEN);
  286. return (0);
  287. }
  288. memset(to, 0, (unsigned int)max);
  289. if (!BN_bn2bin(from, &(to[max - i])))
  290. return (0);
  291. return (1);
  292. }
  293. #ifdef undef
  294. /* unsigned char *from: [max] */
  295. static BIGNUM *RSAref_bin2bn(unsigned char *from, BIGNUM *to, int max)
  296. {
  297. int i;
  298. BIGNUM *ret;
  299. for (i = 0; i < max; i++)
  300. if (from[i])
  301. break;
  302. ret = BN_bin2bn(&(from[i]), max - i, to);
  303. return (ret);
  304. }
  305. static int RSAref_Public_ref2eay(RSArefPublicKey * from, RSA *to)
  306. {
  307. to->n = RSAref_bin2bn(from->m, NULL, RSAref_MAX_LEN);
  308. to->e = RSAref_bin2bn(from->e, NULL, RSAref_MAX_LEN);
  309. if ((to->n == NULL) || (to->e == NULL))
  310. return (0);
  311. return (1);
  312. }
  313. #endif
  314. static int RSAref_Public_eay2ref(RSA *from, R_RSA_PUBLIC_KEY * to)
  315. {
  316. to->bits = BN_num_bits(from->n);
  317. if (!RSAref_bn2bin(from->n, to->modulus, MAX_RSA_MODULUS_LEN))
  318. return (0);
  319. if (!RSAref_bn2bin(from->e, to->exponent, MAX_RSA_MODULUS_LEN))
  320. return (0);
  321. return (1);
  322. }
  323. #ifdef undef
  324. static int RSAref_Private_ref2eay(RSArefPrivateKey * from, RSA *to)
  325. {
  326. if ((to->n = RSAref_bin2bn(from->m, NULL, RSAref_MAX_LEN)) == NULL)
  327. return (0);
  328. if ((to->e = RSAref_bin2bn(from->e, NULL, RSAref_MAX_LEN)) == NULL)
  329. return (0);
  330. if ((to->d = RSAref_bin2bn(from->d, NULL, RSAref_MAX_LEN)) == NULL)
  331. return (0);
  332. if ((to->p =
  333. RSAref_bin2bn(from->prime[0], NULL, RSAref_MAX_PLEN)) == NULL)
  334. return (0);
  335. if ((to->q =
  336. RSAref_bin2bn(from->prime[1], NULL, RSAref_MAX_PLEN)) == NULL)
  337. return (0);
  338. if ((to->dmp1 = RSAref_bin2bn(from->pexp[0], NULL, RSAref_MAX_PLEN))
  339. == NULL)
  340. return (0);
  341. if ((to->dmq1 = RSAref_bin2bn(from->pexp[1], NULL, RSAref_MAX_PLEN))
  342. == NULL)
  343. return (0);
  344. if ((to->iqmp = RSAref_bin2bn(from->coef, NULL, RSAref_MAX_PLEN)) == NULL)
  345. return (0);
  346. return (1);
  347. }
  348. #endif
  349. static int RSAref_Private_eay2ref(RSA *from, R_RSA_PRIVATE_KEY * to)
  350. {
  351. to->bits = BN_num_bits(from->n);
  352. if (!RSAref_bn2bin(from->n, to->modulus, MAX_RSA_MODULUS_LEN))
  353. return (0);
  354. if (!RSAref_bn2bin(from->e, to->publicExponent, MAX_RSA_MODULUS_LEN))
  355. return (0);
  356. if (!RSAref_bn2bin(from->d, to->exponent, MAX_RSA_MODULUS_LEN))
  357. return (0);
  358. if (!RSAref_bn2bin(from->p, to->prime[0], MAX_RSA_PRIME_LEN))
  359. return (0);
  360. if (!RSAref_bn2bin(from->q, to->prime[1], MAX_RSA_PRIME_LEN))
  361. return (0);
  362. if (!RSAref_bn2bin(from->dmp1, to->primeExponent[0], MAX_RSA_PRIME_LEN))
  363. return (0);
  364. if (!RSAref_bn2bin(from->dmq1, to->primeExponent[1], MAX_RSA_PRIME_LEN))
  365. return (0);
  366. if (!RSAref_bn2bin(from->iqmp, to->coefficient, MAX_RSA_PRIME_LEN))
  367. return (0);
  368. return (1);
  369. }
  370. static int rsaref_private_decrypt(int len, const unsigned char *from,
  371. unsigned char *to, RSA *rsa, int padding)
  372. {
  373. int i, outlen = -1;
  374. R_RSA_PRIVATE_KEY RSAkey;
  375. if (!RSAref_Private_eay2ref(rsa, &RSAkey))
  376. goto err;
  377. if ((i =
  378. RSAPrivateDecrypt(to, (unsigned int *)&outlen, (unsigned char *)from,
  379. len, &RSAkey)) != 0) {
  380. RSAREFerr(RSAREF_F_RSAREF_PRIVATE_DECRYPT, i);
  381. outlen = -1;
  382. }
  383. err:
  384. memset(&RSAkey, 0, sizeof(RSAkey));
  385. return (outlen);
  386. }
  387. static int rsaref_private_encrypt(int len, const unsigned char *from,
  388. unsigned char *to, RSA *rsa, int padding)
  389. {
  390. int i, outlen = -1;
  391. R_RSA_PRIVATE_KEY RSAkey;
  392. if (padding != RSA_PKCS1_PADDING) {
  393. RSAREFerr(RSAREF_F_RSAREF_PRIVATE_ENCRYPT,
  394. RSA_R_UNKNOWN_PADDING_TYPE);
  395. goto err;
  396. }
  397. if (!RSAref_Private_eay2ref(rsa, &RSAkey))
  398. goto err;
  399. if ((i =
  400. RSAPrivateEncrypt(to, (unsigned int *)&outlen, (unsigned char *)from,
  401. len, &RSAkey)) != 0) {
  402. RSAREFerr(RSAREF_F_RSAREF_PRIVATE_ENCRYPT, i);
  403. outlen = -1;
  404. }
  405. err:
  406. memset(&RSAkey, 0, sizeof(RSAkey));
  407. return (outlen);
  408. }
  409. static int rsaref_public_decrypt(int len, const unsigned char *from,
  410. unsigned char *to, RSA *rsa, int padding)
  411. {
  412. int i, outlen = -1;
  413. R_RSA_PUBLIC_KEY RSAkey;
  414. if (!RSAref_Public_eay2ref(rsa, &RSAkey))
  415. goto err;
  416. if ((i =
  417. RSAPublicDecrypt(to, (unsigned int *)&outlen, (unsigned char *)from,
  418. len, &RSAkey)) != 0) {
  419. RSAREFerr(RSAREF_F_RSAREF_PUBLIC_DECRYPT, i);
  420. outlen = -1;
  421. }
  422. err:
  423. memset(&RSAkey, 0, sizeof(RSAkey));
  424. return (outlen);
  425. }
  426. static int rsaref_public_encrypt(int len, const unsigned char *from,
  427. unsigned char *to, RSA *rsa, int padding)
  428. {
  429. int outlen = -1;
  430. int i;
  431. R_RSA_PUBLIC_KEY RSAkey;
  432. R_RANDOM_STRUCT rnd;
  433. unsigned char buf[16];
  434. if (padding != RSA_PKCS1_PADDING && padding != RSA_SSLV23_PADDING) {
  435. RSAREFerr(RSAREF_F_RSAREF_PUBLIC_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
  436. goto err;
  437. }
  438. R_RandomInit(&rnd);
  439. R_GetRandomBytesNeeded((unsigned int *)&i, &rnd);
  440. while (i > 0) {
  441. if (RAND_bytes(buf, 16) <= 0)
  442. goto err;
  443. R_RandomUpdate(&rnd, buf, (unsigned int)((i > 16) ? 16 : i));
  444. i -= 16;
  445. }
  446. if (!RSAref_Public_eay2ref(rsa, &RSAkey))
  447. goto err;
  448. if ((i =
  449. RSAPublicEncrypt(to, (unsigned int *)&outlen, (unsigned char *)from,
  450. len, &RSAkey, &rnd)) != 0) {
  451. RSAREFerr(RSAREF_F_RSAREF_PUBLIC_ENCRYPT, i);
  452. outlen = -1;
  453. goto err;
  454. }
  455. err:
  456. memset(&RSAkey, 0, sizeof(RSAkey));
  457. R_RandomFinal(&rnd);
  458. memset(&rnd, 0, sizeof(rnd));
  459. return (outlen);
  460. }
  461. /*****************************************************************************
  462. * Symetric cipher and digest function registrars
  463. **/
  464. static int rsaref_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
  465. const int **nids, int nid)
  466. {
  467. int ok = 1;
  468. if (!cipher) {
  469. /* We are returning a list of supported nids */
  470. *nids = rsaref_cipher_nids;
  471. return (sizeof(rsaref_cipher_nids) -
  472. 1) / sizeof(rsaref_cipher_nids[0]);
  473. }
  474. /* We are being asked for a specific cipher */
  475. switch (nid) {
  476. case NID_des_cbc:
  477. *cipher = &cipher_des_cbc;
  478. break;
  479. case NID_des_ede3_cbc:
  480. *cipher = &cipher_des_ede3_cbc;
  481. break;
  482. case NID_desx_cbc:
  483. *cipher = &cipher_desx_cbc;
  484. break;
  485. default:
  486. ok = 0;
  487. *cipher = NULL;
  488. break;
  489. }
  490. return ok;
  491. }
  492. static int rsaref_digests(ENGINE *e, const EVP_MD **digest,
  493. const int **nids, int nid)
  494. {
  495. int ok = 1;
  496. if (!digest) {
  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]);
  501. }
  502. /* We are being asked for a specific digest */
  503. switch (nid) {
  504. case NID_md2:
  505. *digest = &digest_md2;
  506. break;
  507. case NID_md5:
  508. *digest = &digest_md5;
  509. break;
  510. default:
  511. ok = 0;
  512. *digest = NULL;
  513. break;
  514. }
  515. return ok;
  516. }
  517. /*****************************************************************************
  518. * DES functions
  519. **/
  520. #undef data
  521. #define data(ctx) ((DES_CBC_CTX *)(ctx)->cipher_data)
  522. static int cipher_des_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  523. const unsigned char *iv, int enc)
  524. {
  525. DES_CBCInit(data(ctx), (unsigned char *)key, (unsigned char *)iv, enc);
  526. return 1;
  527. }
  528. static int cipher_des_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
  529. const unsigned char *in, unsigned int inl)
  530. {
  531. int ret = DES_CBCUpdate(data(ctx), out, (unsigned char *)in, inl);
  532. switch (ret) {
  533. case RE_LEN:
  534. RSAREFerr(RSAREF_F_CIPHER_DES_CBC_CODE,
  535. RSAREF_R_LENGTH_NOT_BLOCK_ALIGNED);
  536. break;
  537. case 0:
  538. break;
  539. default:
  540. RSAREFerr(RSAREF_F_CIPHER_DES_CBC_CODE, RSAREF_R_UNKNOWN_FAULT);
  541. }
  542. return !ret;
  543. }
  544. static int cipher_des_cbc_clean(EVP_CIPHER_CTX *ctx)
  545. {
  546. memset(data(ctx), 0, ctx->cipher->ctx_size);
  547. return 1;
  548. }
  549. #undef data
  550. #define data(ctx) ((DES3_CBC_CTX *)(ctx)->cipher_data)
  551. static int cipher_des_ede3_cbc_init(EVP_CIPHER_CTX *ctx,
  552. const unsigned char *key,
  553. const unsigned char *iv, int enc)
  554. {
  555. DES3_CBCInit(data(ctx), (unsigned char *)key, (unsigned char *)iv, enc);
  556. return 1;
  557. }
  558. static int cipher_des_ede3_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
  559. const unsigned char *in, unsigned int inl)
  560. {
  561. int ret = DES3_CBCUpdate(data(ctx), out, (unsigned char *)in, inl);
  562. switch (ret) {
  563. case RE_LEN:
  564. RSAREFerr(RSAREF_F_CIPHER_DES_CBC_CODE,
  565. RSAREF_R_LENGTH_NOT_BLOCK_ALIGNED);
  566. break;
  567. case 0:
  568. break;
  569. default:
  570. RSAREFerr(RSAREF_F_CIPHER_DES_CBC_CODE, RSAREF_R_UNKNOWN_FAULT);
  571. }
  572. return !ret;
  573. }
  574. static int cipher_des_ede3_cbc_clean(EVP_CIPHER_CTX *ctx)
  575. {
  576. memset(data(ctx), 0, ctx->cipher->ctx_size);
  577. return 1;
  578. }
  579. #undef data
  580. #define data(ctx) ((DESX_CBC_CTX *)(ctx)->cipher_data)
  581. static int cipher_desx_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  582. const unsigned char *iv, int enc)
  583. {
  584. DESX_CBCInit(data(ctx), (unsigned char *)key, (unsigned char *)iv, enc);
  585. return 1;
  586. }
  587. static int cipher_desx_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
  588. const unsigned char *in, unsigned int inl)
  589. {
  590. int ret = DESX_CBCUpdate(data(ctx), out, (unsigned char *)in, inl);
  591. switch (ret) {
  592. case RE_LEN:
  593. RSAREFerr(RSAREF_F_CIPHER_DES_CBC_CODE,
  594. RSAREF_R_LENGTH_NOT_BLOCK_ALIGNED);
  595. break;
  596. case 0:
  597. break;
  598. default:
  599. RSAREFerr(RSAREF_F_CIPHER_DES_CBC_CODE, RSAREF_R_UNKNOWN_FAULT);
  600. }
  601. return !ret;
  602. }
  603. static int cipher_desx_cbc_clean(EVP_CIPHER_CTX *ctx)
  604. {
  605. memset(data(ctx), 0, ctx->cipher->ctx_size);
  606. return 1;
  607. }
  608. /*****************************************************************************
  609. * MD functions
  610. **/
  611. #undef data
  612. #define data(ctx) ((MD2_CTX *)(ctx)->md_data)
  613. static int digest_md2_init(EVP_MD_CTX *ctx)
  614. {
  615. MD2Init(data(ctx));
  616. return 1;
  617. }
  618. static int digest_md2_update(EVP_MD_CTX *ctx, const void *data,
  619. unsigned long count)
  620. {
  621. MD2Update(data(ctx), (unsigned char *)data, (unsigned int)count);
  622. return 1;
  623. }
  624. static int digest_md2_final(EVP_MD_CTX *ctx, unsigned char *md)
  625. {
  626. MD2Final(md, data(ctx));
  627. return 1;
  628. }
  629. #undef data
  630. #define data(ctx) ((MD5_CTX *)(ctx)->md_data)
  631. static int digest_md5_init(EVP_MD_CTX *ctx)
  632. {
  633. MD5Init(data(ctx));
  634. return 1;
  635. }
  636. static int digest_md5_update(EVP_MD_CTX *ctx, const void *data,
  637. unsigned long count)
  638. {
  639. MD5Update(data(ctx), (unsigned char *)data, (unsigned int)count);
  640. return 1;
  641. }
  642. static int digest_md5_final(EVP_MD_CTX *ctx, unsigned char *md)
  643. {
  644. MD5Final(md, data(ctx));
  645. return 1;
  646. }