Skip to content

Commit 9a76cc7

Browse files
authored
Merge pull request #1 from philipr-za/philip-add-github-ci
Add github actions ci to repo
2 parents 7b8104f + 566ace8 commit 9a76cc7

File tree

4 files changed

+136
-42
lines changed

4 files changed

+136
-42
lines changed

.github/workflows/rust.yml

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
name: Continuous Integration
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
8+
jobs:
9+
test-latest:
10+
name: Test on latest Rust
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: "Rust Version"
14+
run: rustc --version
15+
16+
- name: "Checkout code"
17+
uses: actions/checkout@v4
18+
19+
- name: Setup
20+
run: |
21+
rustc --version
22+
apt-get update && apt-get install -y valgrind
23+
cargo install cargo-valgrind
24+
25+
- name: Run tests
26+
run: |
27+
cargo valgrind test
28+
cargo test
29+
30+
- name: Upload artifacts
31+
uses: actions/upload-artifact@v4
32+
with:
33+
name: target-latest
34+
path: target
35+
retention-days: 14
36+
37+
test-msrv:
38+
name: Test on Rust MSRV (1.63.0)
39+
runs-on: ubuntu-latest
40+
container:
41+
image: rust:1.63.0
42+
steps:
43+
- uses: actions/checkout@v4
44+
45+
- name: Setup
46+
run: |
47+
rustc --version
48+
apt-get update && apt-get install -y valgrind
49+
cargo install cargo-valgrind --version 2.0.0
50+
51+
- name: Run tests
52+
run: |
53+
cargo valgrind test
54+
cargo test
55+
56+
- name: Upload artifacts
57+
uses: actions/upload-artifact@v4
58+
with:
59+
name: target-msrv
60+
path: target
61+
retention-days: 14
62+
63+
Format:
64+
name: Format - nightly toolchain
65+
runs-on: ubuntu-latest
66+
strategy:
67+
fail-fast: false
68+
steps:
69+
- name: "Checkout repo"
70+
uses: actions/checkout@v4
71+
- name: "Select toolchain"
72+
uses: dtolnay/rust-toolchain@nightly
73+
- name: "Install rustfmt"
74+
run: rustup component add rustfmt
75+
- name: "Check formatting"
76+
run: cargo +nightly fmt --all -- --check

src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ pub enum Error {
2020
#[error("Buffer not large enough to accomodate padded buffer")]
2121
PaddedBufferTooSmall,
2222
#[error("Unable to unpad buffer: {0}")]
23-
UnpadError(String)
23+
UnpadError(String),
2424
}

src/padding.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl Padding for Pkcs7 {
7272
return Err(Error::PaddedBufferTooSmall);
7373
}
7474

75-
let padding_length = padded_length-data_length;
75+
let padding_length = padded_length - data_length;
7676

7777
buffer[data_length..padded_length].fill(padding_length as u8);
7878
Ok(&buffer[0..padded_length])
@@ -84,10 +84,12 @@ impl Padding for Pkcs7 {
8484
Some(l) => *l,
8585
};
8686
if buffer.len() < padding_length as usize {
87-
return Err(Error::UnpadError("Buffer smaller than PKCS7 recorded padded length".to_string()));
87+
return Err(Error::UnpadError(
88+
"Buffer smaller than PKCS7 recorded padded length".to_string(),
89+
));
8890
}
8991
if padding_length == 0 {
90-
return Err(Error::UnpadError("PKCS7 padding length can't be zero".to_string()))
92+
return Err(Error::UnpadError("PKCS7 padding length can't be zero".to_string()));
9193
}
9294

