Shadowrun: Awakened 29 September 2011 - Build 871
Skein.hpp
Go to the documentation of this file.
00001 /*
00002     Copyright (c) 2009-2010 Christopher A. Taylor.  All rights reserved.
00003 
00004     Redistribution and use in source and binary forms, with or without
00005     modification, are permitted provided that the following conditions are met:
00006 
00007     * Redistributions of source code must retain the above copyright notice,
00008       this list of conditions and the following disclaimer.
00009     * Redistributions in binary form must reproduce the above copyright notice,
00010       this list of conditions and the following disclaimer in the documentation
00011       and/or other materials provided with the distribution.
00012     * Neither the name of LibCat nor the names of its contributors may be used
00013       to endorse or promote products derived from this software without
00014       specific prior written permission.
00015 
00016     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00019     ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00020     LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00021     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00022     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00023     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00024     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00025     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00026     POSSIBILITY OF SUCH DAMAGE.
00027 */
00028 
00029 /*
00030     Bruce Schneier's SHA-3 candidate Skein hash function
00031     http://www.skein-hash.info/
00032 */
00033 
00034 #ifndef CAT_SKEIN_HPP
00035 #define CAT_SKEIN_HPP
00036 
00037 #include <cat/crypt/hash/ICryptHash.hpp>
00038 
00039 namespace cat {
00040 
00041 
00042 // Base class for various versions of Skein
00043 class Skein : public ICryptHash
00044 {
00045 protected:
00046     // Tweak word 1 bit field starting positions
00047     static const int T1_POS_TREE_LVL = 112-64; // bits 112..118 : level in hash tree
00048     static const int T1_POS_BIT_PAD = 119-64;  // bit  119      : partial final input byte
00049     static const int T1_POS_BLK_TYPE = 120-64; // bits 120..125 : type field
00050     static const int T1_POS_FIRST = 126-64;    // bits 126      : first block flag
00051     static const int T1_POS_FINAL = 127-64;    // bit  127      : final block flag
00052 
00053     // Tweak word 1 bit field masks
00054     static const u64 T1_MASK_FIRST = (u64)1 << T1_POS_FIRST;
00055     static const u64 T1_MASK_FINAL = (u64)1 << T1_POS_FINAL;
00056     static const u64 T1_MASK_BIT_PAD = (u64)1 << T1_POS_BIT_PAD;
00057     static const u64 T1_MASK_TREE_LVL = (u64)0x7F << T1_POS_TREE_LVL;
00058     static const u64 T1_MASK_BLK_TYPE = (u64)63 << T1_POS_BLK_TYPE;
00059 
00060     static const int BLK_TYPE_KEY = 0;    // key, for MAC and KDF
00061     static const int BLK_TYPE_CFG = 4;    // configuration block
00062     static const int BLK_TYPE_PERS = 8;   // personalization string
00063     static const int BLK_TYPE_PK = 12;    // public key (for digital signature hashing)
00064     static const int BLK_TYPE_KDF = 16;   // key identifier for KDF
00065     static const int BLK_TYPE_NONCE = 20; // nonce for PRNG
00066     static const int BLK_TYPE_MSG = 48;   // message processing
00067     static const int BLK_TYPE_OUT = 63;   // output stage
00068 
00069     static const u32 ID_STRING_LE = 0x33414853;
00070     static const u32 VERSION = 1;
00071     static const u64 SCHEMA_VER = ((u64)VERSION << 32) | ID_STRING_LE;
00072 
00073     static const int MAX_BITS = 512;
00074     static const int MAX_WORDS = MAX_BITS / 64;
00075     static const int MAX_BYTES = MAX_BITS / 8;
00076 
00077     u64 Tweak[2];
00078     u64 State[MAX_WORDS];
00079     u8 Work[MAX_BYTES];
00080     int used_bytes, digest_words;
00081     u64 output_block_counter;
00082     bool output_prng_mode;
00083 
00084     typedef void (Skein::*HashComputation)(const void *message, int blocks, u32 byte_count, u64 *NextState);
00085 
00086     void HashComputation256(const void *message, int blocks, u32 byte_count, u64 *NextState);
00087     void HashComputation512(const void *message, int blocks, u32 byte_count, u64 *NextState);
00088 
00089     HashComputation hash_func;
00090 
00091     void GenerateInitialState(int bits);
00092 
00093 public:
00094     ~Skein();
00095     bool BeginKey(int bits);
00096     bool SetKey(ICryptHash *parent);
00097     bool BeginMAC();
00098     bool BeginKDF();
00099     bool BeginPRNG();
00100     void Crunch(const void *message, int bytes);
00101     void End();
00102     void Generate(void *out, int bytes, int strengthening_rounds = 0);
00103 };
00104 
00105 
00106 } // namespace cat
00107 
00108 #endif // CAT_SKEIN_HPP

Copyright © 2007-2010 by The Shadowrun: Awakened Team. This work is licensed under the GNU Lesser General Public License 3.

GNU Lesser General Public License 3 Sourceforge.net