Skip to content

Commit

Permalink
Merge pull request #32 from r4gus/no-alloc
Browse files Browse the repository at this point in the history
No alloc
  • Loading branch information
r4gus authored Jun 13, 2024
2 parents 4a9a400 + 7f8ffd1 commit 42cd563
Show file tree
Hide file tree
Showing 36 changed files with 942 additions and 1,277 deletions.
52 changes: 34 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
> We track the latest stable release of Zig (`0.12.0`)
> [!IMPORTANT]
> There seems to be an [issue](https://github.com/ziglang/zig/issues/17204) with the export of
> header files that should be fixed for `0.12.0`. Still, I haven't figured out how to import
> headers from another module in Zig `0.12.0`, meaning the `client-example` won't build.
FIDO2 compatible authenticator library written in [Zig](https://ziglang.org/).
FIDO2 compatible authenticator and client library written in [Zig](https://ziglang.org/). The authenticator part requires __zero dynamic allocations__.

If you want to see an example on how to use keylib, check out [PassKeeZ](https://github.com/r4gus/keypass).
> We track the latest stable release of Zig (`0.12.0`)
Also, check out the [Wiki](https://github.com/r4gus/keylib/wiki).
If you want to see an example on how the library could be used, check out [PassKeeZ](https://github.com/r4gus/keypass).

## QA

<details>
<summary><ins>What is FIDO2?</ins></summary>

FIDO2 is a protocol designed for authentication purposes. It can be used as single factor (e.g., as a replacement for password based authentication) or as a second factor.
FIDO2 is a protocol designed for authentication purposes. It can be used as single factor (e.g., as a replacement for password based authentication) or as a second factor (e.g., instead of OTPs).

</details>

Expand All @@ -25,7 +18,7 @@ FIDO2 is a protocol designed for authentication purposes. It can be used as sing

Passkey is a marketing term which is used to refer to a specific FIDO2 authenticator configuration. A authenticator can be configured to use so called discoverable credentials (also referred to as resident keys). Those credentials are stored somewhere on your device, e.g. in a encrypted database. Devices can also be protected by some form of user verification. This can be a PIN or a built in user verification method like a finger print scanner. Passkey refers to FIDO2 using discoverable credentials and some form of user verification.

Please note that this is only one interpretation of what PassKey means as the term itself is nowhere defined.
Please note that this is only one interpretation of what PassKey means as the term itself is nowhere defined (see also [Passkeys's: A Shattered Dream](https://fy.blackhats.net.au/blog/2024-04-26-passkeys-a-shattered-dream/)).

</details>

Expand Down Expand Up @@ -55,20 +48,25 @@ You might have noticed that FIDO2, PassKey and even WebAuthn are often used inte
FIDO2 has a lot of advantages compared to passwords:

1. No secret information is shared, i.e. the private key stays on the authenticator or is protected, e.g. using key wrapping.
2. Each credential is bound to a relying party id (e.g. google.com), which makes social engineering attacks, like phishing websites, quite difficult (maybe impossible).
2. Each credential is bound to a relying party id (e.g. google.com), which makes social engineering attacks, like phishing websites, quite difficult (as long as the client verifies the relying party id properly).
3. Users don't have to be concerned with problems like password complexity.
4. If well implemented, FIDO2 provides a better user experience (e.g., faster logins).
5. A recent paper showed that with some adoptions, FIDO2 is ready for a post quantum world under certain conditions ([FIDO2, CTAP 2.1, and WebAuthn 2: Provable Security and Post-Quantum Instantiation, Cryptology ePrint Archive, Paper 2022/1029](https://eprint.iacr.org/2022/1029.pdf)).

</details>

<details>
<summary><ins>Why shouldn't I use FIDO2?</ins></summary>
<summary><ins>Are there problems with FIDO2?</ins></summary>

Yes, there are:

1. The two FIDO2 subprotocols (CTAP2 and WebAuthn) are way more difficult to implement, compared to password authentication.
1. The two FIDO2 subprotocols (CTAP2 and WebAuthn) are way more difficult to implement, compared to password authentication.
2. There are more points of failure because you have three parties that are involved in the authentication process (authenticator, client, relying party).
3. Currently not all browsers support the CTAP2 protocol well (especially on Linux).
4. You don't want to spend money on an authenticator (you usually can't upgrade) and/or you don't trust platform authenticators.
4. There is no way to verify that a client is trustworthy:
* Rogue clients may communicate with a authenticator without your consent
* Clients may display wrong information
5. The 4th layer introduced for Android, IOS, and Windows to connect authenticators and clients internally could be used as a man in the middle.

</details>

Expand All @@ -88,6 +86,23 @@ We offer support for operations like __authenticatorMakeCredential__, __authenti

</details>

<details>
<summary><ins>Zero dynamic allocations?</ins></summary>

The authenticator part of this library doesn't allocate any memory dynamically. This has some draw backs like a fixed
size for strings (e.g., rpId, user name, etc.) but also reduces the complexity of the code.

The authenticator example uses `88655` bytes of stack space when compiled with `-Doptimize=ReleaseSmall` on Linux (x86\_64).

> The authenticator example has been profiled using valgrind.
> * `zig build auth-example -Doptimize=ReleaseSmall`
> * `valgrind --tool=drd --show-stack-usage=yes ./zig-out/bin/authenticator`
> * Test page: [webauthn.io](https://webauthn.io/) - Register + Authentication
> `thread 1 finished and used 88655 bytes out of 8388608 on its stack.`
> `ThinkPad-X1-Yoga-3rd 6.5.0-35-generic #35~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC x86_64 GNU/Linux`
</details>

## Design

![keylib design](static/design.png)
Expand All @@ -107,8 +122,9 @@ We maintain two examples on how to use the library:

---

- [Passkey test site](https://passkey.org/)
- [FIDO2 test site](https://webauthn.io/)
__FIDO2/Passkey test sites__:
- [passkey.org](https://passkey.org/)
- [webauthn.io](https://webauthn.io/)

## Random Ideas

Expand Down
34 changes: 17 additions & 17 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -86,23 +86,23 @@ pub fn build(b: *std.Build) !void {
// C bindings
// ------------------------------------------------

const c_bindings = b.addStaticLibrary(.{
.name = "keylib",
.root_source_file = .{ .path = "bindings/c/src/keylib.zig" },
.target = target,
.optimize = optimize,
});
c_bindings.root_module.addImport("keylib", keylib_module);
c_bindings.linkLibC();
c_bindings.installHeadersDirectory(
b.path("bindings/c/include"),
"keylib",
.{
.exclude_extensions = &.{},
.include_extensions = &.{".h"},
},
);
b.installArtifact(c_bindings);
//const c_bindings = b.addStaticLibrary(.{
// .name = "keylib",
// .root_source_file = .{ .path = "bindings/c/src/keylib.zig" },
// .target = target,
// .optimize = optimize,
//});
//c_bindings.root_module.addImport("keylib", keylib_module);
//c_bindings.linkLibC();
//c_bindings.installHeadersDirectory(
// b.path("bindings/c/include"),
// "keylib",
// .{
// .exclude_extensions = &.{},
// .include_extensions = &.{".h"},
// },
//);
//b.installArtifact(c_bindings);

const uhid = b.addStaticLibrary(.{
.name = "uhid",
Expand Down
5 changes: 3 additions & 2 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
.version = "0.4.0",
.dependencies = .{
.zbor = .{
.url = "https://github.com/r4gus/zbor/archive/refs/tags/0.13.1.tar.gz",
.hash = "122051750f4d7ecc9b705f1bbea1d431956306c74dafa4e9b625b11a34fab4c06933",
.url = "https://github.com/r4gus/zbor/archive/refs/tags/0.14.1.tar.gz",
.hash = "122075870879317c0823cb584b6a219160ddd46ec91d3311bc6ad086701afa914961",
//.path = "../zbor",
},
.hidapi = .{
.url = "https://github.com/r4gus/hidapi/archive/master.tar.gz",
Expand Down
Loading

0 comments on commit 42cd563

Please sign in to comment.