Skip to content

Commit

Permalink
Adds more tests for coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
dipu-bd committed Aug 17, 2024
1 parent 7c34242 commit 6336e4b
Show file tree
Hide file tree
Showing 36 changed files with 1,602 additions and 204 deletions.
17 changes: 13 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ jobs:
with:
sdk: ${{ matrix.sdk }}

- name: 'Install global packages'
run: |
dart pub global activate coverage
dart pub global activate cobertura
dart pub global activate junitreport
- name: Run tests with coverage
run: bash ./scripts/coverage.sh

Expand All @@ -32,8 +38,13 @@ jobs:
with:
token: ${{ secrets.CODECOV_TOKEN }}

- name: Upload test results to Codecov
if: ${{ !cancelled() }}
uses: codecov/test-results-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}

test-on-stable:
if: github.repository == 'bitanon/cipherlib'
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
Expand All @@ -59,10 +70,9 @@ jobs:
run: dart test

test-on-legacy:
if: github.repository == 'bitanon/cipherlib'
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
os: [ubuntu-latest, windows-latest]
sdk: ['2.14.0']
runs-on: ${{ matrix.os }}
steps:
Expand All @@ -76,7 +86,6 @@ jobs:
run: dart test

test-on-beta:
if: github.repository == 'bitanon/cipherlib'
strategy:
matrix:
os: [ubuntu-latest]
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# cipherlib

