00001 /***************************************************************************** 00002 * Copyright (C) 2011 by Benjamin Hadorn (b_hadorn@bluewin.ch) 00003 ***************************************************************************** 00004 * Project : Zeus Base Library 00005 * Module : SecureHash256 00006 * Package : Zeus.ZeusBase.Security 00007 * Author : Benjamin Hadorn 00008 * Date : 31.12.2011 00009 * System : Zeus-Framework 00010 ***************************************************************************** 00011 * Licence: * 00012 * This library is free software; you can redistribute it and/or modify * 00013 * it under the terms of the GNU Lesser General Public License as * 00014 * published by the Free Software Foundation; either version * 00015 * 2.1 of the License, or (at your option) any later version. * 00016 * * 00017 * This library is distributed in the hope that it will be useful, * 00018 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00019 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00020 * GNU Lesser General Public License for more details. * 00021 * * 00022 * You should have received a copy of the GNU Lesser General Public * 00023 * License along with this library; if not, write to the Free Software * 00024 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA * 00025 *****************************************************************************/ 00026 00027 /***************************************************************************** 00028 * Changes: 00029 *****************************************************************************/ 00030 00031 /* CODE INFORMATION: 00032 The code has been developed with help of the http://www.ouah.org/ogay/. 00033 It has been significantly adapted to Zeus-Framework and simplified in 00034 terms of redundancy and behavior. 00035 00036 The original code carries following copy right and restrictions: 00037 */ 00038 /* 00039 * FIPS 180-2 SHA-224/256/384/512 implementation 00040 * Last update: 02/02/2007 00041 * Issue date: 04/30/2005 00042 * 00043 * Copyright (C) 2005, 2007 Olivier Gay <olivier.gay@a3.epfl.ch> 00044 * All rights reserved. 00045 * 00046 * Redistribution and use in source and binary forms, with or without 00047 * modification, are permitted provided that the following conditions 00048 * are met: 00049 * 1. Redistributions of source code must retain the above copyright 00050 * notice, this list of conditions and the following disclaimer. 00051 * 2. Redistributions in binary form must reproduce the above copyright 00052 * notice, this list of conditions and the following disclaimer in the 00053 * documentation and/or other materials provided with the distribution. 00054 * 3. Neither the name of the project nor the names of its contributors 00055 * may be used to endorse or promote products derived from this software 00056 * without specific prior written permission. 00057 * 00058 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 00059 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00060 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00061 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 00062 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00063 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00064 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00065 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00066 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00067 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00068 * SUCH DAMAGE. 00069 */ 00070 00071 00072 #ifndef SecureHash256H 00073 #define SecureHash256H 00074 00075 #include <zeusbase/Security/AbstractHash.hpp> 00076 00077 BEGIN_NAMESPACE_Zeus 00078 00079 #define SHA256_DIGEST_SIZE 32 // 256 / 8 00080 #define SHA256_BLOCK_SIZE 64 // 512 / 8 00081 00082 /****************************************************************************/ 00086 /****************************************************************************/ 00087 zeusbase_class TSecureHash256 : public TAbstractHash 00088 { 00089 public: 00090 /************************************************************************/ 00093 /************************************************************************/ 00094 enum EDigestSize 00095 { 00096 etHash256 = 0, /* Hash with digest size of 256bit */ 00097 etHash224 = 1 /* Hash with digest size of 224bit */ 00098 }; 00099 00100 TSecureHash256(EDigestSize eType = etHash256); 00101 00102 void getHMAC(const IByteArray& rKey, 00103 const IByteArray& rInput, 00104 IByteArray& rOutput); 00105 00106 protected: 00107 00108 //Methods of AbstractHash 00109 virtual Retval generate(const IByteArray& rInput, IByteArray& rOutput); 00110 00111 /*************************************************************************/ 00114 /*************************************************************************/ 00115 struct TypeSHA256_CTX 00116 { 00117 Uint32 h[8]; 00118 00119 Uint64 uldTotalLen; 00120 Uint32 ulBufferLen; 00121 Uint8 aucBuffer[2 * SHA256_BLOCK_SIZE]; 00122 }; 00123 00124 00125 private: 00127 EDigestSize m_eDigestType; 00129 static Uint32 m_ulSHA256_h0[8]; 00131 static Uint32 m_ulSHA224_h0[8]; 00133 static Uint32 m_ulSHA256_k[64]; 00134 00135 void finishCTX(TypeSHA256_CTX& rCtx, IByteArray& rOutput); 00136 void freeCTX(TypeSHA256_CTX& rCTX); 00137 void initCTX(TypeSHA256_CTX& rCtx); 00138 void processBytes(TypeSHA256_CTX& rCtx, const IByteArray& rInput); 00139 void processBytes(TypeSHA256_CTX& rCtx, const Uint8* puData, Uint uiSize); 00140 00141 void processBlock(TypeSHA256_CTX& rCtx, const Uint8* pucBuffer, Int iBlockNum); 00142 }; 00143 00144 /*****************************************************************************/ 00147 /*****************************************************************************/ 00148 inline void TSecureHash256::processBytes(TypeSHA256_CTX& rCtx, const IByteArray& rInput) 00149 { 00150 processBytes(rCtx, (const Uint8*)rInput.getArray(), rInput.getCount()); 00151 } 00152 00153 END_NAMESPACE_Zeus 00154 00155 #endif