tracking down insane socket tester bugs on windoze, where the main culprit so far...
[feisty_meow.git] / nucleus / 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;
23 typedef struct rsa_st RSA;
24
25 namespace crypto {
26
27 //! Supports public key encryption and decryption.
28 /*!
29   This class uses the OpenSSL package's support for RSA encryption.
30 */
31
32 class rsa_crypto : public virtual basis::nameable
33 {
34 public:
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. */
39
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. */
46
47   rsa_crypto(RSA *key);
48     //!< starts with a pre-existing "key" in the low-level form.
49
50   rsa_crypto(const rsa_crypto &to_copy);
51
52   virtual ~rsa_crypto();
53
54   const rsa_crypto &operator = (const rsa_crypto &to_copy);
55
56   DEFINE_CLASS_NAME("rsa_crypto");
57
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
62     only copy. */
63
64   bool set_key(RSA *key);
65     //!< sets our new "key".
66     /*!< this must be a valid key created via the RSA algorithms. */
67
68   bool check_key(RSA *key);
69     //!< checks the RSA "key" provided for validity.
70
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".
78
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".
86
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. */
92
93   static RSA *generate_key(int key_size);
94     //!< creates a random RSA key using the lower-level openssl methods.
95
96 private:
97   RSA *_key;  //!< our internal key.
98 };
99
100 } //namespace.
101
102 #endif
103