1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
/*
Copyright (C) 2018 MariaDB Corporation AB
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not see <http://www.gnu.org/licenses>
or write to the Free Software Foundation, Inc.,
51 Franklin St., Fifth Floor, Boston, MA 02110, USA
*/
#ifndef _ma_crypt_h_
#define _ma_crypt_h_
#include <ma_hash.h>
#include <stddef.h>
#include <stdarg.h>
/*! Hash algorithms */
#define MA_HASH_RIPEMD160 7
#define MA_HASH_MAX 8
/*! Hash digest sizes */
#define MA_RIPEMD160_HASH_SIZE 20
#if defined(HAVE_WINCRYPT)
typedef void MA_HASH_CTX;
#elif defined(HAVE_OPENSSL)
#include <openssl/evp.h>
typedef EVP_MD_CTX MA_HASH_CTX;
#elif defined(HAVE_GNUTLS)
typedef struct {
void *ctx;
const struct nettle_hash *hash;
} MA_HASH_CTX;
#endif
/**
@brief acquire and initialize new hash context
@param[in] algorithm hash algorithm
@return hash context on success, NULL on error
*/
MA_HASH_CTX *ma_hash_new(unsigned int algorithm);
/**
@brief release and deinitializes a hash context
@param[in] hash context
@return void
*/
void ma_hash_free(MA_HASH_CTX *ctx);
/**
@brief hashes len bytes of data into the hash context.
This function can be called several times on same context to
hash additional data.
@param[in] ctx hash context
@param[in] buffer data buffer
@param[in] len size of buffer
@return void
*/
void ma_hash_input(MA_HASH_CTX *ctx,
const unsigned char *buffer,
size_t len);
/**
@brief retrieves the hash value from hash context
@param[in] ctx hash context
@param[out] digest digest containing hash value
@return void
*/
void ma_hash_result(MA_HASH_CTX *ctx, unsigned char *digest);
/**
@brief returns digest size for a given hash algorithm
@param[in] hash algorithm
@returns digest size or 0 on error
*/
static inline size_t ma_hash_digest_size(unsigned int hash_alg)
{
switch(hash_alg) {
case MA_HASH_MD5:
return MA_MD5_HASH_SIZE;
case MA_HASH_SHA1:
return MA_SHA1_HASH_SIZE;
case MA_HASH_SHA224:
return MA_SHA224_HASH_SIZE;
case MA_HASH_SHA256:
return MA_SHA256_HASH_SIZE;
case MA_HASH_SHA384:
return MA_SHA384_HASH_SIZE;
case MA_HASH_SHA512:
return MA_SHA512_HASH_SIZE;
default:
return 0;
}
}
/**
@brief function to compute hash from buffer.
@param[in] hash_alg hash algorithm
@param[in] buffer buffer
@param[in] buffer_leng length of buffer
@param[out] digest computed hash digest
@return void
*/
static inline void ma_hash(unsigned int algorithm,
const unsigned char *buffer,
size_t buffer_length,
unsigned char *digest)
{
MA_HASH_CTX *ctx= NULL;
ctx= ma_hash_new(algorithm);
ma_hash_input(ctx, buffer, buffer_length);
ma_hash_result(ctx, digest);
ma_hash_free(ctx);
}
#endif /* _ma_crypt_h_ */
|