Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion trantor/net/inner/FileBufferNodeWin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class FileBufferNode : public BufferNode
DWORD n = 0;
if (!ReadFile(sendHandle_,
msgBufferPtr_->beginWrite(),
msgBufferPtr_->writableBytes(),
(uint32_t)msgBufferPtr_->writableBytes(),
&n,
nullptr))
{
Expand Down
6 changes: 3 additions & 3 deletions trantor/utils/Utilities.cc
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,10 @@ Hash128 md5(const void *data, size_t len)
Hash160 sha1(const void *data, size_t len)
{
SHA1_CTX ctx;
TrantorSHA1Init(&ctx);
TrantorSHA1Update(&ctx, (const unsigned char *)data, len);
trantor_sha1_init(&ctx);
trantor_sha1_update(&ctx, (const unsigned char *)data, len);
Hash160 hash;
TrantorSHA1Final((unsigned char *)&hash, &ctx);
trantor_sha1_final((unsigned char *)&hash, &ctx);
return hash;
}

Expand Down
24 changes: 12 additions & 12 deletions trantor/utils/crypto/md5.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ order.
}

/*********************** FUNCTION DEFINITIONS ***********************/
void trantor_md5_transform(MD5_CTX *ctx, const BYTE data[])
void trantor_md5_transform(MD5_CTX *ctx, const uint8_t data[])
{
WORD a, b, c, d, m[16], i, j;
uint32_t a, b, c, d, m[16], i, j;

// MD5 specifies big endian byte order, but this implementation assumes a
// little endian byte order CPU. Reverse all the bytes upon input, and
Expand Down Expand Up @@ -145,7 +145,7 @@ void trantor_md5_init(MD5_CTX *ctx)
ctx->state[3] = 0x10325476;
}

void trantor_md5_update(MD5_CTX *ctx, const BYTE data[], size_t len)
void trantor_md5_update(MD5_CTX *ctx, const uint8_t data[], size_t len)
{
size_t i;

Expand All @@ -162,7 +162,7 @@ void trantor_md5_update(MD5_CTX *ctx, const BYTE data[], size_t len)
}
}

