Skip to content

Commit 825e7bb

Browse files
committed
ajustes de metodos
2 parents b567f59 + 475e269 commit 825e7bb

13 files changed

+546
-138
lines changed

analysis_options.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ linter:
2424
# `// ignore_for_file: name_of_lint` syntax on the line or in the file
2525
# producing the lint.
2626
rules:
27-
# avoid_print: false # Uncomment to disable the `avoid_print` rule
27+
avoid_print: false
2828
# prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule
2929
# Additional information about this file can be found at
3030
# https://dart.dev/guides/language/analysis-options

lib/askar/askar_native_functions.dart

+10-10
Original file line numberDiff line numberDiff line change
@@ -401,10 +401,10 @@ typedef AskarKeyDeriveEcdh1puNative = Int32 Function(
401401
NativeLocalKeyHandle ephem_key,
402402
NativeLocalKeyHandle sender_key,
403403
NativeLocalKeyHandle recip_key,
404-
Pointer<NativeByteBuffer> alg_id,
405-
Pointer<NativeByteBuffer> apu,
406-
Pointer<NativeByteBuffer> apv,
407-
Pointer<NativeByteBuffer> cc_tag,
404+
NativeByteBuffer alg_id,
405+
NativeByteBuffer apu,
406+
NativeByteBuffer apv,
407+
NativeByteBuffer cc_tag,
408408
Int8 receive,
409409
Pointer<NativeLocalKeyHandle> out,
410410
);
@@ -414,10 +414,10 @@ final int Function(
414414
LocalKeyHandle ephem_key,
415415
LocalKeyHandle sender_key,
416416
LocalKeyHandle recip_key,
417-
Pointer<NativeByteBuffer> alg_id,
418-
Pointer<NativeByteBuffer> apu,
419-
Pointer<NativeByteBuffer> apv,
420-
Pointer<NativeByteBuffer> cc_tag,
417+
NativeByteBuffer alg_id,
418+
NativeByteBuffer apu,
419+
NativeByteBuffer apv,
420+
NativeByteBuffer cc_tag,
421421
int receive,
422422
Pointer<NativeLocalKeyHandle> out,
423423
) nativeAskarKeyDeriveEcdh1pu = nativeLib
@@ -576,13 +576,13 @@ final int Function(
576576

577577
typedef AskarKeyFromPublicBytesNative = Int32 Function(
578578
Pointer<Utf8> alg,
579-
Pointer<NativeByteBuffer> public_,
579+
NativeByteBuffer public_,
580580
Pointer<NativeLocalKeyHandle> out,
581581
);
582582

583583
final int Function(
584584
Pointer<Utf8> alg,
585-
Pointer<NativeByteBuffer> public_,
585+
NativeByteBuffer public_,
586586
Pointer<NativeLocalKeyHandle> out,
587587
) nativeAskarKeyFromPublicBytes = nativeLib
588588
.lookup<NativeFunction<AskarKeyFromPublicBytesNative>>('askar_key_from_public_bytes')

lib/askar/askar_utils.dart

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ Pointer<NativeByteBuffer> stringToByteBuffer(String value) {
1111
return bytesListToByteBuffer(utf8.encode(value));
1212
}
1313

14-
Pointer<NativeByteBuffer> bytesListToByteBuffer(Uint8List bytesList) {
14+
Pointer<NativeByteBuffer> bytesListToByteBuffer(Uint8List? bytesList) {
15+
if (bytesList == null) {
16+
return calloc<NativeByteBuffer>();
17+
}
18+
1519
Pointer<Uint8> dataPointer = calloc<Uint8>(bytesList.length);
1620

1721
for (int i = 0; i < bytesList.length; i++) {

lib/askar/askar_wrapper.dart

+86-51
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import 'package:import_so_libaskar/askar/enums/askar_key_algorithm.dart';
1212
import 'package:import_so_libaskar/askar/enums/askar_key_backend.dart';
1313
import 'package:import_so_libaskar/askar/enums/askar_signature_algorithm.dart';
1414
import 'package:import_so_libaskar/askar/enums/askar_store_key_method.dart';
15+
import 'package:import_so_libaskar/askar/exceptions/exceptions.dart';
1516

1617
import 'askar_native_functions.dart';
1718
import 'askar_utils.dart';
@@ -26,6 +27,13 @@ final class AskarResult<T> {
2627
String toString() {
2728
return "AskarResult($errorCode, $value)";
2829
}
30+
31+
T getValueOrException() {
32+
if (errorCode == ErrorCode.success) {
33+
return value;
34+
}
35+
throw AskarErrorCodeException(errorCode);
36+
}
2937
}
3038

3139
typedef LocalKeyHandle = int;
@@ -211,8 +219,8 @@ AskarResult<String> askarStringListGetItem(StringListHandle handle, int index) {
211219
}
212220

213221
AskarResult<Uint8List> askarKeyAeadDecrypt(
214-
LocalKeyHandle handle, Uint8List ciphertext, Uint8List nonce, Uint8List tag,
215-
{Uint8List? aad}) {
222+
LocalKeyHandle handle, Uint8List ciphertext, Uint8List nonce,
223+
{Uint8List? tag, Uint8List? aad}) {
216224
aad ??= Uint8List(0);
217225

218226
Pointer<NativeByteBuffer> ciphertextPtr = bytesListToByteBuffer(ciphertext);
@@ -421,36 +429,54 @@ ErrorCode askarKeyCryptoBoxSealOpen(
421429
return ErrorCode.fromInt(result);
422430
}
423431

424-
ErrorCode askarKeyDeriveEcdh1pu(
425-
String alg,
426-
LocalKeyHandle ephemKey,
432+
AskarResult<LocalKeyHandle> askarKeyDeriveEcdh1pu(
433+
KeyAlgorithm algorithm,
434+
LocalKeyHandle ephemeralKey,
427435
LocalKeyHandle senderKey,
428-
LocalKeyHandle recipKey,
429-
Pointer<NativeByteBuffer> algId,
430-
Pointer<NativeByteBuffer> apu,
431-
Pointer<NativeByteBuffer> apv,
432-
Pointer<NativeByteBuffer> ccTag,
433-
int receive,
434-
Pointer<NativeLocalKeyHandle> out,
436+
LocalKeyHandle recipientKey,
437+
Uint8List algId,
438+
Uint8List apu,
439+
Uint8List apv,
440+
Uint8List ccTag,
441+
bool receive,
435442
) {
436-
final algPointer = alg.toNativeUtf8();
443+
Pointer<NativeLocalKeyHandle> outPtr = calloc<NativeLocalKeyHandle>();
444+
445+
final algPointer = algorithm.value.toNativeUtf8();
446+
final algIdByteBufferPtr = bytesListToByteBuffer(algId);
447+
final apuByteBufferPtr = bytesListToByteBuffer(apu);
448+
final apvByteBufferPtr = bytesListToByteBuffer(apv);
449+
final ccTagByteBufferPtr = bytesListToByteBuffer(ccTag);
437450

438-
final result = nativeAskarKeyDeriveEcdh1pu(
451+
final funcResult = nativeAskarKeyDeriveEcdh1pu(
439452
algPointer,
440-
ephemKey,
453+
ephemeralKey,
441454
senderKey,
442-
recipKey,
443-
algId,
444-
apu,
445-
apv,
446-
ccTag,
447-
receive,
448-
out,
455+
recipientKey,
456+
algIdByteBufferPtr.ref,
457+
apuByteBufferPtr.ref,
458+
apvByteBufferPtr.ref,
459+
ccTagByteBufferPtr.ref,
460+
boolToInt(receive),
461+
outPtr,
449462
);
450463

464+
final errorCode = ErrorCode.fromInt(funcResult);
465+
466+
LocalKeyHandle value = (errorCode == ErrorCode.success ? outPtr.value : 0);
467+
468+
calloc.free(algIdByteBufferPtr.ref.data);
469+
calloc.free(algIdByteBufferPtr);
470+
calloc.free(apuByteBufferPtr.ref.data);
471+
calloc.free(apuByteBufferPtr);
472+
calloc.free(apvByteBufferPtr.ref.data);
473+
calloc.free(apvByteBufferPtr);
474+
calloc.free(ccTagByteBufferPtr.ref.data);
475+
calloc.free(ccTagByteBufferPtr);
451476
calloc.free(algPointer);
477+
calloc.free(outPtr);
452478

453-
return ErrorCode.fromInt(result);
479+
return AskarResult<LocalKeyHandle>(errorCode, value);
454480
}
455481

456482
AskarResult<LocalKeyHandle> askarKeyDeriveEcdhEs(
@@ -632,22 +658,31 @@ ErrorCode askarKeyFromKeyExchange(
632658
return ErrorCode.fromInt(result);
633659
}
634660

635-
ErrorCode askarKeyFromPublicBytes(
636-
String alg,
637-
Pointer<NativeByteBuffer> public_,
638-
Pointer<NativeLocalKeyHandle> out,
661+
AskarResult<LocalKeyHandle> askarKeyFromPublicBytes(
662+
KeyAlgorithm algorithm,
663+
Uint8List publicBytes,
639664
) {
640-
final algPointer = alg.toNativeUtf8();
665+
Pointer<IntPtr> localKeyHandlePtr = calloc<IntPtr>();
666+
667+
final algPointer = algorithm.value.toNativeUtf8();
668+
final byteBufferPointer = bytesListToByteBuffer(publicBytes);
641669

642-
final result = nativeAskarKeyFromPublicBytes(
670+
final funcResult = nativeAskarKeyFromPublicBytes(
643671
algPointer,
644-
public_,
645-
out,
672+
byteBufferPointer.ref,
673+
localKeyHandlePtr,
646674
);
647675

676+
final errorCode = ErrorCode.fromInt(funcResult);
677+
678+
final localKeyHandle = (errorCode == ErrorCode.success) ? localKeyHandlePtr.value : 0;
679+
648680
calloc.free(algPointer);
681+
calloc.free(localKeyHandlePtr);
682+
calloc.free(byteBufferPointer.ref.data);
683+
calloc.free(byteBufferPointer);
649684

650-
return ErrorCode.fromInt(result);
685+
return AskarResult<LocalKeyHandle>(errorCode, localKeyHandle);
651686
}
652687

653688
AskarResult<LocalKeyHandle> askarKeyFromSecretBytes(
@@ -804,16 +839,22 @@ AskarResult<String> askarKeyGetJwkThumbprint(
804839
return AskarResult<String>(errorCode, value);
805840
}
806841

807-
ErrorCode askarKeyGetPublicBytes(
808-
LocalKeyHandle handle,
809-
Pointer<NativeSecretBuffer> out,
810-
) {
811-
final result = nativeAskarKeyGetPublicBytes(
842+
AskarResult<Uint8List> askarKeyGetPublicBytes(LocalKeyHandle handle) {
843+
Pointer<NativeSecretBuffer> secretBufferPtr = calloc<NativeSecretBuffer>();
844+
845+
final funcResult = nativeAskarKeyGetPublicBytes(
812846
handle,
813-
out,
847+
secretBufferPtr,
814848
);
815849

816-
return ErrorCode.fromInt(result);
850+
final errorCode = ErrorCode.fromInt(funcResult);
851+
852+
final value = Uint8List.fromList(secretBufferToBytesList(secretBufferPtr.ref));
853+
854+
calloc.free(secretBufferPtr.ref.data);
855+
calloc.free(secretBufferPtr);
856+
857+
return AskarResult<Uint8List>(errorCode, value);
817858
}
818859

819860
AskarResult<Uint8List> askarKeyGetSecretBytes(LocalKeyHandle handle) {
@@ -867,12 +908,8 @@ AskarResult<Uint8List> askarKeySignMessage(
867908
}
868909

869910
AskarResult<LocalKeyHandle> askarKeyUnwrapKey(
870-
LocalKeyHandle handle,
871-
KeyAlgorithm algorithm,
872-
Uint8List ciphertext,
873-
Uint8List nonce,
874-
Uint8List tag,
875-
) {
911+
LocalKeyHandle handle, KeyAlgorithm algorithm, Uint8List ciphertext,
912+
{Uint8List? nonce, Uint8List? tag}) {
876913
Pointer<NativeLocalKeyHandle> out = calloc<NativeLocalKeyHandle>();
877914

878915
final algPtr = algorithm.value.toNativeUtf8();
@@ -941,14 +978,12 @@ AskarResult<bool> askarKeyVerifySignature(
941978
}
942979

943980
AskarResult<AskarEncryptedBuffer> askarKeyWrapKey(
944-
LocalKeyHandle handle,
945-
LocalKeyHandle other,
946-
Uint8List nonce,
947-
) {
948-
final byteBufferPointer = bytesListToByteBuffer(nonce);
949-
981+
LocalKeyHandle handle, LocalKeyHandle other,
982+
{Uint8List? nonce}) {
950983
Pointer<NativeEncryptedBuffer> encryptedBufferPtr = calloc<NativeEncryptedBuffer>();
951984

985+
final byteBufferPointer = bytesListToByteBuffer(nonce);
986+
952987
final errorCode = ErrorCode.fromInt(nativeAskarKeyWrapKey(
953988
handle,
954989
other,

lib/askar/crypto/askar_encrypted_buffer.dart

+3-1
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,7 @@ AskarEncryptedBuffer readNativeEncryptedBuffer(NativeEncryptedBuffer encryptedBu
4545
int tagPos = encryptedBuffer.tag_pos;
4646

4747
return AskarEncryptedBuffer(
48-
secretBufferToBytesList(encryptedBuffer.buffer), tagPos, noncePos);
48+
Uint8List.fromList(secretBufferToBytesList(encryptedBuffer.buffer)),
49+
tagPos,
50+
noncePos);
4951
}

lib/askar/crypto/askar_jwk.dart

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import 'dart:convert';
2+
import 'dart:typed_data';
3+
4+
class AskarJwk {
5+
final String kty;
6+
final String crv;
7+
final String x;
8+
final String? d;
9+
final String? y;
10+
11+
AskarJwk({required this.kty, required this.crv, required this.x, this.d, this.y});
12+
13+
factory AskarJwk.fromJson(Map<String, dynamic> json) {
14+
return AskarJwk(
15+
kty: json['kty'],
16+
crv: json['crv'],
17+
x: json['x'],
18+
d: json['d'],
19+
y: json['y'],
20+
);
21+
}
22+
23+
factory AskarJwk.fromString(String str) {
24+
return AskarJwk.fromJson(jsonDecode(str));
25+
}
26+
27+
Uint8List toUint8Array() {
28+
return Uint8List.fromList(utf8.encode(jsonEncode(toJson())));
29+
}
30+
31+
Map<String, dynamic> toJson() {
32+
return {
33+
'kty': kty,
34+
'crv': crv,
35+
'x': x,
36+
'd': d,
37+
'y': y,
38+
};
39+
}
40+
}

0 commit comments

Comments
 (0)