crypto.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2006
  4. * 2007, 2008, 2009 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/crypto.h>
  20. #include <grub/misc.h>
  21. #include <grub/mm.h>
  22. #include <grub/term.h>
  23. GRUB_EXPORT(grub_burn_stack);
  24. GRUB_EXPORT(grub_cipher_register);
  25. GRUB_EXPORT(grub_cipher_unregister);
  26. GRUB_EXPORT(grub_md_register);
  27. GRUB_EXPORT(grub_md_unregister);
  28. GRUB_EXPORT(grub_crypto_lookup_md_by_name);
  29. GRUB_EXPORT(grub_crypto_memcmp);
  30. GRUB_EXPORT(grub_crypto_autoload_hook);
  31. GRUB_EXPORT(grub_crypto_gcry_error);
  32. GRUB_EXPORT(grub_crypto_hmac_buffer);
  33. struct grub_crypto_hmac_handle
  34. {
  35. const struct gcry_md_spec *md;
  36. void *ctx;
  37. void *opad;
  38. };
  39. static gcry_cipher_spec_t *grub_ciphers = NULL;
  40. static gcry_md_spec_t *grub_digests = NULL;
  41. void (*grub_crypto_autoload_hook) (const char *name) = NULL;
  42. /* Based on libgcrypt-1.4.4/src/misc.c. */
  43. void
  44. grub_burn_stack (grub_size_t size)
  45. {
  46. char buf[64];
  47. grub_memset (buf, 0, sizeof (buf));
  48. if (size > sizeof (buf))
  49. grub_burn_stack (size - sizeof (buf));
  50. }
  51. void
  52. grub_cipher_register (gcry_cipher_spec_t *cipher)
  53. {
  54. cipher->next = grub_ciphers;
  55. grub_ciphers = cipher;
  56. }
  57. void
  58. grub_cipher_unregister (gcry_cipher_spec_t *cipher)
  59. {
  60. gcry_cipher_spec_t **ciph;
  61. for (ciph = &grub_ciphers; *ciph; ciph = &((*ciph)->next))
  62. if (*ciph == cipher)
  63. {
  64. *ciph = (*ciph)->next;
  65. break;
  66. }
  67. }
  68. void
  69. grub_md_register (gcry_md_spec_t *digest)
  70. {
  71. digest->next = grub_digests;
  72. grub_digests = digest;
  73. }
  74. void
  75. grub_md_unregister (gcry_md_spec_t *cipher)
  76. {
  77. gcry_md_spec_t **ciph;
  78. for (ciph = &grub_digests; *ciph; ciph = &((*ciph)->next))
  79. if (*ciph == cipher)
  80. {
  81. *ciph = (*ciph)->next;
  82. break;
  83. }
  84. }
  85. void
  86. grub_crypto_hash (const gcry_md_spec_t *hash, void *out, const void *in,
  87. grub_size_t inlen)
  88. {
  89. grub_uint8_t ctx[hash->contextsize];
  90. hash->init (&ctx);
  91. hash->write (&ctx, in, inlen);
  92. hash->final (&ctx);
  93. grub_memcpy (out, hash->read (&ctx), hash->mdlen);
  94. }
  95. const gcry_md_spec_t *
  96. grub_crypto_lookup_md_by_name (const char *name)
  97. {
  98. const gcry_md_spec_t *md;
  99. int first = 1;
  100. while (1)
  101. {
  102. for (md = grub_digests; md; md = md->next)
  103. if (grub_strcasecmp (name, md->name) == 0)
  104. return md;
  105. if (grub_crypto_autoload_hook && first)
  106. grub_crypto_autoload_hook (name);
  107. else
  108. return NULL;
  109. first = 0;
  110. }
  111. }
  112. const gcry_cipher_spec_t *
  113. grub_crypto_lookup_cipher_by_name (const char *name)
  114. {
  115. const gcry_cipher_spec_t *ciph;
  116. int first = 1;
  117. while (1)
  118. {
  119. for (ciph = grub_ciphers; ciph; ciph = ciph->next)
  120. {
  121. const char **alias;
  122. if (grub_strcasecmp (name, ciph->name) == 0)
  123. return ciph;
  124. if (!ciph->aliases)
  125. continue;
  126. for (alias = ciph->aliases; *alias; alias++)
  127. if (grub_strcasecmp (name, *alias) == 0)
  128. return ciph;
  129. }
  130. if (grub_crypto_autoload_hook && first)
  131. grub_crypto_autoload_hook (name);
  132. else
  133. return NULL;
  134. first = 0;
  135. }
  136. }
  137. grub_crypto_cipher_handle_t
  138. grub_crypto_cipher_open (const struct gcry_cipher_spec *cipher)
  139. {
  140. grub_crypto_cipher_handle_t ret;
  141. ret = grub_malloc (sizeof (*ret) + cipher->contextsize);
  142. if (!ret)
  143. return NULL;
  144. ret->cipher = cipher;
  145. return ret;
  146. }
  147. gcry_err_code_t
  148. grub_crypto_cipher_set_key (grub_crypto_cipher_handle_t cipher,
  149. const unsigned char *key,
  150. unsigned keylen)
  151. {
  152. return cipher->cipher->setkey (cipher->ctx, key, keylen);
  153. }
  154. void
  155. grub_crypto_cipher_close (grub_crypto_cipher_handle_t cipher)
  156. {
  157. grub_free (cipher);
  158. }
  159. void
  160. grub_crypto_xor (void *out, const void *in1, const void *in2, grub_size_t size)
  161. {
  162. const grub_uint8_t *in1ptr = in1, *in2ptr = in2;
  163. grub_uint8_t *outptr = out;
  164. while (size--)
  165. {
  166. *outptr = *in1ptr ^ *in2ptr;
  167. in1ptr++;
  168. in2ptr++;
  169. outptr++;
  170. }
  171. }
  172. gcry_err_code_t
  173. grub_crypto_ecb_decrypt (grub_crypto_cipher_handle_t cipher,
  174. void *out, void *in, grub_size_t size)
  175. {
  176. grub_uint8_t *inptr, *outptr, *end;
  177. if (!cipher->cipher->decrypt)
  178. return GPG_ERR_NOT_SUPPORTED;
  179. if (size % cipher->cipher->blocksize != 0)
  180. return GPG_ERR_INV_ARG;
  181. end = (grub_uint8_t *) in + size;
  182. for (inptr = in, outptr = out; inptr < end;
  183. inptr += cipher->cipher->blocksize, outptr += cipher->cipher->blocksize)
  184. cipher->cipher->decrypt (cipher->ctx, outptr, inptr);
  185. return GPG_ERR_NO_ERROR;
  186. }
  187. gcry_err_code_t
  188. grub_crypto_ecb_encrypt (grub_crypto_cipher_handle_t cipher,
  189. void *out, void *in, grub_size_t size)
  190. {
  191. grub_uint8_t *inptr, *outptr, *end;
  192. if (!cipher->cipher->encrypt)
  193. return GPG_ERR_NOT_SUPPORTED;
  194. if (size % cipher->cipher->blocksize != 0)
  195. return GPG_ERR_INV_ARG;
  196. end = (grub_uint8_t *) in + size;
  197. for (inptr = in, outptr = out; inptr < end;
  198. inptr += cipher->cipher->blocksize, outptr += cipher->cipher->blocksize)
  199. cipher->cipher->encrypt (cipher->ctx, outptr, inptr);
  200. return GPG_ERR_NO_ERROR;
  201. }
  202. gcry_err_code_t
  203. grub_crypto_cbc_encrypt (grub_crypto_cipher_handle_t cipher,
  204. void *out, void *in, grub_size_t size,
  205. void *iv_in)
  206. {
  207. grub_uint8_t *inptr, *outptr, *end;
  208. void *iv;
  209. if (!cipher->cipher->decrypt)
  210. return GPG_ERR_NOT_SUPPORTED;
  211. if (size % cipher->cipher->blocksize != 0)
  212. return GPG_ERR_INV_ARG;
  213. end = (grub_uint8_t *) in + size;
  214. iv = iv_in;
  215. for (inptr = in, outptr = out; inptr < end;
  216. inptr += cipher->cipher->blocksize, outptr += cipher->cipher->blocksize)
  217. {
  218. grub_crypto_xor (outptr, inptr, iv, cipher->cipher->blocksize);
  219. cipher->cipher->encrypt (cipher->ctx, outptr, outptr);
  220. iv = outptr;
  221. }
  222. grub_memcpy (iv_in, iv, cipher->cipher->blocksize);
  223. return GPG_ERR_NO_ERROR;
  224. }
  225. gcry_err_code_t
  226. grub_crypto_cbc_decrypt (grub_crypto_cipher_handle_t cipher,
  227. void *out, void *in, grub_size_t size,
  228. void *iv)
  229. {
  230. grub_uint8_t *inptr, *outptr, *end;
  231. grub_uint8_t ivt[cipher->cipher->blocksize];
  232. if (!cipher->cipher->decrypt)
  233. return GPG_ERR_NOT_SUPPORTED;
  234. if (size % cipher->cipher->blocksize != 0)
  235. return GPG_ERR_INV_ARG;
  236. end = (grub_uint8_t *) in + size;
  237. for (inptr = in, outptr = out; inptr < end;
  238. inptr += cipher->cipher->blocksize, outptr += cipher->cipher->blocksize)
  239. {
  240. grub_memcpy (ivt, inptr, cipher->cipher->blocksize);
  241. cipher->cipher->decrypt (cipher->ctx, outptr, inptr);
  242. grub_crypto_xor (outptr, outptr, iv, cipher->cipher->blocksize);
  243. grub_memcpy (iv, ivt, cipher->cipher->blocksize);
  244. }
  245. return GPG_ERR_NO_ERROR;
  246. }
  247. /* Based on gcry/cipher/md.c. */
  248. struct grub_crypto_hmac_handle *
  249. grub_crypto_hmac_init (const struct gcry_md_spec *md,
  250. const void *key, grub_size_t keylen)
  251. {
  252. grub_uint8_t *helpkey = NULL;
  253. grub_uint8_t *ipad = NULL, *opad = NULL;
  254. void *ctx = NULL;
  255. struct grub_crypto_hmac_handle *ret = NULL;
  256. unsigned i;
  257. if (md->mdlen > md->blocksize)
  258. return NULL;
  259. ctx = grub_malloc (md->contextsize);
  260. if (!ctx)
  261. goto err;
  262. if ( keylen > md->blocksize )
  263. {
  264. helpkey = grub_malloc (md->mdlen);
  265. if (!helpkey)
  266. goto err;
  267. grub_crypto_hash (md, helpkey, key, keylen);
  268. key = helpkey;
  269. keylen = md->mdlen;
  270. }
  271. ipad = grub_zalloc (md->blocksize);
  272. if (!ipad)
  273. goto err;
  274. opad = grub_zalloc (md->blocksize);
  275. if (!opad)
  276. goto err;
  277. grub_memcpy ( ipad, key, keylen );
  278. grub_memcpy ( opad, key, keylen );
  279. for (i=0; i < md->blocksize; i++ )
  280. {
  281. ipad[i] ^= 0x36;
  282. opad[i] ^= 0x5c;
  283. }
  284. grub_free (helpkey);
  285. helpkey = NULL;
  286. md->init (ctx);
  287. md->write (ctx, ipad, md->blocksize); /* inner pad */
  288. grub_memset (ipad, 0, md->blocksize);
  289. grub_free (ipad);
  290. ipad = NULL;
  291. ret = grub_malloc (sizeof (*ret));
  292. if (!ret)
  293. goto err;
  294. ret->md = md;
  295. ret->ctx = ctx;
  296. ret->opad = opad;
  297. return ret;
  298. err:
  299. grub_free (helpkey);
  300. grub_free (ctx);
  301. grub_free (ipad);
  302. grub_free (opad);
  303. return NULL;
  304. }
  305. void
  306. grub_crypto_hmac_write (struct grub_crypto_hmac_handle *hnd, void *data,
  307. grub_size_t datalen)
  308. {
  309. hnd->md->write (hnd->ctx, data, datalen);
  310. }
  311. gcry_err_code_t
  312. grub_crypto_hmac_fini (struct grub_crypto_hmac_handle *hnd, void *out)
  313. {
  314. grub_uint8_t *p;
  315. grub_uint8_t *ctx2;
  316. ctx2 = grub_malloc (hnd->md->contextsize);
  317. if (!ctx2)
  318. return GPG_ERR_OUT_OF_MEMORY;
  319. hnd->md->final (hnd->ctx);
  320. hnd->md->read (hnd->ctx);
  321. p = hnd->md->read (hnd->ctx);
  322. hnd->md->init (ctx2);
  323. hnd->md->write (ctx2, hnd->opad, hnd->md->blocksize);
  324. hnd->md->write (ctx2, p, hnd->md->mdlen);
  325. hnd->md->final (ctx2);
  326. grub_memset (hnd->opad, 0, hnd->md->blocksize);
  327. grub_free (hnd->opad);
  328. grub_memset (hnd->ctx, 0, hnd->md->contextsize);
  329. grub_free (hnd->ctx);
  330. grub_memcpy (out, hnd->md->read (ctx2), hnd->md->mdlen);
  331. grub_memset (ctx2, 0, hnd->md->contextsize);
  332. grub_free (ctx2);
  333. grub_memset (hnd, 0, sizeof (*hnd));
  334. grub_free (hnd);
  335. return GPG_ERR_NO_ERROR;
  336. }
  337. gcry_err_code_t
  338. grub_crypto_hmac_buffer (const struct gcry_md_spec *md,
  339. const void *key, grub_size_t keylen,
  340. void *data, grub_size_t datalen, void *out)
  341. {
  342. struct grub_crypto_hmac_handle *hnd;
  343. hnd = grub_crypto_hmac_init (md, key, keylen);
  344. if (!hnd)
  345. return GPG_ERR_OUT_OF_MEMORY;
  346. grub_crypto_hmac_write (hnd, data, datalen);
  347. return grub_crypto_hmac_fini (hnd, out);
  348. }
  349. grub_err_t
  350. grub_crypto_gcry_error (gcry_err_code_t in)
  351. {
  352. if (in == GPG_ERR_NO_ERROR)
  353. return GRUB_ERR_NONE;
  354. return GRUB_ACCESS_DENIED;
  355. }
  356. int
  357. grub_crypto_memcmp (const void *a, const void *b, grub_size_t n)
  358. {
  359. register grub_size_t counter = 0;
  360. const grub_uint8_t *pa, *pb;
  361. for (pa = a, pb = b; n; pa++, pb++, n--)
  362. {
  363. if (*pa != *pb)
  364. counter++;
  365. }
  366. return !!counter;
  367. }
  368. #ifndef GRUB_MKPASSWD
  369. GRUB_EXPORT(grub_password_get);
  370. int
  371. grub_password_get (char buf[], unsigned buf_size)
  372. {
  373. unsigned cur_len = 0;
  374. int key;
  375. while (1)
  376. {
  377. key = GRUB_TERM_ASCII_CHAR (grub_getkey ());
  378. if (key == '\n' || key == '\r')
  379. break;
  380. if (key == '\e')
  381. {
  382. cur_len = 0;
  383. break;
  384. }
  385. if (key == '\b')
  386. {
  387. cur_len--;
  388. continue;
  389. }
  390. if (!grub_isprint (key))
  391. continue;
  392. if (cur_len + 2 < buf_size)
  393. buf[cur_len++] = key;
  394. }
  395. grub_memset (buf + cur_len, 0, buf_size - cur_len);
  396. grub_putchar ('\n');
  397. grub_refresh ();
  398. return (key != '\e');
  399. }
  400. #endif