1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-09 03:41:41 +03:00

* Added named unions in SHA256 code for compilers that don't support it.

* Some other porting suggestions from Chris Ghormley.

git-svn-id: svn://svn.code.sf.net/p/axtls/code/trunk@248 9a5d90b5-6617-0410-8a86-bb477d3ed2e3
This commit is contained in:
cameronrich 2015-04-30 06:06:09 +00:00
parent a88fd947b2
commit acf35f0ea7
7 changed files with 54 additions and 45 deletions

View File

@ -1,5 +1,5 @@
# #
# Copyright (c) 2007, Cameron Rich # Copyright (c) 2007-2015, Cameron Rich
# #
# All rights reserved. # All rights reserved.
# #
@ -100,6 +100,7 @@ LDSHARED = -shared
# Linux # Linux
ifndef CONFIG_PLATFORM_CYGWIN ifndef CONFIG_PLATFORM_CYGWIN
ifndef CONFIG_PLATFORM_NOMMU
CFLAGS += -fPIC CFLAGS += -fPIC
# Cygwin # Cygwin
@ -108,6 +109,7 @@ CFLAGS += -DCONFIG_PLATFORM_CYGWIN
LDFLAGS += -enable-auto-import LDFLAGS += -enable-auto-import
endif endif
endif endif
endif
ifdef CONFIG_DEBUG ifdef CONFIG_DEBUG
CFLAGS += -g CFLAGS += -g

View File

@ -151,12 +151,12 @@ typedef struct
{ {
uint64_t h[8]; uint64_t h[8];
uint8_t digest[64]; uint8_t digest[64];
}; } h_dig;
union union
{ {
uint64_t w[80]; uint64_t w[80];
uint8_t buffer[128]; uint8_t buffer[128];
}; } w_buf;
size_t size; size_t size;
uint64_t totalSize; uint64_t totalSize;
} SHA512_CTX; } SHA512_CTX;

View File

@ -28,6 +28,7 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#include <string.h>
#include "os_port.h" #include "os_port.h"
#include "crypto.h" #include "crypto.h"
@ -37,14 +38,14 @@
void SHA384_Init(SHA384_CTX *ctx) void SHA384_Init(SHA384_CTX *ctx)
{ {
//Set initial hash value //Set initial hash value
ctx->h[0] = 0xCBBB9D5DC1059ED8; ctx->h_dig.h[0] = 0xCBBB9D5DC1059ED8;
ctx->h[1] = 0x629A292A367CD507; ctx->h_dig.h[1] = 0x629A292A367CD507;
ctx->h[2] = 0x9159015A3070DD17; ctx->h_dig.h[2] = 0x9159015A3070DD17;
ctx->h[3] = 0x152FECD8F70E5939; ctx->h_dig.h[3] = 0x152FECD8F70E5939;
ctx->h[4] = 0x67332667FFC00B31; ctx->h_dig.h[4] = 0x67332667FFC00B31;
ctx->h[5] = 0x8EB44A8768581511; ctx->h_dig.h[5] = 0x8EB44A8768581511;
ctx->h[6] = 0xDB0C2E0D64F98FA7; ctx->h_dig.h[6] = 0xDB0C2E0D64F98FA7;
ctx->h[7] = 0x47B5481DBEFA4FA4; ctx->h_dig.h[7] = 0x47B5481DBEFA4FA4;
// Number of bytes in the buffer // Number of bytes in the buffer
ctx->size = 0; ctx->size = 0;
@ -71,6 +72,6 @@ void SHA384_Final(uint8_t *digest, SHA384_CTX *ctx)
// Copy the resulting digest // Copy the resulting digest
if (digest != NULL) if (digest != NULL)
memcpy(digest, ctx->digest, SHA384_SIZE); memcpy(digest, ctx->h_dig.digest, SHA384_SIZE);
} }

View File

