[docs] Keys: secret keys, API keys, key pairs; tests first
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
//! Mandatory tests for the key and API-key generators (docs/KEYS.md).
|
||||
|
||||
use custos_gen::{KeyEncoding, api_key, api_key_checksum_ok, secret_key};
|
||||
|
||||
#[test]
|
||||
fn secret_keys_have_the_asked_length_encoding_and_entropy() {
|
||||
let hex = secret_key(32, KeyEncoding::Hex).unwrap();
|
||||
assert_eq!(hex.value.expose().len(), 64);
|
||||
assert!(hex.value.expose().chars().all(|c| c.is_ascii_hexdigit() && !c.is_ascii_uppercase()));
|
||||
assert_eq!(hex.entropy_bits, 256.0);
|
||||
let b64 = secret_key(32, KeyEncoding::Base64Url).unwrap();
|
||||
assert_eq!(b64.value.expose().len(), 43);
|
||||
assert!(b64.value.expose().chars().all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_'));
|
||||
let std = secret_key(48, KeyEncoding::Base64).unwrap();
|
||||
assert_eq!(std.value.expose().len(), 64);
|
||||
// Never the same twice; refused outside 16..=128 bytes.
|
||||
assert_ne!(secret_key(32, KeyEncoding::Hex).unwrap().value, hex.value);
|
||||
assert!(secret_key(15, KeyEncoding::Hex).is_err());
|
||||
assert!(secret_key(129, KeyEncoding::Hex).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn api_keys_carry_a_prefix_190_random_bits_and_a_checksum() {
|
||||
let k = api_key("domi").unwrap();
|
||||
let v = k.value.expose();
|
||||
assert!(v.starts_with("domi_"), "{v}");
|
||||
assert_eq!(v.len(), "domi_".len() + 32 + 6);
|
||||
assert!(v["domi_".len()..].chars().all(|c| c.is_ascii_alphanumeric()));
|
||||
assert!((k.entropy_bits - 190.5).abs() < 0.1, "{}", k.entropy_bits);
|
||||
assert!(api_key_checksum_ok(v));
|
||||
// A typo is caught by the checksum.
|
||||
let mut typo: Vec<char> = v.chars().collect();
|
||||
let i = "domi_".len() + 3;
|
||||
typo[i] = if typo[i] == 'a' { 'b' } else { 'a' };
|
||||
assert!(!api_key_checksum_ok(&typo.into_iter().collect::<String>()));
|
||||
// No prefix is allowed; a bad one is refused.
|
||||
let bare = api_key("").unwrap();
|
||||
assert_eq!(bare.value.expose().len(), 38);
|
||||
assert!(api_key_checksum_ok(bare.value.expose()));
|
||||
for bad in ["Domi", "do mi", "1abc", "averyveryverylongprefix"] {
|
||||
assert!(api_key(bad).is_err(), "{bad}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn generated_keys_never_print() {
|
||||
let k = secret_key(32, KeyEncoding::Hex).unwrap();
|
||||
let printed = format!("{k:?}");
|
||||
assert!(!printed.contains(k.value.expose()), "{printed}");
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
//! Mandatory tests for key pairs (docs/KEYS.md): every format parses back to the same key, the
|
||||
//! fingerprint is ssh-keygen's, Ed25519 signs and verifies, nothing private prints.
|
||||
|
||||
use custos_keys::{Algorithm, generate};
|
||||
|
||||
#[test]
|
||||
fn every_algorithm_round_trips_through_every_format() {
|
||||
for alg in [Algorithm::Ed25519, Algorithm::EcdsaP256, Algorithm::Rsa3072] {
|
||||
let k = generate(alg, "deploy@home").unwrap();
|
||||
assert_eq!(k.algorithm, alg.name());
|
||||
// The OpenSSH private key parses back and carries the same public key and comment.
|
||||
let private = ssh_key::PrivateKey::from_openssh(k.private_openssh.expose()).unwrap();
|
||||
assert_eq!(private.public_key().to_openssh().unwrap(), k.public_openssh);
|
||||
assert_eq!(private.comment(), "deploy@home");
|
||||
assert!(k.public_openssh.ends_with(" deploy@home"));
|
||||
// The fingerprint is the one ssh-keygen -l prints.
|
||||
assert_eq!(private.public_key().fingerprint(ssh_key::HashAlg::Sha256).to_string(), k.fingerprint);
|
||||
assert!(k.fingerprint.starts_with("SHA256:"));
|
||||
// PKCS#8 and SPKI PEM have the right headers and parse back to the same public key.
|
||||
assert!(k.private_pem.expose().starts_with("-----BEGIN PRIVATE KEY-----"));
|
||||
assert!(k.public_pem.starts_with("-----BEGIN PUBLIC KEY-----"));
|
||||
assert!(custos_keys::pem_matches(alg, k.private_pem.expose(), &k.public_pem).unwrap(), "{alg:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ed25519_signs_and_verifies() {
|
||||
let k = generate(Algorithm::Ed25519, "").unwrap();
|
||||
let sig = custos_keys::sign_ed25519(k.private_pem.expose(), b"release 1.0").unwrap();
|
||||
assert!(custos_keys::verify_ed25519(&k.public_pem, b"release 1.0", &sig).unwrap());
|
||||
assert!(!custos_keys::verify_ed25519(&k.public_pem, b"release 1.1", &sig).unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keys_are_unique_and_nothing_private_prints() {
|
||||
let a = generate(Algorithm::Ed25519, "a").unwrap();
|
||||
let b = generate(Algorithm::Ed25519, "a").unwrap();
|
||||
assert_ne!(a.public_openssh, b.public_openssh);
|
||||
let printed = format!("{a:?}");
|
||||
assert!(!printed.contains("PRIVATE"), "{printed}");
|
||||
assert!(!printed.contains(a.private_openssh.expose().lines().nth(1).unwrap()), "{printed}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn algorithm_names_parse() {
|
||||
for alg in Algorithm::ALL {
|
||||
assert_eq!(Algorithm::from_name(alg.name()), Some(alg));
|
||||
}
|
||||
assert_eq!(Algorithm::from_name("dsa"), None);
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
# Keys, API keys and key pairs
|
||||
|
||||
What `custos-gen` and `custos-keys` generate besides passwords. All randomness comes from the OS CSPRNG.
|
||||
|
||||
## Secret keys (`custos_gen::secret_key`)
|
||||
|
||||
16 ..= 128 random bytes (default 32 = 256 bits), written as lowercase hex, base64url without padding, or
|
||||
standard base64 with padding. For symmetric keys, HMAC and JWT secrets, encryption passphrases for tools.
|
||||
|
||||
## API keys (`custos_gen::api_key`)
|
||||
|
||||
```
|
||||
<prefix>_<32 base62 characters><6 base62 characters of checksum> e.g. domi_7Kq…Zx41aP0c
|
||||
```
|
||||
|
||||
- `prefix`: empty, or a lowercase letter then up to 15 lowercase letters or digits. It says what the key is
|
||||
for at a glance, and lets leak scanners find it. No prefix: the key starts with the 32 characters.
|
||||
- 32 base62 characters: 190.5 bits, each character uniform (rejection sampling, no modulo bias).
|
||||
- The checksum is CRC-32 (IEEE) of everything before it, in base62, padded to 6 characters. It catches typos
|
||||
and lets a scanner tell a real key from random text. It is not a security feature.
|
||||
|
||||
## Key pairs (`custos_keys::generate`)
|
||||
|
||||
| Algorithm | Name in the item | Notes |
|
||||
|:--|:--|:--|
|
||||
| Ed25519 | `ed25519` | the default: SSH, signing, minisign-style tools |
|
||||
| ECDSA P-256 | `ecdsa-p256` | where Ed25519 is not accepted (some cloud KMS, older hardware) |
|
||||
| RSA 3072 / 4096 | `rsa-3072`, `rsa-4096` | for systems that accept nothing else; generation takes seconds |
|
||||
|
||||
Each gives:
|
||||
|
||||
- `public-key`: the OpenSSH public line (`ssh-ed25519 AAAA… comment`), for `authorized_keys`;
|
||||
- `public-pem`: the SubjectPublicKeyInfo PEM;
|
||||
- `private-key`*: the PKCS#8 PEM (unencrypted; the vault is its encryption);
|
||||
- `private-openssh`*: the OpenSSH private key (unencrypted, same reason), for `ssh -i`;
|
||||
- `fingerprint`: `SHA256:…`, exactly as `ssh-keygen -l` prints it.
|
||||
|
||||
The crates are RustCrypto's: `ssh-key`, `ed25519-dalek` (its Ed25519 backend), `p256`, `rsa`.
|
||||
Reference in New Issue
Block a user