Skip to content

Commit e6535ac

Browse files
authored
Merge pull request #8 from rozbb/no-mut-mac
Make inputs to `recv_mac` and `meta_recv_mac` immutable
2 parents f3883ba + ff9856f commit e6535ac

File tree

8 files changed

+185
-203
lines changed

8 files changed

+185
-203
lines changed

.github/workflows/ci.yml

Lines changed: 38 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1,150 +1,64 @@
11
name: CI
2+
23
on:
3-
# Run CI on new pushes to the repo or any pull requests.
4-
# If only the README file changed, ignore.
5-
pull_request:
6-
paths-ignore:
7-
- README.md
84
push:
9-
branches: master
10-
paths-ignore:
11-
- README.md
12-
# Allow CI workflow to be run manually
13-
workflow_dispatch:
14-
# Run CI at least once a month no matter what
15-
schedule:
16-
- cron: '0 1 1 */1 *'
5+
branches: ["**"]
6+
paths-ignore: ["README.md", "CHANGELOG.md"]
7+
pull_request:
8+
branches: ["**"]
9+
paths-ignore: ["README.md", "CHANGELOG.md"]
10+
11+
env:
12+
CARGO_TERM_COLOR: always
13+
RUSTFLAGS: "-D warnings"
1714

1815
jobs:
1916
test:
20-
strategy:
21-
matrix:
22-
platform:
23-
- ubuntu-latest
24-
- macos-latest
25-
- windows-latest
26-
toolchain:
27-
- stable
28-
- nightly
29-
runs-on: ${{ matrix.platform }}
30-
steps:
31-
- name: Checkout sources
32-
uses: actions/checkout@v2
33-
34-
- name: Install toolchain
35-
uses: actions-rs/toolchain@v1
36-
with:
37-
toolchain: ${{ matrix.toolchain }}
38-
override: true
39-
40-
- name: Run cargo test --no-default-features
41-
env:
42-
CARGO_INCREMENTAL: 0
43-
RUSTFLAGS: -D warnings
44-
uses: actions-rs/cargo@v1
45-
with:
46-
command: test
47-
args: --no-default-features
48-
49-
- name: Run cargo test --all-features
50-
env:
51-
CARGO_INCREMENTAL: 0
52-
RUSTFLAGS: -D warnings
53-
uses: actions-rs/cargo@v1
54-
with:
55-
command: test
56-
args: --all-features
57-
58-
cross-linux-test:
17+
name: Test on Rust stable
5918
runs-on: ubuntu-latest
6019
strategy:
61-
fail-fast: false
6220
matrix:
63-
rust_target:
64-
# Other interesting archs here:
65-
# https://github.com/rust-embedded/cross/tree/master/docker
66-
- i686-unknown-linux-gnu
67-
- powerpc-unknown-linux-gnu
68-
timeout-minutes: 10
69-
steps:
70-
- name: Checkout sources
71-
uses: actions/checkout@v2
21+
include:
22+
# 32-bit big-endian target
23+
- target: powerpc-unknown-linux-gnu
24+
deps: sudo apt update && sudo apt install gcc-multilib
7225

73-
- name: Install Rust stable
74-
uses: actions-rs/toolchain@v1
75-
with:
76-
toolchain: stable
77-
profile: minimal
78-
target: ${{ matrix.rust_target }}
79-
default: true
80-
81-
- name: Test
82-
env:
83-
CARGO_INCREMENTAL: 0
84-
RUSTFLAGS: -D warnings
85-
uses: actions-rs/cargo@v1
86-
with:
87-
# see https://github.com/rust-embedded/cross
88-
use-cross: true
89-
command: test
90-
args: --all-features --target ${{ matrix.rust_target }}
26+
# 64-bit target
27+
- target: x86_64-unknown-linux-gnu
28+
steps:
29+
- uses: actions/checkout@v3
30+
- uses: dtolnay/rust-toolchain@stable
31+
- run: cargo test
9132

9233
rustfmt:
9334
runs-on: ubuntu-latest
9435
steps:
95-
- name: Checkout sources
96-
uses: actions/checkout@v2
97-
98-
- name: Install stable toolchain
99-
uses: actions-rs/toolchain@v1
100-
with:
101-
profile: minimal
102-
toolchain: stable
103-
components: rustfmt
36+
- uses: actions/checkout@v3
37+
- uses: dtolnay/rust-toolchain@stable
38+
- run: cargo fmt --all -- --check
10439

105-
- name: Run cargo fmt
106-
uses: actions-rs/cargo@v1
107-
with:
108-
command: fmt
109-
args: --all -- --check
11040
clippy:
11141
runs-on: ubuntu-latest
11242
steps:
113-
- uses: actions/checkout@v2
114-
- uses: actions-rs/toolchain@v1
43+
- uses: actions/checkout@v3
44+
- uses: dtolnay/rust-toolchain@stable
11545
with:
116-
profile: minimal
117-
toolchain: stable
11846
components: clippy
11947
- run: cargo clippy --all-features -- -D warnings
12048

