Commit bc08de1c
bc08de1c1b0eMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
Merge pull request #557 from OriPekelman/add-sha1-websocket-accept
feat(crypto): add sp_crypto_sha1_hex + sp_crypto_websocket_accept
Details
- Commit
- bc08de1c1b0e
- Parent
- b047f1a8c88c
- Merge parent
- 193420024253
lib/sp_crypto.cmodifiedView file
@@ -110,6 +110,124 @@
110110
}111111
}112112
113+
/* ---------- SHA-1 ----------114+
* FIPS-180-4 (legacy hash, kept for WebSocket handshake -- RFC 6455115+
* requires SHA-1 specifically for Sec-WebSocket-Accept). Do not use116+
* for new security designs; SHA-256 is the right primitive for those.117+
* Compact reference implementation -- public domain.118+
*/119+
120+
static void sp_crypto_sha1_block(uint32_t H[5], const uint8_t b[64]) {121+
uint32_t w[80], a, sa, sb, sc, sd, se, f, k;122+
int i;123+
for (i = 0; i < 16; i++) {124+
w[i] = ((uint32_t)b[i*4] << 24) | ((uint32_t)b[i*4+1] << 16) |125+
((uint32_t)b[i*4+2] << 8) | (uint32_t)b[i*4+3];126+
}127+
for (i = 16; i < 80; i++) {128+
a = w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16];129+
w[i] = (a << 1) | (a >> 31);130+
}131+
sa = H[0]; sb = H[1]; sc = H[2]; sd = H[3]; se = H[4];132+
for (i = 0; i < 80; i++) {133+
if (i < 20) { f = (sb & sc) | (~sb & sd); k = 0x5A827999; }134+
else if (i < 40) { f = sb ^ sc ^ sd; k = 0x6ED9EBA1; }135+
else if (i < 60) { f = (sb & sc) | (sb & sd) | (sc & sd); k = 0x8F1BBCDC; }136+
else { f = sb ^ sc ^ sd; k = 0xCA62C1D6; }137+
uint32_t t = ((sa << 5) | (sa >> 27)) + f + se + k + w[i];138+
se = sd; sd = sc; sc = (sb << 30) | (sb >> 2); sb = sa; sa = t;139+
}140+
H[0] += sa; H[1] += sb; H[2] += sc; H[3] += sd; H[4] += se;141+
}142+
143+
static void sp_crypto_sha1(const uint8_t *msg, size_t len, uint8_t out[20]) {144+
uint32_t H[5] = {145+
0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0146+
};147+
uint8_t buf[64];148+
size_t i, full = len & ~((size_t)63);149+
for (i = 0; i < full; i += 64) sp_crypto_sha1_block(H, msg + i);150+
size_t rem = len - full;151+
for (i = 0; i < rem; i++) buf[i] = msg[full + i];152+
buf[rem] = 0x80;153+
if (rem >= 56) {154+
for (i = rem + 1; i < 64; i++) buf[i] = 0;155+
sp_crypto_sha1_block(H, buf);156+
for (i = 0; i < 56; i++) buf[i] = 0;157+
} else {158+
for (i = rem + 1; i < 56; i++) buf[i] = 0;159+
}160+
uint64_t bits = (uint64_t)len * 8;161+
for (i = 0; i < 8; i++) buf[56 + i] = (uint8_t)(bits >> (56 - 8*i));162+
sp_crypto_sha1_block(H, buf);163+
for (i = 0; i < 5; i++) {164+
out[i*4] = (uint8_t)(H[i] >> 24);165+
out[i*4+1] = (uint8_t)(H[i] >> 16);166+
out[i*4+2] = (uint8_t)(H[i] >> 8);167+
out[i*4+3] = (uint8_t)(H[i]);168+
}169+
}170+
171+
static char sp_crypto_sha1_hex_buf[41];172+
173+
const char *sp_crypto_sha1_hex(const char *msg) {174+
uint8_t out[20];175+
sp_crypto_sha1((const uint8_t *)msg, strlen(msg), out);176+
static const char H[] = "0123456789abcdef";177+
int i;178+
for (i = 0; i < 20; i++) {179+
sp_crypto_sha1_hex_buf[i*2] = H[(out[i] >> 4) & 0xf];180+
sp_crypto_sha1_hex_buf[i*2+1] = H[out[i] & 0xf];181+
}182+
sp_crypto_sha1_hex_buf[40] = '\0';183+
return sp_crypto_sha1_hex_buf;184+
}185+
186+
/* RFC 6455 §1.3 Sec-WebSocket-Accept:187+
* base64(SHA-1(client_key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"))188+
* Standard Base64 (RFC 4648 §4, `+/` alphabet, `=` padding) -- NOT189+
* the URL-safe variant. 20 bytes of SHA-1 -> 28-char output ("xxx=").190+
* The GUID is the WebSocket protocol's fixed magic string.191+
*/192+
static const char SPC_B64[64] =193+
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";194+
195+
static char sp_crypto_websocket_accept_buf[29];196+
197+
const char *sp_crypto_websocket_accept(const char *client_key) {198+
/* client_key is the 24-char base64 from the request header;199+
* GUID is 36 chars. Total <= 60; cap input at 128 for safety. */200+
char in[128 + 36 + 1];201+
size_t klen = strlen(client_key);202+
if (klen > 128) klen = 128;203+
size_t i;204+
for (i = 0; i < klen; i++) in[i] = client_key[i];205+
static const char guid[] = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";206+
for (i = 0; i < 36; i++) in[klen + i] = guid[i];207+
uint8_t digest[20];208+
sp_crypto_sha1((const uint8_t *)in, klen + 36, digest);209+
/* base64(20 bytes) = 28 chars: 6 full triplets + 2 leftover bytes210+
* -> 3 chars + 1 padding `=`. */211+
int j = 0;212+
for (i = 0; i + 3 <= 20; i += 3) {213+
uint32_t v = ((uint32_t)digest[i] << 16)214+
| ((uint32_t)digest[i+1] << 8)215+
| (uint32_t)digest[i+2];216+
sp_crypto_websocket_accept_buf[j++] = SPC_B64[(v >> 18) & 0x3f];217+
sp_crypto_websocket_accept_buf[j++] = SPC_B64[(v >> 12) & 0x3f];218+
sp_crypto_websocket_accept_buf[j++] = SPC_B64[(v >> 6) & 0x3f];219+
sp_crypto_websocket_accept_buf[j++] = SPC_B64[ v & 0x3f];220+
}221+
/* 2 leftover bytes -> 3 b64 chars + 1 pad */222+
uint32_t v = ((uint32_t)digest[18] << 16) | ((uint32_t)digest[19] << 8);223+
sp_crypto_websocket_accept_buf[j++] = SPC_B64[(v >> 18) & 0x3f];224+
sp_crypto_websocket_accept_buf[j++] = SPC_B64[(v >> 12) & 0x3f];225+
sp_crypto_websocket_accept_buf[j++] = SPC_B64[(v >> 6) & 0x3f];226+
sp_crypto_websocket_accept_buf[j++] = '=';227+
sp_crypto_websocket_accept_buf[j] = '\0';228+
return sp_crypto_websocket_accept_buf;229+
}230+
113231
/* ---------- HMAC-SHA256 (RFC 2104) ---------- */114232
115233
static void sp_crypto_hmac_sha256(const uint8_t *key, size_t klen,lib/sp_crypto.hmodifiedView file
Loading diff…
test/sp_crypto_sha1.rbaddedView file
Loading diff…
test/sp_crypto_sha1.rb.expectedaddedView file
Loading diff…