libslug 0.12.0

A Rust Library For Cryptography Intended For Slug20 That Supports X59 Certificate Format and Post-Quantum 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
//! # ECIES-ED25519-silene
//! 
//! ECIES is an elliptic curve public key encryption algorithm that can be used to encrypt data using public keys that can only be decrypted by the private key.
//! 
//! In this library, we use `ecies-ed25519-silene`, a fork of another library using ECIES with Curve25519 and the SHA3 hash function (as opposed to SHA2).
//! 
//! It implements zeroize for security for the secret key.
//! 
//! ## TODO
//! 
//! - Fix `Message`
//! 
//! - [ ] Add Export Functionality
//! - [ ] Add Different Sources of Entropy
//! 
//! ## ECIES Supported Encodings
//! 
//! - [X] Hex
//! - [X] Base32
//! - [X] Base32unpadded
//! - [X] Base58
//! - [X] Base64
//! - [X] Base64urlsafe

use std::string::FromUtf8Error;

/// # ECIES over Curve25519 (Encryption)
/// 
/// This module contains the required data to implement ECIES over Curve25519. This is the standard method of encryption.

use ecies_ed25519::PublicKey;
use ecies_ed25519::SecretKey;
use ecies_ed25519::Error;
use zeroize::Zeroize;
use zeroize::ZeroizeOnDrop;

// SlugCrypt Structs
use crate::slugcrypt::internals::ciphertext::CipherText;
use crate::slugcrypt::internals::messages::Message;

use serde::{Serialize,Deserialize};
use subtle_encoding::hex;
use base58::{FromBase58,ToBase58,FromBase58Error};
use slugencode::SlugEncodingUsage;
use slugencode::SlugEncodings;
use slugencode::errors::SlugEncodingError;

//use rand::RngCore;
use rand::rngs::OsRng;
//use rand::CryptoRng;

pub mod protocol_info {
    /// PROTOCOL NAME FOR ECIES-ED25519
    pub const ECIES_PROTOCOL_NAME: &str = "ecies-ed25519-sha3";
    pub const ECIES_ED25519_PUBLIC_KEY_SIZE: usize = 32;
    pub const ECIES_ED25519_SECRET_KEY_SIZE: usize = 32;
    pub const ECIES_CURVE: &str = "CURVE25519";
    pub const SYMMETRIC_ENCRYPTION: &str = "AES256-GCM";
    pub const HASH_DERIVIATION: &str = "SHA3";
    pub const RANDOMNESS_SOURCES: &str = "OSCSPRNG";
}

/// # ECIES Encrypt
/// 
/// ECIESEncrypt is the encryption struct for encrypting data using Curve25519 + SHA3
pub struct ECIESEncrypt;

/// # ECIES Decrypt
/// 
/// ECIESDecrypt is the decryption struct for decrypting data using Curve25519 + SHA3
pub struct ECIESDecrypt;

/// # ECPublicKey (ECIES-ED25519 Public Key For Encryption/Decryption)
/// 
/// This provides us with the interface for using ECIES with ED25519 with zeroize implemented to encrypt/decrypt data.
/// 
/// ## Features
/// 
/// - To and From Bytes
/// - To and From Hexadecimal
/// - To and From Base58
#[derive(Clone,Serialize,Deserialize,Debug,PartialEq)]
pub struct ECPublicKey {
    pub public_key: PublicKey,
}

/// # ECSecretKey (ECIES-ED25519 Public Key For Encryption/Decryption)
/// 
/// This provides us with the interface for using ECIES with ED25519 with zeroize implemented to encrypt/decrypt data. It does not implement clone.
/// 
/// ## Features
/// 
/// ### Generation
/// 
/// - Generate from operating system randomness
/// 
/// ### Conversion
/// 
/// - To and From Bytes
/// - To and From Hex (Upper)
/// 
/// ### Encryption
/// 
/// - Encrypt
/// - Decrypt
/// - Get Public Key
#[derive(Serialize,Deserialize,Debug)]
pub struct ECSecretKey {
    pub secret_key: SecretKey,
}

