rust-license-key 0.1.0

A production-grade Rust library for creating and validating offline software licenses using Ed25519 cryptography
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
//! Cryptographic operations for license signing and verification.
//!
//! This module provides Ed25519 key pair generation, signing, and verification
//! functionality. It uses the `ed25519-dalek` crate for cryptographic operations.
//!
//! # Security Notes
//!
//! - Private keys must be kept secret by the license publisher.
//! - Public keys can be safely embedded in client applications.
//! - The Ed25519 algorithm provides 128-bit security level.

use base64::engine::general_purpose::STANDARD as BASE64_STANDARD;
use base64::Engine;
use ed25519_dalek::{
    Signature, Signer, SigningKey, Verifier, VerifyingKey, PUBLIC_KEY_LENGTH, SECRET_KEY_LENGTH,
    SIGNATURE_LENGTH,
};
use rand::rngs::OsRng;

use crate::error::{LicenseError, Result};

// =============================================================================
// Key Pair
// =============================================================================

/// An Ed25519 key pair for signing and verifying licenses.
///
/// The key pair consists of:
/// - A private key (32 bytes) used by the publisher to sign licenses.
/// - A public key (32 bytes) embedded in client applications for verification.
///
/// # Example
///
/// ```
/// use rust_license_key::crypto::KeyPair;
///
/// // Generate a new key pair
/// let key_pair = KeyPair::generate().expect("Failed to generate key pair");
///
/// // Export keys for storage
/// let private_key_base64 = key_pair.private_key_base64();
/// let public_key_base64 = key_pair.public_key_base64();
///
/// println!("Keep this private: {}", private_key_base64);
/// println!("Embed in client: {}", public_key_base64);
/// ```
#[derive(Debug)]
pub struct KeyPair {
    /// The Ed25519 signing key (private key).
    signing_key: SigningKey,
}

impl KeyPair {
    /// Generates a new random Ed25519 key pair.
    ///
    /// Uses the operating system's cryptographically secure random number
    /// generator (CSPRNG) to generate the key material.
    ///
    /// # Returns
    ///
    /// A new `KeyPair` instance, or an error if key generation fails.
    ///
    /// # Security
    ///
    /// The generated private key should be stored securely and never shared.
    /// Only the public key should be distributed with client applications.
    pub fn generate() -> Result<Self> {
        // Use OS-provided CSPRNG for secure key generation
        let mut csprng = OsRng;
        let signing_key = SigningKey::generate(&mut csprng);

        Ok(Self { signing_key })
    }

    /// Creates a key pair from a base64-encoded private key.
    ///
    /// # Arguments
    ///
    /// * `private_key_base64` - The base64-encoded private key (32 bytes when decoded).
    ///
    /// # Security
    ///
    /// This function should only be used in secure publisher-side tooling,
    /// never in client applications.
    pub fn from_private_key_base64(private_key_base64: &str) -> Result<Self> {
        let private_key_bytes = BASE64_STANDARD.decode(private_key_base64).map_err(|e| {
            LicenseError::InvalidPrivateKey {
                reason: format!("invalid base64 encoding: {}", e),
            }
        })?;

        if private_key_bytes.len() != SECRET_KEY_LENGTH {
            return Err(LicenseError::InvalidPrivateKey {
                reason: format!(
                    "invalid key length: expected {} bytes, got {}",
                    SECRET_KEY_LENGTH,
                    private_key_bytes.len()
                ),
            });
        }

        let key_bytes: [u8; SECRET_KEY_LENGTH] =
            private_key_bytes
                .try_into()
                .map_err(|_| LicenseError::InvalidPrivateKey {
                    reason: "failed to convert key bytes".to_string(),
                })?;

        let signing_key = SigningKey::from_bytes(&key_bytes);

        Ok(Self { signing_key })
    }

    /// Creates a key pair from raw private key bytes.
    ///
    /// # Arguments
    ///
    /// * `private_key_bytes` - The raw private key bytes (exactly 32 bytes).
    pub fn from_private_key_bytes(private_key_bytes: &[u8]) -> Result<Self> {
        if private_key_bytes.len() != SECRET_KEY_LENGTH {
            return Err(LicenseError::InvalidPrivateKey {
                reason: format!(
                    "invalid key length: expected {} bytes, got {}",
                    SECRET_KEY_LENGTH,
                    private_key_bytes.len()
                ),
            });
        }

        let key_bytes: [u8; SECRET_KEY_LENGTH] =
            private_key_bytes
                .try_into()
                .map_err(|_| LicenseError::InvalidPrivateKey {
                    reason: "failed to convert key bytes".to_string(),
                })?;

        let signing_key = SigningKey::from_bytes(&key_bytes);

        Ok(Self { signing_key })
    }

    /// Returns the public key for this key pair.
    pub fn public_key(&self) -> PublicKey {
        PublicKey {
            verifying_key: self.signing_key.verifying_key(),
        }
    }

    /// Returns the private key as raw bytes.
    ///
    /// # Security
    ///
    /// Handle the returned bytes with care. They should be stored securely
    /// and never exposed to untrusted parties.
    pub fn private_key_bytes(&self) -> [u8; SECRET_KEY_LENGTH] {
        self.signing_key.to_bytes()
    }

