Bind liboqs ML-DSA-44 signatures, random bytes, and cleansing
Oqs pins liboqs as a source package and wraps the ML-DSA-44 key pair, sign, and verify calls, secure random bytes, and memory cleansing in slice-taking procedures that check buffer sizes and size_t range. The test builds liboqs with only ML-DSA-44 from the locked checkout and signs, verifies, and rejects a changed message, a changed signature, and another key.
This commit is contained in:
commit
595b7a9430
8 files changed
+289
No files matched your search
@@ -0,0 +1,2 @@
|
||||
build/
|
||||
ziran.local.toml
|
||||
@@ -0,0 +1,28 @@
|
||||
BSD 3-Clause License
|
||||
|
||||
Copyright (c) 2026, Waozi
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
3. Neither the name of the copyright holder nor the names of its contributors
|
||||
may be used to endorse or promote products derived from this software without
|
||||
specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
@@ -0,0 +1,32 @@
|
||||
.DEFAULT_GOAL := check
|
||||
|
||||
ZIRAN ?= ziran
|
||||
CC ?= cc
|
||||
CMAKE ?= cmake
|
||||
BUILD := build
|
||||
LIBOQS_BUILD := $(BUILD)/liboqs
|
||||
LIBOQS_A := $(LIBOQS_BUILD)/lib/liboqs.a
|
||||
|
||||
.PHONY: check test clean
|
||||
|
||||
# Checks the package, then signs and verifies against liboqs built from the
|
||||
# locked source with only ML-DSA-44.
|
||||
check: test
|
||||
|
||||
$(LIBOQS_A):
|
||||
$(CMAKE) -S "$$($(ZIRAN) pkg path liboqs)" -B $(LIBOQS_BUILD) \
|
||||
-DCMAKE_BUILD_TYPE=MinSizeRel -DBUILD_SHARED_LIBS=OFF \
|
||||
-DOQS_BUILD_ONLY_LIB=ON -DOQS_USE_OPENSSL=OFF -DOQS_DIST_BUILD=OFF \
|
||||
-DOQS_OPT_TARGET=generic -DOQS_MINIMAL_BUILD=SIG_ml_dsa_44
|
||||
$(CMAKE) --build $(LIBOQS_BUILD) --target oqs
|
||||
|
||||
test: $(LIBOQS_A)
|
||||
$(ZIRAN) check --project
|
||||
$(ZIRAN) build --project --target=c --entry oqs_test:main \
|
||||
-o $(BUILD)/test-c tests/oqs_test.zi
|
||||
$(CC) -std=c11 -I$$($(ZIRAN) pkg path ziran)/include -I$(BUILD)/test-c \
|
||||
$(BUILD)/test-c/*.c $(LIBOQS_A) -o $(BUILD)/oqs-test
|
||||
$(BUILD)/oqs-test
|
||||
|
||||
clean:
|
||||
rm -rf $(BUILD)
|
||||
@@ -0,0 +1,48 @@
|
||||
# Oqs
|
||||
|
||||
Post-quantum signatures for Ziran, from [liboqs](https://github.com/open-quantum-safe/liboqs)
|
||||
(Open Quantum Safe).
|
||||
|
||||
```sh
|
||||
ziran add https://github.com/ziranlang/oqs.git
|
||||
```
|
||||
|
||||
```jai
|
||||
#import "oqs/Oqs"
|
||||
|
||||
public_key: [MlDsa44PublicKeyBytes]u8;
|
||||
secret_key: [MlDsa44SecretKeyBytes]u8;
|
||||
signature: [MlDsa44SignatureBytes]u8;
|
||||
|
||||
Sign :: (message: []u8) -> bool {
|
||||
if !MlDsa44KeyPair(public_key[:], secret_key[:]) { return false }
|
||||
length := MlDsa44Sign(signature[:], message, secret_key[:])
|
||||
return length > 0 &&
|
||||
MlDsa44Verify(message, signature[0:length], public_key[:])
|
||||
}
|
||||
```
|
||||
|
||||
| Procedure | Does |
|
||||
|---|---|
|
||||
| `MlDsa44KeyPair(public_key, secret_key)` | Makes an ML-DSA-44 (FIPS 204) key pair |
|
||||
| `MlDsa44Sign(signature, message, secret_key)` | Signs; returns the signature's length, or -1 |
|
||||
| `MlDsa44Verify(message, signature, public_key)` | Checks a signature |
|
||||
| `RandomBytes(output)` | Fills bytes from the system's secure random source |
|
||||
| `Cleanse(memory)` | Zeroes secrets in a way the compiler keeps |
|
||||
|
||||
The wrappers take slices and refuse buffers too small for a key or
|
||||
signature, and lengths a 32-bit `size_t` cannot hold.
|
||||
|
||||
## Linking liboqs
|
||||
|
||||
The package pins liboqs as a source dependency. Build its static library
|
||||
from the locked checkout and link it with your program:
|
||||
|
||||
```sh
|
||||
cmake -S "$(ziran pkg path liboqs)" -B build/liboqs \
|
||||
-DBUILD_SHARED_LIBS=OFF -DOQS_BUILD_ONLY_LIB=ON -DOQS_USE_OPENSSL=OFF \
|
||||
-DOQS_DIST_BUILD=OFF -DOQS_MINIMAL_BUILD=SIG_ml_dsa_44
|
||||
cmake --build build/liboqs --target oqs
|
||||
```
|
||||
|
||||
`make` does this and runs the tests.
|
||||
+87
@@ -0,0 +1,87 @@
|
||||
// Post-quantum signatures from liboqs (Open Quantum Safe) for Ziran.
|
||||
//
|
||||
// The package pins liboqs as a source dependency; a build links the static
|
||||
// library it builds from `ziran pkg path liboqs`, for example with only
|
||||
// ML-DSA-44: -DOQS_MINIMAL_BUILD=SIG_ml_dsa_44 -DOQS_BUILD_ONLY_LIB=ON.
|
||||
// Lengths cross into C as size_t, which is 32 bits on some targets, so the
|
||||
// wrappers take slices and refuse lengths that do not fit.
|
||||
|
||||
liboqs :: #system_library "liboqs";
|
||||
|
||||
MlDsa44KeyPairRaw :: (public_key: *u8,
|
||||
secret_key: *u8) -> s32 #foreign liboqs "OQS_SIG_ml_dsa_44_keypair";
|
||||
MlDsa44SignRaw :: (signature: *u8, signature_length: *usize, message: *u8,
|
||||
message_length: usize,
|
||||
secret_key: *u8) -> s32 #foreign liboqs "OQS_SIG_ml_dsa_44_sign";
|
||||
MlDsa44VerifyRaw :: (message: *u8, message_length: usize, signature: *u8,
|
||||
signature_length: usize,
|
||||
public_key: *u8) -> s32 #foreign liboqs "OQS_SIG_ml_dsa_44_verify";
|
||||
RandomBytesRaw :: (output: *u8,
|
||||
length: usize) #foreign liboqs "OQS_randombytes";
|
||||
CleanseRaw :: (memory: *void,
|
||||
length: usize) #foreign liboqs "OQS_MEM_cleanse";
|
||||
|
||||
// Sizes of ML-DSA-44 (FIPS 204) keys and signatures, in bytes.
|
||||
MlDsa44PublicKeyBytes :: 1312
|
||||
MlDsa44SecretKeyBytes :: 2560
|
||||
MlDsa44SignatureBytes :: 2420
|
||||
|
||||
// liboqs reports success as 0.
|
||||
OqsSuccess :: 0
|
||||
|
||||
// A length the target's size_t can hold.
|
||||
SizeFits :: (length: s64) -> bool {
|
||||
return length >= 0 && (size_of(usize) >= 8 || length <= 4294967295)
|
||||
}
|
||||
|
||||
Start :: (bytes: []u8) -> *u8 {
|
||||
if bytes.count == 0 { return null }
|
||||
return *bytes[0]
|
||||
}
|
||||
|
||||
// Fills a new key pair. Both slices must be at least the key sizes.
|
||||
#program_export
|
||||
MlDsa44KeyPair :: (public_key: []u8, secret_key: []u8) -> bool {
|
||||
if public_key.count < MlDsa44PublicKeyBytes ||
|
||||
secret_key.count < MlDsa44SecretKeyBytes { return false }
|
||||
return MlDsa44KeyPairRaw(*public_key[0], *secret_key[0]) == OqsSuccess
|
||||
}
|
||||
|
||||
// Signs `message` with `secret_key` into `signature`, which must hold
|
||||
// MlDsa44SignatureBytes. Returns the signature's length, or -1.
|
||||
#program_export
|
||||
MlDsa44Sign :: (signature: []u8, message: []u8, secret_key: []u8) -> s64 {
|
||||
if signature.count < MlDsa44SignatureBytes ||
|
||||
secret_key.count < MlDsa44SecretKeyBytes ||
|
||||
!SizeFits(cast(s64)message.count) { return -1 }
|
||||
length: usize = 0
|
||||
if MlDsa44SignRaw(*signature[0], *length, Start(message),
|
||||
cast(usize)message.count, *secret_key[0]) != OqsSuccess { return -1 }
|
||||
return cast(s64)length
|
||||
}
|
||||
|
||||
// Whether `signature` signs `message` for `public_key`.
|
||||
#program_export
|
||||
MlDsa44Verify :: (message: []u8, signature: []u8, public_key: []u8) -> bool {
|
||||
if signature.count == 0 || public_key.count < MlDsa44PublicKeyBytes ||
|
||||
!SizeFits(cast(s64)message.count) ||
|
||||
!SizeFits(cast(s64)signature.count) { return false }
|
||||
return MlDsa44VerifyRaw(Start(message), cast(usize)message.count,
|
||||
*signature[0], cast(usize)signature.count,
|
||||
*public_key[0]) == OqsSuccess
|
||||
}
|
||||
|
||||
// Fills `output` from the system's secure random source.
|
||||
#program_export
|
||||
RandomBytes :: (output: []u8) {
|
||||
if output.count == 0 || !SizeFits(cast(s64)output.count) { return }
|
||||
RandomBytesRaw(*output[0], cast(usize)output.count)
|
||||
}
|
||||
|
||||
// Zeroes `memory` in a way the compiler does not remove, for keys and other
|
||||
// secrets about to be released.
|
||||
#program_export
|
||||
Cleanse :: (memory: []u8) {
|
||||
if memory.count == 0 || !SizeFits(cast(s64)memory.count) { return }
|
||||
CleanseRaw(cast(*void)*memory[0], cast(usize)memory.count)
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
#import "oqs"
|
||||
|
||||
public_key: [MlDsa44PublicKeyBytes]u8;
|
||||
secret_key: [MlDsa44SecretKeyBytes]u8;
|
||||
other_public: [MlDsa44PublicKeyBytes]u8;
|
||||
other_secret: [MlDsa44SecretKeyBytes]u8;
|
||||
signature: [MlDsa44SignatureBytes]u8;
|
||||
message: [32]u8;
|
||||
random: [64]u8;
|
||||
|
||||
AllZero :: (bytes: []u8) -> bool {
|
||||
index: s64 = 0
|
||||
while index < cast(s64)bytes.count {
|
||||
if bytes[index] != cast(u8)0 { return false }
|
||||
index += 1
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// A signature verifies for its own message and key, and for nothing else.
|
||||
#program_export
|
||||
main :: () -> s32 {
|
||||
if !MlDsa44KeyPair(public_key[:], secret_key[:]) { return 1 }
|
||||
if !MlDsa44KeyPair(other_public[:], other_secret[:]) { return 2 }
|
||||
index: s64 = 0
|
||||
while index < 32 {
|
||||
message[index] = cast(u8)index
|
||||
index += 1
|
||||
}
|
||||
length := MlDsa44Sign(signature[:], message[:], secret_key[:])
|
||||
if length <= 0 || length > MlDsa44SignatureBytes { return 3 }
|
||||
if !MlDsa44Verify(message[:], signature[0:length], public_key[:]) { return 4 }
|
||||
if MlDsa44Verify(message[:], signature[0:length], other_public[:]) { return 5 }
|
||||
message[5] = cast(u8)99
|
||||
if MlDsa44Verify(message[:], signature[0:length], public_key[:]) { return 6 }
|
||||
message[5] = cast(u8)5
|
||||
signature[0] = signature[0] ^ cast(u8)1
|
||||
if MlDsa44Verify(message[:], signature[0:length], public_key[:]) { return 7 }
|
||||
|
||||
// An empty message can be signed and verified.
|
||||
empty := MlDsa44Sign(signature[:], message[0:0], secret_key[:])
|
||||
if empty <= 0 || !MlDsa44Verify(message[0:0], signature[0:empty], public_key[:]) { return 8 }
|
||||
|
||||
// Buffers too small for a key or signature are refused.
|
||||
if MlDsa44KeyPair(public_key[0:10], secret_key[:]) { return 9 }
|
||||
if MlDsa44Sign(signature[0:10], message[:], secret_key[:]) != -1 { return 10 }
|
||||
if MlDsa44Verify(message[:], signature[0:0], public_key[:]) { return 11 }
|
||||
|
||||
RandomBytes(random[:])
|
||||
if AllZero(random[:]) { return 12 }
|
||||
Cleanse(secret_key[:])
|
||||
if !AllZero(secret_key[:]) { return 13 }
|
||||
return 0
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"format": 1,
|
||||
"packages": [
|
||||
{
|
||||
"commit": "e6b9e783747536f34700aab4bd10718c2fecab3f",
|
||||
"dependencies": {},
|
||||
"id": "pb34994bf01670ac3",
|
||||
"name": "liboqs",
|
||||
"ref": "main",
|
||||
"source": true,
|
||||
"url": "https://github.com/open-quantum-safe/liboqs.git"
|
||||
}
|
||||
],
|
||||
"root": {
|
||||
"dependencies": {"liboqs": "pb34994bf01670ac3"},
|
||||
"name": "Oqs"
|
||||
},
|
||||
"toolchain": {
|
||||
"commit": "3afad2d5bb237c2e91147d3bab40adfdbc5d61e6",
|
||||
"ref": "master",
|
||||
"url": "https://github.com/ziranlang/ziran.git"
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
[package]
|
||||
name = "Oqs"
|
||||
module_roots = ["src", "tests"]
|
||||
|
||||
[toolchain]
|
||||
git = "https://github.com/ziranlang/ziran.git"
|
||||
ref = "master"
|
||||
|
||||
[dependencies.liboqs]
|
||||
git = "https://github.com/open-quantum-safe/liboqs.git"
|
||||
ref = "main"
|
||||
source = true
|
||||
|
||||
[exports]
|
||||
Oqs = "src/oqs.zi"
|
||||
Reference in new issue
Block a user