iodine 0.8.4 → 0.8.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1247,13 +1247,23 @@ See also: [Compiler Attributes](./001 compiler attributes.md).
1247
1247
 
1248
1248
  Enabled by `FIO_LEAK_COUNTER` (default `1`). `FIO_NO_LOG` and `FIO_LEAK_COUNTER` are mutually exclusive because leak reports are printed through the log.
1249
1249
 
1250
+ ### `FIO_LEAK_COUNTER_DEC`
1251
+
1252
+ ```c
1253
+ #define FIO_LEAK_COUNTER_DEC(name)
1254
+ ```
1255
+
1256
+ Declares a named counter and its cleanup function without defining them. Use it in declarations-only `FIO_EXTERN` translation units that reference the counter but must not emit a definition.
1257
+
1250
1258
  ### `FIO_LEAK_COUNTER_DEF`
1251
1259
 
1252
1260
  ```c
1253
1261
  #define FIO_LEAK_COUNTER_DEF(name)
1254
1262
  ```
1255
1263
 
1256
- Defines a named counter and a cleanup function that reports remaining allocations at process exit.
1264
+ Defines a named counter, a cleanup function that reports remaining allocations at process exit, and a constructor that registers the cleanup with the `FIO_CALL_AFTER_EXIT` state event (skipped when `FIO_LEAK_COUNTER_SKIP_EXIT` is set).
1265
+
1266
+ Emits non-`static` symbols, so it belongs in exactly one translation unit: the `FIO_EXTERN_COMPLETE` unit when the header is split, or every unit in a header-only build. Declarations-only units use `FIO_LEAK_COUNTER_DEC` instead.
1257
1267
 
1258
1268
  ### `FIO_LEAK_COUNTER_ON_ALLOC`
1259
1269
 
@@ -3482,53 +3492,54 @@ Most defaults are guarded with `#ifndef`; define them before including the heade
3482
3492
 
3483
3493
  ### Memory and Safety
3484
3494
 
3485
- - **`FIO_LEAK_COUNTER`** — Enables memory-leak counting. When enabled, the leak counter tracks allocations made through facil.io allocators and prints a summary at exit.
3486
- Default: `1` (enabled).
3487
- Set to `0` to disable counting and the exit report.
3495
+ - **`FIO_LEAK_COUNTER`** — Enables memory-leak counting. When enabled, the leak counter tracks allocations made through facil.io allocators and prints a summary at exit.
3496
+ Default: `1` (enabled).
3497
+ Set to `0` to disable counting and the exit report.
3488
3498
  Note: this conflicts with `FIO_NO_LOG`, because the leak report uses the logging layer.
3489
- - **`FIO_LEAK_COUNTER_SKIP_EXIT`** — Skip the automatic leak report at exit when leak counting is enabled.
3499
+ - **`FIO_LEAK_COUNTER_SKIP_EXIT`** — Skip the automatic leak report at exit when leak counting is enabled.
3490
3500
  Default: `0` (report at exit).
3491
- - **`FIO_LEAK_COUNTER_DEF(name)`** — Declares a named leak counter.
3501
+ - **`FIO_LEAK_COUNTER_DEC(name)`** — Declares a named leak counter without defining it; use in declarations-only `FIO_EXTERN` units.
3502
+ - **`FIO_LEAK_COUNTER_DEF(name)`** — Defines a named leak counter, its cleanup and the `FIO_CALL_AFTER_EXIT` registration; only in the single `FIO_EXTERN_COMPLETE` unit (or every unit in a header-only build).
3492
3503
  - **`FIO_LEAK_COUNTER_ON_ALLOC(name)`** — Records an allocation against the named counter.
3493
3504
  - **`FIO_LEAK_COUNTER_ON_FREE(name)`** — Records a free against the named counter.
3494
3505
  - **`FIO_LEAK_COUNTER_COUNT(name)`** — Returns the current value of the named counter.
3495
- - **`FIO_MEMORY_INITIALIZE_ALLOCATIONS_DEFAULT`** — When non-zero, facil.io allocators zero-initialize returned memory by default.
3496
- Default: `1` (secure by default).
3506
+ - **`FIO_MEMORY_INITIALIZE_ALLOCATIONS_DEFAULT`** — When non-zero, facil.io allocators zero-initialize returned memory by default.
3507
+ Default: `1` (secure by default).
3497
3508
  Set to `0` if you want raw allocations and prefer to do your own initialization.
3498
- - **`FIO_MEM_PAGE_SIZE_LOG`** — Log₂ of the OS memory page size. The memory allocator uses this to round allocations and map memory efficiently.
3499
- Default: `12` (4096-byte pages).
3509
+ - **`FIO_MEM_PAGE_SIZE_LOG`** — Log₂ of the OS memory page size. The memory allocator uses this to round allocations and map memory efficiently.
3510
+ Default: `12` (4096-byte pages).
3500
3511
  Change it if you are targeting a system with a different page size.
3501
3512
  - **`FIO_STATIC_ALLOC_SAFE_CONCURRENCY_MAX`** — Default slot count for selected internal `FIO_STATIC_ALLOC_DEF` invocations (default: `256`). The macro does not use this setting implicitly; pass it explicitly as the fourth argument where appropriate.
3502
3513
 
3503
3514
  ### Concurrency
3504
3515
 
