first check-in of feisty meow codebase. many things broken still due to recent
[feisty_meow.git] / core / library / crypto / blowfish_crypto.h
1 #ifndef BLOWFISH_CRYPTO_CLASS
2 #define BLOWFISH_CRYPTO_CLASS
3
4 /*****************************************************************************\
5 *                                                                             *
6 *  Name   : blowfish 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 namespace crypto {
22
23 //! Provides BlowFish encryption on byte_arrays using the OpenSSL package.
24
25 class blowfish_crypto : public virtual basis::root_object
26 {
27 public:
28   blowfish_crypto(int key_size);
29     //!< this will create a new random key of the "key_size", in bits.
30     /*!< the valid sizes are from 64 bits to 448 bits (we are forcing a
31     higher minimum than the published algorithm because we have found smaller
32     keys to be unreliable during decryption.  keys of 168 bits and larger
33     should be very secure.  it is said that if a billion computers each tried
34     a billion keys a second, then a 168 bit key would take 10 * 10^24 years
35     to break (using brute force).  this is essentially unbreakable since the
36     age of the universe is only 10 * 10^9 years so far. */
37
38   blowfish_crypto(const basis::byte_array &key, int key_size);
39     //!< uses a pre-existing "key".
40
41   blowfish_crypto(const blowfish_crypto &to_copy);  //!< copy constructor.
42
43   virtual ~blowfish_crypto();
44
45   blowfish_crypto &operator = (const blowfish_crypto &to_copy);
46
47   DEFINE_CLASS_NAME("blowfish_crypto");
48
49   int key_size() const;  // returns the size of our key, in bits.
50
51   static int minimum_key_size();
52     //!< returns the minimum key size (in bits) supported here.
53   static int maximum_key_size();
54     //!< returns the maximum key size (in bits) supported here.
55
56   const basis::byte_array &get_key() const;  //!< returns our current key.
57
58   bool set_key(const basis::byte_array &new_key, int key_size);
59     //!< sets the encryption key to "new_key".
60
61   static void generate_key(int size, basis::byte_array &new_key);
62     //!< creates a "new_key" of the "size" (in bits) specified.
63
64   bool encrypt(const basis::byte_array &source, basis::byte_array &target) const;
65     //!< encrypts the "source" array into the "target" array.
66
67   bool decrypt(const basis::byte_array &source, basis::byte_array &target) const;
68     //!< decrypts the "target" array from the encrypted "source" array.
69
70   // seldom-needed methods...
71
72   static const basis::byte_array &init_vector();
73     //!< returns the initialization vector that is used by this class.
74     /*!< decryption of chunks that were encrypted by this class will require
75     the same init vector as this function returns.  this is mainly provided
76     for third-party applications that want to be able to decrypt interoperably
77     with this class.  if you are creating such an application but for some
78     reason cannot run this class in order to invoke this method, the vector
79     is created by the algorithm in this class's implementation file
80     (currently named blowfish_crypto.cpp). */
81
82 private:
83   int _key_size;  //!< number of bits in the key.
84   basis::byte_array *_key;  //!< our secret key.
85 };
86
87 } //namespace.
88
89 #endif
90