Software /
code /
prosody
Annotate
util-src/hashes.c @ 6413:a552f4170aed
util-src/*.c: Add macro for compiling with Lua 5.2
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Wed, 17 Sep 2014 14:30:29 +0200 |
parent | 6412:0e94f89d0e62 |
child | 6615:8e4572a642cb |
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 |
520 | 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 | |
10 | |
766
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
11 /* |
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
12 * hashes.c |
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
13 * 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
|
14 */ |
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
15 |
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
16 #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
|
17 #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
|
18 |
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 #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
|
20 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
|
21 #else |
5538
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
22 #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
|
23 #endif |
766
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
24 |
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
25 #include "lua.h" |
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
26 #include "lauxlib.h" |
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
27 #include <openssl/sha.h> |
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
28 #include <openssl/md5.h> |
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
29 |
6413
a552f4170aed
util-src/*.c: Add macro for compiling with Lua 5.2
Kim Alvefur <zash@zash.se>
parents:
6412
diff
changeset
|
30 #if (LUA_VERSION_NUM == 502) |
a552f4170aed
util-src/*.c: Add macro for compiling with Lua 5.2
Kim Alvefur <zash@zash.se>
parents:
6412
diff
changeset
|
31 #define luaL_register(L, N, R) luaL_setfuncs(L, R, 0) |
a552f4170aed
util-src/*.c: Add macro for compiling with Lua 5.2
Kim Alvefur <zash@zash.se>
parents:
6412
diff
changeset
|
32 #endif |
a552f4170aed
util-src/*.c: Add macro for compiling with Lua 5.2
Kim Alvefur <zash@zash.se>
parents:
6412
diff
changeset
|
33 |
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
|
34 #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
|
35 #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
|
36 |
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 const char *hex_tab = "0123456789abcdef"; |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
38 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
|
39 int i; |
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
40 for (i = 0; i < length; i++) { |
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
41 out[i*2] = hex_tab[(in[i] >> 4) & 0xF]; |
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
42 out[i*2+1] = hex_tab[(in[i]) & 0xF]; |
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
43 } |
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
44 } |
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 #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
|
47 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
|
48 size_t len; \ |
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
49 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
|
50 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
|
51 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
|
52 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
|
53 if (hex_out) { \ |
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
54 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
|
55 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
|
56 } 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
|
57 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
|
58 } \ |
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
59 return 1; \ |
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 |
4828
f3e841521436
util.hashes: Use defined hash function output lengths.
Kim Alvefur <zash@zash.se>
parents:
2923
diff
changeset
|
62 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
|
63 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
|
64 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
|
65 MAKE_HASH_FUNCTION(Lsha384, SHA384, SHA384_DIGEST_LENGTH) |
0ebc636faa59
util.hashes: Add sha224, sha384, sha512
Kim Alvefur <zash@zash.se>
parents:
4828
diff
changeset
|
66 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
|
67 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
|
68 |
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
|
69 struct hash_desc { |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
70 int (*Init)(void*); |
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 int (*Update)(void*, const void *, size_t); |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
72 int (*Final)(unsigned char*, void*); |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
73 size_t digestLength; |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
74 void *ctx, *ctxo; |
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 }; |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
76 |
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 static void hmac(struct hash_desc *desc, const char *key, size_t 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
|
78 const char *msg, size_t msg_len, unsigned char *result) |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
79 { |
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 union xory { |
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 bytes[64]; |
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 uint32_t quadbytes[16]; |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
83 }; |
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 |
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 int i; |
5774
6ef79af0c445
util.hashes: Silence compiler warning about pointer signedness
Kim Alvefur <zash@zash.se>
parents:
5768
diff
changeset
|
86 unsigned char hashedKey[64]; /* Maximum used digest length */ |
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
|
87 union xory k_ipad, k_opad; |
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 |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
89 if (key_len > 64) { |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
90 desc->Init(desc->ctx); |
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 desc->Update(desc->ctx, key, key_len); |
5768
c892709f035d
util.hashes: Correct argument order
Kim Alvefur <zash@zash.se>
parents:
5576
diff
changeset
|
92 desc->Final(hashedKey, desc->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
|
93 key = (const char*)hashedKey; |
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 key_len = desc->digestLength; |
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 |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
97 memcpy(k_ipad.bytes, key, 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
|
98 memset(k_ipad.bytes + key_len, 0, 64 - 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
|
99 memcpy(k_opad.bytes, k_ipad.bytes, 64); |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
100 |
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 for (i = 0; i < 16; i++) { |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
102 k_ipad.quadbytes[i] ^= HMAC_IPAD; |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
103 k_opad.quadbytes[i] ^= HMAC_OPAD; |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
104 } |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
105 |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
106 desc->Init(desc->ctx); |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
107 desc->Update(desc->ctx, k_ipad.bytes, 64); |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
108 desc->Init(desc->ctxo); |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
109 desc->Update(desc->ctxo, k_opad.bytes, 64); |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
110 desc->Update(desc->ctx, msg, 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
|
111 desc->Final(result, desc->ctx); |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
112 desc->Update(desc->ctxo, result, desc->digestLength); |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
113 desc->Final(result, desc->ctxo); |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
114 } |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
115 |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
116 #define MAKE_HMAC_FUNCTION(myFunc, func, size, type) \ |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
117 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
|
118 type ctx, ctxo; \ |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
119 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
|
120 size_t key_len, 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
|
121 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
|
122 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
|
123 const int hex_out = lua_toboolean(L, 3); \ |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
124 struct hash_desc desc; \ |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
125 desc.Init = (int (*)(void*))func##_Init; \ |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
126 desc.Update = (int (*)(void*, const void *, size_t))func##_Update; \ |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
127 desc.Final = (int (*)(unsigned char*, void*))func##_Final; \ |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
128 desc.digestLength = 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
|
129 desc.ctx = &ctx; \ |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
130 desc.ctxo = &ctxo; \ |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
131 hmac(&desc, key, key_len, msg, msg_len, hash); \ |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
132 if (hex_out) { \ |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
133 toHex(hash, size, result); \ |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
134 lua_pushlstring(L, (char*)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
|
135 } else { \ |
15464633d8fb
util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents:
4829
diff
changeset
|
136 lua_pushlstring(L, (char*)hash, 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
|
137 } \ |
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 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
|
139 } |
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 |
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 MAKE_HMAC_FUNCTION(Lhmac_sha1, SHA1, SHA_DIGEST_LENGTH, SHA_CTX) |
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 MAKE_HMAC_FUNCTION(Lhmac_sha256, SHA256, SHA256_DIGEST_LENGTH, SHA256_CTX) |
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 MAKE_HMAC_FUNCTION(Lhmac_sha512, SHA512, SHA512_DIGEST_LENGTH, SHA512_CTX) |
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 MAKE_HMAC_FUNCTION(Lhmac_md5, MD5, MD5_DIGEST_LENGTH, MD5_CTX) |
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 |
5538
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
146 static int LscramHi(lua_State *L) { |
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
147 union xory { |
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
148 unsigned char bytes[SHA_DIGEST_LENGTH]; |
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
149 uint32_t quadbytes[SHA_DIGEST_LENGTH/4]; |
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
150 }; |
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
151 int i; |
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
152 SHA_CTX ctx, ctxo; |
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
153 unsigned char Ust[SHA_DIGEST_LENGTH]; |
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
154 union xory Und; |
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
155 union xory res; |
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
156 size_t str_len, salt_len; |
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
157 struct hash_desc desc; |
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
158 const char *str = luaL_checklstring(L, 1, &str_len); |
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
159 const char *salt = luaL_checklstring(L, 2, &salt_len); |
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
160 char *salt2; |
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
161 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
|
162 |
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
163 desc.Init = (int (*)(void*))SHA1_Init; |
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
164 desc.Update = (int (*)(void*, const void *, size_t))SHA1_Update; |
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
165 desc.Final = (int (*)(unsigned char*, void*))SHA1_Final; |
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
166 desc.digestLength = SHA_DIGEST_LENGTH; |
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
167 desc.ctx = &ctx; |
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
168 desc.ctxo = &ctxo; |
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
169 |
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
170 salt2 = malloc(salt_len + 4); |
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
171 if (salt2 == NULL) |
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
172 luaL_error(L, "Out of memory in scramHi"); |
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
173 memcpy(salt2, salt, salt_len); |
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
174 memcpy(salt2 + salt_len, "\0\0\0\1", 4); |
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
175 hmac(&desc, str, str_len, salt2, salt_len + 4, Ust); |
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
176 free(salt2); |
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
177 |
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
178 memcpy(res.bytes, Ust, sizeof(res)); |
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
179 for (i = 1; i < iter; i++) { |
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
180 int j; |
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
181 hmac(&desc, str, str_len, (char*)Ust, sizeof(Ust), Und.bytes); |
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
182 for (j = 0; j < SHA_DIGEST_LENGTH/4; j++) |
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
183 res.quadbytes[j] ^= Und.quadbytes[j]; |
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
184 memcpy(Ust, Und.bytes, sizeof(Ust)); |
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
185 } |
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
186 |
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
187 lua_pushlstring(L, (char*)res.bytes, SHA_DIGEST_LENGTH); |
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
188 |
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
189 return 1; |
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
190 } |
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
191 |
766
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
192 static const luaL_Reg Reg[] = |
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
193 { |
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
|
194 { "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
|
195 { "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
|
196 { "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
|
197 { "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
|
198 { "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
|
199 { "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
|
200 { "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
|
201 { "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
|
202 { "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
|
203 { "hmac_md5", Lhmac_md5 }, |
5538
62089c9c142d
util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents:
5537
diff
changeset
|
204 { "scram_Hi_sha1", LscramHi }, |
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
|
205 { NULL, NULL } |
766
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
206 }; |
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
207 |
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
208 LUALIB_API int luaopen_util_hashes(lua_State *L) |
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
209 { |
6411
6c8f6364bc48
util-src/*.c: Don't create globals when loaded
Kim Alvefur <zash@zash.se>
parents:
5774
diff
changeset
|
210 lua_newtable(L); |
6c8f6364bc48
util-src/*.c: Don't create globals when loaded
Kim Alvefur <zash@zash.se>
parents:
5774
diff
changeset
|
211 luaL_register(L, NULL, Reg); |
766
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
212 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
|
213 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
|
214 return 1; |
433a5226267f
Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents:
520
diff
changeset
|
215 } |