    /// Returns the private key as a base64-encoded string.
    ///
    /// # Security
    ///
    /// Handle the returned string with care. It should be stored securely
    /// and never exposed to untrusted parties.
    pub fn private_key_base64(&self) -> String {
        BASE64_STANDARD.encode(self.signing_key.to_bytes())
    }

    /// Returns the public key as a base64-encoded string.
    ///
    /// This is the value that should be embedded in client applications.
    pub fn public_key_base64(&self) -> String {
        self.public_key().to_base64()
    }

    /// Signs the given data with the private key.
    ///
    /// # Arguments
    ///
    /// * `data` - The data to sign (typically the serialized license payload).
    ///
    /// # Returns
    ///
    /// The Ed25519 signature as raw bytes.
    pub fn sign(&self, data: &[u8]) -> [u8; SIGNATURE_LENGTH] {
        let signature = self.signing_key.sign(data);
        signature.to_bytes()
    }

    /// Signs the given data and returns the signature as base64.
    ///
    /// # Arguments
    ///
    /// * `data` - The data to sign.
    ///
    /// # Returns
    ///
    /// The base64-encoded signature.
    pub fn sign_base64(&self, data: &[u8]) -> String {
        BASE64_STANDARD.encode(self.sign(data))
    }
}

// =============================================================================
// Public Key
// =============================================================================

/// An Ed25519 public key for verifying license signatures.
///
/// This is the key that should be embedded in client applications.
/// It can only verify signatures, not create them.
///
/// # Example
///
/// ```
/// use rust_license_key::crypto::PublicKey;
///
/// // Load from base64 (e.g., embedded in application)
/// let public_key_base64 = "..."; // Your public key here
/// // let public_key = PublicKey::from_base64(public_key_base64).unwrap();
/// ```
#[derive(Debug, Clone)]
pub struct PublicKey {
    /// The Ed25519 verifying key.
    verifying_key: VerifyingKey,
}

impl PublicKey {
    /// Creates a public key from a base64-encoded string.
    ///
    /// # Arguments
    ///
    /// * `public_key_base64` - The base64-encoded public key (32 bytes when decoded).
    pub fn from_base64(public_key_base64: &str) -> Result<Self> {
        let public_key_bytes = BASE64_STANDARD.decode(public_key_base64).map_err(|e| {
            LicenseError::InvalidPublicKey {
                reason: format!("invalid base64 encoding: {}", e),
            }
        })?;

        Self::from_bytes(&public_key_bytes)
    }

    /// Creates a public key from raw bytes.
    ///
    /// # Arguments
    ///
    /// * `bytes` - The raw public key bytes (exactly 32 bytes).
    pub fn from_bytes(bytes: &[u8]) -> Result<Self> {
        if bytes.len() != PUBLIC_KEY_LENGTH {
            return Err(LicenseError::InvalidPublicKey {
                reason: format!(
                    "invalid key length: expected {} bytes, got {}",
                    PUBLIC_KEY_LENGTH,
                    bytes.len()
                ),
            });
        }

        let key_bytes: [u8; PUBLIC_KEY_LENGTH] =
            bytes
                .try_into()
                .map_err(|_| LicenseError::InvalidPublicKey {
                    reason: "failed to convert key bytes".to_string(),
                })?;

        let verifying_key =
            VerifyingKey::from_bytes(&key_bytes).map_err(|e| LicenseError::InvalidPublicKey {
                reason: format!("invalid public key: {}", e),
            })?;

        Ok(Self { verifying_key })
    }

    /// Returns the public key as raw bytes.
    pub fn to_bytes(&self) -> [u8; PUBLIC_KEY_LENGTH] {
        self.verifying_key.to_bytes()
    }

    /// Returns the public key as a base64-encoded string.
    pub fn to_base64(&self) -> String {
        BASE64_STANDARD.encode(self.verifying_key.to_bytes())
    }

    /// Verifies a signature against the given data.
    ///
    /// # Arguments
    ///
    /// * `data` - The original data that was signed.
    /// * `signature_bytes` - The signature to verify (64 bytes).
    ///
    /// # Returns
    ///
    /// `Ok(())` if the signature is valid, or an error if verification fails.
    pub fn verify(&self, data: &[u8], signature_bytes: &[u8]) -> Result<()> {
        if signature_bytes.len() != SIGNATURE_LENGTH {
            return Err(LicenseError::InvalidSignature);
        }

        let sig_bytes: [u8; SIGNATURE_LENGTH] = signature_bytes
            .try_into()
            .map_err(|_| LicenseError::InvalidSignature)?;

        let signature = Signature::from_bytes(&sig_bytes);

        self.verifying_key
            .verify(data, &signature)
            .map_err(|_| LicenseError::InvalidSignature)
    }

