patch-crypto_engine_eng_cryptodev_c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. --- openssl-1.0.1e.orig/crypto/engine/eng_cryptodev.c 2013-02-11 16:26:04.000000000 +0100
  2. +++ openssl-1.0.1e/crypto/engine/eng_cryptodev.c 2013-08-09 16:51:49.915851335 +0200
  3. @@ -2,6 +2,7 @@
  4. * Copyright (c) 2002 Bob Beck <beck@openbsd.org>
  5. * Copyright (c) 2002 Theo de Raadt
  6. * Copyright (c) 2002 Markus Friedl
  7. + * Copyright (c) 2012 Nikos Mavrogiannopoulos
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. @@ -74,9 +75,7 @@ struct dev_crypto_state {
  12. int d_fd;
  13. #ifdef USE_CRYPTODEV_DIGESTS
  14. - char dummy_mac_key[HASH_MAX_LEN];
  15. -
  16. - unsigned char digest_res[HASH_MAX_LEN];
  17. + unsigned char digest_res[64];
  18. char *mac_data;
  19. int mac_len;
  20. #endif
  21. @@ -157,15 +156,21 @@ static struct {
  22. static struct {
  23. int id;
  24. int nid;
  25. - int keylen;
  26. + int digestlen;
  27. } digests[] = {
  28. +#if 0
  29. + /* HMAC is not supported */
  30. { CRYPTO_MD5_HMAC, NID_hmacWithMD5, 16},
  31. { CRYPTO_SHA1_HMAC, NID_hmacWithSHA1, 20},
  32. - { CRYPTO_RIPEMD160_HMAC, NID_ripemd160, 16/*?*/},
  33. - { CRYPTO_MD5_KPDK, NID_undef, 0},
  34. - { CRYPTO_SHA1_KPDK, NID_undef, 0},
  35. + { CRYPTO_SHA2_256_HMAC, NID_hmacWithSHA256, 32},
  36. + { CRYPTO_SHA2_384_HMAC, NID_hmacWithSHA384, 48},
  37. + { CRYPTO_SHA2_512_HMAC, NID_hmacWithSHA512, 64},
  38. +#endif
  39. { CRYPTO_MD5, NID_md5, 16},
  40. { CRYPTO_SHA1, NID_sha1, 20},
  41. + { CRYPTO_SHA2_256, NID_sha256, 32},
  42. + { CRYPTO_SHA2_384, NID_sha384, 48},
  43. + { CRYPTO_SHA2_512, NID_sha512, 64},
  44. { 0, NID_undef, 0},
  45. };
  46. #endif
  47. @@ -182,7 +187,7 @@ open_dev_crypto(void)
  48. if ((fd = open("/dev/crypto", O_RDWR, 0)) == -1)
  49. return (-1);
  50. /* close on exec */
  51. - if (fcntl(fd, F_SETFD, 1) == -1) {
  52. + if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) {
  53. close(fd);
  54. fd = -1;
  55. return (-1);
  56. @@ -243,13 +248,14 @@ get_cryptodev_ciphers(const int **cnids)
  57. static int nids[CRYPTO_ALGORITHM_MAX];
  58. struct session_op sess;
  59. int fd, i, count = 0;
  60. + unsigned char fake_key[EVP_MAX_KEY_LENGTH];
  61. if ((fd = get_dev_crypto()) < 0) {
  62. *cnids = NULL;
  63. return (0);
  64. }
  65. memset(&sess, 0, sizeof(sess));
  66. - sess.key = (caddr_t)"123456789abcdefghijklmno";
  67. + sess.key = (void*)fake_key;
  68. for (i = 0; ciphers[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
  69. if (ciphers[i].nid == NID_undef)
  70. @@ -281,6 +287,7 @@ static int
  71. get_cryptodev_digests(const int **cnids)
  72. {
  73. static int nids[CRYPTO_ALGORITHM_MAX];
  74. + unsigned char fake_key[EVP_MAX_KEY_LENGTH];
  75. struct session_op sess;
  76. int fd, i, count = 0;
  77. @@ -289,12 +296,12 @@ get_cryptodev_digests(const int **cnids)
  78. return (0);
  79. }
  80. memset(&sess, 0, sizeof(sess));
  81. - sess.mackey = (caddr_t)"123456789abcdefghijklmno";
  82. + sess.mackey = fake_key;
  83. for (i = 0; digests[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
  84. if (digests[i].nid == NID_undef)
  85. continue;
  86. sess.mac = digests[i].id;
  87. - sess.mackeylen = digests[i].keylen;
  88. + sess.mackeylen = 8;
  89. sess.cipher = 0;
  90. if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
  91. ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
  92. @@ -382,14 +389,14 @@ cryptodev_cipher(EVP_CIPHER_CTX *ctx, un
  93. cryp.ses = sess->ses;
  94. cryp.flags = 0;
  95. cryp.len = inl;
  96. - cryp.src = (caddr_t) in;
  97. - cryp.dst = (caddr_t) out;
  98. + cryp.src = (void*) in;
  99. + cryp.dst = (void*) out;
  100. cryp.mac = 0;
  101. cryp.op = ctx->encrypt ? COP_ENCRYPT : COP_DECRYPT;
  102. if (ctx->cipher->iv_len) {
  103. - cryp.iv = (caddr_t) ctx->iv;
  104. + cryp.iv = (void*) ctx->iv;
  105. if (!ctx->encrypt) {
  106. iiv = in + inl - ctx->cipher->iv_len;
  107. memcpy(save_iv, iiv, ctx->cipher->iv_len);
  108. @@ -440,7 +447,7 @@ cryptodev_init_key(EVP_CIPHER_CTX *ctx,
  109. if ((state->d_fd = get_dev_crypto()) < 0)
  110. return (0);
  111. - sess->key = (caddr_t)key;
  112. + sess->key = (void*)key;
  113. sess->keylen = ctx->key_len;
  114. sess->cipher = cipher;
  115. @@ -660,18 +667,6 @@ digest_nid_to_cryptodev(int nid)
  116. }
  117. -static int
  118. -digest_key_length(int nid)
  119. -{
  120. - int i;
  121. -
  122. - for (i = 0; digests[i].id; i++)
  123. - if (digests[i].nid == nid)
  124. - return digests[i].keylen;
  125. - return (0);
  126. -}
  127. -
  128. -
  129. static int cryptodev_digest_init(EVP_MD_CTX *ctx)
  130. {
  131. struct dev_crypto_state *state = ctx->md_data;
  132. @@ -682,7 +677,6 @@ static int cryptodev_digest_init(EVP_MD_
  133. printf("cryptodev_digest_init: Can't get digest \n");
  134. return (0);
  135. }
  136. -
  137. memset(state, 0, sizeof(struct dev_crypto_state));
  138. if ((state->d_fd = get_dev_crypto()) < 0) {
  139. @@ -690,8 +684,8 @@ static int cryptodev_digest_init(EVP_MD_
  140. return (0);
  141. }
  142. - sess->mackey = state->dummy_mac_key;
  143. - sess->mackeylen = digest_key_length(ctx->digest->type);
  144. + sess->mackey = NULL;
  145. + sess->mackeylen = 0;
  146. sess->mac = digest;
  147. if (ioctl(state->d_fd, CIOCGSESSION, sess) < 0) {
  148. @@ -707,8 +701,8 @@ static int cryptodev_digest_init(EVP_MD_
  149. static int cryptodev_digest_update(EVP_MD_CTX *ctx, const void *data,
  150. size_t count)
  151. {
  152. - struct crypt_op cryp;
  153. struct dev_crypto_state *state = ctx->md_data;
  154. + struct crypt_op cryp;
  155. struct session_op *sess = &state->d_sess;
  156. if (!data || state->d_fd < 0) {
  157. @@ -717,7 +711,7 @@ static int cryptodev_digest_update(EVP_M
  158. }
  159. if (!count) {
  160. - return (0);
  161. + return (1);
  162. }
  163. if (!(ctx->flags & EVP_MD_CTX_FLAG_ONESHOT)) {
  164. @@ -740,9 +734,9 @@ static int cryptodev_digest_update(EVP_M
  165. cryp.ses = sess->ses;
  166. cryp.flags = 0;
  167. cryp.len = count;
  168. - cryp.src = (caddr_t) data;
  169. + cryp.src = (void*) data;
  170. cryp.dst = NULL;
  171. - cryp.mac = (caddr_t) state->digest_res;
  172. + cryp.mac = (void*) state->digest_res;
  173. if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
  174. printf("cryptodev_digest_update: digest failed\n");
  175. return (0);
  176. @@ -757,8 +751,6 @@ static int cryptodev_digest_final(EVP_MD
  177. struct dev_crypto_state *state = ctx->md_data;
  178. struct session_op *sess = &state->d_sess;
  179. - int ret = 1;
  180. -
  181. if (!md || state->d_fd < 0) {
  182. printf("cryptodev_digest_final: illegal input\n");
  183. return(0);
  184. @@ -772,7 +764,7 @@ static int cryptodev_digest_final(EVP_MD
  185. cryp.len = state->mac_len;
  186. cryp.src = state->mac_data;
  187. cryp.dst = NULL;
  188. - cryp.mac = (caddr_t)md;
  189. + cryp.mac = (void*)md;
  190. if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
  191. printf("cryptodev_digest_final: digest failed\n");
  192. return (0);
  193. @@ -783,7 +775,7 @@ static int cryptodev_digest_final(EVP_MD
  194. memcpy(md, state->digest_res, ctx->digest->md_size);
  195. - return (ret);
  196. + return 1;
  197. }
  198. @@ -835,8 +827,8 @@ static int cryptodev_digest_copy(EVP_MD_
  199. digest = digest_nid_to_cryptodev(to->digest->type);
  200. - sess->mackey = dstate->dummy_mac_key;
  201. - sess->mackeylen = digest_key_length(to->digest->type);
  202. + sess->mackey = NULL;
  203. + sess->mackeylen = 0;
  204. sess->mac = digest;
  205. dstate->d_fd = get_dev_crypto();
  206. @@ -861,34 +853,79 @@ static int cryptodev_digest_copy(EVP_MD_
  207. }
  208. -const EVP_MD cryptodev_sha1 = {
  209. +static const EVP_MD cryptodev_sha1 = {
  210. NID_sha1,
  211. - NID_undef,
  212. + NID_sha1WithRSAEncryption,
  213. SHA_DIGEST_LENGTH,
  214. - EVP_MD_FLAG_ONESHOT,
  215. + EVP_MD_FLAG_PKEY_METHOD_SIGNATURE|EVP_MD_FLAG_DIGALGID_ABSENT|EVP_MD_FLAG_ONESHOT,
  216. cryptodev_digest_init,
  217. cryptodev_digest_update,
  218. cryptodev_digest_final,
  219. cryptodev_digest_copy,
  220. cryptodev_digest_cleanup,
  221. - EVP_PKEY_NULL_method,
  222. + EVP_PKEY_RSA_method,
  223. SHA_CBLOCK,
  224. - sizeof(struct dev_crypto_state),
  225. + sizeof(EVP_MD *)+sizeof(struct dev_crypto_state),
  226. };
  227. -const EVP_MD cryptodev_md5 = {
  228. +static const EVP_MD cryptodev_sha256 = {
  229. + NID_sha256,
  230. + NID_sha256WithRSAEncryption,
  231. + SHA256_DIGEST_LENGTH,
  232. + EVP_MD_FLAG_PKEY_METHOD_SIGNATURE|EVP_MD_FLAG_DIGALGID_ABSENT|EVP_MD_FLAG_ONESHOT,
  233. + cryptodev_digest_init,
  234. + cryptodev_digest_update,
  235. + cryptodev_digest_final,
  236. + cryptodev_digest_copy,
  237. + cryptodev_digest_cleanup,
  238. + EVP_PKEY_RSA_method,
  239. + SHA256_CBLOCK,
  240. + sizeof(EVP_MD *)+sizeof(struct dev_crypto_state),
  241. +};
  242. +
  243. +static const EVP_MD cryptodev_sha384 = {
  244. + NID_sha384,
  245. + NID_sha384WithRSAEncryption,
  246. + SHA384_DIGEST_LENGTH,
  247. + EVP_MD_FLAG_PKEY_METHOD_SIGNATURE|EVP_MD_FLAG_DIGALGID_ABSENT|EVP_MD_FLAG_ONESHOT,
  248. + cryptodev_digest_init,
  249. + cryptodev_digest_update,
  250. + cryptodev_digest_final,
  251. + cryptodev_digest_copy,
  252. + cryptodev_digest_cleanup,
  253. + EVP_PKEY_RSA_method,
  254. + SHA512_CBLOCK,
  255. + sizeof(EVP_MD *)+sizeof(struct dev_crypto_state),
  256. +};
  257. +
  258. +static const EVP_MD cryptodev_sha512 = {
  259. + NID_sha512,
  260. + NID_sha512WithRSAEncryption,
  261. + SHA512_DIGEST_LENGTH,
  262. + EVP_MD_FLAG_PKEY_METHOD_SIGNATURE|EVP_MD_FLAG_DIGALGID_ABSENT|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. + SHA512_CBLOCK,
  270. + sizeof(EVP_MD *)+sizeof(struct dev_crypto_state),
  271. +};
  272. +
  273. +static const EVP_MD cryptodev_md5 = {
  274. NID_md5,
  275. - NID_undef,
  276. + NID_md5WithRSAEncryption,
  277. 16 /* MD5_DIGEST_LENGTH */,
  278. - EVP_MD_FLAG_ONESHOT,
  279. + EVP_MD_FLAG_PKEY_METHOD_SIGNATURE|EVP_MD_FLAG_DIGALGID_ABSENT|EVP_MD_FLAG_ONESHOT,
  280. cryptodev_digest_init,
  281. cryptodev_digest_update,
  282. cryptodev_digest_final,
  283. cryptodev_digest_copy,
  284. cryptodev_digest_cleanup,
  285. - EVP_PKEY_NULL_method,
  286. + EVP_PKEY_RSA_method,
  287. 64 /* MD5_CBLOCK */,
  288. - sizeof(struct dev_crypto_state),
  289. + sizeof(EVP_MD *)+sizeof(struct dev_crypto_state),
  290. };
  291. #endif /* USE_CRYPTODEV_DIGESTS */
  292. @@ -909,6 +946,15 @@ cryptodev_engine_digests(ENGINE *e, cons
  293. case NID_sha1:
  294. *digest = &cryptodev_sha1;
  295. break;
  296. + case NID_sha256:
  297. + *digest = &cryptodev_sha256;
  298. + break;
  299. + case NID_sha384:
  300. + *digest = &cryptodev_sha384;
  301. + break;
  302. + case NID_sha512:
  303. + *digest = &cryptodev_sha512;
  304. + break;
  305. default:
  306. #endif /* USE_CRYPTODEV_DIGESTS */
  307. *digest = NULL;
  308. @@ -940,7 +986,7 @@ bn2crparam(const BIGNUM *a, struct crpar
  309. return (1);
  310. memset(b, 0, bytes);
  311. - crp->crp_p = (caddr_t) b;
  312. + crp->crp_p = (void*) b;
  313. crp->crp_nbits = bits;
  314. for (i = 0, j = 0; i < a->top; i++) {
  315. @@ -1193,7 +1239,7 @@ cryptodev_dsa_do_sign(const unsigned cha
  316. kop.crk_op = CRK_DSA_SIGN;
  317. /* inputs: dgst dsa->p dsa->q dsa->g dsa->priv_key */
  318. - kop.crk_param[0].crp_p = (caddr_t)dgst;
  319. + kop.crk_param[0].crp_p = (void*)dgst;
  320. kop.crk_param[0].crp_nbits = dlen * 8;
  321. if (bn2crparam(dsa->p, &kop.crk_param[1]))
  322. goto err;
  323. @@ -1233,7 +1279,7 @@ cryptodev_dsa_verify(const unsigned char
  324. kop.crk_op = CRK_DSA_VERIFY;
  325. /* inputs: dgst dsa->p dsa->q dsa->g dsa->pub_key sig->r sig->s */
  326. - kop.crk_param[0].crp_p = (caddr_t)dgst;
  327. + kop.crk_param[0].crp_p = (void*)dgst;
  328. kop.crk_param[0].crp_nbits = dlen * 8;
  329. if (bn2crparam(dsa->p, &kop.crk_param[1]))
  330. goto err;
  331. @@ -1311,7 +1357,7 @@ cryptodev_dh_compute_key(unsigned char *
  332. goto err;
  333. kop.crk_iparams = 3;
  334. - kop.crk_param[3].crp_p = (caddr_t) key;
  335. + kop.crk_param[3].crp_p = (void*) key;
  336. kop.crk_param[3].crp_nbits = keylen * 8;
  337. kop.crk_oparams = 1;
  338. @@ -1385,7 +1431,7 @@ ENGINE_load_cryptodev(void)
  339. put_dev_crypto(fd);
  340. if (!ENGINE_set_id(engine, "cryptodev") ||
  341. - !ENGINE_set_name(engine, "BSD cryptodev engine") ||
  342. + !ENGINE_set_name(engine, "cryptodev engine") ||
  343. !ENGINE_set_ciphers(engine, cryptodev_engine_ciphers) ||
  344. !ENGINE_set_digests(engine, cryptodev_engine_digests) ||
  345. !ENGINE_set_ctrl_function(engine, cryptodev_ctrl) ||