Software /
code /
prosody
Annotate
util-src/hashes.c @ 10509:0aa0fd1adbb0
mod_admin_telnet: Use existing host comparison when comparing JIDs
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 15 Dec 2019 21:44:58 +0100 |
parent | 10480:94cacf9fd0ae |
child | 10748:93293891709b |
rev | line source |
---|---|
2923
b7049746bd29
Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents:
896
diff
changeset
|
1 /* Prosody IM |
b7049746bd29
Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents:
896
diff
changeset
|
2 -- Copyright (C) 2009-2010 Matthew Wild |
b7049746bd29
Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents:
896
diff
changeset
|
3 -- Copyright (C) 2009-2010 Waqas Hussain |
6615
8e4572a642cb
util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents:
6413
diff
changeset
|
4 -- |
766
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
5 -- This project is MIT/X11 licensed. Please see the |
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
6 -- COPYING file in the source package for more information. |
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
7 -- |
520 | 8 */ |
9 | |
766
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
10 /* |
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
11 * hashes.c |
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
12 * Lua library for sha1, sha256 and md5 hashes |
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
13 */ |
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
14 |
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
15 #include <string.h> |
5538
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
16 #include <stdlib.h> |
5576
7656b9f06bb5
util.hashes: inttypes.h not available with MS Windows SDK, use MS specific __int32 instead.
Waqas Hussain <waqas20@gmail.com>
parents:
5538
diff
changeset
|
17 |
7656b9f06bb5
util.hashes: inttypes.h not available with MS Windows SDK, use MS specific __int32 instead.
Waqas Hussain <waqas20@gmail.com>
parents:
5538
diff
changeset
|
18 #ifdef _MSC_VER |
7656b9f06bb5
util.hashes: inttypes.h not available with MS Windows SDK, use MS specific __int32 instead.
Waqas Hussain <waqas20@gmail.com>
parents:
5538
diff
changeset
|
19 typedef unsigned __int32 uint32_t; |
7656b9f06bb5
util.hashes: inttypes.h not available with MS Windows SDK, use MS specific __int32 instead.
Waqas Hussain <waqas20@gmail.com>
parents:
5538
diff
changeset
|
20 #else |
5538
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
21 #include <inttypes.h> |
5576
7656b9f06bb5
util.hashes: inttypes.h not available with MS Windows SDK, use MS specific __int32 instead.
Waqas Hussain <waqas20@gmail.com>
parents:
5538
diff
changeset
|
22 #endif |
766
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
23 |
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
24 #include "lua.h" |
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
25 #include "lauxlib.h" |
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
26 #include <openssl/sha.h> |
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
27 #include <openssl/md5.h> |
9962
29bc3dff3419
util.hashes: Use HMAC function provided by OpenSSL (fixes #1345)
Kim Alvefur <zash@zash.se>
parents:
7889
diff
changeset
|
28 #include <openssl/hmac.h> |
9965
d8e645b4d195
util.hashes: Use PBKDF2 from libcrypto
Kim Alvefur <zash@zash.se>
parents:
9963
diff
changeset
|
29 #include <openssl/evp.h> |
766
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
30 |
6789
6b180e77c97a
util-src/*.c: Invert Lua 5.2 compat to be 5.2+ by default and a macro to support 5.1
Kim Alvefur <zash@zash.se>
parents:
6620
diff
changeset
|
31 #if (LUA_VERSION_NUM == 501) |
6b180e77c97a
util-src/*.c: Invert Lua 5.2 compat to be 5.2+ by default and a macro to support 5.1
Kim Alvefur <zash@zash.se>
parents:
6620
diff
changeset
|
32 #define luaL_setfuncs(L, R, N) luaL_register(L, NULL, R) |
6413
a552f4170aed
util-src/*.c: Add macro for compiling with Lua 5.2
Kim Alvefur <zash@zash.se>
parents:
6412
diff
changeset
|
33 #endif |
a552f4170aed
util-src/*.c: Add macro for compiling with Lua 5.2
Kim Alvefur <zash@zash.se>
parents:
6412
diff
changeset
|
34 |
5537
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
35 #define HMAC_IPAD 0x36363636 |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
36 #define HMAC_OPAD 0x5c5c5c5c |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
37 |
10480
94cacf9fd0ae
util.*.c: Add static qualifiers everywhere
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9970
diff
changeset
|
38 static const char *hex_tab = "0123456789abcdef"; |
94cacf9fd0ae
util.*.c: Add static qualifiers everywhere
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9970
diff
changeset
|
39 static void toHex(const unsigned char *in, int length, unsigned char *out) { |
766
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
40 int i; |
6615
8e4572a642cb
util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents:
6413
diff
changeset
|
41 |
8e4572a642cb
util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents:
6413
diff
changeset
|
42 for(i = 0; i < length; i++) { |
8e4572a642cb
util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents:
6413
diff
changeset
|
43 out[i * 2] = hex_tab[(in[i] >> 4) & 0xF]; |
8e4572a642cb
util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents:
6413
diff
changeset
|
44 out[i * 2 + 1] = hex_tab[(in[i]) & 0xF]; |
766
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
45 } |
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
46 } |
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
47 |
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
48 #define MAKE_HASH_FUNCTION(myFunc, func, size) \ |
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
49 static int myFunc(lua_State *L) { \ |
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
50 size_t len; \ |
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
51 const char *s = luaL_checklstring(L, 1, &len); \ |
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
52 int hex_out = lua_toboolean(L, 2); \ |
5537
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
53 unsigned char hash[size], result[size*2]; \ |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
54 func((const unsigned char*)s, len, hash); \ |
766
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
55 if (hex_out) { \ |
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
56 toHex(hash, size, result); \ |
5537
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
57 lua_pushlstring(L, (char*)result, size*2); \ |
766
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
58 } else { \ |
5537
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
59 lua_pushlstring(L, (char*)hash, size);\ |
766
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
60 } \ |
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
61 return 1; \ |
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
62 } |
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
63 |
4828
f3e841521436
util.hashes: Use defined hash function output lengths.
Kim Alvefur <zash@zash.se>
parents:
2923
diff
changeset
|
64 MAKE_HASH_FUNCTION(Lsha1, SHA1, SHA_DIGEST_LENGTH) |
4829
0ebc636faa59
util.hashes: Add sha224, sha384, sha512
Kim Alvefur <zash@zash.se>
parents:
4828
diff
changeset
|
65 MAKE_HASH_FUNCTION(Lsha224, SHA224, SHA224_DIGEST_LENGTH) |
4828
f3e841521436
util.hashes: Use defined hash function output lengths.
Kim Alvefur <zash@zash.se>
parents:
2923
diff
changeset
|
66 MAKE_HASH_FUNCTION(Lsha256, SHA256, SHA256_DIGEST_LENGTH) |
4829
0ebc636faa59
util.hashes: Add sha224, sha384, sha512
Kim Alvefur <zash@zash.se>
parents:
4828
diff
changeset
|
67 MAKE_HASH_FUNCTION(Lsha384, SHA384, SHA384_DIGEST_LENGTH) |
0ebc636faa59
util.hashes: Add sha224, sha384, sha512
Kim Alvefur <zash@zash.se>
parents:
4828
diff
changeset
|
68 MAKE_HASH_FUNCTION(Lsha512, SHA512, SHA512_DIGEST_LENGTH) |
4828
f3e841521436
util.hashes: Use defined hash function output lengths.
Kim Alvefur <zash@zash.se>
parents:
2923
diff
changeset
|
69 MAKE_HASH_FUNCTION(Lmd5, MD5, MD5_DIGEST_LENGTH) |
766
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
70 |
5537
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
71 struct hash_desc { |
7889
b8d694646597
util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents:
7835
diff
changeset
|
72 int (*Init)(void *); |
b8d694646597
util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents:
7835
diff
changeset
|
73 int (*Update)(void *, const void *, size_t); |
b8d694646597
util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents:
7835
diff
changeset
|
74 int (*Final)(unsigned char *, void *); |
5537
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
75 size_t digestLength; |
7889
b8d694646597
util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents:
7835
diff
changeset
|
76 void *ctx, *ctxo; |
5537
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
77 }; |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
78 |
9962
29bc3dff3419
util.hashes: Use HMAC function provided by OpenSSL (fixes #1345)
Kim Alvefur <zash@zash.se>
parents:
7889
diff
changeset
|
79 #define MAKE_HMAC_FUNCTION(myFunc, evp, size, type) \ |
5537
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
80 static int myFunc(lua_State *L) { \ |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
81 unsigned char hash[size], result[2*size]; \ |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
82 size_t key_len, msg_len; \ |
9962
29bc3dff3419
util.hashes: Use HMAC function provided by OpenSSL (fixes #1345)
Kim Alvefur <zash@zash.se>
parents:
7889
diff
changeset
|
83 unsigned int out_len; \ |
5537
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
84 const char *key = luaL_checklstring(L, 1, &key_len); \ |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
85 const char *msg = luaL_checklstring(L, 2, &msg_len); \ |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
86 const int hex_out = lua_toboolean(L, 3); \ |
9962
29bc3dff3419
util.hashes: Use HMAC function provided by OpenSSL (fixes #1345)
Kim Alvefur <zash@zash.se>
parents:
7889
diff
changeset
|
87 HMAC(evp(), key, key_len, (const unsigned char*)msg, msg_len, (unsigned char*)hash, &out_len); \ |
5537
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
88 if (hex_out) { \ |
9962
29bc3dff3419
util.hashes: Use HMAC function provided by OpenSSL (fixes #1345)
Kim Alvefur <zash@zash.se>
parents:
7889
diff
changeset
|
89 toHex(hash, out_len, result); \ |
29bc3dff3419
util.hashes: Use HMAC function provided by OpenSSL (fixes #1345)
Kim Alvefur <zash@zash.se>
parents:
7889
diff
changeset
|
90 lua_pushlstring(L, (char*)result, out_len*2); \ |
5537
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
91 } else { \ |
9962
29bc3dff3419
util.hashes: Use HMAC function provided by OpenSSL (fixes #1345)
Kim Alvefur <zash@zash.se>
parents:
7889
diff
changeset
|
92 lua_pushlstring(L, (char*)hash, out_len); \ |
5537
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
93 } \ |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
94 return 1; \ |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
95 } |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
96 |
9962
29bc3dff3419
util.hashes: Use HMAC function provided by OpenSSL (fixes #1345)
Kim Alvefur <zash@zash.se>
parents:
7889
diff
changeset
|
97 MAKE_HMAC_FUNCTION(Lhmac_sha1, EVP_sha1, SHA_DIGEST_LENGTH, SHA_CTX) |
29bc3dff3419
util.hashes: Use HMAC function provided by OpenSSL (fixes #1345)
Kim Alvefur <zash@zash.se>
parents:
7889
diff
changeset
|
98 MAKE_HMAC_FUNCTION(Lhmac_sha256, EVP_sha256, SHA256_DIGEST_LENGTH, SHA256_CTX) |
29bc3dff3419
util.hashes: Use HMAC function provided by OpenSSL (fixes #1345)
Kim Alvefur <zash@zash.se>
parents:
7889
diff
changeset
|
99 MAKE_HMAC_FUNCTION(Lhmac_sha512, EVP_sha512, SHA512_DIGEST_LENGTH, SHA512_CTX) |
29bc3dff3419
util.hashes: Use HMAC function provided by OpenSSL (fixes #1345)
Kim Alvefur <zash@zash.se>
parents:
7889
diff
changeset
|
100 MAKE_HMAC_FUNCTION(Lhmac_md5, EVP_md5, MD5_DIGEST_LENGTH, MD5_CTX) |
5537
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
101 |
9967
dc9bb31cbffe
util.hashes: Rename PBKDF2 function
Kim Alvefur <zash@zash.se>
parents:
9966
diff
changeset
|
102 static int Lpbkdf2_sha1(lua_State *L) { |
9970
4a43feb9ab15
Backed out changeset 61bc5c52c941
Kim Alvefur <zash@zash.se>
parents:
9969
diff
changeset
|
103 unsigned char out[SHA_DIGEST_LENGTH]; |
4a43feb9ab15
Backed out changeset 61bc5c52c941
Kim Alvefur <zash@zash.se>
parents:
9969
diff
changeset
|
104 |
9965
d8e645b4d195
util.hashes: Use PBKDF2 from libcrypto
Kim Alvefur <zash@zash.se>
parents:
9963
diff
changeset
|
105 size_t pass_len, salt_len; |
d8e645b4d195
util.hashes: Use PBKDF2 from libcrypto
Kim Alvefur <zash@zash.se>
parents:
9963
diff
changeset
|
106 const char *pass = luaL_checklstring(L, 1, &pass_len); |
d8e645b4d195
util.hashes: Use PBKDF2 from libcrypto
Kim Alvefur <zash@zash.se>
parents:
9963
diff
changeset
|
107 const unsigned char *salt = (unsigned char *)luaL_checklstring(L, 2, &salt_len); |
5538
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
108 const int iter = luaL_checkinteger(L, 3); |
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
109 |
9970
4a43feb9ab15
Backed out changeset 61bc5c52c941
Kim Alvefur <zash@zash.se>
parents:
9969
diff
changeset
|
110 if(PKCS5_PBKDF2_HMAC(pass, pass_len, salt, salt_len, iter, EVP_sha1(), SHA_DIGEST_LENGTH, out) == 0) { |
9965
d8e645b4d195
util.hashes: Use PBKDF2 from libcrypto
Kim Alvefur <zash@zash.se>
parents:
9963
diff
changeset
|
111 return luaL_error(L, "PKCS5_PBKDF2_HMAC() failed"); |
6615
8e4572a642cb
util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents:
6413
diff
changeset
|
112 } |
8e4572a642cb
util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents:
6413
diff
changeset
|
113 |
9970
4a43feb9ab15
Backed out changeset 61bc5c52c941
Kim Alvefur <zash@zash.se>
parents:
9969
diff
changeset
|
114 lua_pushlstring(L, (char *)out, SHA_DIGEST_LENGTH); |
4a43feb9ab15
Backed out changeset 61bc5c52c941
Kim Alvefur <zash@zash.se>
parents:
9969
diff
changeset
|
115 |
5538
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
116 return 1; |
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
117 } |
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
118 |
9968
d536796a305f
util.hashes: Add PBKDF2-HMAC-SHA256
Kim Alvefur <zash@zash.se>
parents:
9967
diff
changeset
|
119 |
d536796a305f
util.hashes: Add PBKDF2-HMAC-SHA256
Kim Alvefur <zash@zash.se>
parents:
9967
diff
changeset
|
120 static int Lpbkdf2_sha256(lua_State *L) { |
9970
4a43feb9ab15
Backed out changeset 61bc5c52c941
Kim Alvefur <zash@zash.se>
parents:
9969
diff
changeset
|
121 unsigned char out[SHA256_DIGEST_LENGTH]; |
4a43feb9ab15
Backed out changeset 61bc5c52c941
Kim Alvefur <zash@zash.se>
parents:
9969
diff
changeset
|
122 |
9968
d536796a305f
util.hashes: Add PBKDF2-HMAC-SHA256
Kim Alvefur <zash@zash.se>
parents:
9967
diff
changeset
|
123 size_t pass_len, salt_len; |
d536796a305f
util.hashes: Add PBKDF2-HMAC-SHA256
Kim Alvefur <zash@zash.se>
parents:
9967
diff
changeset
|
124 const char *pass = luaL_checklstring(L, 1, &pass_len); |
d536796a305f
util.hashes: Add PBKDF2-HMAC-SHA256
Kim Alvefur <zash@zash.se>
parents:
9967
diff
changeset
|
125 const unsigned char *salt = (unsigned char *)luaL_checklstring(L, 2, &salt_len); |
d536796a305f
util.hashes: Add PBKDF2-HMAC-SHA256
Kim Alvefur <zash@zash.se>
parents:
9967
diff
changeset
|
126 const int iter = luaL_checkinteger(L, 3); |
d536796a305f
util.hashes: Add PBKDF2-HMAC-SHA256
Kim Alvefur <zash@zash.se>
parents:
9967
diff
changeset
|
127 |
9970
4a43feb9ab15
Backed out changeset 61bc5c52c941
Kim Alvefur <zash@zash.se>
parents:
9969
diff
changeset
|
128 if(PKCS5_PBKDF2_HMAC(pass, pass_len, salt, salt_len, iter, EVP_sha256(), SHA256_DIGEST_LENGTH, out) == 0) { |
9968
d536796a305f
util.hashes: Add PBKDF2-HMAC-SHA256
Kim Alvefur <zash@zash.se>
parents:
9967
diff
changeset
|
129 return luaL_error(L, "PKCS5_PBKDF2_HMAC() failed"); |
d536796a305f
util.hashes: Add PBKDF2-HMAC-SHA256
Kim Alvefur <zash@zash.se>
parents:
9967
diff
changeset
|
130 } |
d536796a305f
util.hashes: Add PBKDF2-HMAC-SHA256
Kim Alvefur <zash@zash.se>
parents:
9967
diff
changeset
|
131 |
9970
4a43feb9ab15
Backed out changeset 61bc5c52c941
Kim Alvefur <zash@zash.se>
parents:
9969
diff
changeset
|
132 lua_pushlstring(L, (char *)out, SHA_DIGEST_LENGTH); |
9968
d536796a305f
util.hashes: Add PBKDF2-HMAC-SHA256
Kim Alvefur <zash@zash.se>
parents:
9967
diff
changeset
|
133 return 1; |
d536796a305f
util.hashes: Add PBKDF2-HMAC-SHA256
Kim Alvefur <zash@zash.se>
parents:
9967
diff
changeset
|
134 } |
d536796a305f
util.hashes: Add PBKDF2-HMAC-SHA256
Kim Alvefur <zash@zash.se>
parents:
9967
diff
changeset
|
135 |
6615
8e4572a642cb
util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents:
6413
diff
changeset
|
136 static const luaL_Reg Reg[] = { |
5537
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
137 { "sha1", Lsha1 }, |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
138 { "sha224", Lsha224 }, |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
139 { "sha256", Lsha256 }, |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
140 { "sha384", Lsha384 }, |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
141 { "sha512", Lsha512 }, |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
142 { "md5", Lmd5 }, |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
143 { "hmac_sha1", Lhmac_sha1 }, |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
144 { "hmac_sha256", Lhmac_sha256 }, |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
145 { "hmac_sha512", Lhmac_sha512 }, |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
146 { "hmac_md5", Lhmac_md5 }, |
9967
dc9bb31cbffe
util.hashes: Rename PBKDF2 function
Kim Alvefur <zash@zash.se>
parents:
9966
diff
changeset
|
147 { "scram_Hi_sha1", Lpbkdf2_sha1 }, /* COMPAT */ |
dc9bb31cbffe
util.hashes: Rename PBKDF2 function
Kim Alvefur <zash@zash.se>
parents:
9966
diff
changeset
|
148 { "pbkdf2_hmac_sha1", Lpbkdf2_sha1 }, |
9968
d536796a305f
util.hashes: Add PBKDF2-HMAC-SHA256
Kim Alvefur <zash@zash.se>
parents:
9967
diff
changeset
|
149 { "pbkdf2_hmac_sha256", Lpbkdf2_sha256 }, |
5537
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
150 { NULL, NULL } |
766
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
151 }; |
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
152 |
7889
b8d694646597
util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents:
7835
diff
changeset
|
153 LUALIB_API int luaopen_util_hashes(lua_State *L) { |
7818
54669df178c2
util-src: Make C modules assert that the Lua runtime matches what it was compiled for
Kim Alvefur <zash@zash.se>
parents:
6789
diff
changeset
|
154 #if (LUA_VERSION_NUM > 501) |
54669df178c2
util-src: Make C modules assert that the Lua runtime matches what it was compiled for
Kim Alvefur <zash@zash.se>
parents:
6789
diff
changeset
|
155 luaL_checkversion(L); |
54669df178c2
util-src: Make C modules assert that the Lua runtime matches what it was compiled for
Kim Alvefur <zash@zash.se>
parents:
6789
diff
changeset
|
156 #endif |
6411
6c8f6364bc48
util-src/*.c: Don't create globals when loaded
Kim Alvefur <zash@zash.se>
parents:
5774
diff
changeset
|
157 lua_newtable(L); |
9957
c8cfd2a5845c
util.hashes: Remove redundant semicolon
Kim Alvefur <zash@zash.se>
parents:
7889
diff
changeset
|
158 luaL_setfuncs(L, Reg, 0); |
766
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
159 lua_pushliteral(L, "-3.14"); |
6412
0e94f89d0e62
util-src/*.c: Use the more concise lua_setfield
Kim Alvefur <zash@zash.se>
parents:
6411
diff
changeset
|
160 lua_setfield(L, -2, "version"); |
766
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
161 return 1; |
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
162 } |