X-Git-Url: https://feistymeow.org/gitweb/?a=blobdiff_plain;f=nucleus%2Flibrary%2Fcrypto%2Frsa_crypto.h;fp=nucleus%2Flibrary%2Fcrypto%2Frsa_crypto.h;h=e43f1bdb7e5632f0762ab1a8864afd3136f68077;hb=457b128b77b5b4a0b7dd3094de543de2ce1477ad;hp=0000000000000000000000000000000000000000;hpb=32d7caf45d886d0d24e69eea00511c7815ac15d0;p=feisty_meow.git diff --git a/nucleus/library/crypto/rsa_crypto.h b/nucleus/library/crypto/rsa_crypto.h new file mode 100644 index 00000000..e43f1bdb --- /dev/null +++ b/nucleus/library/crypto/rsa_crypto.h @@ -0,0 +1,102 @@ +#ifndef RSA_CRYPTO_CLASS +#define RSA_CRYPTO_CLASS + +/*****************************************************************************\ +* * +* Name : RSA public key encryption * +* Author : Chris Koeritz * +* * +******************************************************************************* +* Copyright (c) 2005-$now By Author. This program is free software; you can * +* redistribute it and/or modify it under the terms of the GNU General Public * +* License as published by the Free Software Foundation; either version 2 of * +* the License or (at your option) any later version. This is online at: * +* http://www.fsf.org/copyleft/gpl.html * +* Please send any updates to: fred@gruntose.com * +\*****************************************************************************/ + +#include +#include + +// forward. +struct rsa_st; + +namespace crypto { + +//! Supports public key encryption and decryption. +/*! + This class uses the OpenSSL package's support for RSA encryption. +*/ + +class rsa_crypto : public virtual basis::nameable +{ +public: + rsa_crypto(int key_size); + //!< constructs using a randomized private key of the "key_size". + /*!< the "key_size" must be at least 1024 bits for acceptable security. + smaller keys are considered insecure. */ + + rsa_crypto(const basis::byte_array &key); + //!< constructs with the specified "key" as our private key. + /*!< the "key" is used for encryption rather than generating a random one. + the key is only valid if it was created with this class. also, if the key + is a public key, then only the public_encryption and public_decryption + methods will be available. */ + + rsa_crypto(rsa_st *key); + //!< starts with a pre-existing "key" in the low-level form. + + rsa_crypto(const rsa_crypto &to_copy); + + virtual ~rsa_crypto(); + + const rsa_crypto &operator = (const rsa_crypto &to_copy); + + DEFINE_CLASS_NAME("rsa_crypto"); + + bool set_key(basis::byte_array &key); + //!< resets this object's key to "key". + /*!< the key is only valid if this class created it. note: the "key" + is destructively consumed during the set method; do not pass in your + only copy. */ + + bool set_key(rsa_st *key); + //!< sets our new "key". + /*!< this must be a valid key created via the RSA algorithms. */ + + bool check_key(rsa_st *key); + //!< checks the RSA "key" provided for validity. + + bool public_encrypt(const basis::byte_array &source, basis::byte_array &target) const; + //!< encrypts "source" using our public key and stores it in "target". + /*!< public_encrypt and private_decrypt are a pair. an untrusted user can + encrypt with the public key and only the possessor of the private key + should be able to decrypt it. */ + bool private_decrypt(const basis::byte_array &source, basis::byte_array &target) const; + //!< decrypts "source" using our private key and stores it in "target". + + bool private_encrypt(const basis::byte_array &source, basis::byte_array &target) const; + //!< encrypts "source" using our private key and stores it in "target". + /*!< private_encrypt and public_decrypt are also a pair. the trusted + user with the private key can create encrypted chunks that anyone with + the public key can decrypt. */ + bool public_decrypt(const basis::byte_array &source, basis::byte_array &target) const; + //!< decrypts "source" using our public key and stores it in "target". + + bool public_key(basis::byte_array &pubkey) const; + //!< makes a copy of the public key held here. + bool private_key(basis::byte_array &privkey) const; + //!< makes a copy of the private key held here. + /*!< the private key should never be exposed to anyone else. */ + + static rsa_st *generate_key(int key_size); + //!< creates a random RSA key using the lower-level openssl methods. + +private: + rsa_st *_key; //!< our internal key. +}; + +} //namespace. + +#endif +