openbsd_hw.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /* Written by Ben Laurie, 2001 */
  2. /*
  3. * Copyright (c) 2001 The OpenSSL Project. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in
  14. * the documentation and/or other materials provided with the
  15. * distribution.
  16. *
  17. * 3. All advertising materials mentioning features or use of this
  18. * software must display the following acknowledgment:
  19. * "This product includes software developed by the OpenSSL Project
  20. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  21. *
  22. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  23. * endorse or promote products derived from this software without
  24. * prior written permission. For written permission, please contact
  25. * openssl-core@openssl.org.
  26. *
  27. * 5. Products derived from this software may not be called "OpenSSL"
  28. * nor may "OpenSSL" appear in their names without prior written
  29. * permission of the OpenSSL Project.
  30. *
  31. * 6. Redistributions of any form whatsoever must retain the following
  32. * acknowledgment:
  33. * "This product includes software developed by the OpenSSL Project
  34. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  35. *
  36. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  37. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  38. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  39. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  42. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  43. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  44. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  45. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  46. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  47. * OF THE POSSIBILITY OF SUCH DAMAGE.
  48. */
  49. #include <openssl/evp.h>
  50. #include <openssl/objects.h>
  51. #include <openssl/rsa.h>
  52. #include "evp_locl.h"
  53. /*
  54. * This stuff should now all be supported through
  55. * crypto/engine/hw_openbsd_dev_crypto.c unless I botched it up
  56. */
  57. static void *dummy = &dummy;
  58. #if 0
  59. /* check flag after OpenSSL headers to ensure make depend works */
  60. # ifdef OPENSSL_OPENBSD_DEV_CRYPTO
  61. # include <fcntl.h>
  62. # include <stdio.h>
  63. # include <errno.h>
  64. # include <sys/ioctl.h>
  65. # include <crypto/cryptodev.h>
  66. # include <unistd.h>
  67. # include <assert.h>
  68. /* longest key supported in hardware */
  69. # define MAX_HW_KEY 24
  70. # define MAX_HW_IV 8
  71. # define MD5_DIGEST_LENGTH 16
  72. # define MD5_CBLOCK 64
  73. static int fd;
  74. static int dev_failed;
  75. typedef struct session_op session_op;
  76. # define CDATA(ctx) EVP_C_DATA(session_op,ctx)
  77. static void err(const char *str)
  78. {
  79. fprintf(stderr, "%s: errno %d\n", str, errno);
  80. }
  81. static int dev_crypto_init(session_op *ses)
  82. {
  83. if (dev_failed)
  84. return 0;
  85. if (!fd) {
  86. int cryptodev_fd;
  87. if ((cryptodev_fd = open("/dev/crypto", O_RDWR, 0)) < 0) {
  88. err("/dev/crypto");
  89. dev_failed = 1;
  90. return 0;
  91. }
  92. if (ioctl(cryptodev_fd, CRIOGET, &fd) == -1) {
  93. err("CRIOGET failed");
  94. close(cryptodev_fd);
  95. dev_failed = 1;
  96. return 0;
  97. }
  98. close(cryptodev_fd);
  99. }
  100. assert(ses);
  101. memset(ses, '\0', sizeof *ses);
  102. return 1;
  103. }
  104. static int dev_crypto_cleanup(EVP_CIPHER_CTX *ctx)
  105. {
  106. if (ioctl(fd, CIOCFSESSION, &CDATA(ctx)->ses) == -1)
  107. err("CIOCFSESSION failed");
  108. OPENSSL_free(CDATA(ctx)->key);
  109. return 1;
  110. }
  111. static int dev_crypto_init_key(EVP_CIPHER_CTX *ctx, int cipher,
  112. const unsigned char *key, int klen)
  113. {
  114. if (!dev_crypto_init(CDATA(ctx)))
  115. return 0;
  116. CDATA(ctx)->key = OPENSSL_malloc(MAX_HW_KEY);
  117. assert(ctx->cipher->iv_len <= MAX_HW_IV);
  118. memcpy(CDATA(ctx)->key, key, klen);
  119. CDATA(ctx)->cipher = cipher;
  120. CDATA(ctx)->keylen = klen;
  121. if (ioctl(fd, CIOCGSESSION, CDATA(ctx)) == -1) {
  122. err("CIOCGSESSION failed");
  123. return 0;
  124. }
  125. return 1;
  126. }
  127. static int dev_crypto_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  128. const unsigned char *in, unsigned int inl)
  129. {
  130. struct crypt_op cryp;
  131. unsigned char lb[MAX_HW_IV];
  132. if (!inl)
  133. return 1;
  134. assert(CDATA(ctx));
  135. assert(!dev_failed);
  136. memset(&cryp, '\0', sizeof cryp);
  137. cryp.ses = CDATA(ctx)->ses;
  138. cryp.op = ctx->encrypt ? COP_ENCRYPT : COP_DECRYPT;
  139. cryp.flags = 0;
  140. cryp.len = inl;
  141. assert((inl & (ctx->cipher->block_size - 1)) == 0);
  142. cryp.src = (caddr_t) in;
  143. cryp.dst = (caddr_t) out;
  144. cryp.mac = 0;
  145. if (ctx->cipher->iv_len)
  146. cryp.iv = (caddr_t) ctx->iv;
  147. if (!ctx->encrypt)
  148. memcpy(lb, &in[cryp.len - ctx->cipher->iv_len], ctx->cipher->iv_len);
  149. if (ioctl(fd, CIOCCRYPT, &cryp) == -1) {
  150. if (errno == EINVAL) { /* buffers are misaligned */
  151. unsigned int cinl = 0;
  152. char *cin = NULL;
  153. char *cout = NULL;
  154. /* NB: this can only make cinl != inl with stream ciphers */
  155. cinl = (inl + 3) / 4 * 4;
  156. if (((unsigned long)in & 3) || cinl != inl) {
  157. cin = OPENSSL_malloc(cinl);
  158. memcpy(cin, in, inl);
  159. cryp.src = cin;
  160. }
  161. if (((unsigned long)out & 3) || cinl != inl) {
  162. cout = OPENSSL_malloc(cinl);
  163. cryp.dst = cout;
  164. }
  165. cryp.len = cinl;
  166. if (ioctl(fd, CIOCCRYPT, &cryp) == -1) {
  167. err("CIOCCRYPT(2) failed");
  168. printf("src=%p dst=%p\n", cryp.src, cryp.dst);
  169. abort();
  170. return 0;
  171. }
  172. if (cout) {
  173. memcpy(out, cout, inl);
  174. OPENSSL_free(cout);
  175. }
  176. if (cin)
  177. OPENSSL_free(cin);
  178. } else {
  179. err("CIOCCRYPT failed");
  180. abort();
  181. return 0;
  182. }
  183. }
  184. if (ctx->encrypt)
  185. memcpy(ctx->iv, &out[cryp.len - ctx->cipher->iv_len],
  186. ctx->cipher->iv_len);
  187. else
  188. memcpy(ctx->iv, lb, ctx->cipher->iv_len);
  189. return 1;
  190. }
  191. static int dev_crypto_des_ede3_init_key(EVP_CIPHER_CTX *ctx,
  192. const unsigned char *key,
  193. const unsigned char *iv, int enc)
  194. {
  195. return dev_crypto_init_key(ctx, CRYPTO_3DES_CBC, key, 24);
  196. }
  197. # define dev_crypto_des_ede3_cbc_cipher dev_crypto_cipher
  198. BLOCK_CIPHER_def_cbc(dev_crypto_des_ede3, session_op, NID_des_ede3, 8, 24, 8,
  199. 0, dev_crypto_des_ede3_init_key,
  200. dev_crypto_cleanup,
  201. EVP_CIPHER_set_asn1_iv, EVP_CIPHER_get_asn1_iv, NULL)
  202. static int dev_crypto_rc4_init_key(EVP_CIPHER_CTX *ctx,
  203. const unsigned char *key,
  204. const unsigned char *iv, int enc)
  205. {
  206. return dev_crypto_init_key(ctx, CRYPTO_ARC4, key, 16);
  207. }
  208. static const EVP_CIPHER r4_cipher = {
  209. NID_rc4,
  210. 1, 16, 0, /* FIXME: key should be up to 256 bytes */
  211. EVP_CIPH_VARIABLE_LENGTH,
  212. dev_crypto_rc4_init_key,
  213. dev_crypto_cipher,
  214. dev_crypto_cleanup,
  215. sizeof(session_op),
  216. NULL,
  217. NULL,
  218. NULL
  219. };
  220. const EVP_CIPHER *EVP_dev_crypto_rc4(void)
  221. {
  222. return &r4_cipher;
  223. }
  224. typedef struct {
  225. session_op sess;
  226. char *data;
  227. int len;
  228. unsigned char md[EVP_MAX_MD_SIZE];
  229. } MD_DATA;
  230. static int dev_crypto_init_digest(MD_DATA *md_data, int mac)
  231. {
  232. if (!dev_crypto_init(&md_data->sess))
  233. return 0;
  234. md_data->len = 0;
  235. md_data->data = NULL;
  236. md_data->sess.mac = mac;
  237. if (ioctl(fd, CIOCGSESSION, &md_data->sess) == -1) {
  238. err("CIOCGSESSION failed");
  239. return 0;
  240. }
  241. return 1;
  242. }
  243. static int dev_crypto_cleanup_digest(MD_DATA *md_data)
  244. {
  245. if (ioctl(fd, CIOCFSESSION, &md_data->sess.ses) == -1) {
  246. err("CIOCFSESSION failed");
  247. return 0;
  248. }
  249. return 1;
  250. }
  251. /* FIXME: if device can do chained MACs, then don't accumulate */
  252. /* FIXME: move accumulation to the framework */
  253. static int dev_crypto_md5_init(EVP_MD_CTX *ctx)
  254. {
  255. return dev_crypto_init_digest(ctx->md_data, CRYPTO_MD5);
  256. }
  257. static int do_digest(int ses, unsigned char *md, const void *data, int len)
  258. {
  259. struct crypt_op cryp;
  260. static unsigned char md5zero[16] = {
  261. 0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04,
  262. 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e
  263. };
  264. /* some cards can't do zero length */
  265. if (!len) {
  266. memcpy(md, md5zero, 16);
  267. return 1;
  268. }
  269. memset(&cryp, '\0', sizeof cryp);
  270. cryp.ses = ses;
  271. cryp.op = COP_ENCRYPT; /* required to do the MAC rather than check
  272. * it */
  273. cryp.len = len;
  274. cryp.src = (caddr_t) data;
  275. cryp.dst = (caddr_t) data; // FIXME!!!
  276. cryp.mac = (caddr_t) md;
  277. if (ioctl(fd, CIOCCRYPT, &cryp) == -1) {
  278. if (errno == EINVAL) { /* buffer is misaligned */
  279. char *dcopy;
  280. dcopy = OPENSSL_malloc(len);
  281. memcpy(dcopy, data, len);
  282. cryp.src = dcopy;
  283. cryp.dst = cryp.src; // FIXME!!!
  284. if (ioctl(fd, CIOCCRYPT, &cryp) == -1) {
  285. err("CIOCCRYPT(MAC2) failed");
  286. abort();
  287. return 0;
  288. }
  289. OPENSSL_free(dcopy);
  290. } else {
  291. err("CIOCCRYPT(MAC) failed");
  292. abort();
  293. return 0;
  294. }
  295. }
  296. // printf("done\n");
  297. return 1;
  298. }
  299. static int dev_crypto_md5_update(EVP_MD_CTX *ctx, const void *data,
  300. unsigned long len)
  301. {
  302. MD_DATA *md_data = ctx->md_data;
  303. if (ctx->flags & EVP_MD_CTX_FLAG_ONESHOT)
  304. return do_digest(md_data->sess.ses, md_data->md, data, len);
  305. md_data->data = OPENSSL_realloc(md_data->data, md_data->len + len);
  306. memcpy(md_data->data + md_data->len, data, len);
  307. md_data->len += len;
  308. return 1;
  309. }
  310. static int dev_crypto_md5_final(EVP_MD_CTX *ctx, unsigned char *md)
  311. {
  312. int ret;
  313. MD_DATA *md_data = ctx->md_data;
  314. if (ctx->flags & EVP_MD_CTX_FLAG_ONESHOT) {
  315. memcpy(md, md_data->md, MD5_DIGEST_LENGTH);
  316. ret = 1;
  317. } else {
  318. ret = do_digest(md_data->sess.ses, md, md_data->data, md_data->len);
  319. OPENSSL_free(md_data->data);
  320. md_data->data = NULL;
  321. md_data->len = 0;
  322. }
  323. return ret;
  324. }
  325. static int dev_crypto_md5_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
  326. {
  327. const MD_DATA *from_md = from->md_data;
  328. MD_DATA *to_md = to->md_data;
  329. // How do we copy sessions?
  330. assert(from->digest->flags & EVP_MD_FLAG_ONESHOT);
  331. to_md->data = OPENSSL_malloc(from_md->len);
  332. memcpy(to_md->data, from_md->data, from_md->len);
  333. return 1;
  334. }
  335. static int dev_crypto_md5_cleanup(EVP_MD_CTX *ctx)
  336. {
  337. return dev_crypto_cleanup_digest(ctx->md_data);
  338. }
  339. static const EVP_MD md5_md = {
  340. NID_md5,
  341. NID_md5WithRSAEncryption,
  342. MD5_DIGEST_LENGTH,
  343. EVP_MD_FLAG_ONESHOT, // XXX: set according to device info...
  344. dev_crypto_md5_init,
  345. dev_crypto_md5_update,
  346. dev_crypto_md5_final,
  347. dev_crypto_md5_copy,
  348. dev_crypto_md5_cleanup,
  349. EVP_PKEY_RSA_method,
  350. MD5_CBLOCK,
  351. sizeof(MD_DATA),
  352. };
  353. const EVP_MD *EVP_dev_crypto_md5(void)
  354. {
  355. return &md5_md;
  356. }
  357. # endif
  358. #endif