Comparison

util-src/hashes.c @ 10061:5c71693c8345

Merge 0.11->trunk
author Kim Alvefur <zash@zash.se>
date Mon, 08 Jul 2019 02:44:32 +0200
parent 9970:4a43feb9ab15
child 10480:94cacf9fd0ae
comparison
equal deleted inserted replaced
10060:7a36b7ac309b 10061:5c71693c8345
24 #include "lua.h" 24 #include "lua.h"
25 #include "lauxlib.h" 25 #include "lauxlib.h"
26 #include <openssl/sha.h> 26 #include <openssl/sha.h>
27 #include <openssl/md5.h> 27 #include <openssl/md5.h>
28 #include <openssl/hmac.h> 28 #include <openssl/hmac.h>
29 #include <openssl/evp.h>
29 30
30 #if (LUA_VERSION_NUM == 501) 31 #if (LUA_VERSION_NUM == 501)
31 #define luaL_setfuncs(L, R, N) luaL_register(L, NULL, R) 32 #define luaL_setfuncs(L, R, N) luaL_register(L, NULL, R)
32 #endif 33 #endif
33 34
73 int (*Final)(unsigned char *, void *); 74 int (*Final)(unsigned char *, void *);
74 size_t digestLength; 75 size_t digestLength;
75 void *ctx, *ctxo; 76 void *ctx, *ctxo;
76 }; 77 };
77 78
78 static void hmac(struct hash_desc *desc, const char *key, size_t key_len,
79 const char *msg, size_t msg_len, unsigned char *result) {
80 union xory {
81 unsigned char bytes[64];
82 uint32_t quadbytes[16];
83 };
84
85 int i;
86 unsigned char hashedKey[64]; /* Maximum used digest length */
87 union xory k_ipad, k_opad;
88
89 if(key_len > 64) {
90 desc->Init(desc->ctx);
91 desc->Update(desc->ctx, key, key_len);
92 desc->Final(hashedKey, desc->ctx);
93 key = (const char *)hashedKey;
94 key_len = desc->digestLength;
95 }
96
97 memcpy(k_ipad.bytes, key, key_len);
98 memset(k_ipad.bytes + key_len, 0, 64 - key_len);
99 memcpy(k_opad.bytes, k_ipad.bytes, 64);
100
101 for(i = 0; i < 16; i++) {
102 k_ipad.quadbytes[i] ^= HMAC_IPAD;
103 k_opad.quadbytes[i] ^= HMAC_OPAD;
104 }
105
106 desc->Init(desc->ctx);
107 desc->Update(desc->ctx, k_ipad.bytes, 64);
108 desc->Init(desc->ctxo);
109 desc->Update(desc->ctxo, k_opad.bytes, 64);
110 desc->Update(desc->ctx, msg, msg_len);
111 desc->Final(result, desc->ctx);
112 desc->Update(desc->ctxo, result, desc->digestLength);
113 desc->Final(result, desc->ctxo);
114 }
115
116 #define MAKE_HMAC_FUNCTION(myFunc, evp, size, type) \ 79 #define MAKE_HMAC_FUNCTION(myFunc, evp, size, type) \
117 static int myFunc(lua_State *L) { \ 80 static int myFunc(lua_State *L) { \
118 unsigned char hash[size], result[2*size]; \ 81 unsigned char hash[size], result[2*size]; \
119 size_t key_len, msg_len; \ 82 size_t key_len, msg_len; \
120 unsigned int out_len; \ 83 unsigned int out_len; \
134 MAKE_HMAC_FUNCTION(Lhmac_sha1, EVP_sha1, SHA_DIGEST_LENGTH, SHA_CTX) 97 MAKE_HMAC_FUNCTION(Lhmac_sha1, EVP_sha1, SHA_DIGEST_LENGTH, SHA_CTX)
135 MAKE_HMAC_FUNCTION(Lhmac_sha256, EVP_sha256, SHA256_DIGEST_LENGTH, SHA256_CTX) 98 MAKE_HMAC_FUNCTION(Lhmac_sha256, EVP_sha256, SHA256_DIGEST_LENGTH, SHA256_CTX)
136 MAKE_HMAC_FUNCTION(Lhmac_sha512, EVP_sha512, SHA512_DIGEST_LENGTH, SHA512_CTX) 99 MAKE_HMAC_FUNCTION(Lhmac_sha512, EVP_sha512, SHA512_DIGEST_LENGTH, SHA512_CTX)
137 MAKE_HMAC_FUNCTION(Lhmac_md5, EVP_md5, MD5_DIGEST_LENGTH, MD5_CTX) 100 MAKE_HMAC_FUNCTION(Lhmac_md5, EVP_md5, MD5_DIGEST_LENGTH, MD5_CTX)
138 101
139 static int LscramHi(lua_State *L) { 102 static int Lpbkdf2_sha1(lua_State *L) {
140 union xory { 103 unsigned char out[SHA_DIGEST_LENGTH];
141 unsigned char bytes[SHA_DIGEST_LENGTH]; 104
142 uint32_t quadbytes[SHA_DIGEST_LENGTH / 4]; 105 size_t pass_len, salt_len;
143 }; 106 const char *pass = luaL_checklstring(L, 1, &pass_len);
144 int i; 107 const unsigned char *salt = (unsigned char *)luaL_checklstring(L, 2, &salt_len);
145 SHA_CTX ctx, ctxo;
146 unsigned char Ust[SHA_DIGEST_LENGTH];
147 union xory Und;
148 union xory res;
149 size_t str_len, salt_len;
150 struct hash_desc desc;
151 const char *str = luaL_checklstring(L, 1, &str_len);
152 const char *salt = luaL_checklstring(L, 2, &salt_len);
153 char *salt2;
154 const int iter = luaL_checkinteger(L, 3); 108 const int iter = luaL_checkinteger(L, 3);
155 109
156 desc.Init = (int (*)(void *))SHA1_Init; 110 if(PKCS5_PBKDF2_HMAC(pass, pass_len, salt, salt_len, iter, EVP_sha1(), SHA_DIGEST_LENGTH, out) == 0) {
157 desc.Update = (int (*)(void *, const void *, size_t))SHA1_Update; 111 return luaL_error(L, "PKCS5_PBKDF2_HMAC() failed");
158 desc.Final = (int (*)(unsigned char *, void *))SHA1_Final;
159 desc.digestLength = SHA_DIGEST_LENGTH;
160 desc.ctx = &ctx;
161 desc.ctxo = &ctxo;
162
163 salt2 = malloc(salt_len + 4);
164
165 if(salt2 == NULL) {
166 return luaL_error(L, "Out of memory in scramHi");
167 } 112 }
168 113
169 memcpy(salt2, salt, salt_len); 114 lua_pushlstring(L, (char *)out, SHA_DIGEST_LENGTH);
170 memcpy(salt2 + salt_len, "\0\0\0\1", 4);
171 hmac(&desc, str, str_len, salt2, salt_len + 4, Ust);
172 free(salt2);
173 115
174 memcpy(res.bytes, Ust, sizeof(res)); 116 return 1;
117 }
175 118
176 for(i = 1; i < iter; i++) {
177 int j;
178 hmac(&desc, str, str_len, (char *)Ust, sizeof(Ust), Und.bytes);
179 119
180 for(j = 0; j < SHA_DIGEST_LENGTH / 4; j++) { 120 static int Lpbkdf2_sha256(lua_State *L) {
181 res.quadbytes[j] ^= Und.quadbytes[j]; 121 unsigned char out[SHA256_DIGEST_LENGTH];
182 }
183 122
184 memcpy(Ust, Und.bytes, sizeof(Ust)); 123 size_t pass_len, salt_len;
124 const char *pass = luaL_checklstring(L, 1, &pass_len);
125 const unsigned char *salt = (unsigned char *)luaL_checklstring(L, 2, &salt_len);
126 const int iter = luaL_checkinteger(L, 3);
127
128 if(PKCS5_PBKDF2_HMAC(pass, pass_len, salt, salt_len, iter, EVP_sha256(), SHA256_DIGEST_LENGTH, out) == 0) {
129 return luaL_error(L, "PKCS5_PBKDF2_HMAC() failed");
185 } 130 }
186 131
187 lua_pushlstring(L, (char *)res.bytes, SHA_DIGEST_LENGTH); 132 lua_pushlstring(L, (char *)out, SHA_DIGEST_LENGTH);
188
189 return 1; 133 return 1;
190 } 134 }
191 135
192 static const luaL_Reg Reg[] = { 136 static const luaL_Reg Reg[] = {
193 { "sha1", Lsha1 }, 137 { "sha1", Lsha1 },
198 { "md5", Lmd5 }, 142 { "md5", Lmd5 },
199 { "hmac_sha1", Lhmac_sha1 }, 143 { "hmac_sha1", Lhmac_sha1 },
200 { "hmac_sha256", Lhmac_sha256 }, 144 { "hmac_sha256", Lhmac_sha256 },
201 { "hmac_sha512", Lhmac_sha512 }, 145 { "hmac_sha512", Lhmac_sha512 },
202 { "hmac_md5", Lhmac_md5 }, 146 { "hmac_md5", Lhmac_md5 },
203 { "scram_Hi_sha1", LscramHi }, 147 { "scram_Hi_sha1", Lpbkdf2_sha1 }, /* COMPAT */
148 { "pbkdf2_hmac_sha1", Lpbkdf2_sha1 },
149 { "pbkdf2_hmac_sha256", Lpbkdf2_sha256 },
204 { NULL, NULL } 150 { NULL, NULL }
205 }; 151 };
206 152
207 LUALIB_API int luaopen_util_hashes(lua_State *L) { 153 LUALIB_API int luaopen_util_hashes(lua_State *L) {
208 #if (LUA_VERSION_NUM > 501) 154 #if (LUA_VERSION_NUM > 501)
209 luaL_checkversion(L); 155 luaL_checkversion(L);
210 #endif 156 #endif
211 lua_newtable(L); 157 lua_newtable(L);
212 luaL_setfuncs(L, Reg, 0);; 158 luaL_setfuncs(L, Reg, 0);
213 lua_pushliteral(L, "-3.14"); 159 lua_pushliteral(L, "-3.14");
214 lua_setfield(L, -2, "version"); 160 lua_setfield(L, -2, "version");
215 return 1; 161 return 1;
216 } 162 }