Skip to content
This repository has been archived by the owner on May 30, 2022. It is now read-only.

Commit

Permalink
Merge pull request #3 from brevilo/fix-2
Browse files Browse the repository at this point in the history
Fix Session.decrypt()
  • Loading branch information
brevilo authored Nov 12, 2021
2 parents d06e292 + 98dbbbb commit 16a3721
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/main/java/io/github/brevilo/jolm/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,15 +195,21 @@ public Message encrypt(String plainText) throws OlmException {
*/
public String decrypt(Message message) throws OlmException {
// get native message
// (add a separate message buffer as olm_decrypt_max_plaintext_length() destroys it!)
Memory messageBuffer = Utils.toNative(message.getCipherText());
Memory messageBufferCopy = Utils.toNative(message.getCipherText());

// prepare output buffer and index reference
// determine output buffer length
NativeSize maxPlainTextLength =
OlmLibrary.olm_decrypt_max_plaintext_length(
instance, new NativeSize(message.type()), messageBuffer, new NativeSize(messageBuffer));
instance,
new NativeSize(message.type()),
messageBufferCopy,
new NativeSize(messageBufferCopy));

checkOlmResult(maxPlainTextLength);

// prepare output buffer
Memory plainTextBuffer = new Memory(maxPlainTextLength.longValue());

// call olm
Expand All @@ -218,7 +224,7 @@ public String decrypt(Message message) throws OlmException {

checkOlmResult(plainTextLength);

return Utils.fromNative(plainTextBuffer);
return Utils.fromNative(plainTextBuffer).substring(0, plainTextLength.intValue());
}

/**
Expand Down
90 changes: 90 additions & 0 deletions src/test/java/io/github/brevilo/jolm/SessionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright 2021 Oliver Behnke
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.github.brevilo.jolm;

import static org.junit.jupiter.api.Assertions.assertEquals;

import io.github.brevilo.jolm.model.Message;
import io.github.brevilo.jolm.model.OneTimeKeys;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;

@TestInstance(Lifecycle.PER_CLASS)
class SessionTest {
final String message = "5-BY-5!";

Account aliceAccount;
Session aliceSession;

Account bobAccount;
Session bobSession;

String bobIdentityKey;
String bobOneTimeKey;

@BeforeAll
void setUp() throws Exception {
aliceAccount = new Account();
bobAccount = new Account();
}

@AfterAll
void tearDown() throws Exception {
aliceAccount.clear();
aliceSession.clear();

bobAccount.clear();
bobSession.clear();
}

@Test
void testAliceToBob() throws Exception {
// generate bob's identity key
bobIdentityKey = bobAccount.identityKeys().getCurve25519();

// generate bob's one-time key
bobAccount.generateOneTimeKeys(1);
OneTimeKeys bobOneTimeKeys = bobAccount.oneTimeKeys();
bobOneTimeKeys
.getCurve25519()
.forEach(
(id, key) -> {
bobOneTimeKey = key;
});

// create alice's outbound olm Session (to bob)
aliceSession = Session.createOutboundSession(aliceAccount, bobIdentityKey, bobOneTimeKey);

// encrypt message for bob
Message encryptedMessage = aliceSession.encrypt(message);

// create bob's inbound olm session (from alice)
bobSession = Session.createInboundSession(bobAccount, encryptedMessage.getCipherText());

// decrypt message from alice
String decryptedMessage = bobSession.decrypt(encryptedMessage);

// compare message content
assertEquals(decryptedMessage, message);

// remove used one-time key
bobAccount.removeOneTimeKeys(bobSession);
}
}

0 comments on commit 16a3721

Please sign in to comment.