Blame tests/pkcs1-digest-info.c

Packit aea12f
/*
Packit aea12f
 * Copyright (C) 2016 Red Hat, Inc.
Packit aea12f
 *
Packit aea12f
 * Author: Nikos Mavrogiannopoulos
Packit aea12f
 *
Packit aea12f
 * This file is part of GnuTLS.
Packit aea12f
 *
Packit aea12f
 * GnuTLS is free software; you can redistribute it and/or modify it
Packit aea12f
 * under the terms of the GNU General Public License as published by
Packit aea12f
 * the Free Software Foundation; either version 3 of the License, or
Packit aea12f
 * (at your option) any later version.
Packit aea12f
 *
Packit aea12f
 * GnuTLS is distributed in the hope that it will be useful, but
Packit aea12f
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit aea12f
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit aea12f
 * General Public License for more details.
Packit aea12f
 *
Packit aea12f
 * You should have received a copy of the GNU General Public License
Packit aea12f
 * along with GnuTLS; if not, write to the Free Software Foundation,
Packit aea12f
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
Packit aea12f
 */
Packit aea12f
Packit aea12f
/* Parts copied from GnuTLS example programs. */
Packit aea12f
Packit aea12f
#ifdef HAVE_CONFIG_H
Packit aea12f
#include <config.h>
Packit aea12f
#endif
Packit aea12f
Packit aea12f
#include <stdio.h>
Packit aea12f
#include <stdlib.h>
Packit aea12f
Packit aea12f
#include <string.h>
Packit aea12f
#include <stdint.h>
Packit aea12f
#include <gnutls/gnutls.h>
Packit aea12f
#include <gnutls/crypto.h>
Packit aea12f
Packit aea12f
#include "utils.h"
Packit aea12f
Packit aea12f
static void encode(const char *test_name, gnutls_digest_algorithm_t hash, const gnutls_datum_t *raw, const gnutls_datum_t *expected)
Packit aea12f
{
Packit aea12f
	int ret;
Packit aea12f
	gnutls_datum_t out;
Packit aea12f
	gnutls_digest_algorithm_t thash;
Packit aea12f
	uint8_t digest[128];
Packit aea12f
	unsigned int digest_size;
Packit aea12f
Packit aea12f
	ret = gnutls_encode_ber_digest_info(hash, raw, &out;;
Packit aea12f
	if (ret < 0) {
Packit aea12f
		fail("%s: gnutls_encode_ber_digest_info: %s\n", test_name, gnutls_strerror(ret));
Packit aea12f
		exit(1);
Packit aea12f
	}
Packit aea12f
Packit aea12f
	if (expected->size!=out.size) {
Packit aea12f
		hexprint(out.data, out.size);
Packit aea12f
		fail("%s: gnutls_encode_ber_digest_info: output has incorrect size (%d, expected %d)\n", test_name, (int)out.size, expected->size);
Packit aea12f
		exit(1);
Packit aea12f
	}
Packit aea12f
Packit aea12f
	if (memcmp(expected->data, out.data, out.size) != 0) {
Packit aea12f
		hexprint(out.data, out.size);
Packit aea12f
		fail("%s: gnutls_encode_ber_digest_info: output does not match the expected\n", test_name);
Packit aea12f
		exit(1);
Packit aea12f
	}
Packit aea12f
Packit aea12f
	digest_size = sizeof(digest);
Packit aea12f
	ret = gnutls_decode_ber_digest_info(&out, &thash, digest, &digest_size);
Packit aea12f
	if (ret < 0) {
Packit aea12f
		fail("%s: gnutls_decode_ber_digest_info: %s\n", test_name, gnutls_strerror(ret));
Packit aea12f
		exit(1);
Packit aea12f
	}
Packit aea12f
Packit aea12f
	if (thash != hash) {
Packit aea12f
		fail("%s: gnutls_decode_ber_digest_info: wrong hash, got: %d, expected %d\n", test_name, (int)thash, (int)hash);
Packit aea12f
		exit(1);
Packit aea12f
	}
Packit aea12f
Packit aea12f
	if (raw->size!=digest_size) {
Packit aea12f
		fail("%s: gnutls_decode_ber_digest_info: output has incorrect size (%d, expected %d)\n", test_name, digest_size, raw->size);
Packit aea12f
		exit(1);
Packit aea12f
	}
Packit aea12f
Packit aea12f
	if (memcmp(raw->data, digest, digest_size) != 0) {
Packit aea12f
		fail("%s: gnutls_decode_ber_digest_info: output does not match the expected\n", test_name);
Packit aea12f
		exit(1);
Packit aea12f
	}
Packit aea12f
Packit aea12f
	gnutls_free(out.data);
Packit aea12f
Packit aea12f
	return;
Packit aea12f
}
Packit aea12f
Packit aea12f
static void decode(const char *test_name, gnutls_digest_algorithm_t hash, const gnutls_datum_t *raw, const gnutls_datum_t *di, int res)
Packit aea12f
{
Packit aea12f
	int ret;
Packit aea12f
	uint8_t digest[128];
Packit aea12f
	unsigned digest_size;
Packit aea12f
	gnutls_digest_algorithm_t thash;
Packit aea12f
Packit aea12f
	digest_size = sizeof(digest);
Packit aea12f
	ret = gnutls_decode_ber_digest_info(di, &thash, digest, &digest_size);
Packit aea12f
	if (res != ret) {
Packit aea12f
		fail("%s: gnutls_decode_ber_digest_info: %s\n", test_name, gnutls_strerror(ret));
Packit aea12f
		exit(1);
Packit aea12f
	}
Packit aea12f
Packit aea12f
	if (ret < 0) {
Packit aea12f
		return;
Packit aea12f
	}
Packit aea12f
Packit aea12f
	if (thash != hash) {
Packit aea12f
		fail("%s: gnutls_decode_ber_digest_info: wrong hash, got: %d, expected %d\n", test_name, (int)thash, (int)hash);
Packit aea12f
		exit(1);
Packit aea12f
	}
Packit aea12f
Packit aea12f
	if (raw->size!=digest_size) {
Packit aea12f
		fail("%s: gnutls_decode_ber_digest_info: output has incorrect size (%d, expected %d)\n", test_name, digest_size, raw->size);
Packit aea12f
		exit(1);
Packit aea12f
	}
Packit aea12f
Packit aea12f
	if (memcmp(raw->data, digest, digest_size) != 0) {
Packit aea12f
		fail("%s: gnutls_decode_ber_digest_info: output does not match the expected\n", test_name);
Packit aea12f
		exit(1);
Packit aea12f
	}
Packit aea12f
Packit aea12f
	return;
Packit aea12f
}
Packit aea12f
Packit aea12f
struct encode_tests_st {
Packit aea12f
	const char *name;
Packit aea12f
	gnutls_digest_algorithm_t hash;
Packit aea12f
	const gnutls_datum_t raw;
Packit aea12f
	const gnutls_datum_t di;
Packit aea12f
};
Packit aea12f
Packit aea12f
struct encode_tests_st encode_tests[] = {
Packit aea12f
	{
Packit aea12f
		.name = "rnd1",
Packit aea12f
		.hash = GNUTLS_DIG_SHA1,
Packit aea12f
		.raw = {(void*)"\xf6\x9a\x46\x8a\x84\x69\x7a\x28\x83\xda\x52\xcd\x60\x2f\x39\x78\xff\xa1\x32\x12", 20},
Packit aea12f
		.di = {(void*)"\x30\x21\x30\x09\x06\x05\x2b\x0e\x03\x02\x1a\x05\x00\x04\x14\xf6\x9a\x46\x8a\x84\x69\x7a\x28\x83\xda\x52\xcd\x60\x2f\x39\x78\xff\xa1\x32\x12",35}
Packit aea12f
	},
Packit aea12f
	{
Packit aea12f
		.name = "rnd2",
Packit aea12f
		.hash = GNUTLS_DIG_SHA256,
Packit aea12f
		.raw = {(void*)"\x0b\x68\xdf\x4b\x27\xac\xc5\xc5\x52\x43\x74\x32\x39\x5c\x1e\xf5\x6a\xe2\x19\x5a\x58\x75\x81\xa5\x6a\xf5\xbf\x98\x85\xe3\xf9\x25", 32},
Packit aea12f
		.di = {(void*)"\x30\x31\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x01\x05\x00\x04\x20\x0b\x68\xdf\x4b\x27\xac\xc5\xc5\x52\x43\x74\x32\x39\x5c\x1e\xf5\x6a\xe2\x19\x5a\x58\x75\x81\xa5\x6a\xf5\xbf\x98\x85\xe3\xf9\x25",51}
Packit aea12f
	}
Packit aea12f
};
Packit aea12f
Packit aea12f
struct decode_tests_st {
Packit aea12f
	const char *name;
Packit aea12f
	gnutls_digest_algorithm_t hash;
Packit aea12f
	const gnutls_datum_t raw;
Packit aea12f
	const gnutls_datum_t di;
Packit aea12f
	int res;
Packit aea12f
};
Packit aea12f
Packit aea12f
struct decode_tests_st decode_tests[] = {
Packit aea12f
	{
Packit aea12f
		.name = "dec-rnd1",
Packit aea12f
		.hash = GNUTLS_DIG_SHA1,
Packit aea12f
		.di = {(void*)"\x30\x21\x30\x09\x06\x05\x2b\x0e\x03\x02\x1a\x05\x00\x04\x14\xf6\x9a\x46\x8a\x84\x69\x7a\x28\x83\xda\x52\xcd\x60\x2f\x39\x78\xff\xa1\x32\x12",35},
Packit aea12f
		.raw = {(void*)"\xf6\x9a\x46\x8a\x84\x69\x7a\x28\x83\xda\x52\xcd\x60\x2f\x39\x78\xff\xa1\x32\x12", 20},
Packit aea12f
		.res = 0,
Packit aea12f
	},
Packit aea12f
	{
Packit aea12f
		.name = "dec-rnd2",
Packit aea12f
		.hash = GNUTLS_DIG_SHA256,
Packit aea12f
		.raw = {(void*)"\x0b\x68\xdf\x4b\x27\xac\xc5\xc5\x52\x43\x74\x32\x39\x5c\x1e\xf5\x6a\xe2\x19\x5a\x58\x75\x81\xa5\x6a\xf5\xbf\x98\x85\xe3\xf9\x25", 32},
Packit aea12f
		.di = {(void*)"\x30\x31\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x01\x05\x00\x04\x20\x0b\x68\xdf\x4b\x27\xac\xc5\xc5\x52\x43\x74\x32\x39\x5c\x1e\xf5\x6a\xe2\x19\x5a\x58\x75\x81\xa5\x6a\xf5\xbf\x98\x85\xe3\xf9\x25",51},
Packit aea12f
		.res = 0,
Packit aea12f
	},
Packit aea12f
	{
Packit aea12f
		.name = "dec-wrong-tag",
Packit aea12f
		.hash = GNUTLS_DIG_SHA256,
Packit aea12f
		.raw = {(void*)"\x0b\x68\xdf\x4b\x27\xac\xc5\xc5\x52\x43\x74\x32\x39\x5c\x1e\xf5\x6a\xe2\x19\x5a\x58\x75\x81\xa5\x6a\xf5\xbf\x98\x85\xe3\xf9\x25", 32},
Packit aea12f
		.di = {(void*)"\x31\x31\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x01\x05\x00\x04\x20\x0b\x68\xdf\x4b\x27\xac\xc5\xc5\x52\x43\x74\x32\x39\x5c\x1e\xf5\x6a\xe2\x19\x5a\x58\x75\x81\xa5\x6a\xf5\xbf\x98\x85\xe3\xf9\x25",51},
Packit aea12f
		.res = GNUTLS_E_ASN1_TAG_ERROR
Packit aea12f
	},
Packit aea12f
	{
Packit aea12f
		.name = "dec-wrong-der",
Packit aea12f
		.hash = GNUTLS_DIG_SHA256,
Packit aea12f
		.raw = {(void*)"\x0b\x68\xdf\x4b\x27\xac\xc5\xc5\x52\x43\x74\x32\x39\x5c\x1e\xf5\x6a\xe2\x19\x5a\x58\x75\x81\xa5\x6a\xf5\xbf\x98\x85\xe3\xf9\x25", 32},
Packit aea12f
		.di = {(void*)"\x30\x31\x30\x0c\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x01\x05\x00\x04\x20\x0b\x68\xdf\x4b\x27\xac\xc5\xc5\x52\x43\x74\x32\x39\x5c\x1e\xf5\x6a\xe2\x19\x5a\x58\x75\x81\xa5\x6a\xf5\xbf\x98\x86\xe3\xf9\x25",51},
Packit aea12f
		.res = GNUTLS_E_ASN1_DER_ERROR
Packit aea12f
	},
Packit aea12f
	{
Packit aea12f
		.name = "dec-wrong-hash",
Packit aea12f
		.hash = GNUTLS_DIG_SHA256,
Packit aea12f
		.raw = {(void*)"\x0b\x68\xdf\x4b\x27\xac\xc5\xc5\x52\x43\x74\x32\x39\x5c\x1e\xf5\x6a\xe2\x19\x5a\x58\x75\x81\xa5\x6a\xf5\xbf\x98\x85\xe3\xf9\x25", 32},
Packit aea12f
		.di = {(void*)"\x30\x31\x30\x0d\x06\x09\x61\x86\x48\x01\x65\x03\x04\x02\x01\x05\x00\x04\x20\x0b\x68\xdf\x4b\x27\xac\xc5\xc5\x52\x43\x74\x32\x39\x5c\x1e\xf5\x6a\xe2\x19\x5a\x58\x75\x81\xa5\x6a\xf5\xbf\x98\x86\xe3\xf9\x25",51},
Packit aea12f
		.res = GNUTLS_E_UNKNOWN_HASH_ALGORITHM
Packit aea12f
	},
Packit aea12f
};
Packit aea12f
Packit aea12f
void doit(void)
Packit aea12f
{
Packit aea12f
	unsigned i;
Packit aea12f
Packit aea12f
	for (i=0;i
Packit aea12f
		encode(encode_tests[i].name, encode_tests[i].hash, &encode_tests[i].raw, &encode_tests[i].di);
Packit aea12f
	}
Packit aea12f
Packit aea12f
	for (i=0;i
Packit aea12f
		decode(decode_tests[i].name, decode_tests[i].hash, &decode_tests[i].raw, &decode_tests[i].di, decode_tests[i].res);
Packit aea12f
	}
Packit aea12f
}
Packit aea12f