e_ossltest.c 20 KB

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