void trantor_md5_final(MD5_CTX *ctx, BYTE hash[])
void trantor_md5_final(MD5_CTX *ctx, uint8_t hash[])
{
size_t i;

Expand All @@ -186,14 +186,14 @@ void trantor_md5_final(MD5_CTX *ctx, BYTE hash[])

// Append to the padding the total message's length in bits and transform.
ctx->bitlen += ctx->datalen * 8;
ctx->data[56] = ctx->bitlen;
ctx->data[57] = ctx->bitlen >> 8;
ctx->data[58] = ctx->bitlen >> 16;
ctx->data[59] = ctx->bitlen >> 24;
ctx->data[60] = ctx->bitlen >> 32;
ctx->data[61] = ctx->bitlen >> 40;
ctx->data[62] = ctx->bitlen >> 48;
ctx->data[63] = ctx->bitlen >> 56;
ctx->data[56] = (uint8_t)ctx->bitlen;
ctx->data[57] = (uint8_t)(ctx->bitlen >> 8);
ctx->data[58] = (uint8_t)(ctx->bitlen >> 16);
ctx->data[59] = (uint8_t)(ctx->bitlen >> 24);
ctx->data[60] = (uint8_t)(ctx->bitlen >> 32);
ctx->data[61] = (uint8_t)(ctx->bitlen >> 40);
ctx->data[62] = (uint8_t)(ctx->bitlen >> 48);
ctx->data[63] = (uint8_t)(ctx->bitlen >> 56);
trantor_md5_transform(ctx, ctx->data);

// Since this implementation uses little endian byte ordering and MD uses
Expand Down
24 changes: 8 additions & 16 deletions trantor/utils/crypto/md5.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,22 @@

/*************************** HEADER FILES ***************************/
#include <stddef.h>
#include <stdint.h>

/****************************** MACROS ******************************/
#define MD5_BLOCK_SIZE 16 // MD5 outputs a 16 byte digest

/**************************** DATA TYPES ****************************/

#ifndef _WIN32
typedef unsigned char BYTE; // 8-bit byte
typedef unsigned int WORD; // 32-bit word, change to "long" for 16-bit machines
#else
#include <windows.h>
#endif
#define MD5_BLOCK_SIZE 32 // MD5 outputs a 32 byte digest

typedef struct
{
BYTE data[64];
WORD datalen;
unsigned long long bitlen;
WORD state[4];
uint8_t data[64];
uint32_t datalen;
uint64_t bitlen;
uint32_t state[4];
} MD5_CTX;

/*********************** FUNCTION DECLARATIONS **********************/
void trantor_md5_init(MD5_CTX *ctx);
void trantor_md5_update(MD5_CTX *ctx, const BYTE data[], size_t len);
void trantor_md5_final(MD5_CTX *ctx, BYTE hash[]);
void trantor_md5_update(MD5_CTX *ctx, const uint8_t data[], size_t len);
void trantor_md5_final(MD5_CTX *ctx, uint8_t hash[]);

#endif // MD5_H
37 changes: 17 additions & 20 deletions trantor/utils/crypto/sha1.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ A million repetitions of "a"

#include <stdio.h>
#include <string.h>
#include <sys/types.h> /* for u_int*_t */
#if defined(__sun)
#include "solarisfixes.h"
#endif
Expand Down Expand Up @@ -114,17 +113,13 @@ A million repetitions of "a"

/* Hash a single 512-bit block. This is the core of the algorithm. */

#ifdef _WIN32
using u_int32_t = uint32_t;
#endif

void TrantorSHA1Transform(u_int32_t state[5], const unsigned char buffer[64])
void trantor_sha1_transform(uint32_t state[5], const unsigned char buffer[64])
{
u_int32_t a, b, c, d, e;
uint32_t a, b, c, d, e;
typedef union
{
unsigned char c[64];
u_int32_t l[16];
uint32_t l[16];
} CHAR64LONG16;
#ifdef SHA1HANDSOFF
CHAR64LONG16 block[1]; /* use array to appear as a pointer */
Expand Down Expand Up @@ -237,9 +232,9 @@ void TrantorSHA1Transform(u_int32_t state[5], const unsigned char buffer[64])
#endif
}

/* TrantorSHA1Init - Initialize new context */
/* trantor_sha1_init - Initialize new context */

void TrantorSHA1Init(SHA1_CTX* context)
void trantor_sha1_init(SHA1_CTX* context)
{
/* SHA1 initialization constants */
context->state[0] = 0x67452301;
Expand All @@ -252,7 +247,9 @@ void TrantorSHA1Init(SHA1_CTX* context)

/* Run your data through this. */

void TrantorSHA1Update(SHA1_CTX* context, const unsigned char* data, size_t len)
void trantor_sha1_update(SHA1_CTX* context,
const unsigned char* data,
size_t len)
{
size_t i;
size_t j;
Expand All @@ -265,10 +262,10 @@ void TrantorSHA1Update(SHA1_CTX* context, const unsigned char* data, size_t len)
if ((j + len) > 63)
{
memcpy(&context->buffer[j], data, (i = 64 - j));
TrantorSHA1Transform(context->state, context->buffer);
trantor_sha1_transform(context->state, context->buffer);
for (; i + 63 < len; i += 64)
{
TrantorSHA1Transform(context->state, &data[i]);
trantor_sha1_transform(context->state, &data[i]);
}
j = 0;
}
Expand All @@ -279,7 +276,7 @@ void TrantorSHA1Update(SHA1_CTX* context, const unsigned char* data, size_t len)

/* Add padding and return the message digest. */

void TrantorSHA1Final(unsigned char digest[20], SHA1_CTX* context)
void trantor_sha1_final(unsigned char digest[20], SHA1_CTX* context)
{
unsigned i;
unsigned char finalcount[8];
Expand All @@ -295,7 +292,7 @@ void TrantorSHA1Final(unsigned char digest[20], SHA1_CTX* context)

for (i = 0; i < 2; i++)
{
u_int32_t t = context->count[i];
uint32_t t = context->count[i];
int j;

for (j = 0; j < 4; t >>= 8, j++)
Expand All @@ -310,15 +307,15 @@ void TrantorSHA1Final(unsigned char digest[20], SHA1_CTX* context)
}
#endif
c = 0200;
TrantorSHA1Update(context, &c, 1);
trantor_sha1_update(context, &c, 1);
while ((context->count[0] & 504) != 448)
{
c = 0000;
TrantorSHA1Update(context, &c, 1);
trantor_sha1_update(context, &c, 1);
}
TrantorSHA1Update(context,
finalcount,
8); /* Should cause a TrantorSHA1Transform() */
trantor_sha1_update(context,
finalcount,
8); /* Should cause a TrantorSHA1Transform() */
for (i = 0; i < 20; i++)
{
digest[i] =
Expand Down
12 changes: 6 additions & 6 deletions trantor/utils/crypto/sha1.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ typedef struct
unsigned char buffer[64];
} SHA1_CTX;

void TrantorSHA1Transform(uint32_t state[5], const unsigned char buffer[64]);
void TrantorSHA1Init(SHA1_CTX* context);
void TrantorSHA1Update(SHA1_CTX* context,
const unsigned char* data,
size_t len);
void TrantorSHA1Final(unsigned char digest[20], SHA1_CTX* context);
void trantor_sha1_transform(uint32_t state[5], const unsigned char buffer[64]);
void trantor_sha1_init(SHA1_CTX* context);
void trantor_sha1_update(SHA1_CTX* context,
const unsigned char* data,
size_t len);
void trantor_sha1_final(unsigned char digest[20], SHA1_CTX* context);
30 changes: 15 additions & 15 deletions trantor/utils/crypto/sha256.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#define SIG1(x) (ROTRIGHT(x, 17) ^ ROTRIGHT(x, 19) ^ ((x) >> 10))

/**************************** VARIABLES *****************************/
static const unsigned int k[64] = {
static const uint32_t k[64] = {
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
Expand All @@ -43,9 +43,9 @@ static const unsigned int k[64] = {
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2};

/*********************** FUNCTION DEFINITIONS ***********************/
void trantor_sha256_transform(SHA256_CTX *ctx, const BYTE data[])
void trantor_sha256_transform(SHA256_CTX *ctx, const uint8_t data[])
{
WORD a, b, c, d, e, f, g, h, i, j, t1, t2, m[64];
uint32_t a, b, c, d, e, f, g, h, i, j, t1, t2, m[64];

for (i = 0, j = 0; i < 16; ++i, j += 4)
m[i] = (data[j] << 24) | (data[j + 1] << 16) | (data[j + 2] << 8) |
Expand Down Expand Up @@ -100,9 +100,9 @@ void trantor_sha256_init(SHA256_CTX *ctx)
ctx->state[7] = 0x5be0cd19;
}

void trantor_sha256_update(SHA256_CTX *ctx, const BYTE data[], size_t len)
void trantor_sha256_update(SHA256_CTX *ctx, const uint8_t data[], size_t len)
{
WORD i;
uint32_t i;

for (i = 0; i < len; ++i)
{
Expand All @@ -117,9 +117,9 @@ void trantor_sha256_update(SHA256_CTX *ctx, const BYTE data[], size_t len)
}
}

void trantor_sha256_final(SHA256_CTX *ctx, BYTE hash[])
void trantor_sha256_final(SHA256_CTX *ctx, uint8_t hash[])
{
WORD i;
uint32_t i;

i = ctx->datalen;

Expand All @@ -141,14 +141,14 @@ void trantor_sha256_final(SHA256_CTX *ctx, BYTE hash[])

// Append to the padding the total message's length in bits and transform.
ctx->bitlen += ctx->datalen * 8;
ctx->data[63] = ctx->bitlen;
ctx->data[62] = ctx->bitlen >> 8;
ctx->data[61] = ctx->bitlen >> 16;
ctx->data[60] = ctx->bitlen >> 24;
ctx->data[59] = ctx->bitlen >> 32;
ctx->data[58] = ctx->bitlen >> 40;
ctx->data[57] = ctx->bitlen >> 48;
ctx->data[56] = ctx->bitlen >> 56;
ctx->data[63] = (uint8_t)ctx->bitlen;
ctx->data[62] = (uint8_t)(ctx->bitlen >> 8);
ctx->data[61] = (uint8_t)(ctx->bitlen >> 16);
ctx->data[60] = (uint8_t)(ctx->bitlen >> 24);
ctx->data[59] = (uint8_t)(ctx->bitlen >> 32);
ctx->data[58] = (uint8_t)(ctx->bitlen >> 40);
ctx->data[57] = (uint8_t)(ctx->bitlen >> 48);
ctx->data[56] = (uint8_t)(ctx->bitlen >> 56);
trantor_sha256_transform(ctx, ctx->data);

// Since this implementation uses little endian byte ordering and SHA uses
Expand Down
25 changes: 7 additions & 18 deletions trantor/utils/crypto/sha256.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,22 @@
#define SHA256_H

/*************************** HEADER FILES ***************************/
#ifndef _WIN32
#include <stddef.h>
#else
#include <windows.h>
#endif
#include <stdint.h>

/****************************** MACROS ******************************/
#define SHA256_BLOCK_SIZE 32 // SHA256 outputs a 32 byte digest

/**************************** DATA TYPES ****************************/

#ifndef _WIN32
typedef unsigned char BYTE; // 8-bit byte
typedef unsigned int WORD; // 32-bit word, change to "long" for 16-bit machines
#endif

typedef struct
{
BYTE data[64];
WORD datalen;
unsigned long long bitlen;
WORD state[8];
uint8_t data[64];
uint32_t datalen;
uint64_t bitlen;
uint32_t state[8];
} SHA256_CTX;

/*********************** FUNCTION DECLARATIONS **********************/
void trantor_sha256_init(SHA256_CTX *ctx);
void trantor_sha256_update(SHA256_CTX *ctx, const BYTE data[], size_t len);
void trantor_sha256_final(SHA256_CTX *ctx, BYTE hash[]);
void trantor_sha256_update(SHA256_CTX *ctx, const uint8_t data[], size_t len);
void trantor_sha256_final(SHA256_CTX *ctx, uint8_t hash[]);

#endif // SHA256_H
14 changes: 7 additions & 7 deletions trantor/utils/crypto/sha3.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ typedef struct
} sha3_ctx_t;

// Compression function.
void trnator_sha3_keccakf(uint64_t st[25]);
void trantor_sha3_keccakf(uint64_t st[25]);

// OpenSSL - like interfece
// OpenSSL - like interface
int trantor_sha3_init(sha3_ctx_t *c,
int mdlen); // mdlen = hash output in bytes
int trantor_sha3_update(sha3_ctx_t *c, const void *data, size_t len);
Expand All @@ -39,11 +39,11 @@ int trantor_sha3_final(void *md, sha3_ctx_t *c); // digest goes to md
void *trantor_sha3(const void *in, size_t inlen, void *md, int mdlen);

// SHAKE128 and SHAKE256 extensible-output functions
#define trantor_shake128_init(c) trnator_sha3_init(c, 16)
#define trantor_shake256_init(c) trnator_sha3_init(c, 32)
#define trantor_shake_update trnator_sha3_update
#define trantor_shake128_init(c) trantor_sha3_init(c, 16)
#define trantor_shake256_init(c) trantor_sha3_init(c, 32)
#define trantor_shake_update trantor_sha3_update

void trnator_shake_xof(sha3_ctx_t *c);
void trnator_shake_out(sha3_ctx_t *c, void *out, size_t len);
void trantor_shake_xof(sha3_ctx_t *c);
void trantor_shake_out(sha3_ctx_t *c, void *out, size_t len);

#endif