@ -83,14 +83,14 @@ static const uint64_t k[80] =
*/ */
void SHA512_Init(SHA512_CTX *ctx) void SHA512_Init(SHA512_CTX *ctx)
{ {
ctx->h[0] = 0x6A09E667F3BCC908; ctx->h_dig.h[0] = 0x6A09E667F3BCC908;
ctx->h[1] = 0xBB67AE8584CAA73B; ctx->h_dig.h[1] = 0xBB67AE8584CAA73B;
ctx->h[2] = 0x3C6EF372FE94F82B; ctx->h_dig.h[2] = 0x3C6EF372FE94F82B;
ctx->h[3] = 0xA54FF53A5F1D36F1; ctx->h_dig.h[3] = 0xA54FF53A5F1D36F1;
ctx->h[4] = 0x510E527FADE682D1; ctx->h_dig.h[4] = 0x510E527FADE682D1;
ctx->h[5] = 0x9B05688C2B3E6C1F; ctx->h_dig.h[5] = 0x9B05688C2B3E6C1F;
ctx->h[6] = 0x1F83D9ABFB41BD6B; ctx->h_dig.h[6] = 0x1F83D9ABFB41BD6B;
ctx->h[7] = 0x5BE0CD19137E2179; ctx->h_dig.h[7] = 0x5BE0CD19137E2179;
ctx->size = 0; ctx->size = 0;
ctx->totalSize = 0; ctx->totalSize = 0;
} }
@ -102,22 +102,22 @@ static void SHA512_Process(SHA512_CTX *ctx)
uint64_t temp2; uint64_t temp2;
// Initialize the 8 working registers // Initialize the 8 working registers
uint64_t a = ctx->h[0]; uint64_t a = ctx->h_dig.h[0];
uint64_t b = ctx->h[1]; uint64_t b = ctx->h_dig.h[1];
uint64_t c = ctx->h[2]; uint64_t c = ctx->h_dig.h[2];
uint64_t d = ctx->h[3]; uint64_t d = ctx->h_dig.h[3];
uint64_t e = ctx->h[4]; uint64_t e = ctx->h_dig.h[4];
uint64_t f = ctx->h[5]; uint64_t f = ctx->h_dig.h[5];
uint64_t g = ctx->h[6]; uint64_t g = ctx->h_dig.h[6];
uint64_t h = ctx->h[7]; uint64_t h = ctx->h_dig.h[7];
// Process message in 16-word blocks // Process message in 16-word blocks
uint64_t *w = ctx->w; uint64_t *w = ctx->w_buf.w;
// Convert from big-endian byte order to host byte order // Convert from big-endian byte order to host byte order
for (t = 0; t < 16; t++) for (t = 0; t < 16; t++)
w[t] = be64toh(w[t]); w[t] = be64toh(w[t]);
// Prepare the message schedule // Prepare the message schedule
for (t = 16; t < 80; t++) for (t = 16; t < 80; t++)
w[t] = SIGMA4(w[t - 2]) + w[t - 7] + SIGMA3(w[t - 15]) + w[t - 16]; w[t] = SIGMA4(w[t - 2]) + w[t - 7] + SIGMA3(w[t - 15]) + w[t - 16];
@ -141,14 +141,14 @@ static void SHA512_Process(SHA512_CTX *ctx)
} }
// Update the hash value // Update the hash value
ctx->h[0] += a; ctx->h_dig.h[0] += a;
ctx->h[1] += b; ctx->h_dig.h[1] += b;
ctx->h[2] += c; ctx->h_dig.h[2] += c;
ctx->h[3] += d; ctx->h_dig.h[3] += d;
ctx->h[4] += e; ctx->h_dig.h[4] += e;
ctx->h[5] += f; ctx->h_dig.h[5] += f;
ctx->h[6] += g; ctx->h_dig.h[6] += g;
ctx->h[7] += h; ctx->h_dig.h[7] += h;
} }
/** /**
@ -163,7 +163,7 @@ void SHA512_Update(SHA512_CTX *ctx, const uint8_t * msg, int len)
size_t n = MIN(len, 128 - ctx->size); size_t n = MIN(len, 128 - ctx->size);
// Copy the data to the buffer // Copy the data to the buffer
memcpy(ctx->buffer + ctx->size, msg, n); memcpy(ctx->w_buf.buffer + ctx->size, msg, n);
// Update the SHA-512 ctx // Update the SHA-512 ctx
ctx->size += n; ctx->size += n;
@ -203,18 +203,18 @@ void SHA512_Final(uint8_t *digest, SHA512_CTX *ctx)
SHA512_Update(ctx, padding, paddingSize); SHA512_Update(ctx, padding, paddingSize);
// Append the length of the original message // Append the length of the original message
ctx->w[14] = 0; ctx->w_buf.w[14] = 0;
ctx->w[15] = be64toh(totalSize); ctx->w_buf.w[15] = be64toh(totalSize);
// Calculate the message digest // Calculate the message digest
SHA512_Process(ctx); SHA512_Process(ctx);
// Convert from host byte order to big-endian byte order // Convert from host byte order to big-endian byte order
for (i = 0; i < 8; i++) for (i = 0; i < 8; i++)
ctx->h[i] = be64toh(ctx->h[i]); ctx->h_dig.h[i] = be64toh(ctx->h_dig.h[i]);
// Copy the resulting digest // Copy the resulting digest
if (digest != NULL) if (digest != NULL)
memcpy(digest, ctx->digest, SHA512_SIZE); memcpy(digest, ctx->h_dig.digest, SHA512_SIZE);
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2007-2014, Cameron Rich * Copyright (c) 2007-2015, Cameron Rich
* *
* All rights reserved. * All rights reserved.
* *
@ -136,12 +136,17 @@ EXP_FUNC int STDCALL getdomainname(char *buf, int buf_size);
#include <sys/wait.h> #include <sys/wait.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <asm/byteorder.h>
#define SOCKET_READ(A,B,C) read(A,B,C) #define SOCKET_READ(A,B,C) read(A,B,C)
#define SOCKET_WRITE(A,B,C) write(A,B,C) #define SOCKET_WRITE(A,B,C) write(A,B,C)
#define SOCKET_CLOSE(A) if (A >= 0) close(A) #define SOCKET_CLOSE(A) if (A >= 0) close(A)
#define TTY_FLUSH() #define TTY_FLUSH()
#ifndef be64toh
#define be64toh(x) __be64_to_cpu(x)
#endif
#endif /* Not Win32 */ #endif /* Not Win32 */
/* some functions to mutate the way these work */ /* some functions to mutate the way these work */

View File

@ -41,6 +41,7 @@ extern "C" {
#endif #endif
#include "version.h" #include "version.h"
#include "config.h"
#include "os_int.h" #include "os_int.h"
#include "crypto.h" #include "crypto.h"
#include "crypto_misc.h" #include "crypto_misc.h"

File diff suppressed because one or more lines are too long