use free() instead of delete() when realloc is used
[ntk/apt.git] / apt-pkg / contrib / sha2_internal.cc
index 10b82de..131ff5b 100644 (file)
@@ -31,7 +31,9 @@
  *
  * $Id: sha2.c,v 1.1 2001/11/08 00:01:51 adg Exp adg $
  */
+#include <config.h>
 
+#include <endian.h>
 #include <string.h>    /* memcpy()/memset() or bcopy()/bzero() */
 #include <assert.h>    /* assert() */
 #include "sha2_internal.h"
@@ -64,7 +66,7 @@
  * Please make sure that your system defines BYTE_ORDER.  If your
  * architecture is little-endian, make sure it also defines
  * LITTLE_ENDIAN and that the two (BYTE_ORDER and LITTLE_ENDIAN) are
- * equivilent.
+ * equivalent.
  *
  * If your system does not define the above, then you can do so by
  * hand like this:
@@ -219,9 +221,9 @@ typedef u_int64_t sha2_word64;      /* Exactly 8 bytes */
  * library -- they are intended for private internal visibility/use
  * only.
  */
-void SHA512_Last(SHA512_CTX*);
-void SHA256_Transform(SHA256_CTX*, const sha2_word32*);
-void SHA512_Transform(SHA512_CTX*, const sha2_word64*);
+static void SHA512_Last(SHA512_CTX*);
+static void SHA256_Transform(SHA256_CTX*, const sha2_word32*);
+static void SHA512_Transform(SHA512_CTX*, const sha2_word64*);
 
 
 /*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/
@@ -379,7 +381,7 @@ void SHA256_Init(SHA256_CTX* context) {
        (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
        j++
 
-void SHA256_Transform(SHA256_CTX* context, const sha2_word32* data) {
+static void SHA256_Transform(SHA256_CTX* context, const sha2_word32* data) {
        sha2_word32     a, b, c, d, e, f, g, h, s0, s1;
        sha2_word32     T1, *W256;
        int             j;
@@ -437,7 +439,7 @@ void SHA256_Transform(SHA256_CTX* context, const sha2_word32* data) {
 
 #else /* SHA2_UNROLL_TRANSFORM */
 
