Skip to content

Commit 3db3e2f

Browse files
committed
Fix various inconsistencies in OU implementation
Critical: enforce m < p plaintext bound (not m < n); remove Ciphertext::Mul which was missing mod n reduction; fix stream format to preserve leading-zero bytes and distinguish empty from zero plaintext. Security: validate gcd(r,n)=1 for encryption nonce; privatize PrivateKey fields; replace safe prime generation with standard primes (not required by OU, 473x slower at 2048 bits); migrate decryption modpow to crypto-bigint BoxedMontyForm for constant-time exponentiation. Correctness: add gcd(g,n)=1 generator check; precompute l_gp_inv and p² in PrivateKey, eliminating runtime mod_inverse and repeated p² allocation; add Plaintext type with checked_add to surface homomorphic overflow. API: remove Ciphertext::Deref and unvalidated From<&[u8]>; replace glob re-exports with explicit items; cap untrusted block_count at 4M; extract parse_blocks(); make homomorphic_add_packed pub.
1 parent c73cd7e commit 3db3e2f

10 files changed

Lines changed: 954 additions & 458 deletions

File tree

Cargo.lock

Lines changed: 86 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
11
[package]
22
name = "okuchi"
33
version = "0.1.0"
4-
edition = "2024"
4+
edition = "2021"
55
authors = ["Nelson Dominguez <ekkolon@proton.me>"]
66
description = "Okamoto-Uchiyama Cryptosystem Implementation"
77
license = "MIT OR Apache-2.0"
88
readme = "README.md"
99

10-
1110
[dependencies]
12-
num-bigint-dig = { version = "0.8", features = ["zeroize", "prime", "rand", "u64_digit"] }
11+
num-bigint-dig = { version = "0.8", features = [
12+
"zeroize",
13+
"prime",
14+
"rand",
15+
"u64_digit",
16+
] }
1317
num-traits = "0.2"
18+
num-integer = "0.1"
1419
rand = "0.8"
15-
zeroize = { version = "1.6", features = ["derive"] }
16-
thiserror = "2.0"
20+
zeroize = { version = "1.8", features = ["derive"] }
21+
thiserror = "2"
22+
# Constant-time modular arithmetic for the decryption hot path (SECURITY-2).
23+
# BoxedUint provides heap-allocated variable-width integers with CT guarantees.
24+
crypto-bigint = { version = "0.7", features = ["alloc"] }
1725

1826
[dev-dependencies]
19-
criterion = "0.7"
27+
criterion = "0.5"
2028

2129
[[bench]]
2230
name = "okuchi"
2331
harness = false
2432

25-
2633
[profile.release]
2734
opt-level = 3
2835
lto = true

README.md

Lines changed: 67 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,99 @@
11
# Okuchi
22

3-
A pure Rust implementation of the **Okamoto–Uchiyama** cryptosystem - a probabilistic public-key scheme whose security relies on the hardness of factoring and discrete logarithms modulo a composite number.
3+
A pure-Rust implementation of the **Okamoto-Uchiyama (OU)** cryptosystem: a
4+
probabilistic public-key encryption scheme with additive homomorphism, whose
5+
security relies on the hardness of factoring `n = p²q` and computing discrete
6+
logarithms modulo ``.
47

58
**Okuchi** is a portmanteau of **Ok**amoto and **Uchi**yama.
69

10+
---
11+
712
## ⚠️ Important Notice
813

9-
The project is evolving and should be treated as a **work in progress**.
10-
Breaking changes, redesigns, or API removals may occur without notice.
14+
This implementation is **experimental**, **incomplete**, and **not audited** by
15+
any external security professionals. It may contain defects, conceptual
16+
mistakes, side-channel vulnerabilities, insecure parameter choices, or other
17+
issues that could compromise confidentiality, integrity, or availability of
18+
data.
19+
20+
The project is a **work in progress**. Breaking changes, API removals, or
21+
redesigns may occur without notice.
1122

12-
This implementation is (still) **experimental**, **incomplete**, and **not audited** by any external security professionals.
13-
It may contain defects, conceptual mistakes, side-channel vulnerabilities, insecure parameter choices, or other issues that could compromise confidentiality, integrity, or availability of data.
23+
**Do not use Okuchi in production systems, high-risk environments, or anywhere
24+
security or correctness is critical.**
1425

