Added sha1 refactored the access to the digest routines via digest.c. Other misc teaks to format and style of digest code.

git-svn-id: file:///home/mbr/svn/fwknop/trunk@8 510a4753-2344-4c79-9c09-4d669213fbeb
This commit is contained in:
Damien Stuart 2008-12-04 01:47:32 +00:00
parent 49378671fa
commit 8b54a0d4d6
11 changed files with 595 additions and 179 deletions

View File

@ -63,7 +63,9 @@ LIBSRCS = spa_random_number.c \
spa_version.c \
spa_message_type.c \
md5.c \
sha1.c \
sha256.c \
digest.c \
base64.c \
strlcat.c \
strlcpy.c
@ -118,13 +120,15 @@ depend:
#
# DO NOT DELETE
spa_random_number.o: fwknop.h types.h md5.h sha256.h base64.h
spa_user.o: fwknop.h types.h md5.h sha256.h base64.h
spa_timestamp.o: fwknop.h types.h md5.h sha256.h base64.h
spa_version.o: fwknop.h types.h md5.h sha256.h base64.h
spa_message_type.o: fwknop.h types.h md5.h sha256.h base64.h
spa_random_number.o: fwknop.h types.h digest.h md5.h sha.h base64.h
spa_user.o: fwknop.h types.h digest.h md5.h sha.h base64.h
spa_timestamp.o: fwknop.h types.h digest.h md5.h sha.h base64.h
spa_version.o: fwknop.h types.h digest.h md5.h sha.h base64.h
spa_message_type.o: fwknop.h types.h digest.h md5.h sha.h base64.h
md5.o: md5.h types.h
sha256.o: sha256.h types.h
sha1.o: sha.h types.h
sha256.o: sha.h types.h
digest.o: digest.h types.h md5.h sha.h
base64.o: base64.h
fwknop.o: fwknop.h types.h md5.h sha256.h base64.h
fko_test.o: fwknop.h types.h md5.h sha256.h base64.h
fwknop.o: fwknop.h types.h digest.h md5.h sha.h base64.h
fko_test.o: fwknop.h types.h digest.h md5.h sha.h base64.h

83
digest.c Normal file
View File

@ -0,0 +1,83 @@
/* $Id$
*****************************************************************************
*
* File: digest.c
*
* Author: Damien S. Stuart
*
* Purpose: Roll-up of teh digests used by fwknop.
*
* License (GNU Public License):
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*
*****************************************************************************
*/
#include "digest.h"
/* Convert a raw digest into its hex string representation.
*/
void digest_to_hex(uint8 *in, char *digest, int in_len)
{
int i;
for(i=0; i<in_len; i++)
{
sprintf(digest, "%02x", in[i]);
digest += 2;
}
}
/* Compute MD5 hash on in and store the hex string result in out.
*/
void md5(char *in, char *digest, int in_len)
{
MD5Context ctx;
uint8 md[MD5_DIGESTSIZE];
MD5Init(&ctx);
MD5Update(&ctx, (unsigned char*)in, in_len);
MD5Final(md, &ctx);
digest_to_hex(md, digest, MD5_DIGESTSIZE);
}
/* Compute SHA1 hash on in and store the hex string result in out.
*/
void sha1(char *in, char *digest, int in_len)
{
SHA_INFO sha_info;
uint8 md[SHA1_DIGESTSIZE];
sha1_init(&sha_info);
sha1_update(&sha_info, (uint8*)in, in_len);
sha1_final(md, &sha_info);
digest_to_hex(md, digest, SHA1_DIGESTSIZE);
}
/* Compute SHA256 hash on in and store the hex string result in out.
*/
void sha256(char *in, char *digest, int in_len)
{
SHA_INFO sha_info;
uint8 md[SHA256_DIGESTSIZE];
sha256_init(&sha_info);
sha256_update(&sha_info, (uint8*)in, in_len);
sha256_final(&sha_info);
sha256_unpackdigest(md, &sha_info);
digest_to_hex(md, digest, SHA256_DIGESTSIZE);
}
/***EOF***/

45
digest.h Normal file
View File

@ -0,0 +1,45 @@
/* $Id$
*****************************************************************************
*
* File: digest.h
*
* Author: Damien S. Stuart
*
* Purpose: Header for the fwknop digest.c.
*
* License (GNU Public License):
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*
*****************************************************************************
*/
#ifndef _DIGEST_H_
#define _DIGEST_H_
#include <endian.h>
#include "types.h"
/* This should be fine for most linux systems (hopefully).
* TODO: We should look into the portability of this. --DSS
*/
#define BYTEORDER __BYTE_ORDER
#include "md5.h"
#include "sha.h"
void md5(char* in, char* out, int in_len);
void sha1(char* in, char* out, int in_len);
void sha256(char* in, char* out, int in_len);
#endif /* _DIGEST_H_ */
/***EOF***/

