Skip to content

Commit b0bd12b

Browse files
author
cameronrich
committed
* Added SHA384 and SHA512 digests.
git-svn-id: svn://svn.code.sf.net/p/axtls/code/trunk@245 9a5d90b5-6617-0410-8a86-bb477d3ed2e3
1 parent 0d334d8 commit b0bd12b

File tree

14 files changed

+619
-45
lines changed

14 files changed

+619
-45
lines changed

crypto/Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ OBJ=\
4242
rc4.o \
4343
rsa.o \
4444
sha1.o \
45-
sha256.o
45+
sha256.o \
46+
sha384.o \
47+
sha512.o
4648

4749
include ../config/makefile.post
4850

crypto/crypto.h

Lines changed: 39 additions & 2 deletions
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
*
@@ -137,7 +137,44 @@ typedef struct
137137

138138
void SHA256_Init(SHA256_CTX *c);
139139
void SHA256_Update(SHA256_CTX *, const uint8_t *input, int len);
140-
void SHA256_Final(uint8_t digest[32], SHA256_CTX *);
140+
void SHA256_Final(uint8_t *digest, SHA256_CTX *);
141+
142+
/**************************************************************************
143+
* SHA512 declarations
144+
**************************************************************************/
145+
146+
#define SHA512_SIZE 64
147+
148+
typedef struct
149+
{
150+
union
151+
{
152+
uint64_t h[8];
153+
uint8_t digest[64];
154+
};
155+
union
156+
{
157+
uint64_t w[80];
158+
uint8_t buffer[128];
159+
};
160+
size_t size;
161+
uint64_t totalSize;
162+
} SHA512_CTX;
163+
164+
void SHA512_Init(SHA512_CTX *c);
165+
void SHA512_Update(SHA512_CTX *, const uint8_t *input, int len);
166+
void SHA512_Final(uint8_t *digest, SHA512_CTX *);
167+
168+
/**************************************************************************
169+
* SHA384 declarations
170+
**************************************************************************/
171+
172+
#define SHA384_SIZE 48
173+
174+
typedef SHA512_CTX SHA384_CTX;
175+
void SHA384_Init(SHA384_CTX *c);
176+
void SHA384_Update(SHA384_CTX *, const uint8_t *input, int len);
177+
void SHA384_Final(uint8_t *digest, SHA384_CTX *);
141178

142179
/**************************************************************************
143180
* MD5 declarations

crypto/os_int.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ typedef INT64 int64_t;
5656
#include <inttypes.h>
5757
#else
5858
#include <stdint.h>
59+
#include <endian.h>
5960
#endif /* Not Solaris */
6061

6162
#endif /* Not Win32 */

crypto/sha256.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2001-2003 Christophe Devine
2+
* Copyright (c) 2015, Cameron Rich
33
*
44
* All rights reserved.
55
*
@@ -74,7 +74,7 @@ void SHA256_Init(SHA256_CTX *ctx)
7474
ctx->state[7] = 0x5BE0CD19;
7575
}
7676

