Author: mturk
Date: Fri Aug 7 14:07:43 2009
New Revision: 802016
URL: http://svn.apache.org/viewvc?rev=802016&view=rev
Log:
Port APR's sha1 code
Added:
commons/sandbox/runtime/trunk/src/main/native/include/acr_crypto.h (with
props)
commons/sandbox/runtime/trunk/src/main/native/shared/sha.c (with props)
Modified:
commons/sandbox/runtime/trunk/src/main/native/Makefile.in
commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in
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=802016&r1=802015&r2=802016&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/Makefile.in (original)
+++ commons/sandbox/runtime/trunk/src/main/native/Makefile.in Fri Aug 7
14:07:43 2009
@@ -87,6 +87,7 @@
$(SRCDIR)/shared/nbb.$(OBJ) \
$(SRCDIR)/shared/pointer.$(OBJ) \
$(SRCDIR)/shared/object.$(OBJ) \
+ $(SRCDIR)/shared/sha.$(OBJ) \
$(SRCDIR)/shared/string.$(OBJ) \
$(SRCDIR)/shared/tables.$(OBJ) \
$(SRCDIR)/shared/xdr.$(OBJ) \
Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in?rev=802016&r1=802015&r2=802016&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in (original)
+++ commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in Fri Aug 7
14:07:43 2009
@@ -80,6 +80,7 @@
$(SRCDIR)/shared/nbb.$(OBJ) \
$(SRCDIR)/shared/pointer.$(OBJ) \
$(SRCDIR)/shared/object.$(OBJ) \
+ $(SRCDIR)/shared/sha.$(OBJ) \
$(SRCDIR)/shared/string.$(OBJ) \
$(SRCDIR)/shared/tables.$(OBJ) \
$(SRCDIR)/shared/xdr.$(OBJ) \
Added: 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=802016&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr_crypto.h (added)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr_crypto.h Fri Aug
7 14:07:43 2009
@@ -0,0 +1,132 @@
+/* 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.
+ */
+
+#ifndef _ACR_CRYPTO_H
+#define _ACR_CRYPTO_H
+
+#include "acr.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * @file acr_crypto.h
+ * @brief
+ *
+ * ACR Cryptographic functions
+ *
+ */
+
+/**
+ * Size of the SHA1 DIGEST
+ */
+#define ACR_SHA1_DIGESTSIZE 20
+
+/**
+ * Define the Magic String prefix that identifies a password as being
+ * hashed using our algorithm.
+ */
+#define ACR_SHA1PW_ID "{SHA}"
+
+/** length of the SHA Password */
+#define ACR_SHA1PW_IDLEN 5
+
+/** @see acr_sha1_ctx_t */
+typedef struct acr_sha1_ctx_t acr_sha1_ctx_t;
+
+/**
+ * SHA1 context structure
+ */
+struct acr_sha1_ctx_t {
+ /** message digest */
+ acr_uint32_t digest[5];
+ /** 64-bit bit counts */
+ acr_uint32_t count_lo, count_hi;
+ /** SHA data buffer */
+ acr_uint32_t data[16];
+ /** unprocessed amount in data */
+ int local;
+};
+
+/**
+ * Initialize the SHA digest
+ * @param context The SHA context to initialize
+ */
+ACR_DECLARE(void) ACR_Sha1Init(acr_sha1_ctx_t *context);
+
+/**
+ * Update the SHA digest with binary data
+ * @param context The SHA1 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_Sha1Update(acr_sha1_ctx_t *context,
+ const unsigned char *input,
+ unsigned int count);
+
+/**
+ * Update the SHA digest
+ * @param context The SHA1 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_Sha1UpdateA(acr_sha1_ctx_t *context,
+ const char *input,
+ unsigned int count);
+
+/**
+ * Update the SHA digest
+ * @param context The SHA1 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_Sha1UpdateW(acr_sha1_ctx_t *context,
+ const wchar_t *input,
+ unsigned int count);
+
+/**
+ * Finish computing the SHA digest
+ * @param digest the output buffer in which to store the digest
+ * @param context The context to finalize
+ */
+ACR_DECLARE(void) ACR_Sha1Final(unsigned char digest[ACR_SHA1_DIGESTSIZE],
+ acr_sha1_ctx_t *context);
+
+/**
+ * Provide a means to SHA1 crypt/encode a plaintext data using
+ * base 16 encoding.
+ * @param clear The plaintext data.
+ * @param len The length of the plaintext data
+ * @param out The encrypted/encoded password
+ */
+ACR_DECLARE(void) ACR_Sha1Base16A(const char *clear, int len, char *out);
+
+/**
+ * Provide a means to SHA1 crypt/encode a plaintext data using
+ * base 16 encoding.
+ * @param clear The plaintext data.
+ * @param len The length of the plaintext data
+ * @param out The encrypted/encoded password
+ */
+ACR_DECLARE(void) ACR_Sha1Base16W(const wchar_t *clear, int len, wchar_t *out);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _ACR_CRYPTO_H */
+
Propchange: commons/sandbox/runtime/trunk/src/main/native/include/acr_crypto.h
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/sandbox/runtime/trunk/src/main/native/shared/sha.c
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/sha.c?rev=802016&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/sha.c (added)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/sha.c Fri Aug 7
14:07:43 2009
@@ -0,0 +1,285 @@
+/* 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.
+ */
+
+/*
+ *
+ * @author Mladen Turk
+ */
+
+#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[((unsigned char)(a) >> 4)])
+#define LO_NIBBLE_HEX(a) (basis16[((unsigned char)(a) & 0x0F)])
+
+/* a bit faster & bigger, if defined */
+#define UNROLL_LOOPS
+
+/* NIST's proposed modification to SHA, 7/11/94 */
+#define USE_MODIFIED_SHA
+
+/* SHA f()-functions */
+#define f1(x,y,z) ((x & y) | (~x & z))
+#define f2(x,y,z) (x ^ y ^ z)
+#define f3(x,y,z) ((x & y) | (x & z) | (y & z))
+#define f4(x,y,z) (x ^ y ^ z)
+
+/* SHA constants */
+#define CONST1 0x5a827999L
+#define CONST2 0x6ed9eba1L
+#define CONST3 0x8f1bbcdcL
+#define CONST4 0xca62c1d6L
+
+/* 32-bit rotate */
+
+#define ROT32(x,n) ((x << n) | (x >> (32 - n)))
+
+#define FUNC(n,i) \
+ temp = ROT32(A,5) + f##n(B,C,D) + E + W[i] + CONST##n; \
+ E = D; D = C; C = ROT32(B,30); B = A; A = temp
+
+#define SHA_BLOCKSIZE 64
+
+/* do SHA transformation */
+static void sha_transform(acr_sha1_ctx_t *context)
+{
+ int i;
+ acr_uint32_t temp, A, B, C, D, E, W[80];
+
+ for (i = 0; i < 16; ++i) {
+ W[i] = context->data[i];
+ }
+ for (i = 16; i < 80; ++i) {
+ W[i] = W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16];
+#ifdef USE_MODIFIED_SHA
+ W[i] = ROT32(W[i], 1);
+#endif /* USE_MODIFIED_SHA */
+ }
+ A = context->digest[0];
+ B = context->digest[1];
+ C = context->digest[2];
+ D = context->digest[3];
+ E = context->digest[4];
+
+ FUNC(1, 0); FUNC(1, 1); FUNC(1, 2); FUNC(1, 3); FUNC(1, 4);
+ FUNC(1, 5); FUNC(1, 6); FUNC(1, 7); FUNC(1, 8); FUNC(1, 9);
+ FUNC(1,10); FUNC(1,11); FUNC(1,12); FUNC(1,13); FUNC(1,14);
+ FUNC(1,15); FUNC(1,16); FUNC(1,17); FUNC(1,18); FUNC(1,19);
+
+ FUNC(2,20); FUNC(2,21); FUNC(2,22); FUNC(2,23); FUNC(2,24);
+ FUNC(2,25); FUNC(2,26); FUNC(2,27); FUNC(2,28); FUNC(2,29);
+ FUNC(2,30); FUNC(2,31); FUNC(2,32); FUNC(2,33); FUNC(2,34);
+ FUNC(2,35); FUNC(2,36); FUNC(2,37); FUNC(2,38); FUNC(2,39);
+
+ FUNC(3,40); FUNC(3,41); FUNC(3,42); FUNC(3,43); FUNC(3,44);
+ FUNC(3,45); FUNC(3,46); FUNC(3,47); FUNC(3,48); FUNC(3,49);
+ FUNC(3,50); FUNC(3,51); FUNC(3,52); FUNC(3,53); FUNC(3,54);
+ FUNC(3,55); FUNC(3,56); FUNC(3,57); FUNC(3,58); FUNC(3,59);
+
+ FUNC(4,60); FUNC(4,61); FUNC(4,62); FUNC(4,63); FUNC(4,64);
+ FUNC(4,65); FUNC(4,66); FUNC(4,67); FUNC(4,68); FUNC(4,69);
+ FUNC(4,70); FUNC(4,71); FUNC(4,72); FUNC(4,73); FUNC(4,74);
+ FUNC(4,75); FUNC(4,76); FUNC(4,77); FUNC(4,78); FUNC(4,79);
+
+ context->digest[0] += A;
+ context->digest[1] += B;
+ context->digest[2] += C;
+ context->digest[3] += D;
+ context->digest[4] += E;
+}
+
+#if !CC_IS_BIG_ENDIAN
+/* Change endianness of data.
+ * count is the number of bytes to do an endian flip
+ */
+static void byte_reverse(acr_uint32_t *buffer, int count)
+{
+ int i;
+ acr_byte_t ct[4], *cp;
+
+ count /= sizeof(acr_uint32_t);
+ cp = (acr_byte_t *) buffer;
+ for (i = 0; i < count; ++i) {
+ ct[0] = cp[0];
+ ct[1] = cp[1];
+ ct[2] = cp[2];
+ ct[3] = cp[3];
+ cp[0] = ct[3];
+ cp[1] = ct[2];
+ cp[2] = ct[1];
+ cp[3] = ct[0];
+ cp += sizeof(acr_uint32_t);
+ }
+}
+#endif
+
+/* initialize the SHA digest */
+
+ACR_DECLARE(void) ACR_Sha1Init(acr_sha1_ctx_t *context)
+{
+ context->digest[0] = 0x67452301L;
+ context->digest[1] = 0xefcdab89L;
+ context->digest[2] = 0x98badcfeL;
+ context->digest[3] = 0x10325476L;
+ context->digest[4] = 0xc3d2e1f0L;
+ context->count_lo = 0L;
+ context->count_hi = 0L;
+ context->local = 0;
+}
+
+/*
+ * Update the SHA digest
+ */
+ACR_DECLARE(void) ACR_Sha1Update(acr_sha1_ctx_t *context,
+ const unsigned char *buffer,
+ unsigned int count)
+{
+ unsigned int i;
+
+ if ((context->count_lo + ((acr_uint32_t) count << 3)) < context->count_lo)
{
+ ++context->count_hi;
+ }
+ context->count_lo += (acr_uint32_t) count << 3;
+ context->count_hi += (acr_uint32_t) count >> 29;
+ if (context->local) {
+ i = SHA_BLOCKSIZE - context->local;
+ if (i > count) {
+ i = count;
+ }
+ memcpy(((acr_byte_t *) context->data) + context->local, buffer, i);
+ count -= i;
+ buffer += i;
+ context->local += i;
+ if (context->local == SHA_BLOCKSIZE) {
+#if !CC_IS_BIG_ENDIAN
+ byte_reverse(context->data, SHA_BLOCKSIZE);
+#endif
+ sha_transform(context);
+ }
+ else {
+ return;
+ }
+ }
+ while (count >= SHA_BLOCKSIZE) {
+ memcpy(context->data, buffer, SHA_BLOCKSIZE);
+ buffer += SHA_BLOCKSIZE;
+ count -= SHA_BLOCKSIZE;
+#if !CC_IS_BIG_ENDIAN
+ byte_reverse(context->data, SHA_BLOCKSIZE);
+#endif
+ sha_transform(context);
+ }
+ memcpy(context->data, buffer, count);
+ context->local = count;
+}
+
+ACR_DECLARE(void) ACR_Sha1UpdateA(acr_sha1_ctx_t *context,
+ const char *buf,
+ unsigned int count)
+{
+ ACR_Sha1Update(context, (const unsigned char *)buf, count);
+}
+
+ACR_DECLARE(void) ACR_Sha1UpdateW(acr_sha1_ctx_t *context,
+ const wchar_t *buf,
+ unsigned int count)
+{
+ ACR_Sha1Update(context, (const unsigned char *)buf,
+ count * sizeof(wchar_t));
+}
+
+/*
+ * Finish computing the SHA digest
+ */
+ACR_DECLARE(void) ACR_Sha1Final(unsigned char digest[ACR_SHA1_DIGESTSIZE],
+ acr_sha1_ctx_t *context)
+{
+ int count, i, j;
+ acr_uint32_t lo_bit_count, hi_bit_count, k;
+
+ lo_bit_count = context->count_lo;
+ hi_bit_count = context->count_hi;
+ count = (int) ((lo_bit_count >> 3) & 0x3f);
+ ((acr_byte_t *) context->data)[count++] = 0x80;
+
+ if (count > SHA_BLOCKSIZE - 8) {
+ memset(((acr_byte_t *) context->data) + count, 0, SHA_BLOCKSIZE -
count);
+#if !CC_IS_BIG_ENDIAN
+ byte_reverse(context->data, SHA_BLOCKSIZE);
+#endif
+ sha_transform(context);
+ memset((acr_byte_t *) context->data, 0, SHA_BLOCKSIZE - 8);
+ }
+ else {
+ memset(((acr_byte_t *) context->data) + count, 0,
+ SHA_BLOCKSIZE - 8 - count);
+ }
+#if !CC_IS_BIG_ENDIAN
+ byte_reverse(context->data, SHA_BLOCKSIZE);
+#endif
+ context->data[14] = hi_bit_count;
+ context->data[15] = lo_bit_count;
+ sha_transform(context);
+
+ for (i = 0, j = 0; j < ACR_SHA1_DIGESTSIZE; i++) {
+ k = context->digest[i];
+ digest[j++] = (unsigned char)((k >> 24) & 0xff);
+ digest[j++] = (unsigned char)((k >> 16) & 0xff);
+ digest[j++] = (unsigned char)((k >> 8 ) & 0xff);
+ digest[j++] = (unsigned char)( k & 0xff);
+ }
+}
+
+ACR_DECLARE(void) ACR_Sha1Base16A(const char *clear, int len, char *out)
+{
+ int i, x = 0;
+ acr_sha1_ctx_t context;
+ acr_byte_t digest[ACR_SHA1_DIGESTSIZE];
+
+ ACR_Sha1Init(&context);
+ ACR_Sha1UpdateA(&context, clear, len);
+ ACR_Sha1Final(digest, &context);
+ for (i = 0; i < ACR_SHA1_DIGESTSIZE; i++) {
+ out[x++] = HI_NIBBLE_HEX(digest[i]);
+ out[x++] = LO_NIBBLE_HEX(digest[i]);
+ }
+ out[x] = '\0';
+
+}
+
+ACR_DECLARE(void) ACR_Sha1Base16W(const wchar_t *clear, int len, wchar_t *out)
+{
+ int i, x = 0;
+ acr_sha1_ctx_t context;
+ acr_byte_t digest[ACR_SHA1_DIGESTSIZE];
+
+ ACR_Sha1Init(&context);
+ ACR_Sha1UpdateW(&context, clear, len);
+ ACR_Sha1Final(digest, &context);
+ for (i = 0; i < ACR_SHA1_DIGESTSIZE; i++) {
+ out[x++] = HI_NIBBLE_HEX(digest[i]);
+ out[x++] = LO_NIBBLE_HEX(digest[i]);
+ }
+ out[x] = L'\0';
+}
+
Propchange: commons/sandbox/runtime/trunk/src/main/native/shared/sha.c
------------------------------------------------------------------------------
svn:eol-style = native