Skip to content

Add nat20crypto module to linux examples#100

Open
werwurm wants to merge 34 commits into
mainfrom
werwurm/linux_example_nat20crypto
Open

Add nat20crypto module to linux examples#100
werwurm wants to merge 34 commits into
mainfrom
werwurm/linux_example_nat20crypto

Conversation

@werwurm
Copy link
Copy Markdown
Contributor

@werwurm werwurm commented May 4, 2026

The nat20crypto module implements the libnat20 crypto interface
in terms of linux kernel crypto primitives.
The module implements

  • deterministic ECDSA with curves P256 and P384.
  • Bytewise SHA-2 224/256/384/512
  • HMAC
  • HKDF

ED25519 is currently not supported.

werwurm added 10 commits April 29, 2026 09:34
Add a kernel module that provides libnat20 functionality to linux kernel
modules. Also add a configuration to build a minimal linux image with
buildroot and run in on qemu and a workflow to test build nat20lib.ko
This module creates a new character device class intended to implement
the nat20 service protocol implementing DICE based device state
attestation and an embedded CA.
The nat20crypto module implements the libnat20 crypto interface
in terms of linux kernel crypto primitives.
The module implements
- deterministic ECDSA with curves P256 and P384.
- Bytewise SHA-2 224/256/384/512
- HMAC
- HKDF

ED25519 is currently not supported.
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 4, 2026

LCOV of commit f5285c6 during lcov-test-coverage-report #199

Summary coverage rate:
  lines......: 95.7% (3013 of 3147 lines)
  functions..: 99.1% (230 of 232 functions)
  branches...: 87.2% (1646 of 1887 branches)

Files changed coverage rate: n/a

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds a new nat20crypto Linux kernel module example that implements the libnat20 crypto interface using Linux kernel primitives (notably SHA-2 and ECC), and wires it into the Buildroot-based Linux examples and CI so it is built and packaged alongside existing NAT20 components.

Changes:

  • Introduce the examples/linux/nat20crypto kernel module (digest + deterministic ECDSA + key handling glue).
  • Add a Buildroot package (nat20crypto) and enable it in the QEMU Buildroot defconfig.
  • Extend developer tooling (envsetup.sh) and CI workflow to rebuild/build and verify nat20crypto.ko.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
examples/linux/nat20crypto/nat20crypto.c Implements the nat20 crypto context (digest, kdf, ECDSA signing, pubkey export, key lifecycle) on Linux.
examples/linux/nat20crypto/Makefile Adds standalone build/install targets for the nat20crypto kernel module.
examples/linux/nat20crypto/Kbuild Defines module build flags/includes and extra symbols dependency on nat20lib.
examples/linux/nat20crypto/include/nat20crypto.h Exposes module entry points (open/close/make_secret) for consumers.
examples/linux/br_external/utils/envsetup.sh Adds NAT20CRYPTO_OVERRIDE_SRCDIR and brrebuild nat20crypto support.
examples/linux/br_external/package/nat20crypto/nat20crypto.mk Adds Buildroot packaging/build instructions for the nat20crypto kernel module.
examples/linux/br_external/package/nat20crypto/Config.in Adds Buildroot Kconfig option for enabling nat20crypto.
examples/linux/br_external/configs/qemu_br_defconfig Enables BR2_PACKAGE_NAT20CRYPTO=y in the QEMU Buildroot config.
examples/linux/br_external/Config.in Registers the nat20crypto package Kconfig entry in the external tree.
.github/workflows/linux-kmod-build.yml Builds and verifies nat20crypto.ko in CI.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread examples/linux/nat20crypto/nat20crypto.c
Comment thread examples/linux/nat20crypto/nat20crypto.c
Comment thread examples/linux/nat20crypto/nat20crypto.c
Comment thread examples/linux/nat20crypto/nat20crypto.c
Comment thread examples/linux/nat20crypto/nat20crypto.c
Comment thread examples/linux/nat20crypto/nat20crypto.c Outdated
Comment thread examples/linux/br_external/package/nat20crypto/nat20crypto.mk Outdated
Comment thread examples/linux/nat20crypto/nat20crypto.c
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 11 out of 11 changed files in this pull request and generated 5 comments.

Comment thread examples/linux/nat20crypto/Kbuild Outdated
Comment thread examples/linux/nat20crypto/nat20crypto.c
Comment thread examples/linux/nat20crypto/nat20crypto.c Outdated
Comment thread examples/linux/nat20crypto/nat20crypto.c Outdated
Comment thread examples/linux/nat20crypto/SECURITY.md
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 11 out of 11 changed files in this pull request and generated 4 comments.

Comment thread examples/linux/nat20crypto/nat20crypto.c Outdated
Comment thread examples/linux/nat20crypto/nat20crypto.c Outdated
Comment thread examples/linux/nat20crypto/nat20crypto.c
Comment thread examples/linux/nat20crypto/Makefile Outdated
@werwurm werwurm changed the base branch from werwurm/linux_example_nat20device to main May 7, 2026 20:42
@werwurm werwurm marked this pull request as ready for review May 7, 2026 20:48
@werwurm werwurm requested a review from a team as a code owner May 7, 2026 20:48
@werwurm werwurm requested review from seidelrj, smacdude and timhirsh May 7, 2026 20:48
Comment thread examples/linux/nat20crypto/include/nat20crypto.h
Comment thread examples/linux/nat20crypto/nat20crypto.c Outdated
/* This variant is used for ECC keys. */
struct {
size_t ndigits;
uint64_t digits[6];
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where does the 6 come from? is it 48 bytes for ECC-384?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

.size = context_size,
.buffer = context_buffer,
},
32,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sizeof(derived)?

};
/* This variant is used for CDIs. */
struct {
uint8_t bits[32];
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

constant for the 32 bytes (256 bit key). then the constant can be used for created the buffers in the kdf function.

rc = n20_error_crypto_no_resources_e;
goto out;
}
memcpy(new_cdi_key->bits, derived, 32);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sizeof(derived)?

case n20_crypto_key_type_secp256r1_e:
case n20_crypto_key_type_secp384r1_e: {
n20_slice_t x_octets = {
.size = 32,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sizeof(derived)?

}

/* Mod Invert k */
vli_mod_inv(k_inv, k, curve->n, ndigits);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i feel like that its crazy that linux doesn't have a built-in ecc sign function and you have to do the math yourself.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It kind of makes sense.This kind of functionality should really be delegated to secure elements. Checking signatures is different because it doesn't involve confidential key material.

Copy link
Copy Markdown
Member

@timhirsh timhirsh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving .github/workflows/linux-kmod-build.yml.

@werwurm werwurm requested a review from seidelrj May 11, 2026 18:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants