Skip to content

Commit

Permalink
Refactor Nonce to be little-endian by default.
Browse files Browse the repository at this point in the history
- ChaCha20, and Salsa20 will accept Nonce now for counter.
- Test coverage reporting
  • Loading branch information
dipu-bd committed Aug 17, 2024
1 parent 278e541 commit 7c34242
Show file tree
Hide file tree
Showing 28 changed files with 881 additions and 267 deletions.
40 changes: 0 additions & 40 deletions .github/workflows/build.yml

This file was deleted.

82 changes: 77 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,98 @@ on:
push:
branches: [master]
paths: ['**.dart', '**.yaml', '**.yml']
pull_request:
branches: [master]
paths: ['**.dart', '**.yaml']

jobs:
test:
coverage:
if: github.repository == 'bitanon/cipherlib'
strategy:
matrix:
os: [ubuntu-latest]
sdk: ['stable']
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4

- uses: dart-lang/setup-dart@v1
with:
sdk: ${{ matrix.sdk }}

- name: Run tests with coverage
run: bash ./scripts/coverage.sh

- name: Upload results to Codecov
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}

test-on-stable:
if: github.repository == 'bitanon/cipherlib'
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
sdk: ['stable']
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4

- uses: dart-lang/setup-dart@v1
with:
sdk: ${{ matrix.sdk }}

- name: Install dependencies
run: dart pub get

- name: Verify formatting
run: dart format --output=none --set-exit-if-changed .

- name: Analyze project source
run: dart analyze --fatal-infos

- name: Run tests
run: dart test

test-on-legacy:
if: github.repository == 'bitanon/cipherlib'
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
sdk: ['2.14.0', 'stable', 'dev']
sdk: ['2.14.0']
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4

- uses: dart-lang/setup-dart@v1
with:
sdk: ${{ matrix.sdk }}

- name: Run tests
run: dart test

test-on-beta:
if: github.repository == 'bitanon/cipherlib'
strategy:
matrix:
os: [ubuntu-latest]
sdk: ['beta']
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4

# You can specify other versions if desired, see documentation here:
# https://github.com/dart-lang/setup-dart/blob/main/README.md
- uses: dart-lang/[email protected]
- uses: dart-lang/setup-dart@v1
with:
sdk: ${{ matrix.sdk }}

- name: Install dependencies
run: dart pub get

- name: Verify formatting
run: dart format --output=none --set-exit-if-changed .

- name: Analyze project source
run: dart analyze --fatal-infos

- name: Run tests
run: dart test
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
.dart_tool/
.packages

# Conventional directory for build outputs.
# Conventional directory for project outputs.
build/
doc/
coverage/
benchmark/**/*.exe
test/**/*.exe

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# 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)
Expand Down
38 changes: 18 additions & 20 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
# This file configures the static analysis results for your project (errors,
# warnings, and lints).
#
# This enables the 'recommended' set of lints from `package:lints`.
# This set helps identify many issues that may lead to problems when running
# or consuming Dart code, and enforces writing Dart using a single, idiomatic
# style and format.
# For more information about the core and recommended set of lints, see
# https://dart.dev/go/core-lints
#
# If you want a smaller set of lints you can change this to specify
# 'package:lints/core.yaml'. These are just the most critical lints
# (the recommended set includes the core lints).
# The core lints are also what is used by pub.dev for scoring packages.
# For additional information about configuring this file, see
# https://dart.dev/guides/language/analysis-options

include: package:lints/recommended.yaml

# linter:
# rules:
# - camel_case_types
linter:
rules:
- camel_case_types
- only_throw_errors
- comment_references
- library_annotations
- invalid_case_patterns
- combinators_ordering
- always_declare_return_types
- one_member_abstracts
- cancel_subscriptions
- avoid_unused_constructor_parameters

