|
Packit |
aea12f |
#include <config.h>
|
|
Packit |
aea12f |
#include <stdint.h>
|
|
Packit |
aea12f |
#include <stdio.h>
|
|
Packit |
aea12f |
#include <string.h>
|
|
Packit |
aea12f |
#include <utils.h>
|
|
Packit |
aea12f |
#include <stdlib.h>
|
|
Packit |
aea12f |
#include <gnutls/gnutls.h>
|
|
Packit |
aea12f |
#include <gnutls/crypto.h>
|
|
Packit |
aea12f |
#include <gnutls/self-test.h>
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
#if !defined(HAVE_LIBNETTLE) || defined(WIN32)
|
|
Packit |
aea12f |
int main(int argc, char **argv)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
exit(77);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
#else
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
# include <nettle/sha1.h>
|
|
Packit |
aea12f |
# include <nettle/sha2.h>
|
|
Packit |
aea12f |
# include <nettle/hmac.h>
|
|
Packit |
aea12f |
# include <nettle/macros.h>
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
/* this tests whether the API to override ciphers works sanely.
|
|
Packit |
aea12f |
*/
|
|
Packit |
aea12f |
static int used = 0;
|
|
Packit |
aea12f |
static int used_mac = 0;
|
|
Packit |
aea12f |
static void tls_log_func(int level, const char *str)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
fprintf(stderr, "<%d>| %s", level, str);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
struct myhash_ctx {
|
|
Packit |
aea12f |
struct sha1_ctx sha1;
|
|
Packit |
aea12f |
};
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static int myhash_init(gnutls_digest_algorithm_t algo,
|
|
Packit |
aea12f |
void **_ctx)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
struct myhash_ctx *ctx;
|
|
Packit |
aea12f |
ctx = malloc(sizeof(struct myhash_ctx));
|
|
Packit |
aea12f |
if (ctx == NULL) {
|
|
Packit |
aea12f |
return GNUTLS_E_MEMORY_ERROR;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
sha1_init(&ctx->sha1);
|
|
Packit |
aea12f |
*_ctx = ctx;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
return 0;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static void myhash_deinit(void *ctx)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
free(ctx);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static int
|
|
Packit |
aea12f |
myhash_output(void *src_ctx, void *digest, size_t digestsize)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
struct myhash_ctx *ctx;
|
|
Packit |
aea12f |
ctx = src_ctx;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
used = 1;
|
|
Packit |
aea12f |
sha1_digest(&ctx->sha1, digestsize, digest);
|
|
Packit |
aea12f |
return 0;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static
|
|
Packit |
aea12f |
int myhash_fast(gnutls_digest_algorithm_t algo,
|
|
Packit |
aea12f |
const void *text, size_t text_size,
|
|
Packit |
aea12f |
void *digest)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
struct sha1_ctx ctx;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (algo != GNUTLS_DIG_SHA1)
|
|
Packit |
aea12f |
return -1;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
used = 1;
|
|
Packit |
aea12f |
sha1_init(&ctx;;
|
|
Packit |
aea12f |
sha1_update(&ctx, text_size, text);
|
|
Packit |
aea12f |
sha1_digest(&ctx, 20, digest);
|
|
Packit |
aea12f |
return 0;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static int
|
|
Packit |
aea12f |
myhash_update(void *_ctx, const void * data, size_t length)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
struct myhash_ctx *ctx = _ctx;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
sha1_update(&ctx->sha1, length, data);
|
|
Packit |
aea12f |
return 0;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
/* MAC */
|
|
Packit |
aea12f |
struct mymac_ctx {
|
|
Packit |
aea12f |
struct hmac_sha256_ctx sha256;
|
|
Packit |
aea12f |
};
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static int mymac_init(gnutls_mac_algorithm_t algo,
|
|
Packit |
aea12f |
void **ctx)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
*ctx = malloc(sizeof(struct mymac_ctx));
|
|
Packit |
aea12f |
if (*ctx == NULL) {
|
|
Packit |
aea12f |
return GNUTLS_E_MEMORY_ERROR;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
return 0;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static void mymac_deinit(void *ctx)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
free(ctx);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static int
|
|
Packit |
aea12f |
mymac_output(void *src_ctx, void *digest, size_t digestsize)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
struct mymac_ctx *ctx;
|
|
Packit |
aea12f |
ctx = src_ctx;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
hmac_sha256_digest(&ctx->sha256, digestsize, digest);
|
|
Packit |
aea12f |
return 0;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static
|
|
Packit |
aea12f |
int mymac_fast(gnutls_mac_algorithm_t algo,
|
|
Packit |
aea12f |
const void *nonce, size_t nonce_size, const void *key, size_t keysize,
|
|
Packit |
aea12f |
const void *text, size_t text_size,
|
|
Packit |
aea12f |
void *digest)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
struct hmac_sha256_ctx ctx;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (algo != GNUTLS_MAC_SHA256)
|
|
Packit |
aea12f |
return -1;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
used_mac = 1;
|
|
Packit |
aea12f |
hmac_sha256_set_key(&ctx, keysize, key);
|
|
Packit |
aea12f |
hmac_sha256_update(&ctx, text_size, text);
|
|
Packit |
aea12f |
hmac_sha256_digest(&ctx, 32, digest);
|
|
Packit |
aea12f |
return 0;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static int
|
|
Packit |
aea12f |
mymac_update(void *_ctx, const void * data, size_t length)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
struct mymac_ctx *ctx;
|
|
Packit |
aea12f |
ctx = _ctx;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
used_mac = 1;
|
|
Packit |
aea12f |
hmac_sha256_update(&ctx->sha256, length, data);
|
|
Packit |
aea12f |
return 0;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static int
|
|
Packit |
aea12f |
mymac_setkey(void *_ctx, const void * key, size_t length)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
struct mymac_ctx *ctx;
|
|
Packit |
aea12f |
ctx = _ctx;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
hmac_sha256_set_key(&ctx->sha256, length, key);
|
|
Packit |
aea12f |
return 0;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
int main(int argc, char **argv)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
int ret;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_global_set_log_function(tls_log_func);
|
|
Packit |
aea12f |
if (argc > 1)
|
|
Packit |
aea12f |
gnutls_global_set_log_level(4711);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
ret = gnutls_crypto_register_digest(GNUTLS_DIG_SHA1, 1,
|
|
Packit |
aea12f |
myhash_init,
|
|
Packit |
aea12f |
myhash_update,
|
|
Packit |
aea12f |
myhash_output,
|
|
Packit |
aea12f |
myhash_deinit,
|
|
Packit |
aea12f |
myhash_fast);
|
|
Packit |
aea12f |
if (ret < 0) {
|
|
Packit |
aea12f |
fprintf(stderr, "%d: cannot register hash\n", __LINE__);
|
|
Packit |
aea12f |
exit(1);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
ret = gnutls_crypto_register_mac(GNUTLS_MAC_SHA256, 1,
|
|
Packit |
aea12f |
mymac_init,
|
|
Packit |
aea12f |
mymac_setkey,
|
|
Packit |
aea12f |
NULL,
|
|
Packit |
aea12f |
mymac_update,
|
|
Packit |
aea12f |
mymac_output,
|
|
Packit |
aea12f |
mymac_deinit,
|
|
Packit |
aea12f |
mymac_fast);
|
|
Packit |
aea12f |
if (ret < 0) {
|
|
Packit |
aea12f |
fprintf(stderr, "%d: cannot register hash\n", __LINE__);
|
|
Packit |
aea12f |
exit(1);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
global_init();
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (gnutls_digest_self_test(0, GNUTLS_DIG_SHA1) < 0)
|
|
Packit |
aea12f |
return 1;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (used == 0) {
|
|
Packit |
aea12f |
fprintf(stderr, "The hash algorithm was not used\n");
|
|
Packit |
aea12f |
exit(1);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (gnutls_mac_self_test(0, GNUTLS_MAC_SHA256) < 0)
|
|
Packit |
aea12f |
return 1;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (used_mac == 0) {
|
|
Packit |
aea12f |
fprintf(stderr, "The MAC algorithm was not used\n");
|
|
Packit |
aea12f |
exit(1);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_global_deinit();
|
|
Packit |
aea12f |
return 0;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
#endif
|