9395
Ok(&buffer[0..buffer.len() - padding_length as usize])
@@ -141,9 +143,8 @@ mod test {
141143
let mut buffer = [2u8; 2 * BLOCK_SIZE + 1];
142144

143145
buffer[0..2 * BLOCK_SIZE].fill(1u8);
144-
145-
let padded_buffer =
146-
Pkcs7::pad(buffer.as_mut_slice(), DATA_LENGTH, BLOCK_SIZE).unwrap();
146+
147+
let padded_buffer = Pkcs7::pad(buffer.as_mut_slice(), DATA_LENGTH, BLOCK_SIZE).unwrap();
147148

148149
assert_eq!(&padded_buffer[0..DATA_LENGTH], [1u8; DATA_LENGTH].as_slice());
149150

tests/tests.rs

Lines changed: 52 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
* file COPYING or https://opensource.org/licenses/mit-license.php. *
55
**********************************************************************/
66

7-
extern crate hex_conservative;
87
extern crate ctaes;
8+
extern crate hex_conservative;
99

10-
use hex_conservative::FromHex;
11-
use ctaes::{Aes128, Aes128Cbc, Aes192, Aes256, AesBlockCipher, AesCbcBlockCipher};
12-
use ctaes::Aes256Cbc;
1310
use ctaes::Aes192Cbc;
11+
use ctaes::Aes256Cbc;
12+
use ctaes::{Aes128, Aes128Cbc, Aes192, Aes256, AesBlockCipher, AesCbcBlockCipher};
13+
use hex_conservative::FromHex;
1414

1515
struct AesTestVector {
1616
key: Vec<u8>,
@@ -25,79 +25,94 @@ fn aes_test_vectors() -> Vec<AesTestVector> {
2525
vectors.push(AesTestVector {
2626
key: <Vec<u8>>::from_hex("000102030405060708090a0b0c0d0e0f").unwrap(),
2727
plaintext: <Vec<u8>>::from_hex("00112233445566778899aabbccddeeff").unwrap(),
28-
ciphertext: <Vec<u8>>::from_hex("69c4e0d86a7b0430d8cdb78070b4c55a").unwrap()
28+
ciphertext: <Vec<u8>>::from_hex("69c4e0d86a7b0430d8cdb78070b4c55a").unwrap(),
2929
});
3030
vectors.push(AesTestVector {
3131
key: <Vec<u8>>::from_hex("000102030405060708090a0b0c0d0e0f1011121314151617").unwrap(),
3232
plaintext: <Vec<u8>>::from_hex("00112233445566778899aabbccddeeff").unwrap(),
33-
ciphertext: <Vec<u8>>::from_hex("dda97ca4864cdfe06eaf70a0ec0d7191").unwrap()
33+
ciphertext: <Vec<u8>>::from_hex("dda97ca4864cdfe06eaf70a0ec0d7191").unwrap(),
3434
});
3535
vectors.push(AesTestVector {
36-
key: <Vec<u8>>::from_hex("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f").unwrap(),
36+
key: <Vec<u8>>::from_hex(
37+
"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
38+
)
39+
.unwrap(),
3740
plaintext: <Vec<u8>>::from_hex("00112233445566778899aabbccddeeff").unwrap(),
38-
ciphertext: <Vec<u8>>::from_hex("8ea2b7ca516745bfeafc49904b496089").unwrap()
41+
ciphertext: <Vec<u8>>::from_hex("8ea2b7ca516745bfeafc49904b496089").unwrap(),
3942
});
4043

4144
/* AES-ECB test vectors from NIST sp800-38a. */
4245
vectors.push(AesTestVector {
4346
key: <Vec<u8>>::from_hex("2b7e151628aed2a6abf7158809cf4f3c").unwrap(),
4447
plaintext: <Vec<u8>>::from_hex("6bc1bee22e409f96e93d7e117393172a").unwrap(),
45-
ciphertext: <Vec<u8>>::from_hex("3ad77bb40d7a3660a89ecaf32466ef97").unwrap()
48+
ciphertext: <Vec<u8>>::from_hex("3ad77bb40d7a3660a89ecaf32466ef97").unwrap(),
4649
});
4750
vectors.push(AesTestVector {
4851
key: <Vec<u8>>::from_hex("2b7e151628aed2a6abf7158809cf4f3c").unwrap(),
4952
plaintext: <Vec<u8>>::from_hex("ae2d8a571e03ac9c9eb76fac45af8e51").unwrap(),
50-
ciphertext: <Vec<u8>>::from_hex("f5d3d58503b9699de785895a96fdbaaf").unwrap()
53+
ciphertext: <Vec<u8>>::from_hex("f5d3d58503b9699de785895a96fdbaaf").unwrap(),
5154
});
5255
vectors.push(AesTestVector {
5356
key: <Vec<u8>>::from_hex("2b7e151628aed2a6abf7158809cf4f3c").unwrap(),
5457
plaintext: <Vec<u8>>::from_hex("30c81c46a35ce411e5fbc1191a0a52ef").unwrap(),
55-
ciphertext: <Vec<u8>>::from_hex("43b1cd7f598ece23881b00e3ed030688").unwrap()
58+
ciphertext: <Vec<u8>>::from_hex("43b1cd7f598ece23881b00e3ed030688").unwrap(),
5659
});
5760
vectors.push(AesTestVector {
5861
key: <Vec<u8>>::from_hex("2b7e151628aed2a6abf7158809cf4f3c").unwrap(),
5962
plaintext: <Vec<u8>>::from_hex("f69f2445df4f9b17ad2b417be66c3710").unwrap(),
60-
ciphertext: <Vec<u8>>::from_hex("7b0c785e27e8ad3f8223207104725dd4").unwrap()
63+
ciphertext: <Vec<u8>>::from_hex("7b0c785e27e8ad3f8223207104725dd4").unwrap(),
6164
});
6265
vectors.push(AesTestVector {
6366
key: <Vec<u8>>::from_hex("8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b").unwrap(),
6467
plaintext: <Vec<u8>>::from_hex("6bc1bee22e409f96e93d7e117393172a").unwrap(),
65-
ciphertext: <Vec<u8>>::from_hex("bd334f1d6e45f25ff712a214571fa5cc").unwrap()
68+
ciphertext: <Vec<u8>>::from_hex("bd334f1d6e45f25ff712a214571fa5cc").unwrap(),
6669
});
6770
vectors.push(AesTestVector {
6871
key: <Vec<u8>>::from_hex("8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b").unwrap(),
6972
plaintext: <Vec<u8>>::from_hex("ae2d8a571e03ac9c9eb76fac45af8e51").unwrap(),
70-
ciphertext: <Vec<u8>>::from_hex("974104846d0ad3ad7734ecb3ecee4eef").unwrap()
73+
ciphertext: <Vec<u8>>::from_hex("974104846d0ad3ad7734ecb3ecee4eef").unwrap(),
7174
});
7275
vectors.push(AesTestVector {
7376
key: <Vec<u8>>::from_hex("8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b").unwrap(),
7477
plaintext: <Vec<u8>>::from_hex("30c81c46a35ce411e5fbc1191a0a52ef").unwrap(),
75-
ciphertext: <Vec<u8>>::from_hex("ef7afd2270e2e60adce0ba2face6444e").unwrap()
78+
ciphertext: <Vec<u8>>::from_hex("ef7afd2270e2e60adce0ba2face6444e").unwrap(),
7679
});
7780
vectors.push(AesTestVector {
7881
key: <Vec<u8>>::from_hex("8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b").unwrap(),
7982
plaintext: <Vec<u8>>::from_hex("f69f2445df4f9b17ad2b417be66c3710").unwrap(),
80-
ciphertext: <Vec<u8>>::from_hex("9a4b41ba738d6c72fb16691603c18e0e").unwrap()
83+
ciphertext: <Vec<u8>>::from_hex("9a4b41ba738d6c72fb16691603c18e0e").unwrap(),
8184
});
8285
vectors.push(AesTestVector {
83-
key: <Vec<u8>>::from_hex("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4").unwrap(),
86+
key: <Vec<u8>>::from_hex(
87+
"603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4",
88+
)
89+
.unwrap(),
8490
plaintext: <Vec<u8>>::from_hex("6bc1bee22e409f96e93d7e117393172a").unwrap(),
85-
ciphertext: <Vec<u8>>::from_hex("f3eed1bdb5d2a03c064b5a7e3db181f8").unwrap()
91+
ciphertext: <Vec<u8>>::from_hex("f3eed1bdb5d2a03c064b5a7e3db181f8").unwrap(),
8692
});
8793
vectors.push(AesTestVector {
88-
key: <Vec<u8>>::from_hex("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4").unwrap(),
94+
key: <Vec<u8>>::from_hex(
95+
"603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4",
96+
)
97+
.unwrap(),
8998
plaintext: <Vec<u8>>::from_hex("ae2d8a571e03ac9c9eb76fac45af8e51").unwrap(),
90-
ciphertext: <Vec<u8>>::from_hex("591ccb10d410ed26dc5ba74a31362870").unwrap()
99+
ciphertext: <Vec<u8>>::from_hex("591ccb10d410ed26dc5ba74a31362870").unwrap(),
91100
});
92101
vectors.push(AesTestVector {
93-
key: <Vec<u8>>::from_hex("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4").unwrap(),
102+
key: <Vec<u8>>::from_hex(
103+
"603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4",
104+
)
105+
.unwrap(),
94106
plaintext: <Vec<u8>>::from_hex("30c81c46a35ce411e5fbc1191a0a52ef").unwrap(),
95-
ciphertext: <Vec<u8>>::from_hex("b6ed21b99ca6f4f9f153e7b1beafed1d").unwrap()
107+
ciphertext: <Vec<u8>>::from_hex("b6ed21b99ca6f4f9f153e7b1beafed1d").unwrap(),
96108
});
97109
vectors.push(AesTestVector {
98-
key: <Vec<u8>>::from_hex("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4").unwrap(),
110+
key: <Vec<u8>>::from_hex(
111+
"603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4",
112+
)
113+
.unwrap(),
99114
plaintext: <Vec<u8>>::from_hex("f69f2445df4f9b17ad2b417be66c3710").unwrap(),
100-
ciphertext: <Vec<u8>>::from_hex("23304b7a39f9f3ff067d8d8f9e24ecc7").unwrap()
115+
ciphertext: <Vec<u8>>::from_hex("23304b7a39f9f3ff067d8d8f9e24ecc7").unwrap(),
101116
});
102117

103118
vectors
@@ -135,23 +150,22 @@ fn aes_cbc_test_vectors() -> Vec<AesCbcTestVector> {
135150
vectors
136151
}
137152

138-
139153
#[test]
140154
fn test_aes_ciphers() {
141155
for test_vector in aes_test_vectors() {
142156
match test_vector.key.len() {
143157
16 => {
144158
let cipher = Aes128::new(test_vector.key.as_slice()).unwrap();
145159
test_cipher(cipher, &test_vector.plaintext, &test_vector.ciphertext);
146-
},
160+
}
147161
24 => {
148162
let cipher = Aes192::new(test_vector.key.as_slice()).unwrap();
149163
test_cipher(cipher, &test_vector.plaintext, &test_vector.ciphertext);
150-
},
164+
}
151165
32 => {
152166
let cipher = Aes256::new(test_vector.key.as_slice()).unwrap();
153167
test_cipher(cipher, &test_vector.plaintext, &test_vector.ciphertext);
154-
},
168+
}
155169
_ => panic!("Invalid key length"),
156170
};
157171
}
@@ -173,17 +187,20 @@ fn test_aes_cbc_ciphers() {
173187
for test_vector in aes_cbc_test_vectors() {
174188
match test_vector.key.len() {
175189
16 => {
176-
let cipher = Aes128Cbc::new(test_vector.key.as_slice(), test_vector.iv.as_slice()).unwrap();
190+
let cipher =
191+
Aes128Cbc::new(test_vector.key.as_slice(), test_vector.iv.as_slice()).unwrap();
177192
test_cbc_cipher(cipher, &test_vector.plaintext, &test_vector.ciphertext);
178-
},
193+
}
179194
24 => {
180-
let cipher = Aes192Cbc::new(test_vector.key.as_slice(), test_vector.iv.as_slice()).unwrap();
195+
let cipher =
196+
Aes192Cbc::new(test_vector.key.as_slice(), test_vector.iv.as_slice()).unwrap();
181197
test_cbc_cipher(cipher, &test_vector.plaintext, &test_vector.ciphertext);
182-
},
198+
}
183199
32 => {
184-
let cipher = Aes256Cbc::new(test_vector.key.as_slice(), test_vector.iv.as_slice()).unwrap();
200+
let cipher =
201+
Aes256Cbc::new(test_vector.key.as_slice(), test_vector.iv.as_slice()).unwrap();
185202
test_cbc_cipher(cipher, &test_vector.plaintext, &test_vector.ciphertext);
186-
},
203+
}
187204
_ => panic!("Invalid key length"),
188205
};
189206
}
@@ -198,4 +215,4 @@ fn test_cbc_cipher<T: AesCbcBlockCipher>(cipher: T, plaintext: &Vec<u8>, ciphert
198215

199216
cipher.decrypt(ciphered.as_slice(), deciphered.as_mut_slice()).unwrap();
200217
assert_eq!(deciphered.as_slice(), plaintext.as_slice());
201-
}
218+
}

0 commit comments

Comments
 (0)