Comparison

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
comparison
equal deleted inserted replaced
5537:15464633d8fb 5538:62089c9c142d
12 * hashes.c 12 * hashes.c
13 * Lua library for sha1, sha256 and md5 hashes 13 * Lua library for sha1, sha256 and md5 hashes
14 */ 14 */
15 15
16 #include <string.h> 16 #include <string.h>
17 #include <stdlib.h>
18 #include <inttypes.h>
17 19
18 #include "lua.h" 20 #include "lua.h"
19 #include "lauxlib.h" 21 #include "lauxlib.h"
20 #include <openssl/sha.h> 22 #include <openssl/sha.h>
21 #include <openssl/md5.h> 23 #include <openssl/md5.h>
130 MAKE_HMAC_FUNCTION(Lhmac_sha1, SHA1, SHA_DIGEST_LENGTH, SHA_CTX) 132 MAKE_HMAC_FUNCTION(Lhmac_sha1, SHA1, SHA_DIGEST_LENGTH, SHA_CTX)
131 MAKE_HMAC_FUNCTION(Lhmac_sha256, SHA256, SHA256_DIGEST_LENGTH, SHA256_CTX) 133 MAKE_HMAC_FUNCTION(Lhmac_sha256, SHA256, SHA256_DIGEST_LENGTH, SHA256_CTX)
132 MAKE_HMAC_FUNCTION(Lhmac_sha512, SHA512, SHA512_DIGEST_LENGTH, SHA512_CTX) 134 MAKE_HMAC_FUNCTION(Lhmac_sha512, SHA512, SHA512_DIGEST_LENGTH, SHA512_CTX)
133 MAKE_HMAC_FUNCTION(Lhmac_md5, MD5, MD5_DIGEST_LENGTH, MD5_CTX) 135 MAKE_HMAC_FUNCTION(Lhmac_md5, MD5, MD5_DIGEST_LENGTH, MD5_CTX)
134 136
137 static int LscramHi(lua_State *L) {
138 union xory {
139 unsigned char bytes[SHA_DIGEST_LENGTH];
140 uint32_t quadbytes[SHA_DIGEST_LENGTH/4];
141 };
142 int i;
143 SHA_CTX ctx, ctxo;
144 unsigned char Ust[SHA_DIGEST_LENGTH];
145 union xory Und;
146 union xory res;
147 size_t str_len, salt_len;
148 struct hash_desc desc;
149 const char *str = luaL_checklstring(L, 1, &str_len);
150 const char *salt = luaL_checklstring(L, 2, &salt_len);
151 char *salt2;
152 const int iter = luaL_checkinteger(L, 3);
153
154 desc.Init = (int (*)(void*))SHA1_Init;
155 desc.Update = (int (*)(void*, const void *, size_t))SHA1_Update;
156 desc.Final = (int (*)(unsigned char*, void*))SHA1_Final;
157 desc.digestLength = SHA_DIGEST_LENGTH;
158 desc.ctx = &ctx;
159 desc.ctxo = &ctxo;
160
161 salt2 = malloc(salt_len + 4);
162 if (salt2 == NULL)
163 luaL_error(L, "Out of memory in scramHi");
164 memcpy(salt2, salt, salt_len);
165 memcpy(salt2 + salt_len, "\0\0\0\1", 4);
166 hmac(&desc, str, str_len, salt2, salt_len + 4, Ust);
167 free(salt2);
168
169 memcpy(res.bytes, Ust, sizeof(res));
170 for (i = 1; i < iter; i++) {
171 int j;
172 hmac(&desc, str, str_len, (char*)Ust, sizeof(Ust), Und.bytes);
173 for (j = 0; j < SHA_DIGEST_LENGTH/4; j++)
174 res.quadbytes[j] ^= Und.quadbytes[j];
175 memcpy(Ust, Und.bytes, sizeof(Ust));
176 }
177
178 lua_pushlstring(L, (char*)res.bytes, SHA_DIGEST_LENGTH);
179
180 return 1;
181 }
182
135 static const luaL_Reg Reg[] = 183 static const luaL_Reg Reg[] =
136 { 184 {
137 { "sha1", Lsha1 }, 185 { "sha1", Lsha1 },
138 { "sha224", Lsha224 }, 186 { "sha224", Lsha224 },
139 { "sha256", Lsha256 }, 187 { "sha256", Lsha256 },
142 { "md5", Lmd5 }, 190 { "md5", Lmd5 },
143 { "hmac_sha1", Lhmac_sha1 }, 191 { "hmac_sha1", Lhmac_sha1 },
144 { "hmac_sha256", Lhmac_sha256 }, 192 { "hmac_sha256", Lhmac_sha256 },
145 { "hmac_sha512", Lhmac_sha512 }, 193 { "hmac_sha512", Lhmac_sha512 },
146 { "hmac_md5", Lhmac_md5 }, 194 { "hmac_md5", Lhmac_md5 },
195 { "scram_Hi_sha1", LscramHi },
147 { NULL, NULL } 196 { NULL, NULL }
148 }; 197 };
149 198
150 LUALIB_API int luaopen_util_hashes(lua_State *L) 199 LUALIB_API int luaopen_util_hashes(lua_State *L)
151 { 200 {