Skip to content

Commit 72f5ea3

Browse files
committed
tink-daead: add boringssl feature
Use the boring crate to pull in underlying BoringSSL implementation of AES. Continue to use the RustCrypto crates for the -SIV part.
1 parent 72fff9a commit 72f5ea3

File tree

6 files changed

+251
-5
lines changed

6 files changed

+251
-5
lines changed

.github/workflows/ci.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ jobs:
2828
- run: rustc --version
2929
- run: cargo build --release --workspace
3030
- run: (cd core && cargo build --features=json --release --all-targets)
31+
- run: (cd daead && cargo build --features=boringssl --release --all-targets)
32+
- run: (cd examples/daead && cargo build --features=boringssl --release)
3133

3234
test:
3335
runs-on: ubuntu-latest
@@ -111,7 +113,9 @@ jobs:
111113
override: true
112114
components: rustfmt, clippy
113115
- run: rustc --version
114-
- run: cargo clippy --all-features --all-targets -- -Dwarnings
116+
- run: cargo clippy --all-targets -- -Dwarnings
117+
- run: (cd core && cargo clippy --features=json -- -Dwarnings)
118+
- run: (cd daead && cargo clippy --features=boringssl -- -Dwarnings)
115119

116120
doc:
117121
runs-on: ubuntu-latest

Cargo.lock

Lines changed: 152 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

daead/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@ repository = "https://github.com/project-oak/tink-rust"
99
keywords = ["cryptography", "tink", "daead"]
1010
categories = ["cryptography"]
1111

12+
[features]
13+
boringssl = ["boring"]
14+
1215
[dependencies]
1316
aead = { version = "^0.4.2", features = ["std"] }
1417
aes-siv = "^0.6"
18+
boring = { version = "^1.1", optional = true }
19+
cipher = "^0.3"
1520
prost = "^0.8"
1621
tink-core = "^0.2"
1722
tink-proto = "^0.2"

daead/src/subtle/boring.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright 2020 The Tink-Rust Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
////////////////////////////////////////////////////////////////////////////////
16+
17+
//! Provides a Boring-SSL backed AES-256 cipher in a form suitable for
18+
//! use with RustCrypto traits.
19+
20+
use aes_siv::aead::generic_array::{
21+
typenum::{U16, U32, U8},
22+
GenericArray,
23+
};
24+
use std::convert::TryInto;
25+
26+
/// AES-256 block cipher
27+
#[derive(Clone)]
28+
pub struct Aes256 {
29+
key: [u8; 32],
30+
}
31+
32+
impl cipher::NewBlockCipher for Aes256 {
33+
type KeySize = U32;
34+
35+
#[inline]
36+
fn new(key: &GenericArray<u8, U32>) -> Self {
37+
Self {
38+
key: key.as_slice().try_into().unwrap(/* safe: array size checked */),
39+
}
40+
}
41+
}
42+
43+
impl Aes256 {
44+
fn process_block(&self, block: &mut GenericArray<u8, U16>, mode: boring::symm::Mode) {
45+
// To process a single block, use electronic code book mode (ECB) with no padding.
46+
let cipher = boring::symm::Cipher::aes_256_ecb();
47+
let mut c = boring::symm::Crypter::new(cipher, mode, &self.key[..], None).unwrap();
48+
c.pad(false);
49+
let mut output = vec![0; block.len() + cipher.block_size()];
50+
// TODO: investigate whether `boring` has an in-place operation.
51+
let count = c.update(block, &mut output).unwrap();
52+
let rest = c.finalize(&mut output[count..]).unwrap();
53+
output.truncate(count + rest);
54+
block[..16].copy_from_slice(&output)
55+
}
56+
}
57+
58+
impl cipher::BlockCipher for Aes256 {
59+
type BlockSize = U16;
60+
type ParBlocks = U8;
61+
}
62+
63+
impl cipher::BlockEncrypt for Aes256 {
64+
#[inline]
65+
fn encrypt_block(&self, block: &mut GenericArray<u8, U16>) {
66+
self.process_block(block, boring::symm::Mode::Encrypt);
67+
}
68+
}
69+
70+
impl cipher::BlockDecrypt for Aes256 {
71+
#[inline]
72+
fn decrypt_block(&self, block: &mut GenericArray<u8, U16>) {
73+
self.process_block(block, boring::symm::Mode::Decrypt);
74+
}
75+
}

0 commit comments

Comments
 (0)