00001 // Copyright 2006 Benedikt Böhm <hollow@gentoo.org> 00002 // 00003 // This program is free software; you can redistribute it and/or modify 00004 // it under the terms of the GNU General Public License as published by 00005 // the Free Software Foundation; either version 2 of the License, or 00006 // (at your option) any later version. 00007 // 00008 // This program is distributed in the hope that it will be useful, 00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00011 // GNU General Public License for more details. 00012 // 00013 // You should have received a copy of the GNU General Public License 00014 // along with this program; if not, write to the 00015 // Free Software Foundation, Inc., 00016 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00017 00018 /*! 00019 * @defgroup whirlpool Whirlpool hash function 00020 * 00021 * WHIRLPOOL is a cryptographic hash function designed after the Square block 00022 * cipher. WHIRLPOOL is a Miyaguchi-Preneel construction based on a 00023 * substantially modified Advanced Encryption Standard (AES). Given a message 00024 * less than 2^256 bits in length, it returns a 512-bit message digest. 00025 * 00026 * The whirlpool_init() function initializes the hash context pointed to by 00027 * context. After initialization input can be added to the transform routine 00028 * using whirlpool_add(). Once all bytes have been added the transform has to be 00029 * finished by calling whilrpool_finalize(). 00030 * 00031 * An application should not directly use the internal whirlpool_transform() 00032 * function, but always use whirlpool_add(). 00033 * 00034 * The whirlpool_digest() function combines the procedure explained above for a 00035 * single string and returns the digest in hexadecimal notation. 00036 * 00037 * @{ 00038 */ 00039 00040 #ifndef _LUCID_WHIRLPOOL_H 00041 #define _LUCID_WHIRLPOOL_H 00042 00043 #include <stdint.h> 00044 00045 /*! @brief number of bytes in the digest */ 00046 #define DIGESTBYTES 64 00047 00048 /*! @brief number of bits in the digest */ 00049 #define DIGESTBITS (8*DIGESTBYTES) /* 512 */ 00050 00051 00052 /*! @brief number of bytes in the input buffer */ 00053 #define WBLOCKBYTES 64 00054 00055 /*! @brief number of bits in the input buffer */ 00056 #define WBLOCKBITS (8*WBLOCKBYTES) /* 512 */ 00057 00058 00059 /*! @brief number of hashed bytes */ 00060 #define LENGTHBYTES 32 00061 00062 /*! @brief number of hashed bits */ 00063 #define LENGTHBITS (8*LENGTHBYTES) /* 256 */ 00064 00065 /*! 00066 * @brief dynamic whirlpool state data 00067 * 00068 * This struct is used to keep track of the whirlpool transform, i.e. its 00069 * hashing state, input buffer, number of hashed bits, etc. 00070 */ 00071 typedef struct { 00072 uint8_t len[LENGTHBYTES]; /*!< global number of hashed bits */ 00073 uint8_t buf[WBLOCKBYTES]; /*!< buffer of data to hash */ 00074 int bits; /*!< current number of bits on the buffer */ 00075 int pos; /*!< current (possibly incomplete) byte slot on the buffer */ 00076 uint64_t hash[DIGESTBYTES/8]; /*!< the hashing state */ 00077 } whirlpool_t; 00078 00079 /*! 00080 * @brief internal transform routine 00081 * 00082 * @param[in] context whirlpool state context 00083 */ 00084 void whirlpool_transform(whirlpool_t * const context); 00085 00086 /*! 00087 * @brief initialize whirlpool state context 00088 * 00089 * @param[in] context whirlpool state context 00090 */ 00091 void whirlpool_init(whirlpool_t * const context); 00092 00093 /*! 00094 * @brief finalize whirlpool transformation 00095 * 00096 * @param[in] context whirlpool state context 00097 * @param[out] result string to store digest 00098 */ 00099 void whirlpool_finalize(whirlpool_t * const context, unsigned char * const result); 00100 00101 /*! 00102 * @brief add bytes to the transform routine 00103 * 00104 * @param[in] context whirlpool state context 00105 * @param[in] src source string 00106 * @param[in] bits number of bits in the source string 00107 */ 00108 void whirlpool_add(whirlpool_t * const context, 00109 const unsigned char * const src, unsigned long bits); 00110 00111 /*! 00112 * @brief create digest from string 00113 * 00114 * @param[in] str source string 00115 * 00116 * @return digest string (memory obtained by malloc(3)) 00117 * 00118 * @note The caller should free obtained memory using free(3) 00119 * 00120 * @see malloc(3) 00121 * @see free(3) 00122 */ 00123 char *whirlpool_digest(const char *str); 00124 00125 #endif 00126 00127 /*! @} str */