3505
- - **`FIO_USE_THREAD_MUTEX`** — Choose the locking primitive used by thread-safe modules.
3506
- Default: `0` (use facil.io spinlocks).
3507
- Set to `1` to use OS native mutexes (`pthread_mutex_t` on POSIX) instead.
3516
+ - **`FIO_USE_THREAD_MUTEX`** — Choose the locking primitive used by thread-safe modules.
3517
+ Default: `0` (use facil.io spinlocks).
3518
+ Set to `1` to use OS native mutexes (`pthread_mutex_t` on POSIX) instead.
3508
3519
  If you need this decision to apply to only a single `#include`, use `FIO_USE_THREAD_MUTEX_TMP` in [`./001 header.h`](./001%20header.h) instead.
3509
3520
 
3510
3521
  ### Performance and Alignment
3511
3522
 
3512
- - **`FIO_UNALIGNED_ACCESS`** — Allows facil.io to attempt unaligned memory access on CPUs that support it. When enabled, the library may use faster paths in helpers such as `fio_buf2uXX`.
3513
- Default: `1` (attempt detection).
3523
+ - **`FIO_UNALIGNED_ACCESS`** — Allows facil.io to attempt unaligned memory access on CPUs that support it. When enabled, the library may use faster paths in helpers such as `fio_buf2uXX`.
3524
+ Default: `1` (attempt detection).
3514
3525
  Set to `0` on strict-alignment architectures or if you want to forbid unaligned access entirely. The actual capability is reflected by `FIO_UNALIGNED_MEMORY_ACCESS_ENABLED`, which is derived at compile time.
3515
- - **`FIO_LIMIT_INTRINSIC_BUFFER`** — Limits register pressure in some pseudo-intrinsic loops by processing smaller batches and looping more.
3516
- Default: `1`.
3526
+ - **`FIO_LIMIT_INTRINSIC_BUFFER`** — Limits register pressure in some pseudo-intrinsic loops by processing smaller batches and looping more.
3527
+ Default: `1`.
3517
3528
  Set to `0` if you prefer larger unrolled buffers and do not mind the extra register use.
3518
3529
 
3519
3530
  ### Maps
3520
3531
 
3521
- - **`FIO_MAP_WARNING_BITSIZE`** — Bit size at which hash-map allocations trigger an internal warning threshold in the `imap`/`map` templates.
3522
- Default: `24`.
3532
+ - **`FIO_MAP_WARNING_BITSIZE`** — Bit size at which hash-map allocations trigger an internal warning threshold in the `imap`/`map` templates.
3533
+ Default: `24`.
3523
3534
  Raise or lower it to tune when the library starts worrying about map size during growth.
3524
3535
 
3525
3536
  ### Logging
3526
3537
 
3527
3538
  - **`FIO_NO_LOG`** — Define this to remove all logging output. Cannot be combined with `FIO_LEAK_COUNTER`; the two are mutually exclusive.
3528
- - **`FIO_LOG2STDERR`** — Low-level sink used by the logging macros.
3529
- Default: no-op.
3539
+ - **`FIO_LOG2STDERR`** — Low-level sink used by the logging macros.
3540
+ Default: no-op.
3530
3541
  Override to print to `stderr` or route messages to a custom destination.
3531
- - **`FIO_LOG_LENGTH_LIMIT`** — Approximate cap on a single formatted log message, limiting stack memory use.
3542
+ - **`FIO_LOG_LENGTH_LIMIT`** — Approximate cap on a single formatted log message, limiting stack memory use.
3532
3543
  Default: `1024`.
3533
3544
  - **`DEBUG`** — Define this to enable debug-only log macros (`FIO_LOG_D*`) and active `FIO_ASSERT_DEBUG` checks. Also disables SIMD/Neon intrinsics in [`./000 core.h`](./000%20core.h).
3534
3545
 
@@ -5327,6 +5338,8 @@ typedef struct {
5327
5338
  fio_buf_info_s cert;
5328
5339
  fio_buf_info_s trust;
5329
5340
  fio_buf_info_s pass;
5341
+ /** The URL query without the TLS hints at its head / tail. */
5342
+ fio_buf_info_s query;
5330
5343
  bool tls;
5331
5344
  } fio_url_tls_info_s;
5332
5345
  ```
@@ -5376,6 +5389,12 @@ Detects TLS from scheme or query. Implicit TLS schemes include `wss`, `https`, `
5376
5389
 
5377
5390
  Query keys `tls` or `ssl` enable TLS. A nonempty value other than `1` or `true` supplies both key and certificate. A nonempty `key` and `cert` pair also enables TLS; `pass` or `password` supplies its optional passphrase. A nonempty `trust` value enables TLS independently. `key`, `cert`, or passphrase parameters alone do not enable TLS.
5378
5391
 
5392
+ TLS hints are read from the whole query. The returned `query` is the URL query with the hints at its head / tail omitted, a slice of the original (no copy), so it can be forwarded to a peer: `u.query = fio_url_is_tls(u).query;` (`fio_http_connect` does this for the request query). Hints in the middle of the query are read **and** kept, a pass-through for application keys with colliding names. Only hints actually used are omitted: `tls` / `ssl` always, `trust` when nonempty, `key` / `cert` only when both are set, and `pass` / `password` only alongside that pair. i.e.:
5393
+
5394
+ - `?tls&page=1&trust=sys` => `page=1`
5395
+ - `?page=1&tls&sort=a` => `page=1&tls&sort=a` (TLS enabled)
5396
+ - `?key=API_KEY` => `key=API_KEY` (no `cert`, not a hint)
5397
+
5379
5398
  Input slices need no trailing NUL or padding. Returned slices borrow the input buffer. Debug diagnostics report only TLS status and material-presence flags, never material contents.
