Skip to content

Commit

Permalink
XTEA implementation (#422)
Browse files Browse the repository at this point in the history
This PR implements XTEA as described by various sources, including
https://en.wikipedia.org/wiki/XTEA,

XTEA is a historical cipher, which is no longer commonly used today, but
some legacy software still uses it to this day.

I tested that the cipher methods get properly unrolled, which boosts a
3x speed increase, and is one of the faster ciphers in this collection.
(Hopefully there will be an unroll pragma sometime in the future)

Partially solves #1

I decided against pre-computing keys as the computation is trivial, and
doesn't have a noticeable impact on speed, probably because of memory
accesses and that 256 byte don't fit easily in a cache line anyway.
  • Loading branch information
valaphee authored Jul 29, 2024
1 parent e96cb03 commit 0be7856
Show file tree
Hide file tree
Showing 13 changed files with 400 additions and 1 deletion.
60 changes: 60 additions & 0 deletions .github/workflows/xtea.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: xtea

on:
pull_request:
paths:
- "xtea/**"
- "Cargo.*"
push:
branches: master

defaults:
run:
working-directory: xtea

env:
CARGO_INCREMENTAL: 0
RUSTFLAGS: "-Dwarnings"

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
rust:
- 1.65.0 # MSRV
- stable
target:
- thumbv7em-none-eabi
- wasm32-unknown-unknown
steps:
- uses: actions/checkout@v3
- uses: RustCrypto/actions/cargo-cache@master
- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.rust }}
targets: ${{ matrix.target }}
- run: cargo build --no-default-features --release --target ${{ matrix.target }}

minimal-versions:
uses: RustCrypto/actions/.github/workflows/minimal-versions.yml@master
with:
working-directory: ${{ github.workflow }}

