2dbef04e0f3ffd05efa89fe73a2837f229caa0cd
[strongswan.git] / src / charon / plugins / unit_tester / tests / test_rsa_gen.c
1 /*
2 * Copyright (C) 2008 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include <library.h>
17 #include <daemon.h>
18
19 /*******************************************************************************
20 * RSA key generation and signature
21 ******************************************************************************/
22 bool test_rsa_gen()
23 {
24 char buf[] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08};
25 chunk_t data = chunk_from_buf(buf), sig;
26 private_key_t *private;
27 public_key_t *public;
28 u_int key_size;
29
30 for (key_size = 512; key_size <= 2048; key_size *= 2)
31 {
32 private = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, KEY_RSA,
33 BUILD_KEY_SIZE, key_size, BUILD_END);
34 if (!private)
35 {
36 DBG1(DBG_CFG, "generating %d bit RSA key failed");
37 return FALSE;
38 }
39 public = private->get_public_key(private);
40 if (!public)
41 {
42 DBG1(DBG_CFG, "generating public from private key failed");
43 return FALSE;
44 }
45 if (!private->sign(private, SIGN_RSA_EMSA_PKCS1_SHA1, data, &sig))
46 {
47 DBG1(DBG_CFG, "creating RSA signature failed");
48 return FALSE;
49 }
50 if (!public->verify(public, SIGN_RSA_EMSA_PKCS1_SHA1, data, sig))
51 {
52 DBG1(DBG_CFG, "verifying RSA signature failed");
53 return FALSE;
54 }
55 sig.ptr[sig.len-1]++;
56 if (public->verify(public, SIGN_RSA_EMSA_PKCS1_SHA1, data, sig))
57 {
58 DBG1(DBG_CFG, "verifying faked RSA signature succeeded!");
59 return FALSE;
60 }
61 free(sig.ptr);
62 public->destroy(public);
63 private->destroy(private);
64 }
65 return TRUE;
66 }
67