Skip to content

Commit bc23ef1

Browse files
authored
Merge pull request #5 from Cosmian/ffi_speed_thread_safe
Ffi speed thread safe
2 parents a734017 + 8665735 commit bc23ef1

9 files changed

Lines changed: 162 additions & 104 deletions

File tree

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
target/
22
dependency-reduced-pom.xml
3-
hs_err_*.log
3+
hs_err_*.log
4+
.bloop/
5+
project/

.vscode/settings.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,8 @@
77
"Kmip",
88
"serializers"
99
],
10-
"java.format.settings.url": ".vscode/eclipse-codestyle.xml"
10+
"java.format.settings.url": ".vscode/eclipse-codestyle.xml",
11+
"files.watcherExclude": {
12+
"**/target": true
13+
}
1114
}

README.md

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

@@ -58,7 +58,8 @@ KMS Server | Java Lib | abe_gpsw lib
5858
-----------|----------|--------------
5959
1.2.0 | 0.5.0 | 0.3.0
6060
1.2.1 | 0.5.2 | 0.4.0
61-
1.2.1 | 0.6.0 | 0.6.0
61+
1.2.1 | ~~0.6.0~~| ~~0.6.0~~ (yanked)
62+
1.2.1 | 0.6.1 | 0.6.1
6263

6364

6465
### Quick Start ABE+AES
@@ -92,7 +93,7 @@ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
9293
```sh
9394
git clone https://github.com/Cosmian/abe_gpsw.git && \
9495
cd abe_gpsw && \
95-
git checkout v0.6.0
96+
git checkout v0.6.1
9697
```
9798

9899
3. Build the native library, which will be available as `libabe_gpsw.so` (linux) or `libabe_gpsw.dylib` (macos) in the `target` directory

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.6.0</version>
6+
<version>0.6.1</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>

src/main/java/com/cosmian/jna/Ffi.java

Lines changed: 67 additions & 61 deletions
Large diffs are not rendered by default.

src/main/java/com/cosmian/jna/LocalDecryptionCache.java

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/main/java/com/cosmian/jna/LocalEncryptionCache.java

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/main/java/com/cosmian/jna/abe/FfiWrapper.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
package com.cosmian.jna.abe;
22

3-
import com.sun.jna.Native;
43
import com.sun.jna.Library;
54
import com.sun.jna.ptr.IntByReference;
6-
import com.sun.jna.ptr.PointerByReference;
75
import com.sun.jna.Pointer;
86

