Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼


Testing the Final SHA-3 Hashing Algorithms

Last week, I presented the five finalist candidates for the new SHA-3 standard. I this final article, I look at testing them on Mac OS X and compare their results.

For its source of secure hashes, MacOS X relies on the OpenSSL library. That open-source library supplies MD-5 and SHA-1, but not SHA-2. This is because Apple bundles version 0.98 of the library. Version 1.0 of the same library has the SHA-2 support.

But what if we want to use SHA-3 to process an NSString object? We could add the functions themselves to the Cocoa project, then convert and pass the NSString data as a byte stream. This can get tedious, especially when lots of NSString objects have to be processed. A more elegant way is to implement the SHA-3 functions as a category.

The category mechanism is a unique yet effective way to extend an Objective-C class. With categories, we can add new methods to the class, as well as override existing methods. The methods are built separately and are attached to the class at runtime. Unlike protocols, categories can be implemented without subclassing the target class. Unlike subclassing, categories do not need prior knowledge of the class' source code.

Listing One describes the header file for the category SHA3Category. It begins by importing the header files from each of the five SHA-3 candidates (lines 1-5). Then it declares the enum constant SHA3Type (lines 8-10), assigning a unique id to each candidate. Next, the @interface header sets the target class and the category name (line 13). And the @interface block declares the method prototype sha3hash: (line 16). The method is to accept an NSInteger as input and return an NSData as output.

Listing One: The header file for SHA3.

#import "blake_ref.h"
#import "Groestl-ref.h"
#import "jh_ref.h"
#import "KeccakNISTInterface.h"
#import "skein.h"

enum SHA3Type 
    {TypeBlake = 16, TypeGrostl = 32, TypeJH = 64, 
        TypeKeccak = 128, TypeSkein = 256};

@interface NSString (SHA3Category)

// methods:declaration
- (NSData *)sha3Hash:(NSInteger)aTyp;


Listing Two shows how the SHA3Category is implemented. Here, too, the @implementation header names the target class and category (line 4). As for the sha3hash: method, it starts by extracting the NSString data and converts it into a stream of character bytes (tMsg, lines 15-17). Then it allocates space for the local variable tDig, which will hold the hash value (line 23). For purposes of this article, sha3hash: only generates a 256-bit hash.

Listing Two: Implementing SHA3Category

#import "SHA3Category.h"

@implementation NSString (SHA3Category)

// Generate an SHA-3 hash
- (NSData *)sha3Hash:(NSInteger)aTyp
    NSData *tHsh, *tDat;
    unsigned char *tMsg, *tDig;
	unsigned int tChk;
	unsigned tSiz;
    // prepare the message data for hashing
    tDat = [self dataUsingEncoding:NSUTF8StringEncoding];
 	tMsg = (unsigned char*)[tDat bytes];
	tSiz = [tDat length];
	// set the default return value
	tHsh = [NSData data];
	// create the hash buffer
	tDig = (unsigned char*)malloc(sizeof(unsigned char)*32);
    // identify the hash type
    switch (aTyp)
        case TypeBlake:
            // -- sha-3:Blake
			tChk = HashBlake(256, tMsg, tSiz, tDig);
			if (tChk == BLAKE_SUCCESS)
				tSiz = strlen((const char*)tDig);
				tHsh = [NSData dataWithBytes:tDig length:tSiz];
        case TypeGrostl:
            // -- sha-3:Grostl
			tChk = HashGrostl(256, tMsg, tSiz, tDig);
			if (tChk == GROSTL_SUCCESS)
				tSiz = strlen((const char*)tDig);
				tHsh = [NSData dataWithBytes:tDig length:tSiz];
        case TypeJH:
            // -- sha-3:JH
			tChk = HashJH(256, tMsg, tSiz, tDig);
			if (tChk == JH_SUCCESS)
				tSiz = strlen((const char*)tDig);
				tHsh = [NSData dataWithBytes:tDig length:tSiz];
        case TypeKeccak:
            // -- sha-3:Keccak
			tChk = HashKeccak(256, tMsg, tSiz, tDig);
			if (tChk == KECCAK_SUCCESS)
				tSiz = strlen((const char*)tDig);
				tHsh = [NSData dataWithBytes:tDig length:tSiz];
        case TypeSkein:
            // -- sha-3:Skein
			tChk = HashSkein(256, tMsg, tSiz, tDig);
			if (tChk == SKEIN_SUCCESS)
				tSiz = strlen((const char*)tDig);
				tHsh = [NSData dataWithBytes:tDig length:tSiz];
            // -- sha-3:unknown
            // RESERVED
	// dispose the buffer
    // return the hash results
    return (tHsh);


Next, sha3hash: uses the argument aTyp to identify the chosen SHA-3 function. It then calls the function, passing for input the message stream, its size, and the tDig local (lines 26-79). If the function is successful, sha3hash: packages the hash value into an NSData object (tHsh). It frees the memory used by tDig and returns the NSData object as the result (lines 82-85).

Note sha3hash: returns a null NSData object if its aTyp argument has the wrong ID or if the chosen SHA-3 function fails to hash the message data.

Hash Versus Hash

So far, what we knew about each SHA-3 candidate was based on their official specs and on the analysis made by NIST. But how well do these five SHA-3 candidates perform in the real world? And how well do they compare against OpenSSL hash functions?

To find out, we subject the candidates to four basic tests. Our tests use the same message phrase and use the OpenSSL hash functions as points of references. Except for MD-5 and SHA-1, all tests involve 256-bit hash values. The SHA-3 candidates are based on code supplied by their respective authors. The code is not optimized, only altered to address name-space issues.

Tests were done on a MacBook running a 2.4 GHz Intel Core 2 Duo and MacOS X 10.6.4. The message used for the test is the phrase "The big brown fox jumps over the lazy dog."

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.