Skip to content

Commit d67daa2

Browse files
mustyantsevsujankotamkleene
authored
feat: SDK Encrypt (with mocked rewrap) (#45)
Co-authored-by: sujan kota <[email protected]> Co-authored-by: Morgan Kleene <[email protected]>
1 parent af51404 commit d67daa2

File tree

14 files changed

+997
-5
lines changed

14 files changed

+997
-5
lines changed

sdk/pom.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@
44
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
55
<modelVersion>4.0.0</modelVersion>
66
<name>sdk</name>
7+
<build>
8+
<plugins>
9+
<plugin>
10+
<groupId>org.apache.maven.plugins</groupId>
11+
<artifactId>maven-compiler-plugin</artifactId>
12+
<configuration>
13+
<source>11</source>
14+
<target>11</target>
15+
</configuration>
16+
</plugin>
17+
</plugins>
18+
</build>
719
<artifactId>sdk</artifactId>
820
<parent>
921
<artifactId>sdk-pom</artifactId>
@@ -83,6 +95,16 @@
8395
<version>4.13.1</version>
8496
<scope>test</scope>
8597
</dependency>
98+
<dependency>
99+
<groupId>com.google.code.gson</groupId>
100+
<artifactId>gson</artifactId>
101+
<version>2.10.1</version>
102+
</dependency>
103+
<dependency>
104+
<groupId>commons-codec</groupId>
105+
<artifactId>commons-codec</artifactId>
106+
<version>1.17.0</version>
107+
</dependency>
86108
<dependency>
87109
<groupId>org.apache.commons</groupId>
88110
<artifactId>commons-compress</artifactId>

sdk/src/main/java/io/opentdf/platform/sdk/AesGcm.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import java.util.Base64;
1212

1313
public class AesGcm {
14-
private static final int GCM_NONCE_LENGTH = 12; // in bytes
14+
public static final int GCM_NONCE_LENGTH = 12; // in bytes
1515
private static final int GCM_TAG_LENGTH = 16; // in bytes
1616
private static final String CIPHER_TRANSFORM = "AES/GCM/NoPadding";
1717

@@ -37,13 +37,26 @@ public AesGcm(byte[] key) {
3737
*/
3838
public byte[] encrypt(byte[] plaintext) throws NoSuchPaddingException, NoSuchAlgorithmException,
3939
InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
40+
return encrypt(plaintext, 0, plaintext.length);
41+
}
42+
43+
/**
44+
* <p>encrypt.</p>
45+
*
46+
* @param plaintext the plaintext byte array to encrypt
47+
* @param offset where the input start
48+
* @param len input length
49+
* @return the encrypted text
50+
*/
51+
public byte[] encrypt(byte[] plaintext, int offset, int len) throws NoSuchPaddingException, NoSuchAlgorithmException,
52+
InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
4053
Cipher cipher = Cipher.getInstance(CIPHER_TRANSFORM);
4154
byte[] nonce = new byte[GCM_NONCE_LENGTH];
4255
SecureRandom.getInstanceStrong().nextBytes(nonce);
4356
GCMParameterSpec spec = new GCMParameterSpec(GCM_TAG_LENGTH * 8, nonce);
4457
cipher.init(Cipher.ENCRYPT_MODE, key, spec);
4558

46-
byte[] cipherText = cipher.doFinal(plaintext);
59+
byte[] cipherText = cipher.doFinal(plaintext, offset, len);
4760
byte[] cipherTextWithNonce = new byte[nonce.length + cipherText.length];
4861
System.arraycopy(nonce, 0, cipherTextWithNonce, 0, nonce.length);
4962
System.arraycopy(cipherText, 0, cipherTextWithNonce, nonce.length, cipherText.length);
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package io.opentdf.platform.sdk;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
import java.util.function.Consumer;
7+
8+
public class Config {
9+
10+
public static final int TDF3_KEY_SIZE = 2048;
11+
public static final int DEFAULT_SEGMENT_SIZE = 2 * 1024 * 1024; // 2mb
12+
public static final String KAS_PUBLIC_KEY_PATH = "/kas_public_key";
13+
14+
public enum TDFFormat {
15+
JSONFormat,
16+
XMLFormat
17+
}
18+
19+
public enum IntegrityAlgorithm {
20+
HS256,
21+
GMAC
22+
}
23+
24+
public static final int K_HTTP_OK = 200;
25+
26+
public static class KASInfo {
27+
public String URL;
28+
public String PublicKey;
29+
}
30+
31+
public static class TDFConfig {
32+
public int defaultSegmentSize;
33+
public boolean enableEncryption;
34+
public TDFFormat tdfFormat;
35+
public String tdfPublicKey;
36+
public String tdfPrivateKey;
37+
public String metaData;
38+
public IntegrityAlgorithm integrityAlgorithm;
39+
public IntegrityAlgorithm segmentIntegrityAlgorithm;
40+
public List<String> attributes;
41+
public List<KASInfo> kasInfoList;
42+
43+
public TDFConfig() {
44+
this.defaultSegmentSize = DEFAULT_SEGMENT_SIZE;
45+
this.enableEncryption = true;
46+
this.tdfFormat = TDFFormat.JSONFormat;
47+
this.integrityAlgorithm = IntegrityAlgorithm.HS256;
48+
this.segmentIntegrityAlgorithm = IntegrityAlgorithm.GMAC;
49+
this.attributes = new ArrayList<>();
50+
this.kasInfoList = new ArrayList<>();
51+
}
52+
}
53+
54+
@SafeVarargs
55+
public static TDFConfig newTDFConfig(Consumer<TDFConfig>... options) {
56+
TDFConfig config = new TDFConfig();
57+
for (Consumer<TDFConfig> option : options) {
58+
option.accept(config);
59+
}
60+
return config;
61+
}
62+
63+
public static Consumer<TDFConfig> withDataAttributes(String... attributes) {
64+
return (TDFConfig config) -> {
65+
Collections.addAll(config.attributes, attributes);
66+
};
67+
}
68+
69+
public static Consumer<TDFConfig> withKasInformation(KASInfo... kasInfoList) {
70+
return (TDFConfig config) -> {
71+
Collections.addAll(config.kasInfoList, kasInfoList);
72+
};
73+
}
74+
75+
public static Consumer<TDFConfig> withMetaData(String metaData) {
76+
return (TDFConfig config) -> config.metaData = metaData;
77+
}
78+
79+
public static Consumer<TDFConfig> withSegmentSize(int size) {
80+
return (TDFConfig config) -> config.defaultSegmentSize = size;
81+
}
82+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.opentdf.platform.sdk;
2+
3+
import javax.crypto.Mac;
4+
import javax.crypto.spec.SecretKeySpec;
5+
import java.io.UnsupportedEncodingException;
6+
import java.security.InvalidKeyException;
7+
import java.security.NoSuchAlgorithmException;
8+
9+
public class CryptoUtils {
10+
public static byte[] CalculateSHA256Hmac(byte[] key, byte[] data) throws NoSuchAlgorithmException,
11+
InvalidKeyException {
12+
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
13+
SecretKeySpec secret_key = new SecretKeySpec(key, "HmacSHA256");
14+
sha256_HMAC.init(secret_key);
15+
16+
return sha256_HMAC.doFinal(data);
17+
}
18+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package io.opentdf.platform.sdk;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
5+
import java.util.List;
6+
7+
public class Manifest {
8+
static public class Segment {
9+
public String hash;
10+
public long segmentSize;
11+
public long encryptedSegmentSize;
12+
}
13+
14+
static public class RootSignature {
15+
@SerializedName(value = "alg")
16+
public String algorithm;
17+
@SerializedName(value = "sig")
18+
public String signature;
19+
}
20+
21+
static public class IntegrityInformation {
22+
public RootSignature rootSignature;
23+
public String segmentHashAlg;
24+
public int segmentSizeDefault;
25+
public int encryptedSegmentSizeDefault;
26+
public List<Segment> segments;
27+
}
28+
29+
static public class KeyAccess {
30+
@SerializedName(value = "type")
31+
public String keyType;
32+
public String url;
33+
public String protocol;
34+
public String wrappedKey;
35+
public String policyBinding;
36+
public String encryptedMetadata;
37+
}
38+
39+
static public class Method {
40+
public String algorithm;
41+
public String iv;
42+
public Boolean IsStreamable;
43+
}
44+
45+
static public class EncryptionInformation {
46+
@SerializedName(value = "type")
47+
public String keyAccessType;
48+
public String policy;
49+
50+
@SerializedName(value = "keyAccess")
51+
public List<KeyAccess> keyAccessObj;
52+
public Method method;
53+
public IntegrityInformation integrityInformation;
54+
}
55+
56+
static public class Payload {
57+
public String type;
58+
public String url;
59+
public String protocol;
60+
public String mimeType;
61+
public Boolean isEncrypted;
62+
}
63+
public EncryptionInformation encryptionInformation;
64+
public Payload payload;
65+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package io.opentdf.platform.sdk;
2+
3+
import java.util.List;
4+
import java.util.UUID;
5+
6+
public class PolicyObject {
7+
static public class AttributeObject {
8+
public String attribute;
9+
public String displayName;
10+
public boolean isDefault;
11+
public String pubKey;
12+
public String kasURL;
13+
}
14+
15+
static public class Body {
16+
public List<AttributeObject> dataAttributes;
17+
public List<String> dissem;
18+
}
19+
20+
public String uuid;
21+
public Body body;
22+
}

0 commit comments

Comments
 (0)