View File

@ -159,6 +159,23 @@ int main(int argc, char **argv)
tst_string, tst_md5_digest, md5_digest
);
/*********************************************************************
* SHA1 test.
*/
char sha1_digest[41] = {0};
char tst_sha1_digest[] = "afa6c8b3a2fae95785dc7d9685a57835d703ac88";
/* Use our convenient md5 function.
*/
sha1(tst_string, sha1_digest, strlen(tst_string));
printf(
"\nSHA1 of '%s':\n"
" Should be: %s\n"
" Computed as: %s\n",
tst_string, tst_sha1_digest, sha1_digest
);
/*********************************************************************
* SHA256 test.
*/

View File

@ -36,8 +36,7 @@
#include "types.h"
#include "md5.h"
#include "sha256.h"
#include "digest.h"
#include "base64.h"
/* General params

23
md5.c
View File

@ -53,29 +53,6 @@
}
#endif
/* Compute MD5 hash on in and store the hex string result in out.
*/
void md5(char *in, char *digest, int in_len)
{
MD5Context ctx;
int i;
unsigned char md[MD5_DIGESTSIZE];
MD5Init(&ctx);
MD5Update(&ctx, (unsigned char*)in, in_len);
MD5Final(md, &ctx);
for(i=0; i<MD5_DIGESTSIZE; i++)
{
sprintf(digest, "%02x", md[i]);
digest += 2;
}
}
/*
* Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
* initialization constants.

1
md5.h
View File

@ -64,7 +64,6 @@ typedef struct _MD5Context {
unsigned char in[64];
} MD5Context;
void md5(char* in, char* out, int in_len);
void MD5Init(MD5Context*);
void MD5Update(MD5Context *ctx, unsigned char *buf, unsigned len);
void MD5Final(unsigned char digest[16], MD5Context *ctx);

View File

@ -3,10 +3,10 @@
*
* File: sha256.h
*
* Purpose: Header for sha256.c
* Purpose: Header for sha.c
*
* sha - An implementation of the NIST SHA 256/384/512 Message Digest
* algorithm.
* sha - An implementation of the NIST SHA Message Digest
* algorithm. This header covers SHA1 and SHA256
*
* Copyright (C) 2001 Rafael R. Sevilla <sevillar@team.ph.inter.net>
* This library is free software; you can redistribute it and/or
@ -39,6 +39,7 @@
#define BYTEORDER __BYTE_ORDER
#define SHA_BLOCKSIZE 64
#define SHA1_DIGESTSIZE 20
#define SHA256_DIGESTSIZE 32
typedef struct {
@ -46,12 +47,19 @@ typedef struct {
uint32 count_lo, count_hi;
uint8 data[SHA_BLOCKSIZE];
int local;
} SHA256_INFO;
} SHA_INFO;
void sha256(char *in, char *digest, int in_len);
void sha256_init(SHA256_INFO *sha256_info);
void sha256_update(SHA256_INFO *sha256_info, uint8 *buffer, int count);
void sha256_final(SHA256_INFO *sha256_info);
void sha256_unpackdigest(uint8 digest[32], SHA256_INFO *sha256_info);
/* SHA1 prototypes.
*/
void sha1_init(SHA_INFO *sha_info);
void sha1_update(SHA_INFO *sha_info, uint8 *buffer, int count);
void sha1_final(uint8 digest[SHA1_DIGESTSIZE], SHA_INFO *sha_info);
/* SHA256 prototypes.
*/
void sha256_init(SHA_INFO *sha_info);
void sha256_update(SHA_INFO *sha_info, uint8 *buffer, int count);
void sha256_final(SHA_INFO *sha_info);
void sha256_unpackdigest(uint8 digest[SHA256_DIGESTSIZE], SHA_INFO *sha_info);
#endif /* _SHA256_H_ */

277
sha1.c Normal file
View File

