OpenCore  1.0.4
OpenCore Bootloader
1.0.4
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
ocpasswordgen.c
Go to the documentation of this file.
1
6#include <stdio.h>
7#ifdef _WIN32
8 #include <conio.h>
9#else
10 #include <termios.h>
11 #include <unistd.h>
12#endif
13
14#include <Library/DebugLib.h>
15#include <Library/OcCryptoLib.h>
16#include <UserPseudoRandom.h>
17
18//
19// Signal Interrupt Control-C symbol
20//
21#define CHAR_END_OF_TEXT 3
22#define CHAR_DELETE 127
23
24#ifndef _WIN32
25
31int
33 void
34 )
35{
36 int Char;
37 struct termios OldTtyAttrs, NewTtyAttrs;
38
39 tcgetattr (STDIN_FILENO, &OldTtyAttrs);
40 NewTtyAttrs = OldTtyAttrs;
41 NewTtyAttrs.c_lflag = NewTtyAttrs.c_lflag & ~(ICANON|ECHO);
42 tcsetattr (STDIN_FILENO, TCSANOW, &NewTtyAttrs);
43 Char = getchar ();
44 tcsetattr (STDIN_FILENO, TCSANOW, &OldTtyAttrs);
45
46 return Char;
47}
48
49#endif
50
51int
53 void
54 )
55{
56 CHAR8 Char;
57 UINT8 Password[OC_PASSWORD_MAX_LEN];
58 UINT8 PasswordLen;
59 UINT32 Salt[4];
60 UINT8 Index;
61 UINT8 PasswordHash[SHA512_DIGEST_SIZE];
62
63 printf ("Please enter your password: ");
64
65 for (PasswordLen = 0; PasswordLen < OC_PASSWORD_MAX_LEN; ++PasswordLen) {
66 fflush (stdin);
67 Char = getch ();
68
69 // Handle interrupt signal for conio
70 if (Char == CHAR_END_OF_TEXT) {
71 exit (EXIT_FAILURE);
72 }
73
74 if ((Char == EOF) || (Char == CHAR_LINEFEED) || (Char == CHAR_CARRIAGE_RETURN)) {
75 Password[PasswordLen] = '\0';
76 break;
77 }
78
79 if ((Char == CHAR_BACKSPACE) || (Char == CHAR_DELETE)) {
80 if (PasswordLen > 0) {
81 --PasswordLen;
82 }
83
84 continue;
85 }
86
87 Password[PasswordLen] = (UINT8)Char;
88 }
89
90 for (Index = 0; Index < ARRAY_SIZE (Salt); ++Index) {
91 Salt[Index] = pseudo_random ();
92 }
93
95 Password,
96 PasswordLen,
97 (UINT8 *)Salt,
98 sizeof (Salt),
99 PasswordHash
100 );
101
102 printf ("\nPasswordHash: <");
103 for (Index = 0; Index < sizeof (PasswordHash); ++Index) {
104 printf ("%02x", PasswordHash[Index]);
105 }
106
107 printf (">\nPasswordSalt: <");
108 for (Index = 0; Index < sizeof (Salt); ++Index) {
109 printf ("%02x", ((UINT8 *)Salt)[Index]);
110 }
111
112 printf (">\n");
113
114 SecureZeroMem (Password, sizeof (Password));
115 SecureZeroMem (PasswordHash, sizeof (PasswordHash));
116 SecureZeroMem (&PasswordLen, sizeof (PasswordLen));
117
118 return 0;
119}
#define ARRAY_SIZE(Array)
Definition AppleMacEfi.h:34
VOID OcHashPasswordSha512(IN CONST UINT8 *Password, IN UINT32 PasswordSize, IN CONST UINT8 *Salt, IN UINT32 SaltSize, OUT UINT8 *Hash)
#define OC_PASSWORD_MAX_LEN
Definition OcCryptoLib.h:89
#define SHA512_DIGEST_SIZE
Definition OcCryptoLib.h:47
VOID * SecureZeroMem(OUT VOID *Buffer, IN UINTN Length)
Definition SecureMem.c:73
uint32_t pseudo_random(void)
#define CHAR_END_OF_TEXT
#define CHAR_DELETE
int ENTRY_POINT(void)
int getch(void)