Skip to content

Commit a734017

Browse files
authored
Merge pull request #4 from Cosmian/ffi_speed
Ffi speed
2 parents 543d762 + ca60f95 commit a734017

File tree

15 files changed

+561
-64
lines changed

15 files changed

+561
-64
lines changed

README.md

Lines changed: 74 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,29 @@ This library is available on Maven Central
1818
<dependency>
1919
<groupId>com.cosmian</groupId>
2020
<artifactId>cosmian_java_lib</artifactId>
21-
<version>0.5.2</version>
21+
<version>0.6.0</version>
2222
</dependency>
2323
```
2424

2525

26-
2726
- [Confidential Data Access](#confidential-data-access)
2827
- [Versions Correspondence](#versions-correspondence)
2928
- [Quick Start ABE+AES](#quick-start-abeaes)
3029
- [Local ABE+AES encryption and decryption](#local-abeaes-encryption-and-decryption)
3130
- [Building the the ABE GPSW native lib](#building-the-the-abe-gpsw-native-lib)
3231
- [Using the native library](#using-the-native-library)
32+
- [encryption](#encryption)
33+
- [decryption](#decryption)
34+
- [Locally cached encryption and decryption](#locally-cached-encryption-and-decryption)
35+
- [encryption](#encryption-1)
36+
- [decryption](#decryption-1)
3337
- [Secure Computations](#secure-computations)
3438
- [Confidential KMS](#confidential-kms)
3539

3640

3741

42+
43+
3844
## Confidential Data Access
3945

4046
Cosmian Ubiquitous Encryption provides the ability to encrypt data - locally or inside the KMS - using policy attributes. The only users able to decrypt the data are those possessing a key holding the correct access policy.
@@ -46,10 +52,13 @@ In addition, Cosmian Confidential Data Access allows building secure indexes on
4652

4753
### Versions Correspondence
4854

55+
This table shows the minimum versions correspondances between the various components
56+
4957
KMS Server | Java Lib | abe_gpsw lib
5058
-----------|----------|--------------
5159
1.2.0 | 0.5.0 | 0.3.0
5260
1.2.1 | 0.5.2 | 0.4.0
61+
1.2.1 | 0.6.0 | 0.6.0
5362

5463

5564
### Quick Start ABE+AES
@@ -83,7 +92,7 @@ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
8392
```sh
8493
git clone https://github.com/Cosmian/abe_gpsw.git && \
8594
cd abe_gpsw && \
86-
git checkout v0.4.0
95+
git checkout v0.6.0
8796
```
8897

8998
3. Build the native library, which will be available as `libabe_gpsw.so` (linux) or `libabe_gpsw.dylib` (macos) in the `target` directory
@@ -113,6 +122,8 @@ To learn how to use the local ABE+AES hybrid encryption facilities, check [the H
113122

114123
A typical workflow will be as follows
115124

125+
##### encryption
126+
116127
```java
117128
// The data we want to encrypt/decrypt
118129
byte[] data = "This s a test message".getBytes(StandardCharsets.UTF_8);
@@ -142,13 +153,13 @@ Attr[] attributes = new Attr[] { new Attr("Department", "FIN"), new Attr("Securi
142153
// generated AES key.
143154
// This example assumes that the Unique ID can be recovered at time of
144155
// decryption, and is thus not stored as part of the encrypted header.
145-
// If that is not the case check the other signature of #FFI.encryptedHeader()
156+
// If that is not the case check the other signature of #Ffi.encryptedHeader()
146157
// to inject the unique id.
147-
EncryptedHeader encryptedHeader = FFI.encryptHeader(publicKey, attributes);
158+
EncryptedHeader encryptedHeader = Ffi.encryptHeader(publicKey, attributes);
148159

149160
// The data can now be encrypted with the generated key
150161
// The block number is also part of the authentication of the AES scheme
151-
byte[] encryptedBlock = FFI.encryptBlock(encryptedHeader.getSymmetricKey(), uid, 0, data);
162+
byte[] encryptedBlock = Ffi.encryptBlock(encryptedHeader.getSymmetricKey(), uid, 0, data);
152163

153164
// Create a full message with header+encrypted data. The length of the header
154165
// is pre-pended.
@@ -161,7 +172,10 @@ bao.write(encryptedHeader.getEncryptedHeaderBytes());
161172
bao.write(encryptedBlock);
162173
bao.flush();
163174
byte[] ciphertext = bao.toByteArray();
175+
```
176+
##### decryption
164177

178+
```java
165179
//
166180
// Decryption
167181
//
@@ -176,15 +190,67 @@ byte[] encryptedHeader_ = Arrays.copyOfRange(ciphertext, 4, 4 + headerSize_);
176190
byte[] encryptedContent = Arrays.copyOfRange(ciphertext, 4 + headerSize_, ciphertext.length);
177191

178192
// Decrypt he header to recover the symmetric AES key
179-
DecryptedHeader decryptedHeader = FFI.decryptHeader(userDecryptionKey, encryptedHeader_);
193+
DecryptedHeader decryptedHeader = Ffi.decryptHeader(userDecryptionKey, encryptedHeader_);
180194

181195
// decrypt the content, passing the unique id and block number
182-
byte[] data_ = FFI.decryptBlock(decryptedHeader.getSymmetricKey(), uid, 0, encryptedContent);
196+
byte[] data_ = Ffi.decryptBlock(decryptedHeader.getSymmetricKey(), uid, 0, encryptedContent);
183197

184198
// Verify everything is correct
185199
assertTrue(Arrays.equals(data, data_));
186200
```
187201

202+
#### Locally cached encryption and decryption
203+
204+
When doing multiple encryptions/decryptions in a row using the same key, a significant speed increase can be achieved using a local cache of the keys (10x on encryption, 2.5x on decryption).
205+
206+
##### encryption
207+
208+
```java
209+
210+
PublicKey publicKey = PublicKey.fromJson(publicKeyJson);
211+
// Create a cache of the Public Key and Policy
212+
LocalEncryptionCache cache = Ffi.createEncryptionCache(publicKey);
213+
214+
// process multiple messages in a loop
215+
try {
216+
for (...) {
217+
byte[] clearText = ...;
218+
// encrypt the hybrid header using the cache
219+
Attr[] attributes = ...;
220+
EncryptedHeader encryptedHeader = Ffi.encryptHeaderUsingCache(cache, attributes);
221+
// encrypt the block of bytes as before
222+
byte[] encryptedBlock = Ffi.encryptBlock(encryptedHeader.getSymmetricKey(), uid, 0, clearText);
223+
}
224+
finally {
225+
// The cache MUST be destroyed to reclaim memory
226+
Ffi.destroyEncryptionCache(cache);
227+
}
228+
229+
```
230+
231+
##### decryption
232+
233+
```java
234+
PrivateKey decryptionKey = ...;
235+
// Create a cache of the Decryption Key
236+
LocalDecryptionCache cache = Ffi.createDecryptionCache(decryptionKey);
237+
238+
// process multiple cipher texts in a loop
239+
try {
240+
for (...) {
241+
byte[] encryptedHeader = ...;
242+
byte[] encryptedContent = ...;
243+
// decrypt the hybrid header using the cache
244+
DecryptedHeader decryptedHeader = Ffi.decryptHeaderUsingCache(cache, encryptedHeader);
245+
// decrypt the block of bytes as before
246+
byte[] clearText = Ffi.decryptBlock(decryptedHeader.getSymmetricKey(), uid, 0, encryptedContent);
247+
}
248+
finally {
249+
// The cache MUST be destroyed to reclaim memory
250+
Ffi.destroyDecryptionCache(cache);
251+
}
252+
253+
```
188254

189255
## Secure Computations
190256

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>com.cosmian</groupId>
55
<artifactId>cosmian_java_lib</artifactId>
6-
<version>0.5.3-SNAPSHOT</version>
6+
<version>0.6.0</version>
77

88
<name>cosmian_java_lib</name>
99
<description>The Cosmian Java Lib that provides local encryption/decyption and access to the Cosmian public platform APIs</description>

0 commit comments

Comments
 (0)