15-
**Do not use Okuchi in production systems, high-risk environments, or anywhere security or correctness is critical.**
26+
If you choose to use this code despite these warnings, you do so entirely at
27+
your own risk. No guarantees, explicit or implied, are made regarding
28+
correctness, security, or fitness for any purpose. The author(s) assume no
29+
liability for any damages or consequences resulting from use or misuse of this
30+
software.
1631

17-
If you choose to use this code despite these warnings, **you do so entirely at your own risk**. No guarantees - explicit or implied - are made regarding performance, correctness, security, or fitness for any purpose.
18-
The author(s) **assume no liability** for any damages, losses, or consequences resulting from the use, misuse, or inability to use this software.
32+
---
1933

2034
## Goals
2135

22-
- Provide a correct, readable and safe Rust implementation of the **OU** cryptosystem
36+
- Provide a correct, readable, and safe Rust implementation of the OU
37+
cryptosystem
2338
- Serve as a reference for learning and experimentation
24-
- Maintain minimal dependencies and clear internal structure
39+
- Maintain minimal dependencies and a clear internal structure
40+
41+
This project does **not** aim to be a hardened or production-quality
42+
cryptographic library.
43+
44+
---
45+
46+
## Features
47+
48+
- **Probabilistic encryption**: two encryptions of the same plaintext produce
49+
distinct ciphertexts
50+
- **Additive homomorphism**: `E(m1) * E(m2) mod n` decrypts to
51+
`(m1 + m2) mod p`, without decrypting either operand
52+
- **Stream API**: encrypt and decrypt arbitrary-length byte sequences via
53+
automatic block splitting and reassembly
54+
- **Validated plaintext type**: [`Plaintext`] enforces the OU plaintext bound
55+
at construction and provides checked addition for safe homomorphic
56+
accumulation
57+
- **Zeroize on drop**: secret key material (`p`, `q`, derived constants) is
58+
zeroed when the key is dropped
2559

26-
This project **does not** aim to be a hardened or production-quality cryptographic library.
60+
---
61+
62+
## Security Notes
63+
64+
- The minimum enforced key size is **512 bits** (testing only). Use **2048
65+
bits or larger** for any non-trivial use.
66+
- The modular exponentiation `c^(p-1) mod p²` in decryption is routed through
67+
`crypto-bigint` Montgomery form for constant-time guarantees. All other
68+
big-integer operations (`num-bigint-dig`) are **variable-time** and may leak
69+
information about secret values through timing.
70+
- No formal audit has been performed. Do not deploy in adversarial environments.
71+
72+
---
2773

2874
## Usage
2975

30-
As mentioned above, until the library matures and receives proper review, usage should be limited to:
76+
Intended use cases:
3177

32-
- academic experiments
33-
- prototyping
34-
- security research
35-
- code reading and learning
78+
- Academic experiments
79+
- Prototyping
80+
- Security research
81+
- Code reading and learning
3682

3783
**Production use is strongly discouraged.**
3884

39-
### Example
85+
### Encrypt and Decrypt
4086

41-
```rs
87+
```rust
4288
use okuchi::{KeyPair, Okuchi};
4389

44-
let keypair = KeyPair::new(2048).expect("key generation failed");
45-
let pub_key = keypair.pub_key();
90+
let keypair = KeyPair::new(2048).expect("key generation failed");
91+
let pub_key = keypair.pub_key();
4692
let priv_key = keypair.priv_key();
4793

4894
let message = "hello world 🌍";
4995

50-
// Encrypt (stream API)
96+
// Encrypt arbitrary-length data via the stream API
5197
let packed = Okuchi::encrypt_stream(pub_key, message).unwrap();
5298

5399
// Decrypt
@@ -56,7 +102,3 @@ let decrypted = String::from_utf8(decrypted_bytes).unwrap();
56102

57103
assert_eq!(message, decrypted);
58104
```
59-
60-
## References
61-
62-
- Okamoto, T., Uchiyama, S. (1998). _A New Public-Key Cryptosystem as Secure as Factoring._

0 commit comments

Comments
 (0)