Commit a50c0b8d
a50c0b8d1819Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 5 files · +2 added · ~3 modified
Description
runtime,codegen: String#crypt via libspinel_rt.a's HMAC-SHA-256
Pre-fix `"hello".crypt("ab")` emitted 0 and an unresolved-call warning. Spinel doesn't link libc crypt(3), but lib/sp_crypto.c already ships HMAC-SHA-256 b64url — reuse it.
Contract: 13-char string whose first two chars are the salt (or '.' padding for short salts), followed by 11 chars of HMAC-SHA-256(salt, password). The hash bytes won't match MRI's DES output, but length / salt-prefix / determinism do, and CRuby's spec says String#crypt is impl-defined.
Fixes #885
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- a50c0b8d1819
- Parent
- 1ce49240bc2a
lib/sp_runtime.hmodifiedView file
@@ -1748,6 +1748,43 @@
17481748
would see the alloc size and trailing NULs. */17491749
static const char*sp_str_squeeze(const char*s){if(!s)return sp_str_empty;size_t bl=strlen(s);char*r=sp_str_alloc_raw(bl+1);size_t n=0;uint32_t prev=0xFFFFFFFFu;const char*p=s;while(*p){uint32_t cp;int cn=sp_utf8_decode(p,&cp);if(cp!=prev){memcpy(r+n,p,cn);n+=cn;prev=cp;}p+=cn;}r[n]=0;sp_str_set_len(r,n);return r;}17501750
1751+
/* Forward decl from sp_crypto.h (libspinel_rt.a). Used by1752+
sp_str_crypt below to provide a deterministic crypt-like1753+
hash without dragging in libc crypt(3). */1754+
const char *sp_crypto_hmac_sha256_b64url(const char *key, const char *msg);1755+
1756+
/* String#crypt — Spinel's crypt is NOT the libc DES crypt. It1757+
returns `salt[0..1] || hmac_sha256(salt, password)[0..10]` as1758+
a 13-char string (same length as DES crypt, deterministic,1759+
stronger primitive). CRuby's spec says String#crypt is impl-1760+
defined and "should not be used for security"; this matches1761+
that contract while keeping outputs reproducible across1762+
spinel builds. Short salts get padded with '.' so the result1763+
still has the canonical first-2-chars-are-salt shape. */1764+
static const char *sp_str_crypt(const char *s, const char *salt) {1765+
if (!salt) salt = "";1766+
char salt2[3];1767+
salt2[0] = salt[0] ? salt[0] : '.';1768+
salt2[1] = (salt[0] && salt[1]) ? salt[1] : '.';1769+
salt2[2] = 0;1770+
const char *digest = sp_crypto_hmac_sha256_b64url(salt2, s ? s : "");1771+
char *r = sp_str_alloc(13);1772+
r[0] = salt2[0];1773+
r[1] = salt2[1];1774+
for (int i = 0; i < 11; i++) {1775+
char c = digest[i];1776+
/* Map b64url's `-`/`_` to crypt-alphabet `.`/`/` so the1777+
output stays in `[./0-9A-Za-z]` like the historical1778+
crypt result. */1779+
if (c == '-') c = '.';1780+
else if (c == '_') c = '/';1781+
r[2 + i] = c;1782+
}1783+
r[13] = 0;1784+
sp_str_set_len(r, 13);1785+
return r;1786+
}1787+
17511788
/* String#scrub — walk the bytes; for each valid UTF-8 lead +17521789
continuation sequence, copy through. For invalid bytes (lone17531790
continuation, truncated multi-byte, overlong, etc.), emit thespinel_analyze.rbmodifiedView file
Loading diff…
spinel_codegen.rbmodifiedView file
Loading diff…
test/string_crypt.rbaddedView file
Loading diff…
test/string_crypt.rb.expectedaddedView file
Loading diff…