Software /
code /
prosody
Comparison
util-src/hashes.c @ 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
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Fri, 03 Apr 2015 19:52:48 +0200 |
parent | 6413:a552f4170aed |
child | 6620:50eaefeec013 |
comparison
equal
deleted
inserted
replaced
6613:2aae36312eb9 | 6615:8e4572a642cb |
---|---|
1 /* Prosody IM | 1 /* Prosody IM |
2 -- Copyright (C) 2009-2010 Matthew Wild | 2 -- Copyright (C) 2009-2010 Matthew Wild |
3 -- Copyright (C) 2009-2010 Waqas Hussain | 3 -- Copyright (C) 2009-2010 Waqas Hussain |
4 -- | 4 -- |
5 -- This project is MIT/X11 licensed. Please see the | 5 -- This project is MIT/X11 licensed. Please see the |
6 -- COPYING file in the source package for more information. | 6 -- COPYING file in the source package for more information. |
7 -- | 7 -- |
8 */ | 8 */ |
9 | 9 |
32 #endif | 32 #endif |
33 | 33 |
34 #define HMAC_IPAD 0x36363636 | 34 #define HMAC_IPAD 0x36363636 |
35 #define HMAC_OPAD 0x5c5c5c5c | 35 #define HMAC_OPAD 0x5c5c5c5c |
36 | 36 |
37 const char *hex_tab = "0123456789abcdef"; | 37 const char* hex_tab = "0123456789abcdef"; |
38 void toHex(const unsigned char *in, int length, unsigned char *out) { | 38 void toHex(const unsigned char* in, int length, unsigned char* out) { |
39 int i; | 39 int i; |
40 for (i = 0; i < length; i++) { | 40 |
41 out[i*2] = hex_tab[(in[i] >> 4) & 0xF]; | 41 for(i = 0; i < length; i++) { |
42 out[i*2+1] = hex_tab[(in[i]) & 0xF]; | 42 out[i * 2] = hex_tab[(in[i] >> 4) & 0xF]; |
43 out[i * 2 + 1] = hex_tab[(in[i]) & 0xF]; | |
43 } | 44 } |
44 } | 45 } |
45 | 46 |
46 #define MAKE_HASH_FUNCTION(myFunc, func, size) \ | 47 #define MAKE_HASH_FUNCTION(myFunc, func, size) \ |
47 static int myFunc(lua_State *L) { \ | 48 static int myFunc(lua_State *L) { \ |
66 MAKE_HASH_FUNCTION(Lsha512, SHA512, SHA512_DIGEST_LENGTH) | 67 MAKE_HASH_FUNCTION(Lsha512, SHA512, SHA512_DIGEST_LENGTH) |
67 MAKE_HASH_FUNCTION(Lmd5, MD5, MD5_DIGEST_LENGTH) | 68 MAKE_HASH_FUNCTION(Lmd5, MD5, MD5_DIGEST_LENGTH) |
68 | 69 |
69 struct hash_desc { | 70 struct hash_desc { |
70 int (*Init)(void*); | 71 int (*Init)(void*); |
71 int (*Update)(void*, const void *, size_t); | 72 int (*Update)(void*, const void*, size_t); |
72 int (*Final)(unsigned char*, void*); | 73 int (*Final)(unsigned char*, void*); |
73 size_t digestLength; | 74 size_t digestLength; |
74 void *ctx, *ctxo; | 75 void* ctx, *ctxo; |
75 }; | 76 }; |
76 | 77 |
77 static void hmac(struct hash_desc *desc, const char *key, size_t key_len, | 78 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) | 79 const char* msg, size_t msg_len, unsigned char* result) { |
79 { | |
80 union xory { | 80 union xory { |
81 unsigned char bytes[64]; | 81 unsigned char bytes[64]; |
82 uint32_t quadbytes[16]; | 82 uint32_t quadbytes[16]; |
83 }; | 83 }; |
84 | 84 |
85 int i; | 85 int i; |
86 unsigned char hashedKey[64]; /* Maximum used digest length */ | 86 unsigned char hashedKey[64]; /* Maximum used digest length */ |
87 union xory k_ipad, k_opad; | 87 union xory k_ipad, k_opad; |
88 | 88 |
89 if (key_len > 64) { | 89 if(key_len > 64) { |
90 desc->Init(desc->ctx); | 90 desc->Init(desc->ctx); |
91 desc->Update(desc->ctx, key, key_len); | 91 desc->Update(desc->ctx, key, key_len); |
92 desc->Final(hashedKey, desc->ctx); | 92 desc->Final(hashedKey, desc->ctx); |
93 key = (const char*)hashedKey; | 93 key = (const char*)hashedKey; |
94 key_len = desc->digestLength; | 94 key_len = desc->digestLength; |
96 | 96 |
97 memcpy(k_ipad.bytes, key, key_len); | 97 memcpy(k_ipad.bytes, key, key_len); |
98 memset(k_ipad.bytes + key_len, 0, 64 - key_len); | 98 memset(k_ipad.bytes + key_len, 0, 64 - key_len); |
99 memcpy(k_opad.bytes, k_ipad.bytes, 64); | 99 memcpy(k_opad.bytes, k_ipad.bytes, 64); |
100 | 100 |
101 for (i = 0; i < 16; i++) { | 101 for(i = 0; i < 16; i++) { |
102 k_ipad.quadbytes[i] ^= HMAC_IPAD; | 102 k_ipad.quadbytes[i] ^= HMAC_IPAD; |
103 k_opad.quadbytes[i] ^= HMAC_OPAD; | 103 k_opad.quadbytes[i] ^= HMAC_OPAD; |
104 } | 104 } |
105 | 105 |
106 desc->Init(desc->ctx); | 106 desc->Init(desc->ctx); |
141 MAKE_HMAC_FUNCTION(Lhmac_sha1, SHA1, SHA_DIGEST_LENGTH, SHA_CTX) | 141 MAKE_HMAC_FUNCTION(Lhmac_sha1, SHA1, SHA_DIGEST_LENGTH, SHA_CTX) |
142 MAKE_HMAC_FUNCTION(Lhmac_sha256, SHA256, SHA256_DIGEST_LENGTH, SHA256_CTX) | 142 MAKE_HMAC_FUNCTION(Lhmac_sha256, SHA256, SHA256_DIGEST_LENGTH, SHA256_CTX) |
143 MAKE_HMAC_FUNCTION(Lhmac_sha512, SHA512, SHA512_DIGEST_LENGTH, SHA512_CTX) | 143 MAKE_HMAC_FUNCTION(Lhmac_sha512, SHA512, SHA512_DIGEST_LENGTH, SHA512_CTX) |
144 MAKE_HMAC_FUNCTION(Lhmac_md5, MD5, MD5_DIGEST_LENGTH, MD5_CTX) | 144 MAKE_HMAC_FUNCTION(Lhmac_md5, MD5, MD5_DIGEST_LENGTH, MD5_CTX) |
145 | 145 |
146 static int LscramHi(lua_State *L) { | 146 static int LscramHi(lua_State* L) { |
147 union xory { | 147 union xory { |
148 unsigned char bytes[SHA_DIGEST_LENGTH]; | 148 unsigned char bytes[SHA_DIGEST_LENGTH]; |
149 uint32_t quadbytes[SHA_DIGEST_LENGTH/4]; | 149 uint32_t quadbytes[SHA_DIGEST_LENGTH / 4]; |
150 }; | 150 }; |
151 int i; | 151 int i; |
152 SHA_CTX ctx, ctxo; | 152 SHA_CTX ctx, ctxo; |
153 unsigned char Ust[SHA_DIGEST_LENGTH]; | 153 unsigned char Ust[SHA_DIGEST_LENGTH]; |
154 union xory Und; | 154 union xory Und; |
155 union xory res; | 155 union xory res; |
156 size_t str_len, salt_len; | 156 size_t str_len, salt_len; |
157 struct hash_desc desc; | 157 struct hash_desc desc; |
158 const char *str = luaL_checklstring(L, 1, &str_len); | 158 const char* str = luaL_checklstring(L, 1, &str_len); |
159 const char *salt = luaL_checklstring(L, 2, &salt_len); | 159 const char* salt = luaL_checklstring(L, 2, &salt_len); |
160 char *salt2; | 160 char* salt2; |
161 const int iter = luaL_checkinteger(L, 3); | 161 const int iter = luaL_checkinteger(L, 3); |
162 | 162 |
163 desc.Init = (int (*)(void*))SHA1_Init; | 163 desc.Init = (int (*)(void*))SHA1_Init; |
164 desc.Update = (int (*)(void*, const void *, size_t))SHA1_Update; | 164 desc.Update = (int (*)(void*, const void*, size_t))SHA1_Update; |
165 desc.Final = (int (*)(unsigned char*, void*))SHA1_Final; | 165 desc.Final = (int (*)(unsigned char*, void*))SHA1_Final; |
166 desc.digestLength = SHA_DIGEST_LENGTH; | 166 desc.digestLength = SHA_DIGEST_LENGTH; |
167 desc.ctx = &ctx; | 167 desc.ctx = &ctx; |
168 desc.ctxo = &ctxo; | 168 desc.ctxo = &ctxo; |
169 | 169 |
170 salt2 = malloc(salt_len + 4); | 170 salt2 = malloc(salt_len + 4); |
171 if (salt2 == NULL) | 171 |
172 if(salt2 == NULL) { | |
172 luaL_error(L, "Out of memory in scramHi"); | 173 luaL_error(L, "Out of memory in scramHi"); |
174 } | |
175 | |
173 memcpy(salt2, salt, salt_len); | 176 memcpy(salt2, salt, salt_len); |
174 memcpy(salt2 + salt_len, "\0\0\0\1", 4); | 177 memcpy(salt2 + salt_len, "\0\0\0\1", 4); |
175 hmac(&desc, str, str_len, salt2, salt_len + 4, Ust); | 178 hmac(&desc, str, str_len, salt2, salt_len + 4, Ust); |
176 free(salt2); | 179 free(salt2); |
177 | 180 |
178 memcpy(res.bytes, Ust, sizeof(res)); | 181 memcpy(res.bytes, Ust, sizeof(res)); |
179 for (i = 1; i < iter; i++) { | 182 |
183 for(i = 1; i < iter; i++) { | |
180 int j; | 184 int j; |
181 hmac(&desc, str, str_len, (char*)Ust, sizeof(Ust), Und.bytes); | 185 hmac(&desc, str, str_len, (char*)Ust, sizeof(Ust), Und.bytes); |
182 for (j = 0; j < SHA_DIGEST_LENGTH/4; j++) | 186 |
187 for(j = 0; j < SHA_DIGEST_LENGTH / 4; j++) { | |
183 res.quadbytes[j] ^= Und.quadbytes[j]; | 188 res.quadbytes[j] ^= Und.quadbytes[j]; |
189 } | |
190 | |
184 memcpy(Ust, Und.bytes, sizeof(Ust)); | 191 memcpy(Ust, Und.bytes, sizeof(Ust)); |
185 } | 192 } |
186 | 193 |
187 lua_pushlstring(L, (char*)res.bytes, SHA_DIGEST_LENGTH); | 194 lua_pushlstring(L, (char*)res.bytes, SHA_DIGEST_LENGTH); |
188 | 195 |
189 return 1; | 196 return 1; |
190 } | 197 } |
191 | 198 |
192 static const luaL_Reg Reg[] = | 199 static const luaL_Reg Reg[] = { |
193 { | |
194 { "sha1", Lsha1 }, | 200 { "sha1", Lsha1 }, |
195 { "sha224", Lsha224 }, | 201 { "sha224", Lsha224 }, |
196 { "sha256", Lsha256 }, | 202 { "sha256", Lsha256 }, |
197 { "sha384", Lsha384 }, | 203 { "sha384", Lsha384 }, |
198 { "sha512", Lsha512 }, | 204 { "sha512", Lsha512 }, |
203 { "hmac_md5", Lhmac_md5 }, | 209 { "hmac_md5", Lhmac_md5 }, |
204 { "scram_Hi_sha1", LscramHi }, | 210 { "scram_Hi_sha1", LscramHi }, |
205 { NULL, NULL } | 211 { NULL, NULL } |
206 }; | 212 }; |
207 | 213 |
208 LUALIB_API int luaopen_util_hashes(lua_State *L) | 214 LUALIB_API int luaopen_util_hashes(lua_State* L) { |
209 { | |
210 lua_newtable(L); | 215 lua_newtable(L); |
211 luaL_register(L, NULL, Reg); | 216 luaL_register(L, NULL, Reg); |
212 lua_pushliteral(L, "-3.14"); | 217 lua_pushliteral(L, "-3.14"); |
213 lua_setfield(L, -2, "version"); | 218 lua_setfield(L, -2, "version"); |
214 return 1; | 219 return 1; |