5380
5399
 
5381
5400
  ### Macros
@@ -17122,6 +17141,19 @@ void example(void) {
17122
17141
 
17123
17142
  ---
17124
17143
 
17144
+ ## Split (`FIO_EXTERN`) builds
17145
+
17146
+ When the header is split into a declarations-only `FIO_EXTERN` unit and one `FIO_EXTERN_COMPLETE` implementation unit, the leak counter generated for `FIO_REF_NAME` is emitted in two steps:
17147
+
17148
+ - `FIO_LEAK_COUNTER_DEF(FIO_REF_NAME)` — definitions (counter, cleanup, exit registration), placed under the complete-TU gate only.
17149
+ - `FIO_LEAK_COUNTER_DEC(FIO_REF_NAME)` — declarations only, placed under the gate that also enables the `static` helpers in declarations-only units.
17150
+
17151
+ The declarations are needed because the internal helper types are `static` in declarations-only translation units (see [`001 header.h`](./001%20header.h)), so those units still compile their own copies of the inline wrappers and therefore reference the counter symbols. `_DEC` makes the symbols visible without defining them, so the linker resolves them against the single `_DEF` in the implementation unit.
17152
+
17153
+ `FIO_WEAK` is not used for these symbols: a duplicate definition is a real error and is reported by the linker instead of being silently merged.
17154
+
17155
+ ---
17156
+
17125
17157
  ## Memory Layout
17126
17158
 
17127
17159
  ```
@@ -21975,8 +22007,11 @@ fio_http_connect("https://example.com/", NULL, .on_http = on_response,
21975
22007
  .verify = 1);
21976
22008
  ```
21977
22009
 
21978
- The `trust=sys` / `trust=<ca-file>` URL query also works, but the URL query
21979
- is sent to the server as the request query unless `h` already has one.
22010
+ The `trust=sys` / `trust=<ca-file>` URL query also works. The URL query is
22011
+ sent to the server as the request query (unless `h` already has one), with
22012
+ the TLS hints at its head / tail omitted (see `fio_url_is_tls`). Hints in the
22013
+ middle of the query are sent as-is, so place TLS hints first or last, e.g.,
22014
+ `https://example.com/?page=1&trust=sys` sends `?page=1`.
21980
22015
 
21981
22016
  **Note**: interim `1xx` responses other than `101` (e.g., `100 Continue`,
21982
22017
  `103 Early Hints`) are skipped (RFC 9110 §15.2). `on_http` runs once, for
@@ -203,6 +203,10 @@ static VALUE iodine_cli_parse(VALUE self, VALUE required) {
203
203
  FIO_CLI_INT("--max-body -maxbd (" FIO_MACRO2STR(
204
204
  FIO_HTTP_DEFAULT_MAX_BODY_SIZE) ") total message payload limit per "
205
205
  "request, in bytes."),
206
+ FIO_CLI_INT("--max-params -maxpr (" FIO_MACRO2STR(
207
+ FIO_HTTP_PARAMS_MAX_FIELDS) ") request params field limit (query "
208
+ "pairs, form fields and multipart "
209
+ "parts)."),
206
210
  FIO_CLI_INT("--keep-alive -k (" FIO_MACRO2STR(
207
211
  FIO_HTTP_DEFAULT_TIMEOUT) ") HTTP keep-alive timeout in seconds "
208
212
  "(0..255)"),
@@ -1760,20 +1760,8 @@ static void iodine_io_raw_on_attach(fio_io_s *io) {
1760
1760
 
1761
1761
  /** Tests whether a nonblocking client socket completed its connection. */
1762
1762
  static int iodine_io_raw_client_connect_failed(fio_io_s *io) {
1763
- int error = 0;
1764
- #if FIO_OS_WIN
1765
- int error_len = sizeof(error);
1766
- return fio___winsock_fn.getsockopt(fio_io_fd(io),
1767
- SOL_SOCKET,
1768
- SO_ERROR,
1769
- (char *)&error,
1770
- &error_len) ||
1771
- error;
1772
- #else
1773
- socklen_t error_len = sizeof(error);
1774
- return getsockopt(fio_io_fd(io), SOL_SOCKET, SO_ERROR, &error, &error_len) ||
1775
- error;
1776
- #endif
1763
+ /* pending SO_ERROR, or -1 if the query failed */
1764
+ return fio_sock_error(fio_io_fd(io)) != 0;
1777
1765
  }
1778
1766
 
1779
1767
  typedef struct {
@@ -2255,6 +2243,7 @@ FIO_IFUNC iodine_connection_args_s iodine_connection_parse_args(int argc,
2255
2243
  .max_header_size = (uint32_t)fio_cli_get_i("-maxhd"),
2256
2244
  .max_line_len = (uint32_t)fio_cli_get_i("-maxln"),
2257
2245
  .max_body_size = (size_t)fio_cli_get_i("-maxbd"),
2246
+ .max_params = (size_t)fio_cli_get_i("-maxpr"),
2258
2247
  .ws_max_msg_size = (size_t)fio_cli_get_i("-maxms"),
2259
2248
  .timeout = iodine_connection___cli_timeout("-k"),
2260
2249
  .ws_timeout = iodine_connection___cli_timeout("-ping"),
@@ -2266,6 +2255,7 @@ FIO_IFUNC iodine_connection_args_s iodine_connection_parse_args(int argc,
2266
2255
  },
2267
2256
  };
2268
2257
  VALUE proc = Qnil, handler_tmp = Qnil, tls_io_rb = Qnil, timeout = Qnil;
2258
+ VALUE verify_rb = Qnil;
2269
2259
  iodine_rb2c_arg(
2270
2260
  argc,
2271
2261
  argv,
@@ -2283,6 +2273,7 @@ FIO_IFUNC iodine_connection_args_s iodine_connection_parse_args(int argc,
2283
2273
  IODINE_ARG_U8(r.settings.timeout, 0, "timeout", 0),
2284
2274
  IODINE_ARG_U8(r.settings.ws_timeout, 0, "ping", 0),
2285
2275
  IODINE_ARG_BOOL(r.settings.log, 0, "log", 0),
2276
+ IODINE_ARG_RB(verify_rb, 0, "verify", 0),
2286
2277
  IODINE_ARG_BOOL(r.settings.compress_static, 0, "dynamic_deflate", 0),
2287
2278
  IODINE_ARG_BOOL(r.settings.compress_ws, 0, "websocket_deflate", 0),
2288
2279
  IODINE_ARG_BUF(r.method, 0, "method", 0),
@@ -2291,6 +2282,8 @@ FIO_IFUNC iodine_connection_args_s iodine_connection_parse_args(int argc,
2291
2282
  IODINE_ARG_RB(r.cookies, 0, "cookies", 0),
2292
2283
  IODINE_ARG_PROC(proc, 0, "block", 0));
2293
2284
  r.settings.compress_dynamic = r.settings.compress_static;
2285
+ /* client mode only: TLS peer verification (system trust if none set) */
2286
+ r.settings.verify = (verify_rb == Qtrue);
2294
2287
  r.settings.udata = (void *)handler_tmp;
2295
2288
  /* test for errors before allocating or protecting data */
2296
2289
 
@@ -2624,6 +2617,7 @@ static VALUE iodine_connection_initialize(int argc, VALUE *argv, VALUE self) {
2624
2617
  .protocol = protocol,
2625
2618
  .udata = (void *)self,
2626
2619
  .tls = args.settings.tls,
2620
+ .verify = args.settings.verify,
2627
2621
  .timeout = protocol->timeout,
2628
2622
  .on_failed = iodine_io_raw_client_on_failed);
2629
2623
  }
@@ -2939,6 +2933,17 @@ static VALUE iodine_connection_peer_addr(VALUE self) {
2939
2933
  return rb_str_new(buf.buf, buf.len);
2940
2934
  }
2941
2935
 
2936
+ /** Returns the local (server side) network address or `nil`. */
2937
+ static VALUE iodine_connection_local_addr(VALUE self) {
2938
+ fio_buf_info_s buf = FIO_BUF_INFO0;
2939
+ iodine_connection_s *c = iodine_connection_ptr(self);
2940
+ if (c->io)
2941
+ buf = fio_sock_addr(fio_io_fd(c->io), false);
2942
+ if (!buf.len)
2943
+ return Qnil;
2944
+ return rb_str_new(buf.buf, buf.len);
2945
+ }
2946
+
2942
2947
  /** Returns the published peer's address. If unknown, returns {#peer_addr}. */
2943
2948
  static VALUE iodine_connection_from(VALUE self) {
2944
2949
  FIO_STR_INFO_TMP_VAR(addr, 128);
@@ -3611,6 +3616,7 @@ static void Init_Iodine_Connection(void) {
3611
3616
  rb_define_method(m, "tls?", iodine_connection_is_tls, 0);
3612
3617
 
3613
3618
  rb_define_method(m, "peer_addr", iodine_connection_peer_addr, 0);
3619
+ rb_define_method(m, "local_addr", iodine_connection_local_addr, 0);
3614
3620
  rb_define_method(m, "from", iodine_connection_from, 0);
3615
3621
  rb_define_method(m, "certificate", iodine_connection_certificate, 0);
3616
3622
  rb_define_method(m,
@@ -11,6 +11,7 @@ Provides access to modern cryptographic primitives:
11
11
  - AES-128-GCM: AEAD symmetric encryption (16-byte key, 12-byte nonce)
12
12
  - AES-256-GCM: AEAD symmetric encryption (32-byte key, 12-byte nonce)
13
13
  - Ed25519: Digital signatures
14
+ - P256 / P384: ECDSA digital signatures (ES256 / ES384, DER signatures)
14
15
  - X25519: Key exchange and public-key encryption (ECIES with ChaCha20/AES)
15
16
  - HKDF: Key derivation (RFC 5869)
16
17
  ***************************************************************************** */
@@ -21,6 +22,8 @@ static VALUE iodine_rb_XCHACHA20POLY1305;
21
22
  static VALUE iodine_rb_AES128GCM;
22
23
  static VALUE iodine_rb_AES256GCM;
23
24
  static VALUE iodine_rb_ED25519;
25
+ static VALUE iodine_rb_P256;
26
+ static VALUE iodine_rb_P384;
24
27
  static VALUE iodine_rb_X25519;
25
28
  static VALUE iodine_rb_HKDF;
26
29
  static VALUE iodine_rb_X25519MLKEM768;
@@ -617,6 +620,105 @@ FIO_SFUNC VALUE iodine_crypto_ed25519_to_x25519_public(int argc,
617
620
  (void)self;
618
621
  }
619
622
 
623
+ /* *****************************************************************************
624
+ ECDSA P-256 / P-384 Digital Signatures (ES256 / ES384)
625
+ ***************************************************************************** */
626
+
627
+ /*
628
+ * Defines keypair / public_key / sign / verify for one NIST curve.
629
+ *
630
+ * Secret keys are big-endian scalars (`bytes_`), public keys are uncompressed
631
+ * SEC 1 points (0x04 || x || y; verification also accepts compressed points),
632
+ * signatures are DER encoded and messages are hashed with `hash_fn_`.
633
+ */
634
+ #define IODINE_CRYPTO_ECDSA_DEF(curve, bytes_, sig_max_, hash_fn_) \
635
+ /** Generates a new key pair: [secret_key, public_key]. */ \
636
+ FIO_SFUNC VALUE iodine_crypto_##curve##_keypair(VALUE self) { \
637
+ uint8_t sk[bytes_], pk[(bytes_ * 2) + 1]; \
638
+ if (fio_##curve##_keypair(sk, pk)) \
639
+ rb_raise(rb_eRuntimeError, "key generation failed"); \
640
+ VALUE secret = rb_str_new((const char *)sk, bytes_); \
641
+ VALUE public = rb_str_new((const char *)pk, (bytes_ * 2) + 1); \
642
+ fio_memset(sk, 0, bytes_); \
643
+ return rb_ary_new_from_args(2, secret, public); \
644
+ (void)self; \
645
+ } \
646
+ /** Derives the (uncompressed) public key from a secret key. */ \
647
+ FIO_SFUNC VALUE iodine_crypto_##curve##_public_key(int argc, \
648
+ VALUE *argv, \
649
+ VALUE self) { \
650
+ fio_buf_info_s sk = FIO_BUF_INFO0; \
651
+ iodine_rb2c_arg(argc, argv, IODINE_ARG_BUF(sk, 0, "secret_key", 1)); \
652
+ if (sk.len != bytes_) \
653
+ rb_raise(rb_eArgError, \
654
+ "secret_key must be " #bytes_ " bytes (got %zu)", \
655
+ sk.len); \
656
+ uint8_t pk[(bytes_ * 2) + 1]; \
657
+ if (fio_##curve##_public_key(pk, (const uint8_t *)sk.buf)) \
658
+ rb_raise(rb_eArgError, "secret_key is out of range"); \
659
+ return rb_str_new((const char *)pk, (bytes_ * 2) + 1); \
660
+ (void)self; \
661
+ } \
662
+ /** Signs `data` (hashed internally), returning a DER signature. */ \
663
+ FIO_SFUNC VALUE iodine_crypto_##curve##_sign(int argc, \
664
+ VALUE *argv, \
665
+ VALUE self) { \
666
+ fio_buf_info_s data = FIO_BUF_INFO0; \
667
+ fio_buf_info_s sk = FIO_BUF_INFO0; \
668
+ iodine_rb2c_arg(argc, \
669
+ argv, \
670
+ IODINE_ARG_BUF(data, 0, "data", 1), \
671
+ IODINE_ARG_BUF(sk, 0, "secret_key", 1)); \
672
+ if (sk.len != bytes_) \
673
+ rb_raise(rb_eArgError, \
674
+ "secret_key must be " #bytes_ " bytes (got %zu)", \
675
+ sk.len); \
676
+ fio_u512 h = hash_fn_(data.buf, data.len); \
677
+ uint8_t sig[sig_max_]; \
678
+ size_t sig_len = 0; \
679
+ if (fio_ecdsa_##curve##_sign(sig, \
680
+ &sig_len, \
681
+ sizeof(sig), \
682
+ h.u8, \
683
+ (const uint8_t *)sk.buf)) \
684
+ rb_raise(rb_eArgError, "signing failed (invalid secret_key?)"); \
685
+ return rb_str_new((const char *)sig, (long)sig_len); \
686
+ (void)self; \
687
+ } \
688
+ /** Verifies a DER signature over `data` (hashed internally). */ \
689
+ FIO_SFUNC VALUE iodine_crypto_##curve##_verify(int argc, \
690
+ VALUE *argv, \
691
+ VALUE self) { \
692
+ fio_buf_info_s data = FIO_BUF_INFO0; \
693
+ fio_buf_info_s sig = FIO_BUF_INFO0; \
694
+ fio_buf_info_s pk = FIO_BUF_INFO0; \
695
+ iodine_rb2c_arg(argc, \
696
+ argv, \
697
+ IODINE_ARG_BUF(data, 0, "data", 1), \
698
+ IODINE_ARG_BUF(sig, 0, "signature", 1), \
699
+ IODINE_ARG_BUF(pk, 0, "public_key", 1)); \
700
+ fio_u512 h = hash_fn_(data.buf, data.len); \
701
+ return fio_ecdsa_##curve##_verify((const uint8_t *)sig.buf, \
702
+ sig.len, \
703
+ h.u8, \
704
+ (const uint8_t *)pk.buf, \
705
+ pk.len) \
706
+ ? Qfalse \
707
+ : Qtrue; \
708
+ (void)self; \
709
+ }
710
+
711
+ /* SHA-256 widened to the shared fio_u512 return type. */
712
+ FIO_IFUNC fio_u512 iodine___crypto_sha256_512(const void *data, uint64_t len) {
713
+ fio_u512 r = {0};
714
+ r.u256[0] = fio_sha256(data, len);
715
+ return r;
716
+ }
717
+
718
+ IODINE_CRYPTO_ECDSA_DEF(p256, 32, 72, iodine___crypto_sha256_512)
719
+ IODINE_CRYPTO_ECDSA_DEF(p384, 48, 104, fio_sha384)
720
+ #undef IODINE_CRYPTO_ECDSA_DEF
721
+
620
722
  /* *****************************************************************************
621
723
  X25519 Key Exchange
622
724
  ***************************************************************************** */
@@ -1361,6 +1463,21 @@ static void Init_Iodine_Crypto(void) {
1361
1463
  iodine_crypto_ed25519_to_x25519_public,
1362
1464
  -1);
1363
1465
 
1466
+ /* Iodine::Base::Crypto::P256 / P384 (ECDSA) */
1467
+ #define IODINE_CRYPTO_ECDSA_REG(var, name, curve) \
1468
+ var = rb_define_module_under(iodine_rb_CRYPTO, name); \
1469
+ STORE.hold(var); \
1470
+ rb_define_module_function(var, "keypair", iodine_crypto_##curve##_keypair, 0); \
1471
+ rb_define_module_function(var, \
1472
+ "public_key", \
1473
+ iodine_crypto_##curve##_public_key, \
1474
+ -1); \
1475
+ rb_define_module_function(var, "sign", iodine_crypto_##curve##_sign, -1); \
1476
+ rb_define_module_function(var, "verify", iodine_crypto_##curve##_verify, -1);
1477
+ IODINE_CRYPTO_ECDSA_REG(iodine_rb_P256, "P256", p256)
1478
+ IODINE_CRYPTO_ECDSA_REG(iodine_rb_P384, "P384", p384)
1479
+ #undef IODINE_CRYPTO_ECDSA_REG
1480
+
1364
1481
  /* Iodine::Base::Crypto::X25519 */
1365
1482
  iodine_rb_X25519 = rb_define_module_under(iodine_rb_CRYPTO, "X25519");
1366
1483
  STORE.hold(iodine_rb_X25519);
@@ -213,7 +213,8 @@ static VALUE iodine_listener_map(int argc, VALUE *argv, VALUE o) {
213
213
  settings.public_folder = FIO_STR_INFO0;
214
214
  // TODO: test for a handler's public folder property?
215
215
  fio_http_route FIO_NOOP((fio_http_listener_s *)l->listener,
216
- RSTRING_PTR(url),
216
+ IODINE_STORE_IS_SKIP(url) ? "/"
217
+ : RSTRING_PTR(url),
217
218
  settings);
218
219
  }
219
220
  } else { /* not HTTP, URLs are invalid. */
@@ -44,8 +44,8 @@ static uint64_t iodinde_hmap_hash(VALUE k) {
44
44
  fio_buf_info_s buf = (fio_buf_info_s)IODINE_RSTR_INFO(k);
45
45
  return fio_risky_hash(buf.buf, buf.len, (uint64_t)&iodinde_hmap_hash);
46
46
  }
47
- if (RB_SYMBOL_P(k) || RB_FIXNUM_P(k) || k == RUBY_T_TRUE ||
48
- k == RUBY_T_FALSE || k == RUBY_T_NIL)
47
+ if (RB_SYMBOL_P(k) || RB_FIXNUM_P(k) || k == Qtrue || k == Qfalse ||
48
+ k == Qnil)
49
49
  return fio_risky_num((uint64_t)k, (uint64_t)&iodinde_hmap_hash);
50
50
  rb_raise(rb_eTypeError, "key MUST be either a String or a Symbol.");
51
51
  return 0;
@@ -218,13 +218,27 @@ Wrapper API
218
218
  Assigns the TLS context a public certificate, allowing remote parties to
219
219
  validate the connection's identity.
220
220
 
221
- A self signed certificate is automatically created if the `name` argument
222
- is specified and either (or both) of the `cert` (public certificate) or `key`
223
- (private key) arguments are missing.
224
-
225
- Some implementations allow servers to have more than a single certificate, which
226
- will be selected using the SNI extension. I believe the existing OpenSSL
227
- implementation supports this option (untested).
221
+ On servers, a self signed certificate (ECDSA P-256 on the `:iodine` backend)
222
+ is created for `name` if either (or both) of the `cert` (public certificate) or
223
+ `key` (private key) arguments are missing. This is fine for development, but
224
+ browsers will warn - use a CA issued certificate in production.
225
+
226
+ Servers may hold several certificates; both TLS backends select one per
227
+ handshake using the client's SNI host name:
228
+
229
+ 1. a case-insensitive exact match of `name`;
230
+ 2. else a single-label wildcard (`*.example.com` matches `www.example.com`,
231
+ but neither `example.com` nor `a.b.example.com`);
232
+ 3. else the default certificate: the one added with an empty `name`, else the
233
+ first one added.
234
+
235
+ Re-adding a `name` replaces its certificate. An omitted `name` defaults to
236
+ `"localhost"`.
237
+
238
+ On clients, the (default) certificate is presented when the server requests
239
+ one (mutual TLS / client certificate authentication), with its whole chain.
240
+ Use client certificates with PEM files: a name-only client entry presents no
241
+ certificate on the `:iodine` backend.
228
242
 
229
243
  Iodine::TLS#add_cert(name = nil,
230
244
  cert = nil,
@@ -247,8 +247,6 @@ FIO_SFUNC VALUE iodine_utils_is_eq(VALUE mod, VALUE a, VALUE b) { // clang-forma
247
247
  Randomness and Friends
248
248
  ***************************************************************************** */
249
249
 
250
- FIO_DEFINE_RANDOM128_FN(static, iodine_random, 11, 0)
251
-
252
250
  FIO_SFUNC VALUE iodine_utils_hmac512(VALUE self, VALUE secret, VALUE massage) {
253
251
  rb_check_type(secret, RUBY_T_STRING);
254
252
  rb_check_type(massage, RUBY_T_STRING);
@@ -420,12 +418,20 @@ FIO_SFUNC VALUE iodine_utils_risky_hash(int argc, VALUE *argv, VALUE self) {
420
418
  /**
421
419
  * Computes facil.io Risky256 Hash (non-cryptographic, 256-bit).
422
420
  *
421
+ * When `key:` is given, computes the keyed (single pass) variant instead.
422
+ *
423
423
  * @param data [String] Input data to hash
424
+ * @param key: [String] Optional secret key (keyed hashing)
424
425
  * @return [String] 32-byte binary hash
425
426
  */
426
- FIO_SFUNC VALUE iodine_utils_risky256(VALUE self, VALUE data) {
427
- rb_check_type(data, RUBY_T_STRING);
428
- fio_u256 h = fio_risky256(RSTRING_PTR(data), (uint64_t)RSTRING_LEN(data));
427
+ FIO_SFUNC VALUE iodine_utils_risky256(int argc, VALUE *argv, VALUE self) {
428
+ fio_buf_info_s data = FIO_BUF_INFO0, key = FIO_BUF_INFO0;
429
+ iodine_rb2c_arg(argc,
430
+ argv,
431
+ IODINE_ARG_BUF(data, 0, "data", 1),
432
+ IODINE_ARG_BUF(key, 0, "key", 0));
433
+ fio_u256 h = key.buf ? fio_risky256_keyed(key.buf, key.len, data.buf, data.len)
434
+ : fio_risky256(data.buf, data.len);
429
435
  return rb_str_new((const char *)h.u8, 32);
430
436
  (void)self;
431
437
  }
@@ -433,14 +439,21 @@ FIO_SFUNC VALUE iodine_utils_risky256(VALUE self, VALUE data) {
433
439
  /**
434
440
  * Computes facil.io Risky512 Hash (non-cryptographic, 512-bit).
435
441
  *
436
- * First 256 bits are identical to risky256 (SHAKE-style extension).
442
+ * First 256 bits are identical to risky256 (SHAKE-style extension), also when
443
+ * the same `key:` is given to both.
437
444
  *
438
445
  * @param data [String] Input data to hash
446
+ * @param key: [String] Optional secret key (keyed hashing)
439
447
  * @return [String] 64-byte binary hash
440
448
  */
441
- FIO_SFUNC VALUE iodine_utils_risky512(VALUE self, VALUE data) {
442
- rb_check_type(data, RUBY_T_STRING);
443
- fio_u512 h = fio_risky512(RSTRING_PTR(data), (uint64_t)RSTRING_LEN(data));
449
+ FIO_SFUNC VALUE iodine_utils_risky512(int argc, VALUE *argv, VALUE self) {
450
+ fio_buf_info_s data = FIO_BUF_INFO0, key = FIO_BUF_INFO0;
451
+ iodine_rb2c_arg(argc,
452
+ argv,
453
+ IODINE_ARG_BUF(data, 0, "data", 1),
454
+ IODINE_ARG_BUF(key, 0, "key", 0));
455
+ fio_u512 h = key.buf ? fio_risky512_keyed(key.buf, key.len, data.buf, data.len)
456
+ : fio_risky512(data.buf, data.len);
444
457
  return rb_str_new((const char *)h.u8, 64);
445
458
  (void)self;
446
459
  }
@@ -573,7 +586,7 @@ FIO_SFUNC VALUE iodine_utils_hmac_poly(VALUE self,
573
586
 
574
587
  FIO_SFUNC VALUE iodine_utils_uuid(int argc, VALUE *argv, VALUE self) {
575
588
  VALUE r = Qnil;
576
- fio_u128 rand = iodine_random128();
589
+ fio_u128 rand = fio_rand128();
577
590
  fio_buf_info_s secret = {0};
578
591
  fio_buf_info_s info = {0};
579
592
  FIO_STR_INFO_TMP_VAR(str, 128);
@@ -641,7 +654,7 @@ FIO_SFUNC VALUE iodine_utils_random(int argc, VALUE *argv, VALUE self) {
641
654
  if ((size - 1) > 0x0FFFFFFF)
642
655
  rb_raise(rb_eRangeError, "`bytes` count is out of range.");
643
656
  r = rb_str_buf_new(size);
644
- iodine_random_bytes(RSTRING_PTR(r), size);
657
+ fio_rand_bytes(RSTRING_PTR(r), size);
645
658
  rb_str_set_len(r, size);
646
659
  return r;
647
660
  (void)self;
@@ -915,16 +928,12 @@ static void Init_Iodine_Utils(void) {
915
928
  rb_define_singleton_method(m, "sha1", iodine_utils_sha1, 1);
916
929
  rb_define_singleton_method(m, "crc32", iodine_utils_crc32, -1);
917
930
  rb_define_singleton_method(m, "risky_hash", iodine_utils_risky_hash, -1);
918
- rb_define_singleton_method(m, "risky256", iodine_utils_risky256, 1);
919
- rb_define_singleton_method(m, "risky512", iodine_utils_risky512, 1);
931
+ rb_define_singleton_method(m, "risky256", iodine_utils_risky256, -1);
932
+ rb_define_singleton_method(m, "risky512", iodine_utils_risky512, -1);
920
933
  rb_define_singleton_method(m, "risky256_hmac", iodine_utils_risky256_hmac, 2);
921
934
  rb_define_singleton_method(m, "risky512_hmac", iodine_utils_risky512_hmac, 2);
922
935
  rb_define_singleton_method(m, "secure_random", iodine_utils_secure_random, -1);
923
936
  rb_define_singleton_method(m, "blake2b", iodine_utils_blake2b, -1);
924
937
  rb_define_singleton_method(m, "blake2s", iodine_utils_blake2s, -1);
925
-
926
-
927
-
928
- fio_state_callback_add(FIO_CALL_IN_CHILD, iodine_random_on_fork, NULL);
929
938
  } // clang-format on
930
939
  #endif /* H___IODINE_UTILS___H */
@@ -186,6 +186,7 @@ module Iodine
186
186
  # hmac512 => HMAC-SHA512 (compared against OpenSSL HMAC-SHA512)
187
187
  # risky256_hmac => keyed MAC on non-crypto Risky Hash — NOT for security-critical use
188
188
  # risky512_hmac => keyed MAC on non-crypto Risky Hash — NOT for security-critical use
189
+ # risky256(key:) / risky512(key:) => single pass keyed Risky Hash — NOT for security-critical use
189
190
  #
190
191
  # Payload: 512 bytes of the benchmark file itself (representative of a
191
192
  # short-to-medium authenticated payload).
@@ -194,7 +195,7 @@ module Iodine
194
195
  benchmark_payload = (File.exist?(__FILE__) ? IO.binread(__FILE__)[0...512] : 'my message payload')
195
196
 
196
197
  puts "HMAC benchmarks — all MACs ranked — #{benchmark_payload.bytesize}-byte message, #{benchmark_secret.bytesize}-byte secret"
197
- puts " [WARNING: risky256_hmac / risky512_hmac are NOT suitable for security-critical authentication]"
198
+ puts " [WARNING: risky256_hmac / risky512_hmac / keyed risky256 / risky512 are NOT suitable for security-critical authentication]"
198
199
 
199
200
  # Pre-create OpenSSL digest objects outside the hot loop. Compare the
200
201
  # same Base64 output format as Iodine's hmac* methods.
@@ -212,6 +213,13 @@ module Iodine
212
213
  bm.report('Iodine::Utils.risky512_hmac (non-crypto, 64B)') do
213
214
  Iodine::Utils.risky512_hmac(benchmark_secret, benchmark_payload)
214
215
  end
216
+ # Keyed Risky Hash — single pass alternative to the HMAC construction
217
+ bm.report('Iodine::Utils.risky256(key:) (non-crypto, 32B)') do
218
+ Iodine::Utils.risky256(benchmark_payload, key: benchmark_secret)
219
+ end
220
+ bm.report('Iodine::Utils.risky512(key:) (non-crypto, 64B)') do
221
+ Iodine::Utils.risky512(benchmark_payload, key: benchmark_secret)
222
+ end
215
223
  # Poly1305 MAC — no direct OpenSSL equivalent; shown for reference only
216
224
  bm.report('Iodine::Utils.hmac128 (Poly1305) ') do
217
225
  Iodine::Utils.hmac128(benchmark_secret, benchmark_payload)
@@ -1476,7 +1484,7 @@ module Iodine
1476
1484
 
1477
1485
  # Benchmarks Iodine's non-cryptographic hash functions.
1478
1486
  #
1479
- # Tests risky_hash (64-bit), risky256, risky512, and crc32 against
1487
+ # Tests risky_hash (64-bit), risky256, risky512 (unkeyed and keyed), and crc32 against
1480
1488
  # Ruby stdlib equivalents where available.
1481
1489
  #
1482
1490
  # FAIRNESS NOTES:
@@ -1509,6 +1517,8 @@ module Iodine
1509
1517
  # Label width — wide enough for 'Iodine risky512 (non-crypto, 64B)'
1510
1518
  lw = 32
1511
1519
 
1520
+ key = Iodine::Utils.secure_random(bytes: 32)
1521
+
1512
1522
  sizes.each do |size_name, size|
1513
1523
  data = Iodine::Utils.random(size)
1514
1524
 
@@ -1534,6 +1544,13 @@ module Iodine
1534
1544
  x.report('Iodine risky512 (non-crypto, 64B)'.ljust(lw)) do
1535
1545
  Iodine::Utils.risky512(data)
1536
1546
  end
1547
+ # keyed variants (single pass) — e.g., hash flooding resistant keys
1548
+ x.report('Iodine risky256 keyed (32B)'.ljust(lw)) do
1549
+ Iodine::Utils.risky256(data, key: key)
1550
+ end
1551
+ x.report('Iodine risky512 keyed (64B)'.ljust(lw)) do
1552
+ Iodine::Utils.risky512(data, key: key)
1553
+ end
1537
1554
  # Zlib.crc32 — direct equivalent for crc32 (same ITU-T V.42 polynomial)
1538
1555
  x.report('Zlib.crc32'.ljust(lw)) do
1539
1556
  Zlib.crc32(data)
@@ -1651,7 +1668,7 @@ module Iodine
1651
1668
  # - crypto_kdf (HKDF key derivation)
1652
1669
  # - crypto_totp (TOTP generation/verification)
1653
1670
  # - compression (Deflate, Gzip, Brotli)
1654
- # - non_crypto_hashing (risky_hash, crc32, risky256, risky512)
1671
+ # - non_crypto_hashing (risky_hash, crc32, risky256, risky512, keyed variants)
1655
1672
  # - crypto_pqkem (X25519+ML-KEM-768 post-quantum hybrid KEM)
1656
1673
  def self.crypto_all
1657
1674
  puts "\n" + '=' * 70