97
/**
108
* This maps the hybrid_gpsw-aes.rs functions in the abe_gpsw Rust library
119
*/
1210
public interface FfiWrapper extends Library {
1311

14-
FfiWrapper INSTANCE = (FfiWrapper) Native.load("abe_gpsw", FfiWrapper.class);
12+
1513

1614
int set_error(String errorMsg);
1715

@@ -38,23 +36,23 @@ int h_aes_decrypt_block(byte[] clearText, IntByReference clearTextSize, Pointer
3836
int symmetricKeyLength, Pointer uidPointer, int uidLen, int blockNumber,
3937
Pointer clearTextPointer, int clearTextLength);
4038

41-
int h_aes_create_encryption_cache(PointerByReference cachePtrPtr, String policyJson, Pointer publicKeyPointer,
39+
int h_aes_create_encryption_cache(IntByReference cacheHandle, String policyJson, Pointer publicKeyPointer,
4240
int publicKeyLength);
4341

44-
int h_aes_destroy_encryption_cache(PointerByReference cachePtrPtr);
42+
int h_aes_destroy_encryption_cache(int cacheHandle);
4543

4644
int h_aes_encrypt_header_using_cache(byte[] symmetricKey, IntByReference symmetricKeySize, byte[] headerBytes,
47-
IntByReference headerBytesSize, PointerByReference cachePtrPtr,
45+
IntByReference headerBytesSize, int cacheHandle,
4846
String attributesJson, Pointer uidPointer, int uidLen, Pointer additionalDataPointer,
4947
int additionalDataLength);
5048

51-
int h_aes_create_decryption_cache(PointerByReference cachePtrPtr, Pointer userDecryptionKeyPointer,
49+
int h_aes_create_decryption_cache(IntByReference cacheHandle, Pointer userDecryptionKeyPointer,
5250
int userDecryptionKeyLength);
5351

54-
int h_aes_destroy_decryption_cache(PointerByReference cachePtrPtr);
52+
int h_aes_destroy_decryption_cache(int cacheHandle);
5553

5654
int h_aes_decrypt_header_using_cache(byte[] symmetricKey, IntByReference symmetricKeySize, byte[] uidPointer,
5755
IntByReference uidLen, byte[] additionalDataPointer, IntByReference additionalDataLength,
58-
Pointer encryptedHeaderBytes, int encryptedHeaderBytesSize, PointerByReference cachePtrPtr);
56+
Pointer encryptedHeaderBytes, int encryptedHeaderBytesSize, int cacheHandle);
5957

6058
}

src/test/java/com/cosmian/TestABE_AES.java

Lines changed: 76 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212
import java.security.NoSuchAlgorithmException;
1313
import java.util.Arrays;
1414
import java.util.Optional;
15+
import java.util.concurrent.ExecutorService;
16+
import java.util.concurrent.Executors;
17+
import java.util.concurrent.TimeUnit;
1518

1619
import com.cosmian.jna.Ffi;
17-
import com.cosmian.jna.LocalDecryptionCache;
18-
import com.cosmian.jna.LocalEncryptionCache;
1920
import com.cosmian.jna.abe.DecryptedHeader;
2021
import com.cosmian.jna.abe.EncryptedHeader;
2122
import com.cosmian.rest.abe.Abe;
@@ -487,29 +488,29 @@ public void testHybridEncryptionDecryptionUsingCacheLocal() throws Exception {
487488

488489
String publicKeyJson = Resources.load_resource("ffi/public_master_key.json");
489490
PublicKey publicKey = PublicKey.fromJson(publicKeyJson);
490-
LocalEncryptionCache encryptionCachePointer = Ffi.createEncryptionCache(publicKey);
491+
int encryptionCacheHandle = Ffi.createEncryptionCache(publicKey);
491492
Attr[] attributes = new Attr[] { new Attr("Department", "FIN"),
492493
new Attr("Security Level", "Confidential") };
493494
byte[] uid = new byte[] { 1, 2, 3, 4, 5 };
494495
byte[] additional_data = new byte[] { 6, 7, 8, 9, 10 };
495496

496-
EncryptedHeader encryptedHeader = Ffi.encryptHeaderUsingCache(encryptionCachePointer, attributes,
497+
EncryptedHeader encryptedHeader = Ffi.encryptHeaderUsingCache(encryptionCacheHandle, attributes,
497498
Optional.of(uid), Optional.of(additional_data));
498499

499-
Ffi.destroyEncryptionCache(encryptionCachePointer);
500+
Ffi.destroyEncryptionCache(encryptionCacheHandle);
500501

501502
// decrypt
502503

503504
String userDecryptionKeyJson = Resources.load_resource("ffi/fin_confidential_user_key.json");
504505
PrivateKey userDecryptionKey = PrivateKey.fromJson(userDecryptionKeyJson);
505506

506-
LocalDecryptionCache decryptionCachePointer = Ffi.createDecryptionCache(userDecryptionKey);
507+
int decryptionCacheHandle = Ffi.createDecryptionCache(userDecryptionKey);
507508

508509
DecryptedHeader decryptedHeader = Ffi.decryptHeaderUsingCache(
509-
decryptionCachePointer,
510+
decryptionCacheHandle,
510511
encryptedHeader.getEncryptedHeaderBytes(), 10, 10);
511512

512-
Ffi.destroyDecryptionCache(decryptionCachePointer);
513+
Ffi.destroyDecryptionCache(decryptionCacheHandle);
513514

514515
// assert
515516

@@ -543,4 +544,71 @@ public void testHybridEncryptionDecryptionNoCacheLocal() throws Exception {
543544
assertArrayEquals(additional_data, decryptedHeader.getAdditionalData());
544545
}
545546

547+
@Test
548+
public void testHybridEncryptionCacheSerialization() throws Exception {
549+
550+
String publicKeyJson = Resources.load_resource("ffi/public_master_key.json");
551+
PublicKey publicKey = PublicKey.fromJson(publicKeyJson);
552+
553+
// serialize encryption cache
554+
int e_cache = Ffi.createEncryptionCache(publicKey);
555+
556+
Attr[] attributes = new Attr[] { new Attr("Department", "FIN"), new Attr("Security Level", "Confidential") };
557+
byte[] uid = new byte[] { 1, 2, 3, 4, 5 };
558+
byte[] additional_data = new byte[] { 6, 7, 8, 9, 10 };
559+
560+
String userDecryptionKeyJson = Resources.load_resource("ffi/fin_confidential_user_key.json");
561+
PrivateKey userDecryptionKey = PrivateKey.fromJson(userDecryptionKeyJson);
562+
563+
// serialize decryption cache
564+
int d_cache = Ffi.createDecryptionCache(userDecryptionKey);
565+
566+
int threads = 4;
567+
ExecutorService executor = Executors.newFixedThreadPool(threads);
568+
for (int i = 0; i < threads; i++) {
569+
570+
executor.submit(() -> {
571+
572+
String threadName = Thread.currentThread().getName();
573+
// deserialize encryption cache
574+
EncryptedHeader encryptedHeader;
575+
DecryptedHeader decryptedHeader;
576+
try {
577+
578+
encryptedHeader = Ffi.encryptHeaderUsingCache(e_cache, attributes, Optional.of(uid),
579+
Optional.of(additional_data));
580+
decryptedHeader = Ffi.decryptHeaderUsingCache(d_cache, encryptedHeader.getEncryptedHeaderBytes(),
581+
10, 10);
582+
583+
assertArrayEquals(encryptedHeader.getSymmetricKey(), decryptedHeader.getSymmetricKey());
584+
assertArrayEquals(uid, decryptedHeader.getUid());
585+
assertArrayEquals(additional_data, decryptedHeader.getAdditionalData());
586+
587+
System.out.println("Thread name " + threadName + " OK");
588+
} catch (Throwable e) {
589+
e.printStackTrace();
590+
}
591+
592+
});
593+
}
594+
595+
try {
596+
System.out.println("attempt to shutdown executor");
597+
executor.shutdown();
598+
executor.awaitTermination(5, TimeUnit.SECONDS);
599+
} catch (InterruptedException e) {
600+
System.err.println("tasks interrupted");
601+
} finally {
602+
if (!executor.isTerminated()) {
603+
System.err.println("cancel non-finished tasks");
604+
}
605+
executor.shutdownNow();
606+
System.out.println("shutdown finished");
607+
}
608+
609+
Ffi.destroyEncryptionCache(e_cache);
610+
Ffi.destroyDecryptionCache(d_cache);
611+
612+
}
613+
546614
}

0 commit comments

Comments
 (0)