summaryrefslogtreecommitdiff
path: root/src/libstrongswan/credentials/keys/public_key.h
blob: 62dbe4303382bcc66623ec7cb7e8901c76042815 (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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
 * Copyright (C) 2007 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.
 *
 * $Id: public_key.h 4051 2008-06-10 09:08:27Z tobias $
 */
 
/**
 * @defgroup public_key public_key
 * @{ @ingroup keys
 */

#ifndef PUBLIC_KEY_H_
#define PUBLIC_KEY_H_

typedef struct public_key_t public_key_t;
typedef enum key_type_t key_type_t;
typedef enum key_id_type_t key_id_type_t;
typedef enum signature_scheme_t signature_scheme_t;

#include <library.h>
#include <utils/identification.h>

/**
 * Type of a key pair, the used crypto system
 */
enum key_type_t {
	/** key type wildcard */
	KEY_ANY,
	/** RSA crypto system as in PKCS#1 */
	KEY_RSA,
	/** ECDSA as in ANSI X9.62 */
	KEY_ECDSA,
	/** DSS, ElGamal, ... */
};

/**
 * Enum names for key_type_t
 */
extern enum_name_t *key_type_names;

/**
 * Signature scheme for signature creation
 *
 * EMSA-PKCS1 signatures are from the PKCS#1 standard. They include
 * the ASN1-OID of the used hash algorithm.
 */
enum signature_scheme_t {
	/** default scheme of that underlying crypto system */
	SIGN_DEFAULT,
	/** EMSA-PKCS1 with MD5  */
	SIGN_RSA_EMSA_PKCS1_MD5,
	/** EMSA-PKCS1 signature as in PKCS#1 standard using SHA1 as hash.  */
	SIGN_RSA_EMSA_PKCS1_SHA1,
	/** EMSA-PKCS1 signature as in PKCS#1 standard using SHA256 as hash. */
	SIGN_RSA_EMSA_PKCS1_SHA256,
	/** EMSA-PKCS1 signature as in PKCS#1 standard using SHA384 as hash. */
	SIGN_RSA_EMSA_PKCS1_SHA384,
	/** EMSA-PKCS1 signature as in PKCS#1 standard using SHA512 as hash. */
	SIGN_RSA_EMSA_PKCS1_SHA512,
	/** ECDSA using SHA-1 as hash. */
	SIGN_ECDSA_WITH_SHA1,
	/** ECDSA with SHA-256 on the P-256 curve as in RFC 4754 */
	SIGN_ECDSA_256,
	/** ECDSA with SHA-384 on the P-384 curve as in RFC 4754 */
	SIGN_ECDSA_384,
	/** ECDSA with SHA-512 on the P-521 curve as in RFC 4754 */
	SIGN_ECDSA_521,
};

/**
 * Enum names for signature_scheme_t
 */
extern enum_name_t *signature_scheme_names;

/**
 * Abstract interface of a public key.
 */
struct public_key_t {

	/**
	 * Get the key type.
	 *
	 * @return			type of the key
	 */
	key_type_t (*get_type)(public_key_t *this);
	
	/**
	 * Verifies a signature against a chunk of data.
	 *
	 * @param scheme	signature scheme to use for verification, may be default
	 * @param data		data to check signature against
	 * @param signature	signature to check
	 * @return			TRUE if signature matches
	 */
	bool (*verify)(public_key_t *this, signature_scheme_t scheme, 
				   chunk_t data, chunk_t signature);
	
	/**
	 * Encrypt a chunk of data.
	 *
	 * @param crypto	chunk containing plaintext data
	 * @param plain		where to allocate encrypted data
	 * @return 			TRUE if data successfully encrypted
	 */
	bool (*encrypt)(public_key_t *this, chunk_t crypto, chunk_t *plain);
	
	/**
	 * Get the strength of the key in bytes.
	 * 
	 * @return			strength of the key in bytes
	 */
	size_t (*get_keysize) (public_key_t *this);

	/**
	 * Get a unique key identifier, such as a hash over the key.
	 * 
	 * @param type		type of the key ID to get
	 * @return			unique ID of the key as identification_t, or NULL
	 */
	identification_t* (*get_id) (public_key_t *this, id_type_t type);
	
	/**
	 * Get an encoded form of the key.
	 *
	 * @todo Do we need a encoding type specification?
	 *
	 * @return			allocated chunk containing encoded key
	 */
	chunk_t (*get_encoding)(public_key_t *this);	
	
	/**
	 * Increase the refcount of the key.
	 *
	 * @return			this with an increased refcount
	 */
	public_key_t* (*get_ref)(public_key_t *this);
	
	/**
	 * Destroy a public_key instance.
	 */
	void (*destroy)(public_key_t *this);
};

#endif /* PUBLIC_KEY_H_ @} */