Repository navigation
Expand file tree
/
Copy pathquickjs-bcrypt.c
More file actions
154 lines (120 loc) · 4.06 KB
/
Copy pathquickjs-bcrypt.c
File metadata and controls
154 lines (120 loc) · 4.06 KB
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
#include "libbcrypt/bcrypt.h"
#include "buffer-utils.h"
#include <quickjs.h>
static const unsigned int BCRYPT_SALTSIZE = 29;
enum {
BCRYPT_GENSALT,
BCRYPT_CHECKPW,
BCRYPT_HASHPW,
};
static JSValue
js_bcrypt_function(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst argv[], int magic) {
JSValue ret;
InputBuffer input = js_input_chars(ctx, argv[0]);
switch(magic) {
case BCRYPT_GENSALT: {
uint32_t wf = 12;
if(JS_IsNumber(argv[0])) {
JS_ToUint32(ctx, &wf, argv[0]);
--argc;
++argv;
}
if(argc > 0) {
InputBuffer salt = js_input_buffer(ctx, argv[0]);
if(salt.size < BCRYPT_HASHSIZE)
return JS_ThrowInternalError(ctx, "supplied buffer size (%lu) < %d", (unsigned long)salt.size, BCRYPT_HASHSIZE);
ret = JS_NewInt32(ctx, bcrypt_gensalt(wf, (void*)salt.data));
inputbuffer_free(&salt, ctx);
} else {
char s[BCRYPT_HASHSIZE + 1];
memset(s, 0, sizeof(s));
if(bcrypt_gensalt(wf, s))
ret = JS_ThrowInternalError(ctx, "bcrypt_gensalt() failed");
else
ret = JS_NewStringLen(ctx, s, strlen(s));
}
break;
}
case BCRYPT_HASHPW: {
const char* pw;
InputBuffer salt = {0};
char tmp[BCRYPT_HASHSIZE], out[BCRYPT_HASHSIZE], *s;
memset(tmp, 0, sizeof(tmp));
memset(out, 0, sizeof(out));
if(argc > 1)
salt = js_input_chars(ctx, argv[1]);
if(!salt.size) {
int32_t workfactor = 12;
if(argc > 1)
JS_ToInt32(ctx, &workfactor, argv[1]);
bcrypt_gensalt(workfactor, s = tmp);
} else if(salt.size < BCRYPT_SALTSIZE) {
JS_ThrowInternalError(ctx, "supplied salt size (%lu) < %d", (unsigned long)salt.size, BCRYPT_SALTSIZE);
inputbuffer_free(&salt, ctx);
return JS_EXCEPTION;
} else if(salt.size >= BCRYPT_SALTSIZE) {
s = (void*)salt.data;
}
pw = JS_ToCString(ctx, argv[0]);
if(bcrypt_hashpw(pw, s, out))
ret = JS_ThrowInternalError(ctx, "bcrypt_hashpw() failed");
else
ret = JS_NewStringLen(ctx, out, strlen(out));
inputbuffer_free(&salt, ctx);
JS_FreeCString(ctx, pw);
break;
}
case BCRYPT_CHECKPW: {
InputBuffer buf = js_input_chars(ctx, argv[1]);
char x[BCRYPT_HASHSIZE];
if(!buf.size) {
inputbuffer_free(&buf, ctx);
return JS_ThrowInternalError(ctx, "supplied buffer size 0");
}
if(buf.size < (BCRYPT_HASHSIZE - 4)) {
inputbuffer_free(&buf, ctx);
return JS_ThrowInternalError(ctx, "supplied buffer size %lu < %u", (unsigned long)buf.size, BCRYPT_HASHSIZE - 4);
}
memset(x, 0, sizeof(x));
memcpy(x, buf.data, MIN_NUM(buf.size, BCRYPT_HASHSIZE));
inputbuffer_free(&buf, ctx);
const char* pw = JS_ToCString(ctx, argv[0]);
int result = bcrypt_checkpw(pw, x);
if(result == -1)
ret = JS_ThrowInternalError(ctx, "bcrypt_checkpw() returned -1");
else
ret = JS_NewBool(ctx, !result);
JS_FreeCString(ctx, pw);
break;
}
}
return ret;
}
static const JSCFunctionListEntry js_bcrypt_functions[] = {
JS_CFUNC_MAGIC_DEF("genSalt", 0, js_bcrypt_function, BCRYPT_GENSALT),
JS_CFUNC_MAGIC_DEF("hash", 1, js_bcrypt_function, BCRYPT_HASHPW),
JS_CFUNC_MAGIC_DEF("compare", 2, js_bcrypt_function, BCRYPT_CHECKPW),
JS_PROP_INT32_DEF("HASHSIZE", BCRYPT_HASHSIZE, JS_PROP_ENUMERABLE),
JS_PROP_INT32_DEF("SALTSIZE", BCRYPT_SALTSIZE, JS_PROP_ENUMERABLE),
};
static int
js_bcrypt_init(JSContext* ctx, JSModuleDef* m) {
if(m)
JS_SetModuleExportList(ctx, m, js_bcrypt_functions, countof(js_bcrypt_functions));
return 0;
}
#ifdef JS_SHARED_LIBRARY
#define JS_INIT_MODULE js_init_module
#else
#define JS_INIT_MODULE js_init_module_bcrypt
#endif
VISIBLE JSModuleDef*
JS_INIT_MODULE(JSContext* ctx, const char* module_name) {
JSModuleDef* m;
if((m = JS_NewCModule(ctx, module_name, js_bcrypt_init)))
JS_AddModuleExportList(ctx, m, js_bcrypt_functions, countof(js_bcrypt_functions));
return m;
}
/**
* @}
*/