impl ECIESEncrypt {
    /// # Encrypt (ECIES-ED25519-Silene)
    /// 
    /// ## Description
    /// 
    /// Encryption via a public key and a message.
    /// 
    /// **Randomness:** Uses operating system randomness to encrypt.
    pub fn encrypt<T: AsRef<[u8]>>(pk: &ECPublicKey, msg: T) -> Result<CipherText,Error>  {
        let mut csprng = OsRng;

        let ciphertext = ecies_ed25519::encrypt(&pk.public_key, msg.as_ref(), &mut csprng)?;

        return Ok(CipherText::from_bytes(&ciphertext))
    }
}

impl ECIESDecrypt {
    /// # Decrypt (ECIES-ED25519-Silene)
    /// 
    /// ## Description
    /// 
    /// Decryption via a secret key and a ciphertext. Decodes to `Message` struct, a vec.
    pub fn decrypt(sk: &ECSecretKey, ciphertext: &CipherText) -> Result<Message,Error> {
        let decoded_msg = ecies_ed25519::decrypt(&sk.secret_key, ciphertext.as_bytes())?;

        Ok(Message::new(decoded_msg))
    }
}

impl ECPublicKey {
    /// as bytes
    pub fn as_bytes(&self) -> &[u8] {
        self.public_key.as_bytes()
    }
    /// to byte array of 32 bytes
    pub fn to_bytes(&self) -> [u8;32] {
        self.public_key.to_bytes()
    }
    pub fn to_hex(&self) -> Result<String,SlugEncodingError> {
        let x = SlugEncodingUsage::new(SlugEncodings::Hex);
        let xx = x.encode(self.as_bytes())?;
        Ok(xx)
    }
    pub fn to_base32(&self) -> Result<String,SlugEncodingError> {
        let x = SlugEncodingUsage::new(SlugEncodings::Base32);
        let xx = x.encode(self.as_bytes())?;
        Ok(xx)
    }
    pub fn to_base32_unpadded(&self) -> Result<String,SlugEncodingError> {
        let x = SlugEncodingUsage::new(SlugEncodings::Base32unpadded);
        let xx = x.encode(self.as_bytes())?;
        Ok(xx)
    }
    pub fn to_base58(&self) -> Result<String,SlugEncodingError> {
        let x = SlugEncodingUsage::new(SlugEncodings::Base58);
        let xx = x.encode(self.as_bytes())?;
        Ok(xx)
    }
    pub fn to_base64(&self) -> Result<String,SlugEncodingError> {
        let x = SlugEncodingUsage::new(SlugEncodings::Base64);
        let xx = x.encode(self.as_bytes())?;
        Ok(xx)
    }
    pub fn to_base64_url_safe(&self) -> Result<String,SlugEncodingError> {
        let x = SlugEncodingUsage::new(SlugEncodings::Base64urlsafe);
        let xx = x.encode(self.as_bytes())?;
        Ok(xx)
    }
    pub fn from_hex<T: AsRef<str>>(s: T) -> Result<Self,SlugEncodingError> {
        let x = SlugEncodingUsage::new(SlugEncodings::Hex);
        let xx = x.decode(s.as_ref())?;
        let output = Self::from_byte_slice(&xx);

        match output {
            Ok(v) => return Ok(v),
            Err(_) => return Err(SlugEncodingError::DecodingError)
        }
    }
    pub fn from_base32<T: AsRef<str>>(s: T) -> Result<Self,SlugEncodingError> {
        let x = SlugEncodingUsage::new(SlugEncodings::Base32);
        let xx = x.decode(s.as_ref())?;
        let output = Self::from_byte_slice(&xx);

        match output {
            Ok(v) => return Ok(v),
            Err(_) => return Err(SlugEncodingError::DecodingError)
        }
    }
    pub fn from_base32_unpadded<T: AsRef<str>>(s: T) -> Result<Self,SlugEncodingError> {
        let x = SlugEncodingUsage::new(SlugEncodings::Base32unpadded);
        let xx = x.decode(s.as_ref())?;
        let output = Self::from_byte_slice(&xx);

        match output {
            Ok(v) => return Ok(v),
            Err(_) => return Err(SlugEncodingError::DecodingError)
        }
    }
    pub fn from_base58<T: AsRef<str>>(s: T) -> Result<Self,SlugEncodingError> {
        let x = SlugEncodingUsage::new(SlugEncodings::Base58);
        let xx = x.decode(s.as_ref())?;
        let output = Self::from_byte_slice(&xx);

        match output {
            Ok(v) => return Ok(v),
            Err(_) => return Err(SlugEncodingError::DecodingError)
        }
    }
    pub fn from_base64<T: AsRef<str>>(s: T) -> Result<Self,SlugEncodingError> {
        let x = SlugEncodingUsage::new(SlugEncodings::Base64);
        let xx = x.decode(s.as_ref())?;
        let output = Self::from_byte_slice(&xx);

        match output {
            Ok(v) => return Ok(v),
            Err(_) => return Err(SlugEncodingError::DecodingError)
        }
    }
    pub fn from_base64_url_safe<T: AsRef<str>>(s: T) -> Result<Self,SlugEncodingError> {
        let x = SlugEncodingUsage::new(SlugEncodings::Base64urlsafe);
        let xx = x.decode(s.as_ref())?;
        let output = Self::from_byte_slice(&xx);

        match output {
            Ok(v) => return Ok(v),
            Err(_) => return Err(SlugEncodingError::DecodingError)
        }
    }
    /// from byte array of 32 bytes
    pub fn from_bytes(bytes: [u8;32]) -> Result<Self,Error> {
        let public_key = ecies_ed25519::PublicKey::from_bytes(&bytes)?;

        return Ok(Self {
            public_key
        })
    }
    /// from byte slice
    pub fn from_byte_slice(bytes: &[u8]) -> Result<Self,Error> {
        let public_key = ecies_ed25519::PublicKey::from_bytes(bytes)?;

        return Ok(Self {
            public_key
        })
    }
    /// to hex string (constant-time) (upper)
    pub fn to_hex_string(&self) -> Result<String,FromUtf8Error> {
        let bytes = hex::encode_upper(self.public_key.as_bytes());
        Ok(String::from_utf8(bytes)?)
    }
    /// from hex string (constant-time) (upper)
    pub fn from_hex_string<T: AsRef<str>>(bytes: T) -> Result<Vec<u8>,subtle_encoding::Error> {
        Ok(hex::decode_upper(bytes.as_ref().as_bytes())?)
    }
    /// to base58
    pub fn to_base58_string(&self) -> String {
        self.public_key.as_bytes().to_base58()
    }
    /// from base58
    pub fn from_base58_string<T: AsRef<str>>(bs58_str: T) -> Result<Vec<u8>,FromBase58Error> {
        Ok(bs58_str.as_ref().from_base58())?
    }
}

