libsumatracrypt_rs/
encryption.rs1use ecies_ed25519::*;
2use rsa::{Oaep, RsaPrivateKey, RsaPublicKey};
3use rsa::oaep::*;
4use rsa::pkcs8::*;
5
6use ecies_ed25519::{PublicKey,SecretKey};
7use bs58::*;
8use hex::*;
9use schnorrkel::derive;
10use sha2::Sha256;
11use zeroize::*;
12use base32::*;
13
14use serde::{Serialize,Deserialize};
15
16use new_rand::*;
17
18use crate::signatures::ed25519::ED25519PublicKey;
19
20pub struct SumatraRSA4096;
23
24#[derive(Zeroize, ZeroizeOnDrop, Serialize, Deserialize)]
25pub struct SumatraRSAPublicKey(String);
26
27#[derive(Zeroize, ZeroizeOnDrop, Serialize, Deserialize)]
28pub struct SumatraRSASecretKey(String);
29
30pub struct SumatraEncryptECIES;
31
32#[derive(Zeroize, ZeroizeOnDrop,Serialize,Deserialize,Clone)]
33pub struct ECIESPublicKey(String);
34
35#[derive(Zeroize, ZeroizeOnDrop,Serialize,Deserialize,Clone)]
36pub struct ECIESSecretKey(String);
37
38#[derive(Zeroize, ZeroizeOnDrop,Serialize,Deserialize,Clone)]
39pub struct ECIESCipherText(String);
40
41#[derive(Clone, Zeroize, ZeroizeOnDrop,Serialize,Deserialize)]
42pub struct ECIESDecodedMessage(Vec<u8>);
43
44impl SumatraEncryptECIES {
45 pub fn generate() -> (ECIESSecretKey,ECIESPublicKey) {
46 let mut csprng = rand::thread_rng();
48
49 let (sk,pk) = ecies_ed25519::generate_keypair(&mut csprng);
51
52 let secretkey = hex::encode_upper(sk.as_bytes());
53 let publickey = hex::encode_upper(pk.as_bytes());
54
55 return (ECIESSecretKey(secretkey),ECIESPublicKey(publickey))
56 }
57 pub fn encrypt<B: AsRef<[u8]>>(pk: ECIESPublicKey, message: B) -> ECIESCipherText {
59 let mut csprng = rand::thread_rng();
61
62 let pk_bytes = hex::decode(pk.0.as_bytes()).expect("Failed To Decode Public Key From Hex");
64 let publickey = ecies_ed25519::PublicKey::from_bytes(&pk_bytes).expect("Failed To Get Public Key From Bytes");
65
66 let encrypted = ecies_ed25519::encrypt(&publickey, message.as_ref(), &mut csprng).expect("Failed To Encrypt");
67 let ciphertext = bs58::encode(encrypted).into_string();
68
69 return ECIESCipherText(ciphertext)
70
71 }
72 pub fn decrypt(sk: ECIESSecretKey, ciphertext: ECIESCipherText) -> ECIESDecodedMessage {
73 let sk_bytes = hex::decode(sk.0.as_bytes()).expect("Failed To Decoded Secret Key From Hex");
74 let secretkey = ecies_ed25519::SecretKey::from_bytes(&sk_bytes).expect("Failed To Get Secret Key From Bytes");
75
76 let decoded_ciphertext = bs58::decode(&ciphertext.0).into_vec().expect("Failed To Decode Ciphertext From Bs58");
77 let decrypted = ecies_ed25519::decrypt(&secretkey, &decoded_ciphertext.as_ref()).expect("Failed To Decrypt Message From Encryption Key");
78
79 return ECIESDecodedMessage(decrypted)
80 }
81}
82
83impl ECIESDecodedMessage {
84 pub fn to_utf8_string(&self) -> String {
85 return String::from_utf8(self.0.to_vec()).expect("Failed To Decode From String")
86 }
87 pub fn to_vec(&self) -> Vec<u8> {
88 return self.0.to_vec()
89 }
90 pub fn as_bytes(&self) -> &[u8] {
91 return &self.0
92 }
93}
94
95impl ECIESPublicKey {
96 pub fn new<T: AsRef<str>>(pk_hex: T) -> Self {
97 return Self::from_hex(pk_hex.as_ref())
98 }
99 pub fn from_hex<T: AsRef<str>>(pk: T) -> Self {
100 return Self(pk.as_ref().to_string())
101 }
102 pub fn from_bytes<T: AsRef<[u8]>>(pk_bytes: T) -> Self {
103 return Self(hex::encode_upper(pk_bytes.as_ref()))
104 }
105 pub fn to_bytes(&self) -> Vec<u8> {
106 return hex::decode(&self.0).expect("Failed To Decode From Hex");
107 }
108 pub fn public_key(&self) -> &str {
109 return &self.0
110 }
111 pub fn to_dalek_pk(&self) -> ecies_ed25519::PublicKey {
112 let bytes = self.to_bytes();
113
114 return ecies_ed25519::PublicKey::from_bytes(&bytes).expect("Failed To Construct Public Key From Bytes For ECIES")
115 }
116 pub fn to_base32(&self) -> String {
117 let pk_bytes = self.to_bytes();
118 return base32::encode(base32::Alphabet::Rfc4648 { padding: false },&pk_bytes);
119 }
120 pub fn from_base32<T: AsRef<str>>(pk_bs32: T) -> Self {
121 let pk_bytes = base32::decode(base32::Alphabet::Rfc4648 { padding: false }, pk_bs32.as_ref()).expect("Failed To Decode Base32");
122 return Self::from_bytes(&pk_bytes)
123 }
124}
125
126impl ECIESSecretKey {
127 pub fn to_bytes(&self) -> Vec<u8> {
128 return hex::decode(&self.0).expect("Failed To Decode From Hex To Secret Key")
129 }
130}
131
132
133impl SumatraRSA4096 {
134 pub fn generate() -> (SumatraRSASecretKey,SumatraRSAPublicKey) {
135 let mut rng = new_rand::thread_rng();
136 let bits = 4096;
137 let priv_key = RsaPrivateKey::new(&mut rng, bits).expect("failed to generate a key");
138 let pub_key = RsaPublicKey::from(&priv_key);
139
140
141 let sk_pem = priv_key.to_pkcs8_pem(LineEnding::LF).expect("Failed To Encode RSA Secret Key From PKCS8");
142 let pk_pem = pub_key.to_public_key_pem(LineEnding::LF).expect("Failed To Encode RSA Pub from PKCS8");
143 return (SumatraRSASecretKey(sk_pem.to_string()),SumatraRSAPublicKey(pk_pem))
146 }
147 pub fn encrypt<T: AsRef<[u8]>>(pk: SumatraRSAPublicKey, data: T) -> String {
148 let mut rng = new_rand::thread_rng();
149
150 let padding = Oaep::new::<Sha256>();
151
152 let encrypted = pk.decode_from_pem().encrypt(&mut rng, padding, data.as_ref()).expect("Failed To Encode Using RSA");
153
154 return bs58::encode(encrypted).into_string();
155 }
156 pub fn decrypt<T: AsRef<str>>(sk: SumatraRSASecretKey,encrypted: T) -> Vec<u8> {
157 let padding = Oaep::new::<Sha256>();
158
159
160 let decrypted_bytes = bs58::decode(encrypted.as_ref()).into_vec().expect("Failed To Decode RSA Encrypted Data From Base58");
161
162 let dec_data = sk.decode_from_pem().decrypt(padding, &decrypted_bytes).expect("failed to decrypt");
163
164 return dec_data
165 }
166}
167
168impl SumatraRSAPublicKey {
169 pub fn public_key(&self) {
170 &self.0;
171 }
172 pub fn decode_from_pem(&self) -> RsaPublicKey {
173 return RsaPublicKey::from_public_key_pem(&self.0).expect("Failed To Convert Public Key");
174 }
175}
176
177impl SumatraRSASecretKey {
178 pub fn decode_from_pem(&self) -> RsaPrivateKey {
179 return RsaPrivateKey::from_pkcs8_pem(&self.0).expect("Failed To Decoded Secret Key")
180 }
181}