Skip to content
This repository was archived by the owner on Jun 5, 2025. It is now read-only.

Commit de62bf4

Browse files
Adding missing new classes
1 parent 0c2d3c4 commit de62bf4

3 files changed

Lines changed: 121 additions & 0 deletions

File tree

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/**
2+
*
3+
*/
4+
package com.venafi.vcert.sdk.connectors.cloud;
5+
6+
import java.io.ByteArrayOutputStream;
7+
import java.io.IOException;
8+
import java.security.GeneralSecurityException;
9+
10+
import org.bouncycastle.crypto.digests.Blake2bDigest;
11+
12+
import com.iwebpp.crypto.TweetNaclFast;
13+
14+
15+
/**
16+
* The following utility is based on the SealBoxUtility code shared in the stackoverflow question
17+
* <a href="https://stackoverflow.com/questions/42456624/how-can-i-create-or-open-a-libsodium-compatible-sealed-box-in-pure-java">
18+
* How can I create or open a libsodium compatible sealed box in pure Java</a>.
19+
* <br/>
20+
* The main difference is on this version is being used the <a href="https://github.com/bcgit/bc-java/blob/master/core/src/main/java/org/bouncycastle/crypto/digests/Blake2bDigest.java">
21+
* org.bouncycastle.crypto.digests.Blake2bDigest</a> from <a href="https://github.com/bcgit/bc-java">The Bouncy Castle Crypto Package For Java</a>
22+
* instead of <a href="https://github.com/alphazero/Blake2b">Blake2b</a> to get the Blake2b hash.
23+
* <br/><br/>
24+
*
25+
* Has also a dependency on TweetNaclFast from <a href="https://github.com/InstantWebP2P/tweetnacl-java">https://github.com/InstantWebP2P/tweetnacl-java</a>.
26+
*
27+
*
28+
*/
29+
public class SealedBoxUtility {
30+
31+
32+
public static final int crypto_box_NONCEBYTES = 24;
33+
//public static final int crypto_box_PUBLICKEYBYTES = 32;
34+
//public static final int crypto_box_MACBYTES = 16;
35+
//public static final int crypto_box_SEALBYTES = (crypto_box_PUBLICKEYBYTES + crypto_box_MACBYTES);
36+
37+
// libsodium
38+
// int crypto_box_seal(unsigned char *c, const unsigned char *m,
39+
// unsigned long long mlen, const unsigned char *pk);
40+
/**
41+
* Encrypt in a sealed box
42+
*
43+
* @param receiverPubKey receiver public key
44+
* @param clearText clear text
45+
* @return encrypted message
46+
* @throws GeneralSecurityException
47+
*/
48+
public static byte[] crypto_box_seal(byte[] receiverPubKey, byte[] clearText) throws GeneralSecurityException {
49+
50+
// create ephemeral keypair for sender
51+
TweetNaclFast.Box.KeyPair ephkeypair = TweetNaclFast.Box.keyPair();
52+
// create nonce
53+
byte[] nonce = crypto_box_seal_nonce(ephkeypair.getPublicKey(), receiverPubKey);
54+
TweetNaclFast.Box box = new TweetNaclFast.Box(receiverPubKey, ephkeypair.getSecretKey());
55+
byte[] ciphertext = box.box(clearText, nonce);
56+
if (ciphertext == null)
57+
throw new GeneralSecurityException("Could not create the crypto box");
58+
59+
byte[] sealedbox = null;
60+
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
61+
byteArrayOutputStream.write(ephkeypair.getPublicKey());
62+
byteArrayOutputStream.write(ciphertext);
63+
sealedbox = byteArrayOutputStream.toByteArray();
64+
} catch (IOException e) {
65+
throw new GeneralSecurityException("Could not create the sealed crypto box", e);
66+
}
67+
return sealedbox;
68+
}
69+
70+
/**
71+
* hash the combination of senderpk + mypk into nonce using blake2b hash
72+
* @param senderpk the senders public key
73+
* @param mypk my own public key
74+
* @return the nonce computed using Blake2b generic hash
75+
*/
76+
public static byte[] crypto_box_seal_nonce(byte[] senderpk, byte[] mypk){
77+
// C source ported from libsodium
78+
// crypto_generichash_state st;
79+
//
80+
// crypto_generichash_init(&st, NULL, 0U, crypto_box_NONCEBYTES);
81+
// crypto_generichash_update(&st, pk1, crypto_box_PUBLICKEYBYTES);
82+
// crypto_generichash_update(&st, pk2, crypto_box_PUBLICKEYBYTES);
83+
// crypto_generichash_final(&st, nonce, crypto_box_NONCEBYTES);
84+
//
85+
// return 0;
86+
//final org.bouncycastle.jcajce.provider.digest.Blake2b blake2b = Blake2b.Digest.newInstance( crypto_box_NONCEBYTES );
87+
final Blake2bDigest blake2b = new Blake2bDigest( crypto_box_NONCEBYTES*8 );
88+
blake2b.update(senderpk, 0, senderpk.length);
89+
blake2b.update(mypk, 0, mypk.length);
90+
byte[] nonce = new byte[crypto_box_NONCEBYTES];
91+
blake2b.doFinal(nonce, 0);
92+
if (nonce == null || nonce.length!=crypto_box_NONCEBYTES) throw new IllegalArgumentException("Blake2b hashing failed");
93+
return nonce;
94+
}
95+
96+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.venafi.vcert.sdk.connectors.cloud.domain;
2+
3+
import java.time.OffsetDateTime;
4+
import lombok.Data;
5+
6+
@Data
7+
public class EdgeEncryptionKey {
8+
9+
private String id;
10+
private String companyId;
11+
private String key;
12+
private String keyAlgorithm;
13+
private OffsetDateTime lastBackupDate;
14+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.venafi.vcert.sdk.connectors.cloud.endpoint;
2+
3+
import lombok.Data;
4+
5+
@Data
6+
public class KeystoreRequest {
7+
private String exportFormat;
8+
private String encryptedPrivateKeyPassphrase;
9+
private String encryptedKeystorePassphrase;
10+
private String certificateLabel;
11+
}

0 commit comments

Comments
 (0)