diff --git a/engine/Cipher.cpp b/engine/Cipher.cpp new file mode 100644 index 00000000..df85698f --- /dev/null +++ b/engine/Cipher.cpp @@ -0,0 +1,77 @@ +/** + * Cipher.cpp + * This file is part of the YATE Project http://YATE.null.ro + * + * Yet Another Telephony Engine - a fully featured software PBX and IVR + * Copyright (C) 2004-2008 Null Team + * + * 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 program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "yatengine.h" + +using namespace TelEngine; + +const TokenDict Cipher::s_directions[] = +{ + { "bidir", Cipher::Bidir }, + { "encrypt", Cipher::Encrypt }, + { "decrypt", Cipher::Decrypt }, + { 0, 0 } +}; + +Cipher::~Cipher() +{ +} + +void* Cipher::getObject(const String& name) const +{ + if (name == "Cipher") + return const_cast(this); + return GenObject::getObject(name); +} + +bool Cipher::valid(Direction dir) const +{ + return true; +} + +unsigned int Cipher::initVectorSize() const +{ + return 0; +} + +unsigned int Cipher::bufferSize(unsigned int len) const +{ + unsigned int bSize = blockSize(); + if (bSize <= 1) + return len; + return bSize * ((len + bSize - 1) / bSize); +} + +bool Cipher::bufferFull(unsigned int len) const +{ + unsigned int bSize = blockSize(); + if (bSize <= 1) + return true; + return 0 == (len % bSize); +} + +bool Cipher::initVector(const void* vect, unsigned int len, Direction dir) +{ + return false; +} + +/* vi: set ts=8 sw=4 sts=4 noet: */ diff --git a/engine/Makefile.in b/engine/Makefile.in index 1ccd6599..205cbff5 100644 --- a/engine/Makefile.in +++ b/engine/Makefile.in @@ -24,7 +24,8 @@ PINC := $(EINC) @top_srcdir@/yatephone.h CLINC:= $(PINC) @top_srcdir@/yatecbase.h LIBS := CLSOBJS := TelEngine.o ObjList.o HashList.o String.o DataBlock.o NamedList.o \ - URI.o Mime.o Array.o Iterator.o YMD5.o YSHA1.o Base64.o Mutex.o Thread.o Socket.o + URI.o Mime.o Array.o Iterator.o YMD5.o YSHA1.o Base64.o Cipher.o \ + Mutex.o Thread.o Socket.o ENGOBJS := Configuration.o Message.o Plugin.o Engine.o TELOBJS := DataFormat.o Channel.o CLIOBJS := Client.o ClientLogic.o diff --git a/yateclass.h b/yateclass.h index 2043d24c..f74d74d7 100644 --- a/yateclass.h +++ b/yateclass.h @@ -4689,6 +4689,157 @@ protected: ObjList m_filters; }; +/** + * The Cipher class provides an abstraction for data encryption classes + * @short An abstract cipher + */ +class YATE_API Cipher : public GenObject +{ +public: + /** + * Cipher direction + */ + enum Direction { + Bidir, + Encrypt, + Decrypt, + }; + + /** + * Get the dictionary of cipher directions + * @return Pointer to the dictionary of cipher directions + */ + inline static const TokenDict* directions() + { return s_directions; } + + /** + * Get a direction from the dictionary given the name + * @param name Name of the direction + * @param defdir Default direction to return if @ref name is empty or unknown + * @return Direction associated with the given name + */ + inline static Direction direction(const char* name, Direction defdir = Bidir) + { return (Direction)TelEngine::lookup(name,s_directions,defdir); } + + /** + * Destructor + */ + virtual ~Cipher(); + + /** + * Get a pointer to a derived class given that class name + * @param name Name of the class we are asking for + * @return Pointer to the requested class or NULL if this object doesn't implement it + */ + virtual void* getObject(const String& name) const; + + /** + * Check if the cipher instance is valid for a specific direction + * @param dir Direction to check + * @return True if the cipher is able to perform operation on given direction + */ + virtual bool valid(Direction dir = Bidir) const; + + /** + * Get the cipher block size + * @return Cipher block size in bytes + */ + virtual unsigned int blockSize() const = 0; + + /** + * Get the initialization vector size + * @return Initialization vector size in bytes, 0 if not applicable + */ + virtual unsigned int initVectorSize() const; + + /** + * Round up a buffer length to a multiple of block size + * @param len Length of data to encrypt or decrypt in bytes + * @return Length of required buffer in bytes + */ + unsigned int bufferSize(unsigned int len) const; + + /** + * Check if a buffer length is multiple of block size + * @param len Length of data to encrypt or decrypt in bytes + * @return True if buffer length is multiple of block size + */ + bool bufferFull(unsigned int len) const; + + /** + * Set the key required to encrypt or decrypt data + * @param key Pointer to binary key data + * @param len Length of key in bytes + * @param dir Direction to set key for + * @return True if the key was set successfully + */ + virtual bool setKey(const void* key, unsigned int len, Direction dir = Bidir) = 0; + + /** + * Set the key required to encrypt or decrypt data + * @param key Binary key data block + * @param dir Direction to set key for + * @return True if the key was set successfully + */ + inline bool setKey(const DataBlock& key, Direction dir = Bidir) + { return setKey(key.data(),key.length(),dir); } + + /** + * Set the Initialization Vector if applicable + * @param vect Pointer to binary Initialization Vector data + * @param len Length of Initialization Vector in bytes + * @param dir Direction to set the Initialization Vector for + * @return True if the Initialization Vector was set successfully + */ + virtual bool initVector(const void* vect, unsigned int len, Direction dir = Bidir); + + /** + * Set the Initialization Vector is applicable + * @param vect Binary Initialization Vector + * @param dir Direction to set the Initialization Vector for + * @return True if the Initialization Vector was set successfully + */ + inline bool initVector(const DataBlock& vect, Direction dir = Bidir) + { return initVector(vect.data(),vect.length(),dir); } + + /** + * Encrypt data + * @param outData Pointer to buffer for output (encrypted) and possibly input data + * @param len Length of output data, may not be multiple of block size + * @param inpData Pointer to buffer containing input (unencrypted) data, NULL to encrypt in place + * @return True if data was successfully encrypted + */ + virtual bool encrypt(void* outData, unsigned int len, const void* inpData = 0) = 0; + + /** + * Encrypt a DataBlock in place + * @param data Data block to encrypt + * @return True if data was successfully encrypted + */ + inline bool encrypt(DataBlock& data) + { return encrypt(data.data(),data.length()); } + + /** + * Decrypt data + * @param outData Pointer to buffer for output (decrypted) and possibly input data + * @param len Length of output data, may not be multiple of block size + * @param inpData Pointer to buffer containing input (encrypted) data, NULL to decrypt in place + * @return True if data was successfully decrypted + */ + virtual bool decrypt(void* outData, unsigned int len, const void* inpData = 0) = 0; + + /** + * Decrypt a DataBlock in place + * @param data Data block to decrypt + * @return True if data was successfully decrypted + */ + inline bool decrypt(DataBlock& data) + { return decrypt(data.data(),data.length()); } + +private: + static const TokenDict s_directions[]; +}; + /** * The SysUsage class allows collecting some statistics about engine's usage * of system resources