00001 /***************************************************************************** 00002 * Copyright (C) 2011 by Benjamin Hadorn (b_hadorn@bluewin.ch) 00003 ***************************************************************************** 00004 * Project : Zeus Base Library 00005 * Module : SecureHash512 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 #ifndef SecureHash512H 00072 #define SecureHash512H 00073 00074 #include <zeusbase/Security/AbstractHash.hpp> 00075 00076 BEGIN_NAMESPACE_Zeus 00077 00078 #define SHA512_DIGEST_SIZE 64 // 512 / 8 00079 #define SHA512_BLOCK_SIZE 128 // 1024 / 8 00080 00081 /****************************************************************************/ 00085 /****************************************************************************/ 00086 zeusbase_class TSecureHash512 : public TAbstractHash 00087 { 00088 public: 00089 /************************************************************************/ 00092 /************************************************************************/ 00093 enum EDigestSize 00094 { 00095 etHash512 = 0, /* Hash with digest size of 512bit */ 00096 etHash384 = 1 /* Hash with digest size of 384bit */ 00097 }; 00098 00099 TSecureHash512(EDigestSize eType = etHash512); 00100 00101 void getHMAC(const IByteArray& rKey, 00102 const IByteArray& rInput, 00103 IByteArray& rOutput); 00104 00105 protected: 00106 00107 //Methods of AbstractHash 00108 virtual Retval generate(const IByteArray& rInput, IByteArray& rOutput); 00109 00110 /*************************************************************************/ 00113 /*************************************************************************/ 00114 struct TypeSHA512_CTX 00115 { 00116 Uint64 h[8]; 00117 00118 Uint64 uldTotalLen; 00119 Uint32 ulBufferLen; 00120 Uint8 aucBuffer[2 * SHA512_BLOCK_SIZE]; 00121 }; 00122 00123 00124 private: 00126 EDigestSize m_eDigestType; 00128 static Uint64 m_ulSHA512_h0[8]; 00130 static Uint64 m_ulSHA384_h0[8]; 00132 static Uint64 m_ulSHA512_k[80]; 00133 00134 void finishCTX(TypeSHA512_CTX& rCtx, IByteArray& rOutput); 00135 void freeCTX(TypeSHA512_CTX& rCTX); 00136 void initCTX(TypeSHA512_CTX& rCtx); 00137 void processBytes(TypeSHA512_CTX& rCtx, const IByteArray& rInput); 00138 void processBytes(TypeSHA512_CTX& rCtx, const Uint8* puData, Uint uiSize); 00139 00140 void processBlock(TypeSHA512_CTX& rCtx, const Uint8* pucBuffer, Int iBlockNum); 00141 }; 00142 00143 /*****************************************************************************/ 00146 /*****************************************************************************/ 00147 inline void TSecureHash512::processBytes(TypeSHA512_CTX& rCtx, const IByteArray& rInput) 00148 { 00149 processBytes(rCtx, (const Uint8*)rInput.getArray(), rInput.getCount()); 00150 } 00151 00152 END_NAMESPACE_Zeus 00153 00154 00155 #endif