Search in sources :

Example 1 with EncryptedBox

use of im.actor.core.modules.encryption.entity.EncryptedBox in project actor-platform by actorapp.

the class EncryptedMsgActor method onDecrypt.

public void onDecrypt(int uid, ApiEncryptedMessage message) {
    Log.d(TAG, "onDecrypt:" + uid);
    final long start = im.actor.runtime.Runtime.getActorTime();
    ArrayList<EncryptedBoxKey> encryptedBoxKeys = new ArrayList<EncryptedBoxKey>();
    for (ApiEncyptedBoxKey key : message.getBox().getKeys()) {
        if (key.getUsersId() == myUid()) {
            encryptedBoxKeys.add(new EncryptedBoxKey(key.getUsersId(), key.getKeyGroupId(), key.getAlgType(), key.getEncryptedKey()));
        }
    }
    final EncryptedBox encryptedBox = new EncryptedBox(encryptedBoxKeys.toArray(new EncryptedBoxKey[0]), message.getBox().getEncPackage());
// TODO: Implement
// ask(context().getEncryption().getEncryptedChatManager(uid), new EncryptedPeerActor.DecryptBox(encryptedBox), new AskCallback() {
// @Override
// public void onResult(Object obj) {
// Log.d(TAG, "onDecrypt:onResult in " + (Runtime.getActorTime() - start) + " ms");
// EncryptedPeerActor.DecryptBoxResponse re = (EncryptedPeerActor.DecryptBoxResponse) obj;
// try {
// ApiMessage message = ApiMessage.fromBytes(re.getData());
// Log.d(TAG, "onDecrypt:onResult " + message);
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// 
// @Override
// public void onError(Exception e) {
// Log.d(TAG, "onDecrypt:onError");
// e.printStackTrace();
// }
// });
}
Also used : EncryptedBoxKey(im.actor.core.modules.encryption.entity.EncryptedBoxKey) ArrayList(java.util.ArrayList) EncryptedBox(im.actor.core.modules.encryption.entity.EncryptedBox) ApiEncryptedBox(im.actor.core.api.ApiEncryptedBox) ApiEncyptedBoxKey(im.actor.core.api.ApiEncyptedBoxKey)

Example 2 with EncryptedBox

use of im.actor.core.modules.encryption.entity.EncryptedBox in project actor-platform by actorapp.

the class EncryptedPeerActor method doEncrypt.

private Promise<EncryptBoxResponse> doEncrypt(final byte[] data) {
    if (!isReady) {
        stash();
        return null;
    }
    // 
    // Stage 1: Loading User Key Groups
    // Stage 2: Pick sessions for encryption
    // Stage 3: Encrypt box_key int session
    // Stage 4: Encrypt box
    // 
    final byte[] encKey = Crypto.randomBytes(32);
    final byte[] encKeyExtended = keyPrf.calculate(encKey, "ActorPackage", 128);
    Log.d(TAG, "doEncrypt");
    final long start = Runtime.getActorTime();
    return PromisesArray.of(theirKeys.getUserKeysGroups()).filter(new Predicate<UserKeysGroup>() {

        @Override
        public boolean apply(UserKeysGroup keysGroup) {
            return !ignoredKeyGroups.contains(keysGroup.getKeyGroupId());
        }
    }).mapOptional(new Function<UserKeysGroup, Promise<SessionActor>>() {

        @Override
        public Promise<SessionActor> apply(final UserKeysGroup keysGroup) {
            if (activeSessions.containsKey(keysGroup.getKeyGroupId())) {
                return success(activeSessions.get(keysGroup.getKeyGroupId()).getSessions().get(0));
            }
            return context().getEncryption().getSessionManagerInt().pickSession(uid, keysGroup.getKeyGroupId()).failure(new Consumer<Exception>() {

                @Override
                public void apply(Exception e) {
                    ignoredKeyGroups.add(keysGroup.getKeyGroupId());
                }
            }).map(new Function<PeerSession, SessionActor>() {

                @Override
                public SessionActor apply(PeerSession src) {
                    return spawnSession(src);
                }
            });
        }
    }).mapOptional(encrypt(encKeyExtended)).zip().map(new Function<List<EncryptedSessionActor.EncryptedPackageRes>, EncryptBoxResponse>() {

        @Override
        public EncryptBoxResponse apply(List<EncryptedSessionActor.EncryptedPackageRes> src) {
            if (src.size() == 0) {
                throw new RuntimeException("No sessions available");
            }
            Log.d(TAG, "Keys Encrypted in " + (Runtime.getActorTime() - start) + " ms");
            ArrayList<EncryptedBoxKey> encryptedKeys = new ArrayList<>();
            for (EncryptedSessionActor.EncryptedPackageRes r : src) {
                Log.d(TAG, "Keys: " + r.getKeyGroupId());
                encryptedKeys.add(new EncryptedBoxKey(uid, r.getKeyGroupId(), "curve25519", r.getData()));
            }
            byte[] encData;
            try {
                encData = ActorBox.closeBox(ByteStrings.intToBytes(ownKeyGroupId), data, Crypto.randomBytes(32), new ActorBoxKey(encKeyExtended));
            } catch (IntegrityException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
            Log.d(TAG, "All Encrypted in " + (Runtime.getActorTime() - start) + " ms");
            return new EncryptBoxResponse(new EncryptedBox(encryptedKeys.toArray(new EncryptedBoxKey[encryptedKeys.size()]), ByteStrings.merge(ByteStrings.intToBytes(ownKeyGroupId), encData)));
        }
    });
}
Also used : ArrayList(java.util.ArrayList) IntegrityException(im.actor.runtime.crypto.IntegrityException) UserKeysGroup(im.actor.core.modules.encryption.entity.UserKeysGroup) IOException(java.io.IOException) IntegrityException(im.actor.runtime.crypto.IntegrityException) Function(im.actor.runtime.function.Function) Consumer(im.actor.runtime.function.Consumer) PeerSession(im.actor.core.entity.encryption.PeerSession) EncryptedBoxKey(im.actor.core.modules.encryption.entity.EncryptedBoxKey) EncryptedBox(im.actor.core.modules.encryption.entity.EncryptedBox) ArrayList(java.util.ArrayList) List(java.util.List) ActorBoxKey(im.actor.runtime.crypto.box.ActorBoxKey)

Aggregations

EncryptedBox (im.actor.core.modules.encryption.entity.EncryptedBox)2 EncryptedBoxKey (im.actor.core.modules.encryption.entity.EncryptedBoxKey)2 ArrayList (java.util.ArrayList)2 ApiEncryptedBox (im.actor.core.api.ApiEncryptedBox)1 ApiEncyptedBoxKey (im.actor.core.api.ApiEncyptedBoxKey)1 PeerSession (im.actor.core.entity.encryption.PeerSession)1 UserKeysGroup (im.actor.core.modules.encryption.entity.UserKeysGroup)1 IntegrityException (im.actor.runtime.crypto.IntegrityException)1 ActorBoxKey (im.actor.runtime.crypto.box.ActorBoxKey)1 Consumer (im.actor.runtime.function.Consumer)1 Function (im.actor.runtime.function.Function)1 IOException (java.io.IOException)1 List (java.util.List)1