e_ossltest.c 19 KB

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