2 * Copyright (C) 2015 Tobias Brunner
3 * Copyright (C) 2007 Martin Willi
4 * Copyright (C) 2014-2015 Andreas Steffen
5 * HSR Hochschule fuer Technik Rapperswil
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 * @defgroup public_key public_key
26 typedef struct public_key_t public_key_t
;
27 typedef enum key_type_t key_type_t
;
28 typedef enum signature_scheme_t signature_scheme_t
;
29 typedef enum encryption_scheme_t encryption_scheme_t
;
32 #include <utils/identification.h>
33 #include <credentials/cred_encoding.h>
36 * Type of a key pair, the used crypto system
39 /** key type wildcard */
41 /** RSA crypto system as in PKCS#1 */
43 /** ECDSA as in ANSI X9.62 */
53 * Enum names for key_type_t
55 extern enum_name_t
*key_type_names
;
58 * Signature scheme for signature creation
60 * EMSA-PKCS1 signatures are defined in PKCS#1 standard.
61 * A prepended ASN.1 encoded digestInfo field contains the
62 * OID of the used hash algorithm.
64 enum signature_scheme_t
{
65 /** Unknown signature scheme */
67 /** EMSA-PKCS1_v1.5 signature over digest without digestInfo */
68 SIGN_RSA_EMSA_PKCS1_NULL
,
69 /** EMSA-PKCS1_v1.5 signature as in PKCS#1 using RSA and MD5 */
70 SIGN_RSA_EMSA_PKCS1_MD5
,
71 /** EMSA-PKCS1_v1.5 signature as in PKCS#1 using RSA and SHA-1 */
72 SIGN_RSA_EMSA_PKCS1_SHA1
,
73 /** EMSA-PKCS1_v1.5 signature as in PKCS#1 using RSA and SHA-224 */
74 SIGN_RSA_EMSA_PKCS1_SHA224
,
75 /** EMSA-PKCS1_v1.5 signature as in PKCS#1 using RSA and SHA-256 */
76 SIGN_RSA_EMSA_PKCS1_SHA256
,
77 /** EMSA-PKCS1_v1.5 signature as in PKCS#1 using RSA and SHA-384 */
78 SIGN_RSA_EMSA_PKCS1_SHA384
,
79 /** EMSA-PKCS1_v1.5 signature as in PKCS#1 using RSA and SHA-512 */
80 SIGN_RSA_EMSA_PKCS1_SHA512
,
81 /** ECDSA with SHA-1 using DER encoding as in RFC 3279 */
82 SIGN_ECDSA_WITH_SHA1_DER
,
83 /** ECDSA with SHA-256 using DER encoding as in RFC 3279 */
84 SIGN_ECDSA_WITH_SHA256_DER
,
85 /** ECDSA with SHA-384 using DER encoding as in RFC 3279 */
86 SIGN_ECDSA_WITH_SHA384_DER
,
87 /** ECDSA with SHA-1 using DER encoding as in RFC 3279 */
88 SIGN_ECDSA_WITH_SHA512_DER
,
89 /** ECDSA over precomputed digest, signature as in RFC 4754 */
91 /** ECDSA on the P-256 curve with SHA-256 as in RFC 4754 */
93 /** ECDSA on the P-384 curve with SHA-384 as in RFC 4754 */
95 /** ECDSA on the P-521 curve with SHA-512 as in RFC 4754 */
97 /** BLISS with SHA-2_256 */
98 SIGN_BLISS_WITH_SHA2_256
,
99 /** BLISS with SHA-2_384 */
100 SIGN_BLISS_WITH_SHA2_384
,
101 /** BLISS with SHA-2_512 */
102 SIGN_BLISS_WITH_SHA2_512
,
103 /** BLISS with SHA-3_256 */
104 SIGN_BLISS_WITH_SHA3_256
,
105 /** BLISS with SHA-3_384 */
106 SIGN_BLISS_WITH_SHA3_384
,
107 /** BLISS with SHA-3_512 */
108 SIGN_BLISS_WITH_SHA3_512
,
112 * Enum names for signature_scheme_t
114 extern enum_name_t
*signature_scheme_names
;
117 * Encryption scheme for public key data encryption.
119 enum encryption_scheme_t
{
120 /** Unknown encryption scheme */
122 /** RSAES-PKCS1-v1_5 as in PKCS#1 */
124 /** RSAES-OAEP as in PKCS#1, using SHA1 as hash, no label */
125 ENCRYPT_RSA_OAEP_SHA1
,
126 /** RSAES-OAEP as in PKCS#1, using SHA-224 as hash, no label */
127 ENCRYPT_RSA_OAEP_SHA224
,
128 /** RSAES-OAEP as in PKCS#1, using SHA-256 as hash, no label */
129 ENCRYPT_RSA_OAEP_SHA256
,
130 /** RSAES-OAEP as in PKCS#1, using SHA-384 as hash, no label */
131 ENCRYPT_RSA_OAEP_SHA384
,
132 /** RSAES-OAEP as in PKCS#1, using SHA-512 as hash, no label */
133 ENCRYPT_RSA_OAEP_SHA512
,
137 * Enum names for encryption_scheme_t
139 extern enum_name_t
*encryption_scheme_names
;
142 * Abstract interface of a public key.
144 struct public_key_t
{
149 * @return type of the key
151 key_type_t (*get_type
)(public_key_t
*this);
154 * Verifies a signature against a chunk of data.
156 * @param scheme signature scheme to use for verification, may be default
157 * @param data data to check signature against
158 * @param signature signature to check
159 * @return TRUE if signature matches
161 bool (*verify
)(public_key_t
*this, signature_scheme_t scheme
,
162 chunk_t data
, chunk_t signature
);
165 * Encrypt a chunk of data.
167 * @param scheme encryption scheme to use
168 * @param plain chunk containing plaintext data
169 * @param crypto where to allocate encrypted data
170 * @return TRUE if data successfully encrypted
172 bool (*encrypt
)(public_key_t
*this, encryption_scheme_t scheme
,
173 chunk_t plain
, chunk_t
*crypto
);
176 * Check if two public keys are equal.
178 * @param other other public key
179 * @return TRUE, if equality
181 bool (*equals
)(public_key_t
*this, public_key_t
*other
);
184 * Get the strength of the key in bits.
186 * @return strength of the key in bits
188 int (*get_keysize
) (public_key_t
*this);
191 * Get the fingerprint of the key.
193 * @param type type of fingerprint, one of KEYID_*
194 * @param fp fingerprint, points to internal data
195 * @return TRUE if fingerprint type supported
197 bool (*get_fingerprint
)(public_key_t
*this, cred_encoding_type_t type
,
201 * Check if a key has a given fingerprint of any kind.
203 * @param fp fingerprint to check
204 * @return TRUE if key has given fingerprint
206 bool (*has_fingerprint
)(public_key_t
*this, chunk_t fp
);
209 * Get the key in an encoded form as a chunk.
211 * @param type type of the encoding, one of PUBKEY_*
212 * @param encoding encoding of the key, allocated
213 * @return TRUE if encoding supported
215 bool (*get_encoding
)(public_key_t
*this, cred_encoding_type_t type
,
219 * Increase the refcount of the key.
221 * @return this with an increased refcount
223 public_key_t
* (*get_ref
)(public_key_t
*this);
226 * Destroy a public_key instance.
228 void (*destroy
)(public_key_t
*this);
232 * Generic public key equals() implementation, usable by implementors.
234 * @param public public key to check
235 * @param other key to compare
236 * @return TRUE if this is equal to other
238 bool public_key_equals(public_key_t
*public, public_key_t
*other
);
241 * Generic public key has_fingerprint() implementation, usable by implementors.
243 * @param public public key to check
244 * @param fingerprint fingerprint to check
245 * @return TRUE if key has given fingerprint
247 bool public_key_has_fingerprint(public_key_t
*public, chunk_t fingerprint
);
250 * Conversion of ASN.1 signature or hash OID to signature scheme.
252 * @param oid ASN.1 OID
253 * @return signature scheme, SIGN_UNKNOWN if OID is unsupported
255 signature_scheme_t
signature_scheme_from_oid(int oid
);
258 * Conversion of signature scheme to ASN.1 signature OID.
260 * @param scheme signature scheme
261 * @return ASN.1 OID, OID_UNKNOWN if not supported
263 int signature_scheme_to_oid(signature_scheme_t scheme
);
266 * Enumerate signature schemes that are appropriate for a key of the given type
269 * @param type type of the key
270 * @param size size or strength of the key
271 * @return enumerator over signature_scheme_t (increasing strength)
273 enumerator_t
*signature_schemes_for_key(key_type_t type
, int size
);
276 * Determine the type of key associated with a given signature scheme.
278 * @param scheme signature scheme
279 * @return key type (could be KEY_ANY)
281 key_type_t
key_type_from_signature_scheme(signature_scheme_t scheme
);
284 #endif /** PUBLIC_KEY_H_ @}*/