Skip to content

Commit acf35f0

Browse files
author
cameronrich
committed
* 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
1 parent a88fd94 commit acf35f0

File tree

7 files changed

+54
-45
lines changed

7 files changed

+54
-45
lines changed

config/makefile.conf

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# Copyright (c) 2007, Cameron Rich
2+
# Copyright (c) 2007-2015, Cameron Rich
33
#
44
# All rights reserved.
55
#
@@ -100,6 +100,7 @@ LDSHARED = -shared
100100

101101
# Linux
102102
ifndef CONFIG_PLATFORM_CYGWIN
103+
ifndef CONFIG_PLATFORM_NOMMU
103104
CFLAGS += -fPIC
104105

105106
# Cygwin
@@ -108,6 +109,7 @@ CFLAGS += -DCONFIG_PLATFORM_CYGWIN
108109
LDFLAGS += -enable-auto-import
109110
endif
110111
endif
112+
endif
111113

112114
ifdef CONFIG_DEBUG
113115
CFLAGS += -g

crypto/crypto.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,12 @@ typedef struct
151151
{
152152
uint64_t h[8];
153153
uint8_t digest[64];
154-
};
154+
} h_dig;
155155
union
156156
{
157157
uint64_t w[80];
158158
uint8_t buffer[128];
159-
};
159+
} w_buf;
160160
size_t size;
161161
uint64_t totalSize;
162162
} SHA512_CTX;

crypto/sha384.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2929
*/
3030

31+
#include <string.h>
3132
#include "os_port.h"
3233
#include "crypto.h"
3334

@@ -37,14 +38,14 @@
3738
void SHA384_Init(SHA384_CTX *ctx)
3839
{
3940
//Set initial hash value
40-
ctx->h[0] = 0xCBBB9D5DC1059ED8;
41-
ctx->h[1] = 0x629A292A367CD507;
42-
ctx->h[2] = 0x9159015A3070DD17;
43-
ctx->h[3] = 0x152FECD8F70E5939;
44-
ctx->h[4] = 0x67332667FFC00B31;
45-
ctx->h[5] = 0x8EB44A8768581511;
46-
ctx->h[6] = 0xDB0C2E0D64F98FA7;
47-
ctx->h[7] = 0x47B5481DBEFA4FA4;
41+
ctx->h_dig.h[0] = 0xCBBB9D5DC1059ED8;
42+
ctx->h_dig.h[1] = 0x629A292A367CD507;
43+
ctx->h_dig.h[2] = 0x9159015A3070DD17;
44+
ctx->h_dig.h[3] = 0x152FECD8F70E5939;
45+
ctx->h_dig.h[4] = 0x67332667FFC00B31;
46+
ctx->h_dig.h[5] = 0x8EB44A8768581511;
47+
ctx->h_dig.h[6] = 0xDB0C2E0D64F98FA7;
48+
ctx->h_dig.h[7] = 0x47B5481DBEFA4FA4;
4849

4950
// Number of bytes in the buffer
5051
ctx->size = 0;
@@ -71,6 +72,6 @@ void SHA384_Final(uint8_t *digest, SHA384_CTX *ctx)
7172

7273
// Copy the resulting digest
7374
if (digest != NULL)
74-
memcpy(digest, ctx->digest, SHA384_SIZE);
75+
memcpy(digest, ctx->h_dig.digest, SHA384_SIZE);
7576
}
7677

crypto/sha512.c

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,14 @@ static const uint64_t k[80] =
8383
*/
8484
void SHA512_Init(SHA512_CTX *ctx)
8585
{
86-
ctx->h[0] = 0x6A09E667F3BCC908;
87-
ctx->h[1] = 0xBB67AE8584CAA73B;
88-
ctx->h[2] = 0x3C6EF372FE94F82B;
89-
ctx->h[3] = 0xA54FF53A5F1D36F1;
90-
ctx->h[4] = 0x510E527FADE682D1;
91-
ctx->h[5] = 0x9B05688C2B3E6C1F;
92-
ctx->h[6] = 0x1F83D9ABFB41BD6B;
93-
ctx->h[7] = 0x5BE0CD19137E2179;
86+
ctx->h_dig.h[0] = 0x6A09E667F3BCC908;
87+
ctx->h_dig.h[1] = 0xBB67AE8584CAA73B;
88+
ctx->h_dig.h[2] = 0x3C6EF372FE94F82B;
89+
ctx->h_dig.h[3] = 0xA54FF53A5F1D36F1;
90+
ctx->h_dig.h[4] = 0x510E527FADE682D1;
91+
ctx->h_dig.h[5] = 0x9B05688C2B3E6C1F;
92+
ctx->h_dig.h[6] = 0x1F83D9ABFB41BD6B;
93+
ctx->h_dig.h[7] = 0x5BE0CD19137E2179;
9494
ctx->size = 0;
9595
ctx->totalSize = 0;
9696
}
@@ -102,22 +102,22 @@ static void SHA512_Process(SHA512_CTX *ctx)
102102
uint64_t temp2;
103103

