aboutsummaryrefslogtreecommitdiffstats
path: root/Source/charon/testcases/rsa_test.c
blob: 37f349558747adb214518232c485c3b88bf347bb (plain)
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
/**
 * @file rsa_test.h
 * 
 * @brief Tests for the hasher_t classes.
 * 
 */

/*
 * Copyright (C) 2005 Jan Hutter, Martin Willi
 * Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program 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 General Public License
 * for more details.
 */

#include <string.h>
 
#include "rsa_test.h"

#include <daemon.h>
#include <utils/allocator.h>
#include <utils/logger.h>


/* 
 * described in Header-File
 */
void test_rsa(tester_t *tester)
{
	rsa_private_key_t *private_key;
	rsa_public_key_t *public_key;
	chunk_t data, signature, private_key_chunk, public_key_chunk;
	logger_t *logger;
	status_t status;
	u_int8_t test_data[] = {
		0x01,0x02,0x03,0x04,
		0x01,0x02,0x03,0x04,
		0x01,0x02,0x03,0x04,
		0x01,0x02,0x03,0x04,
		0x01,0x02,0x03,0x04,
		0x01,0x02,0x03,0x04,
		0x01,0x02,0x03,0x04,
		0x01,0x02,0x03,0x04,
		0x01,0x02,0x03,0x04,
	};
	data.ptr = test_data;
	data.len = sizeof(test_data);
	
	logger = charon->logger_manager->create_logger(charon->logger_manager, TESTER, NULL);
	logger->enable_level(logger, FULL);
	
	private_key = rsa_private_key_create();
	
	private_key->generate_key(private_key, 1024);
	
	status = private_key->build_emsa_pkcs1_signature(private_key, HASH_MD5, data, &signature);
	tester->assert_true(tester, status == SUCCESS, "build emsa_pkcs1_signature");
	
	public_key = private_key->get_public_key(private_key);
	
	status = public_key->verify_emsa_pkcs1_signature(public_key, data, signature);
	tester->assert_true(tester, status == SUCCESS, "verify emsa_pkcs1_signature");
	
	public_key->get_key(public_key, &public_key_chunk);
	private_key->get_key(private_key, &private_key_chunk);
	
	logger->log_chunk(logger, RAW, "Public Key", &public_key_chunk);
	logger->log_chunk(logger, RAW, "Private Key", &private_key_chunk);
	
	
	allocator_free(public_key_chunk.ptr);
	allocator_free(private_key_chunk.ptr);
	allocator_free(signature.ptr);
	
	private_key->destroy(private_key);
	public_key->destroy(public_key);
	
}