-void SHA256_Transform(SHA256_CTX* context, const sha2_word32* data) {
+static void SHA256_Transform(SHA256_CTX* context, const sha2_word32* data) {
        sha2_word32     a, b, c, d, e, f, g, h, s0, s1;
        sha2_word32     T1, T2, *W256;
        int             j;
@@ -551,7 +553,9 @@ void SHA256_Update(SHA256_CTX* context, const sha2_byte *data, size_t len) {
        }
        while (len >= SHA256_BLOCK_LENGTH) {
                /* Process as many complete blocks as we can */
-               SHA256_Transform(context, (sha2_word32*)data);
+               sha2_byte buffer[SHA256_BLOCK_LENGTH];
+               MEMCPY_BCOPY(buffer, data, SHA256_BLOCK_LENGTH);
+               SHA256_Transform(context, (sha2_word32*)buffer);
                context->bitcount += SHA256_BLOCK_LENGTH << 3;
                len -= SHA256_BLOCK_LENGTH;
                data += SHA256_BLOCK_LENGTH;
@@ -604,7 +608,12 @@ void SHA256_Final(sha2_byte digest[], SHA256_CTX* context) {
                        *context->buffer = 0x80;
                }
                /* Set the bit count: */
-               *(sha2_word64*)&context->buffer[SHA256_SHORT_BLOCK_LENGTH] = context->bitcount;
+               union {
+                       sha2_byte* c;
+                       sha2_word64* l;
+               } bitcount;
+               bitcount.c = &context->buffer[SHA256_SHORT_BLOCK_LENGTH];
+               *(bitcount.l) = context->bitcount;
 
                /* Final transform: */
                SHA256_Transform(context, (sha2_word32*)context->buffer);
@@ -624,7 +633,7 @@ void SHA256_Final(sha2_byte digest[], SHA256_CTX* context) {
        }
 
        /* Clean up state data: */
-       MEMSET_BZERO(context, sizeof(context));
+       MEMSET_BZERO(context, sizeof(*context));
        usedspace = 0;
 }
 
@@ -645,7 +654,7 @@ char *SHA256_End(SHA256_CTX* context, char buffer[]) {
                }
                *buffer = (char)0;
        } else {
-               MEMSET_BZERO(context, sizeof(context));
+               MEMSET_BZERO(context, sizeof(*context));
        }
        MEMSET_BZERO(digest, SHA256_DIGEST_LENGTH);
        return buffer;
@@ -706,7 +715,7 @@ void SHA512_Init(SHA512_CTX* context) {
        (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \
        j++
 
-void SHA512_Transform(SHA512_CTX* context, const sha2_word64* data) {
+static void SHA512_Transform(SHA512_CTX* context, const sha2_word64* data) {
        sha2_word64     a, b, c, d, e, f, g, h, s0, s1;
        sha2_word64     T1, *W512 = (sha2_word64*)context->buffer;
        int             j;
@@ -761,7 +770,7 @@ void SHA512_Transform(SHA512_CTX* context, const sha2_word64* data) {
 
 #else /* SHA2_UNROLL_TRANSFORM */
 
-void SHA512_Transform(SHA512_CTX* context, const sha2_word64* data) {
+static void SHA512_Transform(SHA512_CTX* context, const sha2_word64* data) {
        sha2_word64     a, b, c, d, e, f, g, h, s0, s1;
        sha2_word64     T1, T2, *W512 = (sha2_word64*)context->buffer;
        int             j;
@@ -873,7 +882,9 @@ void SHA512_Update(SHA512_CTX* context, const sha2_byte *data, size_t len) {
        }
        while (len >= SHA512_BLOCK_LENGTH) {
                /* Process as many complete blocks as we can */
-               SHA512_Transform(context, (sha2_word64*)data);
+               sha2_byte buffer[SHA512_BLOCK_LENGTH];
+               MEMCPY_BCOPY(buffer, data, SHA512_BLOCK_LENGTH);
+               SHA512_Transform(context, (sha2_word64*)buffer);
                ADDINC128(context->bitcount, SHA512_BLOCK_LENGTH << 3);
                len -= SHA512_BLOCK_LENGTH;
                data += SHA512_BLOCK_LENGTH;
@@ -887,7 +898,7 @@ void SHA512_Update(SHA512_CTX* context, const sha2_byte *data, size_t len) {
        usedspace = freespace = 0;
 }
 
-void SHA512_Last(SHA512_CTX* context) {
+static void SHA512_Last(SHA512_CTX* context) {
        unsigned int    usedspace;
 
        usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
@@ -921,8 +932,13 @@ void SHA512_Last(SHA512_CTX* context) {
                *context->buffer = 0x80;
        }
        /* Store the length of input data (in bits): */
-       *(sha2_word64*)&context->buffer[SHA512_SHORT_BLOCK_LENGTH] = context->bitcount[1];
-       *(sha2_word64*)&context->buffer[SHA512_SHORT_BLOCK_LENGTH+8] = context->bitcount[0];
+       union {
+               sha2_byte* c;
+               sha2_word64* l;
+       } bitcount;
+       bitcount.c = &context->buffer[SHA512_SHORT_BLOCK_LENGTH];
+       bitcount.l[0] = context->bitcount[1];
+       bitcount.l[1] = context->bitcount[0];
 
        /* Final transform: */
        SHA512_Transform(context, (sha2_word64*)context->buffer);
@@ -954,7 +970,7 @@ void SHA512_Final(sha2_byte digest[], SHA512_CTX* context) {
        }
 
        /* Zero out state data */
-       MEMSET_BZERO(context, sizeof(context));
+       MEMSET_BZERO(context, sizeof(*context));
 }
 
 char *SHA512_End(SHA512_CTX* context, char buffer[]) {
@@ -974,7 +990,7 @@ char *SHA512_End(SHA512_CTX* context, char buffer[]) {
                }
                *buffer = (char)0;
        } else {
-               MEMSET_BZERO(context, sizeof(context));
+               MEMSET_BZERO(context, sizeof(*context));
        }
        MEMSET_BZERO(digest, SHA512_DIGEST_LENGTH);
        return buffer;
@@ -1029,7 +1045,7 @@ void SHA384_Final(sha2_byte digest[], SHA384_CTX* context) {
        }
 
        /* Zero out state data */
-       MEMSET_BZERO(context, sizeof(context));
+       MEMSET_BZERO(context, sizeof(*context));
 }
 
 char *SHA384_End(SHA384_CTX* context, char buffer[]) {
@@ -1049,7 +1065,7 @@ char *SHA384_End(SHA384_CTX* context, char buffer[]) {
                }
                *buffer = (char)0;
        } else {
-               MEMSET_BZERO(context, sizeof(context));
+               MEMSET_BZERO(context, sizeof(*context));
        }
        MEMSET_BZERO(digest, SHA384_DIGEST_LENGTH);
        return buffer;