@@ -122,19 +122,19 @@ func EncryptAsymmetric(plaintext, publicKeyHex string) (string, error) {
122122 return "" , err
123123 }
124124
125- // Spin up ephemeral X25519 keys
125+ // Spin up ephemeral X25519 keys
126126 kp , err := RandomKeyPair ()
127127 if err != nil {
128128 return "" , err
129129 }
130130
131- // Perform a DHKA
131+ // Perform a DHKA
132132 sessionKeys , err := ClientSessionKeys (kp , recipientPubKeyBytes )
133133 if err != nil {
134134 return "" , err
135135 }
136136
137- // Encrypt data with XChaCha20-poly1305
137+ // Encrypt data with XChaCha20-poly1305
138138 ciphertext , err := EncryptB64 (plaintext , sessionKeys .Tx )
139139 if err != nil {
140140 return "" , err
@@ -145,99 +145,99 @@ func EncryptAsymmetric(plaintext, publicKeyHex string) (string, error) {
145145}
146146
147147func DecryptAsymmetric (ciphertextString , privateKeyHex , publicKeyHex string ) (string , error ) {
148- segments := strings .Split (ciphertextString , ":" )
149-
150- version := segments [1 ]
151- if version != "v1" {
152- err := fmt .Errorf ("unsupported version: %s" , version )
153- log .Println (err )
154- return "" , err
155- }
156-
157- ephemeralPublicKeyBytes , err := hex .DecodeString (segments [2 ])
158- if err != nil {
159- log .Printf ("Failed to decode ephemeral public key hex: %v\n " , err )
160- return "" , err
161- }
162-
163- privateKeyBytes , err := hex .DecodeString (privateKeyHex )
164- if err != nil {
165- log .Printf ("Failed to decode private key hex: %v\n " , err )
166- return "" , err
167- }
168-
169- publicKeyBytes , err := hex .DecodeString (publicKeyHex )
170- if err != nil {
171- log .Printf ("Failed to decode public key hex: %v\n " , err )
172- return "" , err
173- }
174-
175- kp := sodium.KXKP {
176- PublicKey : sodium.KXPublicKey {Bytes : publicKeyBytes },
177- SecretKey : sodium.KXSecretKey {Bytes : privateKeyBytes },
178- }
179-
180- // Perform DHKA
181- sessionKeys , err := ServerSessionKeys (kp , ephemeralPublicKeyBytes )
182- if err != nil {
183- return "" , err
184- }
185-
186- // Extract ciphertext from ph.
187- ciphertextB64 := segments [3 ]
188-
189- // Decrypt data with XChaCha20-poly1305
190- plaintext , err := DecryptB64 (ciphertextB64 , sessionKeys .Rx )
191- if err != nil {
192- log .Printf ("Failed to decrypt asymmetrically: %v\n " , err )
193- return "" , err
194- }
195- return plaintext , nil
148+ segments := strings .Split (ciphertextString , ":" )
149+
150+ version := segments [1 ]
151+ if version != "v1" {
152+ err := fmt .Errorf ("unsupported version: %s" , version )
153+ log .Println (err )
154+ return "" , err
155+ }
156+
157+ ephemeralPublicKeyBytes , err := hex .DecodeString (segments [2 ])
158+ if err != nil {
159+ log .Printf ("Failed to decode ephemeral public key hex: %v\n " , err )
160+ return "" , err
161+ }
162+
163+ privateKeyBytes , err := hex .DecodeString (privateKeyHex )
164+ if err != nil {
165+ log .Printf ("Failed to decode private key hex: %v\n " , err )
166+ return "" , err
167+ }
168+
169+ publicKeyBytes , err := hex .DecodeString (publicKeyHex )
170+ if err != nil {
171+ log .Printf ("Failed to decode public key hex: %v\n " , err )
172+ return "" , err
173+ }
174+
175+ kp := sodium.KXKP {
176+ PublicKey : sodium.KXPublicKey {Bytes : publicKeyBytes },
177+ SecretKey : sodium.KXSecretKey {Bytes : privateKeyBytes },
178+ }
179+
180+ // Perform DHKA
181+ sessionKeys , err := ServerSessionKeys (kp , ephemeralPublicKeyBytes )
182+ if err != nil {
183+ return "" , err
184+ }
185+
186+ // Extract ciphertext from ph.
187+ ciphertextB64 := segments [3 ]
188+
189+ // Decrypt data with XChaCha20-poly1305
190+ plaintext , err := DecryptB64 (ciphertextB64 , sessionKeys .Rx )
191+ if err != nil {
192+ log .Printf ("Failed to decrypt asymmetrically: %v\n " , err )
193+ return "" , err
194+ }
195+ return plaintext , nil
196196}
197197
198198// decryptSecret decrypts a secret's key, value, and optional comment using asymmetric decryption.
199199func DecryptSecret (secret map [string ]interface {}, privateKeyHex , publicKeyHex string ) (decryptedKey string , decryptedValue string , decryptedComment string , err error ) {
200- // Decrypt the key
201- key , ok := secret ["key" ].(string )
202- if ! ok {
203- err = fmt .Errorf ("key is not a string" )
204- return
205- }
206- decryptedKey , err = DecryptAsymmetric (key , privateKeyHex , publicKeyHex )
207- if err != nil {
208- log .Printf ("Failed to decrypt key: %v\n " , err )
209- return
210- }
211-
212- // Decrypt the value
213- value , ok := secret ["value" ].(string )
214- if ! ok {
215- err = fmt .Errorf ("value is not a string" )
216- return
217- }
218- decryptedValue , err = DecryptAsymmetric (value , privateKeyHex , publicKeyHex )
219- if err != nil {
220- log .Printf ("Failed to decrypt value: %v\n " , err )
221- return
222- }
223-
224- // Decrypt the comment if it exists
225- comment , ok := secret ["comment" ].(string )
226- if ok && comment != "" {
227- decryptedComment , err = DecryptAsymmetric (comment , privateKeyHex , publicKeyHex )
228- if err != nil {
229- log .Printf ("Failed to decrypt comment: %v\n " , err )
230- err = nil
231- }
232- }
233-
234- return decryptedKey , decryptedValue , decryptedComment , nil
200+ // Decrypt the key
201+ key , ok := secret ["key" ].(string )
202+ if ! ok {
203+ err = fmt .Errorf ("key is not a string" )
204+ return
205+ }
206+ decryptedKey , err = DecryptAsymmetric (key , privateKeyHex , publicKeyHex )
207+ if err != nil {
208+ log .Printf ("Failed to decrypt key: %v\n " , err )
209+ return
210+ }
211+
212+ // Decrypt the value
213+ value , ok := secret ["value" ].(string )
214+ if ! ok {
215+ err = fmt .Errorf ("value is not a string" )
216+ return
217+ }
218+ decryptedValue , err = DecryptAsymmetric (value , privateKeyHex , publicKeyHex )
219+ if err != nil {
220+ log .Printf ("Failed to decrypt value: %v\n " , err )
221+ return
222+ }
223+
224+ // Decrypt the comment if it exists
225+ comment , ok := secret ["comment" ].(string )
226+ if ok && comment != "" {
227+ decryptedComment , err = DecryptAsymmetric (comment , privateKeyHex , publicKeyHex )
228+ if err != nil {
229+ log .Printf ("Failed to decrypt comment: %v\n " , err )
230+ err = nil
231+ }
232+ }
233+
234+ return decryptedKey , decryptedValue , decryptedComment , nil
235235}
236236
237237// Decrypt decrypts the provided ciphertext using the Phase encryption mechanism.
238- func DecryptWrappedKeyShare (Keyshare1 string , Keyshare0 string , AppToken string , Keyshare1UnwrapKey string , PssUserPublicKey string , Host string ) (string , error ) {
238+ func DecryptWrappedKeyShare (Keyshare1 string , Keyshare0 string , TokenType string , AppToken string , Keyshare1UnwrapKey string , PssUserPublicKey string , Host string ) (string , error ) {
239239 // Fetch the wrapped key share using the app token and host
240- wrappedKeyShare , err := network .FetchAppKey (AppToken , Host )
240+ wrappedKeyShare , err := network .FetchAppKey (TokenType , AppToken , Host )
241241 if err != nil {
242242 log .Fatalf ("Failed to fetch wrapped key share: %v" , err )
243243 return "" , err
@@ -293,24 +293,23 @@ func GenerateEnvKeyPair(seed string) (publicKeyHex, privateKeyHex string, err er
293293 return "" , "" , fmt .Errorf ("incorrect seed length: expected 32 bytes, got %d" , len (seedBytes ))
294294 }
295295
296- // Prepare the seed as KXSeed
297- var seedKX sodium.KXSeed
298- copy (seedKX .Bytes [:], seedBytes )
296+ // Prepare the seed as KXSeed
297+ var seedKX sodium.KXSeed
298+ copy (seedKX .Bytes [:], seedBytes )
299299
300300 // Allocate slice if KXSeed.Bytes is a slice
301301 seedKX .Bytes = make ([]byte , len (seedBytes ))
302302 copy (seedKX .Bytes , seedBytes )
303303
304- // Generate key pair from seed
305- keyPair := sodium .SeedKXKP (seedKX )
304+ // Generate key pair from seed
305+ keyPair := sodium .SeedKXKP (seedKX )
306306
307- publicKeyHex = hex .EncodeToString (keyPair .PublicKey .Bytes [:])
308- privateKeyHex = hex .EncodeToString (keyPair .SecretKey .Bytes [:])
307+ publicKeyHex = hex .EncodeToString (keyPair .PublicKey .Bytes [:])
308+ privateKeyHex = hex .EncodeToString (keyPair .SecretKey .Bytes [:])
309309
310- return publicKeyHex , privateKeyHex , nil
310+ return publicKeyHex , privateKeyHex , nil
311311}
312312
313-
314313// Blake2bDigest generates a BLAKE2b hash of the input string with a salt using the sodium library.
315314func Blake2bDigest (inputStr , salt string ) (string , error ) {
316315 hashSize := 32 // 32 bytes (256 bits) as an example
@@ -370,4 +369,4 @@ func ReconstructSecret(share1, share2 string) (string, error) {
370369
371370 // Encode the reconstructed secret back to a hex string.
372371 return hex .EncodeToString (reconstructedSecret ), nil
373- }
372+ }
0 commit comments