77-
void SHA256_Process(const uint8_t digest[64], SHA256_CTX *ctx)
77+
static void SHA256_Process(const uint8_t digest[64], SHA256_CTX *ctx)
7878
{
7979
uint32_t temp1, temp2, W[64];
8080
uint32_t A, B, C, D, E, F, G, H;

crypto/sha384.c

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright (c) 2015, Cameron Rich
3+
*
4+
* All rights reserved.
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions are met:
8+
*
9+
* * Redistributions of source code must retain the above copyright notice,
10+
* this list of conditions and the following disclaimer.
11+
* * Redistributions in binary form must reproduce the above copyright notice,
12+
* this list of conditions and the following disclaimer in the documentation
13+
* and/or other materials provided with the distribution.
14+
* * Neither the name of the axTLS project nor the names of its contributors
15+
* may be used to endorse or promote products derived from this software
16+
* without specific prior written permission.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
31+
#include "os_port.h"
32+
#include "crypto.h"
33+
34+
/**
35+
* Initialize the SHA384 context
36+
*/
37+
void SHA384_Init(SHA384_CTX *ctx)
38+
{
39+
//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;
48+
49+
// Number of bytes in the buffer
50+
ctx->size = 0;
51+
// Total length of the message
52+
ctx->totalSize = 0;
53+
}
54+
55+
/**
56+
* Accepts an array of octets as the next portion of the message.
57+
*/
58+
void SHA384_Update(SHA384_CTX *ctx, const uint8_t * msg, int len)
59+
{
60+
// The function is defined in the exact same manner as SHA-512
61+
SHA512_Update(ctx, msg, len);
62+
}
63+
64+
/**
65+
* Return the 384-bit message digest into the user's array
66+
*/
67+
void SHA384_Final(uint8_t *digest, SHA384_CTX *ctx)
68+
{
69+
// The function is defined in the exact same manner as SHA-512
70+
SHA512_Final(NULL, ctx);
71+
72+
// Copy the resulting digest
73+
if (digest != NULL)
74+
memcpy(digest, ctx->digest, SHA384_SIZE);
75+
}
76+

crypto/sha512.c

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
/*
2+
* Copyright (c) 2015, Cameron Rich
3+
*
4+
* All rights reserved.
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions are met:
8+
*
9+
* * Redistributions of source code must retain the above copyright notice,
10+
* this list of conditions and the following disclaimer.
11+
* * Redistributions in binary form must reproduce the above copyright notice,
12+
* this list of conditions and the following disclaimer in the documentation
13+
* and/or other materials provided with the distribution.
14+
* * Neither the name of the axTLS project nor the names of its contributors
15+
* may be used to endorse or promote products derived from this software
16+
* without specific prior written permission.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
31+
#include <string.h>
32+
#include "os_port.h"
33+
#include "crypto.h"
34+
35+
#define SHR64(a, n) ((a) >> (n))
36+
#define ROR64(a, n) (((a) >> (n)) | ((a) << (64 - (n))))
37+
#define CH(x, y, z) (((x) & (y)) | (~(x) & (z)))
38+
#define MAJ(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
39+
#define SIGMA1(x) (ROR64(x, 28) ^ ROR64(x, 34) ^ ROR64(x, 39))
40+
#define SIGMA2(x) (ROR64(x, 14) ^ ROR64(x, 18) ^ ROR64(x, 41))
41+
#define SIGMA3(x) (ROR64(x, 1) ^ ROR64(x, 8) ^ SHR64(x, 7))
42+
#define SIGMA4(x) (ROR64(x, 19) ^ ROR64(x, 61) ^ SHR64(x, 6))
43+
#define MIN(x, y) ((x) < (y) ? x : y)
44+
45+
static const uint8_t padding[128] =
46+
{
47+
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
48+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
49+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
50+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
51+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
52+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
53+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
54+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
55+
};
56+
57+
static const uint64_t k[80] =
58+
{
59+
0x428A2F98D728AE22, 0x7137449123EF65CD, 0xB5C0FBCFEC4D3B2F, 0xE9B5DBA58189DBBC,
60+
0x3956C25BF348B538, 0x59F111F1B605D019, 0x923F82A4AF194F9B, 0xAB1C5ED5DA6D8118,
61+
0xD807AA98A3030242, 0x12835B0145706FBE, 0x243185BE4EE4B28C, 0x550C7DC3D5FFB4E2,
62+
0x72BE5D74F27B896F, 0x80DEB1FE3B1696B1, 0x9BDC06A725C71235, 0xC19BF174CF692694,
63+
0xE49B69C19EF14AD2, 0xEFBE4786384F25E3, 0x0FC19DC68B8CD5B5, 0x240CA1CC77AC9C65,
64+
0x2DE92C6F592B0275, 0x4A7484AA6EA6E483, 0x5CB0A9DCBD41FBD4, 0x76F988DA831153B5,
65+
0x983E5152EE66DFAB, 0xA831C66D2DB43210, 0xB00327C898FB213F, 0xBF597FC7BEEF0EE4,
66+
0xC6E00BF33DA88FC2, 0xD5A79147930AA725, 0x06CA6351E003826F, 0x142929670A0E6E70,
67+
0x27B70A8546D22FFC, 0x2E1B21385C26C926, 0x4D2C6DFC5AC42AED, 0x53380D139D95B3DF,
68+
0x650A73548BAF63DE, 0x766A0ABB3C77B2A8, 0x81C2C92E47EDAEE6, 0x92722C851482353B,
69+
0xA2BFE8A14CF10364, 0xA81A664BBC423001, 0xC24B8B70D0F89791, 0xC76C51A30654BE30,
70+
0xD192E819D6EF5218, 0xD69906245565A910, 0xF40E35855771202A, 0x106AA07032BBD1B8,
71+
0x19A4C116B8D2D0C8, 0x1E376C085141AB53, 0x2748774CDF8EEB99, 0x34B0BCB5E19B48A8,
72+
0x391C0CB3C5C95A63, 0x4ED8AA4AE3418ACB, 0x5B9CCA4F7763E373, 0x682E6FF3D6B2B8A3,
73+
0x748F82EE5DEFB2FC, 0x78A5636F43172F60, 0x84C87814A1F0AB72, 0x8CC702081A6439EC,
74+
0x90BEFFFA23631E28, 0xA4506CEBDE82BDE9, 0xBEF9A3F7B2C67915, 0xC67178F2E372532B,
75+
0xCA273ECEEA26619C, 0xD186B8C721C0C207, 0xEADA7DD6CDE0EB1E, 0xF57D4F7FEE6ED178,
76+
0x06F067AA72176FBA, 0x0A637DC5A2C898A6, 0x113F9804BEF90DAE, 0x1B710B35131C471B,
77+
0x28DB77F523047D84, 0x32CAAB7B40C72493, 0x3C9EBE0A15C9BEBC, 0x431D67C49C100D4C,
78+
0x4CC5D4BECB3E42B6, 0x597F299CFC657E2A, 0x5FCB6FAB3AD6FAEC, 0x6C44198C4A475817
79+
};
80+
81+
/**
82+
* Initialize the SHA512 context
83+
*/
84+
void SHA512_Init(SHA512_CTX *ctx)
85+
{
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;
94+
ctx->size = 0;
95+
ctx->totalSize = 0;
96+
}
97+
98+
static void SHA512_Process(SHA512_CTX *ctx)
99+
{
100+
int t;
101+
uint64_t temp1;
102+
uint64_t temp2;
103+
104+
// 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];
113+
114+
// Process message in 16-word blocks
115+
uint64_t *w = ctx->w;
116+
117+
// Convert from big-endian byte order to host byte order
118+
for (t = 0; t < 16; t++)
119+
w[t] = be64toh(w[t]);
120+
121+
// Prepare the message schedule
122+
for (t = 16; t < 80; t++)
123+
w[t] = SIGMA4(w[t - 2]) + w[t - 7] + SIGMA3(w[t - 15]) + w[t - 16];
124+
125+
// SHA-512 hash computation
126+
for (t = 0; t < 80; t++)
127+
{
128+
// Calculate T1 and T2
129+
temp1 = h + SIGMA2(e) + CH(e, f, g) + k[t] + w[t];
130+
temp2 = SIGMA1(a) + MAJ(a, b, c);
131+
132+
// Update the working registers
133+
h = g;
134+
g = f;
135+
f = e;
136+
e = d + temp1;
137+
d = c;
138+
c = b;
139+
b = a;
140+
a = temp1 + temp2;
141+
}
142+
143+
// 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;
152+
}
153+
154+
/**
155+
* Accepts an array of octets as the next portion of the message.
156+
*/
157+
void SHA512_Update(SHA512_CTX *ctx, const uint8_t * msg, int len)
158+
{
159+
// Process the incoming data
160+
while (len > 0)
161+
{
162+
// The buffer can hold at most 128 bytes
163+
size_t n = MIN(len, 128 - ctx->size);
164+
165+
// Copy the data to the buffer
166+
memcpy(ctx->buffer + ctx->size, msg, n);
167+
168+
// Update the SHA-512 ctx
169+
ctx->size += n;
170+
ctx->totalSize += n;
171+
// Advance the data pointer
172+
msg = (uint8_t *) msg + n;
173+
// Remaining bytes to process
174+
len -= n;
175+
176+
// Process message in 16-word blocks
177+
if (ctx->size == 128)
178+
{
179+
// Transform the 16-word block
180+
SHA512_Process(ctx);
181+
// Empty the buffer
182+
ctx->size = 0;
183+
}
184+
}
185+
}
186+
187+
/**
188+
* Return the 512-bit message digest into the user's array
189+
*/
190+
void SHA512_Final(uint8_t *digest, SHA512_CTX *ctx)
191+
{
192+
int i;
193+
size_t paddingSize;
194+
uint64_t totalSize;
195+
196+
// Length of the original message (before padding)
197+
totalSize = ctx->totalSize * 8;
198+
199+
// Pad the message so that its length is congruent to 112 modulo 128
200+
paddingSize = (ctx->size < 112) ? (112 - ctx->size) :
201+
(128 + 112 - ctx->size);
202+
// Append padding
203+
SHA512_Update(ctx, padding, paddingSize);
204+
205+
// Append the length of the original message
206+
ctx->w[14] = 0;
207+
ctx->w[15] = be64toh(totalSize);
208+
209+
// Calculate the message digest
210+
SHA512_Process(ctx);
211+
212+
// Convert from host byte order to big-endian byte order
213+
for (i = 0; i < 8; i++)
214+
ctx->h[i] = be64toh(ctx->h[i]);
215+
216+
// Copy the resulting digest
217+
if (digest != NULL)
218+
memcpy(digest, ctx->digest, SHA512_SIZE);
219+
}
220+

ssl/Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ CRYPTO_OBJ=\
7373
$(CRYPTO_PATH)rc4.o \
7474
$(CRYPTO_PATH)rsa.o \
7575
$(CRYPTO_PATH)sha1.o \
76-
$(CRYPTO_PATH)sha256.o
76+
$(CRYPTO_PATH)sha256.o \
77+
$(CRYPTO_PATH)sha384.o \
78+
$(CRYPTO_PATH)sha512.o
7779

7880
OBJ=\
7981
asn1.o \

0 commit comments

Comments
 (0)