analyzer:
exclude:
- build/**

# For more information about the core and recommended set of lints, see
# https://dart.dev/go/core-lints

# For additional information about configuring this file, see
# https://dart.dev/guides/language/analysis-options
- test/**
- benchmark/**
4 changes: 2 additions & 2 deletions benchmark/chacha20.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class CipherlibBenchmark extends Benchmark {

@override
void run() {
cipher.ChaCha20(key, nonce).convert(input);
cipher.chacha20(input, key, nonce: nonce);
}
}

Expand Down Expand Up @@ -60,7 +60,7 @@ class CipherlibStreamBenchmark extends AsyncBenchmark {

@override
Future<void> run() async {
await cipher.ChaCha20(key, nonce).stream(inputStream).drain();
await cipher.chacha20Stream(inputStream, key, nonce: nonce).drain();
}
}

Expand Down
7 changes: 4 additions & 3 deletions dart_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ concurrency: 8
platforms: [vm, node]

tags:
skip-js:
vm-only:
skip: true
on_platform:
node:
skip: true
vm:
skip: false
12 changes: 6 additions & 6 deletions lib/src/aes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class AES {
///
/// **Not Recommended: It is vulnerable to pattern analysis.**
///
/// This implementation follows the specification from [NIST SP 800-38A -
/// This implementation follows the specification from [(NIST SP 800-38A) -
/// Recommendation for Block Cipher Modes of Operation: Methods and
/// Techniques][spec].
///
Expand All @@ -84,7 +84,7 @@ class AES {
/// encryption. CBC mode provides better security than ECB but requires
/// sequential processing.
///
/// This implementation follows the specification from [NIST SP 800-38A -
/// This implementation follows the specification from [(NIST SP 800-38A) -
/// Recommendation for Block Cipher Modes of Operation: Methods and
/// Techniques][spec].
///
Expand Down Expand Up @@ -116,7 +116,7 @@ class AES {
/// encryption and decryption, making it efficient for high-performance
/// applications.
///
/// This implementation follows the specification from [NIST SP 800-38A -
/// This implementation follows the specification from [(NIST SP 800-38A) -
/// Recommendation for Block Cipher Modes of Operation: Methods and
/// Techniques][spec].
///
Expand Down Expand Up @@ -146,7 +146,7 @@ class AES {
/// the plaintext to produce ciphertext. CFB does not require a padding to the
/// plaintext and can be used for error recovery.
///
/// This implementation follows the specification from [NIST SP 800-38A -
/// This implementation follows the specification from [(NIST SP 800-38A) -
/// Recommendation for Block Cipher Modes of Operation: Methods and
/// Techniques][spec].
///
Expand Down Expand Up @@ -193,7 +193,7 @@ class AES {
/// immune to transmission errors but requires careful management of the IV to
/// avoid security issues.
///
/// This implementation follows the specification from [NIST SP 800-38A -
/// This implementation follows the specification from [(NIST SP 800-38A) -
/// Recommendation for Block Cipher Modes of Operation: Methods and
/// Techniques][spec].
///
Expand Down Expand Up @@ -264,7 +264,7 @@ class AES {
/// multiplication for authentication. GCM provides both data confidentiality
/// and authenticity, making it a widely used and highly secure mode.
///
/// This implementation follows the specification from [NIST SP 800-38D -
/// This implementation follows the specification from [(NIST SP 800-38D) -
/// Recommendation for Block Cipher Modes of Operation: Galois/Counter Mode
/// (GCM) and GMAC][spec].
///
Expand Down
24 changes: 16 additions & 8 deletions lib/src/algorithms/aead_cipher.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,17 @@ import 'dart:typed_data';

import 'package:cipherlib/src/core/cipher.dart';
import 'package:cipherlib/src/core/cipher_sink.dart';
import 'package:hashlib/hashlib.dart' show HashDigest, MACSinkBase, MACHashBase;
import 'package:hashlib/hashlib.dart' show HashDigest, MACHashBase, MACSinkBase;

/// The result fromo AEAD ciphers
class AEADResult {
/// The IV, available if and only if cipher does supports it.
final Uint8List? iv;

/// The output message
final Uint8List data;

/// The message authentication code
final HashDigest tag;

const AEADResult({
this.iv,
const AEADResult._({
required this.tag,
required this.data,
});
Expand All @@ -30,7 +26,19 @@ class AEADResult {
bool verify(List<int>? digest) => tag.isEqual(digest);

/// Creates a new instance of AEADResult with IV parameter
AEADResult withIV(Uint8List iv) => AEADResult(tag: tag, data: data, iv: iv);
AEADResultWithIV withIV(Uint8List iv) =>
AEADResultWithIV._(tag: tag, data: data, iv: iv);
}

class AEADResultWithIV extends AEADResult {
/// The IV, available if and only if cipher does supports it.
final Uint8List iv;

const AEADResultWithIV._({
required this.iv,
required HashDigest tag,
required Uint8List data,
}) : super._(tag: tag, data: data);
}

/// Extends the base [AEADCipherSink] to generate message digest for cipher
Expand Down Expand Up @@ -178,7 +186,7 @@ abstract class AEADCipher<C extends Cipher, M extends MACHashBase>
var sink = createSink();
var cipher = sink.add(message, 0, null, true);
var digest = sink.digest();
return AEADResult(
return AEADResult._(
tag: digest,
data: cipher,
);
Expand Down
2 changes: 1 addition & 1 deletion lib/src/algorithms/aes/cbc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,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:hashlib/hashlib.dart';
import 'package:hashlib/hashlib.dart' show randomBytes;

import '_core.dart';

Expand Down
2 changes: 1 addition & 1 deletion lib/src/algorithms/aes/cfb.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,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:hashlib/hashlib.dart';
import 'package:hashlib/hashlib.dart' show randomBytes;

import '_core.dart';

Expand Down
6 changes: 3 additions & 3 deletions lib/src/algorithms/aes/ctr.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ 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';
import 'package:hashlib/hashlib.dart';
import 'package:hashlib/hashlib.dart' show randomBytes;

import '_core.dart';

Expand Down Expand Up @@ -157,8 +157,8 @@ class AESInCTRMode extends SaltedCollateCipher {
Nonce64? nonce,
Nonce64? counter,
}) {
var nonce8 = (nonce ?? Nonce64.random()).bytes;
var counter8 = (counter ?? Nonce64.random()).bytes;
var nonce8 = (nonce?.reverse() ?? Nonce64.random()).bytes;
var counter8 = (counter?.reverse() ?? Nonce64.random()).bytes;
var iv = Uint8List.fromList([...nonce8, ...counter8]);
return AESInCTRMode(key, iv);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/algorithms/aes/gcm.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,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:hashlib/hashlib.dart';
import 'package:hashlib/hashlib.dart' show randomBytes;

import '_core.dart';

Expand Down
Loading

0 comments on commit 7c34242

Please sign in to comment.