e_ossltest.c 21 KB

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