Initial Commit
This commit is contained in:
175
database/FileZillaFTP/source/hash_algorithms/int64.c
Normal file
175
database/FileZillaFTP/source/hash_algorithms/int64.c
Normal file
@@ -0,0 +1,175 @@
|
||||
/*
|
||||
* Handling of the int64 and uint64 types. Done in 32-bit integers,
|
||||
* for (pre-C99) portability. Hopefully once C99 becomes widespread
|
||||
* we can kiss this lot goodbye...
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "int64.h"
|
||||
|
||||
uint64 uint64_div10(uint64 x, int *remainder)
|
||||
{
|
||||
uint64 y;
|
||||
unsigned int rem, r2;
|
||||
y.hi = x.hi / 10;
|
||||
y.lo = x.lo / 10;
|
||||
rem = x.lo % 10;
|
||||
/*
|
||||
* Now we have to add in the remainder left over from x.hi.
|
||||
*/
|
||||
r2 = x.hi % 10;
|
||||
y.lo += r2 * 429496729;
|
||||
rem += r2 * 6;
|
||||
y.lo += rem / 10;
|
||||
rem %= 10;
|
||||
|
||||
if (remainder)
|
||||
*remainder = rem;
|
||||
return y;
|
||||
}
|
||||
|
||||
void uint64_decimal(uint64 x, char *buffer)
|
||||
{
|
||||
char buf[20];
|
||||
int start = 20;
|
||||
int d;
|
||||
|
||||
do {
|
||||
x = uint64_div10(x, &d);
|
||||
assert(start > 0);
|
||||
buf[--start] = d + '0';
|
||||
} while (x.hi || x.lo);
|
||||
|
||||
memcpy(buffer, buf + start, sizeof(buf) - start);
|
||||
buffer[sizeof(buf) - start] = '\0';
|
||||
}
|
||||
|
||||
uint64 uint64_make(unsigned long hi, unsigned long lo)
|
||||
{
|
||||
uint64 y;
|
||||
y.hi = hi & 0xFFFFFFFFU;
|
||||
y.lo = lo & 0xFFFFFFFFU;
|
||||
return y;
|
||||
}
|
||||
|
||||
uint64 uint64_add(uint64 x, uint64 y)
|
||||
{
|
||||
x.lo = (x.lo + y.lo) & 0xFFFFFFFFU;
|
||||
x.hi += y.hi + (x.lo < y.lo ? 1 : 0);
|
||||
return x;
|
||||
}
|
||||
|
||||
uint64 uint64_add32(uint64 x, unsigned long y)
|
||||
{
|
||||
uint64 yy;
|
||||
yy.hi = 0;
|
||||
yy.lo = y;
|
||||
return uint64_add(x, yy);
|
||||
}
|
||||
|
||||
int uint64_compare(uint64 x, uint64 y)
|
||||
{
|
||||
if (x.hi != y.hi)
|
||||
return x.hi < y.hi ? -1 : +1;
|
||||
if (x.lo != y.lo)
|
||||
return x.lo < y.lo ? -1 : +1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint64 uint64_subtract(uint64 x, uint64 y)
|
||||
{
|
||||
x.lo = (x.lo - y.lo) & 0xFFFFFFFFU;
|
||||
x.hi = (x.hi - y.hi - (x.lo > (y.lo ^ 0xFFFFFFFFU) ? 1 : 0)) & 0xFFFFFFFFU;
|
||||
return x;
|
||||
}
|
||||
|
||||
double uint64_to_double(uint64 x)
|
||||
{
|
||||
return (4294967296.0 * x.hi) + (double)x.lo;
|
||||
}
|
||||
|
||||
uint64 uint64_shift_right(uint64 x, int shift)
|
||||
{
|
||||
if (shift < 32) {
|
||||
x.lo >>= shift;
|
||||
x.lo |= (x.hi << (32-shift)) & 0xFFFFFFFFU;
|
||||
x.hi >>= shift;
|
||||
} else {
|
||||
x.lo = x.hi >> (shift-32);
|
||||
x.hi = 0;
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
uint64 uint64_shift_left(uint64 x, int shift)
|
||||
{
|
||||
if (shift < 32) {
|
||||
x.hi = (x.hi << shift) & 0xFFFFFFFFU;
|
||||
x.hi |= (x.lo >> (32-shift));
|
||||
x.lo = (x.lo << shift) & 0xFFFFFFFFU;
|
||||
} else {
|
||||
x.hi = (x.lo << (shift-32)) & 0xFFFFFFFFU;
|
||||
x.lo = 0;
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
uint64 uint64_from_decimal(char *str)
|
||||
{
|
||||
uint64 ret;
|
||||
ret.hi = ret.lo = 0;
|
||||
while (*str >= '0' && *str <= '9') {
|
||||
ret = uint64_add(uint64_shift_left(ret, 3),
|
||||
uint64_shift_left(ret, 1));
|
||||
ret = uint64_add32(ret, *str - '0');
|
||||
str++;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
#ifdef TESTMODE
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
uint64 x, y, z;
|
||||
char buf[80];
|
||||
|
||||
x = uint64_make(0x3456789AUL, 0xDEF01234UL);
|
||||
printf("%08lx.%08lx\n", x.hi, x.lo);
|
||||
uint64_decimal(x, buf);
|
||||
printf("%s\n", buf);
|
||||
|
||||
y = uint64_add32(x, 0xFFFFFFFFU);
|
||||
printf("%08lx.%08lx\n", y.hi, y.lo);
|
||||
uint64_decimal(y, buf);
|
||||
printf("%s\n", buf);
|
||||
|
||||
z = uint64_subtract(y, x);
|
||||
printf("%08lx.%08lx\n", z.hi, z.lo);
|
||||
uint64_decimal(z, buf);
|
||||
printf("%s\n", buf);
|
||||
|
||||
z = uint64_subtract(x, y);
|
||||
printf("%08lx.%08lx\n", z.hi, z.lo);
|
||||
uint64_decimal(z, buf);
|
||||
printf("%s\n", buf);
|
||||
|
||||
y = uint64_shift_right(x, 4);
|
||||
printf("%08lx.%08lx\n", y.hi, y.lo);
|
||||
|
||||
y = uint64_shift_right(x, 36);
|
||||
printf("%08lx.%08lx\n", y.hi, y.lo);
|
||||
|
||||
y = uint64_shift_left(x, 4);
|
||||
printf("%08lx.%08lx\n", x.hi, x.lo);
|
||||
|
||||
y = uint64_shift_left(x, 36);
|
||||
printf("%08lx.%08lx\n", x.hi, x.lo);
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
24
database/FileZillaFTP/source/hash_algorithms/int64.h
Normal file
24
database/FileZillaFTP/source/hash_algorithms/int64.h
Normal file
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Header for int64.c.
|
||||
*/
|
||||
|
||||
#ifndef PUTTY_INT64_H
|
||||
#define PUTTY_INT64_H
|
||||
|
||||
typedef struct {
|
||||
unsigned long hi, lo;
|
||||
} uint64;
|
||||
|
||||
uint64 uint64_div10(uint64 x, int *remainder);
|
||||
void uint64_decimal(uint64 x, char *buffer);
|
||||
uint64 uint64_make(unsigned long hi, unsigned long lo);
|
||||
uint64 uint64_add(uint64 x, uint64 y);
|
||||
uint64 uint64_add32(uint64 x, unsigned long y);
|
||||
int uint64_compare(uint64 x, uint64 y);
|
||||
uint64 uint64_subtract(uint64 x, uint64 y);
|
||||
double uint64_to_double(uint64 x);
|
||||
uint64 uint64_shift_right(uint64 x, int shift);
|
||||
uint64 uint64_shift_left(uint64 x, int shift);
|
||||
uint64 uint64_from_decimal(char *str);
|
||||
|
||||
#endif
|
||||
344
database/FileZillaFTP/source/hash_algorithms/sshmd5.c
Normal file
344
database/FileZillaFTP/source/hash_algorithms/sshmd5.c
Normal file
@@ -0,0 +1,344 @@
|
||||
#include "ssh.h"
|
||||
|
||||
/*
|
||||
* MD5 implementation for PuTTY. Written directly from the spec by
|
||||
* Simon Tatham.
|
||||
*/
|
||||
|
||||
/* ----------------------------------------------------------------------
|
||||
* Core MD5 algorithm: processes 16-word blocks into a message digest.
|
||||
*/
|
||||
|
||||
#define F(x,y,z) ( ((x) & (y)) | ((~(x)) & (z)) )
|
||||
#define G(x,y,z) ( ((x) & (z)) | ((~(z)) & (y)) )
|
||||
#define H(x,y,z) ( (x) ^ (y) ^ (z) )
|
||||
#define I(x,y,z) ( (y) ^ ( (x) | ~(z) ) )
|
||||
|
||||
#define rol(x,y) ( ((x) << (y)) | (((uint32)x) >> (32-y)) )
|
||||
|
||||
#define subround(f,w,x,y,z,k,s,ti) \
|
||||
w = x + rol(w + f(x,y,z) + block[k] + ti, s)
|
||||
|
||||
static void MD5_Core_Init(MD5_Core_State * s)
|
||||
{
|
||||
s->h[0] = 0x67452301;
|
||||
s->h[1] = 0xefcdab89;
|
||||
s->h[2] = 0x98badcfe;
|
||||
s->h[3] = 0x10325476;
|
||||
}
|
||||
|
||||
static void MD5_Block(MD5_Core_State * s, uint32 * block)
|
||||
{
|
||||
uint32 a, b, c, d;
|
||||
|
||||
a = s->h[0];
|
||||
b = s->h[1];
|
||||
c = s->h[2];
|
||||
d = s->h[3];
|
||||
|
||||
subround(F, a, b, c, d, 0, 7, 0xd76aa478);
|
||||
subround(F, d, a, b, c, 1, 12, 0xe8c7b756);
|
||||
subround(F, c, d, a, b, 2, 17, 0x242070db);
|
||||
subround(F, b, c, d, a, 3, 22, 0xc1bdceee);
|
||||
subround(F, a, b, c, d, 4, 7, 0xf57c0faf);
|
||||
subround(F, d, a, b, c, 5, 12, 0x4787c62a);
|
||||
subround(F, c, d, a, b, 6, 17, 0xa8304613);
|
||||
subround(F, b, c, d, a, 7, 22, 0xfd469501);
|
||||
subround(F, a, b, c, d, 8, 7, 0x698098d8);
|
||||
subround(F, d, a, b, c, 9, 12, 0x8b44f7af);
|
||||
subround(F, c, d, a, b, 10, 17, 0xffff5bb1);
|
||||
subround(F, b, c, d, a, 11, 22, 0x895cd7be);
|
||||
subround(F, a, b, c, d, 12, 7, 0x6b901122);
|
||||
subround(F, d, a, b, c, 13, 12, 0xfd987193);
|
||||
subround(F, c, d, a, b, 14, 17, 0xa679438e);
|
||||
subround(F, b, c, d, a, 15, 22, 0x49b40821);
|
||||
subround(G, a, b, c, d, 1, 5, 0xf61e2562);
|
||||
subround(G, d, a, b, c, 6, 9, 0xc040b340);
|
||||
subround(G, c, d, a, b, 11, 14, 0x265e5a51);
|
||||
subround(G, b, c, d, a, 0, 20, 0xe9b6c7aa);
|
||||
subround(G, a, b, c, d, 5, 5, 0xd62f105d);
|
||||
subround(G, d, a, b, c, 10, 9, 0x02441453);
|
||||
subround(G, c, d, a, b, 15, 14, 0xd8a1e681);
|
||||
subround(G, b, c, d, a, 4, 20, 0xe7d3fbc8);
|
||||
subround(G, a, b, c, d, 9, 5, 0x21e1cde6);
|
||||
subround(G, d, a, b, c, 14, 9, 0xc33707d6);
|
||||
subround(G, c, d, a, b, 3, 14, 0xf4d50d87);
|
||||
subround(G, b, c, d, a, 8, 20, 0x455a14ed);
|
||||
subround(G, a, b, c, d, 13, 5, 0xa9e3e905);
|
||||
subround(G, d, a, b, c, 2, 9, 0xfcefa3f8);
|
||||
subround(G, c, d, a, b, 7, 14, 0x676f02d9);
|
||||
subround(G, b, c, d, a, 12, 20, 0x8d2a4c8a);
|
||||
subround(H, a, b, c, d, 5, 4, 0xfffa3942);
|
||||
subround(H, d, a, b, c, 8, 11, 0x8771f681);
|
||||
subround(H, c, d, a, b, 11, 16, 0x6d9d6122);
|
||||
subround(H, b, c, d, a, 14, 23, 0xfde5380c);
|
||||
subround(H, a, b, c, d, 1, 4, 0xa4beea44);
|
||||
subround(H, d, a, b, c, 4, 11, 0x4bdecfa9);
|
||||
subround(H, c, d, a, b, 7, 16, 0xf6bb4b60);
|
||||
subround(H, b, c, d, a, 10, 23, 0xbebfbc70);
|
||||
subround(H, a, b, c, d, 13, 4, 0x289b7ec6);
|
||||
subround(H, d, a, b, c, 0, 11, 0xeaa127fa);
|
||||
subround(H, c, d, a, b, 3, 16, 0xd4ef3085);
|
||||
subround(H, b, c, d, a, 6, 23, 0x04881d05);
|
||||
subround(H, a, b, c, d, 9, 4, 0xd9d4d039);
|
||||
subround(H, d, a, b, c, 12, 11, 0xe6db99e5);
|
||||
subround(H, c, d, a, b, 15, 16, 0x1fa27cf8);
|
||||
subround(H, b, c, d, a, 2, 23, 0xc4ac5665);
|
||||
subround(I, a, b, c, d, 0, 6, 0xf4292244);
|
||||
subround(I, d, a, b, c, 7, 10, 0x432aff97);
|
||||
subround(I, c, d, a, b, 14, 15, 0xab9423a7);
|
||||
subround(I, b, c, d, a, 5, 21, 0xfc93a039);
|
||||
subround(I, a, b, c, d, 12, 6, 0x655b59c3);
|
||||
subround(I, d, a, b, c, 3, 10, 0x8f0ccc92);
|
||||
subround(I, c, d, a, b, 10, 15, 0xffeff47d);
|
||||
subround(I, b, c, d, a, 1, 21, 0x85845dd1);
|
||||
subround(I, a, b, c, d, 8, 6, 0x6fa87e4f);
|
||||
subround(I, d, a, b, c, 15, 10, 0xfe2ce6e0);
|
||||
subround(I, c, d, a, b, 6, 15, 0xa3014314);
|
||||
subround(I, b, c, d, a, 13, 21, 0x4e0811a1);
|
||||
subround(I, a, b, c, d, 4, 6, 0xf7537e82);
|
||||
subround(I, d, a, b, c, 11, 10, 0xbd3af235);
|
||||
subround(I, c, d, a, b, 2, 15, 0x2ad7d2bb);
|
||||
subround(I, b, c, d, a, 9, 21, 0xeb86d391);
|
||||
|
||||
s->h[0] += a;
|
||||
s->h[1] += b;
|
||||
s->h[2] += c;
|
||||
s->h[3] += d;
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------
|
||||
* Outer MD5 algorithm: take an arbitrary length byte string,
|
||||
* convert it into 16-word blocks with the prescribed padding at
|
||||
* the end, and pass those blocks to the core MD5 algorithm.
|
||||
*/
|
||||
|
||||
#define BLKSIZE 64
|
||||
|
||||
void MD5Init(struct MD5Context *s)
|
||||
{
|
||||
MD5_Core_Init(&s->core);
|
||||
s->blkused = 0;
|
||||
s->lenhi = s->lenlo = 0;
|
||||
}
|
||||
|
||||
void MD5Update(struct MD5Context *s, unsigned char const *p, unsigned len)
|
||||
{
|
||||
unsigned char *q = (unsigned char *) p;
|
||||
uint32 wordblock[16];
|
||||
uint32 lenw = len;
|
||||
int i;
|
||||
|
||||
/*
|
||||
* Update the length field.
|
||||
*/
|
||||
s->lenlo += lenw;
|
||||
s->lenhi += (s->lenlo < lenw);
|
||||
|
||||
if (s->blkused + len < BLKSIZE) {
|
||||
/*
|
||||
* Trivial case: just add to the block.
|
||||
*/
|
||||
memcpy(s->block + s->blkused, q, len);
|
||||
s->blkused += len;
|
||||
} else {
|
||||
/*
|
||||
* We must complete and process at least one block.
|
||||
*/
|
||||
while (s->blkused + len >= BLKSIZE) {
|
||||
memcpy(s->block + s->blkused, q, BLKSIZE - s->blkused);
|
||||
q += BLKSIZE - s->blkused;
|
||||
len -= BLKSIZE - s->blkused;
|
||||
/* Now process the block. Gather bytes little-endian into words */
|
||||
for (i = 0; i < 16; i++) {
|
||||
wordblock[i] =
|
||||
(((uint32) s->block[i * 4 + 3]) << 24) |
|
||||
(((uint32) s->block[i * 4 + 2]) << 16) |
|
||||
(((uint32) s->block[i * 4 + 1]) << 8) |
|
||||
(((uint32) s->block[i * 4 + 0]) << 0);
|
||||
}
|
||||
MD5_Block(&s->core, wordblock);
|
||||
s->blkused = 0;
|
||||
}
|
||||
memcpy(s->block, q, len);
|
||||
s->blkused = len;
|
||||
}
|
||||
}
|
||||
|
||||
void MD5Final(unsigned char output[16], struct MD5Context *s)
|
||||
{
|
||||
int i;
|
||||
unsigned pad;
|
||||
unsigned char c[64];
|
||||
uint32 lenhi, lenlo;
|
||||
|
||||
if (s->blkused >= 56)
|
||||
pad = 56 + 64 - s->blkused;
|
||||
else
|
||||
pad = 56 - s->blkused;
|
||||
|
||||
lenhi = (s->lenhi << 3) | (s->lenlo >> (32 - 3));
|
||||
lenlo = (s->lenlo << 3);
|
||||
|
||||
memset(c, 0, pad);
|
||||
c[0] = 0x80;
|
||||
MD5Update(s, c, pad);
|
||||
|
||||
c[7] = (lenhi >> 24) & 0xFF;
|
||||
c[6] = (lenhi >> 16) & 0xFF;
|
||||
c[5] = (lenhi >> 8) & 0xFF;
|
||||
c[4] = (lenhi >> 0) & 0xFF;
|
||||
c[3] = (lenlo >> 24) & 0xFF;
|
||||
c[2] = (lenlo >> 16) & 0xFF;
|
||||
c[1] = (lenlo >> 8) & 0xFF;
|
||||
c[0] = (lenlo >> 0) & 0xFF;
|
||||
|
||||
MD5Update(s, c, 8);
|
||||
|
||||
for (i = 0; i < 4; i++) {
|
||||
output[4 * i + 3] = (s->core.h[i] >> 24) & 0xFF;
|
||||
output[4 * i + 2] = (s->core.h[i] >> 16) & 0xFF;
|
||||
output[4 * i + 1] = (s->core.h[i] >> 8) & 0xFF;
|
||||
output[4 * i + 0] = (s->core.h[i] >> 0) & 0xFF;
|
||||
}
|
||||
}
|
||||
|
||||
void MD5Simple(void const *p, unsigned len, unsigned char output[16])
|
||||
{
|
||||
struct MD5Context s;
|
||||
|
||||
MD5Init(&s);
|
||||
MD5Update(&s, (unsigned char const *)p, len);
|
||||
MD5Final(output, &s);
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------
|
||||
* The above is the MD5 algorithm itself. Now we implement the
|
||||
* HMAC wrapper on it.
|
||||
*
|
||||
* Some of these functions are exported directly, because they are
|
||||
* useful elsewhere (SOCKS5 CHAP authentication uses HMAC-MD5).
|
||||
*/
|
||||
|
||||
void *hmacmd5_make_context(void)
|
||||
{
|
||||
return snewn(3, struct MD5Context);
|
||||
}
|
||||
|
||||
void hmacmd5_free_context(void *handle)
|
||||
{
|
||||
sfree(handle);
|
||||
}
|
||||
|
||||
void hmacmd5_key(void *handle, void const *keyv, int len)
|
||||
{
|
||||
struct MD5Context *keys = (struct MD5Context *)handle;
|
||||
unsigned char foo[64];
|
||||
unsigned char const *key = (unsigned char const *)keyv;
|
||||
int i;
|
||||
|
||||
memset(foo, 0x36, 64);
|
||||
for (i = 0; i < len && i < 64; i++)
|
||||
foo[i] ^= key[i];
|
||||
MD5Init(&keys[0]);
|
||||
MD5Update(&keys[0], foo, 64);
|
||||
|
||||
memset(foo, 0x5C, 64);
|
||||
for (i = 0; i < len && i < 64; i++)
|
||||
foo[i] ^= key[i];
|
||||
MD5Init(&keys[1]);
|
||||
MD5Update(&keys[1], foo, 64);
|
||||
|
||||
memset(foo, 0, 64); /* burn the evidence */
|
||||
}
|
||||
|
||||
static void hmacmd5_key_16(void *handle, unsigned char *key)
|
||||
{
|
||||
hmacmd5_key(handle, key, 16);
|
||||
}
|
||||
|
||||
static void hmacmd5_start(void *handle)
|
||||
{
|
||||
struct MD5Context *keys = (struct MD5Context *)handle;
|
||||
|
||||
keys[2] = keys[0]; /* structure copy */
|
||||
}
|
||||
|
||||
static void hmacmd5_bytes(void *handle, unsigned char const *blk, int len)
|
||||
{
|
||||
struct MD5Context *keys = (struct MD5Context *)handle;
|
||||
MD5Update(&keys[2], blk, len);
|
||||
}
|
||||
|
||||
static void hmacmd5_genresult(void *handle, unsigned char *hmac)
|
||||
{
|
||||
struct MD5Context *keys = (struct MD5Context *)handle;
|
||||
struct MD5Context s;
|
||||
unsigned char intermediate[16];
|
||||
|
||||
s = keys[2]; /* structure copy */
|
||||
MD5Final(intermediate, &s);
|
||||
s = keys[1]; /* structure copy */
|
||||
MD5Update(&s, intermediate, 16);
|
||||
MD5Final(hmac, &s);
|
||||
}
|
||||
|
||||
static int hmacmd5_verresult(void *handle, unsigned char const *hmac)
|
||||
{
|
||||
unsigned char correct[16];
|
||||
hmacmd5_genresult(handle, correct);
|
||||
return !memcmp(correct, hmac, 16);
|
||||
}
|
||||
|
||||
static void hmacmd5_do_hmac_internal(void *handle,
|
||||
unsigned char const *blk, int len,
|
||||
unsigned char const *blk2, int len2,
|
||||
unsigned char *hmac)
|
||||
{
|
||||
hmacmd5_start(handle);
|
||||
hmacmd5_bytes(handle, blk, len);
|
||||
if (blk2) hmacmd5_bytes(handle, blk2, len2);
|
||||
hmacmd5_genresult(handle, hmac);
|
||||
}
|
||||
|
||||
void hmacmd5_do_hmac(void *handle, unsigned char const *blk, int len,
|
||||
unsigned char *hmac)
|
||||
{
|
||||
hmacmd5_do_hmac_internal(handle, blk, len, NULL, 0, hmac);
|
||||
}
|
||||
|
||||
static void hmacmd5_do_hmac_ssh(void *handle, unsigned char const *blk, int len,
|
||||
unsigned long seq, unsigned char *hmac)
|
||||
{
|
||||
unsigned char seqbuf[16];
|
||||
|
||||
seqbuf[0] = (unsigned char) ((seq >> 24) & 0xFF);
|
||||
seqbuf[1] = (unsigned char) ((seq >> 16) & 0xFF);
|
||||
seqbuf[2] = (unsigned char) ((seq >> 8) & 0xFF);
|
||||
seqbuf[3] = (unsigned char) ((seq) & 0xFF);
|
||||
|
||||
hmacmd5_do_hmac_internal(handle, seqbuf, 4, blk, len, hmac);
|
||||
}
|
||||
|
||||
static void hmacmd5_generate(void *handle, unsigned char *blk, int len,
|
||||
unsigned long seq)
|
||||
{
|
||||
hmacmd5_do_hmac_ssh(handle, blk, len, seq, blk + len);
|
||||
}
|
||||
|
||||
static int hmacmd5_verify(void *handle, unsigned char *blk, int len,
|
||||
unsigned long seq)
|
||||
{
|
||||
unsigned char correct[16];
|
||||
hmacmd5_do_hmac_ssh(handle, blk, len, seq, correct);
|
||||
return !memcmp(correct, blk + len, 16);
|
||||
}
|
||||
|
||||
const struct ssh_mac ssh_hmac_md5 = {
|
||||
hmacmd5_make_context, hmacmd5_free_context, hmacmd5_key_16,
|
||||
hmacmd5_generate, hmacmd5_verify,
|
||||
hmacmd5_start, hmacmd5_bytes, hmacmd5_genresult, hmacmd5_verresult,
|
||||
"hmac-md5",
|
||||
16,
|
||||
"HMAC-MD5"
|
||||
};
|
||||
367
database/FileZillaFTP/source/hash_algorithms/sshsh512.c
Normal file
367
database/FileZillaFTP/source/hash_algorithms/sshsh512.c
Normal file
@@ -0,0 +1,367 @@
|
||||
/*
|
||||
* SHA-512 algorithm as described at
|
||||
*
|
||||
* http://csrc.nist.gov/cryptval/shs.html
|
||||
*/
|
||||
|
||||
#ifdef SHA512_STANDALONE
|
||||
typedef struct {
|
||||
uint64 h[8];
|
||||
unsigned char block[128];
|
||||
int blkused;
|
||||
uint32 len[4];
|
||||
} SHA512_State;
|
||||
#else
|
||||
#include "ssh.h"
|
||||
#endif
|
||||
|
||||
#define BLKSIZE 128
|
||||
|
||||
/*
|
||||
* Arithmetic implementations. Note that AND, XOR and NOT can
|
||||
* overlap destination with one source, but the others can't.
|
||||
*/
|
||||
#define add(r,x,y) ( r.lo = y.lo + x.lo, \
|
||||
r.hi = y.hi + x.hi + ((uint32)r.lo < (uint32)y.lo) )
|
||||
#define rorB(r,x,y) ( r.lo = ((uint32)x.hi >> ((y)-32)) | ((uint32)x.lo << (64-(y))), \
|
||||
r.hi = ((uint32)x.lo >> ((y)-32)) | ((uint32)x.hi << (64-(y))) )
|
||||
#define rorL(r,x,y) ( r.lo = ((uint32)x.lo >> (y)) | ((uint32)x.hi << (32-(y))), \
|
||||
r.hi = ((uint32)x.hi >> (y)) | ((uint32)x.lo << (32-(y))) )
|
||||
#define shrB(r,x,y) ( r.lo = (uint32)x.hi >> ((y)-32), r.hi = 0 )
|
||||
#define shrL(r,x,y) ( r.lo = ((uint32)x.lo >> (y)) | ((uint32)x.hi << (32-(y))), \
|
||||
r.hi = (uint32)x.hi >> (y) )
|
||||
#define putty_and(r,x,y) ( r.lo = x.lo & y.lo, r.hi = x.hi & y.hi )
|
||||
#define putty_xor(r,x,y) ( r.lo = x.lo ^ y.lo, r.hi = x.hi ^ y.hi )
|
||||
#define putty_not(r,x) ( r.lo = ~x.lo, r.hi = ~x.hi )
|
||||
#define INIT(h,l) { h, l }
|
||||
#define BUILD(r,h,l) ( r.hi = h, r.lo = l )
|
||||
#define EXTRACT(h,l,r) ( h = r.hi, l = r.lo )
|
||||
|
||||
/* ----------------------------------------------------------------------
|
||||
* Core SHA512 algorithm: processes 16-doubleword blocks into a
|
||||
* message digest.
|
||||
*/
|
||||
|
||||
#define Ch(r,t,x,y,z) ( putty_not(t,x), putty_and(r,t,z), putty_and(t,x,y), putty_xor(r,r,t) )
|
||||
#define Maj(r,t,x,y,z) ( putty_and(r,x,y), putty_and(t,x,z), putty_xor(r,r,t), \
|
||||
putty_and(t,y,z), putty_xor(r,r,t) )
|
||||
#define bigsigma0(r,t,x) ( rorL(r,x,28), rorB(t,x,34), putty_xor(r,r,t), \
|
||||
rorB(t,x,39), putty_xor(r,r,t) )
|
||||
#define bigsigma1(r,t,x) ( rorL(r,x,14), rorL(t,x,18), putty_xor(r,r,t), \
|
||||
rorB(t,x,41), putty_xor(r,r,t) )
|
||||
#define smallsigma0(r,t,x) ( rorL(r,x,1), rorL(t,x,8), putty_xor(r,r,t), \
|
||||
shrL(t,x,7), putty_xor(r,r,t) )
|
||||
#define smallsigma1(r,t,x) ( rorL(r,x,19), rorB(t,x,61), putty_xor(r,r,t), \
|
||||
shrL(t,x,6), putty_xor(r,r,t) )
|
||||
|
||||
static void SHA512_Core_Init(SHA512_State *s) {
|
||||
static const uint64 iv[] = {
|
||||
INIT(0x6a09e667, 0xf3bcc908),
|
||||
INIT(0xbb67ae85, 0x84caa73b),
|
||||
INIT(0x3c6ef372, 0xfe94f82b),
|
||||
INIT(0xa54ff53a, 0x5f1d36f1),
|
||||
INIT(0x510e527f, 0xade682d1),
|
||||
INIT(0x9b05688c, 0x2b3e6c1f),
|
||||
INIT(0x1f83d9ab, 0xfb41bd6b),
|
||||
INIT(0x5be0cd19, 0x137e2179),
|
||||
};
|
||||
int i;
|
||||
for (i = 0; i < 8; i++)
|
||||
s->h[i] = iv[i];
|
||||
}
|
||||
|
||||
static void SHA512_Block(SHA512_State *s, uint64 *block) {
|
||||
uint64 w[80];
|
||||
uint64 a,b,c,d,e,f,g,h;
|
||||
static const uint64 k[] = {
|
||||
INIT(0x428a2f98, 0xd728ae22), INIT(0x71374491, 0x23ef65cd),
|
||||
INIT(0xb5c0fbcf, 0xec4d3b2f), INIT(0xe9b5dba5, 0x8189dbbc),
|
||||
INIT(0x3956c25b, 0xf348b538), INIT(0x59f111f1, 0xb605d019),
|
||||
INIT(0x923f82a4, 0xaf194f9b), INIT(0xab1c5ed5, 0xda6d8118),
|
||||
INIT(0xd807aa98, 0xa3030242), INIT(0x12835b01, 0x45706fbe),
|
||||
INIT(0x243185be, 0x4ee4b28c), INIT(0x550c7dc3, 0xd5ffb4e2),
|
||||
INIT(0x72be5d74, 0xf27b896f), INIT(0x80deb1fe, 0x3b1696b1),
|
||||
INIT(0x9bdc06a7, 0x25c71235), INIT(0xc19bf174, 0xcf692694),
|
||||
INIT(0xe49b69c1, 0x9ef14ad2), INIT(0xefbe4786, 0x384f25e3),
|
||||
INIT(0x0fc19dc6, 0x8b8cd5b5), INIT(0x240ca1cc, 0x77ac9c65),
|
||||
INIT(0x2de92c6f, 0x592b0275), INIT(0x4a7484aa, 0x6ea6e483),
|
||||
INIT(0x5cb0a9dc, 0xbd41fbd4), INIT(0x76f988da, 0x831153b5),
|
||||
INIT(0x983e5152, 0xee66dfab), INIT(0xa831c66d, 0x2db43210),
|
||||
INIT(0xb00327c8, 0x98fb213f), INIT(0xbf597fc7, 0xbeef0ee4),
|
||||
INIT(0xc6e00bf3, 0x3da88fc2), INIT(0xd5a79147, 0x930aa725),
|
||||
INIT(0x06ca6351, 0xe003826f), INIT(0x14292967, 0x0a0e6e70),
|
||||
INIT(0x27b70a85, 0x46d22ffc), INIT(0x2e1b2138, 0x5c26c926),
|
||||
INIT(0x4d2c6dfc, 0x5ac42aed), INIT(0x53380d13, 0x9d95b3df),
|
||||
INIT(0x650a7354, 0x8baf63de), INIT(0x766a0abb, 0x3c77b2a8),
|
||||
INIT(0x81c2c92e, 0x47edaee6), INIT(0x92722c85, 0x1482353b),
|
||||
INIT(0xa2bfe8a1, 0x4cf10364), INIT(0xa81a664b, 0xbc423001),
|
||||
INIT(0xc24b8b70, 0xd0f89791), INIT(0xc76c51a3, 0x0654be30),
|
||||
INIT(0xd192e819, 0xd6ef5218), INIT(0xd6990624, 0x5565a910),
|
||||
INIT(0xf40e3585, 0x5771202a), INIT(0x106aa070, 0x32bbd1b8),
|
||||
INIT(0x19a4c116, 0xb8d2d0c8), INIT(0x1e376c08, 0x5141ab53),
|
||||
INIT(0x2748774c, 0xdf8eeb99), INIT(0x34b0bcb5, 0xe19b48a8),
|
||||
INIT(0x391c0cb3, 0xc5c95a63), INIT(0x4ed8aa4a, 0xe3418acb),
|
||||
INIT(0x5b9cca4f, 0x7763e373), INIT(0x682e6ff3, 0xd6b2b8a3),
|
||||
INIT(0x748f82ee, 0x5defb2fc), INIT(0x78a5636f, 0x43172f60),
|
||||
INIT(0x84c87814, 0xa1f0ab72), INIT(0x8cc70208, 0x1a6439ec),
|
||||
INIT(0x90befffa, 0x23631e28), INIT(0xa4506ceb, 0xde82bde9),
|
||||
INIT(0xbef9a3f7, 0xb2c67915), INIT(0xc67178f2, 0xe372532b),
|
||||
INIT(0xca273ece, 0xea26619c), INIT(0xd186b8c7, 0x21c0c207),
|
||||
INIT(0xeada7dd6, 0xcde0eb1e), INIT(0xf57d4f7f, 0xee6ed178),
|
||||
INIT(0x06f067aa, 0x72176fba), INIT(0x0a637dc5, 0xa2c898a6),
|
||||
INIT(0x113f9804, 0xbef90dae), INIT(0x1b710b35, 0x131c471b),
|
||||
INIT(0x28db77f5, 0x23047d84), INIT(0x32caab7b, 0x40c72493),
|
||||
INIT(0x3c9ebe0a, 0x15c9bebc), INIT(0x431d67c4, 0x9c100d4c),
|
||||
INIT(0x4cc5d4be, 0xcb3e42b6), INIT(0x597f299c, 0xfc657e2a),
|
||||
INIT(0x5fcb6fab, 0x3ad6faec), INIT(0x6c44198c, 0x4a475817),
|
||||
};
|
||||
|
||||
int t;
|
||||
|
||||
for (t = 0; t < 16; t++)
|
||||
w[t] = block[t];
|
||||
|
||||
for (t = 16; t < 80; t++) {
|
||||
uint64 p, q, r, tmp;
|
||||
smallsigma1(p, tmp, w[t-2]);
|
||||
smallsigma0(q, tmp, w[t-15]);
|
||||
add(r, p, q);
|
||||
add(p, r, w[t-7]);
|
||||
add(w[t], p, w[t-16]);
|
||||
}
|
||||
|
||||
a = s->h[0]; b = s->h[1]; c = s->h[2]; d = s->h[3];
|
||||
e = s->h[4]; f = s->h[5]; g = s->h[6]; h = s->h[7];
|
||||
|
||||
for (t = 0; t < 80; t+=8) {
|
||||
uint64 tmp, p, q, r;
|
||||
|
||||
#define ROUND(j,a,b,c,d,e,f,g,h) \
|
||||
bigsigma1(p, tmp, e); \
|
||||
Ch(q, tmp, e, f, g); \
|
||||
add(r, p, q); \
|
||||
add(p, r, k[j]) ; \
|
||||
add(q, p, w[j]); \
|
||||
add(r, q, h); \
|
||||
bigsigma0(p, tmp, a); \
|
||||
Maj(tmp, q, a, b, c); \
|
||||
add(q, tmp, p); \
|
||||
add(p, r, d); \
|
||||
d = p; \
|
||||
add(h, q, r);
|
||||
|
||||
ROUND(t+0, a,b,c,d,e,f,g,h);
|
||||
ROUND(t+1, h,a,b,c,d,e,f,g);
|
||||
ROUND(t+2, g,h,a,b,c,d,e,f);
|
||||
ROUND(t+3, f,g,h,a,b,c,d,e);
|
||||
ROUND(t+4, e,f,g,h,a,b,c,d);
|
||||
ROUND(t+5, d,e,f,g,h,a,b,c);
|
||||
ROUND(t+6, c,d,e,f,g,h,a,b);
|
||||
ROUND(t+7, b,c,d,e,f,g,h,a);
|
||||
}
|
||||
|
||||
{
|
||||
uint64 tmp;
|
||||
#define UPDATE(state, local) ( tmp = state, add(state, tmp, local) )
|
||||
UPDATE(s->h[0], a); UPDATE(s->h[1], b);
|
||||
UPDATE(s->h[2], c); UPDATE(s->h[3], d);
|
||||
UPDATE(s->h[4], e); UPDATE(s->h[5], f);
|
||||
UPDATE(s->h[6], g); UPDATE(s->h[7], h);
|
||||
}
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------
|
||||
* Outer SHA512 algorithm: take an arbitrary length byte string,
|
||||
* convert it into 16-doubleword blocks with the prescribed padding
|
||||
* at the end, and pass those blocks to the core SHA512 algorithm.
|
||||
*/
|
||||
|
||||
void SHA512_Init(SHA512_State *s) {
|
||||
int i;
|
||||
SHA512_Core_Init(s);
|
||||
s->blkused = 0;
|
||||
for (i = 0; i < 4; i++)
|
||||
s->len[i] = 0;
|
||||
}
|
||||
|
||||
void SHA512_Bytes(SHA512_State *s, const void *p, int len) {
|
||||
unsigned char *q = (unsigned char *)p;
|
||||
uint64 wordblock[16];
|
||||
uint32 lenw = len;
|
||||
int i;
|
||||
|
||||
/*
|
||||
* Update the length field.
|
||||
*/
|
||||
for (i = 0; i < 4; i++) {
|
||||
s->len[i] += lenw;
|
||||
lenw = (s->len[i] < lenw);
|
||||
}
|
||||
|
||||
if (s->blkused && s->blkused+len < BLKSIZE) {
|
||||
/*
|
||||
* Trivial case: just add to the block.
|
||||
*/
|
||||
memcpy(s->block + s->blkused, q, len);
|
||||
s->blkused += len;
|
||||
} else {
|
||||
/*
|
||||
* We must complete and process at least one block.
|
||||
*/
|
||||
while (s->blkused + len >= BLKSIZE) {
|
||||
memcpy(s->block + s->blkused, q, BLKSIZE - s->blkused);
|
||||
q += BLKSIZE - s->blkused;
|
||||
len -= BLKSIZE - s->blkused;
|
||||
/* Now process the block. Gather bytes big-endian into words */
|
||||
for (i = 0; i < 16; i++) {
|
||||
uint32 h, l;
|
||||
h = ( ((uint32)s->block[i*8+0]) << 24 ) |
|
||||
( ((uint32)s->block[i*8+1]) << 16 ) |
|
||||
( ((uint32)s->block[i*8+2]) << 8 ) |
|
||||
( ((uint32)s->block[i*8+3]) << 0 );
|
||||
l = ( ((uint32)s->block[i*8+4]) << 24 ) |
|
||||
( ((uint32)s->block[i*8+5]) << 16 ) |
|
||||
( ((uint32)s->block[i*8+6]) << 8 ) |
|
||||
( ((uint32)s->block[i*8+7]) << 0 );
|
||||
BUILD(wordblock[i], h, l);
|
||||
}
|
||||
SHA512_Block(s, wordblock);
|
||||
s->blkused = 0;
|
||||
}
|
||||
memcpy(s->block, q, len);
|
||||
s->blkused = len;
|
||||
}
|
||||
}
|
||||
|
||||
void SHA512_Final(SHA512_State *s, unsigned char *digest) {
|
||||
int i;
|
||||
int pad;
|
||||
unsigned char c[BLKSIZE];
|
||||
uint32 len[4];
|
||||
|
||||
if (s->blkused >= BLKSIZE-16)
|
||||
pad = (BLKSIZE-16) + BLKSIZE - s->blkused;
|
||||
else
|
||||
pad = (BLKSIZE-16) - s->blkused;
|
||||
|
||||
for (i = 4; i-- ;) {
|
||||
uint32 lenhi = s->len[i];
|
||||
uint32 lenlo = i > 0 ? s->len[i-1] : 0;
|
||||
len[i] = (lenhi << 3) | (lenlo >> (32-3));
|
||||
}
|
||||
|
||||
memset(c, 0, pad);
|
||||
c[0] = 0x80;
|
||||
SHA512_Bytes(s, &c, pad);
|
||||
|
||||
for (i = 0; i < 4; i++) {
|
||||
c[i*4+0] = (len[3-i] >> 24) & 0xFF;
|
||||
c[i*4+1] = (len[3-i] >> 16) & 0xFF;
|
||||
c[i*4+2] = (len[3-i] >> 8) & 0xFF;
|
||||
c[i*4+3] = (len[3-i] >> 0) & 0xFF;
|
||||
}
|
||||
|
||||
SHA512_Bytes(s, &c, 16);
|
||||
|
||||
for (i = 0; i < 8; i++) {
|
||||
uint32 h, l;
|
||||
EXTRACT(h, l, s->h[i]);
|
||||
digest[i*8+0] = (h >> 24) & 0xFF;
|
||||
digest[i*8+1] = (h >> 16) & 0xFF;
|
||||
digest[i*8+2] = (h >> 8) & 0xFF;
|
||||
digest[i*8+3] = (h >> 0) & 0xFF;
|
||||
digest[i*8+4] = (l >> 24) & 0xFF;
|
||||
digest[i*8+5] = (l >> 16) & 0xFF;
|
||||
digest[i*8+6] = (l >> 8) & 0xFF;
|
||||
digest[i*8+7] = (l >> 0) & 0xFF;
|
||||
}
|
||||
}
|
||||
|
||||
void SHA512_Simple(const void *p, int len, unsigned char *output) {
|
||||
SHA512_State s;
|
||||
|
||||
SHA512_Init(&s);
|
||||
SHA512_Bytes(&s, p, len);
|
||||
SHA512_Final(&s, output);
|
||||
}
|
||||
|
||||
#ifdef TEST
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
|
||||
int main(void) {
|
||||
unsigned char digest[64];
|
||||
int i, j, errors;
|
||||
|
||||
struct {
|
||||
const char *teststring;
|
||||
unsigned char digest512[64];
|
||||
} tests[] = {
|
||||
{ "abc", {
|
||||
0xdd, 0xaf, 0x35, 0xa1, 0x93, 0x61, 0x7a, 0xba,
|
||||
0xcc, 0x41, 0x73, 0x49, 0xae, 0x20, 0x41, 0x31,
|
||||
0x12, 0xe6, 0xfa, 0x4e, 0x89, 0xa9, 0x7e, 0xa2,
|
||||
0x0a, 0x9e, 0xee, 0xe6, 0x4b, 0x55, 0xd3, 0x9a,
|
||||
0x21, 0x92, 0x99, 0x2a, 0x27, 0x4f, 0xc1, 0xa8,
|
||||
0x36, 0xba, 0x3c, 0x23, 0xa3, 0xfe, 0xeb, 0xbd,
|
||||
0x45, 0x4d, 0x44, 0x23, 0x64, 0x3c, 0xe8, 0x0e,
|
||||
0x2a, 0x9a, 0xc9, 0x4f, 0xa5, 0x4c, 0xa4, 0x9f,
|
||||
} },
|
||||
{ "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"
|
||||
"hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", {
|
||||
0x8e, 0x95, 0x9b, 0x75, 0xda, 0xe3, 0x13, 0xda,
|
||||
0x8c, 0xf4, 0xf7, 0x28, 0x14, 0xfc, 0x14, 0x3f,
|
||||
0x8f, 0x77, 0x79, 0xc6, 0xeb, 0x9f, 0x7f, 0xa1,
|
||||
0x72, 0x99, 0xae, 0xad, 0xb6, 0x88, 0x90, 0x18,
|
||||
0x50, 0x1d, 0x28, 0x9e, 0x49, 0x00, 0xf7, 0xe4,
|
||||
0x33, 0x1b, 0x99, 0xde, 0xc4, 0xb5, 0x43, 0x3a,
|
||||
0xc7, 0xd3, 0x29, 0xee, 0xb6, 0xdd, 0x26, 0x54,
|
||||
0x5e, 0x96, 0xe5, 0x5b, 0x87, 0x4b, 0xe9, 0x09,
|
||||
} },
|
||||
{ NULL, {
|
||||
0xe7, 0x18, 0x48, 0x3d, 0x0c, 0xe7, 0x69, 0x64,
|
||||
0x4e, 0x2e, 0x42, 0xc7, 0xbc, 0x15, 0xb4, 0x63,
|
||||
0x8e, 0x1f, 0x98, 0xb1, 0x3b, 0x20, 0x44, 0x28,
|
||||
0x56, 0x32, 0xa8, 0x03, 0xaf, 0xa9, 0x73, 0xeb,
|
||||
0xde, 0x0f, 0xf2, 0x44, 0x87, 0x7e, 0xa6, 0x0a,
|
||||
0x4c, 0xb0, 0x43, 0x2c, 0xe5, 0x77, 0xc3, 0x1b,
|
||||
0xeb, 0x00, 0x9c, 0x5c, 0x2c, 0x49, 0xaa, 0x2e,
|
||||
0x4e, 0xad, 0xb2, 0x17, 0xad, 0x8c, 0xc0, 0x9b,
|
||||
} },
|
||||
};
|
||||
|
||||
errors = 0;
|
||||
|
||||
for (i = 0; i < sizeof(tests) / sizeof(*tests); i++) {
|
||||
if (tests[i].teststring) {
|
||||
SHA512_Simple(tests[i].teststring,
|
||||
strlen(tests[i].teststring), digest);
|
||||
} else {
|
||||
SHA512_State s;
|
||||
int n;
|
||||
SHA512_Init(&s);
|
||||
for (n = 0; n < 1000000 / 40; n++)
|
||||
SHA512_Bytes(&s, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
||||
40);
|
||||
SHA512_Final(&s, digest);
|
||||
}
|
||||
for (j = 0; j < 64; j++) {
|
||||
if (digest[j] != tests[i].digest512[j]) {
|
||||
fprintf(stderr,
|
||||
"\"%s\" digest512 byte %d should be 0x%02x, is 0x%02x\n",
|
||||
tests[i].teststring, j, tests[i].digest512[j],
|
||||
digest[j]);
|
||||
errors++;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
printf("%d errors\n", errors);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
423
database/FileZillaFTP/source/hash_algorithms/sshsha.c
Normal file
423
database/FileZillaFTP/source/hash_algorithms/sshsha.c
Normal file
@@ -0,0 +1,423 @@
|
||||
/*
|
||||
* SHA1 hash algorithm. Used in SSH-2 as a MAC, and the transform is
|
||||
* also used as a `stirring' function for the PuTTY random number
|
||||
* pool. Implemented directly from the specification by Simon
|
||||
* Tatham.
|
||||
*/
|
||||
|
||||
#ifdef SHA512_STANDALONE
|
||||
typedef unsigned int word32;
|
||||
typedef struct {
|
||||
uint32 h[5];
|
||||
unsigned char block[64];
|
||||
int blkused;
|
||||
uint32 lenhi, lenlo;
|
||||
} SHA_State;
|
||||
#else
|
||||
#include "ssh.h"
|
||||
#endif
|
||||
|
||||
/* ----------------------------------------------------------------------
|
||||
* Core SHA algorithm: processes 16-word blocks into a message digest.
|
||||
*/
|
||||
|
||||
#define rol(x,y) ( ((x) << (y)) | (((uint32)x) >> (32-y)) )
|
||||
|
||||
static void SHA_Core_Init(uint32 h[5])
|
||||
{
|
||||
h[0] = 0x67452301;
|
||||
h[1] = 0xefcdab89;
|
||||
h[2] = 0x98badcfe;
|
||||
h[3] = 0x10325476;
|
||||
h[4] = 0xc3d2e1f0;
|
||||
}
|
||||
|
||||
void SHATransform(word32 * digest, word32 * block)
|
||||
{
|
||||
word32 w[80];
|
||||
word32 a, b, c, d, e;
|
||||
int t;
|
||||
|
||||
for (t = 0; t < 16; t++)
|
||||
w[t] = block[t];
|
||||
|
||||
for (t = 16; t < 80; t++) {
|
||||
word32 tmp = w[t - 3] ^ w[t - 8] ^ w[t - 14] ^ w[t - 16];
|
||||
w[t] = rol(tmp, 1);
|
||||
}
|
||||
|
||||
a = digest[0];
|
||||
b = digest[1];
|
||||
c = digest[2];
|
||||
d = digest[3];
|
||||
e = digest[4];
|
||||
|
||||
for (t = 0; t < 20; t++) {
|
||||
word32 tmp =
|
||||
rol(a, 5) + ((b & c) | (d & ~b)) + e + w[t] + 0x5a827999;
|
||||
e = d;
|
||||
d = c;
|
||||
c = rol(b, 30);
|
||||
b = a;
|
||||
a = tmp;
|
||||
}
|
||||
for (t = 20; t < 40; t++) {
|
||||
word32 tmp = rol(a, 5) + (b ^ c ^ d) + e + w[t] + 0x6ed9eba1;
|
||||
e = d;
|
||||
d = c;
|
||||
c = rol(b, 30);
|
||||
b = a;
|
||||
a = tmp;
|
||||
}
|
||||
for (t = 40; t < 60; t++) {
|
||||
word32 tmp = rol(a,
|
||||
5) + ((b & c) | (b & d) | (c & d)) + e + w[t] +
|
||||
0x8f1bbcdc;
|
||||
e = d;
|
||||
d = c;
|
||||
c = rol(b, 30);
|
||||
b = a;
|
||||
a = tmp;
|
||||
}
|
||||
for (t = 60; t < 80; t++) {
|
||||
word32 tmp = rol(a, 5) + (b ^ c ^ d) + e + w[t] + 0xca62c1d6;
|
||||
e = d;
|
||||
d = c;
|
||||
c = rol(b, 30);
|
||||
b = a;
|
||||
a = tmp;
|
||||
}
|
||||
|
||||
digest[0] += a;
|
||||
digest[1] += b;
|
||||
digest[2] += c;
|
||||
digest[3] += d;
|
||||
digest[4] += e;
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------
|
||||
* Outer SHA algorithm: take an arbitrary length byte string,
|
||||
* convert it into 16-word blocks with the prescribed padding at
|
||||
* the end, and pass those blocks to the core SHA algorithm.
|
||||
*/
|
||||
|
||||
void SHA_Init(SHA_State * s)
|
||||
{
|
||||
SHA_Core_Init(s->h);
|
||||
s->blkused = 0;
|
||||
s->lenhi = s->lenlo = 0;
|
||||
}
|
||||
|
||||
void SHA_Bytes(SHA_State * s, void *p, int len)
|
||||
{
|
||||
unsigned char *q = (unsigned char *) p;
|
||||
uint32 wordblock[16];
|
||||
uint32 lenw = len;
|
||||
int i;
|
||||
|
||||
/*
|
||||
* Update the length field.
|
||||
*/
|
||||
s->lenlo += lenw;
|
||||
s->lenhi += (s->lenlo < lenw);
|
||||
|
||||
if (s->blkused && s->blkused + len < 64) {
|
||||
/*
|
||||
* Trivial case: just add to the block.
|
||||
*/
|
||||
memcpy(s->block + s->blkused, q, len);
|
||||
s->blkused += len;
|
||||
} else {
|
||||
/*
|
||||
* We must complete and process at least one block.
|
||||
*/
|
||||
while (s->blkused + len >= 64) {
|
||||
memcpy(s->block + s->blkused, q, 64 - s->blkused);
|
||||
q += 64 - s->blkused;
|
||||
len -= 64 - s->blkused;
|
||||
/* Now process the block. Gather bytes big-endian into words */
|
||||
for (i = 0; i < 16; i++) {
|
||||
wordblock[i] =
|
||||
(((uint32) s->block[i * 4 + 0]) << 24) |
|
||||
(((uint32) s->block[i * 4 + 1]) << 16) |
|
||||
(((uint32) s->block[i * 4 + 2]) << 8) |
|
||||
(((uint32) s->block[i * 4 + 3]) << 0);
|
||||
}
|
||||
SHATransform(s->h, wordblock);
|
||||
s->blkused = 0;
|
||||
}
|
||||
memcpy(s->block, q, len);
|
||||
s->blkused = len;
|
||||
}
|
||||
}
|
||||
|
||||
void SHA_Final(SHA_State * s, unsigned char *output)
|
||||
{
|
||||
int i;
|
||||
int pad;
|
||||
unsigned char c[64];
|
||||
uint32 lenhi, lenlo;
|
||||
|
||||
if (s->blkused >= 56)
|
||||
pad = 56 + 64 - s->blkused;
|
||||
else
|
||||
pad = 56 - s->blkused;
|
||||
|
||||
lenhi = (s->lenhi << 3) | (s->lenlo >> (32 - 3));
|
||||
lenlo = (s->lenlo << 3);
|
||||
|
||||
memset(c, 0, pad);
|
||||
c[0] = 0x80;
|
||||
SHA_Bytes(s, &c, pad);
|
||||
|
||||
c[0] = (lenhi >> 24) & 0xFF;
|
||||
c[1] = (lenhi >> 16) & 0xFF;
|
||||
c[2] = (lenhi >> 8) & 0xFF;
|
||||
c[3] = (lenhi >> 0) & 0xFF;
|
||||
c[4] = (lenlo >> 24) & 0xFF;
|
||||
c[5] = (lenlo >> 16) & 0xFF;
|
||||
c[6] = (lenlo >> 8) & 0xFF;
|
||||
c[7] = (lenlo >> 0) & 0xFF;
|
||||
|
||||
SHA_Bytes(s, &c, 8);
|
||||
|
||||
for (i = 0; i < 5; i++) {
|
||||
output[i * 4] = (s->h[i] >> 24) & 0xFF;
|
||||
output[i * 4 + 1] = (s->h[i] >> 16) & 0xFF;
|
||||
output[i * 4 + 2] = (s->h[i] >> 8) & 0xFF;
|
||||
output[i * 4 + 3] = (s->h[i]) & 0xFF;
|
||||
}
|
||||
}
|
||||
|
||||
void SHA_Simple(void *p, int len, unsigned char *output)
|
||||
{
|
||||
SHA_State s;
|
||||
|
||||
SHA_Init(&s);
|
||||
SHA_Bytes(&s, p, len);
|
||||
SHA_Final(&s, output);
|
||||
}
|
||||
|
||||
/*
|
||||
* Thin abstraction for things where hashes are pluggable.
|
||||
*/
|
||||
|
||||
#ifndef SHA512_STANDALONE
|
||||
static void *sha1_init(void)
|
||||
{
|
||||
SHA_State *s;
|
||||
|
||||
s = snew(SHA_State);
|
||||
SHA_Init(s);
|
||||
return s;
|
||||
}
|
||||
|
||||
static void sha1_bytes(void *handle, void *p, int len)
|
||||
{
|
||||
SHA_State *s = handle;
|
||||
|
||||
SHA_Bytes(s, p, len);
|
||||
}
|
||||
|
||||
static void sha1_final(void *handle, unsigned char *output)
|
||||
{
|
||||
SHA_State *s = handle;
|
||||
|
||||
SHA_Final(s, output);
|
||||
sfree(s);
|
||||
}
|
||||
|
||||
const struct ssh_hash ssh_sha1 = {
|
||||
sha1_init, sha1_bytes, sha1_final, 20, "SHA-1"
|
||||
};
|
||||
|
||||
/* ----------------------------------------------------------------------
|
||||
* The above is the SHA-1 algorithm itself. Now we implement the
|
||||
* HMAC wrapper on it.
|
||||
*/
|
||||
|
||||
static void *sha1_make_context(void)
|
||||
{
|
||||
return snewn(3, SHA_State);
|
||||
}
|
||||
|
||||
static void sha1_free_context(void *handle)
|
||||
{
|
||||
sfree(handle);
|
||||
}
|
||||
|
||||
static void sha1_key_internal(void *handle, unsigned char *key, int len)
|
||||
{
|
||||
SHA_State *keys = (SHA_State *)handle;
|
||||
unsigned char foo[64];
|
||||
int i;
|
||||
|
||||
memset(foo, 0x36, 64);
|
||||
for (i = 0; i < len && i < 64; i++)
|
||||
foo[i] ^= key[i];
|
||||
SHA_Init(&keys[0]);
|
||||
SHA_Bytes(&keys[0], foo, 64);
|
||||
|
||||
memset(foo, 0x5C, 64);
|
||||
for (i = 0; i < len && i < 64; i++)
|
||||
foo[i] ^= key[i];
|
||||
SHA_Init(&keys[1]);
|
||||
SHA_Bytes(&keys[1], foo, 64);
|
||||
|
||||
memset(foo, 0, 64); /* burn the evidence */
|
||||
}
|
||||
|
||||
static void sha1_key(void *handle, unsigned char *key)
|
||||
{
|
||||
sha1_key_internal(handle, key, 20);
|
||||
}
|
||||
|
||||
static void sha1_key_buggy(void *handle, unsigned char *key)
|
||||
{
|
||||
sha1_key_internal(handle, key, 16);
|
||||
}
|
||||
|
||||
static void hmacsha1_start(void *handle)
|
||||
{
|
||||
SHA_State *keys = (SHA_State *)handle;
|
||||
|
||||
keys[2] = keys[0]; /* structure copy */
|
||||
}
|
||||
|
||||
static void hmacsha1_bytes(void *handle, unsigned char const *blk, int len)
|
||||
{
|
||||
SHA_State *keys = (SHA_State *)handle;
|
||||
SHA_Bytes(&keys[2], (void *)blk, len);
|
||||
}
|
||||
|
||||
static void hmacsha1_genresult(void *handle, unsigned char *hmac)
|
||||
{
|
||||
SHA_State *keys = (SHA_State *)handle;
|
||||
SHA_State s;
|
||||
unsigned char intermediate[20];
|
||||
|
||||
s = keys[2]; /* structure copy */
|
||||
SHA_Final(&s, intermediate);
|
||||
s = keys[1]; /* structure copy */
|
||||
SHA_Bytes(&s, intermediate, 20);
|
||||
SHA_Final(&s, hmac);
|
||||
}
|
||||
|
||||
static void sha1_do_hmac(void *handle, unsigned char *blk, int len,
|
||||
unsigned long seq, unsigned char *hmac)
|
||||
{
|
||||
unsigned char seqbuf[4];
|
||||
|
||||
seqbuf[0] = (unsigned char) ((seq >> 24) & 0xFF);
|
||||
seqbuf[1] = (unsigned char) ((seq >> 16) & 0xFF);
|
||||
seqbuf[2] = (unsigned char) ((seq >> 8) & 0xFF);
|
||||
seqbuf[3] = (unsigned char) ((seq) & 0xFF);
|
||||
|
||||
hmacsha1_start(handle);
|
||||
hmacsha1_bytes(handle, seqbuf, 4);
|
||||
hmacsha1_bytes(handle, blk, len);
|
||||
hmacsha1_genresult(handle, hmac);
|
||||
}
|
||||
|
||||
static void sha1_generate(void *handle, unsigned char *blk, int len,
|
||||
unsigned long seq)
|
||||
{
|
||||
sha1_do_hmac(handle, blk, len, seq, blk + len);
|
||||
}
|
||||
|
||||
static int hmacsha1_verresult(void *handle, unsigned char const *hmac)
|
||||
{
|
||||
unsigned char correct[20];
|
||||
hmacsha1_genresult(handle, correct);
|
||||
return !memcmp(correct, hmac, 20);
|
||||
}
|
||||
|
||||
static int sha1_verify(void *handle, unsigned char *blk, int len,
|
||||
unsigned long seq)
|
||||
{
|
||||
unsigned char correct[20];
|
||||
sha1_do_hmac(handle, blk, len, seq, correct);
|
||||
return !memcmp(correct, blk + len, 20);
|
||||
}
|
||||
|
||||
static void hmacsha1_96_genresult(void *handle, unsigned char *hmac)
|
||||
{
|
||||
unsigned char full[20];
|
||||
hmacsha1_genresult(handle, full);
|
||||
memcpy(hmac, full, 12);
|
||||
}
|
||||
|
||||
static void sha1_96_generate(void *handle, unsigned char *blk, int len,
|
||||
unsigned long seq)
|
||||
{
|
||||
unsigned char full[20];
|
||||
sha1_do_hmac(handle, blk, len, seq, full);
|
||||
memcpy(blk + len, full, 12);
|
||||
}
|
||||
|
||||
static int hmacsha1_96_verresult(void *handle, unsigned char const *hmac)
|
||||
{
|
||||
unsigned char correct[20];
|
||||
hmacsha1_genresult(handle, correct);
|
||||
return !memcmp(correct, hmac, 12);
|
||||
}
|
||||
|
||||
static int sha1_96_verify(void *handle, unsigned char *blk, int len,
|
||||
unsigned long seq)
|
||||
{
|
||||
unsigned char correct[20];
|
||||
sha1_do_hmac(handle, blk, len, seq, correct);
|
||||
return !memcmp(correct, blk + len, 12);
|
||||
}
|
||||
|
||||
void hmac_sha1_simple(void *key, int keylen, void *data, int datalen,
|
||||
unsigned char *output) {
|
||||
SHA_State states[2];
|
||||
unsigned char intermediate[20];
|
||||
|
||||
sha1_key_internal(states, key, keylen);
|
||||
SHA_Bytes(&states[0], data, datalen);
|
||||
SHA_Final(&states[0], intermediate);
|
||||
|
||||
SHA_Bytes(&states[1], intermediate, 20);
|
||||
SHA_Final(&states[1], output);
|
||||
}
|
||||
|
||||
const struct ssh_mac ssh_hmac_sha1 = {
|
||||
sha1_make_context, sha1_free_context, sha1_key,
|
||||
sha1_generate, sha1_verify,
|
||||
hmacsha1_start, hmacsha1_bytes, hmacsha1_genresult, hmacsha1_verresult,
|
||||
"hmac-sha1",
|
||||
20,
|
||||
"HMAC-SHA1"
|
||||
};
|
||||
|
||||
const struct ssh_mac ssh_hmac_sha1_96 = {
|
||||
sha1_make_context, sha1_free_context, sha1_key,
|
||||
sha1_96_generate, sha1_96_verify,
|
||||
hmacsha1_start, hmacsha1_bytes,
|
||||
hmacsha1_96_genresult, hmacsha1_96_verresult,
|
||||
"hmac-sha1-96",
|
||||
12,
|
||||
"HMAC-SHA1-96"
|
||||
};
|
||||
|
||||
const struct ssh_mac ssh_hmac_sha1_buggy = {
|
||||
sha1_make_context, sha1_free_context, sha1_key_buggy,
|
||||
sha1_generate, sha1_verify,
|
||||
hmacsha1_start, hmacsha1_bytes, hmacsha1_genresult, hmacsha1_verresult,
|
||||
"hmac-sha1",
|
||||
20,
|
||||
"bug-compatible HMAC-SHA1"
|
||||
};
|
||||
|
||||
const struct ssh_mac ssh_hmac_sha1_96_buggy = {
|
||||
sha1_make_context, sha1_free_context, sha1_key_buggy,
|
||||
sha1_96_generate, sha1_96_verify,
|
||||
hmacsha1_start, hmacsha1_bytes,
|
||||
hmacsha1_96_genresult, hmacsha1_96_verresult,
|
||||
"hmac-sha1-96",
|
||||
12,
|
||||
"bug-compatible HMAC-SHA1-96"
|
||||
};
|
||||
#endif
|
||||
Reference in New Issue
Block a user