Skip to content

Commit 32711a2

Browse files
committed
core: crypto - replace the sha1 impmlementation
1 parent 22d7536 commit 32711a2

File tree

2 files changed

+119
-193
lines changed

2 files changed

+119
-193
lines changed

src/core/crypto/sha256.c

Lines changed: 113 additions & 189 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,213 +1059,137 @@ char *sr_SHA384_Data(const sha2_byte *data, size_t len,
10591059

10601060
/*** SHA-1 ******************/
10611061

1062-
#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
10631062

1064-
/* blk0() and blk() perform the initial expand. */
1065-
/* I got the idea of expanding during the round function from SSLeay */
1063+
#define ROTLEFT(a, b) ((a << b) | (a >> (32 - b)))
10661064

1067-
#ifdef LITTLE_ENDIAN
1068-
#define blk0(i) \
1069-
(block->l[i] = (rol(block->l[i], 24) & (uint32_t)0xFF00FF00) \
1070-
| (rol(block->l[i], 8) & (uint32_t)0x00FF00FF))
1071-
#else
1072-
#define blk0(i) block->l[i]
1073-
#endif
10741065

1075-
#define blk(i) \
1076-
(block->l[i & 15] = rol(block->l[(i + 13) & 15] ^ block->l[(i + 8) & 15] \
1077-
^ block->l[(i + 2) & 15] \
1078-
^ block->l[i & 15], \
1079-
1))
1080-
1081-
/* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
1082-
#define R0(v, w, x, y, z, i) \
1083-
z += ((w & (x ^ y)) ^ y) + blk0(i) + 0x5A827999 + rol(v, 5); \
1084-
w = rol(w, 30);
1085-
#define R1(v, w, x, y, z, i) \
1086-
z += ((w & (x ^ y)) ^ y) + blk(i) + 0x5A827999 + rol(v, 5); \
1087-
w = rol(w, 30);
1088-
#define R2(v, w, x, y, z, i) \
1089-
z += (w ^ x ^ y) + blk(i) + 0x6ED9EBA1 + rol(v, 5); \
1090-
w = rol(w, 30);
1091-
#define R3(v, w, x, y, z, i) \
1092-
z += (((w | x) & y) | (w & x)) + blk(i) + 0x8F1BBCDC + rol(v, 5); \
1093-
w = rol(w, 30);
1094-
#define R4(v, w, x, y, z, i) \
1095-
z += (w ^ x ^ y) + blk(i) + 0xCA62C1D6 + rol(v, 5); \
1096-
w = rol(w, 30);
1097-
1098-
typedef union _BYTE64QUAD16
1066+
void sha1_transform(SHA1_CTX *ctx, const unsigned char data[])
10991067
{
1100-
uint8_t c[64];
1101-
uint32_t l[16];
1102-
} BYTE64QUAD16;
1068+
unsigned int a, b, c, d, e, i, j, t, m[80];
1069+
1070+
for(i = 0, j = 0; i < 16; ++i, j += 4)
1071+
m[i] = (data[j] << 24) + (data[j + 1] << 16) + (data[j + 2] << 8)
1072+
+ (data[j + 3]);
1073+
for(; i < 80; ++i) {
1074+
m[i] = (m[i - 3] ^ m[i - 8] ^ m[i - 14] ^ m[i - 16]);
1075+
m[i] = (m[i] << 1) | (m[i] >> 31);
1076+
}
11031077

1104-
/* Hash a single 512-bit block. This is the core of the algorithm. */
1105-
void SHA1_Transform(uint32_t state[5], const uint8_t buffer[64])
1106-
{
1107-
uint8_t a, b, c, d, e;
1108-
BYTE64QUAD16 *block;
1109-
1110-
block = (BYTE64QUAD16 *)buffer;
1111-
/* Copy context->state[] to working vars */
1112-
a = state[0];
1113-
b = state[1];
1114-
c = state[2];
1115-
d = state[3];
1116-
e = state[4];
1117-
/* 4 rounds of 20 operations each. Loop unrolled. */
1118-
R0(a, b, c, d, e, 0);
1119-
R0(e, a, b, c, d, 1);
1120-
R0(d, e, a, b, c, 2);
1121-
R0(c, d, e, a, b, 3);
1122-
R0(b, c, d, e, a, 4);
1123-
R0(a, b, c, d, e, 5);
1124-
R0(e, a, b, c, d, 6);
1125-
R0(d, e, a, b, c, 7);
1126-
R0(c, d, e, a, b, 8);
1127-
R0(b, c, d, e, a, 9);
1128-
R0(a, b, c, d, e, 10);
1129-
R0(e, a, b, c, d, 11);
1130-
R0(d, e, a, b, c, 12);
1131-
R0(c, d, e, a, b, 13);
1132-
R0(b, c, d, e, a, 14);
1133-
R0(a, b, c, d, e, 15);
1134-
R1(e, a, b, c, d, 16);
1135-
R1(d, e, a, b, c, 17);
1136-
R1(c, d, e, a, b, 18);
1137-
R1(b, c, d, e, a, 19);
1138-
R2(a, b, c, d, e, 20);
1139-
R2(e, a, b, c, d, 21);
1140-
R2(d, e, a, b, c, 22);
1141-
R2(c, d, e, a, b, 23);
1142-
R2(b, c, d, e, a, 24);
1143-
R2(a, b, c, d, e, 25);
1144-
R2(e, a, b, c, d, 26);
1145-
R2(d, e, a, b, c, 27);
1146-
R2(c, d, e, a, b, 28);
1147-
R2(b, c, d, e, a, 29);
1148-
R2(a, b, c, d, e, 30);
1149-
R2(e, a, b, c, d, 31);
1150-
R2(d, e, a, b, c, 32);
1151-
R2(c, d, e, a, b, 33);
1152-
R2(b, c, d, e, a, 34);
1153-
R2(a, b, c, d, e, 35);
1154-
R2(e, a, b, c, d, 36);
1155-
R2(d, e, a, b, c, 37);
1156-
R2(c, d, e, a, b, 38);
1157-
R2(b, c, d, e, a, 39);
1158-
R3(a, b, c, d, e, 40);
1159-
R3(e, a, b, c, d, 41);
1160-
R3(d, e, a, b, c, 42);
1161-
R3(c, d, e, a, b, 43);
1162-
R3(b, c, d, e, a, 44);
1163-
R3(a, b, c, d, e, 45);
1164-
R3(e, a, b, c, d, 46);
1165-
R3(d, e, a, b, c, 47);
1166-
R3(c, d, e, a, b, 48);
1167-
R3(b, c, d, e, a, 49);
1168-
R3(a, b, c, d, e, 50);
1169-
R3(e, a, b, c, d, 51);
1170-
R3(d, e, a, b, c, 52);
1171-
R3(c, d, e, a, b, 53);
1172-
R3(b, c, d, e, a, 54);
1173-
R3(a, b, c, d, e, 55);
1174-
R3(e, a, b, c, d, 56);
1175-
R3(d, e, a, b, c, 57);
1176-
R3(c, d, e, a, b, 58);
1177-
R3(b, c, d, e, a, 59);
1178-
R4(a, b, c, d, e, 60);
1179-
R4(e, a, b, c, d, 61);
1180-
R4(d, e, a, b, c, 62);
1181-
R4(c, d, e, a, b, 63);
1182-
R4(b, c, d, e, a, 64);
1183-
R4(a, b, c, d, e, 65);
1184-
R4(e, a, b, c, d, 66);
1185-
R4(d, e, a, b, c, 67);
1186-
R4(c, d, e, a, b, 68);
1187-
R4(b, c, d, e, a, 69);
1188-
R4(a, b, c, d, e, 70);
1189-
R4(e, a, b, c, d, 71);
1190-
R4(d, e, a, b, c, 72);
1191-
R4(c, d, e, a, b, 73);
1192-
R4(b, c, d, e, a, 74);
1193-
R4(a, b, c, d, e, 75);
1194-
R4(e, a, b, c, d, 76);
1195-
R4(d, e, a, b, c, 77);
1196-
R4(c, d, e, a, b, 78);
1197-
R4(b, c, d, e, a, 79);
1198-
/* Add the working vars back into context.state[] */
1199-
state[0] += a;
1200-
state[1] += b;
1201-
state[2] += c;
1202-
state[3] += d;
1203-
state[4] += e;
1204-
/* Wipe variables */
1205-
a = b = c = d = e = 0;
1078+
a = ctx->state[0];
1079+
b = ctx->state[1];
1080+
c = ctx->state[2];
1081+
d = ctx->state[3];
1082+
e = ctx->state[4];
1083+
1084+
for(i = 0; i < 20; ++i) {
1085+
t = ROTLEFT(a, 5) + ((b & c) ^ (~b & d)) + e + ctx->k[0] + m[i];
1086+
e = d;
1087+
d = c;
1088+
c = ROTLEFT(b, 30);
1089+
b = a;
1090+
a = t;
1091+
}
1092+
for(; i < 40; ++i) {
1093+
t = ROTLEFT(a, 5) + (b ^ c ^ d) + e + ctx->k[1] + m[i];
1094+
e = d;
1095+
d = c;
1096+
c = ROTLEFT(b, 30);
1097+
b = a;
1098+
a = t;
1099+
}
1100+
for(; i < 60; ++i) {
1101+
t = ROTLEFT(a, 5) + ((b & c) ^ (b & d) ^ (c & d)) + e + ctx->k[2]
1102+
+ m[i];
1103+
e = d;
1104+
d = c;
1105+
c = ROTLEFT(b, 30);
1106+
b = a;
1107+
a = t;
1108+
}
1109+
for(; i < 80; ++i) {
1110+
t = ROTLEFT(a, 5) + (b ^ c ^ d) + e + ctx->k[3] + m[i];
1111+
e = d;
1112+
d = c;
1113+
c = ROTLEFT(b, 30);
1114+
b = a;
1115+
a = t;
1116+
}
1117+
1118+
ctx->state[0] += a;
1119+
ctx->state[1] += b;
1120+
ctx->state[2] += c;
1121+
ctx->state[3] += d;
1122+
ctx->state[4] += e;
12061123
}
12071124

1208-
/* SHA1_Init - Initialize new context */
1209-
void sr_SHA1_Init(SHA1_CTX *context)
1125+
void sr_SHA1_Init(SHA1_CTX *ctx)
12101126
{
1211-
/* SHA1 initialization constants */
1212-
context->state[0] = 0x67452301;
1213-
context->state[1] = 0xEFCDAB89;
1214-
context->state[2] = 0x98BADCFE;
1215-
context->state[3] = 0x10325476;
1216-
context->state[4] = 0xC3D2E1F0;
1217-
context->count[0] = context->count[1] = 0;
1127+
ctx->datalen = 0;
1128+
ctx->bitlen = 0;
1129+
ctx->state[0] = 0x67452301;
1130+
ctx->state[1] = 0xEFCDAB89;
1131+
ctx->state[2] = 0x98BADCFE;
1132+
ctx->state[3] = 0x10325476;
1133+
ctx->state[4] = 0xc3d2e1f0;
1134+
ctx->k[0] = 0x5a827999;
1135+
ctx->k[1] = 0x6ed9eba1;
1136+
ctx->k[2] = 0x8f1bbcdc;
1137+
ctx->k[3] = 0xca62c1d6;
12181138
}
12191139

1220-
/* Run your data through this. */
1221-
void sr_SHA1_Update(SHA1_CTX *context, const uint8_t *data, size_t len)
1140+
void sr_SHA1_Update(SHA1_CTX *ctx, const unsigned char data[], size_t len)
12221141
{
1223-
unsigned int i, j;
1224-
1225-
j = (context->count[0] >> 3) & 63;
1226-
if((context->count[0] += len << 3) < (len << 3))
1227-
context->count[1]++;
1228-
context->count[1] += (len >> 29);
1229-
if((j + len) > 63) {
1230-
memcpy(&context->buffer[j], data, (i = 64 - j));
1231-
SHA1_Transform(context->state, context->buffer);
1232-
for(; i + 63 < len; i += 64) {
1233-
SHA1_Transform(context->state, &data[i]);
1142+
size_t i;
1143+
1144+
for(i = 0; i < len; ++i) {
1145+
ctx->data[ctx->datalen] = data[i];
1146+
ctx->datalen++;
1147+
if(ctx->datalen == 64) {
1148+
sha1_transform(ctx, ctx->data);
1149+
ctx->bitlen += 512;
1150+
ctx->datalen = 0;
12341151
}
1235-
j = 0;
1236-
} else
1237-
i = 0;
1238-
memcpy(&context->buffer[j], &data[i], len - i);
1152+
}
12391153
}
12401154

1241-
1242-
/* Add padding and return the message digest. */
1243-
void sr_SHA1_Final(uint8_t digest[SHA1_DIGEST_LENGTH], SHA1_CTX *context)
1155+
void sr_SHA1_Final(unsigned char hash[SHA1_DIGEST_LENGTH], SHA1_CTX *ctx)
12441156
{
1245-
uint32_t i, j;
1246-
uint8_t finalcount[8];
1157+
unsigned int i;
12471158

1248-
for(i = 0; i < 8; i++) {
1249-
finalcount[i] = (uint8_t)((context->count[(i >= 4 ? 0 : 1)]
1250-
>> ((3 - (i & 3)) * 8))
1251-
& 255); /* Endian independent */
1252-
}
1253-
sr_SHA1_Update(context, (uint8_t *)"\200", 1);
1254-
while((context->count[0] & 504) != 448) {
1255-
sr_SHA1_Update(context, (uint8_t *)"\0", 1);
1159+
i = ctx->datalen;
1160+
1161+
if(ctx->datalen < 56) {
1162+
ctx->data[i++] = 0x80;
1163+
while(i < 56)
1164+
ctx->data[i++] = 0x00;
1165+
} else {
1166+
ctx->data[i++] = 0x80;
1167+
while(i < 64)
1168+
ctx->data[i++] = 0x00;
1169+
sha1_transform(ctx, ctx->data);
1170+
memset(ctx->data, 0, 56);
12561171
}
1257-
/* Should cause a SHA1_Transform() */
1258-
sr_SHA1_Update(context, finalcount, 8);
1259-
for(i = 0; i < SHA1_DIGEST_LENGTH; i++) {
1260-
digest[i] = (uint8_t)((context->state[i >> 2] >> ((3 - (i & 3)) * 8))
1261-
& 255);
1172+
1173+
ctx->bitlen += ctx->datalen * 8;
1174+
ctx->data[63] = ctx->bitlen;
1175+
ctx->data[62] = ctx->bitlen >> 8;
1176+
ctx->data[61] = ctx->bitlen >> 16;
1177+
ctx->data[60] = ctx->bitlen >> 24;
1178+
ctx->data[59] = ctx->bitlen >> 32;
1179+
ctx->data[58] = ctx->bitlen >> 40;
1180+
ctx->data[57] = ctx->bitlen >> 48;
1181+
ctx->data[56] = ctx->bitlen >> 56;
1182+
sha1_transform(ctx, ctx->data);
1183+
1184+
#if BYTE_ORDER == LITTLE_ENDIAN
1185+
for(i = 0; i < 4; ++i) {
1186+
hash[i] = (ctx->state[0] >> (24 - i * 8)) & 0x000000ff;
1187+
hash[i + 4] = (ctx->state[1] >> (24 - i * 8)) & 0x000000ff;
1188+
hash[i + 8] = (ctx->state[2] >> (24 - i * 8)) & 0x000000ff;
1189+
hash[i + 12] = (ctx->state[3] >> (24 - i * 8)) & 0x000000ff;
1190+
hash[i + 16] = (ctx->state[4] >> (24 - i * 8)) & 0x000000ff;
12621191
}
1263-
/* Wipe variables */
1264-
i = j = 0;
1265-
memset(context->buffer, 0, SHA1_BLOCK_LENGTH);
1266-
memset(context->state, 0, SHA1_DIGEST_LENGTH);
1267-
memset(context->count, 0, 8);
1268-
memset(&finalcount, 0, 8);
1192+
#endif
12691193
}
12701194

12711195
char *sr_SHA1_End(SHA1_CTX *context, char buffer[SHA1_DIGEST_STRING_LENGTH])

src/core/crypto/sha256.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,11 @@ typedef unsigned long long u_int64_t; /* 8-bytes (64-bits) */
7070

7171
typedef struct _SHA1_CTX
7272
{
73-
uint32_t state[5];
74-
uint32_t count[2];
75-
uint8_t buffer[SHA1_BLOCK_LENGTH];
73+
unsigned char data[SHA1_BLOCK_LENGTH];
74+
unsigned int datalen;
75+
unsigned long long bitlen;
76+
unsigned int state[5];
77+
unsigned int k[4];
7678
} SHA1_CTX;
7779

7880
typedef struct _SHA256_CTX
@@ -99,7 +101,7 @@ typedef unsigned long long u_int64_t; /* 8-bytes (64-bits) */
99101

100102
void sr_SHA1_Init(SHA1_CTX *);
101103
void sr_SHA1_Update(SHA1_CTX *, const uint8_t *, size_t);
102-
void sr_SHA1_Final(sha2_byte[SHA1_DIGEST_LENGTH], SHA1_CTX *);
104+
void sr_SHA1_Final(unsigned char hash[SHA1_DIGEST_LENGTH], SHA1_CTX *ctx);
103105
char *sr_SHA1_End(SHA1_CTX *, char[SHA1_DIGEST_STRING_LENGTH]);
104106
char *sr_SHA1_Data(
105107
const uint8_t *, size_t, char[SHA1_DIGEST_STRING_LENGTH]);

0 commit comments

Comments
 (0)