@ -0,0 +1,277 @@
/* $Id$
*****************************************************************************
*
* File: sha1.c
*
* Purpose: Implementation of the SHA1 message-digest algorithm for
* libfwknop.
*
* NIST Secure Hash Algorithm
* Heavily modified by Uwe Hollerbach <uh@alumni.caltech edu>
* from Peter C. Gutmann's implementation as found in
* Applied Cryptography by Bruce Schneier
* Further modifications to include the "UNRAVEL" stuff, below
*
* This code is in the public domain
*
*****************************************************************************
*/
#include "sha.h"
/* 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
/* truncate to 32 bits -- should be a null op on 32-bit machines */
#define T32(x) ((x) & 0xffffffffL)
/* 32-bit rotate */
#define R32(x,n) T32(((x << n) | (x >> (32 - n))))
/* the generic case, for when the overall rotation is not unraveled */
#define FG(n) \
T = T32(R32(A,5) + f##n(B,C,D) + E + *WP++ + CONST##n); \
E = D; D = C; C = R32(B,30); B = A; A = T
/* specific cases, for when the overall rotation is unraveled */
#define FA(n) \
T = T32(R32(A,5) + f##n(B,C,D) + E + *WP++ + CONST##n); B = R32(B,30)
#define FB(n) \
E = T32(R32(T,5) + f##n(A,B,C) + D + *WP++ + CONST##n); A = R32(A,30)
#define FC(n) \
D = T32(R32(E,5) + f##n(T,A,B) + C + *WP++ + CONST##n); T = R32(T,30)
#define FD(n) \
C = T32(R32(D,5) + f##n(E,T,A) + B + *WP++ + CONST##n); E = R32(E,30)
#define FE(n) \
B = T32(R32(C,5) + f##n(D,E,T) + A + *WP++ + CONST##n); D = R32(D,30)
#define FT(n) \
A = T32(R32(B,5) + f##n(C,D,E) + T + *WP++ + CONST##n); C = R32(C,30)
void sha1_transform(SHA_INFO *sha_info)
{
int i;
uint8 *dp;
uint32 T, A, B, C, D, E, W[80], *WP;
dp = sha_info->data;
#undef SWAP_DONE
#if BYTEORDER == 1234
#define SWAP_DONE
for (i = 0; i < 16; ++i) {
T = *((uint32 *) dp);
dp += 4;
W[i] =
((T << 24) & 0xff000000) |
((T << 8) & 0x00ff0000) |
((T >> 8) & 0x0000ff00) | ((T >> 24) & 0x000000ff);
}
#endif
#if BYTEORDER == 4321
#define SWAP_DONE
for (i = 0; i < 16; ++i) {
T = *((uint32 *) dp);
dp += 4;
W[i] = TRUNC32(T);
}
#endif
#if BYTEORDER == 12345678
#define SWAP_DONE
for (i = 0; i < 16; i += 2) {
T = *((uint32 *) dp);
dp += 8;
W[i] = ((T << 24) & 0xff000000) | ((T << 8) & 0x00ff0000) |
((T >> 8) & 0x0000ff00) | ((T >> 24) & 0x000000ff);
T >>= 32;
W[i+1] = ((T << 24) & 0xff000000) | ((T << 8) & 0x00ff0000) |
((T >> 8) & 0x0000ff00) | ((T >> 24) & 0x000000ff);
}
#endif
#if BYTEORDER == 87654321
#define SWAP_DONE
for (i = 0; i < 16; i += 2) {
T = *((uint32 *) dp);
dp += 8;
W[i] = TRUNC32(T >> 32);
W[i+1] = TRUNC32(T);
}
#endif
#ifndef SWAP_DONE
#error Unknown byte order -- you need to add code here
#endif /* SWAP_DONE */
for (i = 16; i < 80; ++i) {
W[i] = W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16];
W[i] = R32(W[i], 1);
}
A = sha_info->digest[0];
B = sha_info->digest[1];
C = sha_info->digest[2];
D = sha_info->digest[3];
E = sha_info->digest[4];
WP = W;
#ifdef UNRAVEL
FA(1); FB(1); FC(1); FD(1); FE(1); FT(1); FA(1); FB(1); FC(1); FD(1);
FE(1); FT(1); FA(1); FB(1); FC(1); FD(1); FE(1); FT(1); FA(1); FB(1);
FC(2); FD(2); FE(2); FT(2); FA(2); FB(2); FC(2); FD(2); FE(2); FT(2);
FA(2); FB(2); FC(2); FD(2); FE(2); FT(2); FA(2); FB(2); FC(2); FD(2);
FE(3); FT(3); FA(3); FB(3); FC(3); FD(3); FE(3); FT(3); FA(3); FB(3);
FC(3); FD(3); FE(3); FT(3); FA(3); FB(3); FC(3); FD(3); FE(3); FT(3);
FA(4); FB(4); FC(4); FD(4); FE(4); FT(4); FA(4); FB(4); FC(4); FD(4);
FE(4); FT(4); FA(4); FB(4); FC(4); FD(4); FE(4); FT(4); FA(4); FB(4);
sha_info->digest[0] = T32(sha_info->digest[0] + E);
sha_info->digest[1] = T32(sha_info->digest[1] + T);
sha_info->digest[2] = T32(sha_info->digest[2] + A);
sha_info->digest[3] = T32(sha_info->digest[3] + B);
sha_info->digest[4] = T32(sha_info->digest[4] + C);
#else /* !UNRAVEL */
#ifdef UNROLL_LOOPS
FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1);
FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1);
FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2);
FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2);
FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3);
FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3);
FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4);
FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4);
#else /* !UNROLL_LOOPS */
for (i = 0; i < 20; ++i) { FG(1); }
for (i = 20; i < 40; ++i) { FG(2); }
for (i = 40; i < 60; ++i) { FG(3); }
for (i = 60; i < 80; ++i) { FG(4); }
#endif /* !UNROLL_LOOPS */
sha_info->digest[0] = T32(sha_info->digest[0] + A);
sha_info->digest[1] = T32(sha_info->digest[1] + B);
sha_info->digest[2] = T32(sha_info->digest[2] + C);
sha_info->digest[3] = T32(sha_info->digest[3] + D);
sha_info->digest[4] = T32(sha_info->digest[4] + E);
#endif /* !UNRAVEL */
}
/* initialize the SHA digest */
void sha1_init(SHA_INFO *sha_info)
{
sha_info->digest[0] = 0x67452301L;
sha_info->digest[1] = 0xefcdab89L;
sha_info->digest[2] = 0x98badcfeL;
sha_info->digest[3] = 0x10325476L;
sha_info->digest[4] = 0xc3d2e1f0L;
sha_info->count_lo = 0L;
sha_info->count_hi = 0L;
sha_info->local = 0;
}
/* update the SHA digest */
void sha1_update(SHA_INFO *sha_info, uint8 *buffer, int count)
{
int i;
uint32 clo;
clo = T32(sha_info->count_lo + ((uint32) count << 3));
if (clo < sha_info->count_lo) {
++sha_info->count_hi;
}
sha_info->count_lo = clo;
sha_info->count_hi += (uint32) count >> 29;
if (sha_info->local) {
i = SHA_BLOCKSIZE - sha_info->local;
if (i > count) {
i = count;
}
memcpy(((uint8 *) sha_info->data) + sha_info->local, buffer, i);
count -= i;
buffer += i;
sha_info->local += i;
if (sha_info->local == SHA_BLOCKSIZE) {
sha1_transform(sha_info);
} else {
return;
}
}
while (count >= SHA_BLOCKSIZE) {
memcpy(sha_info->data, buffer, SHA_BLOCKSIZE);
buffer += SHA_BLOCKSIZE;
count -= SHA_BLOCKSIZE;
sha1_transform(sha_info);
}
memcpy(sha_info->data, buffer, count);
sha_info->local = count;
}
void sha1_transform_and_copy(unsigned char digest[20], SHA_INFO *sha_info)
{
sha1_transform(sha_info);
digest[ 0] = (unsigned char) ((sha_info->digest[0] >> 24) & 0xff);
digest[ 1] = (unsigned char) ((sha_info->digest[0] >> 16) & 0xff);
digest[ 2] = (unsigned char) ((sha_info->digest[0] >> 8) & 0xff);
digest[ 3] = (unsigned char) ((sha_info->digest[0] ) & 0xff);
digest[ 4] = (unsigned char) ((sha_info->digest[1] >> 24) & 0xff);
digest[ 5] = (unsigned char) ((sha_info->digest[1] >> 16) & 0xff);
digest[ 6] = (unsigned char) ((sha_info->digest[1] >> 8) & 0xff);
digest[ 7] = (unsigned char) ((sha_info->digest[1] ) & 0xff);
digest[ 8] = (unsigned char) ((sha_info->digest[2] >> 24) & 0xff);
digest[ 9] = (unsigned char) ((sha_info->digest[2] >> 16) & 0xff);
digest[10] = (unsigned char) ((sha_info->digest[2] >> 8) & 0xff);
digest[11] = (unsigned char) ((sha_info->digest[2] ) & 0xff);
digest[12] = (unsigned char) ((sha_info->digest[3] >> 24) & 0xff);
digest[13] = (unsigned char) ((sha_info->digest[3] >> 16) & 0xff);
digest[14] = (unsigned char) ((sha_info->digest[3] >> 8) & 0xff);
digest[15] = (unsigned char) ((sha_info->digest[3] ) & 0xff);
digest[16] = (unsigned char) ((sha_info->digest[4] >> 24) & 0xff);
digest[17] = (unsigned char) ((sha_info->digest[4] >> 16) & 0xff);
digest[18] = (unsigned char) ((sha_info->digest[4] >> 8) & 0xff);
digest[19] = (unsigned char) ((sha_info->digest[4] ) & 0xff);
}
/* finish computing the SHA digest */
void sha1_final(uint8 digest[20], SHA_INFO *sha_info)
{
int count;
uint32 lo_bit_count, hi_bit_count;
lo_bit_count = sha_info->count_lo;
hi_bit_count = sha_info->count_hi;
count = (int) ((lo_bit_count >> 3) & 0x3f);
((uint8 *) sha_info->data)[count++] = 0x80;
if (count > SHA_BLOCKSIZE - 8) {
memset(((uint8 *) sha_info->data) + count, 0, SHA_BLOCKSIZE - count);
sha1_transform(sha_info);
memset((uint8 *) sha_info->data, 0, SHA_BLOCKSIZE - 8);
} else {
memset(((uint8 *) sha_info->data) + count, 0,
SHA_BLOCKSIZE - 8 - count);
}
sha_info->data[56] = (uint8)((hi_bit_count >> 24) & 0xff);
sha_info->data[57] = (uint8)((hi_bit_count >> 16) & 0xff);
sha_info->data[58] = (uint8)((hi_bit_count >> 8) & 0xff);
sha_info->data[59] = (uint8)((hi_bit_count >> 0) & 0xff);
sha_info->data[60] = (uint8)((lo_bit_count >> 24) & 0xff);
sha_info->data[61] = (uint8)((lo_bit_count >> 16) & 0xff);
sha_info->data[62] = (uint8)((lo_bit_count >> 8) & 0xff);
sha_info->data[63] = (uint8)((lo_bit_count >> 0) & 0xff);
sha1_transform_and_copy(digest, sha_info);
}
/***EOF***/

