forked from 4a256b6b3e7t3e8b7t9q7t/libhydrogen
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpw.cpp
166 lines (130 loc) · 3.92 KB
/
pw.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "hydrogen.h"
#include <string>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <list>
#include <termios.h>
#include <unistd.h>
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#define CONTEXT "password"
#define OPSLIMIT 10000
#define MEMLIMIT 0
#define THREADS 1
#define MEMLIMIT_MAX 0
#define THREADS_MAX 1
#define OPSLIMIT_MAX 50000
#ifdef WIN32
#include <windows.h>
#else
#include <termios.h>
#include <unistd.h>
#endif
/* Disable echo function from: https://stackoverflow.com/questions/1413445/reading-a-password-from-stdcin */
void set_echo(bool enable = true)
{
#ifdef WIN32
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
DWORD mode;
GetConsoleMode(hStdin, &mode);
if( !enable )
mode &= ~ENABLE_ECHO_INPUT;
else
mode |= ENABLE_ECHO_INPUT;
SetConsoleMode(hStdin, mode );
#else
struct termios tty;
tcgetattr(STDIN_FILENO, &tty);
if( !enable )
tty.c_lflag &= ~ECHO;
else
tty.c_lflag |= ECHO;
(void) tcsetattr(STDIN_FILENO, TCSANOW, &tty);
#endif
}
char *userinput(FILE* fp, size_t size) {
char *in; int buf;
size_t len = 0;
try {
in = (char*)(realloc(NULL, sizeof(char) * size));
if(!in)
return in;
while (EOF != (buf = fgetc(fp)) && buf != '\n') {
in[len++] = buf;
if (len == size) {
if (len >= 65) {
throw len;
break;
} else {
in = (char*)(realloc(in, sizeof(char) * (size += 1)));
if(!in)
return in;
}
}
}
in[len++]='\0';
return (char*)(realloc(in, sizeof(char)*len));
} catch(size_t &len) {
printf("Error! Password cannot be longer than 64 characters. Please try again. \n");
return NULL;
}
}
int main()
{
hydro_init();
void hydro_memzero(void *pnt, size_t len);
uint8_t new_master_key[hydro_pwhash_MASTERKEYBYTES];
hydro_pwhash_keygen(new_master_key);
uint8_t h[64];
uint8_t static_key[128];
char h_hex[129]; // byte for each char + 1 null term byte
printf("Enter a password for key derivation: \t");
set_echo(false);
char *in = userinput(stdin, sizeof(stdin));
if (in == NULL) {
hydro_memzero((void*)(stdin), sizeof(stdin));
return -1;
}
printf("\n");
const char *input = in;
set_echo(true);
memset(new_master_key, 'x', sizeof new_master_key);
hydro_pwhash_deterministic(h, sizeof h, input, sizeof (input - 1), CONTEXT, new_master_key, OPSLIMIT, 0, 1);
hydro_bin2hex(h_hex, sizeof h_hex, h, sizeof h);
uint8_t derived_key[32];
char de_hex[65];
hydro_pwhash_deterministic(derived_key, sizeof derived_key, input, sizeof (input-1),
CONTEXT, new_master_key, OPSLIMIT, MEMLIMIT, THREADS);
uint8_t stored[hydro_pwhash_STOREDBYTES];
hydro_pwhash_create(stored, h_hex, sizeof h_hex, new_master_key,
OPSLIMIT, MEMLIMIT, THREADS);
hydro_bin2hex(h_hex, sizeof h_hex, h, sizeof h);
hydro_bin2hex(de_hex, sizeof de_hex, derived_key, sizeof derived_key);
printf("Stored Representation: \t%s\n", h_hex);
printf("Derived key: \t%s\n", de_hex);
hydro_memzero((void*)(input), sizeof(input));
hydro_memzero((void*)(in), sizeof(in));
printf("Please re-type your password to verify: \t"); // working
set_echo(false);
uint8_t htwo[64];
char htwo_hex[129]; // byte for each char + 1 null term byte
char *intwo = userinput(stdin, sizeof(stdin));
if (intwo == NULL)
return -1;
printf("\n");
const char *inputtwo = intwo;
set_echo(true);
hydro_pwhash_deterministic(htwo, sizeof htwo, inputtwo, sizeof (inputtwo - 1), CONTEXT, new_master_key, OPSLIMIT, 0, 1);
hydro_bin2hex(htwo_hex, sizeof htwo_hex, htwo, sizeof htwo);
if (hydro_pwhash_verify(stored, htwo_hex, sizeof htwo_hex, new_master_key,
OPSLIMIT_MAX, MEMLIMIT_MAX, THREADS_MAX) != 0) {
printf("Incorrect password. \n");
}
else {
printf("Verification Passed. \n");
}
return 0;
}