2 * @file rsa_private_key.h
4 * @brief Interface of rsa_private_key_t.
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23 #ifndef RSA_PRIVATE_KEY_H_
24 #define RSA_PRIVATE_KEY_H_
27 #include <definitions.h>
28 #include <crypto/rsa/rsa_public_key.h>
29 #include <crypto/hashers/hasher.h>
32 typedef struct rsa_private_key_t rsa_private_key_t
;
35 * @brief RSA private key with associated functions.
37 * Currently only supports signing using EMSA encoding.
40 * - rsa_private_key_create()
41 * - rsa_private_key_create_from_chunk()
42 * - rsa_private_key_create_from_file()
44 * @see rsa_public_key_t
46 * @todo Implement get_key(), save_key(), get_public_key()
50 struct rsa_private_key_t
{
53 * @brief Build a signature over a chunk using EMSA-PKCS1 encoding.
55 * This signature creates a hash using the specified hash algorithm, concatenates
56 * it with an ASN1-OID of the hash algorithm and runs the RSASP1 function
59 * @param this calling object
60 * @param hash_algorithm hash algorithm to use for hashing
61 * @param data data to sign
62 * @param[out] signature allocated signature
65 * - INVALID_STATE, if key not set
66 * - NOT_SUPPORTED, if hash algorithm not supported
68 status_t (*build_emsa_pkcs1_signature
) (rsa_private_key_t
*this, hash_algorithm_t hash_algorithm
, chunk_t data
, chunk_t
*signature
);
71 * @brief Gets the key.
75 * @param this calling object
76 * @param key key (in a propriarity format)
79 * - INVALID_STATE, if key not set
81 status_t (*get_key
) (rsa_private_key_t
*this, chunk_t
*key
);
84 * @brief Saves a key to a file.
88 * @param this calling object
89 * @param file file to which the key should be written.
90 * @return NOT_SUPPORTED
92 status_t (*save_key
) (rsa_private_key_t
*this, char *file
);
95 * @brief Generate a new key.
97 * Generates a new private_key with specified key size
99 * @param this calling object
100 * @param key_size size of the key in bits
103 * - INVALID_ARG if key_size invalid
105 status_t (*generate_key
) (rsa_private_key_t
*this, size_t key_size
);
108 * @brief Create a rsa_public_key_t with the public
111 * @param this calling object
114 rsa_public_key_t
*(*get_public_key
) (rsa_private_key_t
*this);
117 * @brief Check if a private key belongs to a public key.
119 * Compares the public part of the private key with the
120 * public key, return TRUE if it equals.
122 * @param this private key
123 * @param public public key
124 * @return TRUE, if keys belong together
126 bool (*belongs_to
) (rsa_private_key_t
*this, rsa_public_key_t
*public);
129 * @brief Clone the private key.
131 * @param this private key to clone
132 * @return clone of this
134 rsa_private_key_t
*(*clone
) (rsa_private_key_t
*this);
137 * @brief Destroys the private key.
139 * @param this private key to destroy
141 void (*destroy
) (rsa_private_key_t
*this);
145 * @brief Generate a new RSA key with specified key lenght.
147 * @param key_size size of the key in bits
148 * @return generated rsa_private_key_t.
152 rsa_private_key_t
*rsa_private_key_create(size_t key_size
);
155 * @brief Load an RSA private key from a chunk.
157 * Load a key from a chunk, encoded as described in PKCS#1
158 * (ASN1 DER encoded).
160 * @param chunk chunk containing the DER encoded key
161 * @return loaded rsa_private_key_t, or NULL
165 rsa_private_key_t
*rsa_private_key_create_from_chunk(chunk_t chunk
);
168 * @brief Load an RSA private key from a file.
170 * Load a key from a file, which is either in a unencrypted binary
171 * format (DER), or in a (encrypted) PEM format. The supplied
172 * passphrase is used to decrypt an ecrypted key.
174 * @param filename filename which holds the key
175 * @param passphrase optional passphase for decryption
176 * @return loaded rsa_private_key_t, or NULL
178 * @todo Implement PEM file loading
179 * @todo Implement key decryption
183 rsa_private_key_t
*rsa_private_key_create_from_file(char *filename
, char *passphrase
);
185 #endif /*RSA_PRIVATE_KEY_H_*/