Skip to content

Commit cd5d77b

Browse files
committed
init
0 parents  commit cd5d77b

File tree

392 files changed

+86549
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

392 files changed

+86549
-0
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# https://dart.dev/guides/libraries/private-files
2+
# Created by `dart pub`
3+
.dart_tool/
4+
5+
# Avoid committing pubspec.lock for library packages; see
6+
# https://dart.dev/guides/libraries/private-files#pubspeclock.
7+
pubspec.lock
8+
.DS_Store

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "src/third_party/openssl"]
2+
path = src/third_party/openssl_repo
3+
url = https://github.com/openssl/openssl.git

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## 1.0.0
2+
3+
- Initial version.

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<!--
2+
This README describes the package. If you publish this package to pub.dev,
3+
this README's contents appear on the landing page for your package.
4+
5+
For information about how to write a good package README, see the guide for
6+
[writing package pages](https://dart.dev/tools/pub/writing-package-pages).
7+
8+
For general information about developing packages, see the Dart guide for
9+
[creating packages](https://dart.dev/guides/libraries/create-packages)
10+
and the Flutter guide for
11+
[developing packages and plugins](https://flutter.dev/to/develop-packages).
12+
-->
13+
14+
TODO: Put a short description of the package here that helps potential users
15+
know whether this package might be useful for them.
16+
17+
## Features
18+
19+
TODO: List what your package can do. Maybe include images, gifs, or videos.
20+
21+
## Getting started
22+
23+
TODO: List prerequisites and provide or point to information on how to
24+
start using the package.
25+
26+
## Usage
27+
28+
TODO: Include short and useful examples for package users. Add longer examples
29+
to `/example` folder.
30+
31+
```dart
32+
const like = 'sample';
33+
```
34+
35+
## Additional information
36+
37+
TODO: Tell users more about the package: where to find more information, how to
38+
contribute to the package, how to file issues, what response they can expect
39+
from the package authors, and more.

analysis_options.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# This file configures the static analysis results for your project (errors,
2+
# warnings, and lints).
3+
#
4+
# This enables the 'recommended' set of lints from `package:lints`.
5+
# This set helps identify many issues that may lead to problems when running
6+
# or consuming Dart code, and enforces writing Dart using a single, idiomatic
7+
# style and format.
8+
#
9+
# If you want a smaller set of lints you can change this to specify
10+
# 'package:lints/core.yaml'. These are just the most critical lints
11+
# (the recommended set includes the core lints).
12+
# The core lints are also what is used by pub.dev for scoring packages.
13+
14+
include: package:lints/recommended.yaml
15+
16+
# Uncomment the following section to specify additional rules.
17+
18+
# linter:
19+
# rules:
20+
# - camel_case_types
21+
22+
# analyzer:
23+
# exclude:
24+
# - path/to/excluded/files/**
25+
26+
# For more information about the core and recommended set of lints, see
27+
# https://dart.dev/go/core-lints
28+
29+
# For additional information about configuring this file, see
30+
# https://dart.dev/guides/language/analysis-options

hooks/build.dart

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import 'package:code_assets/code_assets.dart';
2+
import 'package:hooks/hooks.dart';
3+
import 'package:logging/logging.dart';
4+
import 'package:native_toolchain_c/native_toolchain_c.dart';
5+
6+
void main(List<String> args) async {
7+
await build(args, (input, output) async {
8+
if (input.config.buildCodeAssets == false) {
9+
// If code assets should not be built, skip the build.
10+
return;
11+
}
12+
13+
final cBuilder = CBuilder.library(
14+
name: 'openssl3',
15+
assetName: 'src/third_party/openssl3.g.dart',
16+
sources: ['third_party/openssl/openssl3.c'],
17+
defines: {
18+
if (input.config.code.targetOS == OS.windows)
19+
// Ensure symbols are exported in dll.
20+
'SQLITE_API': '__declspec(dllexport)',
21+
},
22+
);
23+
await cBuilder.run(
24+
input: input,
25+
output: output,
26+
logger: Logger('')
27+
..level = Level.ALL
28+
..onRecord.listen((record) => print(record.message)),
29+
);
30+
});
31+
}

lib/openssl_dart.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/// Support for doing something awesome.
2+
///
3+
/// More dartdocs go here.
4+
library;
5+
6+
export 'src/openssl_dart_base.dart';
7+
8+
// TODO: Export any libraries intended for clients of this package.

lib/src/openssl_dart_base.dart

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import 'dart:ffi' as ffi;
2+
import 'dart:typed_data';
3+
4+
import 'package:ffi/ffi.dart';
5+
6+
import 'third_party/openssl.g.dart' as openssl;
7+
8+
class OpenSSL {
9+
static final OpenSSL _instance = OpenSSL._();
10+
factory OpenSSL() => _instance;
11+
OpenSSL._();
12+
13+
Uint8List aesEncrypt(Uint8List plaintext, Uint8List key, Uint8List iv) {
14+
return _aes(plaintext, key, iv, encrypt: true);
15+
}
16+
17+
Uint8List aesDecrypt(Uint8List ciphertext, Uint8List key, Uint8List iv) {
18+
return _aes(ciphertext, key, iv, encrypt: false);
19+
}
20+
21+
Uint8List _aes(Uint8List input, Uint8List key, Uint8List iv, {required bool encrypt}) {
22+
if (key.length != 32) {
23+
throw ArgumentError.value(key.length, 'key', 'Key must be 32 bytes for AES-256-CBC');
24+
}
25+
26+
if (iv.length != 16) {
27+
throw ArgumentError.value(iv.length, 'iv', 'IV must be 16 bytes for AES-256-CBC');
28+
}
29+
30+
final ctx = openssl.EVP_CIPHER_CTX_new();
31+
if (ctx == ffi.nullptr) throw StateError('Failed to create EVP_CIPHER_CTX');
32+
33+
final inPtr = malloc<ffi.Uint8>(input.isEmpty ? 1 : input.length);
34+
final keyPtr = malloc<ffi.Uint8>(key.length);
35+
final ivPtr = malloc<ffi.Uint8>(iv.length);
36+
final outPtr = malloc<ffi.Uint8>(input.length + openssl.EVP_MAX_BLOCK_LENGTH);
37+
final outLenPtr = malloc<ffi.Int>();
38+
39+
try {
40+
inPtr.asTypedList(input.length).setAll(0, input);
41+
keyPtr.asTypedList(key.length).setAll(0, key);
42+
ivPtr.asTypedList(iv.length).setAll(0, iv);
43+
44+
final initResult = encrypt
45+
? openssl.EVP_EncryptInit_ex(
46+
ctx,
47+
openssl.EVP_aes_256_cbc(),
48+
ffi.nullptr,
49+
keyPtr.cast<ffi.UnsignedChar>(),
50+
ivPtr.cast<ffi.UnsignedChar>(),
51+
)
52+
: openssl.EVP_DecryptInit_ex(
53+
ctx,
54+
openssl.EVP_aes_256_cbc(),
55+
ffi.nullptr,
56+
keyPtr.cast<ffi.UnsignedChar>(),
57+
ivPtr.cast<ffi.UnsignedChar>(),
58+
);
59+
_checkResult(initResult, 'init');
60+
61+
final updateResult = encrypt
62+
? openssl.EVP_EncryptUpdate(
63+
ctx,
64+
outPtr.cast<ffi.UnsignedChar>(),
65+
outLenPtr,
66+
inPtr.cast<ffi.UnsignedChar>(),
67+
input.length,
68+
)
69+
: openssl.EVP_DecryptUpdate(
70+
ctx,
71+
outPtr.cast<ffi.UnsignedChar>(),
72+
outLenPtr,
73+
inPtr.cast<ffi.UnsignedChar>(),
74+
input.length,
75+
);
76+
_checkResult(updateResult, 'update');
77+
78+
var outLen = outLenPtr.value;
79+
80+
final finalResult = encrypt
81+
? openssl.EVP_EncryptFinal_ex(ctx, outPtr.elementAt(outLen).cast<ffi.UnsignedChar>(), outLenPtr)
82+
: openssl.EVP_DecryptFinal_ex(ctx, outPtr.elementAt(outLen).cast<ffi.UnsignedChar>(), outLenPtr);
83+
_checkResult(finalResult, 'final');
84+
85+
outLen += outLenPtr.value;
86+
87+
return Uint8List.fromList(outPtr.asTypedList(outLen));
88+
} finally {
89+
malloc.free(inPtr);
90+
malloc.free(keyPtr);
91+
malloc.free(ivPtr);
92+
malloc.free(outPtr);
93+
malloc.free(outLenPtr);
94+
openssl.EVP_CIPHER_CTX_free(ctx);
95+
}
96+
}
97+
98+
void _checkResult(int result, String step) {
99+
if (result != 1) throw StateError('OpenSSL AES $step step failed, result: $result');
100+
}
101+
}

lib/src/third_party/openssl.g.dart

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// AUTO GENERATED FILE, DO NOT EDIT.
2+
//
3+
// Generated by `package:ffigen`.
4+
// ignore_for_file: type=lint, unused_import
5+
import 'dart:ffi' as ffi;
6+
7+
@ffi.Native<
8+
ffi.Int Function(
9+
ffi.Pointer<evp_cipher_ctx_st>,
10+
ffi.Pointer<evp_cipher_st>,
11+
ffi.Pointer<engine_st>,
12+
ffi.Pointer<ffi.UnsignedChar>,
13+
ffi.Pointer<ffi.UnsignedChar>,
14+
)
15+
>()
16+
external int EVP_EncryptInit_ex(
17+
ffi.Pointer<evp_cipher_ctx_st> ctx,
18+
ffi.Pointer<evp_cipher_st> cipher,
19+
ffi.Pointer<engine_st> impl,
20+
ffi.Pointer<ffi.UnsignedChar> key,
21+
ffi.Pointer<ffi.UnsignedChar> iv,
22+
);
23+
24+
@ffi.Native<
25+
ffi.Int Function(
26+
ffi.Pointer<evp_cipher_ctx_st>,
27+
ffi.Pointer<ffi.UnsignedChar>,
28+
ffi.Pointer<ffi.Int>,
29+
ffi.Pointer<ffi.UnsignedChar>,
30+
ffi.Int,
31+
)
32+
>()
33+
external int EVP_EncryptUpdate(
34+
ffi.Pointer<evp_cipher_ctx_st> ctx,
35+
ffi.Pointer<ffi.UnsignedChar> out,
36+
ffi.Pointer<ffi.Int> outl,
37+
ffi.Pointer<ffi.UnsignedChar> in$,
38+
int inl,
39+
);
40+
41+
@ffi.Native<
42+
ffi.Int Function(
43+
ffi.Pointer<evp_cipher_ctx_st>,
44+
ffi.Pointer<ffi.UnsignedChar>,
45+
ffi.Pointer<ffi.Int>,
46+
)
47+
>()
48+
external int EVP_EncryptFinal_ex(
49+
ffi.Pointer<evp_cipher_ctx_st> ctx,
50+
ffi.Pointer<ffi.UnsignedChar> out,
51+
ffi.Pointer<ffi.Int> outl,
52+
);
53+
54+
@ffi.Native<
55+
ffi.Int Function(
56+
ffi.Pointer<evp_cipher_ctx_st>,
57+
ffi.Pointer<evp_cipher_st>,
58+
ffi.Pointer<engine_st>,
59+
ffi.Pointer<ffi.UnsignedChar>,
60+
ffi.Pointer<ffi.UnsignedChar>,
61+
)
62+
>()
63+
external int EVP_DecryptInit_ex(
64+
ffi.Pointer<evp_cipher_ctx_st> ctx,
65+
ffi.Pointer<evp_cipher_st> cipher,
66+
ffi.Pointer<engine_st> impl,
67+
ffi.Pointer<ffi.UnsignedChar> key,
68+
ffi.Pointer<ffi.UnsignedChar> iv,
69+
);
70+
71+
@ffi.Native<
72+
ffi.Int Function(
73+
ffi.Pointer<evp_cipher_ctx_st>,
74+
ffi.Pointer<ffi.UnsignedChar>,
75+
ffi.Pointer<ffi.Int>,
76+
ffi.Pointer<ffi.UnsignedChar>,
77+
ffi.Int,
78+
)
79+
>()
80+
external int EVP_DecryptUpdate(
81+
ffi.Pointer<evp_cipher_ctx_st> ctx,
82+
ffi.Pointer<ffi.UnsignedChar> out,
83+
ffi.Pointer<ffi.Int> outl,
84+
ffi.Pointer<ffi.UnsignedChar> in$,
85+
int inl,
86+
);
87+
88+
@ffi.Native<
89+
ffi.Int Function(
90+
ffi.Pointer<evp_cipher_ctx_st>,
91+
ffi.Pointer<ffi.UnsignedChar>,
92+
ffi.Pointer<ffi.Int>,
93+
)
94+
>()
95+
external int EVP_DecryptFinal_ex(
96+
ffi.Pointer<evp_cipher_ctx_st> ctx,
97+
ffi.Pointer<ffi.UnsignedChar> outm,
98+
ffi.Pointer<ffi.Int> outl,
99+
);
100+
101+
@ffi.Native<ffi.Pointer<evp_cipher_ctx_st> Function()>()
102+
external ffi.Pointer<evp_cipher_ctx_st> EVP_CIPHER_CTX_new();
103+
104+
@ffi.Native<ffi.Void Function(ffi.Pointer<evp_cipher_ctx_st>)>()
105+
external void EVP_CIPHER_CTX_free(ffi.Pointer<evp_cipher_ctx_st> c);
106+
107+
@ffi.Native<ffi.Pointer<evp_cipher_st> Function()>()
108+
external ffi.Pointer<evp_cipher_st> EVP_aes_256_cbc();
109+
110+
final class evp_cipher_st extends ffi.Opaque {}
111+
112+
final class evp_cipher_ctx_st extends ffi.Opaque {}
113+
114+
final class engine_st extends ffi.Opaque {}
115+
116+
const int EVP_MAX_BLOCK_LENGTH = 32;

pubspec.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: openssl_dart
2+
description: A starting point for Dart libraries or applications.
3+
version: 1.0.0
4+
# repository: https://github.com/my_org/my_repo
5+
6+
environment:
7+
sdk: ^3.10.1
8+
9+
# Add regular dependencies here.
10+
dependencies:
11+
hooks: ^1.0.0
12+
code_assets: ^1.0.0
13+
native_toolchain_c: ^0.17.4
14+
logging: ^1.3.0
15+
ffi: ^2.1.5
16+
# path: ^1.9.0
17+
18+
dev_dependencies:
19+
ffigen: ^20.1.1
20+
lints: ^6.0.0
21+
test: ^1.29.0

0 commit comments

Comments
 (0)