121-
codecov:
49+
# Test no_std build-only
50+
build-nostd:
51+
name: Build on no_std target (thumbv7em-none-eabi)
12252
runs-on: ubuntu-latest
12353
steps:
124-
- name: Checkout repository
125-
uses: actions/checkout@v2
126-
127-
- name: Install stable toolchain
128-
uses: actions-rs/toolchain@v1
54+
- uses: actions/checkout@v3
55+
- uses: dtolnay/rust-toolchain@master
12956
with:
13057
toolchain: stable
131-
override: true
132-
133-
- name: Run cargo-tarpaulin
134-
uses: actions-rs/[email protected]
135-
env:
136-
CARGO_INCREMENTAL: 0
137-
with:
138-
version: 0.15.0
139-
args: --all-features
140-
141-
- name: Upload to codecov.io
142-
uses: codecov/[email protected]
143-
with:
144-
token: ${{secrets.CODECOV_TOKEN}}
145-
146-
- name: Archive code coverage results
147-
uses: actions/upload-artifact@v1
148-
with:
149-
name: code-coverage-report
150-
path: cobertura.xml
58+
targets: thumbv7em-none-eabi
59+
- uses: taiki-e/install-action@cargo-hack
60+
# No default features build
61+
- name: no_std / no feat
62+
run: cargo build --target thumbv7em-none-eabi --release --no-default-features
63+
- name: no_std / cargo hack
64+
run: cargo hack build --target thumbv7em-none-eabi --release --each-feature --exclude-features default,std

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## Unrelease
8+
9+
* Made `(meta_)recv_mac` inputs take a `&[u8; N]` rather than a `&mut [u8]`
10+
711
## [0.8.1] - 2022-10-10
812

913
### Changes

Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ documentation = "https://docs.rs/strobe-rs"
55
description = "An implementation of the Strobe protocol framework in pure Rust"
66
readme = "README.md"
77
version = "0.8.1"
8-
edition = "2018"
8+
edition = "2021"
99
authors = ["Michael Rosenberg <[email protected]>"]
1010
license = "MIT/Apache-2.0"
1111
keywords = ["crypto", "strobe", "protocol", "framework", "no_std"]
@@ -20,14 +20,17 @@ serialize_secret_state = ["serde", "serde-big-array"]
2020
bitflags = "1.3"
2121
byteorder = { version = "1.4", default-features = false }
2222
keccak = "0.1"
23-
serde = { version = "1", optional = true, default-features = false, features = ["derive"] }
23+
serde = { version = "1", optional = true, default-features = false, features = [
24+
"derive",
25+
] }
2426
serde-big-array = { version = "0.4", optional = true }
2527
subtle = { version = "2.4", default-features = false }
2628
zeroize = { version = "1.5", features = ["derive"] }
2729

2830
[dev-dependencies]
2931
criterion = { version = "0.5", features = ["html_reports"] }
3032
hex = "0.4"
33+
rand = "0.8.5"
3134
serde = { version = "1", features = ["derive"] }
3235
serde_json = "1"
3336

