e_ossltest.c 22 KB

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