Annotate

util-src/hashes.c @ 5538:62089c9c142d

util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
author Florian Zeitz <florob@babelmonkeys.de>
date Sun, 28 Apr 2013 02:28:42 +0200
parent 5537:15464633d8fb
child 5576:7656b9f06bb5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
e96ac4bb6dd8 and the C files too
Matthew Wild <mwild1@gmail.com>
parents: 441
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
e96ac4bb6dd8 and the C files too
Matthew Wild <mwild1@gmail.com>
parents: 441
diff changeset
8 */
e96ac4bb6dd8 and the C files too
Matthew Wild <mwild1@gmail.com>
parents: 441
diff changeset
9
e96ac4bb6dd8 and the C files too
Matthew Wild <mwild1@gmail.com>
parents: 441
diff changeset
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>
62089c9c142d util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents: 5537
diff changeset
18 #include <inttypes.h>
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
19
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
20 #include "lua.h"
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
21 #include "lauxlib.h"
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
22 #include <openssl/sha.h>
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
23 #include <openssl/md5.h>
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
24
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
25 #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
26 #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
27
15464633d8fb util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents: 4829
diff changeset
28 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
29 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
30 int i;
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
31 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
32 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
33 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
34 }
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
35 }
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
36
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
37 #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
38 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
39 size_t len; \
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
40 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
41 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
42 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
43 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
44 if (hex_out) { \
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
45 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
46 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
47 } 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
48 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
49 } \
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
50 return 1; \
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
51 }
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
52
4828
f3e841521436 util.hashes: Use defined hash function output lengths.
Kim Alvefur <zash@zash.se>
parents: 2923
diff changeset
53 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
54 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
55 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
56 MAKE_HASH_FUNCTION(Lsha384, SHA384, SHA384_DIGEST_LENGTH)
0ebc636faa59 util.hashes: Add sha224, sha384, sha512
Kim Alvefur <zash@zash.se>
parents: 4828
diff changeset
57 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
58 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
59
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
60 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
61 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
62 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
63 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
64 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
65 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
66 };
15464633d8fb util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents: 4829
diff changeset
67
15464633d8fb util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents: 4829
diff changeset
68 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
69 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
70 {
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 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
72 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
73 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
74 };
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 int 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
77 char hashedKey[64]; /* Maximum used digest length */
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 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
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 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
81 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
82 desc->Update(desc->ctx, 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
83 desc->Final(desc->ctx, 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
84 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
85 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
86 }
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
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 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
89 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
90 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
91
15464633d8fb util.hmac, util.hashes: Implement HMAC functions in C, and move to util.hashes
Florian Zeitz <florob@babelmonkeys.de>
parents: 4829
diff changeset
92 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
93 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
94 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
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 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
98 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
99 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
100 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
101 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
102 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
103 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
104 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
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
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 #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
108 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
109 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
110 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
111 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
112 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
113 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
114 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
115 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
116 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
117 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
118 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
119 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
120 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
121 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
122 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
123 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
124 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
125 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
126 } 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
127 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
128 } \
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 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
130 }
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
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 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
133 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
134 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
135 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
136
5538
62089c9c142d util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents: 5537
diff changeset
137 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
138 union xory {
62089c9c142d util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents: 5537
diff changeset
139 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
140 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
141 };
62089c9c142d util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents: 5537
diff changeset
142 int i;
62089c9c142d util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents: 5537
diff changeset
143 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
144 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
145 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
146 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
147 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
148 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
149 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
150 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
151 char *salt2;
62089c9c142d util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents: 5537
diff changeset
152 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
153
62089c9c142d util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents: 5537
diff changeset
154 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
155 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
156 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
157 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
158 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
159 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
160
62089c9c142d util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents: 5537
diff changeset
161 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
162 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
163 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
164 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
165 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
166 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
167 free(salt2);
62089c9c142d util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents: 5537
diff changeset
168
62089c9c142d util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents: 5537
diff changeset
169 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
170 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
171 int j;
62089c9c142d util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents: 5537
diff changeset
172 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
173 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
174 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
175 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
176 }
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 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
179
62089c9c142d util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents: 5537
diff changeset
180 return 1;
62089c9c142d util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents: 5537
diff changeset
181 }
62089c9c142d util.hashes, util.sasl.scram: Implement SCRAM-SHA1's Hi in C
Florian Zeitz <florob@babelmonkeys.de>
parents: 5537
diff changeset
182
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
183 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
184 {
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
185 { "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
186 { "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
187 { "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
188 { "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
189 { "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
190 { "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
191 { "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
192 { "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
193 { "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
194 { "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
195 { "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
196 { NULL, NULL }
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
197 };
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
198
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
199 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
200 {
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
201 luaL_register(L, "hashes", Reg);
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
202 lua_pushliteral(L, "version"); /** version */
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
203 lua_pushliteral(L, "-3.14");
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
204 lua_settable(L,-3);
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
205 return 1;
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
206 }