Software /
code /
prosody
Comparison
util-src/hashes.c @ 7889:b8d694646597
util-src/*.c: Attach pointer * to name instead of type
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 12 Feb 2017 16:42:29 +0100 |
parent | 7835:a809dcfd0c5b |
child | 9957:c8cfd2a5845c |
child | 9962:29bc3dff3419 |
comparison
equal
deleted
inserted
replaced
7888:74187ee6ed55 | 7889:b8d694646597 |
---|---|
31 #endif | 31 #endif |
32 | 32 |
33 #define HMAC_IPAD 0x36363636 | 33 #define HMAC_IPAD 0x36363636 |
34 #define HMAC_OPAD 0x5c5c5c5c | 34 #define HMAC_OPAD 0x5c5c5c5c |
35 | 35 |
36 const char* hex_tab = "0123456789abcdef"; | 36 const char *hex_tab = "0123456789abcdef"; |
37 void toHex(const unsigned char* in, int length, unsigned char* out) { | 37 void toHex(const unsigned char *in, int length, unsigned char *out) { |
38 int i; | 38 int i; |
39 | 39 |
40 for(i = 0; i < length; i++) { | 40 for(i = 0; i < length; i++) { |
41 out[i * 2] = hex_tab[(in[i] >> 4) & 0xF]; | 41 out[i * 2] = hex_tab[(in[i] >> 4) & 0xF]; |
42 out[i * 2 + 1] = hex_tab[(in[i]) & 0xF]; | 42 out[i * 2 + 1] = hex_tab[(in[i]) & 0xF]; |
65 MAKE_HASH_FUNCTION(Lsha384, SHA384, SHA384_DIGEST_LENGTH) | 65 MAKE_HASH_FUNCTION(Lsha384, SHA384, SHA384_DIGEST_LENGTH) |
66 MAKE_HASH_FUNCTION(Lsha512, SHA512, SHA512_DIGEST_LENGTH) | 66 MAKE_HASH_FUNCTION(Lsha512, SHA512, SHA512_DIGEST_LENGTH) |
67 MAKE_HASH_FUNCTION(Lmd5, MD5, MD5_DIGEST_LENGTH) | 67 MAKE_HASH_FUNCTION(Lmd5, MD5, MD5_DIGEST_LENGTH) |
68 | 68 |
69 struct hash_desc { | 69 struct hash_desc { |
70 int (*Init)(void*); | 70 int (*Init)(void *); |
71 int (*Update)(void*, const void*, size_t); | 71 int (*Update)(void *, const void *, size_t); |
72 int (*Final)(unsigned char*, void*); | 72 int (*Final)(unsigned char *, void *); |
73 size_t digestLength; | 73 size_t digestLength; |
74 void* ctx, *ctxo; | 74 void *ctx, *ctxo; |
75 }; | 75 }; |
76 | 76 |
77 static void hmac(struct hash_desc* desc, const char* key, size_t key_len, | 77 static void hmac(struct hash_desc *desc, const char *key, size_t key_len, |
78 const char* msg, size_t msg_len, unsigned char* result) { | 78 const char *msg, size_t msg_len, unsigned char *result) { |
79 union xory { | 79 union xory { |
80 unsigned char bytes[64]; | 80 unsigned char bytes[64]; |
81 uint32_t quadbytes[16]; | 81 uint32_t quadbytes[16]; |
82 }; | 82 }; |
83 | 83 |
87 | 87 |
88 if(key_len > 64) { | 88 if(key_len > 64) { |
89 desc->Init(desc->ctx); | 89 desc->Init(desc->ctx); |
90 desc->Update(desc->ctx, key, key_len); | 90 desc->Update(desc->ctx, key, key_len); |
91 desc->Final(hashedKey, desc->ctx); | 91 desc->Final(hashedKey, desc->ctx); |
92 key = (const char*)hashedKey; | 92 key = (const char *)hashedKey; |
93 key_len = desc->digestLength; | 93 key_len = desc->digestLength; |
94 } | 94 } |
95 | 95 |
96 memcpy(k_ipad.bytes, key, key_len); | 96 memcpy(k_ipad.bytes, key, key_len); |
97 memset(k_ipad.bytes + key_len, 0, 64 - key_len); | 97 memset(k_ipad.bytes + key_len, 0, 64 - key_len); |
140 MAKE_HMAC_FUNCTION(Lhmac_sha1, SHA1, SHA_DIGEST_LENGTH, SHA_CTX) | 140 MAKE_HMAC_FUNCTION(Lhmac_sha1, SHA1, SHA_DIGEST_LENGTH, SHA_CTX) |
141 MAKE_HMAC_FUNCTION(Lhmac_sha256, SHA256, SHA256_DIGEST_LENGTH, SHA256_CTX) | 141 MAKE_HMAC_FUNCTION(Lhmac_sha256, SHA256, SHA256_DIGEST_LENGTH, SHA256_CTX) |
142 MAKE_HMAC_FUNCTION(Lhmac_sha512, SHA512, SHA512_DIGEST_LENGTH, SHA512_CTX) | 142 MAKE_HMAC_FUNCTION(Lhmac_sha512, SHA512, SHA512_DIGEST_LENGTH, SHA512_CTX) |
143 MAKE_HMAC_FUNCTION(Lhmac_md5, MD5, MD5_DIGEST_LENGTH, MD5_CTX) | 143 MAKE_HMAC_FUNCTION(Lhmac_md5, MD5, MD5_DIGEST_LENGTH, MD5_CTX) |
144 | 144 |
145 static int LscramHi(lua_State* L) { | 145 static int LscramHi(lua_State *L) { |
146 union xory { | 146 union xory { |
147 unsigned char bytes[SHA_DIGEST_LENGTH]; | 147 unsigned char bytes[SHA_DIGEST_LENGTH]; |
148 uint32_t quadbytes[SHA_DIGEST_LENGTH / 4]; | 148 uint32_t quadbytes[SHA_DIGEST_LENGTH / 4]; |
149 }; | 149 }; |
150 int i; | 150 int i; |
152 unsigned char Ust[SHA_DIGEST_LENGTH]; | 152 unsigned char Ust[SHA_DIGEST_LENGTH]; |
153 union xory Und; | 153 union xory Und; |
154 union xory res; | 154 union xory res; |
155 size_t str_len, salt_len; | 155 size_t str_len, salt_len; |
156 struct hash_desc desc; | 156 struct hash_desc desc; |
157 const char* str = luaL_checklstring(L, 1, &str_len); | 157 const char *str = luaL_checklstring(L, 1, &str_len); |
158 const char* salt = luaL_checklstring(L, 2, &salt_len); | 158 const char *salt = luaL_checklstring(L, 2, &salt_len); |
159 char* salt2; | 159 char *salt2; |
160 const int iter = luaL_checkinteger(L, 3); | 160 const int iter = luaL_checkinteger(L, 3); |
161 | 161 |
162 desc.Init = (int (*)(void*))SHA1_Init; | 162 desc.Init = (int (*)(void *))SHA1_Init; |
163 desc.Update = (int (*)(void*, const void*, size_t))SHA1_Update; | 163 desc.Update = (int (*)(void *, const void *, size_t))SHA1_Update; |
164 desc.Final = (int (*)(unsigned char*, void*))SHA1_Final; | 164 desc.Final = (int (*)(unsigned char *, void *))SHA1_Final; |
165 desc.digestLength = SHA_DIGEST_LENGTH; | 165 desc.digestLength = SHA_DIGEST_LENGTH; |
166 desc.ctx = &ctx; | 166 desc.ctx = &ctx; |
167 desc.ctxo = &ctxo; | 167 desc.ctxo = &ctxo; |
168 | 168 |
169 salt2 = malloc(salt_len + 4); | 169 salt2 = malloc(salt_len + 4); |
179 | 179 |
180 memcpy(res.bytes, Ust, sizeof(res)); | 180 memcpy(res.bytes, Ust, sizeof(res)); |
181 | 181 |
182 for(i = 1; i < iter; i++) { | 182 for(i = 1; i < iter; i++) { |
183 int j; | 183 int j; |
184 hmac(&desc, str, str_len, (char*)Ust, sizeof(Ust), Und.bytes); | 184 hmac(&desc, str, str_len, (char *)Ust, sizeof(Ust), Und.bytes); |
185 | 185 |
186 for(j = 0; j < SHA_DIGEST_LENGTH / 4; j++) { | 186 for(j = 0; j < SHA_DIGEST_LENGTH / 4; j++) { |
187 res.quadbytes[j] ^= Und.quadbytes[j]; | 187 res.quadbytes[j] ^= Und.quadbytes[j]; |
188 } | 188 } |
189 | 189 |
190 memcpy(Ust, Und.bytes, sizeof(Ust)); | 190 memcpy(Ust, Und.bytes, sizeof(Ust)); |
191 } | 191 } |
192 | 192 |
193 lua_pushlstring(L, (char*)res.bytes, SHA_DIGEST_LENGTH); | 193 lua_pushlstring(L, (char *)res.bytes, SHA_DIGEST_LENGTH); |
194 | 194 |
195 return 1; | 195 return 1; |
196 } | 196 } |
197 | 197 |
198 static const luaL_Reg Reg[] = { | 198 static const luaL_Reg Reg[] = { |
208 { "hmac_md5", Lhmac_md5 }, | 208 { "hmac_md5", Lhmac_md5 }, |
209 { "scram_Hi_sha1", LscramHi }, | 209 { "scram_Hi_sha1", LscramHi }, |
210 { NULL, NULL } | 210 { NULL, NULL } |
211 }; | 211 }; |
212 | 212 |
213 LUALIB_API int luaopen_util_hashes(lua_State* L) { | 213 LUALIB_API int luaopen_util_hashes(lua_State *L) { |
214 #if (LUA_VERSION_NUM > 501) | 214 #if (LUA_VERSION_NUM > 501) |
215 luaL_checkversion(L); | 215 luaL_checkversion(L); |
216 #endif | 216 #endif |
217 lua_newtable(L); | 217 lua_newtable(L); |
218 luaL_setfuncs(L, Reg, 0);; | 218 luaL_setfuncs(L, Reg, 0);; |