11 #include <drift/dslcore.h>
12 #include <drift/GenLib.h>
13 #include <drift/Mutex.h>
14 #include <drift/hash.h>
15 #include <drift/hmac.h>
17 extern DSL_Mutex * dslMutex();
19 typedef vector<const HMAC_PROVIDER*> hmacProviderList;
20 hmacProviderList* dslHMACProviders() {
21 AutoMutexPtr(dslMutex());
22 static hmacProviderList hmac_providers { &dsl_native_hmacers };
23 return &hmac_providers;
27 AutoMutexPtr(dslMutex());
28 hmacProviderList* hmac_providers = dslHMACProviders();
29 hmac_providers->insert(hmac_providers->begin(), p);
31 void DSL_CC dsl_remove_hmac_provider(
const HMAC_PROVIDER * p) {
32 AutoMutexPtr(dslMutex());
33 hmacProviderList* hmac_providers = dslHMACProviders();
34 for (
auto x = hmac_providers->begin(); x != hmac_providers->end(); x++) {
36 hmac_providers->erase(x);
41 void DSL_CC dsl_get_hmac_providers(vector<const HMAC_PROVIDER *>& p) {
42 AutoMutexPtr(dslMutex());
43 p = *dslHMACProviders();
48 hmacProviderList* hmac_providers = dslHMACProviders();
49 for (
auto x = hmac_providers->begin(); x != hmac_providers->end(); x++) {
60 if (ctx == NULL || data == NULL) {
63 ctx->provider->hmac_update(ctx, data, len);
67 if (ctx == NULL || out == NULL) {
70 return ctx->provider->hmac_finish(ctx, out, outlen);
73 DSL_API
bool DSL_CC
hmacdata(
const char * name,
const uint8 *key,
size_t keylen,
const uint8 *data,
size_t datalen,
char * out,
size_t outlen) {
78 if (outlen < ((ctx->hashSize*2)+1)) {
84 size_t size = ctx->hashSize;
85 unsigned char * hashtmp = (
unsigned char *)dsl_malloc(size);
88 ret = (
bin2hex(hashtmp, size, out, outlen) != NULL);
94 DSL_API
bool DSL_CC
hmacfile(
const char * name,
const uint8 *key,
size_t keylen,
const char * fn,
char * out,
size_t outlen) {
97 bool ret =
hmacfile_rw(name, key, keylen, fp, out, outlen);
104 DSL_API
bool DSL_CC
hmacfile_fp(
const char * name,
const uint8 *key,
size_t keylen, FILE * fpp,
char * out,
size_t outlen) {
105 if (fpp == NULL) {
return false; }
109 bool ret =
hmacfile_rw(name, key, keylen, fp, out, outlen);
116 DSL_API
bool DSL_CC
hmacfile_rw(
const char * name,
const uint8 *key,
size_t keylen,
DSL_FILE * fp,
char * out,
size_t outlen) {
121 if (outlen < ((ctx->hashSize*2)+1)) {
127 fp->
seek(fp, 0, SEEK_END);
128 int64 left = fp->
tell(fp);
129 fp->
seek(fp, 0, SEEK_SET);
133 int64 toRead = (left >=
sizeof(buf)) ?
sizeof(buf):left;
139 if (fp->
read(buf, toRead, fp) == toRead) {
148 size_t size = ctx->hashSize;
149 unsigned char * hashtmp = (
unsigned char *)dsl_malloc(ctx->hashSize);
152 ret2 = (
bin2hex(hashtmp, size, out, outlen) != NULL);
155 return (ret && ret2);
DSL_API void DSL_CC dsl_free(void *ptr)
DSL_API_CLASS char *DSL_CC bin2hex(const uint8_t *data, size_t datalen, char *out, size_t outsize)
DSL_API bool DSL_CC hmacfile_rw(const char *name, const uint8 *key, size_t keylen, DSL_FILE *fp, char *out, size_t outlen)
Wrapper around hmac_init()/hmac_update()/hmac_finish(). If raw_output == true out will contain binary...
DSL_API bool DSL_CC hmacfile(const char *name, const uint8 *key, size_t keylen, const char *fn, char *out, size_t outlen)
Wrapper around hmac_init()/hmac_update()/hmac_finish(). If raw_output == true out will contain binary...
bool DSL_CC hmac_finish(HASH_HMAC_CTX *ctx, uint8 *out, size_t outlen)
Finalize HMAC and store in out. outlen should be >= hashSize in the HASH_CTX struct....
HASH_HMAC_CTX *DSL_CC hmac_init(const char *name, const uint8 *key, size_t length)
DSL_API bool DSL_CC hmacdata(const char *name, const uint8 *key, size_t keylen, const uint8 *data, size_t datalen, char *out, size_t outlen)
Wrapper around hmac_init()/hmac_update()/hmac_finish(). If raw_output == true out will contain binary...
DSL_API bool DSL_CC hmacfile_fp(const char *name, const uint8 *key, size_t keylen, FILE *fpp, char *out, size_t outlen)
Wrapper around hmac_init()/hmac_update()/hmac_finish(). If raw_output == true out will contain binary...
void DSL_CC hmac_update(HASH_HMAC_CTX *ctx, const uint8 *data, size_t len)
Call with the data you want to hash, can be called multiple times to hash a large file in chunks for ...
DSL_API DSL_FILE *DSL_CC RW_ConvertFile(FILE *fp, bool autoclose)
DSL_API DSL_FILE *DSL_CC RW_OpenFile(const char *fn, const char *mode)
void(* close)(DSL_FILE *fp)
bool(* seek)(DSL_FILE *fp, int64 pos, int mode)
int64(* read)(void *buf, int64 size, DSL_FILE *fp)
int64(* tell)(DSL_FILE *fp)