test:
runs-on: ubuntu-latest
strategy:
matrix:
rust:
- 1.65.0 # MSRV
- stable
steps:
- uses: actions/checkout@v3
- uses: RustCrypto/actions/cargo-cache@master
- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.rust }}
- run: cargo check --all-features
- run: cargo test --no-default-features
- run: cargo test
- run: cargo test --all-features
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ members = [
"cast5",
"cast6",
"des",
"gift",
"idea",
"kuznyechik",
"magma",
Expand All @@ -19,7 +20,7 @@ members = [
"speck",
"twofish",
"threefish",
"gift",
"xtea",
]

[profile.dev]
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ It's generally recommended not to use other cipher implementations in this repos
| [Speck] | [`speck-cipher`] | [![crates.io](https://img.shields.io/crates/v/speck-cipher.svg)](https://crates.io/crates/speck-cipher) | [![Documentation](https://docs.rs/speck-cipher/badge.svg)](https://docs.rs/speck-cipher) | ![MSRV 1.65][msrv-1.65] |
| [Threefish] | [`threefish`] | [![crates.io](https://img.shields.io/crates/v/threefish.svg)](https://crates.io/crates/threefish) | [![Documentation](https://docs.rs/threefish/badge.svg)](https://docs.rs/threefish) | ![MSRV 1.65][msrv-1.65] |
| [Twofish] | [`twofish`] | [![crates.io](https://img.shields.io/crates/v/twofish.svg)](https://crates.io/crates/twofish) | [![Documentation](https://docs.rs/twofish/badge.svg)](https://docs.rs/twofish) | ![MSRV 1.65][msrv-1.65] |
| [XTEA] | [`xtea`] | [![crates.io](https://img.shields.io/crates/v/xtea.svg)](https://crates.io/crates/xtea) | [![Documentation](https://docs.rs/xtea/badge.svg)](https://docs.rs/xtea) | ![MSRV 1.65][msrv-1.65] |

### Minimum Supported Rust Version (MSRV) Policy

Expand Down Expand Up @@ -105,6 +106,7 @@ dual licensed as above, without any additional terms or conditions.
[`speck-cipher`]: ./speck
[`threefish`]: ./threefish
[`twofish`]: ./twofish
[`xtea`]: ./xtea

[//]: # (links)

Expand All @@ -131,3 +133,4 @@ dual licensed as above, without any additional terms or conditions.
[Speck]: https://en.wikipedia.org/wiki/Speck_(cipher)
[Threefish]: https://en.wikipedia.org/wiki/Threefish
[Twofish]: https://en.wikipedia.org/wiki/Twofish
[XTEA]: https://en.wikipedia.org/wiki/XTEA
9 changes: 9 additions & 0 deletions xtea/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 0.1.0 (2024-05-11)
- Initial release
26 changes: 26 additions & 0 deletions xtea/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "xtea"
version = "0.1.0-pre"
description = "XTEA block cipher"
authors = ["RustCrypto Developers"]
license = "MIT OR Apache-2.0"
edition = "2021"
rust-version = "1.65"
readme = "README.md"
documentation = "https://docs.rs/xtea"
repository = "https://github.com/RustCrypto/block-ciphers"
keywords = ["crypto", "xtea", "block-cipher"]
categories = ["cryptography", "no-std"]

[dependencies]
cipher = "=0.5.0-pre.4"

[dev-dependencies]
cipher = { version = "=0.5.0-pre.4", features = ["dev"] }

[features]
zeroize = ["cipher/zeroize"]

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
13 changes: 13 additions & 0 deletions xtea/LICENSE-APACHE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright 2024 Kevin Ludwig

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
25 changes: 25 additions & 0 deletions xtea/LICENSE-MIT
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Copyright (c) 2024 Kevin Ludwig

Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the
Software without restriction, including without
limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice
shall be included in all copies or substantial portions
of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
71 changes: 71 additions & 0 deletions xtea/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# RustCrypto: XTEA Cipher

[![crate][crate-image]][crate-link]
[![Docs][docs-image]][docs-link]
![Apache2/MIT licensed][license-image]
![Rust Version][rustc-image]
[![Project Chat][chat-image]][chat-link]
[![Build Status][build-image]][build-link]
[![HAZMAT][hazmat-image]][hazmat-link]

Pure Rust implementation of the [XTEA block cipher][1].

[Documentation][docs-link]

## ⚠️ Security Warning: [Hazmat!][hazmat-link]

This crate does not ensure ciphertexts are authentic (i.e. by using a MAC to
verify ciphertext integrity), which can lead to serious vulnerabilities
if used incorrectly!

No security audits of this crate have ever been performed, and it has not been
thoroughly assessed to ensure its operation is constant-time on common CPU
architectures.

USE AT YOUR OWN RISK!

## Minimum Supported Rust Version

Rust **1.65** or higher.

Minimum supported Rust version can be changed in the future, but it will be
done with a minor version bump.

## SemVer Policy

- All on-by-default features of this library are covered by SemVer
- MSRV is considered exempt from SemVer as noted above

## License

Licensed under either of:

* [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0)
* [MIT license](http://opensource.org/licenses/MIT)

at your option.

### Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
dual licensed as above, without any additional terms or conditions.

[//]: # (badges)

[crate-image]: https://img.shields.io/crates/v/xtea.svg
[crate-link]: https://crates.io/crates/xtea
[docs-image]: https://docs.rs/xtea/badge.svg
[docs-link]: https://docs.rs/xtea/
[license-image]: https://img.shields.io/badge/license-Apache2.0/MIT-blue.svg
[rustc-image]: https://img.shields.io/badge/rustc-1.65+-blue.svg
[hazmat-image]: https://img.shields.io/badge/crypto-hazmat%E2%9A%A0-red.svg
[hazmat-link]: https://github.com/RustCrypto/meta/blob/master/HAZMAT.md
[chat-image]: https://img.shields.io/badge/zulip-join_chat-blue.svg
[chat-link]: https://rustcrypto.zulipchat.com/#narrow/stream/260039-block-ciphers
[build-image]: https://github.com/RustCrypto/block-ciphers/workflows/xtea/badge.svg?branch=master&event=push
[build-link]: https://github.com/RustCrypto/block-ciphers/actions?query=workflow%3Axtea

[//]: # (general links)

[1]: https://en.wikipedia.org/wiki/XTEA
8 changes: 8 additions & 0 deletions xtea/benches/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![feature(test)]
extern crate test;

use cipher::{block_decryptor_bench, block_encryptor_bench};
use xtea::Xtea;

block_encryptor_bench!(Key: Xtea, xtea_encrypt_block, xtea_encrypt_blocks);
block_decryptor_bench!(Key: Xtea, xtea_decrypt_block, xtea_decrypt_blocks);
2 changes: 2 additions & 0 deletions xtea/src/consts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub const DELTA: u32 = 0x9e3779b9;
pub const ROUNDS: usize = 32;
Loading

0 comments on commit 0be7856

Please sign in to comment.