grub-mkpasswd-pbkdf2.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 1992-1999,2001,2003,2004,2005,2009 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <config.h>
  19. #include <grub/types.h>
  20. #include <grub/crypto.h>
  21. #include <grub/auth.h>
  22. #include <grub/emu/misc.h>
  23. #include <grub/util/misc.h>
  24. #include <grub/i18n.h>
  25. #include <grub/misc.h>
  26. #include <unistd.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #define _GNU_SOURCE 1
  31. #pragma GCC diagnostic ignored "-Wmissing-prototypes"
  32. #pragma GCC diagnostic ignored "-Wmissing-declarations"
  33. #include <argp.h>
  34. #pragma GCC diagnostic error "-Wmissing-prototypes"
  35. #pragma GCC diagnostic error "-Wmissing-declarations"
  36. #include "progname.h"
  37. static struct argp_option options[] = {
  38. {"iteration-count", 'c', N_("NUM"), 0, N_("Number of PBKDF2 iterations"), 0},
  39. {"buflen", 'l', N_("NUM"), 0, N_("Length of generated hash"), 0},
  40. {"salt", 's', N_("NUM"), 0, N_("Length of salt"), 0},
  41. { 0, 0, 0, 0, 0, 0 }
  42. };
  43. struct arguments
  44. {
  45. unsigned int count;
  46. unsigned int buflen;
  47. unsigned int saltlen;
  48. };
  49. static error_t
  50. argp_parser (int key, char *arg, struct argp_state *state)
  51. {
  52. /* Get the input argument from argp_parse, which we
  53. know is a pointer to our arguments structure. */
  54. struct arguments *arguments = state->input;
  55. switch (key)
  56. {
  57. case 'c':
  58. arguments->count = strtoul (arg, NULL, 0);
  59. break;
  60. case 'l':
  61. arguments->buflen = strtoul (arg, NULL, 0);
  62. break;
  63. case 's':
  64. arguments->saltlen = strtoul (arg, NULL, 0);
  65. break;
  66. default:
  67. return ARGP_ERR_UNKNOWN;
  68. }
  69. return 0;
  70. }
  71. static struct argp argp = {
  72. options, argp_parser, N_("[OPTIONS]"),
  73. N_("Generate PBKDF2 password hash."),
  74. NULL, NULL, NULL
  75. };
  76. static void
  77. hexify (char *hex, grub_uint8_t *bin, grub_size_t n)
  78. {
  79. while (n--)
  80. {
  81. if (((*bin & 0xf0) >> 4) < 10)
  82. *hex = ((*bin & 0xf0) >> 4) + '0';
  83. else
  84. *hex = ((*bin & 0xf0) >> 4) + 'A' - 10;
  85. hex++;
  86. if ((*bin & 0xf) < 10)
  87. *hex = (*bin & 0xf) + '0';
  88. else
  89. *hex = (*bin & 0xf) + 'A' - 10;
  90. hex++;
  91. bin++;
  92. }
  93. *hex = 0;
  94. }
  95. int
  96. main (int argc, char *argv[])
  97. {
  98. struct arguments arguments = {
  99. .count = 10000,
  100. .buflen = 64,
  101. .saltlen = 64
  102. };
  103. char *result, *ptr;
  104. gcry_err_code_t gcry_err;
  105. grub_uint8_t *buf, *salt;
  106. char pass1[GRUB_AUTH_MAX_PASSLEN];
  107. char pass2[GRUB_AUTH_MAX_PASSLEN];
  108. grub_util_host_init (&argc, &argv);
  109. /* Check for options. */
  110. if (argp_parse (&argp, argc, argv, 0, 0, &arguments) != 0)
  111. {
  112. fprintf (stderr, "%s", _("Error in parsing command line arguments\n"));
  113. exit(1);
  114. }
  115. buf = xmalloc (arguments.buflen);
  116. salt = xmalloc (arguments.saltlen);
  117. printf ("%s", _("Enter password: "));
  118. if (!grub_password_get (pass1, GRUB_AUTH_MAX_PASSLEN))
  119. {
  120. free (buf);
  121. free (salt);
  122. grub_util_error ("%s", _("failure to read password"));
  123. }
  124. printf ("%s", _("Reenter password: "));
  125. if (!grub_password_get (pass2, GRUB_AUTH_MAX_PASSLEN))
  126. {
  127. free (buf);
  128. free (salt);
  129. grub_util_error ("%s", _("failure to read password"));
  130. }
  131. if (strcmp (pass1, pass2) != 0)
  132. {
  133. memset (pass1, 0, sizeof (pass1));
  134. memset (pass2, 0, sizeof (pass2));
  135. free (buf);
  136. free (salt);
  137. grub_util_error ("%s", _("passwords don't match"));
  138. }
  139. memset (pass2, 0, sizeof (pass2));
  140. if (grub_get_random (salt, arguments.saltlen))
  141. {
  142. memset (pass1, 0, sizeof (pass1));
  143. free (buf);
  144. free (salt);
  145. grub_util_error ("%s", _("couldn't retrieve random data for salt"));
  146. }
  147. gcry_err = grub_crypto_pbkdf2 (GRUB_MD_SHA512,
  148. (grub_uint8_t *) pass1, strlen (pass1),
  149. salt, arguments.saltlen,
  150. arguments.count, buf, arguments.buflen);
  151. memset (pass1, 0, sizeof (pass1));
  152. if (gcry_err)
  153. {
  154. memset (buf, 0, arguments.buflen);
  155. free (buf);
  156. memset (salt, 0, arguments.saltlen);
  157. free (salt);
  158. grub_util_error (_("cryptographic error number %d"), gcry_err);
  159. }
  160. result = xmalloc (sizeof ("grub.pbkdf2.sha512.XXXXXXXXXXXXXXXXXXX.S.S")
  161. + arguments.buflen * 2 + arguments.saltlen * 2);
  162. ptr = result;
  163. memcpy (ptr, "grub.pbkdf2.sha512.", sizeof ("grub.pbkdf2.sha512.") - 1);
  164. ptr += sizeof ("grub.pbkdf2.sha512.") - 1;
  165. grub_snprintf (ptr, sizeof ("XXXXXXXXXXXXXXXXXXX"), "%d", arguments.count);
  166. ptr += strlen (ptr);
  167. *ptr++ = '.';
  168. hexify (ptr, salt, arguments.saltlen);
  169. ptr += arguments.saltlen * 2;
  170. *ptr++ = '.';
  171. hexify (ptr, buf, arguments.buflen);
  172. ptr += arguments.buflen * 2;
  173. *ptr = '\0';
  174. printf (_("PBKDF2 hash of your password is %s\n"), result);
  175. memset (buf, 0, arguments.buflen);
  176. free (buf);
  177. memset (salt, 0, arguments.saltlen);
  178. free (salt);
  179. return 0;
  180. }