237
sha256.c
View File

@ -1,16 +1,14 @@
/* $Id$
*****************************************************************************
*
* File: md5.c
* File: sha256.c
*
* Purpose: Implementation of the SHA256 message-digest algorithm for
* libfwknop. This file also happens to include SHA 384 and 512
* though they are not currently used by fwknop.
* libfwknop.
*
* sha - An implementation of the NIST SHA 256/384/512 Message Digest
* algorithm
*
* Copyright (C) 2001 Rafael R. Sevilla <sevillar@team.ph.inter.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
@ -27,7 +25,7 @@
*
*****************************************************************************
*/
#include "sha256.h"
#include "sha.h"
/* Truncate to 32 bits -- should be a null op on 32-bit machines
*/
@ -71,33 +69,13 @@ static uint32 K[64] = {
0x90befffaL, 0xa4506cebL, 0xbef9a3f7L, 0xc67178f2L
};
/* Convenience function fwknop.
*/
void sha256(char *in, char *digest, int in_len)
{
int i;
SHA256_INFO sha256_info;
uint8 md[SHA256_DIGESTSIZE];
sha256_init(&sha256_info);
sha256_update(&sha256_info, (uint8*)in, in_len);
sha256_final(&sha256_info);
sha256_unpackdigest(md, &sha256_info);
for(i=0; i<SHA256_DIGESTSIZE; i++)
{
sprintf(digest, "%02x", md[i]);
digest += 2;
}
}
static void sha256_transform(SHA256_INFO *sha256_info)
static void sha256_transform(SHA_INFO *sha_info)
{
int i, j;
uint8 *dp;
uint32 T, T1, T2, A, B, C, D, E, F, G, H, W[64];
dp = sha256_info->data;
dp = sha_info->data;
#undef SWAP_DONE
@ -148,14 +126,14 @@ static void sha256_transform(SHA256_INFO *sha256_info)
#ifndef SWAP_DONE
#error Unknown byte order -- you need to add code here
#endif /* SWAP_DONE */
A = sha256_info->digest[0];
B = sha256_info->digest[1];
C = sha256_info->digest[2];
D = sha256_info->digest[3];
E = sha256_info->digest[4];
F = sha256_info->digest[5];
G = sha256_info->digest[6];
H = sha256_info->digest[7];
A = sha_info->digest[0];
B = sha_info->digest[1];
C = sha_info->digest[2];
D = sha_info->digest[3];
E = sha_info->digest[4];
F = sha_info->digest[5];
G = sha_info->digest[6];
H = sha_info->digest[7];
for (i=16; i<64; i++)
W[i] = TRUNC32(LSIG1(W[i-2]) + W[i-7] + LSIG0(W[i-15]) + W[i-16]);
@ -173,148 +151,135 @@ static void sha256_transform(SHA256_INFO *sha256_info)
A = TRUNC32(T1 + T2);
}
sha256_info->digest[0] = TRUNC32(sha256_info->digest[0] + A);
sha256_info->digest[1] = TRUNC32(sha256_info->digest[1] + B);
sha256_info->digest[2] = TRUNC32(sha256_info->digest[2] + C);
sha256_info->digest[3] = TRUNC32(sha256_info->digest[3] + D);
sha256_info->digest[4] = TRUNC32(sha256_info->digest[4] + E);
sha256_info->digest[5] = TRUNC32(sha256_info->digest[5] + F);
sha256_info->digest[6] = TRUNC32(sha256_info->digest[6] + G);
sha256_info->digest[7] = TRUNC32(sha256_info->digest[7] + H);
sha_info->digest[0] = TRUNC32(sha_info->digest[0] + A);
sha_info->digest[1] = TRUNC32(sha_info->digest[1] + B);
sha_info->digest[2] = TRUNC32(sha_info->digest[2] + C);
sha_info->digest[3] = TRUNC32(sha_info->digest[3] + D);
sha_info->digest[4] = TRUNC32(sha_info->digest[4] + E);
sha_info->digest[5] = TRUNC32(sha_info->digest[5] + F);
sha_info->digest[6] = TRUNC32(sha_info->digest[6] + G);
sha_info->digest[7] = TRUNC32(sha_info->digest[7] + H);
}
void sha256_init(SHA256_INFO *sha256_info)
void sha256_init(SHA_INFO *sha_info)
{
sha256_info->digest[0] = 0x6a09e667L;
sha256_info->digest[1] = 0xbb67ae85L;
sha256_info->digest[2] = 0x3c6ef372L;
sha256_info->digest[3] = 0xa54ff53aL;
sha256_info->digest[4] = 0x510e527fL;
sha256_info->digest[5] = 0x9b05688cL;
sha256_info->digest[6] = 0x1f83d9abL;
sha256_info->digest[7] = 0x5be0cd19L;
sha256_info->count_lo = 0L;
sha256_info->count_hi = 0L;
sha256_info->local = 0;
memset((uint8 *)sha256_info->data, 0, SHA_BLOCKSIZE);
sha_info->digest[0] = 0x6a09e667L;
sha_info->digest[1] = 0xbb67ae85L;
sha_info->digest[2] = 0x3c6ef372L;
sha_info->digest[3] = 0xa54ff53aL;
sha_info->digest[4] = 0x510e527fL;
sha_info->digest[5] = 0x9b05688cL;
sha_info->digest[6] = 0x1f83d9abL;
sha_info->digest[7] = 0x5be0cd19L;
sha_info->count_lo = 0L;
sha_info->count_hi = 0L;
sha_info->local = 0;
memset((uint8 *)sha_info->data, 0, SHA_BLOCKSIZE);
}
/* Update the SHA digest
*/
void sha256_update(SHA256_INFO *sha256_info, uint8 *buffer, int count)
void sha256_update(SHA_INFO *sha_info, uint8 *buffer, int count)
{
int i;
uint32 clo;
clo = TRUNC32(sha256_info->count_lo + ((uint8) count << 3));
if (clo < sha256_info->count_lo) {
sha256_info->count_hi++;
clo = TRUNC32(sha_info->count_lo + ((uint8) count << 3));
if (clo < sha_info->count_lo) {
sha_info->count_hi++;
}
sha256_info->count_lo = clo;
sha256_info->count_hi += (uint8) count >> 29;
if (sha256_info->local) {
i = SHA_BLOCKSIZE - sha256_info->local;
sha_info->count_lo = clo;
sha_info->count_hi += (uint8) count >> 29;
if (sha_info->local) {
i = SHA_BLOCKSIZE - sha_info->local;
if (i > count) {
i = count;
}
memcpy(((uint8 *) sha256_info->data) + sha256_info->local, buffer, i);
memcpy(((uint8 *) sha_info->data) + sha_info->local, buffer, i);
count -= i;
buffer += i;
sha256_info->local += i;
if (sha256_info->local == SHA_BLOCKSIZE) {
sha256_transform(sha256_info);
sha_info->local += i;
if (sha_info->local == SHA_BLOCKSIZE) {
sha256_transform(sha_info);
} else {
return;
}
}
while (count >= SHA_BLOCKSIZE) {
memcpy(sha256_info->data, buffer, SHA_BLOCKSIZE);
memcpy(sha_info->data, buffer, SHA_BLOCKSIZE);
buffer += SHA_BLOCKSIZE;
count -= SHA_BLOCKSIZE;
sha256_transform(sha256_info);
sha256_transform(sha_info);
}
memcpy(sha256_info->data, buffer, count);
sha256_info->local = count;
memcpy(sha_info->data, buffer, count);
sha_info->local = count;
}
/* Finish computing the SHA digest
*/
void sha256_final(SHA256_INFO *sha256_info)
void sha256_final(SHA_INFO *sha_info)
{
int count;
uint32 lo_bit_count, hi_bit_count;
lo_bit_count = sha256_info->count_lo;
hi_bit_count = sha256_info->count_hi;
lo_bit_count = sha_info->count_lo;
hi_bit_count = sha_info->count_hi;
count = (int) ((lo_bit_count >> 3) & 0x3f);
((uint8 *) sha256_info->data)[count++] = 0x80;
((uint8 *) sha_info->data)[count++] = 0x80;
if (count > SHA_BLOCKSIZE - 8) {
memset(((uint8 *) sha256_info->data) + count, 0, SHA_BLOCKSIZE - count);
sha256_transform(sha256_info);
memset((uint8 *) sha256_info->data, 0, SHA_BLOCKSIZE - 8);
memset(((uint8 *) sha_info->data) + count, 0, SHA_BLOCKSIZE - count);
sha256_transform(sha_info);
memset((uint8 *) sha_info->data, 0, SHA_BLOCKSIZE - 8);
} else {
memset(((uint8 *) sha256_info->data) + count, 0,
memset(((uint8 *) sha_info->data) + count, 0,
SHA_BLOCKSIZE - 8 - count);
}
sha256_info->data[56] = (hi_bit_count >> 24) & 0xff;
sha256_info->data[57] = (hi_bit_count >> 16) & 0xff;
sha256_info->data[58] = (hi_bit_count >> 8) & 0xff;
sha256_info->data[59] = (hi_bit_count >> 0) & 0xff;
sha256_info->data[60] = (lo_bit_count >> 24) & 0xff;
sha256_info->data[61] = (lo_bit_count >> 16) & 0xff;
sha256_info->data[62] = (lo_bit_count >> 8) & 0xff;
sha256_info->data[63] = (lo_bit_count >> 0) & 0xff;
sha256_transform(sha256_info);
sha_info->data[56] = (hi_bit_count >> 24) & 0xff;
sha_info->data[57] = (hi_bit_count >> 16) & 0xff;
sha_info->data[58] = (hi_bit_count >> 8) & 0xff;
sha_info->data[59] = (hi_bit_count >> 0) & 0xff;
sha_info->data[60] = (lo_bit_count >> 24) & 0xff;
sha_info->data[61] = (lo_bit_count >> 16) & 0xff;
sha_info->data[62] = (lo_bit_count >> 8) & 0xff;
sha_info->data[63] = (lo_bit_count >> 0) & 0xff;
sha256_transform(sha_info);
}
void sha256_unpackdigest(uint8 digest[32], SHA256_INFO *sha256_info)
void sha256_unpackdigest(uint8 digest[32], SHA_INFO *sha_info)
{
digest[ 0] = (unsigned char) ((sha256_info->digest[0] >> 24) & 0xff);
digest[ 1] = (unsigned char) ((sha256_info->digest[0] >> 16) & 0xff);
digest[ 2] = (unsigned char) ((sha256_info->digest[0] >> 8) & 0xff);
digest[ 3] = (unsigned char) ((sha256_info->digest[0] ) & 0xff);
digest[ 4] = (unsigned char) ((sha256_info->digest[1] >> 24) & 0xff);
digest[ 5] = (unsigned char) ((sha256_info->digest[1] >> 16) & 0xff);
digest[ 6] = (unsigned char) ((sha256_info->digest[1] >> 8) & 0xff);
digest[ 7] = (unsigned char) ((sha256_info->digest[1] ) & 0xff);
digest[ 8] = (unsigned char) ((sha256_info->digest[2] >> 24) & 0xff);
digest[ 9] = (unsigned char) ((sha256_info->digest[2] >> 16) & 0xff);
digest[10] = (unsigned char) ((sha256_info->digest[2] >> 8) & 0xff);
digest[11] = (unsigned char) ((sha256_info->digest[2] ) & 0xff);
digest[12] = (unsigned char) ((sha256_info->digest[3] >> 24) & 0xff);
digest[13] = (unsigned char) ((sha256_info->digest[3] >> 16) & 0xff);
digest[14] = (unsigned char) ((sha256_info->digest[3] >> 8) & 0xff);
digest[15] = (unsigned char) ((sha256_info->digest[3] ) & 0xff);
digest[16] = (unsigned char) ((sha256_info->digest[4] >> 24) & 0xff);
digest[17] = (unsigned char) ((sha256_info->digest[4] >> 16) & 0xff);
digest[18] = (unsigned char) ((sha256_info->digest[4] >> 8) & 0xff);
digest[19] = (unsigned char) ((sha256_info->digest[4] ) & 0xff);
digest[20] = (unsigned char) ((sha256_info->digest[5] >> 24) & 0xff);
digest[21] = (unsigned char) ((sha256_info->digest[5] >> 16) & 0xff);
digest[22] = (unsigned char) ((sha256_info->digest[5] >> 8) & 0xff);
digest[23] = (unsigned char) ((sha256_info->digest[5] ) & 0xff);
digest[24] = (unsigned char) ((sha256_info->digest[6] >> 24) & 0xff);
digest[25] = (unsigned char) ((sha256_info->digest[6] >> 16) & 0xff);
digest[26] = (unsigned char) ((sha256_info->digest[6] >> 8) & 0xff);
digest[27] = (unsigned char) ((sha256_info->digest[6] ) & 0xff);
digest[28] = (unsigned char) ((sha256_info->digest[7] >> 24) & 0xff);
digest[29] = (unsigned char) ((sha256_info->digest[7] >> 16) & 0xff);
digest[30] = (unsigned char) ((sha256_info->digest[7] >> 8) & 0xff);
digest[31] = (unsigned char) ((sha256_info->digest[7] ) & 0xff);
digest[ 0] = (unsigned char) ((sha_info->digest[0] >> 24) & 0xff);
digest[ 1] = (unsigned char) ((sha_info->digest[0] >> 16) & 0xff);
digest[ 2] = (unsigned char) ((sha_info->digest[0] >> 8) & 0xff);
digest[ 3] = (unsigned char) ((sha_info->digest[0] ) & 0xff);
digest[ 4] = (unsigned char) ((sha_info->digest[1] >> 24) & 0xff);
digest[ 5] = (unsigned char) ((sha_info->digest[1] >> 16) & 0xff);
digest[ 6] = (unsigned char) ((sha_info->digest[1] >> 8) & 0xff);
digest[ 7] = (unsigned char) ((sha_info->digest[1] ) & 0xff);
digest[ 8] = (unsigned char) ((sha_info->digest[2] >> 24) & 0xff);
digest[ 9] = (unsigned char) ((sha_info->digest[2] >> 16) & 0xff);
digest[10] = (unsigned char) ((sha_info->digest[2] >> 8) & 0xff);
digest[11] = (unsigned char) ((sha_info->digest[2] ) & 0xff);
digest[12] = (unsigned char) ((sha_info->digest[3] >> 24) & 0xff);
digest[13] = (unsigned char) ((sha_info->digest[3] >> 16) & 0xff);
digest[14] = (unsigned char) ((sha_info->digest[3] >> 8) & 0xff);
digest[15] = (unsigned char) ((sha_info->digest[3] ) & 0xff);
digest[16] = (unsigned char) ((sha_info->digest[4] >> 24) & 0xff);
digest[17] = (unsigned char) ((sha_info->digest[4] >> 16) & 0xff);
digest[18] = (unsigned char) ((sha_info->digest[4] >> 8) & 0xff);
digest[19] = (unsigned char) ((sha_info->digest[4] ) & 0xff);
digest[20] = (unsigned char) ((sha_info->digest[5] >> 24) & 0xff);
digest[21] = (unsigned char) ((sha_info->digest[5] >> 16) & 0xff);
digest[22] = (unsigned char) ((sha_info->digest[5] >> 8) & 0xff);
digest[23] = (unsigned char) ((sha_info->digest[5] ) & 0xff);
digest[24] = (unsigned char) ((sha_info->digest[6] >> 24) & 0xff);
digest[25] = (unsigned char) ((sha_info->digest[6] >> 16) & 0xff);
digest[26] = (unsigned char) ((sha_info->digest[6] >> 8) & 0xff);
digest[27] = (unsigned char) ((sha_info->digest[6] ) & 0xff);
digest[28] = (unsigned char) ((sha_info->digest[7] >> 24) & 0xff);
digest[29] = (unsigned char) ((sha_info->digest[7] >> 16) & 0xff);
digest[30] = (unsigned char) ((sha_info->digest[7] >> 8) & 0xff);
digest[31] = (unsigned char) ((sha_info->digest[7] ) & 0xff);
}
#define BLOCK_SIZE 8192
/*
void sha256_print(SHA256_INFO *sha256_info)
{
printf("%08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
sha256_info->digest[0], sha256_info->digest[1],
sha256_info->digest[2], sha256_info->digest[3],
sha256_info->digest[4], sha256_info->digest[5],
sha256_info->digest[6], sha256_info->digest[7]
);
}
*/
/***EOF***/

42
types.h Normal file
View File

@ -0,0 +1,42 @@
/* $Id$
*****************************************************************************
*
* File: types.h
*
* Purpose: Typedefs for fwknop.
*
* Copyright (C) 2008 Damien Stuart (dstuart@dstuart.org)
*
* License (GNU Public License):
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*
*****************************************************************************
*/
#ifndef _TYPES_H_
#define _TYPES_H_
#include <sys/types.h>
typedef u_int8_t uint8;
typedef u_int16_t uint16;
typedef u_int32_t uint32;
typedef u_int64_t uint64;
typedef int8_t int8;
typedef int16_t int16;
typedef int32_t int32;
typedef int64_t int64;
#endif /* _TYPES_H_ */
/***EOF***/