Back to index

php5  5.3.10
Defines | Functions | Variables
hash_salsa.c File Reference
#include "php_hash.h"
#include "php_hash_salsa.h"

Go to the source code of this file.

Defines

#define R(a, b)   (((a) << (b)) | ((a) >> (32 - (b))))

Functions

static void Salsa10 (php_hash_uint32 x[16], php_hash_uint32 in[16])
static void Salsa20 (php_hash_uint32 x[16], php_hash_uint32 in[16])
static void SalsaTransform (PHP_SALSA_CTX *context, const unsigned char input[64])
PHP_HASH_API void PHP_SALSA10Init (PHP_SALSA_CTX *context)
PHP_HASH_API void PHP_SALSA20Init (PHP_SALSA_CTX *context)
PHP_HASH_API void PHP_SALSAUpdate (PHP_SALSA_CTX *context, const unsigned char *input, size_t len)
PHP_HASH_API void PHP_SALSAFinal (unsigned char digest[64], PHP_SALSA_CTX *context)

Variables

const php_hash_ops php_hash_salsa10_ops
const php_hash_ops php_hash_salsa20_ops

Define Documentation

#define R (   a,
 
)    (((a) << (b)) | ((a) >> (32 - (b))))

Definition at line 25 of file hash_salsa.c.


Function Documentation

Definition at line 143 of file hash_salsa.c.

{
       memset(context, 0, sizeof(*context));
       context->Transform = Salsa10;
}

Here is the call graph for this function:

Definition at line 149 of file hash_salsa.c.

{
       memset(context, 0, sizeof(*context));
       context->Transform = Salsa20;
}

Here is the call graph for this function:

PHP_HASH_API void PHP_SALSAFinal ( unsigned char  digest[64],
PHP_SALSA_CTX context 
)

Definition at line 179 of file hash_salsa.c.

{
       php_hash_uint32 i, j;
       
       if (context->length) {
              SalsaTransform(context, context->buffer);
       }
       
       for (i = 0, j = 0; j < 64; i++, j += 4) {
              digest[j] = (unsigned char) ((context->state[i] >> 24) & 0xff);
              digest[j + 1] = (unsigned char) ((context->state[i] >> 16) & 0xff);
              digest[j + 2] = (unsigned char) ((context->state[i] >> 8) & 0xff);
              digest[j + 3] = (unsigned char) (context->state[i] & 0xff);
       }
       
       memset(context, 0, sizeof(*context));
}

Here is the call graph for this function:

PHP_HASH_API void PHP_SALSAUpdate ( PHP_SALSA_CTX context,
const unsigned char *  input,
size_t  len 
)

Definition at line 155 of file hash_salsa.c.

{
       if (context->length + len < 64) {
              memcpy(&context->buffer[context->length], input, len);
              context->length += len;
       } else {
              size_t i = 0, r = (context->length + len) % 64;
              
              if (context->length) {
                     i = 64 - context->length;
                     memcpy(&context->buffer[context->length], input, i);
                     SalsaTransform(context, context->buffer);
                     memset(context->buffer, 0, 64);
              }
              
              for (; i + 64 <= len; i += 64) {
                     SalsaTransform(context, input + i);
              }
              
              memcpy(context->buffer, input + i, r);
              context->length = r;
       }
}

Here is the call graph for this function:

static void Salsa10 ( php_hash_uint32  x[16],
php_hash_uint32  in[16] 
) [static]

Definition at line 46 of file hash_salsa.c.