README.md

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,60 @@ strobe-rs
66
[![Version](https://img.shields.io/crates/v/strobe-rs.svg)](https://crates.io/crates/strobe-rs)
77
[![Docs](https://docs.rs/strobe-rs/badge.svg)](https://docs.rs/strobe-rs)
88

9-
This is a relatively barebones, `no_std` implementation of the [Strobe protocol framework][strobe] in pure Rust. It is intended to be used as a library to build other protocols and frameworks. This implementation currently only supports Keccak-f\[1600\] as the internal permutation function, which is the largest possible block size, so big deal.
9+
This is a pure Rust, `no_std` implementation of the [Strobe protocol framework][strobe]. It is intended to be used as a library to build other protocols and frameworks. This implementation currently only supports Keccak-f\[1600\] as the internal permutation function, which is the largest possible block size, so big deal.
1010

1111
[strobe]: https://strobe.sourceforge.io/
1212

1313
Example
1414
-------
1515

16-
A simple [program](examples/basic.rs) that encrypts and decrypts a message:
16+
A simple [program](examples/basic.rs) that does authenticated encryption and decryption:
1717

1818
```rust
1919
use strobe_rs::{SecParam, Strobe};
2020

21-
fn main() {
22-
let mut rx = Strobe::new(b"correctnesstest", SecParam::B256);
23-
let mut tx = Strobe::new(b"correctnesstest", SecParam::B256);
24-
25-
rx.key(b"the-combination-on-my-luggage", false);
26-
tx.key(b"the-combination-on-my-luggage", false);
21+
use rand::RngCore;
2722

28-
let mut msg = b"Attack at dawn".to_vec();
29-
rx.send_enc(msg.as_mut_slice(), false);
23+
// NOTE: This is just a simple authenticated encryption scheme. For a robust AEAD construction,
24+
// see the example at https://strobe.sourceforge.io/examples/aead/
3025

31-
// Rename for clarity. `msg` has been encrypted in-place.
32-
let mut ciphertext = msg;
26+
fn main() {
27+
let mut rng = rand::thread_rng();
3328

34-
tx.recv_enc(ciphertext.as_mut_slice(), false);
29+
// Sender and receiver
30+
let mut tx = Strobe::new(b"correctnesstest", SecParam::B256);
31+
let mut rx = Strobe::new(b"correctnesstest", SecParam::B256);
3532

36-
// And back again.
33+
// Key both sides with a predetermined key
34+
let k = b"the-combination-on-my-luggage";
35+
tx.key(k, false);
36+
rx.key(k, false);
37+
38+
// Have the transmitter sample and send a nonce (192 bits) in the clear
39+
let mut nonce = [0u8; 24];
40+
rng.fill_bytes(&mut nonce);
41+
rx.recv_clr(&nonce, false);
42+
tx.send_clr(&nonce, false);
43+
44+
// Have the transmitter send an authenticated ciphertext (with a 256 bit MAC)
45+
let orig_msg = b"groceries: kaymac, ajvar, cream, diced onion, red pepper, grilled meat";
46+
let mut msg_buf = *orig_msg;
47+
tx.send_enc(&mut msg_buf, false);
48+
let mut mac = [0u8; 32];
49+
tx.send_mac(&mut mac, false);
50+
51+
// Rename for clarity. `msg_buf` has been encrypted in-place.
52+
let mut ciphertext = msg_buf;
53+
54+
// Have the receiver receive the ciphertext and MAC
55+
rx.recv_enc(ciphertext.as_mut_slice(), false);
56+
let res = rx.recv_mac(&mac);
57+
58+
// Check that the MAC verifies
59+
assert!(res.is_ok());
60+
// Check that the decrypted ciphertext equals the original plaintext
3761
let round_trip_msg = ciphertext;
38-
39-
assert_eq!(&round_trip_msg, b"Attack at dawn");
62+
assert_eq!(&round_trip_msg, orig_msg);
4063
}
4164
```
4265

examples/basic.rs

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,45 @@
11
use strobe_rs::{SecParam, Strobe};
22

3+
use rand::RngCore;
4+
5+
// NOTE: This is just a simple authenticated encryption scheme. For a robust AEAD construction,
6+
// see the example at https://strobe.sourceforge.io/examples/aead/
7+
38
fn main() {
4-
let mut rx = Strobe::new(b"correctnesstest", SecParam::B256);
9+
let mut rng = rand::thread_rng();
10+
11+
// Sender and receiver
512
let mut tx = Strobe::new(b"correctnesstest", SecParam::B256);
13+
let mut rx = Strobe::new(b"correctnesstest", SecParam::B256);
614

7-
rx.key(b"the-combination-on-my-luggage", false);
8-
tx.key(b"the-combination-on-my-luggage", false);
15+
// Key both sides with a predetermined key
16+
let k = b"the-combination-on-my-luggage";
17+
tx.key(k, false);
18+
rx.key(k, false);
919

10-
let mut msg = b"Attack at dawn".to_vec();
11-
rx.send_enc(msg.as_mut_slice(), false);
20+
// Have the transmitter sample and send a nonce (192 bits) in the clear
21+
let mut nonce = [0u8; 24];
22+
rng.fill_bytes(&mut nonce);
23+
rx.recv_clr(&nonce, false);
24+
tx.send_clr(&nonce, false);
1225

13-
// Rename for clarity. `msg` has been encrypted in-place.
14-
let mut ciphertext = msg;
26+
// Have the transmitter send an authenticated ciphertext (with a 256 bit MAC)
27+
let orig_msg = b"groceries: kaymac, ajvar, cream, diced onion, red pepper, grilled meat";
28+
let mut msg_buf = *orig_msg;
29+
tx.send_enc(&mut msg_buf, false);
30+
let mut mac = [0u8; 32];
31+
tx.send_mac(&mut mac, false);
1532

16-
tx.recv_enc(ciphertext.as_mut_slice(), false);
33+
// Rename for clarity. `msg_buf` has been encrypted in-place.
34+
let mut ciphertext = msg_buf;
1735

18-
// And back again.
19-
let round_trip_msg = ciphertext;
36+
// Have the receiver receive the ciphertext and MAC
37+
rx.recv_enc(ciphertext.as_mut_slice(), false);
38+
let res = rx.recv_mac(&mac);
2039

21-
assert_eq!(&round_trip_msg, b"Attack at dawn");
40+
// Check that the MAC verifies
41+
assert!(res.is_ok());
42+
// Check that the decrypted ciphertext equals the original plaintext
43+
let round_trip_msg = ciphertext;
44+
assert_eq!(&round_trip_msg, orig_msg);
2245
}

0 commit comments

Comments
 (0)