Use bits instead of bytes for a private/public key
[strongswan.git] / src / libstrongswan / credentials / keys / private_key.h
1 /*
2 * Copyright (C) 2007 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 /**
17 * @defgroup private_key private_key
18 * @{ @ingroup keys
19 */
20
21 #ifndef PRIVATE_KEY_H_
22 #define PRIVATE_KEY_H_
23
24 typedef struct private_key_t private_key_t;
25
26 #include <credentials/cred_encoding.h>
27 #include <credentials/keys/public_key.h>
28
29 /**
30 * Abstract private key interface.
31 */
32 struct private_key_t {
33
34 /**
35 * Get the key type.
36 *
37 * @return type of the key
38 */
39 key_type_t (*get_type)(private_key_t *this);
40
41 /**
42 * Create a signature over a chunk of data.
43 *
44 * @param scheme signature scheme to use
45 * @param data chunk of data to sign
46 * @param signature where to allocate created signature
47 * @return TRUE if signature created
48 */
49 bool (*sign)(private_key_t *this, signature_scheme_t scheme,
50 chunk_t data, chunk_t *signature);
51 /**
52 * Decrypt a chunk of data.
53 *
54 * @param scheme expected encryption scheme used
55 * @param crypto chunk containing encrypted data
56 * @param plain where to allocate decrypted data
57 * @return TRUE if data decrypted and plaintext allocated
58 */
59 bool (*decrypt)(private_key_t *this, encryption_scheme_t scheme,
60 chunk_t crypto, chunk_t *plain);
61
62 /**
63 * Get the strength of the key in bits.
64 *
65 * @return strength of the key in bits
66 */
67 int (*get_keysize) (private_key_t *this);
68
69 /**
70 * Get the public part from the private key.
71 *
72 * @return public key
73 */
74 public_key_t* (*get_public_key)(private_key_t *this);
75
76 /**
77 * Check if two private keys are equal.
78 *
79 * @param other other private key
80 * @return TRUE, if equality
81 */
82 bool (*equals) (private_key_t *this, private_key_t *other);
83
84 /**
85 * Check if a private key belongs to a public key.
86 *
87 * @param public public key
88 * @return TRUE, if keys belong together
89 */
90 bool (*belongs_to) (private_key_t *this, public_key_t *public);
91
92 /**
93 * Get the fingerprint of the key.
94 *
95 * @param type type of fingerprint, one of KEYID_*
96 * @param fp fingerprint, points to internal data
97 * @return TRUE if fingerprint type supported
98 */
99 bool (*get_fingerprint)(private_key_t *this, cred_encoding_type_t type,
100 chunk_t *fp);
101
102 /**
103 * Check if a key has a given fingerprint of any kind.
104 *
105 * @param fp fingerprint to check
106 * @return TRUE if key has given fingerprint
107 */
108 bool (*has_fingerprint)(private_key_t *this, chunk_t fp);
109
110 /**
111 * Get the key in an encoded form as a chunk.
112 *
113 * @param type type of the encoding, one of PRIVKEY_*
114 * @param encoding encoding of the key, allocated
115 * @return TRUE if encoding supported
116 */
117 bool (*get_encoding)(private_key_t *this, cred_encoding_type_t type,
118 chunk_t *encoding);
119
120 /**
121 * Increase the refcount to this private key.
122 *
123 * @return this, with an increased refcount
124 */
125 private_key_t* (*get_ref)(private_key_t *this);
126
127 /**
128 * Decrease refcount, destroy private_key if no more references.
129 */
130 void (*destroy)(private_key_t *this);
131 };
132
133 /**
134 * Generic private key equals() implementation, usable by implementors.
135 *
136 * @param this first key to compare
137 * @param other second key to compare
138 * @return TRUE if this is equal to other
139 */
140 bool private_key_equals(private_key_t *this, private_key_t *other);
141
142 /**
143 * Generic private key belongs_to() implementation, usable by implementors.
144 *
145 * @param private private key to check
146 * @param public public key to compare
147 * @return TRUE if this is equal to other
148 */
149 bool private_key_belongs_to(private_key_t *private, public_key_t *public);
150
151 /**
152 * Generic private key has_fingerprint() implementation, usable by implementors.
153 *
154 * @param this key to check fingerprint
155 * @param fingerprint fingerprint to check
156 * @return TRUE if key has given fingerprint
157 */
158 bool private_key_has_fingerprint(private_key_t *this, chunk_t fingerprint);
159
160 #endif /** PRIVATE_KEY_H_ @}*/