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>
23 typedef struct rsa_st RSA;
27 //! Supports public key encryption and decryption.
29 This class uses the OpenSSL package's support for RSA encryption.
32 class rsa_crypto : public virtual basis::nameable
35 rsa_crypto(int key_size);
36 //!< constructs using a randomized private key of the "key_size".
37 /*!< the "key_size" must be at least 1024 bits for acceptable security.
38 smaller keys are considered insecure. */
40 rsa_crypto(const basis::byte_array &key);
41 //!< constructs with the specified "key" as our private key.
42 /*!< the "key" is used for encryption rather than generating a random one.
43 the key is only valid if it was created with this class. also, if the key
44 is a public key, then only the public_encryption and public_decryption
45 methods will be available. */
48 //!< starts with a pre-existing "key" in the low-level form.
50 rsa_crypto(const rsa_crypto &to_copy);
52 virtual ~rsa_crypto();
54 const rsa_crypto &operator = (const rsa_crypto &to_copy);
56 DEFINE_CLASS_NAME("rsa_crypto");
58 bool set_key(basis::byte_array &key);
59 //!< resets this object's key to "key".
60 /*!< the key is only valid if this class created it. note: the "key"
61 is destructively consumed during the set method; do not pass in your
64 bool set_key(RSA *key);
65 //!< sets our new "key".
66 /*!< this must be a valid key created via the RSA algorithms. */
68 bool check_key(RSA *key);
69 //!< checks the RSA "key" provided for validity.
71 bool public_encrypt(const basis::byte_array &source, basis::byte_array &target) const;
72 //!< encrypts "source" using our public key and stores it in "target".
73 /*!< public_encrypt and private_decrypt are a pair. an untrusted user can
74 encrypt with the public key and only the possessor of the private key
75 should be able to decrypt it. */
76 bool private_decrypt(const basis::byte_array &source, basis::byte_array &target) const;
77 //!< decrypts "source" using our private key and stores it in "target".
79 bool private_encrypt(const basis::byte_array &source, basis::byte_array &target) const;
80 //!< encrypts "source" using our private key and stores it in "target".
81 /*!< private_encrypt and public_decrypt are also a pair. the trusted
82 user with the private key can create encrypted chunks that anyone with
83 the public key can decrypt. */
84 bool public_decrypt(const basis::byte_array &source, basis::byte_array &target) const;
85 //!< decrypts "source" using our public key and stores it in "target".
87 bool public_key(basis::byte_array &pubkey) const;
88 //!< makes a copy of the public key held here.
89 bool private_key(basis::byte_array &privkey) const;
90 //!< makes a copy of the private key held here.
91 /*!< the private key should never be exposed to anyone else. */
93 static RSA *generate_key(int key_size);
94 //!< creates a random RSA key using the lower-level openssl methods.
97 RSA *_key; //!< our internal key.