Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update AesCbcSecretKey Class #141

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions lib/src/impl_ffi/impl_ffi.aescbc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@

part of 'impl_ffi.dart';

Future<AesCbcSecretKey> aesCbc_importRawKey(List<int> keyData) async =>
_AesCbcSecretKey(_aesImportRawKey(keyData));
Future<AesCbcSecretKeyImpl> aesCbc_importRawKey(List<int> keyData) async =>
_AesCbcSecretKeyImpl(_aesImportRawKey(keyData));

Future<AesCbcSecretKey> aesCbc_importJsonWebKey(
Future<AesCbcSecretKeyImpl> aesCbc_importJsonWebKey(
Map<String, dynamic> jwk,
) async =>
_AesCbcSecretKey(_aesImportJwkKey(
_AesCbcSecretKeyImpl(_aesImportJwkKey(
jwk,
expectedJwkAlgSuffix: 'CBC',
));

Future<AesCbcSecretKey> aesCbc_generateKey(int length) async =>
_AesCbcSecretKey(_aesGenerateKey(length));
Future<AesCbcSecretKeyImpl> aesCbc_generateKey(int length) async =>
_AesCbcSecretKeyImpl(_aesGenerateKey(length));

Stream<Uint8List> _aesCbcEncryptOrDecrypt(
Uint8List key,
Expand Down Expand Up @@ -93,9 +93,30 @@ Stream<Uint8List> _aesCbcEncryptOrDecrypt(
});
}

class _AesCbcSecretKey implements AesCbcSecretKey {
final class _StaticAesCbcSecretKeyImpl implements StaticAesCbcSecretKeyImpl {
const _StaticAesCbcSecretKeyImpl();

@override
Future<AesCbcSecretKeyImpl> importRawKey(List<int> keyData) async {
// TODO: Move implementation into this method in a follow up PR
// TODO: Move implementation into this method in a follow up PR
return await aesCbc_importRawKey(keyData);
HamdaanAliQuatil marked this conversation as resolved.
Show resolved Hide resolved
}

@override
Future<AesCbcSecretKeyImpl> importJsonWebKey(Map<String, dynamic> jwk) async {
return await aesCbc_importJsonWebKey(jwk);
}

@override
Future<AesCbcSecretKeyImpl> generateKey(int length) async {
return await aesCbc_generateKey(length);
}
}

final class _AesCbcSecretKeyImpl extends AesCbcSecretKeyImpl {
final Uint8List _key;
_AesCbcSecretKey(this._key);
_AesCbcSecretKeyImpl(this._key);

@override
String toString() {
Expand Down
11 changes: 11 additions & 0 deletions lib/src/impl_ffi/impl_ffi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import 'package:webcrypto/src/third_party/boringssl/generated_bindings.dart';

import '../jsonwebkey.dart' show JsonWebKey;
import '../webcrypto/webcrypto.dart';
import '../impl_interface/impl_interface.dart';
import '../boringssl/lookup/lookup.dart' show ssl, ERR_GET_LIB, ERR_GET_REASON;

part 'impl_ffi.aescbc.dart';
Expand Down Expand Up @@ -67,3 +68,13 @@ class _KeyPair<S, T> implements KeyPair<S, T> {

_KeyPair({required this.privateKey, required this.publicKey});
}


const WebCryptoImpl webCryptImpl = _WebCryptoImpl();

final class _WebCryptoImpl implements WebCryptoImpl {
const _WebCryptoImpl();

@override
final aesCbcSecretKey = const _StaticAesCbcSecretKeyImpl();
}
30 changes: 30 additions & 0 deletions lib/src/impl_interface/impl_interface.aescbc.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

part of 'impl_interface.dart';

abstract interface class StaticAesCbcSecretKeyImpl {
Future<AesCbcSecretKeyImpl> importRawKey(List<int> keyData);
Future<AesCbcSecretKeyImpl> importJsonWebKey(Map<String, dynamic> jwk);
Future<AesCbcSecretKeyImpl> generateKey(int length);
}

abstract class AesCbcSecretKeyImpl {
HamdaanAliQuatil marked this conversation as resolved.
Show resolved Hide resolved
Future<Uint8List> encryptBytes(List<int> data, List<int> iv);
Future<Uint8List> decryptBytes(List<int> data, List<int> iv);
Stream<Uint8List> encryptStream(Stream<List<int>> data, List<int> iv);
Stream<Uint8List> decryptStream(Stream<List<int>> data, List<int> iv);
Future<Uint8List> exportRawKey();
Future<Map<String, dynamic>> exportJsonWebKey();
}
41 changes: 41 additions & 0 deletions lib/src/impl_interface/impl_interface.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

library impl_stub;
HamdaanAliQuatil marked this conversation as resolved.
Show resolved Hide resolved

import 'dart:typed_data';
import 'dart:async';


part 'impl_interface.aescbc.dart';

/// Interface to be provided by platform implementations.
///
/// A platform implementation of `package:webcrypto` must define a
/// constant `webCryptImpl` as follows:
/// ```dart
/// const WebCryptoImpl webCryptImpl = const _MyPlatformImplemetation();
/// ```
///
/// The only platform implementations are:
/// * `lib/src/impl_ffi/impl_ffi.dart`,
/// * `lib/src/impl_js/impl_js.dart`, and,
/// * `lib/src/impl_stub/impl_stub.dart`.
///
/// These interfaces are not public and should not be implemented
/// outside this package. Should platform implementations ever become
/// plugable these interfaces will be renamed.
abstract interface class WebCryptoImpl {
HamdaanAliQuatil marked this conversation as resolved.
Show resolved Hide resolved
StaticAesCbcSecretKeyImpl get aesCbcSecretKey;
}
35 changes: 27 additions & 8 deletions lib/src/impl_js/impl_js.aescbc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ part of 'impl_js.dart';

const _aesCbcAlgorithm = subtle.Algorithm(name: 'AES-CBC');

Future<AesCbcSecretKey> aesCbc_importRawKey(List<int> keyData) async {
return _AesCbcSecretKey(await _importKey(
Future<AesCbcSecretKeyImpl> aesCbc_importRawKey(List<int> keyData) async {
return _AesCbcSecretKeyImpl(await _importKey(
'raw',
keyData,
_aesCbcAlgorithm,
Expand All @@ -28,28 +28,47 @@ Future<AesCbcSecretKey> aesCbc_importRawKey(List<int> keyData) async {
));
}

Future<AesCbcSecretKey> aesCbc_importJsonWebKey(
Future<AesCbcSecretKeyImpl> aesCbc_importJsonWebKey(
Map<String, dynamic> jwk,
) async {
return _AesCbcSecretKey(await _importJsonWebKey(
return _AesCbcSecretKeyImpl(await _importJsonWebKey(
jwk,
_aesCbcAlgorithm,
_usagesEncryptDecrypt,
'secret',
));
}

Future<AesCbcSecretKey> aesCbc_generateKey(int length) async {
return _AesCbcSecretKey(await _generateKey(
Future<AesCbcSecretKeyImpl> aesCbc_generateKey(int length) async {
return _AesCbcSecretKeyImpl(await _generateKey(
_aesCbcAlgorithm.update(length: length),
_usagesEncryptDecrypt,
'secret',
));
}

class _AesCbcSecretKey implements AesCbcSecretKey {
final class _StaticAesCbcSecretKeyImpl implements StaticAesCbcSecretKeyImpl {
const _StaticAesCbcSecretKeyImpl();

@override
Future<AesCbcSecretKeyImpl> importRawKey(List<int> keyData) async {
return await aesCbc_importRawKey(keyData);
}

@override
Future<AesCbcSecretKeyImpl> importJsonWebKey(Map<String, dynamic> jwk) async {
return await aesCbc_importJsonWebKey(jwk);
}

@override
Future<AesCbcSecretKeyImpl> generateKey(int length) async {
return await aesCbc_generateKey(length);
}
}

final class _AesCbcSecretKeyImpl implements AesCbcSecretKeyImpl {
final subtle.JSCryptoKey _key;
_AesCbcSecretKey(this._key);
_AesCbcSecretKeyImpl(this._key);

@override
String toString() {
Expand Down
11 changes: 11 additions & 0 deletions lib/src/impl_js/impl_js.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ library impl_js;
import 'dart:async';
import 'dart:typed_data';

import 'package:webcrypto/src/impl_interface/impl_interface.dart';

import '../webcrypto/webcrypto.dart';
import '../crypto_subtle.dart' as subtle;

Expand Down Expand Up @@ -53,3 +55,12 @@ class _KeyPair<S, T> implements KeyPair<S, T> {

_KeyPair({required this.privateKey, required this.publicKey});
}

const WebCryptoImpl webCryptImpl = _WebCryptoImpl();

final class _WebCryptoImpl implements WebCryptoImpl {
const _WebCryptoImpl();

@override
final aesCbcSecretKey = const _StaticAesCbcSecretKeyImpl();
}
8 changes: 0 additions & 8 deletions lib/src/impl_stub.dart
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,6 @@ Future<AesCtrSecretKey> aesCtr_generateKey(int length) => throw _notImplemented;

//---------------------- AES-CBC

Future<AesCbcSecretKey> aesCbc_importRawKey(List<int> keyData) =>
throw _notImplemented;

Future<AesCbcSecretKey> aesCbc_importJsonWebKey(Map<String, dynamic> jwk) =>
throw _notImplemented;

Future<AesCbcSecretKey> aesCbc_generateKey(int length) => throw _notImplemented;

//---------------------- AES-GCM

Future<AesGcmSecretKey> aesGcm_importRawKey(List<int> keyData) =>
Expand Down
31 changes: 31 additions & 0 deletions lib/src/impl_stub/impl_stub.aescbc.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

part of 'impl_stub.dart';

final class _StaticAesCbcSecretKeyImpl implements StaticAesCbcSecretKeyImpl {
const _StaticAesCbcSecretKeyImpl();

@override
Future<AesCbcSecretKeyImpl> importRawKey(List<int> keyData) =>
throw UnimplementedError('Not implemented');

@override
Future<AesCbcSecretKeyImpl> importJsonWebKey(Map<String, dynamic> jwk) =>
throw UnimplementedError('Not implemented');

@override
Future<AesCbcSecretKeyImpl> generateKey(int length) =>
throw UnimplementedError('Not implemented');
}
28 changes: 28 additions & 0 deletions lib/src/impl_stub/impl_stub.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

library webcrypto.impl_stub;

import 'package:webcrypto/src/impl_interface/impl_interface.dart';

part 'impl_stub.aescbc.dart';

const WebCryptoImpl webCryptImpl = _WebCryptoImpl();

final class _WebCryptoImpl implements WebCryptoImpl {
const _WebCryptoImpl();

@override
final aesCbcSecretKey = const _StaticAesCbcSecretKeyImpl();
}
Loading
Loading