-
Notifications
You must be signed in to change notification settings - Fork 0
/
lpdkdf2.c
132 lines (102 loc) · 3.25 KB
/
lpdkdf2.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
//
// Created by alex on 25/07/2015.
//
#include <lauxlib.h>
#include <stdint.h>
#include <base64encode.h>
#include "lpdkdf2.h"
#include "fastpbkdf2/fastpbkdf2.h"
#include <openssl/rand.h>
#include <stdio.h>
#include <time.h>
// max base64 size result supported
#define BUFF_SIZE 1024 * 32
// convert bytes to hex
static void byteHex(char buff[], char key[], int size) {
srand(time(NULL));
char *fmt[] = {"%02x", "%02X"};
for (int i = 0; i < size; i++) {
sprintf(buff + 2 * i, fmt[rand() % 2], key[i]);
}
}
// random salt generator
void rand_salt(lua_State *L) {
lua_Object obj = lua_getparam(L, 1);
int num = (int) lua_getnumber(L, obj);
num = num > 0 ? num : 32;
char key[num];
if (RAND_bytes((unsigned char *) key, num)) {
char buff[num * 2 + 1];
byteHex(buff, key, num);
lua_pushlstring(L, &buff[0], num);
} else {
lua_pushnil(L); // error
}
}
/*
* HMAC-SHA1
* params: password (str), salt (str), iterations (int)
*/
void pbkdf2_hmac_sha1(lua_State *L) {
char *pwd = luaL_check_string(L, 1);
char *salt = luaL_check_string(L, 2);
uint32_t iterations = (unsigned) luaL_check_int(L, 3);
size_t byteSize = 20; // sha1-bit (20-byte)
char encoded[byteSize];
fastpbkdf2_hmac_sha1(
(unsigned char *) pwd, strlen(pwd),
(unsigned char *) salt, strlen(salt),
iterations,
(unsigned char *) encoded, byteSize);
char out[BUFF_SIZE];
int length = base64_encode((unsigned char *) encoded, byteSize, out);
lua_pushlstring(L, &out[0], length);
}
/*
* HMAC-SHA256
* params: password (str), salt (str), iterations (int)
*/
void pbkdf2_hmac_sha256(lua_State *L) {
char *pwd = luaL_check_string(L, 1);
char *salt = luaL_check_string(L, 2);
uint32_t iterations = (unsigned) luaL_check_int(L, 3);
size_t byteSize = 32; // 256-bit (32-byte)
char encoded[byteSize];
fastpbkdf2_hmac_sha256(
(unsigned char *) pwd, strlen(pwd),
(unsigned char *) salt, strlen(salt),
iterations,
(unsigned char *) encoded, byteSize);
char out[BUFF_SIZE];
int length = base64_encode((unsigned char *) encoded, byteSize, out);
lua_pushlstring(L, &out[0], length);
}
/*
* HMAC-SHA512
* params: password (str), salt (str), iterations (int)
*/
void pbkdf2_hmac_sha512(lua_State *L) {
char *pwd = luaL_check_string(L, 1);
char *salt = luaL_check_string(L, 2);
uint32_t iterations = (unsigned) luaL_check_int(L, 3);
size_t byteSize = 64; // 512-bit (64-byte)
char encoded[byteSize];
fastpbkdf2_hmac_sha512(
(unsigned char *) pwd, strlen(pwd),
(unsigned char *) salt, strlen(salt),
iterations,
(unsigned char *) encoded, byteSize);
char out[BUFF_SIZE];
int length = base64_encode((unsigned char *) encoded, byteSize, out);
lua_pushlstring(L, &out[0], length);
}
static struct luaL_reg lpbkdf2[] = {
{"pbkdf2_hmac_sha1", pbkdf2_hmac_sha1},
{"pbkdf2_hmac_sha256", pbkdf2_hmac_sha256},
{"pbkdf2_hmac_sha512", pbkdf2_hmac_sha512},
{"rand_salt", rand_salt}
};
int LUA_LIBRARY lua_lpbkdf2open(lua_State *L) {
luaL_openlib(L, lpbkdf2, (sizeof(lpbkdf2)/sizeof(lpbkdf2[0])));
return 0;
}