104104
// Initialize the 8 working registers
105-
uint64_t a = ctx->h[0];
106-
uint64_t b = ctx->h[1];
107-
uint64_t c = ctx->h[2];
108-
uint64_t d = ctx->h[3];
109-
uint64_t e = ctx->h[4];
110-
uint64_t f = ctx->h[5];
111-
uint64_t g = ctx->h[6];
112-
uint64_t h = ctx->h[7];
105+
uint64_t a = ctx->h_dig.h[0];
106+
uint64_t b = ctx->h_dig.h[1];
107+
uint64_t c = ctx->h_dig.h[2];
108+
uint64_t d = ctx->h_dig.h[3];
109+
uint64_t e = ctx->h_dig.h[4];
110+
uint64_t f = ctx->h_dig.h[5];
111+
uint64_t g = ctx->h_dig.h[6];
112+
uint64_t h = ctx->h_dig.h[7];
113113

114114
// Process message in 16-word blocks
115-
uint64_t *w = ctx->w;
115+
uint64_t *w = ctx->w_buf.w;
116116

117117
// Convert from big-endian byte order to host byte order
118118
for (t = 0; t < 16; t++)
119119
w[t] = be64toh(w[t]);
120-
120+
121121
// Prepare the message schedule
122122
for (t = 16; t < 80; t++)
123123
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)
141141
}
142142

143143
// Update the hash value
144-
ctx->h[0] += a;
145-
ctx->h[1] += b;
146-
ctx->h[2] += c;
147-
ctx->h[3] += d;
148-
ctx->h[4] += e;
149-
ctx->h[5] += f;
150-
ctx->h[6] += g;
151-
ctx->h[7] += h;
144+
ctx->h_dig.h[0] += a;
145+
ctx->h_dig.h[1] += b;
146+
ctx->h_dig.h[2] += c;
147+
ctx->h_dig.h[3] += d;
148+
ctx->h_dig.h[4] += e;
149+
ctx->h_dig.h[5] += f;
150+
ctx->h_dig.h[6] += g;
151+
ctx->h_dig.h[7] += h;
152152
}
153153

154154
/**
@@ -163,7 +163,7 @@ void SHA512_Update(SHA512_CTX *ctx, const uint8_t * msg, int len)
163163
size_t n = MIN(len, 128 - ctx->size);
164164

165165
// Copy the data to the buffer
166-
memcpy(ctx->buffer + ctx->size, msg, n);
166+
memcpy(ctx->w_buf.buffer + ctx->size, msg, n);
167167

168168
// Update the SHA-512 ctx
169169
ctx->size += n;
@@ -203,18 +203,18 @@ void SHA512_Final(uint8_t *digest, SHA512_CTX *ctx)
203203
SHA512_Update(ctx, padding, paddingSize);
204204

205205
// Append the length of the original message
206-
ctx->w[14] = 0;
207-
ctx->w[15] = be64toh(totalSize);
206+
ctx->w_buf.w[14] = 0;
207+
ctx->w_buf.w[15] = be64toh(totalSize);
208208

209209
// Calculate the message digest
210210
SHA512_Process(ctx);
211211

212212
// Convert from host byte order to big-endian byte order
213213
for (i = 0; i < 8; i++)
214-
ctx->h[i] = be64toh(ctx->h[i]);
214+
ctx->h_dig.h[i] = be64toh(ctx->h_dig.h[i]);
215215

216216
// Copy the resulting digest
217217
if (digest != NULL)
218-
memcpy(digest, ctx->digest, SHA512_SIZE);
218+
memcpy(digest, ctx->h_dig.digest, SHA512_SIZE);
219219
}
220220

ssl/os_port.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2007-2014, Cameron Rich
2+
* Copyright (c) 2007-2015, Cameron Rich
33
*
44
* All rights reserved.
55
*
@@ -136,12 +136,17 @@ EXP_FUNC int STDCALL getdomainname(char *buf, int buf_size);
136136
#include <sys/wait.h>
137137
#include <netinet/in.h>
138138
#include <arpa/inet.h>
139+
#include <asm/byteorder.h>
139140

140141
#define SOCKET_READ(A,B,C) read(A,B,C)
141142
#define SOCKET_WRITE(A,B,C) write(A,B,C)
142143
#define SOCKET_CLOSE(A) if (A >= 0) close(A)
143144
#define TTY_FLUSH()
144145

146+
#ifndef be64toh
147+
#define be64toh(x) __be64_to_cpu(x)
148+
#endif
149+
145150
#endif /* Not Win32 */
146151

147152
/* some functions to mutate the way these work */

ssl/tls1.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ extern "C" {
4141
#endif
4242

4343
#include "version.h"
44+
#include "config.h"
4445
#include "os_int.h"
4546
#include "crypto.h"
4647
#include "crypto_misc.h"

www/index.html

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)