Contenido principal

BLAKE Hash extension for PHP

Enero 27, 2011

"The BLAKE Hash is one of the five finalist algorithms that participates in the NIST SHA-3 competition. This hash functions is based on the HAIFA structure and the ChaCha core function.
The algorithm was designed by Jean-Philippe Aumasson, Luca Henzen, Willi Meier and Raphael C.-W. Phan, official website is http://131002.net/blake/.

There are four instances of the function, the first two, BLAKE224 and BLAKE256 work with 32-bit words and they have an output of 28 and 32 bytes respectively. The other two, BLAKE384 and BLAKE512 work with 64-bit words and they have an output of 48 and 64 bytes respectively.

The algorithm has been designed to be secure and to have a high performance (Even better than SHA-2)."

I was looking for an implementation on PHP but I found nothing, so I had to do it from scratch. My first attempt was to port the code from C to PHP using gmp library, but the script was incredibly slow, it calculates BLAKE512 in 0.07 seconds (I think the slow speed is because the gmp library creates a lot of resources), I wanted to make it faster and the best option was to make a native code in C that was able to work together with PHP.

The code is based on the final round package sent.

The extension

This PHP extension is composed by five functions: blake, blake_file, blake_init, blake_update and blake_final.

Both, blake and blake_file are used to parse strings and files, the other functions are used for incremental hashing.

The functions and its parameters are:
blake()
Description
string blake ( string $data , int $type [, string $salt [, bool $raw_output = false ]] )
Parameters
data: Message to be hashed.
type: Type of instace to use. It must be: BLAKE_224, BLAKE_256, BLAKE_384 or BLAKE_512.
salt: Salt to use. When use BLAKE_224 or BLAKE_256, salt must be 16 bytes, Otherwise, it must be 32 bytes, or a null string for use with raw_output.
raw_output: When set to true, outputs raw binary data.
Return values
Returns a string containing the calculated message digest as lowercase hexits unless raw_output is set to true in which case the raw binary representation of the message digest is returned.

blake_file()
Description
string blake_file ( string $filename , int $type [, string $salt[, bool $raw_output = false ]] )
Parameters
filename: URL describing location of file to be hashed; Supports fopen wrappers.
type: Type of instace to use. It must be: BLAKE_224, BLAKE_256, BLAKE_384 or BLAKE_512.
salt: Salt to use. When use BLAKE_224 or BLAKE_256, salt must be 16 bytes, Otherwise, it must be 32 bytes, or a null string for use with raw_output.
raw_output: When set to true, outputs raw binary data.
Return values
Returns a string containing the calculated message digest as lowercase hexits unless raw_output is set to true in which case the raw binary representation of the message digest is returned.

blake_init
Description
resource blake_init ( int $type [, string $salt ] )
Parameters
type: Type of instace to use. It must be: BLAKE_224, BLAKE_256, BLAKE_384 or BLAKE_512.
salt: Salt to use. When use BLAKE_224 or BLAKE_256, salt must be 16 bytes, Otherwise, it must be 32 bytes, or a null string for use with raw_output.
Return values
Returns a Blake state resource for use with blake_update() and blake_final().

blake_update
Description
bool blake_update ( resource $state, string $data )
Parameters
state: Blake state returned by blake_init()
data: Message to be updated in the incremental hashing.
Return values
Returns TRUE.

blake_final
Description
string blake_final ( resource $state [, bool $raw_output = false ] )
Parameters
state: Blake state returned by blake_init()
raw_output: When set to true, outputs raw binary data.
Return values
Returns a string containing the calculated message digest as lowercase hexits unless raw_output is set to true in which case the raw binary representation of the message digest is returned.

Resources

The package of BLAKE hash extension for PHP contains:
Source code of extension, Windows libraries for VC6 and VC9 (Both with thread safe and non thread safe) and Linux shared library (Compiled with archlinux i686).

Compilation

Compilation in Windows is a bit hard and requires:
:arrow: Microsoft Visual C++ (6.0 or 9)
:arrow: Windows SDK
:arrow: Some binary tools
The wiki site of PHP describes perfectly how to build PHP and PECL extensions step by step.

Compilation in Linux is easier.
Download the package and extract its contents in a folder, then:

cd src
phpize
./configure
make

(If you want to test whether the extension is working properly you should execute "make test" command after compile the extension.)

Then, with administrator privileges copy the shared library contained in modules folder to PHP extension folder.

At the end, add "extension=php_blake.dll" (Windows) or "extension=blake.so" (Linux) to your php.ini file, and that's all.

Examples

<?php
echo blake('sinfocol', BLAKE_224) . PHP_EOL;
echo blake('sinfocol', BLAKE_256) . PHP_EOL;
echo blake('sinfocol', BLAKE_384) . PHP_EOL;
echo blake('sinfocol', BLAKE_512) . PHP_EOL;

//Using incremental hashing and 0123456789abcdef as the salt
$blake = blake_init(BLAKE_256, '0123456789abcdef');
blake_update($blake, 'sinfocol');
echo blake_final($blake);

/* Expected output
1427375d4a16cc70ab4155a7c721f975f92867aa53703ccd1c8f5a4b
be2f4e21cf62f1e98ba6800a73a8b887e8c69e9fbe914d64c769299b111c8974
f41a32404f454bf925b16f7b38bfd8e1910cbd31100a4e7a4ec6cbb54115ea2c0289133953a4a28b04f6ddf14a1884cb
3ec1d1ba3dfbc3ac553f5d8ad5e6c34de7b449cc9ed04b0453f9fa859f80f47e994b6f84f859a86b66b203b0d335867b4cece8c7a0dfa5092e17b1271a5a7e70
a7cae55a3d0f5235ae2d0e2c74ce469d855d11561a5326e46e6d7c9c4d319681
*/
?>

Screenshot

BLAKE hash extension for PHP

Project can be found at https://code.google.com/p/blakehash-php/

Archivado en: Criptografía |

9 comentarios

  1. Juan Escobar Enero 27, 2011 @ 9:24 am

    Congratulations, it now works in my Linux :)

  2. Sysroot Enero 28, 2011 @ 11:29 am

    Great!
    I'm thinking to improve the actual hash library by adding some hashes.
    Stay alert for any change.

  3. hecky Enero 28, 2011 @ 2:10 pm

    Master, this is the hash you talked to me about? The one with issues in the processor architecture?

  4. will Enero 28, 2011 @ 2:18 pm

    Buena extension gracias.

    PD. El que escribio el post no entiende español, o es solo que a todos les dio por comentar en ingles?. En serio.

  5. Sysroot Enero 28, 2011 @ 3:10 pm

    Even in english (master...)!

    And yep, PHP is limited by the processor architecture, if you are using x86 machine you can not implement operations with 64bit integers.
    C99 standard introduces some features, one of them is the new data type "long long", it does not matter if you use x86 or x64 machine, you can use operations between 64bit integers.
    But PHP is a complicated platform that does not have data types.

  6. zarek Enero 28, 2011 @ 5:10 pm

    o por Dios no e entendido nada pero lo leere pronto se nota que es un muy buen articulo como todos los demas.........
    gracias por compartir!!

  7. Manuel Enero 28, 2011 @ 7:10 pm

    Y ahora, el blog sera en ingles?
    no es que no le entienda, pero me gusta mas en españolxD

  8. Sysroot Enero 29, 2011 @ 8:13 pm

    Solo es una excepción a la regla, español forever!

  9. Sysroot Febrero 1, 2011 @ 11:13 pm

    will: Pues no sé... es raro pues todos saben que soy la única persona que administra el sitio.

Deja un comentario