mirror of
https://github.com/Xevion/easy7zip.git
synced 2025-12-09 14:07:08 -06:00
Add some hash functions
- new: md2, md4, md5, sha384, sha512, xxhash-32, xxhash-64 - put Blake2sp hash stuff back to rar code - added the hashes to GUI and Explorer Menu code
This commit is contained in:
57
C/hashes/hash.h
Normal file
57
C/hashes/hash.h
Normal file
@@ -0,0 +1,57 @@
|
||||
|
||||
/*
|
||||
* Copyright (c) 1999 Kungliga Tekniska Högskolan
|
||||
* (Royal Institute of Technology, Stockholm, Sweden).
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of KTH nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software without
|
||||
* specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
|
||||
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
|
||||
|
||||
/* $Id$ */
|
||||
|
||||
/* stuff in common between md4, md5, and sha1 */
|
||||
|
||||
#ifndef __hash_h__
|
||||
#define __hash_h__
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "../7zTypes.h"
|
||||
|
||||
#ifndef uint32_t
|
||||
typedef UInt32 uint32_t;
|
||||
#endif
|
||||
|
||||
#ifndef uint64_t
|
||||
typedef UInt64 uint64_t;
|
||||
#endif
|
||||
|
||||
#ifndef min
|
||||
#define min(a,b) (((a)>(b))?(b):(a))
|
||||
#endif
|
||||
|
||||
#endif /* __hash_h__ */
|
||||
133
C/hashes/md2.c
Normal file
133
C/hashes/md2.c
Normal file
@@ -0,0 +1,133 @@
|
||||
|
||||
/*
|
||||
* Copyright (c) 2006 Kungliga Tekniska Högskolan
|
||||
* (Royal Institute of Technology, Stockholm, Sweden).
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "hash.h"
|
||||
#include "md2.h"
|
||||
|
||||
static const unsigned char subst[256] = {
|
||||
41, 46, 67, 201, 162, 216, 124, 1, 61, 54, 84, 161, 236, 240, 6,
|
||||
19, 98, 167, 5, 243, 192, 199, 115, 140, 152, 147, 43, 217, 188,
|
||||
76, 130, 202, 30, 155, 87, 60, 253, 212, 224, 22, 103, 66, 111, 24,
|
||||
138, 23, 229, 18, 190, 78, 196, 214, 218, 158, 222, 73, 160, 251,
|
||||
245, 142, 187, 47, 238, 122, 169, 104, 121, 145, 21, 178, 7, 63,
|
||||
148, 194, 16, 137, 11, 34, 95, 33, 128, 127, 93, 154, 90, 144, 50,
|
||||
39, 53, 62, 204, 231, 191, 247, 151, 3, 255, 25, 48, 179, 72, 165,
|
||||
181, 209, 215, 94, 146, 42, 172, 86, 170, 198, 79, 184, 56, 210,
|
||||
150, 164, 125, 182, 118, 252, 107, 226, 156, 116, 4, 241, 69, 157,
|
||||
112, 89, 100, 113, 135, 32, 134, 91, 207, 101, 230, 45, 168, 2, 27,
|
||||
96, 37, 173, 174, 176, 185, 246, 28, 70, 97, 105, 52, 64, 126, 15,
|
||||
85, 71, 163, 35, 221, 81, 175, 58, 195, 92, 249, 206, 186, 197,
|
||||
234, 38, 44, 83, 13, 110, 133, 40, 132, 9, 211, 223, 205, 244, 65,
|
||||
129, 77, 82, 106, 220, 55, 200, 108, 193, 171, 250, 36, 225, 123,
|
||||
8, 12, 189, 177, 74, 120, 136, 149, 139, 227, 99, 232, 109, 233,
|
||||
203, 213, 254, 59, 0, 29, 57, 242, 239, 183, 14, 102, 88, 208, 228,
|
||||
166, 119, 114, 248, 235, 117, 75, 10, 49, 68, 80, 180, 143, 237,
|
||||
31, 26, 219, 153, 141, 51, 159, 17, 131, 20
|
||||
};
|
||||
|
||||
void
|
||||
MD2_Init (struct md2 *m)
|
||||
{
|
||||
memset(m, 0, sizeof(*m));
|
||||
}
|
||||
|
||||
static void
|
||||
calc(struct md2 *m, const void *v)
|
||||
{
|
||||
unsigned char x[48], L;
|
||||
const unsigned char *p = v;
|
||||
int i, j, t;
|
||||
|
||||
L = m->checksum[15];
|
||||
for (i = 0; i < 16; i++)
|
||||
L = m->checksum[i] ^= subst[p[i] ^ L];
|
||||
|
||||
for (i = 0; i < 16; i++) {
|
||||
x[i] = m->state[i];
|
||||
x[i + 16] = p[i];
|
||||
x[i + 32] = x[i] ^ p[i];
|
||||
}
|
||||
|
||||
t = 0;
|
||||
for (i = 0; i < 18; i++) {
|
||||
for (j = 0; j < 48; j++)
|
||||
t = x[j] ^= subst[t];
|
||||
t = (t + i) & 0xff;
|
||||
}
|
||||
|
||||
memcpy(m->state, x, 16);
|
||||
memset(x, 0, sizeof(x));
|
||||
}
|
||||
|
||||
void
|
||||
MD2_Update (struct md2 *m, const void *v, size_t len)
|
||||
{
|
||||
size_t idx = m->len & 0xf;
|
||||
const unsigned char *p = v;
|
||||
|
||||
m->len += len;
|
||||
if (len + idx >= 16) {
|
||||
if (idx) {
|
||||
memcpy(m->data + idx, p, 16 - idx);
|
||||
calc(m, m->data);
|
||||
p += 16;
|
||||
len -= 16 - idx;
|
||||
}
|
||||
while (len >= 16) {
|
||||
calc(m, p);
|
||||
p += 16;
|
||||
len -= 16;
|
||||
}
|
||||
idx = 0;
|
||||
}
|
||||
|
||||
memcpy(m->data + idx, p, len);
|
||||
}
|
||||
|
||||
void
|
||||
MD2_Final (void *res, struct md2 *m)
|
||||
{
|
||||
unsigned char pad[16];
|
||||
size_t padlen;
|
||||
|
||||
padlen = 16 - (m->len % 16);
|
||||
memset(pad, (int)padlen, padlen);
|
||||
|
||||
MD2_Update(m, pad, padlen);
|
||||
memcpy(pad, m->checksum, 16);
|
||||
MD2_Update(m, pad, 16);
|
||||
|
||||
memcpy(res, m->state, MD2_DIGEST_LENGTH);
|
||||
memset(m, 0, sizeof(*m));
|
||||
}
|
||||
57
C/hashes/md2.h
Normal file
57
C/hashes/md2.h
Normal file
@@ -0,0 +1,57 @@
|
||||
|
||||
/*
|
||||
* Copyright (c) 2006 Kungliga Tekniska Högskolan
|
||||
* (Royal Institute of Technology, Stockholm, Sweden).
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/* $Id$ */
|
||||
|
||||
#ifndef HEIM_MD2_H
|
||||
#define HEIM_MD2_H 1
|
||||
|
||||
#include "hash.h"
|
||||
|
||||
#define MD2_DIGEST_LENGTH 16
|
||||
|
||||
struct md2 {
|
||||
size_t len;
|
||||
unsigned char data[16]; /* stored unalligned data between Update's */
|
||||
unsigned char checksum[16];
|
||||
unsigned char state[16]; /* lower 16 bytes of X */
|
||||
};
|
||||
|
||||
typedef struct md2 MD2_CTX;
|
||||
|
||||
void MD2_Init (struct md2 *m);
|
||||
void MD2_Update (struct md2 *m, const void *p, size_t len);
|
||||
void MD2_Final (void *res, struct md2 *m);
|
||||
|
||||
#endif /* HEIM_MD2_H */
|
||||
264
C/hashes/md4.c
Normal file
264
C/hashes/md4.c
Normal file
@@ -0,0 +1,264 @@
|
||||
|
||||
/*
|
||||
* Copyright (c) 1995 - 2001 Kungliga Tekniska Högskolan
|
||||
* (Royal Institute of Technology, Stockholm, Sweden).
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "hash.h"
|
||||
#include "md4.h"
|
||||
|
||||
#define A m->counter[0]
|
||||
#define B m->counter[1]
|
||||
#define C m->counter[2]
|
||||
#define D m->counter[3]
|
||||
#define X data
|
||||
|
||||
/* Vector Crays doesn't have a good 32-bit type, or more precisely,
|
||||
int32_t as defined by <bind/bitypes.h> isn't 32 bits, and we don't
|
||||
want to depend in being able to redefine this type. To cope with
|
||||
this we have to clamp the result in some places to [0,2^32); no
|
||||
need to do this on other machines. Did I say this was a mess?
|
||||
*/
|
||||
|
||||
#ifdef _CRAY
|
||||
#define CRAYFIX(X) ((X) & 0xffffffff)
|
||||
#else
|
||||
#define CRAYFIX(X) (X)
|
||||
#endif
|
||||
|
||||
static uint32_t cshift (uint32_t x, unsigned int n)
|
||||
{
|
||||
x = CRAYFIX(x);
|
||||
return CRAYFIX((x << n) | (x >> (32 - n)));
|
||||
}
|
||||
|
||||
void
|
||||
MD4_Init (struct md4 *m)
|
||||
{
|
||||
m->sz[0] = 0;
|
||||
m->sz[1] = 0;
|
||||
D = 0x10325476;
|
||||
C = 0x98badcfe;
|
||||
B = 0xefcdab89;
|
||||
A = 0x67452301;
|
||||
}
|
||||
|
||||
#define F(x,y,z) CRAYFIX((x & y) | (~x & z))
|
||||
#define G(x,y,z) ((x & y) | (x & z) | (y & z))
|
||||
#define H(x,y,z) (x ^ y ^ z)
|
||||
|
||||
#define DOIT(a,b,c,d,k,s,i,OP) \
|
||||
a = cshift(a + OP(b,c,d) + X[k] + i, s)
|
||||
|
||||
#define DO1(a,b,c,d,k,s,i) DOIT(a,b,c,d,k,s,i,F)
|
||||
#define DO2(a,b,c,d,k,s,i) DOIT(a,b,c,d,k,s,i,G)
|
||||
#define DO3(a,b,c,d,k,s,i) DOIT(a,b,c,d,k,s,i,H)
|
||||
|
||||
static void
|
||||
calc (struct md4 *m, uint32_t *data)
|
||||
{
|
||||
uint32_t AA, BB, CC, DD;
|
||||
|
||||
AA = A;
|
||||
BB = B;
|
||||
CC = C;
|
||||
DD = D;
|
||||
|
||||
/* Round 1 */
|
||||
|
||||
DO1(A,B,C,D,0,3,0);
|
||||
DO1(D,A,B,C,1,7,0);
|
||||
DO1(C,D,A,B,2,11,0);
|
||||
DO1(B,C,D,A,3,19,0);
|
||||
|
||||
DO1(A,B,C,D,4,3,0);
|
||||
DO1(D,A,B,C,5,7,0);
|
||||
DO1(C,D,A,B,6,11,0);
|
||||
DO1(B,C,D,A,7,19,0);
|
||||
|
||||
DO1(A,B,C,D,8,3,0);
|
||||
DO1(D,A,B,C,9,7,0);
|
||||
DO1(C,D,A,B,10,11,0);
|
||||
DO1(B,C,D,A,11,19,0);
|
||||
|
||||
DO1(A,B,C,D,12,3,0);
|
||||
DO1(D,A,B,C,13,7,0);
|
||||
DO1(C,D,A,B,14,11,0);
|
||||
DO1(B,C,D,A,15,19,0);
|
||||
|
||||
/* Round 2 */
|
||||
|
||||
DO2(A,B,C,D,0,3,0x5A827999);
|
||||
DO2(D,A,B,C,4,5,0x5A827999);
|
||||
DO2(C,D,A,B,8,9,0x5A827999);
|
||||
DO2(B,C,D,A,12,13,0x5A827999);
|
||||
|
||||
DO2(A,B,C,D,1,3,0x5A827999);
|
||||
DO2(D,A,B,C,5,5,0x5A827999);
|
||||
DO2(C,D,A,B,9,9,0x5A827999);
|
||||
DO2(B,C,D,A,13,13,0x5A827999);
|
||||
|
||||
DO2(A,B,C,D,2,3,0x5A827999);
|
||||
DO2(D,A,B,C,6,5,0x5A827999);
|
||||
DO2(C,D,A,B,10,9,0x5A827999);
|
||||
DO2(B,C,D,A,14,13,0x5A827999);
|
||||
|
||||
DO2(A,B,C,D,3,3,0x5A827999);
|
||||
DO2(D,A,B,C,7,5,0x5A827999);
|
||||
DO2(C,D,A,B,11,9,0x5A827999);
|
||||
DO2(B,C,D,A,15,13,0x5A827999);
|
||||
|
||||
/* Round 3 */
|
||||
|
||||
DO3(A,B,C,D,0,3,0x6ED9EBA1);
|
||||
DO3(D,A,B,C,8,9,0x6ED9EBA1);
|
||||
DO3(C,D,A,B,4,11,0x6ED9EBA1);
|
||||
DO3(B,C,D,A,12,15,0x6ED9EBA1);
|
||||
|
||||
DO3(A,B,C,D,2,3,0x6ED9EBA1);
|
||||
DO3(D,A,B,C,10,9,0x6ED9EBA1);
|
||||
DO3(C,D,A,B,6,11,0x6ED9EBA1);
|
||||
DO3(B,C,D,A,14,15,0x6ED9EBA1);
|
||||
|
||||
DO3(A,B,C,D,1,3,0x6ED9EBA1);
|
||||
DO3(D,A,B,C,9,9,0x6ED9EBA1);
|
||||
DO3(C,D,A,B,5,11,0x6ED9EBA1);
|
||||
DO3(B,C,D,A,13,15,0x6ED9EBA1);
|
||||
|
||||
DO3(A,B,C,D,3,3,0x6ED9EBA1);
|
||||
DO3(D,A,B,C,11,9,0x6ED9EBA1);
|
||||
DO3(C,D,A,B,7,11,0x6ED9EBA1);
|
||||
DO3(B,C,D,A,15,15,0x6ED9EBA1);
|
||||
|
||||
A += AA;
|
||||
B += BB;
|
||||
C += CC;
|
||||
D += DD;
|
||||
}
|
||||
|
||||
/*
|
||||
* From `Performance analysis of MD5' by Joseph D. Touch <touch@isi.edu>
|
||||
*/
|
||||
|
||||
#if defined(WORDS_BIGENDIAN)
|
||||
static uint32_t
|
||||
swap_uint32_t (uint32_t t)
|
||||
{
|
||||
uint32_t temp1, temp2;
|
||||
|
||||
temp1 = cshift(t, 16);
|
||||
temp2 = temp1 >> 8;
|
||||
temp1 &= 0x00ff00ff;
|
||||
temp2 &= 0x00ff00ff;
|
||||
temp1 <<= 8;
|
||||
return temp1 | temp2;
|
||||
}
|
||||
#endif
|
||||
|
||||
struct x32{
|
||||
unsigned int a:32;
|
||||
unsigned int b:32;
|
||||
};
|
||||
|
||||
void
|
||||
MD4_Update (struct md4 *m, const void *v, size_t len)
|
||||
{
|
||||
const unsigned char *p = v;
|
||||
size_t old_sz = m->sz[0];
|
||||
size_t offset;
|
||||
|
||||
m->sz[0] += (unsigned int)len * 8;
|
||||
if (m->sz[0] < old_sz)
|
||||
++m->sz[1];
|
||||
offset = (old_sz / 8) % 64;
|
||||
while(len > 0) {
|
||||
size_t l = min(len, 64 - offset);
|
||||
memcpy(m->save + offset, p, l);
|
||||
offset += l;
|
||||
p += l;
|
||||
len -= l;
|
||||
if(offset == 64) {
|
||||
#if defined(WORDS_BIGENDIAN)
|
||||
int i;
|
||||
uint32_t current[16];
|
||||
struct x32 *us = (struct x32*)m->save;
|
||||
for(i = 0; i < 8; i++){
|
||||
current[2*i+0] = swap_uint32_t(us[i].a);
|
||||
current[2*i+1] = swap_uint32_t(us[i].b);
|
||||
}
|
||||
calc(m, current);
|
||||
#else
|
||||
calc(m, (uint32_t*)m->save);
|
||||
#endif
|
||||
offset = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
MD4_Final (void *res, struct md4 *m)
|
||||
{
|
||||
unsigned char zeros[72];
|
||||
unsigned offset = (m->sz[0] / 8) % 64;
|
||||
unsigned int dstart = (120 - offset - 1) % 64 + 1;
|
||||
|
||||
*zeros = 0x80;
|
||||
memset (zeros + 1, 0, sizeof(zeros) - 1);
|
||||
zeros[dstart+0] = (m->sz[0] >> 0) & 0xff;
|
||||
zeros[dstart+1] = (m->sz[0] >> 8) & 0xff;
|
||||
zeros[dstart+2] = (m->sz[0] >> 16) & 0xff;
|
||||
zeros[dstart+3] = (m->sz[0] >> 24) & 0xff;
|
||||
zeros[dstart+4] = (m->sz[1] >> 0) & 0xff;
|
||||
zeros[dstart+5] = (m->sz[1] >> 8) & 0xff;
|
||||
zeros[dstart+6] = (m->sz[1] >> 16) & 0xff;
|
||||
zeros[dstart+7] = (m->sz[1] >> 24) & 0xff;
|
||||
MD4_Update (m, zeros, dstart + 8);
|
||||
{
|
||||
int i;
|
||||
unsigned char *r = (unsigned char *)res;
|
||||
|
||||
for (i = 0; i < 4; ++i) {
|
||||
r[4*i] = m->counter[i] & 0xFF;
|
||||
r[4*i+1] = (m->counter[i] >> 8) & 0xFF;
|
||||
r[4*i+2] = (m->counter[i] >> 16) & 0xFF;
|
||||
r[4*i+3] = (m->counter[i] >> 24) & 0xFF;
|
||||
}
|
||||
}
|
||||
#if 0
|
||||
{
|
||||
int i;
|
||||
uint32_t *r = (uint32_t *)res;
|
||||
|
||||
for (i = 0; i < 4; ++i)
|
||||
r[i] = swap_uint32_t (m->counter[i]);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
56
C/hashes/md4.h
Normal file
56
C/hashes/md4.h
Normal file
@@ -0,0 +1,56 @@
|
||||
|
||||
/*
|
||||
* Copyright (c) 1995 - 2001 Kungliga Tekniska Högskolan
|
||||
* (Royal Institute of Technology, Stockholm, Sweden).
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/* $Id$ */
|
||||
|
||||
#ifndef HEIM_MD4_H
|
||||
#define HEIM_MD4_H 1
|
||||
|
||||
#include "hash.h"
|
||||
|
||||
#define MD4_DIGEST_LENGTH 16
|
||||
|
||||
struct md4 {
|
||||
unsigned int sz[2];
|
||||
uint32_t counter[4];
|
||||
unsigned char save[64];
|
||||
};
|
||||
|
||||
typedef struct md4 MD4_CTX;
|
||||
|
||||
void MD4_Init (struct md4 *m);
|
||||
void MD4_Update (struct md4 *m, const void *p, size_t len);
|
||||
void MD4_Final (void *res, struct md4 *m);
|
||||
|
||||
#endif /* HEIM_MD4_H */
|
||||
288
C/hashes/md5.c
Normal file
288
C/hashes/md5.c
Normal file
@@ -0,0 +1,288 @@
|
||||
|
||||
/*
|
||||
* Copyright (c) 1995 - 2001 Kungliga Tekniska Högskolan
|
||||
* (Royal Institute of Technology, Stockholm, Sweden).
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "hash.h"
|
||||
#include "md5.h"
|
||||
|
||||
#define A m->counter[0]
|
||||
#define B m->counter[1]
|
||||
#define C m->counter[2]
|
||||
#define D m->counter[3]
|
||||
#define X data
|
||||
|
||||
/* Vector Crays doesn't have a good 32-bit type, or more precisely,
|
||||
int32_t as defined by <bind/bitypes.h> isn't 32 bits, and we don't
|
||||
want to depend in being able to redefine this type. To cope with
|
||||
this we have to clamp the result in some places to [0,2^32); no
|
||||
need to do this on other machines. Did I say this was a mess?
|
||||
*/
|
||||
|
||||
#ifdef _CRAY
|
||||
#define CRAYFIX(X) ((X) & 0xffffffff)
|
||||
#else
|
||||
#define CRAYFIX(X) (X)
|
||||
#endif
|
||||
|
||||
static uint32_t cshift (uint32_t x, unsigned int n)
|
||||
{
|
||||
x = CRAYFIX(x);
|
||||
return CRAYFIX((x << n) | (x >> (32 - n)));
|
||||
}
|
||||
|
||||
void
|
||||
MD5_Init (struct md5 *m)
|
||||
{
|
||||
m->sz[0] = 0;
|
||||
m->sz[1] = 0;
|
||||
D = 0x10325476;
|
||||
C = 0x98badcfe;
|
||||
B = 0xefcdab89;
|
||||
A = 0x67452301;
|
||||
}
|
||||
|
||||
#define F(x,y,z) CRAYFIX((x & y) | (~x & z))
|
||||
#define G(x,y,z) CRAYFIX((x & z) | (y & ~z))
|
||||
#define H(x,y,z) (x ^ y ^ z)
|
||||
#define I(x,y,z) CRAYFIX(y ^ (x | ~z))
|
||||
|
||||
#define DOIT(a,b,c,d,k,s,i,OP) \
|
||||
a = b + cshift(a + OP(b,c,d) + X[k] + (i), s)
|
||||
|
||||
#define DO1(a,b,c,d,k,s,i) DOIT(a,b,c,d,k,s,i,F)
|
||||
#define DO2(a,b,c,d,k,s,i) DOIT(a,b,c,d,k,s,i,G)
|
||||
#define DO3(a,b,c,d,k,s,i) DOIT(a,b,c,d,k,s,i,H)
|
||||
#define DO4(a,b,c,d,k,s,i) DOIT(a,b,c,d,k,s,i,I)
|
||||
|
||||
static void
|
||||
calc (struct md5 *m, uint32_t *data)
|
||||
{
|
||||
uint32_t AA, BB, CC, DD;
|
||||
|
||||
AA = A;
|
||||
BB = B;
|
||||
CC = C;
|
||||
DD = D;
|
||||
|
||||
/* Round 1 */
|
||||
|
||||
DO1(A,B,C,D,0,7,0xd76aa478);
|
||||
DO1(D,A,B,C,1,12,0xe8c7b756);
|
||||
DO1(C,D,A,B,2,17,0x242070db);
|
||||
DO1(B,C,D,A,3,22,0xc1bdceee);
|
||||
|
||||
DO1(A,B,C,D,4,7,0xf57c0faf);
|
||||
DO1(D,A,B,C,5,12,0x4787c62a);
|
||||
DO1(C,D,A,B,6,17,0xa8304613);
|
||||
DO1(B,C,D,A,7,22,0xfd469501);
|
||||
|
||||
DO1(A,B,C,D,8,7,0x698098d8);
|
||||
DO1(D,A,B,C,9,12,0x8b44f7af);
|
||||
DO1(C,D,A,B,10,17,0xffff5bb1);
|
||||
DO1(B,C,D,A,11,22,0x895cd7be);
|
||||
|
||||
DO1(A,B,C,D,12,7,0x6b901122);
|
||||
DO1(D,A,B,C,13,12,0xfd987193);
|
||||
DO1(C,D,A,B,14,17,0xa679438e);
|
||||
DO1(B,C,D,A,15,22,0x49b40821);
|
||||
|
||||
/* Round 2 */
|
||||
|
||||
DO2(A,B,C,D,1,5,0xf61e2562);
|
||||
DO2(D,A,B,C,6,9,0xc040b340);
|
||||
DO2(C,D,A,B,11,14,0x265e5a51);
|
||||
DO2(B,C,D,A,0,20,0xe9b6c7aa);
|
||||
|
||||
DO2(A,B,C,D,5,5,0xd62f105d);
|
||||
DO2(D,A,B,C,10,9,0x2441453);
|
||||
DO2(C,D,A,B,15,14,0xd8a1e681);
|
||||
DO2(B,C,D,A,4,20,0xe7d3fbc8);
|
||||
|
||||
DO2(A,B,C,D,9,5,0x21e1cde6);
|
||||
DO2(D,A,B,C,14,9,0xc33707d6);
|
||||
DO2(C,D,A,B,3,14,0xf4d50d87);
|
||||
DO2(B,C,D,A,8,20,0x455a14ed);
|
||||
|
||||
DO2(A,B,C,D,13,5,0xa9e3e905);
|
||||
DO2(D,A,B,C,2,9,0xfcefa3f8);
|
||||
DO2(C,D,A,B,7,14,0x676f02d9);
|
||||
DO2(B,C,D,A,12,20,0x8d2a4c8a);
|
||||
|
||||
/* Round 3 */
|
||||
|
||||
DO3(A,B,C,D,5,4,0xfffa3942);
|
||||
DO3(D,A,B,C,8,11,0x8771f681);
|
||||
DO3(C,D,A,B,11,16,0x6d9d6122);
|
||||
DO3(B,C,D,A,14,23,0xfde5380c);
|
||||
|
||||
DO3(A,B,C,D,1,4,0xa4beea44);
|
||||
DO3(D,A,B,C,4,11,0x4bdecfa9);
|
||||
DO3(C,D,A,B,7,16,0xf6bb4b60);
|
||||
DO3(B,C,D,A,10,23,0xbebfbc70);
|
||||
|
||||
DO3(A,B,C,D,13,4,0x289b7ec6);
|
||||
DO3(D,A,B,C,0,11,0xeaa127fa);
|
||||
DO3(C,D,A,B,3,16,0xd4ef3085);
|
||||
DO3(B,C,D,A,6,23,0x4881d05);
|
||||
|
||||
DO3(A,B,C,D,9,4,0xd9d4d039);
|
||||
DO3(D,A,B,C,12,11,0xe6db99e5);
|
||||
DO3(C,D,A,B,15,16,0x1fa27cf8);
|
||||
DO3(B,C,D,A,2,23,0xc4ac5665);
|
||||
|
||||
/* Round 4 */
|
||||
|
||||
DO4(A,B,C,D,0,6,0xf4292244);
|
||||
DO4(D,A,B,C,7,10,0x432aff97);
|
||||
DO4(C,D,A,B,14,15,0xab9423a7);
|
||||
DO4(B,C,D,A,5,21,0xfc93a039);
|
||||
|
||||
DO4(A,B,C,D,12,6,0x655b59c3);
|
||||
DO4(D,A,B,C,3,10,0x8f0ccc92);
|
||||
DO4(C,D,A,B,10,15,0xffeff47d);
|
||||
DO4(B,C,D,A,1,21,0x85845dd1);
|
||||
|
||||
DO4(A,B,C,D,8,6,0x6fa87e4f);
|
||||
DO4(D,A,B,C,15,10,0xfe2ce6e0);
|
||||
DO4(C,D,A,B,6,15,0xa3014314);
|
||||
DO4(B,C,D,A,13,21,0x4e0811a1);
|
||||
|
||||
DO4(A,B,C,D,4,6,0xf7537e82);
|
||||
DO4(D,A,B,C,11,10,0xbd3af235);
|
||||
DO4(C,D,A,B,2,15,0x2ad7d2bb);
|
||||
DO4(B,C,D,A,9,21,0xeb86d391);
|
||||
|
||||
A += AA;
|
||||
B += BB;
|
||||
C += CC;
|
||||
D += DD;
|
||||
}
|
||||
|
||||
/*
|
||||
* From `Performance analysis of MD5' by Joseph D. Touch <touch@isi.edu>
|
||||
*/
|
||||
|
||||
#if defined(WORDS_BIGENDIAN)
|
||||
static uint32_t
|
||||
swap_uint32_t (uint32_t t)
|
||||
{
|
||||
uint32_t temp1, temp2;
|
||||
|
||||
temp1 = cshift(t, 16);
|
||||
temp2 = temp1 >> 8;
|
||||
temp1 &= 0x00ff00ff;
|
||||
temp2 &= 0x00ff00ff;
|
||||
temp1 <<= 8;
|
||||
return temp1 | temp2;
|
||||
}
|
||||
#endif
|
||||
|
||||
struct x32{
|
||||
unsigned int a:32;
|
||||
unsigned int b:32;
|
||||
};
|
||||
|
||||
void
|
||||
MD5_Update (struct md5 *m, const void *v, size_t len)
|
||||
{
|
||||
const unsigned char *p = v;
|
||||
size_t old_sz = m->sz[0];
|
||||
size_t offset;
|
||||
|
||||
m->sz[0] += (unsigned int)len * 8;
|
||||
if (m->sz[0] < old_sz)
|
||||
++m->sz[1];
|
||||
offset = (old_sz / 8) % 64;
|
||||
while(len > 0){
|
||||
size_t l = min(len, 64 - offset);
|
||||
memcpy(m->save + offset, p, l);
|
||||
offset += l;
|
||||
p += l;
|
||||
len -= l;
|
||||
if(offset == 64){
|
||||
#if defined(WORDS_BIGENDIAN)
|
||||
int i;
|
||||
uint32_t current[16];
|
||||
struct x32 *us = (struct x32*)m->save;
|
||||
for(i = 0; i < 8; i++){
|
||||
current[2*i+0] = swap_uint32_t(us[i].a);
|
||||
current[2*i+1] = swap_uint32_t(us[i].b);
|
||||
}
|
||||
calc(m, current);
|
||||
#else
|
||||
calc(m, (uint32_t*)m->save);
|
||||
#endif
|
||||
offset = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
MD5_Final (void *res, struct md5 *m)
|
||||
{
|
||||
unsigned char zeros[72];
|
||||
unsigned offset = (m->sz[0] / 8) % 64;
|
||||
unsigned int dstart = (120 - offset - 1) % 64 + 1;
|
||||
|
||||
*zeros = 0x80;
|
||||
memset (zeros + 1, 0, sizeof(zeros) - 1);
|
||||
zeros[dstart+0] = (m->sz[0] >> 0) & 0xff;
|
||||
zeros[dstart+1] = (m->sz[0] >> 8) & 0xff;
|
||||
zeros[dstart+2] = (m->sz[0] >> 16) & 0xff;
|
||||
zeros[dstart+3] = (m->sz[0] >> 24) & 0xff;
|
||||
zeros[dstart+4] = (m->sz[1] >> 0) & 0xff;
|
||||
zeros[dstart+5] = (m->sz[1] >> 8) & 0xff;
|
||||
zeros[dstart+6] = (m->sz[1] >> 16) & 0xff;
|
||||
zeros[dstart+7] = (m->sz[1] >> 24) & 0xff;
|
||||
MD5_Update (m, zeros, dstart + 8);
|
||||
{
|
||||
int i;
|
||||
unsigned char *r = (unsigned char *)res;
|
||||
|
||||
for (i = 0; i < 4; ++i) {
|
||||
r[4*i] = m->counter[i] & 0xFF;
|
||||
r[4*i+1] = (m->counter[i] >> 8) & 0xFF;
|
||||
r[4*i+2] = (m->counter[i] >> 16) & 0xFF;
|
||||
r[4*i+3] = (m->counter[i] >> 24) & 0xFF;
|
||||
}
|
||||
}
|
||||
#if 0
|
||||
{
|
||||
int i;
|
||||
uint32_t *r = (uint32_t *)res;
|
||||
|
||||
for (i = 0; i < 4; ++i)
|
||||
r[i] = swap_uint32_t (m->counter[i]);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
56
C/hashes/md5.h
Normal file
56
C/hashes/md5.h
Normal file
@@ -0,0 +1,56 @@
|
||||
|
||||
/*
|
||||
* Copyright (c) 1995 - 2001 Kungliga Tekniska Högskolan
|
||||
* (Royal Institute of Technology, Stockholm, Sweden).
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/* $Id$ */
|
||||
|
||||
#ifndef HEIM_MD5_H
|
||||
#define HEIM_MD5_H 1
|
||||
|
||||
#include "hash.h"
|
||||
|
||||
#define MD5_DIGEST_LENGTH 16
|
||||
|
||||
struct md5 {
|
||||
unsigned int sz[2];
|
||||
uint32_t counter[4];
|
||||
unsigned char save[64];
|
||||
};
|
||||
|
||||
typedef struct md5 MD5_CTX;
|
||||
|
||||
void MD5_Init (struct md5 *m);
|
||||
void MD5_Update (struct md5 *m, const void *p, size_t len);
|
||||
void MD5_Final (void *res, struct md5 *m); /* uint32_t res[4] */
|
||||
|
||||
#endif /* HEIM_MD5_H */
|
||||
68
C/hashes/sha.h
Normal file
68
C/hashes/sha.h
Normal file
@@ -0,0 +1,68 @@
|
||||
|
||||
/*
|
||||
* Copyright (c) 1995 - 2001 Kungliga Tekniska Högskolan
|
||||
* (Royal Institute of Technology, Stockholm, Sweden).
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/* $Id$ */
|
||||
|
||||
#ifndef HEIM_SHA_H
|
||||
#define HEIM_SHA_H 1
|
||||
|
||||
#include "hash.h"
|
||||
|
||||
/*
|
||||
* SHA-2 512
|
||||
*/
|
||||
|
||||
#define SHA512_DIGEST_LENGTH 64
|
||||
|
||||
struct hc_sha512state {
|
||||
uint64_t sz[2];
|
||||
uint64_t counter[8];
|
||||
unsigned char save[128];
|
||||
};
|
||||
|
||||
typedef struct hc_sha512state SHA512_CTX;
|
||||
|
||||
void SHA512_Init (SHA512_CTX *);
|
||||
void SHA512_Update (SHA512_CTX *, const void *, size_t);
|
||||
void SHA512_Final (void *, SHA512_CTX *);
|
||||
|
||||
#define SHA384_DIGEST_LENGTH 48
|
||||
|
||||
typedef struct hc_sha512state SHA384_CTX;
|
||||
|
||||
void SHA384_Init (SHA384_CTX *);
|
||||
void SHA384_Update (SHA384_CTX *, const void *, size_t);
|
||||
void SHA384_Final (void *, SHA384_CTX *);
|
||||
|
||||
#endif /* HEIM_SHA_H */
|
||||
297
C/hashes/sha512.c
Normal file
297
C/hashes/sha512.c
Normal file
@@ -0,0 +1,297 @@
|
||||
|
||||
/*
|
||||
* Copyright (c) 2006, 2010 Kungliga Tekniska Högskolan
|
||||
* (Royal Institute of Technology, Stockholm, Sweden).
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "hash.h"
|
||||
#include "sha.h"
|
||||
|
||||
#define Ch(x,y,z) (((x) & (y)) ^ ((~(x)) & (z)))
|
||||
#define Maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
|
||||
|
||||
#define ROTR(x,n) (((x)>>(n)) | ((x) << (64 - (n))))
|
||||
|
||||
#define Sigma0(x) (ROTR(x,28) ^ ROTR(x,34) ^ ROTR(x,39))
|
||||
#define Sigma1(x) (ROTR(x,14) ^ ROTR(x,18) ^ ROTR(x,41))
|
||||
#define sigma0(x) (ROTR(x,1) ^ ROTR(x,8) ^ ((x)>>7))
|
||||
#define sigma1(x) (ROTR(x,19) ^ ROTR(x,61) ^ ((x)>>6))
|
||||
|
||||
#define A m->counter[0]
|
||||
#define B m->counter[1]
|
||||
#define C m->counter[2]
|
||||
#define D m->counter[3]
|
||||
#define E m->counter[4]
|
||||
#define F m->counter[5]
|
||||
#define G m->counter[6]
|
||||
#define H m->counter[7]
|
||||
|
||||
static uint64_t cshift64 (uint64_t x, unsigned int n)
|
||||
{
|
||||
return ((uint64_t)x << (uint64_t)n) | ((uint64_t)x >> ((uint64_t)64 - (uint64_t)n));
|
||||
}
|
||||
|
||||
static const uint64_t constant_512[80] = {
|
||||
0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL,
|
||||
0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
|
||||
0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,
|
||||
0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL,
|
||||
0xd807aa98a3030242ULL, 0x12835b0145706fbeULL,
|
||||
0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
|
||||
0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL,
|
||||
0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL,
|
||||
0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL,
|
||||
0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL,
|
||||
0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL,
|
||||
0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
|
||||
0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL,
|
||||
0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL,
|
||||
0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL,
|
||||
0x06ca6351e003826fULL, 0x142929670a0e6e70ULL,
|
||||
0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL,
|
||||
0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
|
||||
0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL,
|
||||
0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
|
||||
0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL,
|
||||
0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL,
|
||||
0xd192e819d6ef5218ULL, 0xd69906245565a910ULL,
|
||||
0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
|
||||
0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL,
|
||||
0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL,
|
||||
0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL,
|
||||
0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL,
|
||||
0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL,
|
||||
0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
|
||||
0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL,
|
||||
0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,
|
||||
0xca273eceea26619cULL, 0xd186b8c721c0c207ULL,
|
||||
0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL,
|
||||
0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL,
|
||||
0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
|
||||
0x28db77f523047d84ULL, 0x32caab7b40c72493ULL,
|
||||
0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL,
|
||||
0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL,
|
||||
0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL
|
||||
};
|
||||
|
||||
void
|
||||
SHA512_Init (SHA512_CTX *m)
|
||||
{
|
||||
m->sz[0] = 0;
|
||||
m->sz[1] = 0;
|
||||
A = 0x6a09e667f3bcc908ULL;
|
||||
B = 0xbb67ae8584caa73bULL;
|
||||
C = 0x3c6ef372fe94f82bULL;
|
||||
D = 0xa54ff53a5f1d36f1ULL;
|
||||
E = 0x510e527fade682d1ULL;
|
||||
F = 0x9b05688c2b3e6c1fULL;
|
||||
G = 0x1f83d9abfb41bd6bULL;
|
||||
H = 0x5be0cd19137e2179ULL;
|
||||
}
|
||||
|
||||
static void
|
||||
calc (SHA512_CTX *m, uint64_t *in)
|
||||
{
|
||||
uint64_t AA, BB, CC, DD, EE, FF, GG, HH;
|
||||
uint64_t data[80];
|
||||
int i;
|
||||
|
||||
AA = A;
|
||||
BB = B;
|
||||
CC = C;
|
||||
DD = D;
|
||||
EE = E;
|
||||
FF = F;
|
||||
GG = G;
|
||||
HH = H;
|
||||
|
||||
for (i = 0; i < 16; ++i)
|
||||
data[i] = in[i];
|
||||
for (i = 16; i < 80; ++i)
|
||||
data[i] = sigma1(data[i-2]) + data[i-7] +
|
||||
sigma0(data[i-15]) + data[i - 16];
|
||||
|
||||
for (i = 0; i < 80; i++) {
|
||||
uint64_t T1, T2;
|
||||
|
||||
T1 = HH + Sigma1(EE) + Ch(EE, FF, GG) + constant_512[i] + data[i];
|
||||
T2 = Sigma0(AA) + Maj(AA,BB,CC);
|
||||
|
||||
HH = GG;
|
||||
GG = FF;
|
||||
FF = EE;
|
||||
EE = DD + T1;
|
||||
DD = CC;
|
||||
CC = BB;
|
||||
BB = AA;
|
||||
AA = T1 + T2;
|
||||
}
|
||||
|
||||
A += AA;
|
||||
B += BB;
|
||||
C += CC;
|
||||
D += DD;
|
||||
E += EE;
|
||||
F += FF;
|
||||
G += GG;
|
||||
H += HH;
|
||||
}
|
||||
|
||||
/*
|
||||
* From `Performance analysis of MD5' by Joseph D. Touch <touch@isi.edu>
|
||||
*/
|
||||
|
||||
#if !defined(WORDS_BIGENDIAN) || defined(_CRAY)
|
||||
static uint64_t
|
||||
swap_uint64_t (uint64_t t)
|
||||
{
|
||||
uint64_t temp;
|
||||
|
||||
temp = cshift64(t, 32);
|
||||
temp = ((temp & 0xff00ff00ff00ff00ULL) >> 8) |
|
||||
((temp & 0x00ff00ff00ff00ffULL) << 8);
|
||||
return ((temp & 0xffff0000ffff0000ULL) >> 16) |
|
||||
((temp & 0x0000ffff0000ffffULL) << 16);
|
||||
}
|
||||
|
||||
struct x64{
|
||||
uint64_t a;
|
||||
uint64_t b;
|
||||
};
|
||||
#endif
|
||||
|
||||
void
|
||||
SHA512_Update (SHA512_CTX *m, const void *v, size_t len)
|
||||
{
|
||||
const unsigned char *p = v;
|
||||
size_t old_sz = m->sz[0];
|
||||
size_t offset;
|
||||
|
||||
m->sz[0] += len * 8;
|
||||
if (m->sz[0] < old_sz)
|
||||
++m->sz[1];
|
||||
offset = (old_sz / 8) % 128;
|
||||
while(len > 0){
|
||||
size_t l = min(len, 128 - offset);
|
||||
memcpy(m->save + offset, p, l);
|
||||
offset += l;
|
||||
p += l;
|
||||
len -= l;
|
||||
if(offset == 128){
|
||||
#if !defined(WORDS_BIGENDIAN) || defined(_CRAY)
|
||||
int i;
|
||||
uint64_t current[16];
|
||||
struct x64 *us = (struct x64*)m->save;
|
||||
for(i = 0; i < 8; i++){
|
||||
current[2*i+0] = swap_uint64_t(us[i].a);
|
||||
current[2*i+1] = swap_uint64_t(us[i].b);
|
||||
}
|
||||
calc(m, current);
|
||||
#else
|
||||
calc(m, (uint64_t*)m->save);
|
||||
#endif
|
||||
offset = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
SHA512_Final (void *res, SHA512_CTX *m)
|
||||
{
|
||||
unsigned char zeros[128 + 16];
|
||||
unsigned offset = (m->sz[0] / 8) % 128;
|
||||
unsigned int dstart = (240 - offset - 1) % 128 + 1;
|
||||
|
||||
*zeros = 0x80;
|
||||
memset (zeros + 1, 0, sizeof(zeros) - 1);
|
||||
zeros[dstart+15] = (m->sz[0] >> 0) & 0xff;
|
||||
zeros[dstart+14] = (m->sz[0] >> 8) & 0xff;
|
||||
zeros[dstart+13] = (m->sz[0] >> 16) & 0xff;
|
||||
zeros[dstart+12] = (m->sz[0] >> 24) & 0xff;
|
||||
zeros[dstart+11] = (m->sz[0] >> 32) & 0xff;
|
||||
zeros[dstart+10] = (m->sz[0] >> 40) & 0xff;
|
||||
zeros[dstart+9] = (m->sz[0] >> 48) & 0xff;
|
||||
zeros[dstart+8] = (m->sz[0] >> 56) & 0xff;
|
||||
|
||||
zeros[dstart+7] = (m->sz[1] >> 0) & 0xff;
|
||||
zeros[dstart+6] = (m->sz[1] >> 8) & 0xff;
|
||||
zeros[dstart+5] = (m->sz[1] >> 16) & 0xff;
|
||||
zeros[dstart+4] = (m->sz[1] >> 24) & 0xff;
|
||||
zeros[dstart+3] = (m->sz[1] >> 32) & 0xff;
|
||||
zeros[dstart+2] = (m->sz[1] >> 40) & 0xff;
|
||||
zeros[dstart+1] = (m->sz[1] >> 48) & 0xff;
|
||||
zeros[dstart+0] = (m->sz[1] >> 56) & 0xff;
|
||||
SHA512_Update (m, zeros, dstart + 16);
|
||||
{
|
||||
int i;
|
||||
unsigned char *r = (unsigned char*)res;
|
||||
|
||||
for (i = 0; i < 8; ++i) {
|
||||
r[8*i+7] = m->counter[i] & 0xFF;
|
||||
r[8*i+6] = (m->counter[i] >> 8) & 0xFF;
|
||||
r[8*i+5] = (m->counter[i] >> 16) & 0xFF;
|
||||
r[8*i+4] = (m->counter[i] >> 24) & 0xFF;
|
||||
r[8*i+3] = (m->counter[i] >> 32) & 0XFF;
|
||||
r[8*i+2] = (m->counter[i] >> 40) & 0xFF;
|
||||
r[8*i+1] = (m->counter[i] >> 48) & 0xFF;
|
||||
r[8*i] = (m->counter[i] >> 56) & 0xFF;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
SHA384_Init(SHA384_CTX *m)
|
||||
{
|
||||
m->sz[0] = 0;
|
||||
m->sz[1] = 0;
|
||||
A = 0xcbbb9d5dc1059ed8ULL;
|
||||
B = 0x629a292a367cd507ULL;
|
||||
C = 0x9159015a3070dd17ULL;
|
||||
D = 0x152fecd8f70e5939ULL;
|
||||
E = 0x67332667ffc00b31ULL;
|
||||
F = 0x8eb44a8768581511ULL;
|
||||
G = 0xdb0c2e0d64f98fa7ULL;
|
||||
H = 0x47b5481dbefa4fa4ULL;
|
||||
}
|
||||
|
||||
void
|
||||
SHA384_Update (SHA384_CTX *m, const void *v, size_t len)
|
||||
{
|
||||
SHA512_Update(m, v, len);
|
||||
}
|
||||
|
||||
void
|
||||
SHA384_Final (void *res, SHA384_CTX *m)
|
||||
{
|
||||
unsigned char data[SHA512_DIGEST_LENGTH];
|
||||
SHA512_Final(data, m);
|
||||
memcpy(res, data, SHA384_DIGEST_LENGTH);
|
||||
}
|
||||
288
C/md5.c
288
C/md5.c
@@ -1,288 +0,0 @@
|
||||
|
||||
/*
|
||||
* This is an OpenSSL-compatible implementation of the RSA Data Security, Inc.
|
||||
* MD5 Message-Digest Algorithm (RFC 1321).
|
||||
*
|
||||
* Homepage:
|
||||
* http://openwall.info/wiki/people/solar/software/public-domain-source-code/md5
|
||||
*
|
||||
* Author:
|
||||
* Alexander Peslyak, better known as Solar Designer <solar at openwall.com>
|
||||
*
|
||||
* This software was written by Alexander Peslyak in 2001. No copyright is
|
||||
* claimed, and the software is hereby placed in the public domain.
|
||||
* In case this attempt to disclaim copyright and place the software in the
|
||||
* public domain is deemed null and void, then the software is
|
||||
* Copyright (c) 2001 Alexander Peslyak and it is hereby released to the
|
||||
* general public under the following terms:
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted.
|
||||
*
|
||||
* There's ABSOLUTELY NO WARRANTY, express or implied.
|
||||
*
|
||||
* (This is a heavily cut-down "BSD license".)
|
||||
*
|
||||
* This differs from Colin Plumb's older public domain implementation in that
|
||||
* no exactly 32-bit integer data type is required (any 32-bit or wider
|
||||
* unsigned integer data type will do), there's no compile-time endianness
|
||||
* configuration, and the function prototypes match OpenSSL's. No code from
|
||||
* Colin Plumb's implementation has been reused; this comment merely compares
|
||||
* the properties of the two independent implementations.
|
||||
*
|
||||
* The primary goals of this implementation are portability and ease of use.
|
||||
* It is meant to be fast, but not as fast as possible. Some known
|
||||
* optimizations are not included to reduce source code size and avoid
|
||||
* compile-time configuration.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "md5.h"
|
||||
|
||||
/*
|
||||
* The basic MD5 functions.
|
||||
*
|
||||
* F and G are optimized compared to their RFC 1321 definitions for
|
||||
* architectures that lack an AND-NOT instruction, just like in Colin Plumb's
|
||||
* implementation.
|
||||
*/
|
||||
#define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
|
||||
#define G(x, y, z) ((y) ^ ((z) & ((x) ^ (y))))
|
||||
#define H(x, y, z) (((x) ^ (y)) ^ (z))
|
||||
#define H2(x, y, z) ((x) ^ ((y) ^ (z)))
|
||||
#define I(x, y, z) ((y) ^ ((x) | ~(z)))
|
||||
|
||||
/*
|
||||
* The MD5 transformation for all four rounds.
|
||||
*/
|
||||
#define STEP(f, a, b, c, d, x, t, s) \
|
||||
(a) += f((b), (c), (d)) + (x) + (t); \
|
||||
(a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); \
|
||||
(a) += (b);
|
||||
|
||||
/*
|
||||
* SET reads 4 input bytes in little-endian byte order and stores them in a
|
||||
* properly aligned word in host byte order.
|
||||
*
|
||||
* The check for little-endian architectures that tolerate unaligned memory
|
||||
* accesses is just an optimization. Nothing will break if it fails to detect
|
||||
* a suitable architecture.
|
||||
*
|
||||
* Unfortunately, this optimization may be a C strict aliasing rules violation
|
||||
* if the caller's data buffer has effective type that cannot be aliased by
|
||||
* MD5_u32plus. In practice, this problem may occur if these MD5 routines are
|
||||
* inlined into a calling function, or with future and dangerously advanced
|
||||
* link-time optimizations. For the time being, keeping these MD5 routines in
|
||||
* their own translation unit avoids the problem.
|
||||
*/
|
||||
#if defined(__i386__) || defined(__x86_64__) || defined(__vax__)
|
||||
#define SET(n) \
|
||||
(*(MD5_u32plus *)&ptr[(n) * 4])
|
||||
#define GET(n) \
|
||||
SET(n)
|
||||
#else
|
||||
#define SET(n) \
|
||||
(ctx->block[(n)] = \
|
||||
(MD5_u32plus)ptr[(n) * 4] | \
|
||||
((MD5_u32plus)ptr[(n) * 4 + 1] << 8) | \
|
||||
((MD5_u32plus)ptr[(n) * 4 + 2] << 16) | \
|
||||
((MD5_u32plus)ptr[(n) * 4 + 3] << 24))
|
||||
#define GET(n) \
|
||||
(ctx->block[(n)])
|
||||
#endif
|
||||
|
||||
/*
|
||||
* This processes one or more 64-byte data blocks, but does NOT update the bit
|
||||
* counters. There are no alignment requirements.
|
||||
*/
|
||||
static const void *body(MD5_CTX *ctx, const void *data, unsigned long size)
|
||||
{
|
||||
const unsigned char *ptr;
|
||||
MD5_u32plus a, b, c, d;
|
||||
MD5_u32plus saved_a, saved_b, saved_c, saved_d;
|
||||
|
||||
ptr = (const unsigned char *)data;
|
||||
|
||||
a = ctx->a;
|
||||
b = ctx->b;
|
||||
c = ctx->c;
|
||||
d = ctx->d;
|
||||
|
||||
do {
|
||||
saved_a = a;
|
||||
saved_b = b;
|
||||
saved_c = c;
|
||||
saved_d = d;
|
||||
|
||||
/* Round 1 */
|
||||
STEP(F, a, b, c, d, SET(0), 0xd76aa478, 7)
|
||||
STEP(F, d, a, b, c, SET(1), 0xe8c7b756, 12)
|
||||
STEP(F, c, d, a, b, SET(2), 0x242070db, 17)
|
||||
STEP(F, b, c, d, a, SET(3), 0xc1bdceee, 22)
|
||||
STEP(F, a, b, c, d, SET(4), 0xf57c0faf, 7)
|
||||
STEP(F, d, a, b, c, SET(5), 0x4787c62a, 12)
|
||||
STEP(F, c, d, a, b, SET(6), 0xa8304613, 17)
|
||||
STEP(F, b, c, d, a, SET(7), 0xfd469501, 22)
|
||||
STEP(F, a, b, c, d, SET(8), 0x698098d8, 7)
|
||||
STEP(F, d, a, b, c, SET(9), 0x8b44f7af, 12)
|
||||
STEP(F, c, d, a, b, SET(10), 0xffff5bb1, 17)
|
||||
STEP(F, b, c, d, a, SET(11), 0x895cd7be, 22)
|
||||
STEP(F, a, b, c, d, SET(12), 0x6b901122, 7)
|
||||
STEP(F, d, a, b, c, SET(13), 0xfd987193, 12)
|
||||
STEP(F, c, d, a, b, SET(14), 0xa679438e, 17)
|
||||
STEP(F, b, c, d, a, SET(15), 0x49b40821, 22)
|
||||
|
||||
/* Round 2 */
|
||||
STEP(G, a, b, c, d, GET(1), 0xf61e2562, 5)
|
||||
STEP(G, d, a, b, c, GET(6), 0xc040b340, 9)
|
||||
STEP(G, c, d, a, b, GET(11), 0x265e5a51, 14)
|
||||
STEP(G, b, c, d, a, GET(0), 0xe9b6c7aa, 20)
|
||||
STEP(G, a, b, c, d, GET(5), 0xd62f105d, 5)
|
||||
STEP(G, d, a, b, c, GET(10), 0x02441453, 9)
|
||||
STEP(G, c, d, a, b, GET(15), 0xd8a1e681, 14)
|
||||
STEP(G, b, c, d, a, GET(4), 0xe7d3fbc8, 20)
|
||||
STEP(G, a, b, c, d, GET(9), 0x21e1cde6, 5)
|
||||
STEP(G, d, a, b, c, GET(14), 0xc33707d6, 9)
|
||||
STEP(G, c, d, a, b, GET(3), 0xf4d50d87, 14)
|
||||
STEP(G, b, c, d, a, GET(8), 0x455a14ed, 20)
|
||||
STEP(G, a, b, c, d, GET(13), 0xa9e3e905, 5)
|
||||
STEP(G, d, a, b, c, GET(2), 0xfcefa3f8, 9)
|
||||
STEP(G, c, d, a, b, GET(7), 0x676f02d9, 14)
|
||||
STEP(G, b, c, d, a, GET(12), 0x8d2a4c8a, 20)
|
||||
|
||||
/* Round 3 */
|
||||
STEP(H, a, b, c, d, GET(5), 0xfffa3942, 4)
|
||||
STEP(H2, d, a, b, c, GET(8), 0x8771f681, 11)
|
||||
STEP(H, c, d, a, b, GET(11), 0x6d9d6122, 16)
|
||||
STEP(H2, b, c, d, a, GET(14), 0xfde5380c, 23)
|
||||
STEP(H, a, b, c, d, GET(1), 0xa4beea44, 4)
|
||||
STEP(H2, d, a, b, c, GET(4), 0x4bdecfa9, 11)
|
||||
STEP(H, c, d, a, b, GET(7), 0xf6bb4b60, 16)
|
||||
STEP(H2, b, c, d, a, GET(10), 0xbebfbc70, 23)
|
||||
STEP(H, a, b, c, d, GET(13), 0x289b7ec6, 4)
|
||||
STEP(H2, d, a, b, c, GET(0), 0xeaa127fa, 11)
|
||||
STEP(H, c, d, a, b, GET(3), 0xd4ef3085, 16)
|
||||
STEP(H2, b, c, d, a, GET(6), 0x04881d05, 23)
|
||||
STEP(H, a, b, c, d, GET(9), 0xd9d4d039, 4)
|
||||
STEP(H2, d, a, b, c, GET(12), 0xe6db99e5, 11)
|
||||
STEP(H, c, d, a, b, GET(15), 0x1fa27cf8, 16)
|
||||
STEP(H2, b, c, d, a, GET(2), 0xc4ac5665, 23)
|
||||
|
||||
/* Round 4 */
|
||||
STEP(I, a, b, c, d, GET(0), 0xf4292244, 6)
|
||||
STEP(I, d, a, b, c, GET(7), 0x432aff97, 10)
|
||||
STEP(I, c, d, a, b, GET(14), 0xab9423a7, 15)
|
||||
STEP(I, b, c, d, a, GET(5), 0xfc93a039, 21)
|
||||
STEP(I, a, b, c, d, GET(12), 0x655b59c3, 6)
|
||||
STEP(I, d, a, b, c, GET(3), 0x8f0ccc92, 10)
|
||||
STEP(I, c, d, a, b, GET(10), 0xffeff47d, 15)
|
||||
STEP(I, b, c, d, a, GET(1), 0x85845dd1, 21)
|
||||
STEP(I, a, b, c, d, GET(8), 0x6fa87e4f, 6)
|
||||
STEP(I, d, a, b, c, GET(15), 0xfe2ce6e0, 10)
|
||||
STEP(I, c, d, a, b, GET(6), 0xa3014314, 15)
|
||||
STEP(I, b, c, d, a, GET(13), 0x4e0811a1, 21)
|
||||
STEP(I, a, b, c, d, GET(4), 0xf7537e82, 6)
|
||||
STEP(I, d, a, b, c, GET(11), 0xbd3af235, 10)
|
||||
STEP(I, c, d, a, b, GET(2), 0x2ad7d2bb, 15)
|
||||
STEP(I, b, c, d, a, GET(9), 0xeb86d391, 21)
|
||||
|
||||
a += saved_a;
|
||||
b += saved_b;
|
||||
c += saved_c;
|
||||
d += saved_d;
|
||||
|
||||
ptr += 64;
|
||||
} while (size -= 64);
|
||||
|
||||
ctx->a = a;
|
||||
ctx->b = b;
|
||||
ctx->c = c;
|
||||
ctx->d = d;
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void MD5_Init(MD5_CTX *ctx)
|
||||
{
|
||||
ctx->a = 0x67452301;
|
||||
ctx->b = 0xefcdab89;
|
||||
ctx->c = 0x98badcfe;
|
||||
ctx->d = 0x10325476;
|
||||
|
||||
ctx->lo = 0;
|
||||
ctx->hi = 0;
|
||||
}
|
||||
|
||||
void MD5_Update(MD5_CTX *ctx, const void *data, unsigned long size)
|
||||
{
|
||||
MD5_u32plus saved_lo;
|
||||
unsigned long used, available;
|
||||
|
||||
saved_lo = ctx->lo;
|
||||
if ((ctx->lo = (saved_lo + size) & 0x1fffffff) < saved_lo)
|
||||
ctx->hi++;
|
||||
ctx->hi += size >> 29;
|
||||
|
||||
used = saved_lo & 0x3f;
|
||||
|
||||
if (used) {
|
||||
available = 64 - used;
|
||||
|
||||
if (size < available) {
|
||||
memcpy(&ctx->buffer[used], data, size);
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy(&ctx->buffer[used], data, available);
|
||||
data = (const unsigned char *)data + available;
|
||||
size -= available;
|
||||
body(ctx, ctx->buffer, 64);
|
||||
}
|
||||
|
||||
if (size >= 64) {
|
||||
data = body(ctx, data, size & ~(unsigned long)0x3f);
|
||||
size &= 0x3f;
|
||||
}
|
||||
|
||||
memcpy(ctx->buffer, data, size);
|
||||
}
|
||||
|
||||
#define OUT(dst, src) \
|
||||
(dst)[0] = (unsigned char)(src); \
|
||||
(dst)[1] = (unsigned char)((src) >> 8); \
|
||||
(dst)[2] = (unsigned char)((src) >> 16); \
|
||||
(dst)[3] = (unsigned char)((src) >> 24);
|
||||
|
||||
void MD5_Final(MD5_CTX *ctx, unsigned char *result)
|
||||
{
|
||||
unsigned long used, available;
|
||||
|
||||
used = ctx->lo & 0x3f;
|
||||
|
||||
ctx->buffer[used++] = 0x80;
|
||||
|
||||
available = 64 - used;
|
||||
|
||||
if (available < 8) {
|
||||
memset(&ctx->buffer[used], 0, available);
|
||||
body(ctx, ctx->buffer, 64);
|
||||
used = 0;
|
||||
available = 64;
|
||||
}
|
||||
|
||||
memset(&ctx->buffer[used], 0, available - 8);
|
||||
|
||||
ctx->lo <<= 3;
|
||||
OUT(&ctx->buffer[56], ctx->lo)
|
||||
OUT(&ctx->buffer[60], ctx->hi)
|
||||
|
||||
body(ctx, ctx->buffer, 64);
|
||||
|
||||
OUT(&result[0], ctx->a)
|
||||
OUT(&result[4], ctx->b)
|
||||
OUT(&result[8], ctx->c)
|
||||
OUT(&result[12], ctx->d)
|
||||
|
||||
memset(ctx, 0, sizeof(*ctx));
|
||||
}
|
||||
43
C/md5.h
43
C/md5.h
@@ -1,43 +0,0 @@
|
||||
/*
|
||||
* This is an OpenSSL-compatible implementation of the RSA Data Security, Inc.
|
||||
* MD5 Message-Digest Algorithm (RFC 1321).
|
||||
*
|
||||
* Homepage:
|
||||
* http://openwall.info/wiki/people/solar/software/public-domain-source-code/md5
|
||||
*
|
||||
* Author:
|
||||
* Alexander Peslyak, better known as Solar Designer <solar at openwall.com>
|
||||
*
|
||||
* This software was written by Alexander Peslyak in 2001. No copyright is
|
||||
* claimed, and the software is hereby placed in the public domain.
|
||||
* In case this attempt to disclaim copyright and place the software in the
|
||||
* public domain is deemed null and void, then the software is
|
||||
* Copyright (c) 2001 Alexander Peslyak and it is hereby released to the
|
||||
* general public under the following terms:
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted.
|
||||
*
|
||||
* There's ABSOLUTELY NO WARRANTY, express or implied.
|
||||
*
|
||||
* See md5.c for more information.
|
||||
*/
|
||||
|
||||
#ifndef _MD5_H
|
||||
#define _MD5_H
|
||||
|
||||
/* Any 32-bit or wider unsigned integer data type will do */
|
||||
typedef unsigned int MD5_u32plus;
|
||||
|
||||
typedef struct {
|
||||
MD5_u32plus lo, hi;
|
||||
MD5_u32plus a, b, c, d;
|
||||
unsigned char buffer[64];
|
||||
MD5_u32plus block[16];
|
||||
} MD5_CTX;
|
||||
|
||||
extern void MD5_Init(MD5_CTX *ctx);
|
||||
extern void MD5_Update(MD5_CTX *ctx, const void *data, unsigned long size);
|
||||
extern void MD5_Final(MD5_CTX *ctx, unsigned char *result);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user