Skip to content

Commit

Permalink
feat(ton): Add support for crypto_box encryption used in TON Connect (
Browse files Browse the repository at this point in the history
#3964)

* feat(ton): Add support for `crypto_box` key-pair generation and message encryption

* Remove `test_nist256p1_sign_verify_ring` as no longer needed

* feat(ton): Add `crypto_box` Rust FFI functions

* feat(ton): Add C++ FFI interface

* feat(ton): Fix C++ interface

* Add Kotlin, Swift tests

* [CI] Trigger CI

* feat(ton): Fix rustfmt warnings

* feat(ton): Fix clippy warnings

* feat(ton): Fix C++ tests
  • Loading branch information
satoshiotomakan authored Aug 5, 2024
1 parent f96c6b7 commit a21d447
Show file tree
Hide file tree
Showing 29 changed files with 1,062 additions and 77 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.trustwallet.core.app.utils

import com.trustwallet.core.app.utils.toHexBytes
import com.trustwallet.core.app.utils.toHex
import org.junit.Assert.*
import org.junit.Test
import wallet.core.jni.*

class TestCryptoBox {
init {
System.loadLibrary("TrustWalletCore")
}

@Test
fun testEncryptDecryptEasy() {
val mySecret = CryptoBoxSecretKey()
val myPubkey = mySecret.publicKey

val otherSecret = CryptoBoxSecretKey()
val otherPubkey = otherSecret.publicKey

val message = "Well done is better than well said. -Benjamin Franklin"
val encrypted = CryptoBox.encryptEasy(mySecret, otherPubkey, message.toByteArray())

// Step 2. Make sure the Box can be decrypted by the other side.
val decrypted = CryptoBox.decryptEasy(otherSecret, myPubkey, encrypted)
assertEquals(decrypted.toString(Charsets.UTF_8), message)
}
}
40 changes: 40 additions & 0 deletions include/TrustWalletCore/TWCryptoBox.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// SPDX-License-Identifier: Apache-2.0
//
// Copyright © 2017 Trust Wallet.

#pragma once

#include "TWBase.h"
#include "TWCryptoBoxPublicKey.h"
#include "TWCryptoBoxSecretKey.h"
#include "TWData.h"
#include "TWString.h"

TW_EXTERN_C_BEGIN

/// `crypto_box` encryption algorithms.
TW_EXPORT_STRUCT
struct TWCryptoBox;

/// Encrypts message using `my_secret` and `other_pubkey`.
/// The output will have a randomly generated nonce prepended to it.
/// The output will be Overhead + 24 bytes longer than the original.
///
/// \param mySecret *non-null* pointer to my secret key.
/// \param otherPubkey *non-null* pointer to other's public key.
/// \param message *non-null* pointer to the message to be encrypted.
/// \return *nullable* pointer to the encrypted message with randomly generated nonce prepended to it.
TW_EXPORT_STATIC_METHOD
TWData* _Nonnull TWCryptoBoxEncryptEasy(struct TWCryptoBoxSecretKey* _Nonnull mySecret, struct TWCryptoBoxPublicKey* _Nonnull otherPubkey, TWData* _Nonnull message);

/// Decrypts box produced by `TWCryptoBoxEncryptEasy`.
/// We assume a 24-byte nonce is prepended to the encrypted text in box.
///
/// \param mySecret *non-null* pointer to my secret key.
/// \param otherPubkey *non-null* pointer to other's public key.
/// \param encrypted *non-null* pointer to the encrypted message with nonce prepended to it.
/// \return *nullable* pointer to the decrypted message.
TW_EXPORT_STATIC_METHOD
TWData* _Nullable TWCryptoBoxDecryptEasy(struct TWCryptoBoxSecretKey* _Nonnull mySecret, struct TWCryptoBoxPublicKey* _Nonnull otherPubkey, TWData* _Nonnull encrypted);

TW_EXTERN_C_END
45 changes: 45 additions & 0 deletions include/TrustWalletCore/TWCryptoBoxPublicKey.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// SPDX-License-Identifier: Apache-2.0
//
// Copyright © 2017 Trust Wallet.

#pragma once

#include "TWBase.h"
#include "TWData.h"
#include "TWString.h"

TW_EXTERN_C_BEGIN

/// Public key used in `crypto_box` cryptography.
TW_EXPORT_CLASS
struct TWCryptoBoxPublicKey;

/// Determines if the given public key is valid or not.
///
/// \param data *non-null* byte array.
/// \return true if the public key is valid, false otherwise.
TW_EXPORT_STATIC_METHOD
bool TWCryptoBoxPublicKeyIsValid(TWData* _Nonnull data);

/// Create a `crypto_box` public key with the given block of data.
///
/// \param data *non-null* byte array. Expected to have 32 bytes.
/// \note Should be deleted with \tw_crypto_box_public_key_delete.
/// \return Nullable pointer to Public Key.
TW_EXPORT_STATIC_METHOD
struct TWCryptoBoxPublicKey* _Nullable TWCryptoBoxPublicKeyCreateWithData(TWData* _Nonnull data);

/// Delete the given public key.
///
/// \param publicKey *non-null* pointer to public key.
TW_EXPORT_METHOD
void TWCryptoBoxPublicKeyDelete(struct TWCryptoBoxPublicKey* _Nonnull publicKey);

/// Returns the raw data of the given public-key.
///
/// \param publicKey *non-null* pointer to a public key.
/// \return C-compatible result with a C-compatible byte array.
TW_EXPORT_PROPERTY
TWData* _Nonnull TWCryptoBoxPublicKeyData(struct TWCryptoBoxPublicKey* _Nonnull publicKey);

TW_EXTERN_C_END
38 changes: 38 additions & 0 deletions include/TrustWalletCore/TWCryptoBoxSecretKey.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-License-Identifier: Apache-2.0
//
// Copyright © 2017 Trust Wallet.

#pragma once

#include "TWBase.h"
#include "TWCryptoBoxPublicKey.h"
#include "TWData.h"
#include "TWString.h"

TW_EXTERN_C_BEGIN

/// Secret key used in `crypto_box` cryptography.
TW_EXPORT_CLASS
struct TWCryptoBoxSecretKey;

/// Create a random secret key.
///
/// \note Should be deleted with \tw_crypto_box_secret_key_delete.
/// \return *non-null* pointer to Secret Key.
TW_EXPORT_STATIC_METHOD
struct TWCryptoBoxSecretKey* _Nonnull TWCryptoBoxSecretKeyCreate();

/// Delete the given secret `key`.
///
/// \param key *non-null* pointer to secret key.
TW_EXPORT_METHOD
void TWCryptoBoxSecretKeyDelete(struct TWCryptoBoxSecretKey* _Nonnull key);

/// Returns the public key associated with the given `key`.
///
/// \param key *non-null* pointer to the private key.
/// \return *non-null* pointer to the corresponding public key.
TW_EXPORT_METHOD
struct TWCryptoBoxPublicKey* _Nonnull TWCryptoBoxSecretKeyGetPublicKey(struct TWCryptoBoxSecretKey* _Nonnull key);

TW_EXTERN_C_END
134 changes: 97 additions & 37 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a21d447

Please sign in to comment.