encryption_infoton.cpp

Go to the documentation of this file.
00001 #ifndef ENCRYPTION_INFOTON_IMPLEMENTATION_FILE
00002 #define ENCRYPTION_INFOTON_IMPLEMENTATION_FILE
00003 
00004 /*****************************************************************************\
00005 *                                                                             *
00006 *  Name   : encryption_infoton                                                *
00007 *  Author : Chris Koeritz                                                     *
00008 *                                                                             *
00009 *******************************************************************************
00010 * Copyright (c) 2004-$now By Author.  This program is free software; you can  *
00011 * redistribute it and/or modify it under the terms of the GNU General Public  *
00012 * License as published by the Free Software Foundation; either version 2 of   *
00013 * the License or (at your option) any later version.  This is online at:      *
00014 *     http://www.fsf.org/copyleft/gpl.html                                    *
00015 * Please send any updates to: fred@gruntose.com                               *
00016 \*****************************************************************************/
00017 
00018 #ifndef OMIT_CRYPTO_SUPPORT
00019 
00020 #include "encryption_infoton.h"
00021 
00022 #include <basis/byte_array.h>
00023 #include <basis/log_base.h>
00024 #include <basis/mutex.h>
00025 #include <basis/function.h>
00026 #include <data_struct/static_memory_gremlin.h>
00027 #include <crypto/blowfish_crypto.h>
00028 #include <crypto/rsa_crypto.h>
00029 #include <octopus/tentacle.h>
00030 #include <textual/byte_format.h>
00031 
00032 const int encryption_infoton::BLOWFISH_KEY_SIZE = 314;
00033   // our key size is almost double the recommended key size (168 bits).
00034   // this would take a very long time to crack using brute force.
00035 
00036 const int encryption_infoton::RSA_KEY_SIZE = 1480;
00037   // a little bit larger than the 1024 bit threshold.
00038 
00039 #undef LOG
00040 #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger(), s);
00041 
00042 encryption_infoton::encryption_infoton(const byte_array &pub_key,
00043     const byte_array &secret_blowfish)
00044 : infoton(encryption_classifier()),
00045   _public_key(pub_key),
00046   _encrypted_blowfish_key(secret_blowfish),
00047   _success(tentacle::NOT_FOUND)
00048 {}
00049 
00050 encryption_infoton::encryption_infoton(const encryption_infoton &to_copy)
00051 : object_base(),
00052   infoton(to_copy),
00053   _public_key(to_copy._public_key),
00054   _encrypted_blowfish_key(to_copy._encrypted_blowfish_key),
00055   _success(to_copy._success)
00056 {
00057 }
00058 
00059 encryption_infoton::~encryption_infoton() {}
00060 
00061 clonable *encryption_infoton::clone() const
00062 { return cloner<encryption_infoton>(*this); }
00063 
00064 encryption_infoton &encryption_infoton::operator =
00065     (const encryption_infoton &to_copy)
00066 {
00067   if (this == &to_copy) return *this;
00068   _public_key = to_copy._public_key;
00069   _encrypted_blowfish_key = to_copy._encrypted_blowfish_key;
00070   _success = to_copy._success;
00071   return *this;
00072 }
00073 
00074 const char *encryption_class_constant = "#octcod";
00075 
00076 SAFE_STATIC_CONST(string_array, encryption_infoton::encryption_classifier,
00077     (1, &encryption_class_constant))
00078 
00079 int encryption_infoton::packed_size() const
00080 {
00081   return sizeof(int)  // packed outcome.
00082       + _public_key.length() + sizeof(int)  // public key array.
00083       + _encrypted_blowfish_key.length() + sizeof(int);  // secret key array.
00084 }
00085 
00086 void encryption_infoton::pack(byte_array &packed_form) const
00087 {
00088   _success.pack(packed_form);
00089   basis::attach(packed_form, _public_key);
00090   basis::attach(packed_form, _encrypted_blowfish_key);
00091 }
00092 
00093 bool encryption_infoton::unpack(byte_array &packed_form)
00094 {
00095   if (!_success.unpack(packed_form)) return false;
00096   if (!basis::detach(packed_form, _public_key)) return false;
00097   if (!basis::detach(packed_form, _encrypted_blowfish_key)) return false;
00098   return true;
00099 }
00100 
00101 outcome encryption_infoton::prepare_blowfish_key(blowfish_crypto &new_key)
00102 {
00103   FUNCDEF("prepare_blowfish_key");
00104   _encrypted_blowfish_key.reset();  // clean out stuff to create.
00105   if (!_public_key.length()) {
00106     // wrong type of request being seen or something.
00107     _success = tentacle::BAD_INPUT;
00108     return _success;
00109   }
00110 
00111   RSA_crypto pub(_public_key);  // suck in the provided key.
00112   blowfish_crypto agreed_key(BLOWFISH_KEY_SIZE);  // random blowfish key.
00113   new_key = agreed_key;
00114 
00115   // now encrypt the new key for transit.
00116   bool worked = pub.public_encrypt(agreed_key.get_key(),
00117       _encrypted_blowfish_key);
00118   if (!worked) _success = tentacle::GARBAGE;  // lacking a better description.
00119   else _success = tentacle::OKAY;
00120   return _success;
00121 }
00122 
00123 outcome encryption_infoton::prepare_both_keys(RSA_crypto &private_key)
00124 {
00125   RSA_crypto priv(RSA_KEY_SIZE);  // generate random key.
00126   outcome to_return = prepare_public_key(priv);
00127   if (to_return == tentacle::OKAY) private_key = priv;
00128   return to_return;
00129 }
00130 
00131 outcome encryption_infoton::prepare_public_key(const RSA_crypto &private_key)
00132 {
00133   bool worked = private_key.public_key(_public_key);
00134   if (!worked) return tentacle::DISALLOWED;  // why would that ever fail?
00135   return tentacle::OKAY;
00136 }
00137 
00138 outcome encryption_infoton::extract_response(const RSA_crypto &private_key,
00139     blowfish_crypto &new_key) const
00140 {
00141   FUNCDEF("extract_response");
00142   if (_success != tentacle::OKAY) return _success;
00143   byte_array decrypted;
00144   bool worked = private_key.private_decrypt(_encrypted_blowfish_key, decrypted);
00145   if (!worked) return tentacle::BAD_INPUT;  // that one we hope is accurate.
00146   new_key.set_key(decrypted, BLOWFISH_KEY_SIZE);
00147   return tentacle::OKAY;
00148 }
00149 
00150 #endif // crypto support
00151 
00152 
00153 #endif //ENCRYPTION_INFOTON_IMPLEMENTATION_FILE
00154 

Generated on Fri Nov 28 04:29:32 2008 for HOOPLE Libraries by  doxygen 1.5.1