{
       int i;
       
       for (i = 10; i > 0; --i) {
              x[ 4] ^= R(x[ 0]+x[12], 6);  x[ 8] ^= R(x[ 4]+x[ 0],17);
              x[12] += R(x[ 8]|x[ 4],16);  x[ 0] += R(x[12]^x[ 8], 5);
              x[ 9] += R(x[ 5]|x[ 1], 8);  x[13] += R(x[ 9]|x[ 5], 7);
              x[ 1] ^= R(x[13]+x[ 9],17);  x[ 5] += R(x[ 1]^x[13],12);
              x[14] ^= R(x[10]+x[ 6], 7);  x[ 2] += R(x[14]^x[10],15);
              x[ 6] ^= R(x[ 2]+x[14],13);  x[10] ^= R(x[ 6]+x[ 2],15);
              x[ 3] += R(x[15]|x[11],20);  x[ 7] ^= R(x[ 3]+x[15],16);
              x[11] += R(x[ 7]^x[ 3], 7);  x[15] += R(x[11]^x[ 7], 8);
              x[ 1] += R(x[ 0]|x[ 3], 8)^i;x[ 2] ^= R(x[ 1]+x[ 0],14);
              x[ 3] ^= R(x[ 2]+x[ 1], 6);  x[ 0] += R(x[ 3]^x[ 2],18);
              x[ 6] += R(x[ 5]^x[ 4], 8);  x[ 7] += R(x[ 6]^x[ 5],12);
              x[ 4] += R(x[ 7]|x[ 6],13);  x[ 5] ^= R(x[ 4]+x[ 7],15);
              x[11] ^= R(x[10]+x[ 9],18);  x[ 8] += R(x[11]^x[10],11);
              x[ 9] ^= R(x[ 8]+x[11], 8);  x[10] += R(x[ 9]|x[ 8], 6);
              x[12] += R(x[15]^x[14],17);  x[13] ^= R(x[12]+x[15],15);
              x[14] += R(x[13]|x[12], 9);  x[15] += R(x[14]^x[13], 7);
       }
       for (i = 0; i < 16; ++i) {
              x[i] += in[i];
       }
}

Here is the caller graph for this function:

static void Salsa20 ( php_hash_uint32  x[16],
php_hash_uint32  in[16] 
) [static]

Definition at line 93 of file hash_salsa.c.

{
       int i;
       
       for (i = 20; i > 0; i -= 2) {
              x[ 4] ^= R(x[ 0]+x[12], 7);  x[ 8] ^= R(x[ 4]+x[ 0], 9);
              x[12] ^= R(x[ 8]+x[ 4],13);  x[ 0] ^= R(x[12]+x[ 8],18);
              x[ 9] ^= R(x[ 5]+x[ 1], 7);  x[13] ^= R(x[ 9]+x[ 5], 9);
              x[ 1] ^= R(x[13]+x[ 9],13);  x[ 5] ^= R(x[ 1]+x[13],18);
              x[14] ^= R(x[10]+x[ 6], 7);  x[ 2] ^= R(x[14]+x[10], 9);
              x[ 6] ^= R(x[ 2]+x[14],13);  x[10] ^= R(x[ 6]+x[ 2],18);
              x[ 3] ^= R(x[15]+x[11], 7);  x[ 7] ^= R(x[ 3]+x[15], 9);
              x[11] ^= R(x[ 7]+x[ 3],13);  x[15] ^= R(x[11]+x[ 7],18);
              x[ 1] ^= R(x[ 0]+x[ 3], 7);  x[ 2] ^= R(x[ 1]+x[ 0], 9);
              x[ 3] ^= R(x[ 2]+x[ 1],13);  x[ 0] ^= R(x[ 3]+x[ 2],18);
              x[ 6] ^= R(x[ 5]+x[ 4], 7);  x[ 7] ^= R(x[ 6]+x[ 5], 9);
              x[ 4] ^= R(x[ 7]+x[ 6],13);  x[ 5] ^= R(x[ 4]+x[ 7],18);
              x[11] ^= R(x[10]+x[ 9], 7);  x[ 8] ^= R(x[11]+x[10], 9);
              x[ 9] ^= R(x[ 8]+x[11],13);  x[10] ^= R(x[ 9]+x[ 8],18);
              x[12] ^= R(x[15]+x[14], 7);  x[13] ^= R(x[12]+x[15], 9);
              x[14] ^= R(x[13]+x[12],13);  x[15] ^= R(x[14]+x[13],18);
       }
       for (i = 0; i < 16; ++i) {
              x[i] += in[i];
       }
}

Here is the caller graph for this function:

static void SalsaTransform ( PHP_SALSA_CTX context,
const unsigned char  input[64] 
) [inline, static]

Definition at line 121 of file hash_salsa.c.

{
       php_hash_uint32 i, j, a[16];

#if 0
       fprintf(stderr, "> INPUT: %.*s\n", 64, input);
#endif
       
       for (i = 0, j = 0; j < 64; i++, j += 4) {
              a[i] = ((php_hash_uint32) input[j + 3]) | (((php_hash_uint32) input[j + 2]) << 8) |
                     (((php_hash_uint32) input[j + 1]) << 16) | (((php_hash_uint32) input[j]) << 24);
       }
       
       if (!context->init) {
              memcpy(context->state, a, sizeof(a));
              context->init = 1;
       }
       
       context->Transform(context->state, a);
       memset(a, 0, sizeof(a));
}

Here is the caller graph for this function:


Variable Documentation