[![plugin version](https://img.shields.io/pub/v/cipherlib?label=pub)](https://pub.dev/packages/cipherlib)
[![test](https://github.com/bitanon/cipherlib/actions/workflows/test.yml/badge.svg?branch=master)](https://github.com/bitanon/cipherlib/actions/workflows/test.yml)
[![codecov](https://codecov.io/gh/bitanon/cipherlib/graph/badge.svg?token=ISIYJ8MNI0)](https://codecov.io/gh/bitanon/cipherlib)
[![plugin version](https://img.shields.io/pub/v/cipherlib?label=pub)](https://pub.dev/packages/cipherlib)
[![dart support](https://img.shields.io/badge/dart-%3e%3d%202.14.0-39f?logo=dart)](https://dart.dev/guides/whats-new#september-8-2021-214-release)
[![likes](https://img.shields.io/pub/likes/cipherlib?logo=dart)](https://pub.dev/packages/cipherlib/score)
[![pub points](https://img.shields.io/pub/points/cipherlib?logo=dart&color=teal)](https://pub.dev/packages/cipherlib/score)
[![popularity](https://img.shields.io/pub/popularity/cipherlib?logo=dart)](https://pub.dev/packages/cipherlib/score)
[![dart support](https://img.shields.io/badge/dart-%3e%3d%202.14.0-39f?logo=dart)](https://dart.dev/guides/whats-new#september-8-2021-214-release)

Implementations of cryptographic algorithms for encryption and decryption in Dart.

Expand Down
3 changes: 0 additions & 3 deletions lib/src/algorithms/aead_cipher.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,6 @@ class AEADCipherSink implements CipherSink, MACSinkBase {

@override
void init([List<int>? keypair]) {
if (keypair != null) {
_hasher.init(keypair);
}
if (_aad != null) {
_hasher.add(_aad!);
// pad with zero
Expand Down
4 changes: 2 additions & 2 deletions lib/src/algorithms/aes/cfb.dart
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class AESInCFBModeDecryptSink extends CipherSink {
/// Provides encryption for AES cipher in CFB mode.
class AESInCFBModeEncrypt extends SaltedCipher {
@override
String get name => "AES#encrypt/CFB";
String get name => "AES#encrypt/CFB/${Padding.none.name}";

/// Key for the cipher
final Uint8List key;
Expand All @@ -181,7 +181,7 @@ class AESInCFBModeEncrypt extends SaltedCipher {
/// Provides decryption for AES cipher in CFB mode.
class AESInCFBModeDecrypt extends SaltedCipher {
@override
String get name => "AES#decrypt/CFB";
String get name => "AES#decrypt/CFB/${Padding.none.name}";

/// Key for the cipher
final Uint8List key;
Expand Down
7 changes: 4 additions & 3 deletions lib/src/algorithms/aes/ctr.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class AESInCTRModeSink extends CipherSink {
/// Provides AES cipher in CTR mode.
class AESInCTRModeCipher extends SaltedCipher {
@override
final String name = "AES/CTR";
String get name => "AES#cipher/CTR/${Padding.none.name}";

/// Key for the cipher
final Uint8List key;
Expand Down Expand Up @@ -140,9 +140,10 @@ class AESInCTRMode extends SaltedCollateCipher {
}
var iv8 = iv is Uint8List ? iv : Uint8List.fromList(iv);
var key8 = key is Uint8List ? key : Uint8List.fromList(key);
var cipher = AESInCTRModeCipher(key8, iv8);
return AESInCTRMode._(
encryptor: AESInCTRModeCipher(key8, iv8),
decryptor: AESInCTRModeCipher(key8, iv8),
encryptor: cipher,
decryptor: cipher,
);
}

Expand Down
4 changes: 2 additions & 2 deletions lib/src/algorithms/aes/gcm.dart
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ class AESInGCMModeDecryptSink extends _AESInGCMModeSinkBase {
/// Provides AES cipher in GCM mode for encryption.
class AESInGCMModeEncrypt extends SaltedCipher {
@override
final String name = "AES#encrypt/GCM";
String get name => "AES#encrypt/GCM/${Padding.none.name}";

/// Key for the cipher
final Uint8List key;
Expand All @@ -425,7 +425,7 @@ class AESInGCMModeEncrypt extends SaltedCipher {
/// Provides AES cipher in GCM mode for decryption.
class AESInGCMModeDecrypt extends SaltedCipher {
@override
final String name = "AES#decrypt/GCM";
String get name => "AES#decrypt/GCM/${Padding.none.name}";

/// Key for the cipher
final Uint8List key;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/algorithms/aes/ofb.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class AESInOFBModeSink extends CipherSink {
/// Provides encryption for AES cipher in OFB mode.
class AESInOFBModeCipher extends SaltedCipher {
@override
final String name = "AES#cipher/OFB";
String get name => "AES#cipher/OFB/${Padding.none.name}";

/// Key for the cipher
final Uint8List key;
Expand Down
49 changes: 22 additions & 27 deletions lib/src/algorithms/aes/xts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import 'dart:typed_data';

import 'package:cipherlib/src/algorithms/padding.dart';
import 'package:cipherlib/src/core/cipher_sink.dart';
import 'package:cipherlib/src/core/salted_cipher.dart';
import 'package:cipherlib/src/utils/nonce.dart';
Expand Down Expand Up @@ -38,8 +39,8 @@ class AESInXTSModeEncryptSink extends CipherSink {

int _pos = 0;
int _rpos = 0;
int _msgLength = 0;
bool _closed = false;
bool _firstBlockAvailable = false;
final Uint8List _ekey;
final Uint8List _tkey;
final Uint8List _iv;
Expand All @@ -60,8 +61,8 @@ class AESInXTSModeEncryptSink extends CipherSink {
void reset() {
_pos = 0;
_rpos = 0;
_msgLength = 0;
_closed = false;
_firstBlockAvailable = false;
for (int i = 0; i < 16; ++i) {
_tweak[i] = _iv[i];
}
Expand All @@ -85,14 +86,14 @@ class AESInXTSModeEncryptSink extends CipherSink {

n = _pos + end - start;
if (!last) n -= 16;
n += _msgLength < 16 ? _rpos : 16;
n += _firstBlockAvailable ? 16 : _rpos;
if (!last) n -= (n & 15);
if (n < 0) n = 0;
var output = Uint8List(n);

p = 0;
for (i = start; i < end; ++i) {
if (_msgLength >= 16) {
if (_firstBlockAvailable) {
_block[_pos] = _residue[_rpos];
_pos++;
if (_pos == 16) {
Expand All @@ -108,17 +109,16 @@ class AESInXTSModeEncryptSink extends CipherSink {
}
}
_residue[_rpos++] = data[i];
_rpos &= 15;
_msgLength++;
if (_rpos == 16) {
_firstBlockAvailable = true;
_rpos = 0;
}
}

if (last) {
if (_msgLength < 16) {
if (!_firstBlockAvailable) {
throw StateError('The message length must be at least 16-bytes');
}
if (_pos != _rpos) {
throw StateError('Invalid residue length');
}
n = _pos;
for (j = n; j < 16; j++) {
_block[j] = _residue[j];
Expand Down Expand Up @@ -165,8 +165,6 @@ class AESInXTSModeEncryptSink extends CipherSink {

if (n == p) {
return output;
} else if (p == 0) {
return Uint8List(0);
} else {
return output.sublist(0, p);
}
Expand All @@ -188,8 +186,8 @@ class AESInXTSModeDecryptSink extends CipherSink {

int _pos = 0;
int _rpos = 0;
int _msgLength = 0;
bool _closed = false;
bool _firstBlockAvailable = false;
final Uint8List _dkey;
final Uint8List _tkey;
final Uint8List _iv;
Expand All @@ -211,8 +209,8 @@ class AESInXTSModeDecryptSink extends CipherSink {
void reset() {
_pos = 0;
_rpos = 0;
_msgLength = 0;
_closed = false;
_firstBlockAvailable = false;
for (int i = 0; i < 16; ++i) {
_tweak[i] = _iv[i];
}
Expand All @@ -236,14 +234,14 @@ class AESInXTSModeDecryptSink extends CipherSink {

n = _pos + end - start;
if (!last) n -= 16;
n += _msgLength < 16 ? _rpos : 16;
n += _firstBlockAvailable ? 16 : _rpos;
if (!last) n -= (n & 15);
if (n < 0) n = 0;
var output = Uint8List(n);

p = 0;
for (i = start; i < end; ++i) {
if (_msgLength >= 16) {
if (_firstBlockAvailable) {
_block[_pos] = _residue[_rpos];
_pos++;
if (_pos == 16) {
Expand All @@ -259,17 +257,16 @@ class AESInXTSModeDecryptSink extends CipherSink {
}
}
_residue[_rpos++] = data[i];
_rpos &= 15;
_msgLength++;
if (_rpos == 16) {
_firstBlockAvailable = true;
_rpos = 0;
}
}

if (last) {
if (_msgLength < 16) {
if (!_firstBlockAvailable) {
throw StateError('The message length must be at least 16-bytes');
}
if (_pos != _rpos) {
throw StateError('Invalid residue length');
}
n = _pos;
for (j = n; j < 16; j++) {
_block[j] = _residue[j];
Expand Down Expand Up @@ -316,8 +313,6 @@ class AESInXTSModeDecryptSink extends CipherSink {

if (n == p) {
return output;
} else if (p == 0) {
return Uint8List(0);
} else {
return output.sublist(0, p);
}
Expand All @@ -327,7 +322,7 @@ class AESInXTSModeDecryptSink extends CipherSink {
/// Provides encryption for AES cipher in XTS mode.
class AESInXTSModeEncrypt extends SaltedCipher {
@override
String get name => "AES#encrypt/XTS";
String get name => "AES#encrypt/XTS/${Padding.none.name}";

/// Key for the plaintext encryption
final Uint8List ekey;
Expand All @@ -350,7 +345,7 @@ class AESInXTSModeEncrypt extends SaltedCipher {
/// Provides decryption for AES cipher in XTS mode.
class AESInXTSModeDecrypt extends SaltedCipher {
@override
String get name => "AES#decrypt/XTS";
String get name => "AES#decrypt/XTS/${Padding.none.name}";

/// Key for the ciphertext decryption
final Uint8List ekey;
Expand All @@ -373,7 +368,7 @@ class AESInXTSModeDecrypt extends SaltedCipher {
/// Provides encryption and decryption for AES cipher in XTS mode.
class AESInXTSMode extends SaltedCollateCipher {
@override
String get name => "AES/XTS";
String get name => "AES/XTS/${Padding.none.name}";

@override
final AESInXTSModeEncrypt encryptor;
Expand Down
10 changes: 7 additions & 3 deletions lib/src/algorithms/chacha20.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ class ChaCha20Sink extends CipherSink {
if (_nonce.length != 8 && _nonce.length != 12) {
throw ArgumentError('The nonce should be either 8 or 12 bytes');
}
if (_counter.length != 4 && _counter.length != 8) {
throw ArgumentError('The counter should be either 4 or 8 bytes');
if (_nonce.length == 8 && _counter.length < 8) {
throw ArgumentError('The counter should be 8 bytes');
}
if (_nonce.length == 12 && _counter.length < 4) {
throw ArgumentError('The counter should be 4 bytes');
}
_counterSize = _nonce.length == 8 ? 8 : 4;
reset();
Expand Down Expand Up @@ -257,9 +260,10 @@ class ChaCha20 extends SaltedCipher {
}) {
nonce ??= randomBytes(8);
counter ??= Nonce64.int64(1);
var counter8 = counter.bytes;
var key8 = key is Uint8List ? key : Uint8List.fromList(key);
var nonce8 = nonce is Uint8List ? nonce : Uint8List.fromList(nonce);
return ChaCha20(key8, nonce8, counter.bytes);
return ChaCha20(key8, nonce8, counter8);
}

@override
Expand Down
23 changes: 4 additions & 19 deletions lib/src/algorithms/padding.dart
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ class _BytePadding extends Padding {

class _ANSIPadding extends Padding {
@override
final String name = "ANSI-X9.23";
final String name = "ANSI";

const _ANSIPadding();

Expand Down Expand Up @@ -248,7 +248,7 @@ class _ANSIPadding extends Padding {

class _PKCS7Padding extends Padding {
@override
final String name = "PKCS#7";
String get name => "PKCS7";

const _PKCS7Padding();

Expand Down Expand Up @@ -284,9 +284,9 @@ class _PKCS7Padding extends Padding {
}
}

class _PKCS5Padding extends Padding {
class _PKCS5Padding extends _PKCS7Padding {
@override
final String name = "PKCS#5";
final String name = "PKCS5";

const _PKCS5Padding();

Expand All @@ -305,19 +305,4 @@ class _PKCS5Padding extends Padding {
}
return true;
}

@override
int getPadLength(List<int> block, [int? size]) {
size ??= block.length;
int n = block[size - 1];
if (size < n) {
throw StateError('Invalid padding');
}
for (int p = size - n; p < size; p++) {
if (block[p] != n) {
throw StateError('Invalid padding');
}
}
return n;
}
}
Loading

0 comments on commit 6336e4b

Please sign in to comment.