aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/algo_base
diff options
context:
space:
mode:
authorlloyd <[email protected]>2014-01-18 18:17:08 +0000
committerlloyd <[email protected]>2014-01-18 18:17:08 +0000
commitb96ad4c05c0a8f835b54ef4e2cff849c749409b0 (patch)
treed8a5805e6d85cd66d2336f624f0cbeaaf1d5bed3 /src/lib/algo_base
parent6b457468faa88180142de9bd2ba0fee90be43463 (diff)
Split off the keyed interfaces of transform to Keyed_Transform
Remove the unhelpful 'Algorithm' base class which had previously acted more or less as a global base.
Diffstat (limited to 'src/lib/algo_base')
-rw-r--r--src/lib/algo_base/algo_base.h41
-rw-r--r--src/lib/algo_base/sym_algo.h30
-rw-r--r--src/lib/algo_base/transform.h61
3 files changed, 71 insertions, 61 deletions
diff --git a/src/lib/algo_base/algo_base.h b/src/lib/algo_base/algo_base.h
deleted file mode 100644
index f757a9a83..000000000
--- a/src/lib/algo_base/algo_base.h
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
-* Algorithm Base Class
-* (C) 2010 Jack Lloyd
-*
-* Distributed under the terms of the Botan license
-*/
-
-#ifndef BOTAN_ALGO_BASE_CLASS_H__
-#define BOTAN_ALGO_BASE_CLASS_H__
-
-#include <botan/build.h>
-#include <string>
-
-namespace Botan {
-
-/**
-* This class represents an algorithm of some kind
-*/
-class BOTAN_DLL Algorithm
- {
- public:
- /**
- * Zeroize internal state
- */
- virtual void clear() = 0;
-
- /**
- * @return name of this algorithm
- */
- virtual std::string name() const = 0;
-
- Algorithm() {}
- Algorithm(const Algorithm&) = delete;
- Algorithm& operator=(const Algorithm&) = delete;
-
- virtual ~Algorithm() {}
- };
-
-}
-
-#endif
diff --git a/src/lib/algo_base/sym_algo.h b/src/lib/algo_base/sym_algo.h
index c937d08ff..0b3b21f5e 100644
--- a/src/lib/algo_base/sym_algo.h
+++ b/src/lib/algo_base/sym_algo.h
@@ -8,7 +8,6 @@
#ifndef BOTAN_SYMMETRIC_ALGORITHM_H__
#define BOTAN_SYMMETRIC_ALGORITHM_H__
-#include <botan/algo_base.h>
#include <botan/key_spec.h>
#include <botan/exceptn.h>
#include <botan/symkey.h>
@@ -19,9 +18,13 @@ namespace Botan {
/**
* This class represents a symmetric algorithm object.
*/
-class BOTAN_DLL SymmetricAlgorithm : public Algorithm
+class BOTAN_DLL SymmetricAlgorithm
{
public:
+ virtual ~SymmetricAlgorithm() {}
+
+ virtual void clear() = 0;
+
/**
* @return object describing limits on key size
*/
@@ -58,7 +61,15 @@ class BOTAN_DLL SymmetricAlgorithm : public Algorithm
* @param key the SymmetricKey to be set.
*/
void set_key(const SymmetricKey& key)
- { set_key(key.begin(), key.length()); }
+ {
+ set_key(key.begin(), key.length());
+ }
+
+ template<typename Alloc>
+ void set_key(const std::vector<byte, Alloc>& key)
+ {
+ set_key(&key[0], key.size());
+ }
/**
* Set the symmetric key of this object.
@@ -72,11 +83,8 @@ class BOTAN_DLL SymmetricAlgorithm : public Algorithm
key_schedule(key, length);
}
- template<typename Alloc>
- void set_key(const std::vector<byte, Alloc>& v)
- {
- set_key(&v[0], v.size());
- }
+ virtual std::string name() const = 0;
+
private:
/**
* Run the key schedule
@@ -86,12 +94,6 @@ class BOTAN_DLL SymmetricAlgorithm : public Algorithm
virtual void key_schedule(const byte key[], size_t length) = 0;
};
-/**
-* The two possible directions for cipher filters, determining whether they
-* actually perform encryption or decryption.
-*/
-enum Cipher_Dir { ENCRYPTION, DECRYPTION };
-
}
#endif
diff --git a/src/lib/algo_base/transform.h b/src/lib/algo_base/transform.h
index 5e59eb80c..229425efd 100644
--- a/src/lib/algo_base/transform.h
+++ b/src/lib/algo_base/transform.h
@@ -8,15 +8,19 @@
#ifndef BOTAN_TRANSFORM_H__
#define BOTAN_TRANSFORM_H__
-#include <botan/sym_algo.h>
+#include <botan/secmem.h>
+#include <botan/key_spec.h>
+#include <botan/exceptn.h>
+#include <botan/symkey.h>
#include <string>
+#include <vector>
namespace Botan {
/**
* Interface for general transformations on data
*/
-class BOTAN_DLL Transformation : public SymmetricAlgorithm
+class BOTAN_DLL Transformation
{
public:
/**
@@ -75,10 +79,6 @@ class BOTAN_DLL Transformation : public SymmetricAlgorithm
*/
virtual size_t default_nonce_length() const = 0;
- BOTAN_DEPRECATED("Use default_nonce_length")
- size_t default_nonce_size() const
- { return default_nonce_length(); }
-
/**
* Return true iff nonce_len is a valid length for the nonce
*/
@@ -92,9 +92,58 @@ class BOTAN_DLL Transformation : public SymmetricAlgorithm
*/
virtual std::string provider() const { return "core"; }
+ virtual std::string name() const = 0;
+
+ virtual void clear() = 0;
+
virtual ~Transformation() {}
};
+class BOTAN_DLL Keyed_Transform : public Transformation
+ {
+ public:
+ /**
+ * @return object describing limits on key size
+ */
+ virtual Key_Length_Specification key_spec() const = 0;
+
+ /**
+ * Check whether a given key length is valid for this algorithm.
+ * @param length the key length to be checked.
+ * @return true if the key length is valid.
+ */
+ bool valid_keylength(size_t length) const
+ {
+ return key_spec().valid_keylength(length);
+ }
+
+ template<typename Alloc>
+ void set_key(const std::vector<byte, Alloc>& key)
+ {
+ set_key(&key[0], key.size());
+ }
+
+ void set_key(const SymmetricKey& key)
+ {
+ set_key(key.begin(), key.length());
+ }
+
+ /**
+ * Set the symmetric key of this transform
+ * @param key contains the key material
+ * @param length in bytes of key param
+ */
+ void set_key(const byte key[], size_t length)
+ {
+ if(!valid_keylength(length))
+ throw Invalid_Key_Length(name(), length);
+ key_schedule(key, length);
+ }
+
+ private:
+ virtual void key_schedule(const byte key[], size_t length) = 0;
+ };
+
}
#endif