1 #ifndef RSA_CRYPTO_CLASS
2 #define RSA_CRYPTO_CLASS
4 /*****************************************************************************\
6 * Name : RSA public key encryption *
7 * Author : Chris Koeritz *
9 *******************************************************************************
10 * Copyright (c) 2005-$now By Author. This program is free software; you can *
11 * redistribute it and/or modify it under the terms of the GNU General Public *
12 * License as published by the Free Software Foundation; either version 2 of *
13 * the License or (at your option) any later version. This is online at: *
14 * http://www.fsf.org/copyleft/gpl.html *
15 * Please send any updates to: fred@gruntose.com *
16 \*****************************************************************************/
18 #include <basis/byte_array.h>
19 #include <basis/contracts.h>
21 #include <openssl/rsa.h>
28 //! Supports public key encryption and decryption.
30 This class uses the OpenSSL package's support for RSA encryption.
33 class rsa_crypto : public virtual basis::nameable
36 rsa_crypto(int key_size);
37 //!< constructs using a randomized private key of the "key_size".
38 /*!< the "key_size" must be at least 1024 bits for acceptable security.
39 smaller keys are considered insecure. */
41 rsa_crypto(const basis::byte_array &key);
42 //!< constructs with the specified "key" as our private key.
43 /*!< the "key" is used for encryption rather than generating a random one.
44 the key is only valid if it was created with this class. also, if the key
45 is a public key, then only the public_encryption and public_decryption
46 methods will be available. */
49 //!< starts with a pre-existing "key" in the low-level form.
51 rsa_crypto(const rsa_crypto &to_copy);
53 virtual ~rsa_crypto();
55 const rsa_crypto &operator = (const rsa_crypto &to_copy);
57 DEFINE_CLASS_NAME("rsa_crypto");
59 bool set_key(basis::byte_array &key);
60 //!< resets this object's key to "key".
61 /*!< the key is only valid if this class created it. note: the "key"
62 is destructively consumed during the set method; do not pass in your
65 bool set_key(RSA *key);
66 //!< sets our new "key".
67 /*!< this must be a valid key created via the RSA algorithms. */
69 bool check_key(RSA *key);
70 //!< checks the RSA "key" provided for validity.
72 bool public_encrypt(const basis::byte_array &source, basis::byte_array &target) const;
73 //!< encrypts "source" using our public key and stores it in "target".
74 /*!< public_encrypt and private_decrypt are a pair. an untrusted user can
75 encrypt with the public key and only the possessor of the private key
76 should be able to decrypt it. */
77 bool private_decrypt(const basis::byte_array &source, basis::byte_array &target) const;
78 //!< decrypts "source" using our private key and stores it in "target".
80 bool private_encrypt(const basis::byte_array &source, basis::byte_array &target) const;
81 //!< encrypts "source" using our private key and stores it in "target".
82 /*!< private_encrypt and public_decrypt are also a pair. the trusted
83 user with the private key can create encrypted chunks that anyone with
84 the public key can decrypt. */
85 bool public_decrypt(const basis::byte_array &source, basis::byte_array &target) const;
86 //!< decrypts "source" using our public key and stores it in "target".
88 bool public_key(basis::byte_array &pubkey) const;
89 //!< makes a copy of the public key held here.
90 bool private_key(basis::byte_array &privkey) const;
91 //!< makes a copy of the private key held here.
92 /*!< the private key should never be exposed to anyone else. */
94 static RSA *generate_key(int key_size);
95 //!< creates a random RSA key using the lower-level openssl methods.
98 RSA *_key; //!< our internal key.