ocpasswordgen: Hide password characters on input
**/
#include <stdio.h>
+#ifdef _WIN32
+ #include <conio.h>
+#else
+ #include <termios.h>
+ #include <unistd.h>
+#endif
#include <Library/DebugLib.h>
#include <Library/OcCryptoLib.h>
#include <UserPseudoRandom.h>
+//
+// Signal Interrupt Control-C symbol
+//
+#define CHAR_END_OF_TEXT 3
+#define CHAR_DELETE 127
+
+#ifndef _WIN32
+
+/**
+ * getch unix-compatible implementation
+ *
+ * @return integer with ch codenumber
+ */
+int
+getch (
+ void
+ )
+{
+ int Char;
+ struct termios OldTtyAttrs, NewTtyAttrs;
+
+ tcgetattr (STDIN_FILENO, &OldTtyAttrs);
+ NewTtyAttrs = OldTtyAttrs;
+ NewTtyAttrs.c_lflag = NewTtyAttrs.c_lflag & ~(ICANON|ECHO);
+ tcsetattr (STDIN_FILENO, TCSANOW, &NewTtyAttrs);
+ Char = getchar ();
+ tcsetattr (STDIN_FILENO, TCSANOW, &OldTtyAttrs);
+
+ return Char;
+}
+
+#endif
+
int
ENTRY_POINT (
void
@@ -21,14 +60,30 @@ ENTRY_POINT (
UINT8 Index;
UINT8 PasswordHash[SHA512_DIGEST_SIZE];
- DEBUG ((DEBUG_ERROR, "Please enter your password: "));
+ printf ("Please enter your password: ");
for (PasswordLen = 0; PasswordLen < OC_PASSWORD_MAX_LEN; ++PasswordLen) {
- Char = getchar ();
- if ((Char == EOF) || (Char == '\n')) {
+ fflush (stdin);
+ Char = getch ();
+
+ // Handle interrupt signal for conio
+ if (Char == CHAR_END_OF_TEXT) {
+ exit (EXIT_FAILURE);
+ }
+
+ if ((Char == EOF) || (Char == CHAR_LINEFEED) || (Char == CHAR_CARRIAGE_RETURN)) {
+ Password[PasswordLen] = '\0';
break;
}
+ if ((Char == CHAR_BACKSPACE) || (Char == CHAR_DELETE)) {
+ if (PasswordLen > 0) {
+ --PasswordLen;
+ }
+
+ continue;
+ }
+
Password[PasswordLen] = (UINT8)Char;
}
@@ -44,17 +99,17 @@ ENTRY_POINT (
PasswordHash
);
- DEBUG ((DEBUG_ERROR, "\nPasswordHash: <"));
+ printf ("\nPasswordHash: <");
for (Index = 0; Index < sizeof (PasswordHash); ++Index) {
- DEBUG ((DEBUG_ERROR, "%02x", PasswordHash[Index]));
+ printf ("%02x", PasswordHash[Index]);
}
printf (">\nPasswordSalt: <");
for (Index = 0; Index < sizeof (Salt); ++Index) {
- DEBUG ((DEBUG_ERROR, "%02x", ((UINT8 *)Salt)[Index]));
+ printf ("%02x", ((UINT8 *)Salt)[Index]);
}
- DEBUG ((DEBUG_ERROR, ">\n"));
+ printf (">\n");
SecureZeroMem (Password, sizeof (Password));
SecureZeroMem (PasswordHash, sizeof (PasswordHash));
