0002-cryptodev-Fix-issue-with-signature-generation.patch 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. From 90fd7e8f1a316cda86ee442b43fcd7d5e5baeede Mon Sep 17 00:00:00 2001
  2. From: Gustavo Zacarias <gustavo@zacarias.com.ar>
  3. Date: Sat, 16 May 2015 18:55:08 +0200
  4. Subject: cryptodev: Fix issue with signature generation
  5. Forward port of 0001-cryptodev-Fix-issue-with-signature-generation.patch
  6. from http://rt.openssl.org/Ticket/Display.html?id=2770&user=guest&pass=guest
  7. It was originally targetted at 1.0.2-beta3.
  8. Without this patch digest acceleration via cryptodev is broken.
  9. Signed-off-by: Gustavo Zacarias <gustavo@zacarias.com.ar>
  10. Signed-off-by: Ryan Barnett <ryanbarnett3@gmail.com>
  11. ---
  12. crypto/engine/eng_cryptodev.c | 195 +++++++++++++++++++++++++++++++-----------
  13. 1 file changed, 146 insertions(+), 49 deletions(-)
  14. diff --git a/crypto/engine/eng_cryptodev.c b/crypto/engine/eng_cryptodev.c
  15. index 926d95c..7021d9a 100644
  16. --- a/crypto/engine/eng_cryptodev.c
  17. +++ b/crypto/engine/eng_cryptodev.c
  18. @@ -2,6 +2,7 @@
  19. * Copyright (c) 2002 Bob Beck <beck@openbsd.org>
  20. * Copyright (c) 2002 Theo de Raadt
  21. * Copyright (c) 2002 Markus Friedl
  22. + * Copyright (c) 2012 Nikos Mavrogiannopoulos
  23. * All rights reserved.
  24. *
  25. * Redistribution and use in source and binary forms, with or without
  26. @@ -72,7 +73,6 @@ struct dev_crypto_state {
  27. struct session_op d_sess;
  28. int d_fd;
  29. # ifdef USE_CRYPTODEV_DIGESTS
  30. - char dummy_mac_key[HASH_MAX_LEN];
  31. unsigned char digest_res[HASH_MAX_LEN];
  32. char *mac_data;
  33. int mac_len;
  34. @@ -189,8 +189,10 @@ static struct {
  35. static struct {
  36. int id;
  37. int nid;
  38. - int keylen;
  39. + int digestlen;
  40. } digests[] = {
  41. +#if 0
  42. + /* HMAC is not supported */
  43. {
  44. CRYPTO_MD5_HMAC, NID_hmacWithMD5, 16
  45. },
  46. @@ -198,15 +200,15 @@ static struct {
  47. CRYPTO_SHA1_HMAC, NID_hmacWithSHA1, 20
  48. },
  49. {
  50. - CRYPTO_RIPEMD160_HMAC, NID_ripemd160, 16
  51. - /* ? */
  52. + CRYPTO_SHA2_256_HMAC, NID_hmacWithSHA256, 32
  53. },
  54. {
  55. - CRYPTO_MD5_KPDK, NID_undef, 0
  56. + CRYPTO_SHA2_384_HMAC, NID_hmacWithSHA384, 48
  57. },
  58. {
  59. - CRYPTO_SHA1_KPDK, NID_undef, 0
  60. + CRYPTO_SHA2_512_HMAC, NID_hmacWithSHA512, 64
  61. },
  62. +#endif
  63. {
  64. CRYPTO_MD5, NID_md5, 16
  65. },
  66. @@ -214,6 +216,15 @@ static struct {
  67. CRYPTO_SHA1, NID_sha1, 20
  68. },
  69. {
  70. + CRYPTO_SHA2_256, NID_sha256, 32
  71. + },
  72. + {
  73. + CRYPTO_SHA2_384, NID_sha384, 48
  74. + },
  75. + {
  76. + CRYPTO_SHA2_512, NID_sha512, 64
  77. + },
  78. + {
  79. 0, NID_undef, 0
  80. },
  81. };
  82. @@ -288,13 +299,14 @@ static int get_cryptodev_ciphers(const int **cnids)
  83. static int nids[CRYPTO_ALGORITHM_MAX];
  84. struct session_op sess;
  85. int fd, i, count = 0;
  86. + unsigned char fake_key[CRYPTO_CIPHER_MAX_KEY_LEN];
  87. if ((fd = get_dev_crypto()) < 0) {
  88. *cnids = NULL;
  89. return (0);
  90. }
  91. memset(&sess, 0, sizeof(sess));
  92. - sess.key = (caddr_t) "123456789abcdefghijklmno";
  93. + sess.key = (void*)fake_key;
  94. for (i = 0; ciphers[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
  95. if (ciphers[i].nid == NID_undef)
  96. @@ -327,18 +339,19 @@ static int get_cryptodev_digests(const int **cnids)
  97. static int nids[CRYPTO_ALGORITHM_MAX];
  98. struct session_op sess;
  99. int fd, i, count = 0;
  100. + unsigned char fake_key[CRYPTO_CIPHER_MAX_KEY_LEN];
  101. if ((fd = get_dev_crypto()) < 0) {
  102. *cnids = NULL;
  103. return (0);
  104. }
  105. memset(&sess, 0, sizeof(sess));
  106. - sess.mackey = (caddr_t) "123456789abcdefghijklmno";
  107. + sess.mackey = fake_key;
  108. for (i = 0; digests[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
  109. if (digests[i].nid == NID_undef)
  110. continue;
  111. sess.mac = digests[i].id;
  112. - sess.mackeylen = digests[i].keylen;
  113. + sess.mackeylen = 8;
  114. sess.cipher = 0;
  115. if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
  116. ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
  117. @@ -424,14 +437,14 @@ cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  118. cryp.ses = sess->ses;
  119. cryp.flags = 0;
  120. cryp.len = inl;
  121. - cryp.src = (caddr_t) in;
  122. - cryp.dst = (caddr_t) out;
  123. + cryp.src = (void*) in;
  124. + cryp.dst = (void*) out;
  125. cryp.mac = 0;
  126. cryp.op = ctx->encrypt ? COP_ENCRYPT : COP_DECRYPT;
  127. if (ctx->cipher->iv_len) {
  128. - cryp.iv = (caddr_t) ctx->iv;
  129. + cryp.iv = (void*) ctx->iv;
  130. if (!ctx->encrypt) {
  131. iiv = in + inl - ctx->cipher->iv_len;
  132. memcpy(save_iv, iiv, ctx->cipher->iv_len);
  133. @@ -483,7 +496,7 @@ cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  134. if ((state->d_fd = get_dev_crypto()) < 0)
  135. return (0);
  136. - sess->key = (caddr_t) key;
  137. + sess->key = (void*)key;
  138. sess->keylen = ctx->key_len;
  139. sess->cipher = cipher;
  140. @@ -749,16 +762,6 @@ static int digest_nid_to_cryptodev(int nid)
  141. return (0);
  142. }
  143. -static int digest_key_length(int nid)
  144. -{
  145. - int i;
  146. -
  147. - for (i = 0; digests[i].id; i++)
  148. - if (digests[i].nid == nid)
  149. - return digests[i].keylen;
  150. - return (0);
  151. -}
  152. -
  153. static int cryptodev_digest_init(EVP_MD_CTX *ctx)
  154. {
  155. struct dev_crypto_state *state = ctx->md_data;
  156. @@ -769,7 +772,6 @@ static int cryptodev_digest_init(EVP_MD_CTX *ctx)
  157. printf("cryptodev_digest_init: Can't get digest \n");
  158. return (0);
  159. }
  160. -
  161. memset(state, 0, sizeof(struct dev_crypto_state));
  162. if ((state->d_fd = get_dev_crypto()) < 0) {
  163. @@ -777,8 +779,8 @@ static int cryptodev_digest_init(EVP_MD_CTX *ctx)
  164. return (0);
  165. }
  166. - sess->mackey = state->dummy_mac_key;
  167. - sess->mackeylen = digest_key_length(ctx->digest->type);
  168. + sess->mackey = NULL;
  169. + sess->mackeylen = 0;
  170. sess->mac = digest;
  171. if (ioctl(state->d_fd, CIOCGSESSION, sess) < 0) {
  172. @@ -794,8 +796,8 @@ static int cryptodev_digest_init(EVP_MD_CTX *ctx)
  173. static int cryptodev_digest_update(EVP_MD_CTX *ctx, const void *data,
  174. size_t count)
  175. {
  176. - struct crypt_op cryp;
  177. struct dev_crypto_state *state = ctx->md_data;
  178. + struct crypt_op cryp;
  179. struct session_op *sess = &state->d_sess;
  180. if (!data || state->d_fd < 0) {
  181. @@ -804,7 +806,7 @@ static int cryptodev_digest_update(EVP_MD_CTX *ctx, const void *data,
  182. }
  183. if (!count) {
  184. - return (0);
  185. + return (1);
  186. }
  187. if (!(ctx->flags & EVP_MD_CTX_FLAG_ONESHOT)) {
  188. @@ -828,9 +830,9 @@ static int cryptodev_digest_update(EVP_MD_CTX *ctx, const void *data,
  189. cryp.ses = sess->ses;
  190. cryp.flags = 0;
  191. cryp.len = count;
  192. - cryp.src = (caddr_t) data;
  193. + cryp.src = (void*) data;
  194. cryp.dst = NULL;
  195. - cryp.mac = (caddr_t) state->digest_res;
  196. + cryp.mac = (void*) state->digest_res;
  197. if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
  198. printf("cryptodev_digest_update: digest failed\n");
  199. return (0);
  200. @@ -844,8 +846,6 @@ static int cryptodev_digest_final(EVP_MD_CTX *ctx, unsigned char *md)
  201. struct dev_crypto_state *state = ctx->md_data;
  202. struct session_op *sess = &state->d_sess;
  203. - int ret = 1;
  204. -
  205. if (!md || state->d_fd < 0) {
  206. printf("cryptodev_digest_final: illegal input\n");
  207. return (0);
  208. @@ -859,7 +859,7 @@ static int cryptodev_digest_final(EVP_MD_CTX *ctx, unsigned char *md)
  209. cryp.len = state->mac_len;
  210. cryp.src = state->mac_data;
  211. cryp.dst = NULL;
  212. - cryp.mac = (caddr_t) md;
  213. + cryp.mac = (void*)md;
  214. if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
  215. printf("cryptodev_digest_final: digest failed\n");
  216. return (0);
  217. @@ -870,7 +870,7 @@ static int cryptodev_digest_final(EVP_MD_CTX *ctx, unsigned char *md)
  218. memcpy(md, state->digest_res, ctx->digest->md_size);
  219. - return (ret);
  220. + return 1;
  221. }
  222. static int cryptodev_digest_cleanup(EVP_MD_CTX *ctx)
  223. @@ -921,8 +921,8 @@ static int cryptodev_digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
  224. digest = digest_nid_to_cryptodev(to->digest->type);
  225. - sess->mackey = dstate->dummy_mac_key;
  226. - sess->mackeylen = digest_key_length(to->digest->type);
  227. + sess->mackey = NULL;
  228. + sess->mackeylen = 0;
  229. sess->mac = digest;
  230. dstate->d_fd = get_dev_crypto();
  231. @@ -947,32 +947,116 @@ static int cryptodev_digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
  232. const EVP_MD cryptodev_sha1 = {
  233. NID_sha1,
  234. - NID_undef,
  235. + NID_sha1WithRSAEncryption,
  236. SHA_DIGEST_LENGTH,
  237. +#if defined(EVP_MD_FLAG_PKEY_METHOD_SIGNATURE) && defined(EVP_MD_FLAG_DIGALGID_ABSENT)
  238. + EVP_MD_FLAG_PKEY_METHOD_SIGNATURE|
  239. + EVP_MD_FLAG_DIGALGID_ABSENT|
  240. +#endif
  241. EVP_MD_FLAG_ONESHOT,
  242. cryptodev_digest_init,
  243. cryptodev_digest_update,
  244. cryptodev_digest_final,
  245. cryptodev_digest_copy,
  246. cryptodev_digest_cleanup,
  247. - EVP_PKEY_NULL_method,
  248. + EVP_PKEY_RSA_method,
  249. SHA_CBLOCK,
  250. - sizeof(struct dev_crypto_state),
  251. + sizeof(EVP_MD *)+sizeof(struct dev_crypto_state),
  252. };
  253. -const EVP_MD cryptodev_md5 = {
  254. +static const EVP_MD cryptodev_sha256 = {
  255. + NID_sha256,
  256. + NID_sha256WithRSAEncryption,
  257. + SHA256_DIGEST_LENGTH,
  258. +#if defined(EVP_MD_FLAG_PKEY_METHOD_SIGNATURE) && defined(EVP_MD_FLAG_DIGALGID_ABSENT)
  259. + EVP_MD_FLAG_PKEY_METHOD_SIGNATURE|
  260. + EVP_MD_FLAG_DIGALGID_ABSENT|
  261. +#endif
  262. + EVP_MD_FLAG_ONESHOT,
  263. + cryptodev_digest_init,
  264. + cryptodev_digest_update,
  265. + cryptodev_digest_final,
  266. + cryptodev_digest_copy,
  267. + cryptodev_digest_cleanup,
  268. + EVP_PKEY_RSA_method,
  269. + SHA256_CBLOCK,
  270. + sizeof(EVP_MD *)+sizeof(struct dev_crypto_state),
  271. +};
  272. +
  273. +static const EVP_MD cryptodev_sha224 = {
  274. + NID_sha224,
  275. + NID_sha224WithRSAEncryption,
  276. + SHA224_DIGEST_LENGTH,
  277. +#if defined(EVP_MD_FLAG_PKEY_METHOD_SIGNATURE) && defined(EVP_MD_FLAG_DIGALGID_ABSENT)
  278. + EVP_MD_FLAG_PKEY_METHOD_SIGNATURE|
  279. + EVP_MD_FLAG_DIGALGID_ABSENT|
  280. +#endif
  281. + EVP_MD_FLAG_ONESHOT,
  282. + cryptodev_digest_init,
  283. + cryptodev_digest_update,
  284. + cryptodev_digest_final,
  285. + cryptodev_digest_copy,
  286. + cryptodev_digest_cleanup,
  287. + EVP_PKEY_RSA_method,
  288. + SHA256_CBLOCK,
  289. + sizeof(EVP_MD *)+sizeof(struct dev_crypto_state),
  290. +};
  291. +
  292. +static const EVP_MD cryptodev_sha384 = {
  293. + NID_sha384,
  294. + NID_sha384WithRSAEncryption,
  295. + SHA384_DIGEST_LENGTH,
  296. +#if defined(EVP_MD_FLAG_PKEY_METHOD_SIGNATURE) && defined(EVP_MD_FLAG_DIGALGID_ABSENT)
  297. + EVP_MD_FLAG_PKEY_METHOD_SIGNATURE|
  298. + EVP_MD_FLAG_DIGALGID_ABSENT|
  299. +#endif
  300. + EVP_MD_FLAG_ONESHOT,
  301. + cryptodev_digest_init,
  302. + cryptodev_digest_update,
  303. + cryptodev_digest_final,
  304. + cryptodev_digest_copy,
  305. + cryptodev_digest_cleanup,
  306. + EVP_PKEY_RSA_method,
  307. + SHA512_CBLOCK,
  308. + sizeof(EVP_MD *)+sizeof(struct dev_crypto_state),
  309. +};
  310. +
  311. +static const EVP_MD cryptodev_sha512 = {
  312. + NID_sha512,
  313. + NID_sha512WithRSAEncryption,
  314. + SHA512_DIGEST_LENGTH,
  315. +#if defined(EVP_MD_FLAG_PKEY_METHOD_SIGNATURE) && defined(EVP_MD_FLAG_DIGALGID_ABSENT)
  316. + EVP_MD_FLAG_PKEY_METHOD_SIGNATURE|
  317. + EVP_MD_FLAG_DIGALGID_ABSENT|
  318. +#endif
  319. + EVP_MD_FLAG_ONESHOT,
  320. + cryptodev_digest_init,
  321. + cryptodev_digest_update,
  322. + cryptodev_digest_final,
  323. + cryptodev_digest_copy,
  324. + cryptodev_digest_cleanup,
  325. + EVP_PKEY_RSA_method,
  326. + SHA512_CBLOCK,
  327. + sizeof(EVP_MD *)+sizeof(struct dev_crypto_state),
  328. +};
  329. +
  330. +static const EVP_MD cryptodev_md5 = {
  331. NID_md5,
  332. - NID_undef,
  333. + NID_md5WithRSAEncryption,
  334. 16 /* MD5_DIGEST_LENGTH */ ,
  335. +#if defined(EVP_MD_FLAG_PKEY_METHOD_SIGNATURE) && defined(EVP_MD_FLAG_DIGALGID_ABSENT)
  336. + EVP_MD_FLAG_PKEY_METHOD_SIGNATURE|
  337. + EVP_MD_FLAG_DIGALGID_ABSENT|
  338. +#endif
  339. EVP_MD_FLAG_ONESHOT,
  340. cryptodev_digest_init,
  341. cryptodev_digest_update,
  342. cryptodev_digest_final,
  343. cryptodev_digest_copy,
  344. cryptodev_digest_cleanup,
  345. - EVP_PKEY_NULL_method,
  346. + EVP_PKEY_RSA_method,
  347. 64 /* MD5_CBLOCK */ ,
  348. - sizeof(struct dev_crypto_state),
  349. + sizeof(EVP_MD *)+sizeof(struct dev_crypto_state),
  350. };
  351. # endif /* USE_CRYPTODEV_DIGESTS */
  352. @@ -992,6 +1076,18 @@ cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
  353. case NID_sha1:
  354. *digest = &cryptodev_sha1;
  355. break;
  356. + case NID_sha224:
  357. + *digest = &cryptodev_sha224;
  358. + break;
  359. + case NID_sha256:
  360. + *digest = &cryptodev_sha256;
  361. + break;
  362. + case NID_sha384:
  363. + *digest = &cryptodev_sha384;
  364. + break;
  365. + case NID_sha512:
  366. + *digest = &cryptodev_sha512;
  367. + break;
  368. default:
  369. # endif /* USE_CRYPTODEV_DIGESTS */
  370. *digest = NULL;
  371. @@ -1022,7 +1118,7 @@ static int bn2crparam(const BIGNUM *a, struct crparam *crp)
  372. return (1);
  373. memset(b, 0, bytes);
  374. - crp->crp_p = (caddr_t) b;
  375. + crp->crp_p = (void*) b;
  376. crp->crp_nbits = bits;
  377. for (i = 0, j = 0; i < a->top; i++) {
  378. @@ -1277,7 +1373,7 @@ static DSA_SIG *cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen,
  379. kop.crk_op = CRK_DSA_SIGN;
  380. /* inputs: dgst dsa->p dsa->q dsa->g dsa->priv_key */
  381. - kop.crk_param[0].crp_p = (caddr_t) dgst;
  382. + kop.crk_param[0].crp_p = (void*)dgst;
  383. kop.crk_param[0].crp_nbits = dlen * 8;
  384. if (bn2crparam(dsa->p, &kop.crk_param[1]))
  385. goto err;
  386. @@ -1317,7 +1413,7 @@ cryptodev_dsa_verify(const unsigned char *dgst, int dlen,
  387. kop.crk_op = CRK_DSA_VERIFY;
  388. /* inputs: dgst dsa->p dsa->q dsa->g dsa->pub_key sig->r sig->s */
  389. - kop.crk_param[0].crp_p = (caddr_t) dgst;
  390. + kop.crk_param[0].crp_p = (void*)dgst;
  391. kop.crk_param[0].crp_nbits = dlen * 8;
  392. if (bn2crparam(dsa->p, &kop.crk_param[1]))
  393. goto err;
  394. @@ -1398,9 +1494,10 @@ cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
  395. goto err;
  396. kop.crk_iparams = 3;
  397. - kop.crk_param[3].crp_p = (caddr_t) key;
  398. - kop.crk_param[3].crp_nbits = keylen * 8;
  399. + kop.crk_param[3].crp_p = (void*) key;
  400. + kop.crk_param[3].crp_nbits = keylen;
  401. kop.crk_oparams = 1;
  402. + dhret = keylen / 8;
  403. if (ioctl(fd, CIOCKEY, &kop) == -1) {
  404. const DH_METHOD *meth = DH_OpenSSL();
  405. @@ -1470,7 +1567,7 @@ void ENGINE_load_cryptodev(void)
  406. put_dev_crypto(fd);
  407. if (!ENGINE_set_id(engine, "cryptodev") ||
  408. - !ENGINE_set_name(engine, "BSD cryptodev engine") ||
  409. + !ENGINE_set_name(engine, "cryptodev engine") ||
  410. !ENGINE_set_ciphers(engine, cryptodev_engine_ciphers) ||
  411. !ENGINE_set_digests(engine, cryptodev_engine_digests) ||
  412. !ENGINE_set_ctrl_function(engine, cryptodev_ctrl) ||
  413. --
  414. 1.9.1