openssl-003-cryptodev-Fix-issue-with-signature-generation.patch 12 KB

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