Click here to Skip to main content
Click here to Skip to main content

A Tiger Hash Implementation for C#

, 10 Mar 2012 CDDL
Rate this:
Please Sign up or sign in to vote.
A Tiger hash Implementation for C#, based on .NET's HashAlgorithm class.

Introduction

This set of pure C# classes implements the cryptographic Tiger hash algorithm. It inherits from .NET's HashAlgorithm class and so is usable as drop-in in any place where .NET's built-in hash functions are already used. Although not as fast as native machine code generated by C++ or ASM, it's still about 30% faster than the other C# implementations I found (2010).

The program has been tested with a number of different binaries, and most of the test vectors supplied by the creators of Tiger.

Background

A cryptographic hash maps an arbitrary-length data block (e.g., password, file) to a fixed-length hash value as one-way function (= irreversible).

Tiger was designed in 1995, so it had enough time to be well-analyzed, and no successful attacks on full 24-round Tiger are known to date. It is always a good idea to consult Wikipedia or The Hash Function Lounge to check if new vulnerabilities have been found since this article was written.

Its level of security is comparable to RIPEMD-160 or SHA-256. It works on whole 512-bit input data blocks, and produces 192 bits of hash value output. Input data that doesn't align to 512-bit boundaries (as usually is the case) is padded accordingly.

Tiger itself is a 64-bit-optimized algorithm, but still runs well on narrower buses. This implementation does some optimizations that do not drag down the 64-bit performance noticeably but help the 32-bit systems very much.

The underlying cryptographic design is basically described on Wikipedia and in more detail on the inventors´ webpage. The data for the S-Boxes has been taken from the reference implementation.

Some of the functionality (e.g., FileStream processing) is provided by the .NET Framework through the abstract HashAlgorithm base class.

Using the code

The class is being created/instantiated directly, but used/called through the abstract HashAlgorithm class:

using System.Security.Cryptography;
using softwareunion;

HashAlgorithm myhash;

switch(AlgorithmToUse)
{   case "MD5":   myhash=new MD5CryptoServiceProvider();
    case "TIGER": myhash=new Tiger();  // <<-- create the object
// [...]
    default: throw new NotImplementedException();
}

// vv-- then ask framework's HashAlgorithm class to
//      coordinate and call our own class methods
myhash.ComputeHash( File.OpenRead("myfile.bin") );

byte[] the_hash_result=myhash.HashValue;

There is a version 2 of Tiger that only differs in the padding value of 0x01 being upgraded to 0x80 (there's already a comment for it in the ProcessFinalBlock function).

Optimizations

  • Using Array.Copy(...) can also process arrays of different types, and runs with highly optimized native code, while pure C# loops that copy element per element are only as good as the JIT allows them to be.
  • Calling functions inside a loop may be comfortable, but is rarely a good idea and is not always optimized by the JIT compiler.
  • Loop unrolling often gives good results under C++, but my tests on this code (using .NET 2.0) have shown compact loops giving better performance.
  • Converting to (byte) is slightly faster than &0xFF.
  • Arrays are fastest when indexed by an int.
  • Functions with no local variables are called faster.

Room for Future Improvements

  • Test where "unchecked" clauses could bring performance improvement.
  • Can the BitTools.RotLeft functions be further optimized? 
  • Can the Array type or any other part of the framework provide the functionality of TypeBlindCopy(...) - that is pack an array of bytes into an array of ulongs?
  • Can any more temporary gcheap allocations be removed? 

History

  • 2011-01-21: Tests finished, going public.
  • 2012-03-09: Adding some weblinks.

License

This article, along with any associated source code and files, is licensed under The Common Development and Distribution License (CDDL)

Share

About the Author

mastamac
Chief Technology Officer Software Union
Austria Austria
has been working a total of 14 years as programmer and system developer, plus some years of working as network technician and application supporter. Also experienced modeling, optimizing and administering FireBird and SqlServer databases. Loathes MySql.
Works with C++, C++/CLI, C#, JavaScript, PHP, PSQL and TextPad.

Comments and Discussions

 
QuestionHow did you figure out the S-Boxes? PinmemberManfred R. Bihy22-Jan-11 10:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 10 Mar 2012
Article Copyright 2011 by mastamac
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid