Skip to content

Instantly share code, notes, and snippets.

@BrianLeishman

BrianLeishman/sha3.c

Last active Feb 27, 2018
Embed
What would you like to do?
MySQL UDF for sha3 hash generation
/*
* Copyright (c) 2018, brian
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* File: sha3.c
* Author: brian
*
* Created on February 23, 2018, 2:15 PM
*/
/*
* USAGE INSTRUCTIONS:
*
* make sure libmysqlclient-dev and rhash is installed:
* apt install libmysqlclient-dev rhash librhash-dev
*
* Replace "/usr/lib/mysql/plugin" with your MySQL plugins directory (can be found by running "select @@plugin_dir;")
* gcc -I/usr/include/mysql -o sha3.so -shared sha3.c -lrhash -fPIC && cp sha3.so /usr/lib/mysql/plugin/sha3.so
*
* Then, on the server:
* create function`sha3`returns string soname'sha3.so';
*
* And use/test like:
* select sha3('yeet',224);
*
* Note: this function is intended to work EXACTLY like the native sha2 function,
* so when MySQL eventually adds support for sha3,
* the native function SHOULD seamlessly be used instead of this one.
*
* Yeet!
*
*/
#ifdef STANDARD
/* STANDARD is defined, don't use any mysql functions */
#include <string.h>
#ifdef __WIN__
typedef unsigned __int64 ulonglong; /* Microsofts 64 bit types */
typedef __int64 longlong;
#else
typedef unsigned long long ulonglong;
typedef long long longlong;
#endif /*__WIN__*/
#else
#include <my_global.h>
#include <my_sys.h>
#if defined(MYSQL_SERVER)
#include <m_string.h>
#else
/* when compiled as standalone */
#include <string.h>
#endif
#endif
#include <mysql.h>
#include <rhash.h>
#ifdef HAVE_DLOPEN
my_bool sha3_init(UDF_INIT *initid, UDF_ARGS *args, char *message);
void sha3_deinit(UDF_INIT *initid);
char *sha3(UDF_INIT *initid __attribute__ ((unused)), UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *message __attribute__ ((unused)));
my_bool sha3_init(UDF_INIT *initid, UDF_ARGS *args, char *message) {
if (args->arg_count != 2) {
strcpy(message, "`sha3`() requires 2 parameters: the message part, and the bits");
return 1;
}
args->arg_type[1] = INT_RESULT;
initid->maybe_null = 1; //can return null
return 0;
}
char *sha3(UDF_INIT *initid __attribute__ ((unused)), UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *message __attribute__ ((unused))) {
long long int_val;
int_val = *((long long*) args->args[1]);
if (int_val == 0) {
int_val = 256;
}
if (args->args[0] == NULL || args->args[1] == NULL ||
(int_val != 224 && int_val != 256 && int_val != 384 && int_val != 512)) {
*is_null = 1;
return 0;
}
unsigned long len = int_val / 8;
unsigned char hash[len];
unsigned char *hex = malloc(sizeof (char) * ((len * 2) + 1));
unsigned hash_id;
switch (int_val) {
case 224:
hash_id = RHASH_SHA3_224;
break;
case 256:
hash_id = RHASH_SHA3_256;
break;
case 384:
hash_id = RHASH_SHA3_384;
break;
case 512:
hash_id = RHASH_SHA3_512;
break;
}
if (rhash_msg(hash_id, args->args[0], args->lengths[0], hash)) {
*is_null = 1;
return 0;
}
int i;
for (i = 0; i < len; i++) {
sprintf(hex + i * 2, "%02x", hash[i]);
}
*length = len * 2;
return hex;
}
#endif /* HAVE_DLOPEN */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment