Skip to content

Commit

Permalink
add askarKeyCryptoBoxOpen
Browse files Browse the repository at this point in the history
  • Loading branch information
ssferraz committed Feb 13, 2025
1 parent ec2c310 commit d4c73cc
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 35 deletions.
2 changes: 1 addition & 1 deletion example/lib/pages/execute_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class _ExecutePageState extends State<ExecutePage> {
}

closeSession() async {
print('CLOSE SESSION');
debugPrint('CLOSE SESSION');
if (session != null) {
await session!.close();
result = '$result SessionClose:\n';
Expand Down
8 changes: 4 additions & 4 deletions lib/askar/askar_native_functions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -341,16 +341,16 @@ final int Function(
typedef AskarKeyCryptoBoxOpenNative = Int32 Function(
NativeLocalKeyHandle recip_key,
NativeLocalKeyHandle sender_key,
Pointer<NativeByteBuffer> message,
Pointer<NativeByteBuffer> nonce,
NativeByteBuffer message,
NativeByteBuffer nonce,
Pointer<NativeSecretBuffer> out,
);

final int Function(
int recip_key,
int sender_key,
Pointer<NativeByteBuffer> message,
Pointer<NativeByteBuffer> nonce,
NativeByteBuffer message,
NativeByteBuffer nonce,
Pointer<NativeSecretBuffer> out,
) nativeAskarKeyCryptoBoxOpen = nativeLib
.lookup<NativeFunction<AskarKeyCryptoBoxOpenNative>>('askar_key_crypto_box_open')
Expand Down
40 changes: 28 additions & 12 deletions lib/askar/askar_wrapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -415,22 +415,38 @@ AskarResult<Uint8List> askarKeyCryptoBox(
}
}

ErrorCode askarKeyCryptoBoxOpen(
AskarResult<Uint8List> askarKeyCryptoBoxOpen(
LocalKeyHandle recipKey,
LocalKeyHandle senderKey,
Pointer<NativeByteBuffer> message,
Pointer<NativeByteBuffer> nonce,
Pointer<NativeSecretBuffer> out,
Uint8List message,
Uint8List nonce,
) {
final result = nativeAskarKeyCryptoBoxOpen(
recipKey.toInt(),
senderKey.toInt(),
message,
nonce,
out,
);
Pointer<NativeSecretBuffer> secretBufferPtr = calloc<NativeSecretBuffer>();
Pointer<NativeByteBuffer> messageByteBufferPtr = nullptr;
Pointer<NativeByteBuffer> nonceByteBufferPtr = nullptr;
try {
messageByteBufferPtr = bytesListToByteBuffer(message);
nonceByteBufferPtr = bytesListToByteBuffer(nonce);

return ErrorCode.fromInt(result);
final result = nativeAskarKeyCryptoBoxOpen(
recipKey.toInt(),
senderKey.toInt(),
messageByteBufferPtr.ref,
nonceByteBufferPtr.ref,
secretBufferPtr,
);

final errorCode = ErrorCode.fromInt(result);

final Uint8List value = (errorCode == ErrorCode.success)
? secretBufferToBytesList(secretBufferPtr.ref)
: Uint8List(0);
return AskarResult<Uint8List>(errorCode, value);
} finally {
freeSecretBufferPointer(secretBufferPtr);
freeByteBufferPointer(messageByteBufferPtr);
freeByteBufferPointer(nonceByteBufferPtr);
}
}

AskarResult<Uint8List> askarKeyCryptoBoxRandomNonce() {
Expand Down
39 changes: 23 additions & 16 deletions lib/askar/crypto/crypto_box.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,34 @@ class CryptoBox {
required Uint8List nonce,
}) {
try {
return askarKeyCryptoBox(recipientKey.handle, senderKey.handle, message, nonce)
.getValueOrException();
return askarKeyCryptoBox(
recipientKey.handle,
senderKey.handle,
message,
nonce,
).getValueOrException();
} catch (e) {
throw AskarKeyException('Error on crypto box: $e');
}
}

// TODO
// static Uint8List open({
// required AskarKey recipientKey,
// required AskarKey senderKey,
// required Uint8List message,
// required Uint8List nonce,
// }) {
// return askarKeyCryptoBoxOpen(
// nonce: nonce,
// message: message,
// senderKey: senderKey.handle,
// recipientKey: recipientKey.handle,
// ).getValueOrException();
// }
static Uint8List open({
required Key recipientKey,
required Key senderKey,
required Uint8List message,
required Uint8List nonce,
}) {
try {
return askarKeyCryptoBoxOpen(
recipientKey.handle,
senderKey.handle,
message,
nonce,
).getValueOrException();
} catch (e) {
throw AskarKeyException('Error on crypto box open: $e');
}
}

static Uint8List seal({
required Key recipientKey,
Expand Down
15 changes: 14 additions & 1 deletion test/askar_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,9 @@ void main() {

final message = utf8.encode("foobar");

keyCryptoBoxTest(recipKey, senderKey, message, nonce);
final messageEncrypted = keyCryptoBoxTest(recipKey, senderKey, message, nonce);

keyCryptoBoxOpenTest(recipKey, senderKey, messageEncrypted.value, nonce);

askarKeyFree(recipKey);
askarKeyFree(senderKey);
Expand Down Expand Up @@ -1372,6 +1374,17 @@ AskarResult<Uint8List> keyCryptoBoxTest(
return result;
}

AskarResult<Uint8List> keyCryptoBoxOpenTest(LocalKeyHandle recipKey,
LocalKeyHandle senderKey, Uint8List message, Uint8List nonce) {
final result = askarKeyCryptoBoxOpen(recipKey, senderKey, message, nonce);

printAskarResult('KeyCryptoBoxOpen', result);
expect(result.errorCode, ErrorCode.success);
expect(result.value.isNotEmpty, equals(true));

return result;
}

AskarResult<Uint8List> keyCryptoBoxSealTest(LocalKeyHandle handle, Uint8List message) {
final result = askarKeyCryptoBoxSeal(handle, message);

Expand Down
2 changes: 1 addition & 1 deletion todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@

- [x] askar_key_crypto_box: (args_0: Buffer, args_1: Buffer, args_2: Buffer, args_3: Buffer, args_4: Buffer) => number;

- [ ] askar_key_crypto_box_open: (args_0: Buffer, args_1: Buffer, args_2: Buffer, args_3: Buffer, args_4: Buffer) => number;
- [x] askar_key_crypto_box_open: (args_0: Buffer, args_1: Buffer, args_2: Buffer, args_3: Buffer, args_4: Buffer) => number;

- [x] askar_key_crypto_box_random_nonce: (args_0: Buffer) => number;

Expand Down

0 comments on commit d4c73cc

Please sign in to comment.