first check-in of feisty meow codebase. many things broken still due to recent
[feisty_meow.git] / core / library / crypto / rsa_crypto.h
1 #ifndef RSA_CRYPTO_CLASS
2 #define RSA_CRYPTO_CLASS
3
4 /*****************************************************************************\
5 *                                                                             *
6 *  Name   : RSA public key encryption                                         *
7 *  Author : Chris Koeritz                                                     *
8 *                                                                             *
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 \*****************************************************************************/
17
18 #include <basis/byte_array.h>
19 #include <basis/contracts.h>
20
21 // forward.
22 struct rsa_st;
23
24 namespace crypto {
25
26 //! Supports public key encryption and decryption.
27 /*!
28   This class uses the OpenSSL package's support for RSA encryption.
29 */
30
31 class rsa_crypto : public virtual basis::nameable
32 {
33 public:
34   rsa_crypto(int key_size);
35     //!< constructs using a randomized private key of the "key_size".
36     /*!< the "key_size" must be at least 1024 bits for acceptable security.
37     smaller keys are considered insecure. */
38
39   rsa_crypto(const basis::byte_array &key);
40     //!< constructs with the specified "key" as our private key.
41     /*!< the "key" is used for encryption rather than generating a random one.
42     the key is only valid if it was created with this class.  also, if the key
43     is a public key, then only the public_encryption and public_decryption
44     methods will be available. */
45
46   rsa_crypto(rsa_st *key);
47     //!< starts with a pre-existing "key" in the low-level form.
48
49   rsa_crypto(const rsa_crypto &to_copy);
50
51   virtual ~rsa_crypto();
52
53   const rsa_crypto &operator = (const rsa_crypto &to_copy);
54
55   DEFINE_CLASS_NAME("rsa_crypto");
56
57   bool set_key(basis::byte_array &key);
58     //!< resets this object's key to "key".
59     /*!< the key is only valid if this class created it.  note: the "key"
60     is destructively consumed during the set method; do not pass in your
61     only copy. */
62
63   bool set_key(rsa_st *key);
64     //!< sets our new "key".
65     /*!< this must be a valid key created via the RSA algorithms. */
66
67   bool check_key(rsa_st *key);
68     //!< checks the RSA "key" provided for validity.
69
70   bool public_encrypt(const basis::byte_array &source, basis::byte_array &target) const;
71     //!< encrypts "source" using our public key and stores it in "target".
72     /*!< public_encrypt and private_decrypt are a pair.  an untrusted user can
73     encrypt with the public key and only the possessor of the private key
74     should be able to decrypt it. */
75   bool private_decrypt(const basis::byte_array &source, basis::byte_array &target) const;
76     //!< decrypts "source" using our private key and stores it in "target".
77
78   bool private_encrypt(const basis::byte_array &source, basis::byte_array &target) const;
79     //!< encrypts "source" using our private key and stores it in "target".
80     /*!< private_encrypt and public_decrypt are also a pair.  the trusted
81     user with the private key can create encrypted chunks that anyone with
82     the public key can decrypt. */
83   bool public_decrypt(const basis::byte_array &source, basis::byte_array &target) const;
84     //!< decrypts "source" using our public key and stores it in "target".
85
86   bool public_key(basis::byte_array &pubkey) const;
87     //!< makes a copy of the public key held here.
88   bool private_key(basis::byte_array &privkey) const;
89     //!< makes a copy of the private key held here.
90     /*!< the private key should never be exposed to anyone else. */
91
92   static rsa_st *generate_key(int key_size);
93     //!< creates a random RSA key using the lower-level openssl methods.
94
95 private:
96   rsa_st *_key;  //!< our internal key.
97 };
98
99 } //namespace.
100
101 #endif
102