    /// Verifies a base64-encoded signature against the given data.
    ///
    /// # Arguments
    ///
    /// * `data` - The original data that was signed.
    /// * `signature_base64` - The base64-encoded signature.
    ///
    /// # Returns
    ///
    /// `Ok(())` if the signature is valid, or an error if verification fails.
    pub fn verify_base64(&self, data: &[u8], signature_base64: &str) -> Result<()> {
        let signature_bytes = BASE64_STANDARD
            .decode(signature_base64)
            .map_err(|_| LicenseError::InvalidSignature)?;

        self.verify(data, &signature_bytes)
    }
}

// =============================================================================
// Utility Functions
// =============================================================================

/// Generates a new key pair and returns the keys as base64 strings.
///
/// This is a convenience function for quickly generating keys.
///
/// # Returns
///
/// A tuple of (private_key_base64, public_key_base64).
///
/// # Example
///
/// ```
/// use rust_license_key::crypto::generate_key_pair_base64;
///
/// let (private_key, public_key) = generate_key_pair_base64().expect("Key generation failed");
/// println!("Private (keep secret): {}", private_key);
/// println!("Public (embed in app): {}", public_key);
/// ```
pub fn generate_key_pair_base64() -> Result<(String, String)> {
    let key_pair = KeyPair::generate()?;
    Ok((key_pair.private_key_base64(), key_pair.public_key_base64()))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_key_pair_generation() {
        let key_pair = KeyPair::generate().expect("Key generation should succeed");

        // Verify key lengths
        assert_eq!(key_pair.private_key_bytes().len(), SECRET_KEY_LENGTH);
        assert_eq!(key_pair.public_key().to_bytes().len(), PUBLIC_KEY_LENGTH);
    }

    #[test]
    fn test_key_pair_from_private_key() {
        let original = KeyPair::generate().expect("Key generation should succeed");
        let private_key_base64 = original.private_key_base64();

        let restored =
            KeyPair::from_private_key_base64(&private_key_base64).expect("Should restore key pair");

        // Public keys should match
        assert_eq!(
            original.public_key().to_bytes(),
            restored.public_key().to_bytes()
        );
    }

    #[test]
    fn test_public_key_from_base64() {
        let key_pair = KeyPair::generate().expect("Key generation should succeed");
        let public_key_base64 = key_pair.public_key_base64();

        let public_key =
            PublicKey::from_base64(&public_key_base64).expect("Should parse public key");

        assert_eq!(public_key.to_bytes(), key_pair.public_key().to_bytes());
    }

    #[test]
    fn test_sign_and_verify() {
        let key_pair = KeyPair::generate().expect("Key generation should succeed");
        let data = b"This is the license payload data";

        // Sign with private key
        let signature = key_pair.sign(data);

        // Verify with public key
        let public_key = key_pair.public_key();
        public_key
            .verify(data, &signature)
            .expect("Signature should be valid");
    }

    #[test]
    fn test_sign_and_verify_base64() {
        let key_pair = KeyPair::generate().expect("Key generation should succeed");
        let data = b"This is the license payload data";

        // Sign with private key
        let signature_base64 = key_pair.sign_base64(data);

        // Verify with public key
        let public_key = key_pair.public_key();
        public_key
            .verify_base64(data, &signature_base64)
            .expect("Signature should be valid");
    }

    #[test]
    fn test_verify_invalid_signature() {
        let key_pair = KeyPair::generate().expect("Key generation should succeed");
        let data = b"This is the license payload data";
        let wrong_data = b"This is different data";

        // Sign correct data
        let signature = key_pair.sign(data);

        // Verify against wrong data should fail
        let public_key = key_pair.public_key();
        assert!(public_key.verify(wrong_data, &signature).is_err());
    }

    #[test]
    fn test_verify_with_wrong_key() {
        let key_pair_1 = KeyPair::generate().expect("Key generation should succeed");
        let key_pair_2 = KeyPair::generate().expect("Key generation should succeed");
        let data = b"This is the license payload data";

        // Sign with key_pair_1
        let signature = key_pair_1.sign(data);

        // Verify with key_pair_2 should fail
        let public_key_2 = key_pair_2.public_key();
        assert!(public_key_2.verify(data, &signature).is_err());
    }

    #[test]
    fn test_invalid_private_key_length() {
        let invalid_key = BASE64_STANDARD.encode(vec![0u8; 16]); // Wrong length
        let result = KeyPair::from_private_key_base64(&invalid_key);
        assert!(result.is_err());
    }

    #[test]
    fn test_invalid_public_key_length() {
        let invalid_key = BASE64_STANDARD.encode(vec![0u8; 16]); // Wrong length
        let result = PublicKey::from_base64(&invalid_key);
        assert!(result.is_err());
    }

    #[test]
    fn test_invalid_base64_encoding() {
        let result = KeyPair::from_private_key_base64("not valid base64!!!");
        assert!(result.is_err());

        let result = PublicKey::from_base64("not valid base64!!!");
        assert!(result.is_err());
    }

    #[test]
    fn test_generate_key_pair_base64_convenience() {
        let (private_key, public_key) =
            generate_key_pair_base64().expect("Key generation should succeed");

        // Verify we can recreate the key pair from the private key
        let key_pair = KeyPair::from_private_key_base64(&private_key)
            .expect("Should create key pair from private key");

        // And the public key matches
        assert_eq!(key_pair.public_key_base64(), public_key);
    }
}