Author: mturk
Date: Tue Aug 11 11:42:38 2009
New Revision: 803069

URL: http://svn.apache.org/viewvc?rev=803069&view=rev
Log:
Add sha2 implementation 256/512 only. We can add 384 later if needed

Added:
    commons/sandbox/runtime/trunk/src/main/native/shared/sha2.c   (with props)
Modified:
    commons/sandbox/runtime/trunk/src/main/native/Makefile.in
    commons/sandbox/runtime/trunk/src/main/native/include/acr_crypto.h

Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.in
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.in?rev=803069&r1=803068&r2=803069&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/Makefile.in (original)
+++ commons/sandbox/runtime/trunk/src/main/native/Makefile.in Tue Aug 11 
11:42:38 2009
@@ -91,6 +91,7 @@
        $(SRCDIR)/shared/base64.$(OBJ) \
        $(SRCDIR)/shared/md5.$(OBJ) \
        $(SRCDIR)/shared/sha1.$(OBJ) \
+       $(SRCDIR)/shared/sha2.$(OBJ) \
        $(SRCDIR)/shared/sbuf.$(OBJ) \
        $(SRCDIR)/shared/string.$(OBJ) \
        $(SRCDIR)/shared/tables.$(OBJ) \

Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr_crypto.h
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_crypto.h?rev=803069&r1=803068&r2=803069&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr_crypto.h 
(original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr_crypto.h Tue Aug 
11 11:42:38 2009
@@ -39,17 +39,41 @@
 #define ACR_MD5_DIGEST_LENGTH           16
 #define ACR_MD5_DIGEST_STRING_LENGTH    (ACR_MD5_DIGEST_LENGTH * 2 + 1)
 
-typedef struct acr_sha1_ctx_t {
+/*** SHA-256/384/512 Various Length Definitions ***********************/
+#define ACR_SHA256_BLOCK_LENGTH         64
+#define ACR_SHA256_DIGEST_LENGTH        32
+#define ACR_SHA256_DIGEST_STRING_LENGTH (ACR_SHA256_DIGEST_LENGTH * 2 + 1)
+#define ACR_SHA384_BLOCK_LENGTH         128
+#define ACR_SHA384_DIGEST_LENGTH        48
+#define ACR_SHA384_DIGEST_STRING_LENGTH (ACR_SHA384_DIGEST_LENGTH * 2 + 1)
+#define ACR_SHA512_BLOCK_LENGTH         128
+#define ACR_SHA512_DIGEST_LENGTH        64
+#define ACR_SHA512_DIGEST_STRING_LENGTH (ACR_SHA512_DIGEST_LENGTH * 2 + 1)
+
+typedef struct acr_sha1_ctx_t   acr_sha1_ctx_t;
+typedef struct acr_sha2_ctx_t   acr_sha2_ctx_t;
+typedef struct acr_md5_ctx_t    acr_md5_ctx_t;
+
+struct acr_sha1_ctx_t {
     acr_uint32_t state[5];
     acr_uint64_t count;
     acr_byte_t   buffer[ACR_SHA1_BLOCK_LENGTH];
-} acr_sha1_ctx_t;
+};
 
-typedef struct acr_md5_ctx_t {
+struct acr_md5_ctx_t {
     acr_uint32_t state[4];                      /* state */
     acr_uint64_t count;                         /* number of bits, mod 2^64 */
     acr_byte_t buffer[ACR_MD5_BLOCK_LENGTH];    /* input buffer */
-} acr_md5_ctx_t;
+};
+
+struct acr_sha2_ctx_t {
+    union {
+        acr_uint32_t   st32[8];
+        acr_uint64_t   st64[8];
+    } state;
+    acr_uint64_t   bitcount[2];
+    acr_byte_t     buffer[ACR_SHA512_BLOCK_LENGTH];
+};
 
 
 /** 
@@ -122,6 +146,96 @@
                                        wchar_t *out);
 
 /**
+ * Initialize the SHA256 digest
+ * @param context The SHA2 context to initialize
+ */
+ACR_DECLARE(void) ACR_SHA256Init(acr_sha2_ctx_t *context);
+
+/**
+ * Initialize the SHA512 digest
+ * @param context The SHA2 context to initialize
+ */
+ACR_DECLARE(void) ACR_SHA512Init(acr_sha2_ctx_t *context);
+
+/**
+ * Update the SHA256 digest with binary data
+ * @param context The SHA2 context to update
+ * @param input The buffer to add to the SHA digest
+ * @param count The length of the input buffer
+ */
+ACR_DECLARE(void) ACR_SHA256Update(acr_sha2_ctx_t *context,
+                                   const unsigned char *input,
+                                   size_t count);
+
+/**
+ * Update the SHA512 digest with binary data
+ * @param context The SHA2 context to update
+ * @param input The buffer to add to the SHA digest
+ * @param count The length of the input buffer
+ */
+ACR_DECLARE(void) ACR_SHA512Update(acr_sha2_ctx_t *context,
+                                   const unsigned char *input,
+                                   size_t count);
+
+/**
+ * Finish computing the SHA256 digest
+ * @param digest the output buffer in which to store the digest
+ * @param context The context to finalize
+ */
+ACR_DECLARE(void) ACR_SHA256Final(unsigned char 
digest[ACR_SHA256_DIGEST_LENGTH],
+                                  acr_sha2_ctx_t *context);
+
+
+/**
+ * Finish computing the SHA256 digest
+ * @param digest the output buffer in which to store the digest
+ * @param context The context to finalize
+ */
+ACR_DECLARE(void) ACR_SHA512Final(unsigned char 
digest[ACR_SHA512_DIGEST_LENGTH],
+                                  acr_sha2_ctx_t *context);
+
+
+/**
+ * Provide a means to SHA256 crypt/encode a plaintext data using
+ * base 16 hexadecimal encoding.
+ * @param clear The plaintext data.
+ * @param len The length of the plaintext data
+ * @param out The encrypted/encoded password
+ */
+ACR_DECLARE(char *) ACR_SHA256EncodeA(const char *clear, size_t len,
+                                      char *out);
+
+/**
+ * Provide a means to SHA256 crypt/encode a plaintext data using
+ * base 16 hexadecimal encoding.
+ * @param clear The plaintext data.
+ * @param len The length of the plaintext data
+ * @param out The encrypted/encoded password
+ */
+ACR_DECLARE(wchar_t *) ACR_SHA256EncodeW(const wchar_t *clear, size_t len,
+                                         wchar_t *out);
+
+/**
+ * Provide a means to SHA256 crypt/encode a plaintext data using
+ * base 16 hexadecimal encoding.
+ * @param clear The plaintext data.
+ * @param len The length of the plaintext data
+ * @param out The encrypted/encoded password
+ */
+ACR_DECLARE(char *) ACR_SHA512EncodeA(const char *clear, size_t len,
+                                      char *out);
+
+/**
+ * Provide a means to SHA256 crypt/encode a plaintext data using
+ * base 16 hexadecimal encoding.
+ * @param clear The plaintext data.
+ * @param len The length of the plaintext data
+ * @param out The encrypted/encoded password
+ */
+ACR_DECLARE(wchar_t *) ACR_SHA512EncodeW(const wchar_t *clear, size_t len,
+                                         wchar_t *out);
+
+/**
  * Initialize the MD5 digest
  * @param context The MD5 context to initialize
  */

Added: commons/sandbox/runtime/trunk/src/main/native/shared/sha2.c
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/sha2.c?rev=803069&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/sha2.c (added)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/sha2.c Tue Aug 11 
11:42:38 2009
@@ -0,0 +1,893 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+/*
+ * FILE:    sha2.c
+ * AUTHOR:  Aaron D. Gifford <m...@aarongifford.com>
+ * 
+ * Copyright (c) 2000-2001, Aaron D. Gifford
+ * All rights reserved.
+ *
+ */
+
+/*
+ * UNROLLED TRANSFORM LOOP NOTE:
+ * You can define SHA2_UNROLL_TRANSFORM to use the unrolled transform
+ * loop version for the hash transform rounds (defined using macros
+ * later in this file).  Either define on the command line, for example:
+ *
+ *   cc -DSHA2_UNROLL_TRANSFORM -o sha2 sha2.c sha2prog.c
+ *
+ * or define below:
+ *
+ *   #define SHA2_UNROLL_TRANSFORM
+ *
+ */
+
+#include "acr.h"
+#include "acr_private.h"
+#include "acr_error.h"
+#include "acr_string.h"
+#include "acr_clazz.h"
+#include "acr_crypto.h"
+
+static const char basis16[] =
+    "0123456789abcdef";
+
+#define HI_NIBBLE_HEX(a) basis16[(a) >> 4]
+#define LO_NIBBLE_HEX(a) basis16[(a) & 0x0F]
+
+
+/*** SHA-256/384/512 Various Length Definitions ***********************/
+/* NOTE: Most of these are in sha2.h */
+#define ACR_SHA256_SHORT_BLOCK_LENGTH   (ACR_SHA256_BLOCK_LENGTH -  8)
+#define ACR_SHA384_SHORT_BLOCK_LENGTH   (ACR_SHA384_BLOCK_LENGTH - 16)
+#define ACR_SHA512_SHORT_BLOCK_LENGTH   (ACR_SHA512_BLOCK_LENGTH - 16)
+
+/*** ENDIAN SPECIFIC COPY MACROS **************************************/
+#define BE_8_TO_32(dst, cp) do {                                            \
+    (dst) = (acr_uint32_t)(cp)[3] | ((acr_uint32_t)(cp)[2] << 8) |          \
+        ((acr_uint32_t)(cp)[1] << 16) | ((acr_uint32_t)(cp)[0] << 24);      \
+} while(0)
+
+#define BE_8_TO_64(dst, cp) do {                                            \
+    (dst) = (acr_uint64_t)(cp)[7] | ((acr_uint64_t)(cp)[6] << 8) |          \
+        ((acr_uint64_t)(cp)[5] << 16) | ((acr_uint64_t)(cp)[4] << 24) |     \
+        ((acr_uint64_t)(cp)[3] << 32) | ((acr_uint64_t)(cp)[2] << 40) |     \
+        ((acr_uint64_t)(cp)[1] << 48) | ((acr_uint64_t)(cp)[0] << 56);      \
+} while (0)
+
+#define BE_64_TO_8(cp, src) do {                                            \
+    (cp)[0] = (acr_byte_t)(((src) >> 56) & 0xFF);                           \
+    (cp)[1] = (acr_byte_t)(((src) >> 48) & 0xFF);                           \
+    (cp)[2] = (acr_byte_t)(((src) >> 40) & 0xFF);                           \
+    (cp)[3] = (acr_byte_t)(((src) >> 32) & 0xFF);                           \
+    (cp)[4] = (acr_byte_t)(((src) >> 24) & 0xFF);                           \
+    (cp)[5] = (acr_byte_t)(((src) >> 16) & 0xFF);                           \
+    (cp)[6] = (acr_byte_t)(((src) >>  8) & 0xFF);                           \
+    (cp)[7] = (acr_byte_t)(((src) >>  0) & 0xFF);                           \
+} while (0)
+
+#define BE_32_TO_8(cp, src) do {                                            \
+    (cp)[0] = (acr_byte_t)(((src) >> 24) & 0xFF);                           \
+    (cp)[1] = (acr_byte_t)(((src) >> 16) & 0xFF);                           \
+    (cp)[2] = (acr_byte_t)(((src) >>  8) & 0xFF);                           \
+    (cp)[3] = (acr_byte_t)(((src) >>  0) & 0xFF);                           \
+} while (0)
+
+/*
+ * Macro for incrementally adding the unsigned 64-bit integer n to the
+ * unsigned 128-bit integer (represented using a two-element array of
+ * 64-bit words):
+ */
+#define ADDINC128(w,n) do {                         \
+    (w)[0] += (acr_uint64_t)(n);                    \
+    if ((w)[0] < (n)) {                             \
+        (w)[1]++;                                   \
+    }                                               \
+} while (0)
+
+/*** THE SIX LOGICAL FUNCTIONS ****************************************/
+/*
+ * Bit shifting and rotation (used by the six SHA-XYZ logical functions:
+ *
+ *   NOTE:  The naming of R and S appears backwards here (R is a SHIFT and
+ *   S is a ROTATION) because the SHA-256/384/512 description document
+ *   (see http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf) uses this
+ *   same "backwards" definition.
+ */
+/* Shift-right (used in SHA-256, SHA-384, and SHA-512): */
+#define R(b,x)      ((x) >> (b))
+/* 32-bit Rotate-right (used in SHA-256): */
+#define S32(b,x)    (((x) >> (b)) | ((x) << (32 - (b))))
+/* 64-bit Rotate-right (used in SHA-384 and SHA-512): */
+#define S64(b,x)    (((x) >> (b)) | ((x) << (64 - (b))))
+
+/* Two of six logical functions used in SHA-256, SHA-384, and SHA-512: */
+#define Ch(x,y,z)   (((x) & (y)) ^ ((~(x)) & (z)))
+#define Maj(x,y,z)  (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
+
+/* Four of six logical functions used in SHA-256: */
+#define Sigma0_256(x)   (S32(2,  (x)) ^ S32(13, (x)) ^ S32(22, (x)))
+#define Sigma1_256(x)   (S32(6,  (x)) ^ S32(11, (x)) ^ S32(25, (x)))
+#define sigma0_256(x)   (S32(7,  (x)) ^ S32(18, (x)) ^ R(3 ,   (x)))
+#define sigma1_256(x)   (S32(17, (x)) ^ S32(19, (x)) ^ R(10,   (x)))
+
+/* Four of six logical functions used in SHA-384 and SHA-512: */
+#define Sigma0_512(x)   (S64(28, (x)) ^ S64(34, (x)) ^ S64(39, (x)))
+#define Sigma1_512(x)   (S64(14, (x)) ^ S64(18, (x)) ^ S64(41, (x)))
+#define sigma0_512(x)   (S64( 1, (x)) ^ S64( 8, (x)) ^ R( 7,   (x)))
+#define sigma1_512(x)   (S64(19, (x)) ^ S64(61, (x)) ^ R( 6,   (x)))
+
+
+/*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/
+/* Hash constant words K for SHA-256: */
+const static acr_uint32_t K256[64] = {
+    0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
+    0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
+    0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
+    0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
+    0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
+    0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
+    0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
+    0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
+    0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
+    0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
+    0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
+    0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
+    0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
+    0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
+    0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
+    0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
+};
+
+/* Initial hash value H for SHA-256: */
+const static acr_uint32_t sha256_initial_hash_value[8] = {
+    0x6a09e667,
+    0xbb67ae85,
+    0x3c6ef372,
+    0xa54ff53a,
+    0x510e527f,
+    0x9b05688c,
+    0x1f83d9ab,
+    0x5be0cd19
+};
+
+/* Hash constant words K for SHA-384 and SHA-512: */
+const static acr_uint64_t K512[80] = {
+    ACR_UINT64_C(0x428a2f98d728ae22), ACR_UINT64_C(0x7137449123ef65cd),
+    ACR_UINT64_C(0xb5c0fbcfec4d3b2f), ACR_UINT64_C(0xe9b5dba58189dbbc),
+    ACR_UINT64_C(0x3956c25bf348b538), ACR_UINT64_C(0x59f111f1b605d019),
+    ACR_UINT64_C(0x923f82a4af194f9b), ACR_UINT64_C(0xab1c5ed5da6d8118),
+    ACR_UINT64_C(0xd807aa98a3030242), ACR_UINT64_C(0x12835b0145706fbe),
+    ACR_UINT64_C(0x243185be4ee4b28c), ACR_UINT64_C(0x550c7dc3d5ffb4e2),
+    ACR_UINT64_C(0x72be5d74f27b896f), ACR_UINT64_C(0x80deb1fe3b1696b1),
+    ACR_UINT64_C(0x9bdc06a725c71235), ACR_UINT64_C(0xc19bf174cf692694),
+    ACR_UINT64_C(0xe49b69c19ef14ad2), ACR_UINT64_C(0xefbe4786384f25e3),
+    ACR_UINT64_C(0x0fc19dc68b8cd5b5), ACR_UINT64_C(0x240ca1cc77ac9c65),
+    ACR_UINT64_C(0x2de92c6f592b0275), ACR_UINT64_C(0x4a7484aa6ea6e483),
+    ACR_UINT64_C(0x5cb0a9dcbd41fbd4), ACR_UINT64_C(0x76f988da831153b5),
+    ACR_UINT64_C(0x983e5152ee66dfab), ACR_UINT64_C(0xa831c66d2db43210),
+    ACR_UINT64_C(0xb00327c898fb213f), ACR_UINT64_C(0xbf597fc7beef0ee4),
+    ACR_UINT64_C(0xc6e00bf33da88fc2), ACR_UINT64_C(0xd5a79147930aa725),
+    ACR_UINT64_C(0x06ca6351e003826f), ACR_UINT64_C(0x142929670a0e6e70),
+    ACR_UINT64_C(0x27b70a8546d22ffc), ACR_UINT64_C(0x2e1b21385c26c926),
+    ACR_UINT64_C(0x4d2c6dfc5ac42aed), ACR_UINT64_C(0x53380d139d95b3df),
+    ACR_UINT64_C(0x650a73548baf63de), ACR_UINT64_C(0x766a0abb3c77b2a8),
+    ACR_UINT64_C(0x81c2c92e47edaee6), ACR_UINT64_C(0x92722c851482353b),
+    ACR_UINT64_C(0xa2bfe8a14cf10364), ACR_UINT64_C(0xa81a664bbc423001),
+    ACR_UINT64_C(0xc24b8b70d0f89791), ACR_UINT64_C(0xc76c51a30654be30),
+    ACR_UINT64_C(0xd192e819d6ef5218), ACR_UINT64_C(0xd69906245565a910),
+    ACR_UINT64_C(0xf40e35855771202a), ACR_UINT64_C(0x106aa07032bbd1b8),
+    ACR_UINT64_C(0x19a4c116b8d2d0c8), ACR_UINT64_C(0x1e376c085141ab53),
+    ACR_UINT64_C(0x2748774cdf8eeb99), ACR_UINT64_C(0x34b0bcb5e19b48a8),
+    ACR_UINT64_C(0x391c0cb3c5c95a63), ACR_UINT64_C(0x4ed8aa4ae3418acb),
+    ACR_UINT64_C(0x5b9cca4f7763e373), ACR_UINT64_C(0x682e6ff3d6b2b8a3),
+    ACR_UINT64_C(0x748f82ee5defb2fc), ACR_UINT64_C(0x78a5636f43172f60),
+    ACR_UINT64_C(0x84c87814a1f0ab72), ACR_UINT64_C(0x8cc702081a6439ec),
+    ACR_UINT64_C(0x90befffa23631e28), ACR_UINT64_C(0xa4506cebde82bde9),
+    ACR_UINT64_C(0xbef9a3f7b2c67915), ACR_UINT64_C(0xc67178f2e372532b),
+    ACR_UINT64_C(0xca273eceea26619c), ACR_UINT64_C(0xd186b8c721c0c207),
+    ACR_UINT64_C(0xeada7dd6cde0eb1e), ACR_UINT64_C(0xf57d4f7fee6ed178),
+    ACR_UINT64_C(0x06f067aa72176fba), ACR_UINT64_C(0x0a637dc5a2c898a6),
+    ACR_UINT64_C(0x113f9804bef90dae), ACR_UINT64_C(0x1b710b35131c471b),
+    ACR_UINT64_C(0x28db77f523047d84), ACR_UINT64_C(0x32caab7b40c72493),
+    ACR_UINT64_C(0x3c9ebe0a15c9bebc), ACR_UINT64_C(0x431d67c49c100d4c),
+    ACR_UINT64_C(0x4cc5d4becb3e42b6), ACR_UINT64_C(0x597f299cfc657e2a),
+    ACR_UINT64_C(0x5fcb6fab3ad6faec), ACR_UINT64_C(0x6c44198c4a475817)
+};
+
+/* Initial hash value H for SHA-384 */
+const static acr_uint64_t sha384_initial_hash_value[8] = {
+    ACR_UINT64_C(0xcbbb9d5dc1059ed8),
+    ACR_UINT64_C(0x629a292a367cd507),
+    ACR_UINT64_C(0x9159015a3070dd17),
+    ACR_UINT64_C(0x152fecd8f70e5939),
+    ACR_UINT64_C(0x67332667ffc00b31),
+    ACR_UINT64_C(0x8eb44a8768581511),
+    ACR_UINT64_C(0xdb0c2e0d64f98fa7),
+    ACR_UINT64_C(0x47b5481dbefa4fa4)
+};
+
+/* Initial hash value H for SHA-512 */
+const static acr_uint64_t sha512_initial_hash_value[8] = {
+    ACR_UINT64_C(0x6a09e667f3bcc908),
+    ACR_UINT64_C(0xbb67ae8584caa73b),
+    ACR_UINT64_C(0x3c6ef372fe94f82b),
+    ACR_UINT64_C(0xa54ff53a5f1d36f1),
+    ACR_UINT64_C(0x510e527fade682d1),
+    ACR_UINT64_C(0x9b05688c2b3e6c1f),
+    ACR_UINT64_C(0x1f83d9abfb41bd6b),
+    ACR_UINT64_C(0x5be0cd19137e2179)
+};
+
+/*** SHA-256: *********************************************************/
+ACR_DECLARE(void)
+ACR_SHA256Init(acr_sha2_ctx_t *context)
+{
+    if (context == NULL)
+        return;
+    memcpy(context->state.st32, sha256_initial_hash_value,
+        sizeof(sha256_initial_hash_value));
+    memset(context->buffer, 0, sizeof(context->buffer));
+    context->bitcount[0] = 0;
+}
+
+#ifdef SHA2_UNROLL_TRANSFORM
+
+/* Unrolled SHA-256 round macros: */
+
+#define ROUND256_0_TO_15(a,b,c,d,e,f,g,h) do {                              \
+    BE_8_TO_32(W256[j], data);                                              \
+    data += 4;                                                              \
+    T1 = (h) + Sigma1_256((e)) + Ch((e), (f), (g)) + K256[j] + W256[j];     \
+    (d) += T1;                                                              \
+    (h) = T1 + Sigma0_256((a)) + Maj((a), (b), (c));                        \
+    j++;                                                                    \
+} while(0)
+
+#define ROUND256(a,b,c,d,e,f,g,h) do {                                      \
+    s0 = W256[(j+1)&0x0f];                                                  \
+    s0 = sigma0_256(s0);                                                    \
+    s1 = W256[(j+14)&0x0f];                                                 \
+    s1 = sigma1_256(s1);                                                    \
+    T1 = (h) + Sigma1_256((e)) + Ch((e), (f), (g)) + K256[j] +              \
+         (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0);                      \
+    (d) += T1;                                                              \
+    (h) = T1 + Sigma0_256((a)) + Maj((a), (b), (c));                        \
+    j++;                                                                    \
+} while(0)
+
+static void
+acr_SHA256Transform(acr_uint32_t state[8],
+                    const acr_byte_t data[ACR_SHA256_BLOCK_LENGTH])
+{
+    acr_uint32_t   a, b, c, d, e, f, g, h, s0, s1;
+    acr_uint32_t   T1, W256[16];
+    int     j;
+
+    /* Initialize registers with the prev. intermediate value */
+    a = state[0];
+    b = state[1];
+    c = state[2];
+    d = state[3];
+    e = state[4];
+    f = state[5];
+    g = state[6];
+    h = state[7];
+
+    j = 0;
+    do {
+        /* Rounds 0 to 15 (unrolled): */
+        ROUND256_0_TO_15(a,b,c,d,e,f,g,h);
+        ROUND256_0_TO_15(h,a,b,c,d,e,f,g);
+        ROUND256_0_TO_15(g,h,a,b,c,d,e,f);
+        ROUND256_0_TO_15(f,g,h,a,b,c,d,e);
+        ROUND256_0_TO_15(e,f,g,h,a,b,c,d);
+        ROUND256_0_TO_15(d,e,f,g,h,a,b,c);
+        ROUND256_0_TO_15(c,d,e,f,g,h,a,b);
+        ROUND256_0_TO_15(b,c,d,e,f,g,h,a);
+    } while (j < 16);
+
+    /* Now for the remaining rounds up to 63: */
+    do {
+        ROUND256(a,b,c,d,e,f,g,h);
+        ROUND256(h,a,b,c,d,e,f,g);
+        ROUND256(g,h,a,b,c,d,e,f);
+        ROUND256(f,g,h,a,b,c,d,e);
+        ROUND256(e,f,g,h,a,b,c,d);
+        ROUND256(d,e,f,g,h,a,b,c);
+        ROUND256(c,d,e,f,g,h,a,b);
+        ROUND256(b,c,d,e,f,g,h,a);
+    } while (j < 64);
+
+    /* Compute the current intermediate hash value */
+    state[0] += a;
+    state[1] += b;
+    state[2] += c;
+    state[3] += d;
+    state[4] += e;
+    state[5] += f;
+    state[6] += g;
+    state[7] += h;
+
+    /* Clean up */
+    a = b = c = d = e = f = g = h = T1 = 0;
+}
+
+#else /* SHA2_UNROLL_TRANSFORM */
+
+static void
+acr_SHA256Transform(acr_uint32_t state[8],
+                    const acr_byte_t data[ACR_SHA256_BLOCK_LENGTH])
+{
+    acr_uint32_t   a, b, c, d, e, f, g, h, s0, s1;
+    acr_uint32_t   T1, T2, W256[16];
+    int     j;
+
+    /* Initialize registers with the prev. intermediate value */
+    a = state[0];
+    b = state[1];
+    c = state[2];
+    d = state[3];
+    e = state[4];
+    f = state[5];
+    g = state[6];
+    h = state[7];
+
+    j = 0;
+    do {
+        BE_8_TO_32(W256[j], data);
+        data += 4;
+        /* Apply the SHA-256 compression function to update a..h */
+        T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + W256[j];
+        T2 = Sigma0_256(a) + Maj(a, b, c);
+        h = g;
+        g = f;
+        f = e;
+        e = d + T1;
+        d = c;
+        c = b;
+        b = a;
+        a = T1 + T2;
+
+        j++;
+    } while (j < 16);
+
+    do {
+        /* Part of the message block expansion: */
+        s0 = W256[(j+1)&0x0f];
+        s0 = sigma0_256(s0);
+        s1 = W256[(j+14)&0x0f]; 
+        s1 = sigma1_256(s1);
+
+        /* Apply the SHA-256 compression function to update a..h */
+        T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + 
+             (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0);
+        T2 = Sigma0_256(a) + Maj(a, b, c);
+        h = g;
+        g = f;
+        f = e;
+        e = d + T1;
+        d = c;
+        c = b;
+        b = a;
+        a = T1 + T2;
+
+        j++;
+    } while (j < 64);
+
+    /* Compute the current intermediate hash value */
+    state[0] += a;
+    state[1] += b;
+    state[2] += c;
+    state[3] += d;
+    state[4] += e;
+    state[5] += f;
+    state[6] += g;
+    state[7] += h;
+
+    /* Clean up */
+    a = b = c = d = e = f = g = h = T1 = T2 = 0;
+}
+
+#endif /* SHA2_UNROLL_TRANSFORM */
+
+ACR_DECLARE(void)
+ACR_SHA256Update(acr_sha2_ctx_t *context, const acr_byte_t *data, size_t len)
+{
+    size_t  freespace, usedspace;
+
+    /* Calling with no data is valid (we do nothing) */
+    if (len == 0)
+        return;
+
+    usedspace = (context->bitcount[0] >> 3) % ACR_SHA256_BLOCK_LENGTH;
+    if (usedspace > 0) {
+        /* Calculate how much free space is available in the buffer */
+        freespace = ACR_SHA256_BLOCK_LENGTH - usedspace;
+
+        if (len >= freespace) {
+            /* Fill the buffer completely and process it */
+            memcpy(&context->buffer[usedspace], data, freespace);
+            context->bitcount[0] += freespace << 3;
+            len -= freespace;
+            data += freespace;
+            acr_SHA256Transform(context->state.st32, context->buffer);
+        } else {
+            /* The buffer is not yet full */
+            memcpy(&context->buffer[usedspace], data, len);
+            context->bitcount[0] += len << 3;
+            /* Clean up: */
+            usedspace = freespace = 0;
+            return;
+        }
+    }
+    while (len >= ACR_SHA256_BLOCK_LENGTH) {
+        /* Process as many complete blocks as we can */
+        acr_SHA256Transform(context->state.st32, data);
+        context->bitcount[0] += ACR_SHA256_BLOCK_LENGTH << 3;
+        len -= ACR_SHA256_BLOCK_LENGTH;
+        data += ACR_SHA256_BLOCK_LENGTH;
+    }
+    if (len > 0) {
+        /* There's left-overs, so save 'em */
+        memcpy(context->buffer, data, len);
+        context->bitcount[0] += len << 3;
+    }
+    /* Clean up: */
+    usedspace = freespace = 0;
+}
+
+static void
+acr_SHA256Pad(acr_sha2_ctx_t *context)
+{
+    unsigned int    usedspace;
+
+    usedspace = (context->bitcount[0] >> 3) % ACR_SHA256_BLOCK_LENGTH;
+    if (usedspace > 0) {
+        /* Begin padding with a 1 bit: */
+        context->buffer[usedspace++] = 0x80;
+
+        if (usedspace <= ACR_SHA256_SHORT_BLOCK_LENGTH) {
+            /* Set-up for the last transform: */
+            memset(&context->buffer[usedspace], 0,
+                ACR_SHA256_SHORT_BLOCK_LENGTH - usedspace);
+        } else {
+            if (usedspace < ACR_SHA256_BLOCK_LENGTH) {
+                memset(&context->buffer[usedspace], 0,
+                    ACR_SHA256_BLOCK_LENGTH - usedspace);
+            }
+            /* Do second-to-last transform: */
+            acr_SHA256Transform(context->state.st32, context->buffer);
+
+            /* Prepare for last transform: */
+            memset(context->buffer, 0, ACR_SHA256_SHORT_BLOCK_LENGTH);
+        }
+    } else {
+        /* Set-up for the last transform: */
+        memset(context->buffer, 0, ACR_SHA256_SHORT_BLOCK_LENGTH);
+
+        /* Begin padding with a 1 bit: */
+        *context->buffer = 0x80;
+    }
+    /* Store the length of input data (in bits) in big endian format: */
+    BE_64_TO_8(&context->buffer[ACR_SHA256_SHORT_BLOCK_LENGTH],
+        context->bitcount[0]);
+
+    /* Final transform: */
+    acr_SHA256Transform(context->state.st32, context->buffer);
+
+    /* Clean up: */
+    usedspace = 0;
+}
+
+ACR_DECLARE(void)
+ACR_SHA256Final(acr_byte_t digest[ACR_SHA256_DIGEST_LENGTH], acr_sha2_ctx_t 
*context)
+{
+    acr_SHA256Pad(context);
+
+    /* If no digest buffer is passed, we don't bother doing this: */
+    if (digest != NULL) {
+#if !CC_IS_BIG_ENDIAN
+        int i;
+
+        /* Convert TO host byte order */
+        for (i = 0; i < 8; i++)
+            BE_32_TO_8(digest + i * 4, context->state.st32[i]);
+#else
+        memcpy(digest, context->state.st32, ACR_SHA256_DIGEST_LENGTH);
+#endif
+        memset(context, 0, sizeof(*context));
+    }
+}
+
+ACR_DECLARE(char *) ACR_SHA256EncodeA(const char *clear, size_t len, char *out)
+{
+    int i, x = 0;
+    acr_sha2_ctx_t context;
+    acr_byte_t digest[ACR_SHA256_DIGEST_LENGTH];
+
+    if (out == NULL && (out = malloc(ACR_SHA256_DIGEST_STRING_LENGTH)) == NULL)
+        return NULL;
+
+    ACR_SHA256Init(&context);
+    ACR_SHA256Update(&context, (acr_byte_t *)clear, len);
+    ACR_SHA256Final(digest, &context);
+    for (i = 0; i < ACR_SHA256_DIGEST_LENGTH; i++) {
+        out[x++] = HI_NIBBLE_HEX(digest[i]);
+        out[x++] = LO_NIBBLE_HEX(digest[i]);
+    }
+    out[x] = '\0';
+
+    memset(digest, 0, sizeof(digest));
+    return out;
+}
+
+ACR_DECLARE(wchar_t *) ACR_SHA256EncodeW(const wchar_t *clear, size_t len,
+                                         wchar_t *out)
+{
+    int i, x = 0;
+    acr_sha2_ctx_t context;
+    acr_byte_t digest[ACR_SHA256_DIGEST_LENGTH];
+
+    if (out == NULL &&
+       (out = malloc(ACR_SHA256_DIGEST_STRING_LENGTH * sizeof(wchar_t))) == 
NULL)
+        return NULL;
+
+    ACR_SHA256Init(&context);
+    ACR_SHA256Update(&context, (acr_byte_t *)clear, len * sizeof(wchar_t));
+    ACR_SHA256Final(digest, &context);
+    for (i = 0; i < ACR_SHA256_DIGEST_LENGTH; i++) {
+        out[x++] = HI_NIBBLE_HEX(digest[i]);
+        out[x++] = LO_NIBBLE_HEX(digest[i]);
+    }
+    out[x] = L'\0';
+
+    memset(digest, 0, sizeof(digest));
+    return out;
+}
+
+/*** SHA-512: *********************************************************/
+ACR_DECLARE(void)
+ACR_SHA512Init(acr_sha2_ctx_t *context)
+{
+    if (context == NULL)
+        return;
+    memcpy(context->state.st64, sha512_initial_hash_value,
+        sizeof(sha512_initial_hash_value));
+    memset(context->buffer, 0, sizeof(context->buffer));
+    context->bitcount[0] = context->bitcount[1] =  0;
+}
+
+#ifdef SHA2_UNROLL_TRANSFORM
+
+/* Unrolled SHA-512 round macros: */
+
+#define ROUND512_0_TO_15(a,b,c,d,e,f,g,h) do {                              \
+    BE_8_TO_64(W512[j], data);                                              \
+    data += 8;                                                              \
+    T1 = (h) + Sigma1_512((e)) + Ch((e), (f), (g)) + K512[j] + W512[j];     \
+    (d) += T1;                                                              \
+    (h) = T1 + Sigma0_512((a)) + Maj((a), (b), (c));                        \
+    j++;                                                                    \
+} while(0)
+
+
+#define ROUND512(a,b,c,d,e,f,g,h) do {                                      \
+    s0 = W512[(j+1)&0x0f];                                                  \
+    s0 = sigma0_512(s0);                                                    \
+    s1 = W512[(j+14)&0x0f];                                                 \
+    s1 = sigma1_512(s1);                                                    \
+    T1 = (h) + Sigma1_512((e)) + Ch((e), (f), (g)) + K512[j] +              \
+             (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0);                  \
+    (d) += T1;                                                              \
+    (h) = T1 + Sigma0_512((a)) + Maj((a), (b), (c));                        \
+    j++;                                                                    \
+} while(0)
+
+static void
+acr_SHA512Transform(acr_uint64_t state[8],
+                    const acr_byte_t data[ACR_SHA512_BLOCK_LENGTH])
+{
+    acr_uint64_t   a, b, c, d, e, f, g, h, s0, s1;
+    acr_uint64_t   T1, W512[16];
+    int     j;
+
+    /* Initialize registers with the prev. intermediate value */
+    a = state[0];
+    b = state[1];
+    c = state[2];
+    d = state[3];
+    e = state[4];
+    f = state[5];
+    g = state[6];
+    h = state[7];
+
+    j = 0;
+    do {
+        /* Rounds 0 to 15 (unrolled): */
+        ROUND512_0_TO_15(a,b,c,d,e,f,g,h);
+        ROUND512_0_TO_15(h,a,b,c,d,e,f,g);
+        ROUND512_0_TO_15(g,h,a,b,c,d,e,f);
+        ROUND512_0_TO_15(f,g,h,a,b,c,d,e);
+        ROUND512_0_TO_15(e,f,g,h,a,b,c,d);
+        ROUND512_0_TO_15(d,e,f,g,h,a,b,c);
+        ROUND512_0_TO_15(c,d,e,f,g,h,a,b);
+        ROUND512_0_TO_15(b,c,d,e,f,g,h,a);
+    } while (j < 16);
+
+    /* Now for the remaining rounds up to 79: */
+    do {
+        ROUND512(a,b,c,d,e,f,g,h);
+        ROUND512(h,a,b,c,d,e,f,g);
+        ROUND512(g,h,a,b,c,d,e,f);
+        ROUND512(f,g,h,a,b,c,d,e);
+        ROUND512(e,f,g,h,a,b,c,d);
+        ROUND512(d,e,f,g,h,a,b,c);
+        ROUND512(c,d,e,f,g,h,a,b);
+        ROUND512(b,c,d,e,f,g,h,a);
+    } while (j < 80);
+
+    /* Compute the current intermediate hash value */
+    state[0] += a;
+    state[1] += b;
+    state[2] += c;
+    state[3] += d;
+    state[4] += e;
+    state[5] += f;
+    state[6] += g;
+    state[7] += h;
+
+    /* Clean up */
+    a = b = c = d = e = f = g = h = T1 = 0;
+}
+
+#else /* SHA2_UNROLL_TRANSFORM */
+
+static void
+acr_SHA512Transform(acr_uint64_t state[8],
+                    const acr_byte_t data[ACR_SHA512_BLOCK_LENGTH])
+{
+    acr_uint64_t   a, b, c, d, e, f, g, h, s0, s1;
+    acr_uint64_t   T1, T2, W512[16];
+    int     j;
+
+    /* Initialize registers with the prev. intermediate value */
+    a = state[0];
+    b = state[1];
+    c = state[2];
+    d = state[3];
+    e = state[4];
+    f = state[5];
+    g = state[6];
+    h = state[7];
+
+    j = 0;
+    do {
+        BE_8_TO_64(W512[j], data);
+        data += 8;
+        /* Apply the SHA-512 compression function to update a..h */
+        T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + W512[j];
+        T2 = Sigma0_512(a) + Maj(a, b, c);
+        h = g;
+        g = f;
+        f = e;
+        e = d + T1;
+        d = c;
+        c = b;
+        b = a;
+        a = T1 + T2;
+
+        j++;
+    } while (j < 16);
+
+    do {
+        /* Part of the message block expansion: */
+        s0 = W512[(j+1)&0x0f];
+        s0 = sigma0_512(s0);
+        s1 = W512[(j+14)&0x0f];
+        s1 =  sigma1_512(s1);
+
+        /* Apply the SHA-512 compression function to update a..h */
+        T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] +
+             (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0);
+        T2 = Sigma0_512(a) + Maj(a, b, c);
+        h = g;
+        g = f;
+        f = e;
+        e = d + T1;
+        d = c;
+        c = b;
+        b = a;
+        a = T1 + T2;
+
+        j++;
+    } while (j < 80);
+
+    /* Compute the current intermediate hash value */
+    state[0] += a;
+    state[1] += b;
+    state[2] += c;
+    state[3] += d;
+    state[4] += e;
+    state[5] += f;
+    state[6] += g;
+    state[7] += h;
+
+    /* Clean up */
+    a = b = c = d = e = f = g = h = T1 = T2 = 0;
+}
+
+#endif /* SHA2_UNROLL_TRANSFORM */
+
+ACR_DECLARE(void)
+ACR_SHA512Update(acr_sha2_ctx_t *context, const acr_byte_t *data, size_t len)
+{
+    size_t  freespace, usedspace;
+
+    /* Calling with no data is valid (we do nothing) */
+    if (len == 0)
+        return;
+
+    usedspace = (context->bitcount[0] >> 3) % ACR_SHA512_BLOCK_LENGTH;
+    if (usedspace > 0) {
+        /* Calculate how much free space is available in the buffer */
+        freespace = ACR_SHA512_BLOCK_LENGTH - usedspace;
+
+        if (len >= freespace) {
+            /* Fill the buffer completely and process it */
+            memcpy(&context->buffer[usedspace], data, freespace);
+            ADDINC128(context->bitcount, freespace << 3);
+            len -= freespace;
+            data += freespace;
+            acr_SHA512Transform(context->state.st64, context->buffer);
+        } else {
+            /* The buffer is not yet full */
+            memcpy(&context->buffer[usedspace], data, len);
+            ADDINC128(context->bitcount, len << 3);
+            /* Clean up: */
+            usedspace = freespace = 0;
+            return;
+        }
+    }
+    while (len >= ACR_SHA512_BLOCK_LENGTH) {
+        /* Process as many complete blocks as we can */
+        acr_SHA512Transform(context->state.st64, data);
+        ADDINC128(context->bitcount, ACR_SHA512_BLOCK_LENGTH << 3);
+        len -= ACR_SHA512_BLOCK_LENGTH;
+        data += ACR_SHA512_BLOCK_LENGTH;
+    }
+    if (len > 0) {
+        /* There's left-overs, so save 'em */
+        memcpy(context->buffer, data, len);
+        ADDINC128(context->bitcount, len << 3);
+    }
+    /* Clean up: */
+    usedspace = freespace = 0;
+}
+
+void
+acr_SHA512Pad(acr_sha2_ctx_t *context)
+{
+    unsigned int    usedspace;
+
+    usedspace = (context->bitcount[0] >> 3) % ACR_SHA512_BLOCK_LENGTH;
+    if (usedspace > 0) {
+        /* Begin padding with a 1 bit: */
+        context->buffer[usedspace++] = 0x80;
+
+        if (usedspace <= ACR_SHA512_SHORT_BLOCK_LENGTH) {
+            /* Set-up for the last transform: */
+            memset(&context->buffer[usedspace], 0, 
ACR_SHA512_SHORT_BLOCK_LENGTH - usedspace);
+        } else {
+            if (usedspace < ACR_SHA512_BLOCK_LENGTH) {
+                memset(&context->buffer[usedspace], 0, ACR_SHA512_BLOCK_LENGTH 
- usedspace);
+            }
+            /* Do second-to-last transform: */
+            acr_SHA512Transform(context->state.st64, context->buffer);
+
+            /* And set-up for the last transform: */
+            memset(context->buffer, 0, ACR_SHA512_BLOCK_LENGTH - 2);
+        }
+    } else {
+        /* Prepare for final transform: */
+        memset(context->buffer, 0, ACR_SHA512_SHORT_BLOCK_LENGTH);
+
+        /* Begin padding with a 1 bit: */
+        *context->buffer = 0x80;
+    }
+    /* Store the length of input data (in bits) in big endian format: */
+    BE_64_TO_8(&context->buffer[ACR_SHA512_SHORT_BLOCK_LENGTH],
+        context->bitcount[1]);
+    BE_64_TO_8(&context->buffer[ACR_SHA512_SHORT_BLOCK_LENGTH + 8],
+        context->bitcount[0]);
+
+    /* Final transform: */
+    acr_SHA512Transform(context->state.st64, context->buffer);
+
+    /* Clean up: */
+    usedspace = 0;
+}
+
+ACR_DECLARE(void)
+ACR_SHA512Final(acr_byte_t digest[ACR_SHA512_DIGEST_LENGTH],
+                acr_sha2_ctx_t *context)
+{
+    acr_SHA512Pad(context);
+
+    /* If no digest buffer is passed, we don't bother doing this: */
+    if (digest != NULL) {
+#if !CC_IS_BIG_ENDIAN
+        int i;
+
+        /* Convert TO host byte order */
+        for (i = 0; i < 8; i++)
+            BE_64_TO_8(digest + i * 8, context->state.st64[i]);
+#else
+        memcpy(digest, context->state.st64, ACR_SHA512_DIGEST_LENGTH);
+#endif
+        memset(context, 0, sizeof(*context));
+    }
+}
+
+ACR_DECLARE(char *) ACR_SHA512EncodeA(const char *clear, size_t len, char *out)
+{
+    int i, x = 0;
+    acr_sha2_ctx_t context;
+    acr_byte_t digest[ACR_SHA512_DIGEST_LENGTH];
+
+    if (out == NULL && (out = malloc(ACR_SHA512_DIGEST_STRING_LENGTH)) == NULL)
+        return NULL;
+
+    ACR_SHA512Init(&context);
+    ACR_SHA512Update(&context, (acr_byte_t *)clear, len);
+    ACR_SHA512Final(digest, &context);
+    for (i = 0; i < ACR_SHA512_DIGEST_LENGTH; i++) {
+        out[x++] = HI_NIBBLE_HEX(digest[i]);
+        out[x++] = LO_NIBBLE_HEX(digest[i]);
+    }
+    out[x] = '\0';
+
+    memset(digest, 0, sizeof(digest));
+    return out;
+}
+
+ACR_DECLARE(wchar_t *) ACR_SHA512EncodeW(const wchar_t *clear, size_t len,
+                                         wchar_t *out)
+{
+    int i, x = 0;
+    acr_sha2_ctx_t context;
+    acr_byte_t digest[ACR_SHA512_DIGEST_LENGTH];
+
+    if (out == NULL &&
+       (out = malloc(ACR_SHA512_DIGEST_STRING_LENGTH * sizeof(wchar_t))) == 
NULL)
+        return NULL;
+
+    ACR_SHA512Init(&context);
+    ACR_SHA512Update(&context, (acr_byte_t *)clear, len * sizeof(wchar_t));
+    ACR_SHA512Final(digest, &context);
+    for (i = 0; i < ACR_SHA512_DIGEST_LENGTH; i++) {
+        out[x++] = HI_NIBBLE_HEX(digest[i]);
+        out[x++] = LO_NIBBLE_HEX(digest[i]);
+    }
+    out[x] = L'\0';
+
+    memset(digest, 0, sizeof(digest));
+    return out;
+}
+

Propchange: commons/sandbox/runtime/trunk/src/main/native/shared/sha2.c
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to