e_ossltest.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. /*
  2. * Written by Matt Caswell (matt@openssl.org) for the OpenSSL project.
  3. */
  4. /* ====================================================================
  5. * Copyright (c) 2015 The OpenSSL Project. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. All advertising materials mentioning features or use of this
  20. * software must display the following acknowledgment:
  21. * "This product includes software developed by the OpenSSL Project
  22. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  23. *
  24. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  25. * endorse or promote products derived from this software without
  26. * prior written permission. For written permission, please contact
  27. * licensing@OpenSSL.org.
  28. *
  29. * 5. Products derived from this software may not be called "OpenSSL"
  30. * nor may "OpenSSL" appear in their names without prior written
  31. * permission of the OpenSSL Project.
  32. *
  33. * 6. Redistributions of any form whatsoever must retain the following
  34. * acknowledgment:
  35. * "This product includes software developed by the OpenSSL Project
  36. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  37. *
  38. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  39. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  40. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  41. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  42. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  43. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  44. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  45. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  46. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  47. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  48. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  49. * OF THE POSSIBILITY OF SUCH DAMAGE.
  50. * ====================================================================
  51. */
  52. /*
  53. * This is the OSSLTEST engine. It provides deliberately crippled digest
  54. * implementations for test purposes. It is highly insecure and must NOT be
  55. * used for any purpose except testing
  56. */
  57. #include <stdio.h>
  58. #include <string.h>
  59. #include <openssl/engine.h>
  60. #include <openssl/sha.h>
  61. #include <openssl/md5.h>
  62. #include <openssl/rsa.h>
  63. #include <openssl/evp.h>
  64. #include <openssl/modes.h>
  65. #include <openssl/aes.h>
  66. #include <openssl/crypto.h>
  67. #define OSSLTEST_LIB_NAME "OSSLTEST"
  68. #include "e_ossltest_err.c"
  69. /* Engine Id and Name */
  70. static const char *engine_ossltest_id = "ossltest";
  71. static const char *engine_ossltest_name = "OpenSSL Test engine support";
  72. /* Engine Lifetime functions */
  73. static int ossltest_destroy(ENGINE *e);
  74. static int ossltest_init(ENGINE *e);
  75. static int ossltest_finish(ENGINE *e);
  76. void ENGINE_load_ossltest(void);
  77. /* Set up digests */
  78. static int ossltest_digests(ENGINE *e, const EVP_MD **digest,
  79. const int **nids, int nid);
  80. /* MD5 */
  81. static int digest_md5_init(EVP_MD_CTX *ctx);
  82. static int digest_md5_update(EVP_MD_CTX *ctx, const void *data,
  83. size_t count);
  84. static int digest_md5_final(EVP_MD_CTX *ctx, unsigned char *md);
  85. static EVP_MD *_hidden_md5_md = NULL;
  86. static const EVP_MD *digest_md5(void)
  87. {
  88. if (_hidden_md5_md == NULL) {
  89. EVP_MD *md;
  90. if ((md = EVP_MD_meth_new(NID_md5, NID_md5WithRSAEncryption)) == NULL
  91. || !EVP_MD_meth_set_result_size(md, MD5_DIGEST_LENGTH)
  92. || !EVP_MD_meth_set_input_blocksize(md, MD5_CBLOCK)
  93. || !EVP_MD_meth_set_app_datasize(md,
  94. sizeof(EVP_MD *) + sizeof(MD5_CTX))
  95. || !EVP_MD_meth_set_flags(md, 0)
  96. || !EVP_MD_meth_set_init(md, digest_md5_init)
  97. || !EVP_MD_meth_set_update(md, digest_md5_update)
  98. || !EVP_MD_meth_set_final(md, digest_md5_final)) {
  99. EVP_MD_meth_free(md);
  100. md = NULL;
  101. }
  102. _hidden_md5_md = md;
  103. }
  104. return _hidden_md5_md;
  105. }
  106. /* SHA1 */
  107. static int digest_sha1_init(EVP_MD_CTX *ctx);
  108. static int digest_sha1_update(EVP_MD_CTX *ctx, const void *data,
  109. size_t count);
  110. static int digest_sha1_final(EVP_MD_CTX *ctx, unsigned char *md);
  111. static EVP_MD *_hidden_sha1_md = NULL;
  112. static const EVP_MD *digest_sha1(void)
  113. {
  114. if (_hidden_sha1_md == NULL) {
  115. EVP_MD *md;
  116. if ((md = EVP_MD_meth_new(NID_sha1, NID_sha1WithRSAEncryption)) == NULL
  117. || !EVP_MD_meth_set_result_size(md, SHA_DIGEST_LENGTH)
  118. || !EVP_MD_meth_set_input_blocksize(md, SHA_CBLOCK)
  119. || !EVP_MD_meth_set_app_datasize(md,
  120. sizeof(EVP_MD *) + sizeof(SHA_CTX))
  121. || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT)
  122. || !EVP_MD_meth_set_init(md, digest_sha1_init)
  123. || !EVP_MD_meth_set_update(md, digest_sha1_update)
  124. || !EVP_MD_meth_set_final(md, digest_sha1_final)) {
  125. EVP_MD_meth_free(md);
  126. md = NULL;
  127. }
  128. _hidden_sha1_md = md;
  129. }
  130. return _hidden_sha1_md;
  131. }
  132. /* SHA256 */
  133. static int digest_sha256_init(EVP_MD_CTX *ctx);
  134. static int digest_sha256_update(EVP_MD_CTX *ctx, const void *data,
  135. size_t count);
  136. static int digest_sha256_final(EVP_MD_CTX *ctx, unsigned char *md);
  137. static EVP_MD *_hidden_sha256_md = NULL;
  138. static const EVP_MD *digest_sha256(void)
  139. {
  140. if (_hidden_sha256_md == NULL) {
  141. EVP_MD *md;
  142. if ((md = EVP_MD_meth_new(NID_sha256, NID_sha256WithRSAEncryption)) == NULL
  143. || !EVP_MD_meth_set_result_size(md, SHA256_DIGEST_LENGTH)
  144. || !EVP_MD_meth_set_input_blocksize(md, SHA256_CBLOCK)
  145. || !EVP_MD_meth_set_app_datasize(md,
  146. sizeof(EVP_MD *) + sizeof(SHA256_CTX))
  147. || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT)
  148. || !EVP_MD_meth_set_init(md, digest_sha256_init)
  149. || !EVP_MD_meth_set_update(md, digest_sha256_update)
  150. || !EVP_MD_meth_set_final(md, digest_sha256_final)) {
  151. EVP_MD_meth_free(md);
  152. md = NULL;
  153. }
  154. _hidden_sha256_md = md;
  155. }
  156. return _hidden_sha256_md;
  157. }
  158. /* SHA384/SHA512 */
  159. static int digest_sha384_init(EVP_MD_CTX *ctx);
  160. static int digest_sha512_init(EVP_MD_CTX *ctx);
  161. static int digest_sha512_update(EVP_MD_CTX *ctx, const void *data,
  162. size_t count);
  163. static int digest_sha384_final(EVP_MD_CTX *ctx, unsigned char *md);
  164. static int digest_sha512_final(EVP_MD_CTX *ctx, unsigned char *md);
  165. static EVP_MD *_hidden_sha384_md = NULL;
  166. static const EVP_MD *digest_sha384(void)
  167. {
  168. if (_hidden_sha384_md == NULL) {
  169. EVP_MD *md;
  170. if ((md = EVP_MD_meth_new(NID_sha384, NID_sha384WithRSAEncryption)) == NULL
  171. || !EVP_MD_meth_set_result_size(md, SHA384_DIGEST_LENGTH)
  172. || !EVP_MD_meth_set_input_blocksize(md, SHA512_CBLOCK)
  173. || !EVP_MD_meth_set_app_datasize(md,
  174. sizeof(EVP_MD *) + sizeof(SHA512_CTX))
  175. || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT)
  176. || !EVP_MD_meth_set_init(md, digest_sha384_init)
  177. || !EVP_MD_meth_set_update(md, digest_sha512_update)
  178. || !EVP_MD_meth_set_final(md, digest_sha384_final)) {
  179. EVP_MD_meth_free(md);
  180. md = NULL;
  181. }
  182. _hidden_sha384_md = md;
  183. }
  184. return _hidden_sha384_md;
  185. }
  186. static EVP_MD *_hidden_sha512_md = NULL;
  187. static const EVP_MD *digest_sha512(void)
  188. {
  189. if (_hidden_sha512_md == NULL) {
  190. EVP_MD *md;
  191. if ((md = EVP_MD_meth_new(NID_sha512, NID_sha512WithRSAEncryption)) == NULL
  192. || !EVP_MD_meth_set_result_size(md, SHA512_DIGEST_LENGTH)
  193. || !EVP_MD_meth_set_input_blocksize(md, SHA512_CBLOCK)
  194. || !EVP_MD_meth_set_app_datasize(md,
  195. sizeof(EVP_MD *) + sizeof(SHA512_CTX))
  196. || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT)
  197. || !EVP_MD_meth_set_init(md, digest_sha512_init)
  198. || !EVP_MD_meth_set_update(md, digest_sha512_update)
  199. || !EVP_MD_meth_set_final(md, digest_sha512_final)) {
  200. EVP_MD_meth_free(md);
  201. md = NULL;
  202. }
  203. _hidden_sha512_md = md;
  204. }
  205. return _hidden_sha512_md;
  206. }
  207. static void destroy_digests(void)
  208. {
  209. EVP_MD_meth_free(_hidden_md5_md);
  210. _hidden_md5_md = NULL;
  211. EVP_MD_meth_free(_hidden_sha1_md);
  212. _hidden_sha1_md = NULL;
  213. EVP_MD_meth_free(_hidden_sha256_md);
  214. _hidden_sha256_md = NULL;
  215. EVP_MD_meth_free(_hidden_sha384_md);
  216. _hidden_sha384_md = NULL;
  217. EVP_MD_meth_free(_hidden_sha512_md);
  218. _hidden_sha512_md = NULL;
  219. }
  220. static int ossltest_digest_nids(const int **nids)
  221. {
  222. static int digest_nids[6] = { 0, 0, 0, 0, 0, 0 };
  223. static int pos = 0;
  224. static int init = 0;
  225. if (!init) {
  226. const EVP_MD *md;
  227. if ((md = digest_md5()) != NULL)
  228. digest_nids[pos++] = EVP_MD_type(md);
  229. if ((md = digest_sha1()) != NULL)
  230. digest_nids[pos++] = EVP_MD_type(md);
  231. if ((md = digest_sha256()) != NULL)
  232. digest_nids[pos++] = EVP_MD_type(md);
  233. if ((md = digest_sha384()) != NULL)
  234. digest_nids[pos++] = EVP_MD_type(md);
  235. if ((md = digest_sha512()) != NULL)
  236. digest_nids[pos++] = EVP_MD_type(md);
  237. digest_nids[pos] = 0;
  238. init = 1;
  239. }
  240. *nids = digest_nids;
  241. return pos;
  242. }
  243. /* Setup ciphers */
  244. static int ossltest_ciphers(ENGINE *, const EVP_CIPHER **,
  245. const int **, int);
  246. static int ossltest_cipher_nids[] = {
  247. NID_aes_128_cbc, 0
  248. };
  249. /* AES128 */
  250. int ossltest_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  251. const unsigned char *iv, int enc);
  252. int ossltest_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  253. const unsigned char *in, size_t inl);
  254. static EVP_CIPHER *_hidden_aes_128_cbc = NULL;
  255. static const EVP_CIPHER *ossltest_aes_128_cbc(void)
  256. {
  257. if (_hidden_aes_128_cbc == NULL
  258. && ((_hidden_aes_128_cbc = EVP_CIPHER_meth_new(NID_aes_128_cbc,
  259. 16 /* block size */,
  260. 16 /* key len */)) == NULL
  261. || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_cbc,16)
  262. || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_cbc,
  263. EVP_CIPH_FLAG_DEFAULT_ASN1
  264. | EVP_CIPH_CBC_MODE)
  265. || !EVP_CIPHER_meth_set_init(_hidden_aes_128_cbc,
  266. ossltest_aes128_init_key)
  267. || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_cbc,
  268. ossltest_aes128_cbc_cipher)
  269. || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_cbc,
  270. EVP_CIPHER_impl_ctx_size(EVP_aes_128_cbc())))) {
  271. EVP_CIPHER_meth_free(_hidden_aes_128_cbc);
  272. _hidden_aes_128_cbc = NULL;
  273. }
  274. return _hidden_aes_128_cbc;
  275. }
  276. static void destroy_ciphers(void)
  277. {
  278. EVP_CIPHER_meth_free(_hidden_aes_128_cbc);
  279. _hidden_aes_128_cbc = NULL;
  280. }
  281. static int bind_ossltest(ENGINE *e)
  282. {
  283. /* Ensure the ossltest error handling is set up */
  284. ERR_load_OSSLTEST_strings();
  285. if (!ENGINE_set_id(e, engine_ossltest_id)
  286. || !ENGINE_set_name(e, engine_ossltest_name)
  287. || !ENGINE_set_digests(e, ossltest_digests)
  288. || !ENGINE_set_ciphers(e, ossltest_ciphers)
  289. || !ENGINE_set_destroy_function(e, ossltest_destroy)
  290. || !ENGINE_set_init_function(e, ossltest_init)
  291. || !ENGINE_set_finish_function(e, ossltest_finish)) {
  292. OSSLTESTerr(OSSLTEST_F_BIND_OSSLTEST, OSSLTEST_R_INIT_FAILED);
  293. return 0;
  294. }
  295. return 1;
  296. }
  297. #ifndef OPENSSL_NO_DYNAMIC_ENGINE
  298. static int bind_helper(ENGINE *e, const char *id)
  299. {
  300. if (id && (strcmp(id, engine_ossltest_id) != 0))
  301. return 0;
  302. if (!bind_ossltest(e))
  303. return 0;
  304. return 1;
  305. }
  306. IMPLEMENT_DYNAMIC_CHECK_FN()
  307. IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
  308. #endif
  309. static ENGINE *engine_ossltest(void)
  310. {
  311. ENGINE *ret = ENGINE_new();
  312. if (ret == NULL)
  313. return NULL;
  314. if (!bind_ossltest(ret)) {
  315. ENGINE_free(ret);
  316. return NULL;
  317. }
  318. return ret;
  319. }
  320. void ENGINE_load_ossltest(void)
  321. {
  322. /* Copied from eng_[openssl|dyn].c */
  323. ENGINE *toadd = engine_ossltest();
  324. if (!toadd)
  325. return;
  326. ENGINE_add(toadd);
  327. ENGINE_free(toadd);
  328. ERR_clear_error();
  329. }
  330. static int ossltest_init(ENGINE *e)
  331. {
  332. return 1;
  333. }
  334. static int ossltest_finish(ENGINE *e)
  335. {
  336. return 1;
  337. }
  338. static int ossltest_destroy(ENGINE *e)
  339. {
  340. destroy_digests();
  341. destroy_ciphers();
  342. ERR_unload_OSSLTEST_strings();
  343. return 1;
  344. }
  345. static int ossltest_digests(ENGINE *e, const EVP_MD **digest,
  346. const int **nids, int nid)
  347. {
  348. int ok = 1;
  349. if (!digest) {
  350. /* We are returning a list of supported nids */
  351. return ossltest_digest_nids(nids);
  352. }
  353. /* We are being asked for a specific digest */
  354. switch (nid) {
  355. case NID_md5:
  356. *digest = digest_md5();
  357. break;
  358. case NID_sha1:
  359. *digest = digest_sha1();
  360. break;
  361. case NID_sha256:
  362. *digest = digest_sha256();
  363. break;
  364. case NID_sha384:
  365. *digest = digest_sha384();
  366. break;
  367. case NID_sha512:
  368. *digest = digest_sha512();
  369. break;
  370. default:
  371. ok = 0;
  372. *digest = NULL;
  373. break;
  374. }
  375. return ok;
  376. }
  377. static int ossltest_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
  378. const int **nids, int nid)
  379. {
  380. int ok = 1;
  381. if (!cipher) {
  382. /* We are returning a list of supported nids */
  383. *nids = ossltest_cipher_nids;
  384. return (sizeof(ossltest_cipher_nids) - 1)
  385. / sizeof(ossltest_cipher_nids[0]);
  386. }
  387. /* We are being asked for a specific cipher */
  388. switch (nid) {
  389. case NID_aes_128_cbc:
  390. *cipher = ossltest_aes_128_cbc();
  391. break;
  392. default:
  393. ok = 0;
  394. *cipher = NULL;
  395. break;
  396. }
  397. return ok;
  398. }
  399. static void fill_known_data(unsigned char *md, unsigned int len)
  400. {
  401. unsigned int i;
  402. for (i=0; i<len; i++) {
  403. md[i] = (unsigned char)(i & 0xff);
  404. }
  405. }
  406. /*
  407. * MD5 implementation. We go through the motions of doing MD5 by deferring to
  408. * the standard implementation. Then we overwrite the result with a will defined
  409. * value, so that all "MD5" digests using the test engine always end up with
  410. * the same value.
  411. */
  412. #undef data
  413. #define data(ctx) ((MD5_CTX *)EVP_MD_CTX_md_data(ctx))
  414. static int digest_md5_init(EVP_MD_CTX *ctx)
  415. {
  416. return MD5_Init(data(ctx));
  417. }
  418. static int digest_md5_update(EVP_MD_CTX *ctx, const void *data,
  419. size_t count)
  420. {
  421. return MD5_Update(data(ctx), data, (size_t)count);
  422. }
  423. static int digest_md5_final(EVP_MD_CTX *ctx, unsigned char *md)
  424. {
  425. int ret;
  426. ret = MD5_Final(md, data(ctx));
  427. if (ret > 0) {
  428. fill_known_data(md, MD5_DIGEST_LENGTH);
  429. }
  430. return ret;
  431. }
  432. /*
  433. * SHA1 implementation.
  434. */
  435. #undef data
  436. #define data(ctx) ((SHA_CTX *)EVP_MD_CTX_md_data(ctx))
  437. static int digest_sha1_init(EVP_MD_CTX *ctx)
  438. {
  439. return SHA1_Init(data(ctx));
  440. }
  441. static int digest_sha1_update(EVP_MD_CTX *ctx, const void *data,
  442. size_t count)
  443. {
  444. return SHA1_Update(data(ctx), data, (size_t)count);
  445. }
  446. static int digest_sha1_final(EVP_MD_CTX *ctx, unsigned char *md)
  447. {
  448. int ret;
  449. ret = SHA1_Final(md, data(ctx));
  450. if (ret > 0) {
  451. fill_known_data(md, SHA_DIGEST_LENGTH);
  452. }
  453. return ret;
  454. }
  455. /*
  456. * SHA256 implementation.
  457. */
  458. #undef data
  459. #define data(ctx) ((SHA256_CTX *)EVP_MD_CTX_md_data(ctx))
  460. static int digest_sha256_init(EVP_MD_CTX *ctx)
  461. {
  462. return SHA256_Init(data(ctx));
  463. }
  464. static int digest_sha256_update(EVP_MD_CTX *ctx, const void *data,
  465. size_t count)
  466. {
  467. return SHA256_Update(data(ctx), data, (size_t)count);
  468. }
  469. static int digest_sha256_final(EVP_MD_CTX *ctx, unsigned char *md)
  470. {
  471. int ret;
  472. ret = SHA256_Final(md, data(ctx));
  473. if (ret > 0) {
  474. fill_known_data(md, SHA256_DIGEST_LENGTH);
  475. }
  476. return ret;
  477. }
  478. /*
  479. * SHA384/512 implementation.
  480. */
  481. #undef data
  482. #define data(ctx) ((SHA512_CTX *)EVP_MD_CTX_md_data(ctx))
  483. static int digest_sha384_init(EVP_MD_CTX *ctx)
  484. {
  485. return SHA384_Init(data(ctx));
  486. }
  487. static int digest_sha512_init(EVP_MD_CTX *ctx)
  488. {
  489. return SHA512_Init(data(ctx));
  490. }
  491. static int digest_sha512_update(EVP_MD_CTX *ctx, const void *data,
  492. size_t count)
  493. {
  494. return SHA512_Update(data(ctx), data, (size_t)count);
  495. }
  496. static int digest_sha384_final(EVP_MD_CTX *ctx, unsigned char *md)
  497. {
  498. int ret;
  499. /* Actually uses SHA512_Final! */
  500. ret = SHA512_Final(md, data(ctx));
  501. if (ret > 0) {
  502. fill_known_data(md, SHA384_DIGEST_LENGTH);
  503. }
  504. return ret;
  505. }
  506. static int digest_sha512_final(EVP_MD_CTX *ctx, unsigned char *md)
  507. {
  508. int ret;
  509. ret = SHA512_Final(md, data(ctx));
  510. if (ret > 0) {
  511. fill_known_data(md, SHA512_DIGEST_LENGTH);
  512. }
  513. return ret;
  514. }
  515. /*
  516. * AES128 Implementation
  517. */
  518. int ossltest_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  519. const unsigned char *iv, int enc)
  520. {
  521. return EVP_CIPHER_meth_get_init(EVP_aes_128_cbc()) (ctx, key, iv, enc);
  522. }
  523. int ossltest_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  524. const unsigned char *in, size_t inl)
  525. {
  526. unsigned char *tmpbuf;
  527. int ret;
  528. tmpbuf = OPENSSL_malloc(inl);
  529. if (tmpbuf == NULL)
  530. return -1;
  531. /* Remember what we were asked to encrypt */
  532. memcpy(tmpbuf, in, inl);
  533. /* Go through the motions of encrypting it */
  534. ret = EVP_CIPHER_meth_get_do_cipher(EVP_aes_128_cbc())(ctx, out, in, inl);
  535. /* Throw it all away and just use the plaintext as the output */
  536. memcpy(out, tmpbuf, inl);
  537. OPENSSL_free(tmpbuf);
  538. return ret;
  539. }