impl ECSecretKey {
    pub fn generate() -> Self {
        let mut rng = OsRng;

        let secret_key = ecies_ed25519::SecretKey::generate(&mut rng);

        ECSecretKey {
            secret_key
        }
    }
    /// to bytes (32-byte array)
    pub fn to_bytes(&self) -> [u8;32] {
        self.secret_key.to_bytes()
    }
    /// as bytes
    pub fn as_bytes(&self) -> &[u8] {
        self.secret_key.as_bytes()
    }
    /// from bytes (32-byte array)
    pub fn from_bytes(bytes: [u8;32]) -> Result<Self,Error> {
        let secret_key = ecies_ed25519::SecretKey::from_bytes(&bytes)?;
        
        return Ok(Self {
            secret_key
        })
    }
    /// from byte slice
    pub fn from_byte_slice(bytes: &[u8]) -> Result<Self,Error> {
        let secret_key = ecies_ed25519::SecretKey::from_bytes(bytes)?;

        return Ok(Self {
            secret_key
        })
    }
    /// Converts ECIES-Curve25519 Secret Key To Public Key
    pub fn public_key(&self) -> ECPublicKey {
        let public_key = ecies_ed25519::PublicKey::from_secret(&self.secret_key);

        ECPublicKey {
            public_key
        }
    }
    /// Encrypt message using ECIES-ED25519
    pub fn encrypt<T: AsRef<[u8]>>(pk: ECPublicKey, msg: T) -> Result<CipherText,Error> {
        let mut rng = OsRng;

        let ciphertext = ecies_ed25519::encrypt(&pk.public_key, msg.as_ref(), &mut rng)?;

        return Ok(CipherText::from_bytes(&ciphertext))
    }
    /// Decrypt message using ECIES-ED25519 returning a Message struct
    pub fn decrypt(self, ciphertext: &CipherText) -> Result<Message,Error> {
        ECIESDecrypt::decrypt(&self, &ciphertext)
    }
    /// To Hex String (Upper)
    pub fn to_hex_string(&self) -> Result<String,FromUtf8Error> {
        let bytes = hex::encode_upper(self.secret_key.as_bytes());
        Ok(String::from_utf8(bytes)?)
    }
        pub fn to_hex(&self) -> Result<String,SlugEncodingError> {
        let x = SlugEncodingUsage::new(SlugEncodings::Hex);
        let xx = x.encode(self.as_bytes())?;
        Ok(xx)
    }
    pub fn to_base32(&self) -> Result<String,SlugEncodingError> {
        let x = SlugEncodingUsage::new(SlugEncodings::Base32);
        let xx = x.encode(self.as_bytes())?;
        Ok(xx)
    }
    pub fn to_base32_unpadded(&self) -> Result<String,SlugEncodingError> {
        let x = SlugEncodingUsage::new(SlugEncodings::Base32unpadded);
        let xx = x.encode(self.as_bytes())?;
        Ok(xx)
    }
    pub fn to_base58(&self) -> Result<String,SlugEncodingError> {
        let x = SlugEncodingUsage::new(SlugEncodings::Base58);
        let xx = x.encode(self.as_bytes())?;
        Ok(xx)
    }
    pub fn to_base64(&self) -> Result<String,SlugEncodingError> {
        let x = SlugEncodingUsage::new(SlugEncodings::Base64);
        let xx = x.encode(self.as_bytes())?;
        Ok(xx)
    }
    pub fn to_base64_url_safe(&self) -> Result<String,SlugEncodingError> {
        let x = SlugEncodingUsage::new(SlugEncodings::Base64urlsafe);
        let xx = x.encode(self.as_bytes())?;
        Ok(xx)
    }
    pub fn from_hex<T: AsRef<str>>(s: T) -> Result<Self,SlugEncodingError> {
        let x = SlugEncodingUsage::new(SlugEncodings::Hex);
        let xx = x.decode(s.as_ref())?;
        let output = Self::from_byte_slice(&xx);

        match output {
            Ok(v) => return Ok(v),
            Err(_) => return Err(SlugEncodingError::DecodingError)
        }
    }
    pub fn from_base32<T: AsRef<str>>(s: T) -> Result<Self,SlugEncodingError> {
        let x = SlugEncodingUsage::new(SlugEncodings::Base32);
        let xx = x.decode(s.as_ref())?;
        let output = Self::from_byte_slice(&xx);

        match output {
            Ok(v) => return Ok(v),
            Err(_) => return Err(SlugEncodingError::DecodingError)
        }
    }
    pub fn from_base32_unpadded<T: AsRef<str>>(s: T) -> Result<Self,SlugEncodingError> {
        let x = SlugEncodingUsage::new(SlugEncodings::Base32unpadded);
        let xx = x.decode(s.as_ref())?;
        let output = Self::from_byte_slice(&xx);

        match output {
            Ok(v) => return Ok(v),
            Err(_) => return Err(SlugEncodingError::DecodingError)
        }
    }
    pub fn from_base58<T: AsRef<str>>(s: T) -> Result<Self,SlugEncodingError> {
        let x = SlugEncodingUsage::new(SlugEncodings::Base58);
        let xx = x.decode(s.as_ref())?;
        let output = Self::from_byte_slice(&xx);

        match output {
            Ok(v) => return Ok(v),
            Err(_) => return Err(SlugEncodingError::DecodingError)
        }
    }
    pub fn from_base64<T: AsRef<str>>(s: T) -> Result<Self,SlugEncodingError> {
        let x = SlugEncodingUsage::new(SlugEncodings::Base64);
        let xx = x.decode(s.as_ref())?;
        let output = Self::from_byte_slice(&xx);

        match output {
            Ok(v) => return Ok(v),
            Err(_) => return Err(SlugEncodingError::DecodingError)
        }
    }
    pub fn from_base64_url_safe<T: AsRef<str>>(s: T) -> Result<Self,SlugEncodingError> {
        let x = SlugEncodingUsage::new(SlugEncodings::Base64urlsafe);
        let xx = x.decode(s.as_ref())?;
        let output = Self::from_byte_slice(&xx);

        match output {
            Ok(v) => return Ok(v),
            Err(_) => return Err(SlugEncodingError::DecodingError)
        }
    }
    /// From Hex String (Upper)
    pub fn from_hex_string<T: AsRef<str>>(bytes: T) -> Result<Vec<u8>,subtle_encoding::Error> {
        Ok(hex::decode_upper(bytes.as_ref().as_bytes())?)
    }
}