@Test public void openUsingSharedkeyThrowsExceptionOnFailure() { doReturn(-1) .when(this.sodium) .crypto_box_curve25519xsalsa20poly1305_open_afternm( any(byte[].class), any(byte[].class), anyInt(), any(byte[].class), eq(sharedKey.getKeyBytes())); final Throwable kaclEx = catchThrowable(() -> this.kalium.openAfterPrecomputation(message, nonce, sharedKey)); assertThat(kaclEx) .isInstanceOf(EncryptorException.class) .hasMessage("Kalium could not open the payload using the shared key"); verify(this.sodium) .crypto_box_curve25519xsalsa20poly1305_open_afternm( any(byte[].class), any(byte[].class), anyInt(), any(byte[].class), any(byte[].class)); }
@Override public byte[] openAfterPrecomputation(final byte[] encryptedPayload, final Nonce nonce, final SharedKey sharedKey) { LOGGER.info("Opening message using shared key {}", sharedKey); LOGGER.debug( "Opening message using nonce {} and shared key {}", nonce, sharedKey); final byte[] paddedInput = pad(encryptedPayload, CRYPTO_BOX_CURVE25519XSALSA20POLY1305_BOXZEROBYTES); final byte[] paddedOutput = new byte[paddedInput.length]; final int sodiumResult = this.sodium.crypto_box_curve25519xsalsa20poly1305_open_afternm( paddedOutput, paddedInput, paddedInput.length, nonce.getNonceBytes(), sharedKey.getKeyBytes()); if (sodiumResult == -1) { LOGGER.error("Could not open sealed payload using shared key {}", sharedKey); throw new EncryptorException("Kalium could not open the payload using the shared key"); } LOGGER.info("Opened sealed payload for shared key {}", sharedKey); LOGGER.debug( "Opened payload using nonce {} and sharedKey {}", nonce, sharedKey); return extract(paddedOutput, CRYPTO_BOX_CURVE25519XSALSA20POLY1305_ZEROBYTES); }
Kalium implements Encryptor { @Override public byte[] openAfterPrecomputation(final byte[] encryptedPayload, final Nonce nonce, final SharedKey sharedKey) { LOGGER.info("Opening message using shared key {}", sharedKey); LOGGER.debug( "Opening message using nonce {} and shared key {}", nonce, sharedKey); final byte[] paddedInput = pad(encryptedPayload, CRYPTO_BOX_CURVE25519XSALSA20POLY1305_BOXZEROBYTES); final byte[] paddedOutput = new byte[paddedInput.length]; final int sodiumResult = this.sodium.crypto_box_curve25519xsalsa20poly1305_open_afternm( paddedOutput, paddedInput, paddedInput.length, nonce.getNonceBytes(), sharedKey.getKeyBytes()); if (sodiumResult == -1) { LOGGER.error("Could not open sealed payload using shared key {}", sharedKey); throw new EncryptorException("Kalium could not open the payload using the shared key"); } LOGGER.info("Opened sealed payload for shared key {}", sharedKey); LOGGER.debug( "Opened payload using nonce {} and sharedKey {}", nonce, sharedKey); return extract(paddedOutput, CRYPTO_BOX_CURVE25519XSALSA20POLY1305_ZEROBYTES); } }
Kalium implements Encryptor { @Override public byte[] openAfterPrecomputation(final byte[] encryptedPayload, final Nonce nonce, final SharedKey sharedKey) { LOGGER.info("Opening message using shared key {}", sharedKey); LOGGER.debug( "Opening message using nonce {} and shared key {}", nonce, sharedKey); final byte[] paddedInput = pad(encryptedPayload, CRYPTO_BOX_CURVE25519XSALSA20POLY1305_BOXZEROBYTES); final byte[] paddedOutput = new byte[paddedInput.length]; final int sodiumResult = this.sodium.crypto_box_curve25519xsalsa20poly1305_open_afternm( paddedOutput, paddedInput, paddedInput.length, nonce.getNonceBytes(), sharedKey.getKeyBytes()); if (sodiumResult == -1) { LOGGER.error("Could not open sealed payload using shared key {}", sharedKey); throw new EncryptorException("Kalium could not open the payload using the shared key"); } LOGGER.info("Opened sealed payload for shared key {}", sharedKey); LOGGER.debug( "Opened payload using nonce {} and sharedKey {}", nonce, sharedKey); return extract(paddedOutput, CRYPTO_BOX_CURVE25519XSALSA20POLY1305_ZEROBYTES); } Kalium(final NaCl.Sodium sodium); }
Kalium implements Encryptor { @Override public byte[] openAfterPrecomputation(final byte[] encryptedPayload, final Nonce nonce, final SharedKey sharedKey) { LOGGER.info("Opening message using shared key {}", sharedKey); LOGGER.debug( "Opening message using nonce {} and shared key {}", nonce, sharedKey); final byte[] paddedInput = pad(encryptedPayload, CRYPTO_BOX_CURVE25519XSALSA20POLY1305_BOXZEROBYTES); final byte[] paddedOutput = new byte[paddedInput.length]; final int sodiumResult = this.sodium.crypto_box_curve25519xsalsa20poly1305_open_afternm( paddedOutput, paddedInput, paddedInput.length, nonce.getNonceBytes(), sharedKey.getKeyBytes()); if (sodiumResult == -1) { LOGGER.error("Could not open sealed payload using shared key {}", sharedKey); throw new EncryptorException("Kalium could not open the payload using the shared key"); } LOGGER.info("Opened sealed payload for shared key {}", sharedKey); LOGGER.debug( "Opened payload using nonce {} and sharedKey {}", nonce, sharedKey); return extract(paddedOutput, CRYPTO_BOX_CURVE25519XSALSA20POLY1305_ZEROBYTES); } Kalium(final NaCl.Sodium sodium); @Override SharedKey computeSharedKey(final PublicKey publicKey, final PrivateKey privateKey); @Override byte[] seal(
            final byte[] message, final Nonce nonce, final PublicKey publicKey, final PrivateKey privateKey); @Override byte[] open(
            final byte[] cipherText, final Nonce nonce, final PublicKey publicKey, final PrivateKey privateKey); @Override byte[] sealAfterPrecomputation(final byte[] message, final Nonce nonce, final SharedKey sharedKey); @Override byte[] openAfterPrecomputation(final byte[] encryptedPayload, final Nonce nonce, final SharedKey sharedKey); @Override Nonce randomNonce(); @Override KeyPair generateNewKeys(); @Override SharedKey createSingleKey(); }
