-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnpm-core.c
201 lines (165 loc) · 4.04 KB
/
npm-core.c
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#define _DEFAULT_SOURCE
#include <errno.h>
#include <string.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <unistd.h>
#include "common.h"
#include "config.h"
#include "monocypher.h"
#include "util.h"
char *argv0;
uint8_t plain[PASSWORD_MAX_LEN + 1];
uint8_t cipher[PASSWORD_MAX_LEN + 1];
uint8_t master[PASSWORD_MAX_LEN + 1];
uint8_t key[KEY_LEN];
uint8_t mac[MAC_LEN];
uint8_t data[SALT_LEN + NONCE_LEN];
uint8_t *salt = data;
uint8_t *nonce = data + SALT_LEN;
char *work;
void
clear()
{
explicit_bzero(plain, sizeof(plain));
explicit_bzero(cipher, sizeof(cipher));
explicit_bzero(master, sizeof(master));
explicit_bzero(key, sizeof(key));
explicit_bzero(nonce, sizeof(nonce));
explicit_bzero(data, sizeof(data));
}
void
error(const char *s)
{
fprintf(stderr, "%s: %s\n", argv0, s);
}
void
usage()
{
fprintf(stderr, "%s: [-d file] | [-e]\n", argv0);
exit(1);
}
int
main(int argc, char *argv[])
{
size_t len;
FILE *file = NULL;
argv0 = argv[0];
if (argc == 1)
usage();
if (strcmp(argv[1], "-e") && strcmp(argv[1], "-d"))
usage();
if (!strcmp(argv[1], "-e") && argc != 2) {
error("option -e does not take an argument");
usage();
}
if (!strcmp(argv[1], "-d") && argc != 3) {
error("option -d takes exactly 1 argument");
usage();
}
/* we want to prevent secret data from being swapped to disk */
if (mlock(plain, sizeof(plain)) < 0) {
perror("mlock failed");
goto fail;
}
if (mlock(master, sizeof(master)) < 0) {
perror("mlock failed");
goto fail;
}
if (mlock(key, sizeof(key)) < 0) {
perror("mlock failed");
goto fail;
}
if (strcmp(argv[1], "-e") == 0) {
if (getentropy(salt, SALT_LEN) < 0) {
error("failed to generate salt");
goto fail;
}
switch (len = get_password(stdin, master)) {
case -1:
error("encountered EOF when reading master password");
goto fail;
case -2:
error("entered master password is too long");
goto fail;
}
work = malloc(M_COST * 1024);
if (!work) {
error("failed to allocate work buffer for argon2");
goto fail;
}
crypto_argon2i(key, KEY_LEN, work, M_COST, T_COST, master, len, salt, SALT_LEN);
if (getentropy(nonce, NONCE_LEN) < 0) {
error("failed to generate nonce");
goto fail;
}
switch (len = get_password(stdin, plain)) {
case -1:
error("encountered EOF when reading password");
goto fail;
case -2:
error("entered password is too long");
goto fail;
}
crypto_lock_aead(mac, cipher, key, nonce, data, sizeof(data), plain, PASSWORD_MAX_LEN);
fwrite(salt, sizeof(char), SALT_LEN, stdout);
fwrite(nonce, sizeof(char), NONCE_LEN, stdout);
fwrite(mac, sizeof(char), MAC_LEN, stdout);
fwrite(cipher, sizeof(char), PASSWORD_MAX_LEN, stdout);
} else if (strcmp(argv[1], "-d") == 0) {
file = fopen(argv[2], "r");
if (file == NULL) {
error("failed to open file");
goto fail;
}
len = fread(salt, sizeof(char), SALT_LEN, file);
if (len < SALT_LEN) {
error("failed to read salt");
goto fail;
}
len = fread(nonce, sizeof(char), NONCE_LEN, file);
if (len < NONCE_LEN) {
error("failed to read nonce");
goto fail;
}
len = fread(mac, sizeof(char), MAC_LEN, file);
if (len < MAC_LEN) {
error("failed to read MAC");
goto fail;
}
len = fread(cipher, sizeof(char), PASSWORD_MAX_LEN, file);
if (len < PASSWORD_MAX_LEN) {
error("failed to read encrypted data");
goto fail;
}
switch (len = get_password(stdin, master)) {
case -1:
error("encountered EOF when reading master password");
goto fail;
case -2:
error("entered master password is too long");
goto fail;
}
work = malloc(M_COST * 1024);
if (!work) {
error("failed to allocate argon2 work buffer");
goto fail;
}
crypto_argon2i(key, KEY_LEN, work, M_COST, T_COST, master, len, salt, SALT_LEN);
if (crypto_unlock_aead(plain, key, nonce, mac, data, sizeof(data), cipher, PASSWORD_MAX_LEN) != 0) {
error("incorrect master password");
goto fail;
}
puts((char *)plain);
fclose(file);
}
clear();
return 0;
fail:
if (file)
fclose(file);
clear();
return 1;
}