|
12 | 12 | import java.security.NoSuchAlgorithmException; |
13 | 13 | import java.util.Arrays; |
14 | 14 | import java.util.Optional; |
| 15 | +import java.util.concurrent.ExecutorService; |
| 16 | +import java.util.concurrent.Executors; |
| 17 | +import java.util.concurrent.TimeUnit; |
15 | 18 |
|
16 | 19 | import com.cosmian.jna.Ffi; |
17 | | -import com.cosmian.jna.LocalDecryptionCache; |
18 | | -import com.cosmian.jna.LocalEncryptionCache; |
19 | 20 | import com.cosmian.jna.abe.DecryptedHeader; |
20 | 21 | import com.cosmian.jna.abe.EncryptedHeader; |
21 | 22 | import com.cosmian.rest.abe.Abe; |
@@ -487,29 +488,29 @@ public void testHybridEncryptionDecryptionUsingCacheLocal() throws Exception { |
487 | 488 |
|
488 | 489 | String publicKeyJson = Resources.load_resource("ffi/public_master_key.json"); |
489 | 490 | PublicKey publicKey = PublicKey.fromJson(publicKeyJson); |
490 | | - LocalEncryptionCache encryptionCachePointer = Ffi.createEncryptionCache(publicKey); |
| 491 | + int encryptionCacheHandle = Ffi.createEncryptionCache(publicKey); |
491 | 492 | Attr[] attributes = new Attr[] { new Attr("Department", "FIN"), |
492 | 493 | new Attr("Security Level", "Confidential") }; |
493 | 494 | byte[] uid = new byte[] { 1, 2, 3, 4, 5 }; |
494 | 495 | byte[] additional_data = new byte[] { 6, 7, 8, 9, 10 }; |
495 | 496 |
|
496 | | - EncryptedHeader encryptedHeader = Ffi.encryptHeaderUsingCache(encryptionCachePointer, attributes, |
| 497 | + EncryptedHeader encryptedHeader = Ffi.encryptHeaderUsingCache(encryptionCacheHandle, attributes, |
497 | 498 | Optional.of(uid), Optional.of(additional_data)); |
498 | 499 |
|
499 | | - Ffi.destroyEncryptionCache(encryptionCachePointer); |
| 500 | + Ffi.destroyEncryptionCache(encryptionCacheHandle); |
500 | 501 |
|
501 | 502 | // decrypt |
502 | 503 |
|
503 | 504 | String userDecryptionKeyJson = Resources.load_resource("ffi/fin_confidential_user_key.json"); |
504 | 505 | PrivateKey userDecryptionKey = PrivateKey.fromJson(userDecryptionKeyJson); |
505 | 506 |
|
506 | | - LocalDecryptionCache decryptionCachePointer = Ffi.createDecryptionCache(userDecryptionKey); |
| 507 | + int decryptionCacheHandle = Ffi.createDecryptionCache(userDecryptionKey); |
507 | 508 |
|
508 | 509 | DecryptedHeader decryptedHeader = Ffi.decryptHeaderUsingCache( |
509 | | - decryptionCachePointer, |
| 510 | + decryptionCacheHandle, |
510 | 511 | encryptedHeader.getEncryptedHeaderBytes(), 10, 10); |
511 | 512 |
|
512 | | - Ffi.destroyDecryptionCache(decryptionCachePointer); |
| 513 | + Ffi.destroyDecryptionCache(decryptionCacheHandle); |
513 | 514 |
|
514 | 515 | // assert |
515 | 516 |
|
@@ -543,4 +544,71 @@ public void testHybridEncryptionDecryptionNoCacheLocal() throws Exception { |
543 | 544 | assertArrayEquals(additional_data, decryptedHeader.getAdditionalData()); |
544 | 545 | } |
545 | 546 |
|
| 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 | + |
546 | 614 | } |
0 commit comments