Kalium implements Encryptor { @Override public byte[] openAfterPrecomputation(final byte[] encryptedPayload, final Nonce nonce, final SharedKey sharedKey) { LOGGER.info("Opening message using shared key {}", sharedKey); LOGGER.debug( "Opening message using nonce {} and shared key {}", nonce, sharedKey); final byte[] paddedInput = pad(encryptedPayload, CRYPTO_BOX_CURVE25519XSALSA20POLY1305_BOXZEROBYTES); final byte[] paddedOutput = new byte[paddedInput.length]; final int sodiumResult = this.sodium.crypto_box_curve25519xsalsa20poly1305_open_afternm( paddedOutput, paddedInput, paddedInput.length, nonce.getNonceBytes(), sharedKey.getKeyBytes()); if (sodiumResult == -1) { LOGGER.error("Could not open sealed payload using shared key {}", sharedKey); throw new EncryptorException("Kalium could not open the payload using the shared key"); } LOGGER.info("Opened sealed payload for shared key {}", sharedKey); LOGGER.debug( "Opened payload using nonce {} and sharedKey {}", nonce, sharedKey); return extract(paddedOutput, CRYPTO_BOX_CURVE25519XSALSA20POLY1305_ZEROBYTES); } Kalium(final NaCl.Sodium sodium); @Override SharedKey computeSharedKey(final PublicKey publicKey, final PrivateKey privateKey); @Override byte[] seal(
            final byte[] message, final Nonce nonce, final PublicKey publicKey, final PrivateKey privateKey); @Override byte[] open(
            final byte[] cipherText, final Nonce nonce, final PublicKey publicKey, final PrivateKey privateKey); @Override byte[] sealAfterPrecomputation(final byte[] message, final Nonce nonce, final SharedKey sharedKey); @Override byte[] openAfterPrecomputation(final byte[] encryptedPayload, final Nonce nonce, final SharedKey sharedKey); @Override Nonce randomNonce(); @Override KeyPair generateNewKeys(); @Override SharedKey createSingleKey();  }