des-0.8.1/.cargo_vcs_info.json 0000644 00000000141 00000000001 0011611 0 ustar {
"git": {
"sha1": "7de364ede310f1ea7080c99ddf1138aeb47f9a69"
},
"path_in_vcs": "des"
} des-0.8.1/CHANGELOG.md 0000644 0000000 0000000 00000002630 00726746425 0012247 0 ustar 0000000 0000000 # 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.8.1 (2022-02-17)
### Fixed
- Minimal versions build ([#303])
[#303]: https://github.com/RustCrypto/block-ciphers/pull/303
## 0.8.0 (2022-02-10)
### Changed
- Bump `cipher` dependency to v0.4 ([#284])
[#284]: https://github.com/RustCrypto/block-ciphers/pull/284
## 0.7.0 (2021-04-29)
### Changed
- Bump `cipher` dependency to v0.3 ([#235])
[#235]: https://github.com/RustCrypto/block-ciphers/pull/235
## 0.6.0 (2020-10-16)
### Changed
- Replace `block-cipher`/`stream-cipher` with `cipher` crate ([#167])
[#167]: https://github.com/RustCrypto/block-ciphers/pull/167
## 0.5.0 (2020-08-07)
### Changed
- Bump `block-cipher` dependency to v0.8 ([#138])
- Bump `opaque-debug` dependency to v0.3 ([#140])
[#138]: https://github.com/RustCrypto/block-ciphers/pull/138
[#140]: https://github.com/RustCrypto/block-ciphers/pull/140
## 0.4.0 (2020-06-08)
### Changed
- Bump `block-cipher` dependency to v0.7 ([#90])
- Upgrade to Rust 2018 edition ([#90])
[#90]: https://github.com/RustCrypto/block-ciphers/pull/90
## 0.3.0 (2018-12-23)
## 0.2.0 (2018-11-14)
## 0.1.0 (2017-11-26)
## 0.0.4 (2017-01-18)
## 0.0.3 (2016-08-27)
## 0.0.2 (2016-08-10)
## 0.0.1 (2016-04-24)
des-0.8.1/Cargo.toml 0000644 00000002147 00000000001 0007617 0 ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
#
# When uploading crates to the registry Cargo will automatically
# "normalize" Cargo.toml files for maximal compatibility
# with all versions of Cargo and also rewrite `path` dependencies
# to registry (e.g., crates.io) dependencies.
#
# If you are reading this file be aware that the original Cargo.toml
# will likely look very different (and much more reasonable).
# See Cargo.toml.orig for the original contents.
[package]
edition = "2021"
rust-version = "1.56"
name = "des"
version = "0.8.1"
authors = ["RustCrypto Developers"]
description = "DES and Triple DES (3DES, TDES) block ciphers implementation"
documentation = "https://docs.rs/des"
readme = "README.md"
keywords = ["crypto", "des", "tdes", "block-cipher"]
categories = ["cryptography", "no-std"]
license = "MIT OR Apache-2.0"
repository = "https://github.com/RustCrypto/block-ciphers"
[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
[dependencies.cipher]
version = "0.4.2"
[dev-dependencies.cipher]
version = "0.4.2"
features = ["dev"]
[features]
zeroize = ["cipher/zeroize"]
des-0.8.1/Cargo.toml.orig 0000644 0000000 0000000 00000001316 00726746425 0013325 0 ustar 0000000 0000000 [package]
name = "des"
version = "0.8.1" # Also update html_root_url in lib.rs when bumping this
description = "DES and Triple DES (3DES, TDES) block ciphers implementation"
authors = ["RustCrypto Developers"]
license = "MIT OR Apache-2.0"
edition = "2021"
rust-version = "1.56"
readme = "README.md"
documentation = "https://docs.rs/des"
repository = "https://github.com/RustCrypto/block-ciphers"
keywords = ["crypto", "des", "tdes", "block-cipher"]
categories = ["cryptography", "no-std"]
[dependencies]
cipher = "0.4.2"
[dev-dependencies]
cipher = { version = "0.4.2", features = ["dev"] }
[features]
zeroize = ["cipher/zeroize"]
[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
des-0.8.1/LICENSE-APACHE 0000644 0000000 0000000 00000025123 00726746425 0012364 0 ustar 0000000 0000000 Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright 2017 Gulshan Singh
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.
des-0.8.1/LICENSE-MIT 0000644 0000000 0000000 00000002100 00726746425 0012062 0 ustar 0000000 0000000 Copyright (c) 2017 Gulshan Singh, Antoni Boucher, Artyom Pavlov
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.
des-0.8.1/README.md 0000644 0000000 0000000 00000005062 00726746425 0011717 0 ustar 0000000 0000000 # RustCrypto: Data Encryption Standard (DES) and 3DES
[![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 [DES cipher][1], including triple DES (3DES).
[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.56** 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/des.svg
[crate-link]: https://crates.io/crates/des
[docs-image]: https://docs.rs/des/badge.svg
[docs-link]: https://docs.rs/des/
[license-image]: https://img.shields.io/badge/license-Apache2.0/MIT-blue.svg
[rustc-image]: https://img.shields.io/badge/rustc-1.56+-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/des/badge.svg?branch=master&event=push
[build-link]: https://github.com/RustCrypto/block-ciphers/actions?query=workflow%3Ades
[//]: # (general links)
[1]: https://en.wikipedia.org/wiki/Data_Encryption_Standard
des-0.8.1/benches/mod.rs 0000644 0000000 0000000 00000000742 00726746425 0013174 0 ustar 0000000 0000000 #![feature(test)]
extern crate test;
use cipher::{block_decryptor_bench, block_encryptor_bench};
use des::{Des, TdesEde3};
block_encryptor_bench!(Key: Des, des_encrypt_block, des_encrypt_blocks);
block_decryptor_bench!(Key: Des, des_decrypt_block, des_decrypt_blocks);
block_encryptor_bench!(
Key: TdesEde3,
tdes_ede3_encrypt_block,
tdes_ede3_encrypt_blocks,
);
block_decryptor_bench!(
Key: TdesEde3,
tdes_ede3_decrypt_block,
tdes_ede3_decrypt_blocks,
);
des-0.8.1/src/consts.rs 0000644 0000000 0000000 00000005647 00726746425 0013117 0 ustar 0000000 0000000 pub const SHIFTS: [u8; 16] = [1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1];
// These boxes are not the same ones that appear in the literature. Normally,
// the first and the last bits of the six input bits are used to choose the row
// and the middle four bits are used to choose the column. These sboxes are
// rearranged so that the bottom four bits choose the column and the top two
// bits choose the row. In other words, we can directly index the sbox array
// with the 6 input bits to get the correct value.
#[rustfmt::skip]
pub const SBOXES: [[u8; 64]; 8] = [
[
14, 0, 4, 15, 13, 7, 1, 4, 2, 14, 15, 2, 11, 13, 8, 1,
3, 10, 10, 6, 6, 12, 12, 11, 5, 9, 9, 5, 0, 3, 7, 8,
4, 15, 1, 12, 14, 8, 8, 2, 13, 4, 6, 9, 2, 1, 11, 7,
15, 5, 12, 11, 9, 3, 7, 14, 3, 10, 10, 0, 5, 6, 0, 13,
],
[
15, 3, 1, 13, 8, 4, 14, 7, 6, 15, 11, 2, 3, 8, 4, 14,
9, 12, 7, 0, 2, 1, 13, 10, 12, 6, 0, 9, 5, 11, 10, 5,
0, 13, 14, 8, 7, 10, 11, 1, 10, 3, 4, 15, 13, 4, 1, 2,
5, 11, 8, 6, 12, 7, 6, 12, 9, 0, 3, 5, 2, 14, 15, 9,
],
[
10, 13, 0, 7, 9, 0, 14, 9, 6, 3, 3, 4, 15, 6, 5, 10,
1, 2, 13, 8, 12, 5, 7, 14, 11, 12, 4, 11, 2, 15, 8, 1,
13, 1, 6, 10, 4, 13, 9, 0, 8, 6, 15, 9, 3, 8, 0, 7,
11, 4, 1, 15, 2, 14, 12, 3, 5, 11, 10, 5, 14, 2, 7, 12,
],
[
7, 13, 13, 8, 14, 11, 3, 5, 0, 6, 6, 15, 9, 0, 10, 3,
1, 4, 2, 7, 8, 2, 5, 12, 11, 1, 12, 10, 4, 14, 15, 9,
10, 3, 6, 15, 9, 0, 0, 6, 12, 10, 11, 1, 7, 13, 13, 8,
15, 9, 1, 4, 3, 5, 14, 11, 5, 12, 2, 7, 8, 2, 4, 14,
],
[
2, 14, 12, 11, 4, 2, 1, 12, 7, 4, 10, 7, 11, 13, 6, 1,
8, 5, 5, 0, 3, 15, 15, 10, 13, 3, 0, 9, 14, 8, 9, 6,
4, 11, 2, 8, 1, 12, 11, 7, 10, 1, 13, 14, 7, 2, 8, 13,
15, 6, 9, 15, 12, 0, 5, 9, 6, 10, 3, 4, 0, 5, 14, 3,
],
[
12, 10, 1, 15, 10, 4, 15, 2, 9, 7, 2, 12, 6, 9, 8, 5,
0, 6, 13, 1, 3, 13, 4, 14, 14, 0, 7, 11, 5, 3, 11, 8,
9, 4, 14, 3, 15, 2, 5, 12, 2, 9, 8, 5, 12, 15, 3, 10,
7, 11, 0, 14, 4, 1, 10, 7, 1, 6, 13, 0, 11, 8, 6, 13,
],
[
4, 13, 11, 0, 2, 11, 14, 7, 15, 4, 0, 9, 8, 1, 13, 10,
3, 14, 12, 3, 9, 5, 7, 12, 5, 2, 10, 15, 6, 8, 1, 6,
1, 6, 4, 11, 11, 13, 13, 8, 12, 1, 3, 4, 7, 10, 14, 7,
10, 9, 15, 5, 6, 0, 8, 15, 0, 14, 5, 2, 9, 3, 2, 12,
],
[
13, 1, 2, 15, 8, 13, 4, 8, 6, 10, 15, 3, 11, 7, 1, 4,
10, 12, 9, 5, 3, 6, 14, 11, 5, 0, 0, 14, 12, 9, 7, 2,
7, 2, 11, 1, 4, 14, 1, 7, 9, 4, 12, 10, 14, 8, 2, 13,
0, 15, 6, 12, 10, 9, 13, 0, 15, 3, 3, 5, 5, 6, 8, 11,
],
];
des-0.8.1/src/des.rs 0000644 0000000 0000000 00000016445 00726746425 0012357 0 ustar 0000000 0000000 //! Data Encryption Standard (DES) block cipher.
#![allow(clippy::unreadable_literal)]
use cipher::{consts::U8, AlgorithmName, BlockCipher, Key, KeyInit, KeySizeUser};
use core::fmt;
#[cfg(feature = "zeroize")]
use cipher::zeroize::{Zeroize, ZeroizeOnDrop};
use crate::consts::{SBOXES, SHIFTS};
/// Data Encryption Standard (DES) block cipher.
#[derive(Clone)]
pub struct Des {
pub(crate) keys: [u64; 16],
}
/// Swap bits in `a` using a delta swap
fn delta_swap(a: u64, delta: u64, mask: u64) -> u64 {
let b = (a ^ (a >> delta)) & mask;
a ^ b ^ (b << delta)
}
/// Swap bits using the PC-1 table
fn pc1(mut key: u64) -> u64 {
key = delta_swap(key, 2, 0x3333000033330000);
key = delta_swap(key, 4, 0x0f0f0f0f00000000);
key = delta_swap(key, 8, 0x009a000a00a200a8);
key = delta_swap(key, 16, 0x00006c6c0000cccc);
key = delta_swap(key, 1, 0x1045500500550550);
key = delta_swap(key, 32, 0x00000000f0f0f5fa);
key = delta_swap(key, 8, 0x00550055006a00aa);
key = delta_swap(key, 2, 0x0000333330000300);
key & 0xFFFFFFFFFFFFFF00
}
/// Swap bits using the PC-2 table
fn pc2(key: u64) -> u64 {
let key = key.rotate_left(61);
let b1 = (key & 0x0021000002000000) >> 7;
let b2 = (key & 0x0008020010080000) << 1;
let b3 = key & 0x0002200000000000;
let b4 = (key & 0x0000000000100020) << 19;
let b5 = (key.rotate_left(54) & 0x0005312400000011).wrapping_mul(0x0000000094200201)
& 0xea40100880000000;
let b6 = (key.rotate_left(7) & 0x0022110000012001).wrapping_mul(0x0001000000610006)
& 0x1185004400000000;
let b7 = (key.rotate_left(6) & 0x0000520040200002).wrapping_mul(0x00000080000000c1)
& 0x0028811000200000;
let b8 = (key & 0x01000004c0011100).wrapping_mul(0x0000000000004284) & 0x0400082244400000;
let b9 = (key.rotate_left(60) & 0x0000000000820280).wrapping_mul(0x0000000000089001)
& 0x0000000110880000;
let b10 = (key.rotate_left(49) & 0x0000000000024084).wrapping_mul(0x0000000002040005)
& 0x000000000a030000;
b1 | b2 | b3 | b4 | b5 | b6 | b7 | b8 | b9 | b10
}
/// Swap bits using the reverse FP table
fn fp(mut message: u64) -> u64 {
message = delta_swap(message, 24, 0x000000FF000000FF);
message = delta_swap(message, 24, 0x00000000FF00FF00);
message = delta_swap(message, 36, 0x000000000F0F0F0F);
message = delta_swap(message, 18, 0x0000333300003333);
delta_swap(message, 9, 0x0055005500550055)
}
/// Swap bits using the IP table
fn ip(mut message: u64) -> u64 {
message = delta_swap(message, 9, 0x0055005500550055);
message = delta_swap(message, 18, 0x0000333300003333);
message = delta_swap(message, 36, 0x000000000F0F0F0F);
message = delta_swap(message, 24, 0x00000000FF00FF00);
delta_swap(message, 24, 0x000000FF000000FF)
}
/// Swap bits using the E table
fn e(block: u64) -> u64 {
const BLOCK_LEN: usize = 32;
const RESULT_LEN: usize = 48;
let b1 = (block << (BLOCK_LEN - 1)) & 0x8000000000000000;
let b2 = (block >> 1) & 0x7C00000000000000;
let b3 = (block >> 3) & 0x03F0000000000000;
let b4 = (block >> 5) & 0x000FC00000000000;
let b5 = (block >> 7) & 0x00003F0000000000;
let b6 = (block >> 9) & 0x000000FC00000000;
let b7 = (block >> 11) & 0x00000003F0000000;
let b8 = (block >> 13) & 0x000000000FC00000;
let b9 = (block >> 15) & 0x00000000003E0000;
let b10 = (block >> (RESULT_LEN - 1)) & 0x0000000000010000;
b1 | b2 | b3 | b4 | b5 | b6 | b7 | b8 | b9 | b10
}
/// Swap bits using the P table
fn p(block: u64) -> u64 {
let block = block.rotate_left(44);
let b1 = (block & 0x0000000000200000) << 32;
let b2 = (block & 0x0000000000480000) << 13;
let b3 = (block & 0x0000088000000000) << 12;
let b4 = (block & 0x0000002020120000) << 25;
let b5 = (block & 0x0000000442000000) << 14;
let b6 = (block & 0x0000000001800000) << 37;
let b7 = (block & 0x0000000004000000) << 24;
let b8 = (block & 0x0000020280015000).wrapping_mul(0x0000020080800083) & 0x02000a6400000000;
let b9 = (block.rotate_left(29) & 0x01001400000000aa).wrapping_mul(0x0000210210008081)
& 0x0902c01200000000;
let b10 = (block & 0x0000000910040000).wrapping_mul(0x0000000c04000020) & 0x8410010000000000;
b1 | b2 | b3 | b4 | b5 | b6 | b7 | b8 | b9 | b10
}
/// Generate the 16 subkeys
pub(crate) fn gen_keys(key: u64) -> [u64; 16] {
let mut keys: [u64; 16] = [0; 16];
let key = pc1(key);
// The most significant bit is bit zero, and there are only 56 bits in
// the key after applying PC1, so we need to remove the eight least
// significant bits from the key.
let key = key >> 8;
let mut c = key >> 28;
let mut d = key & 0x0FFFFFFF;
for i in 0..16 {
c = rotate(c, SHIFTS[i]);
d = rotate(d, SHIFTS[i]);
// We need the `<< 8` because the most significant bit is bit zero,
// so we need to shift our 56 bit value 8 bits to the left.
keys[i] = pc2(((c << 28) | d) << 8);
}
keys
}
/// Performs a left rotate on a 28 bit number
fn rotate(mut val: u64, shift: u8) -> u64 {
let top_bits = val >> (28 - shift);
val <<= shift;
(val | top_bits) & 0x0FFFFFFF
}
fn round(input: u64, key: u64) -> u64 {
let l = input & (0xFFFF_FFFF << 32);
let r = input << 32;
r | ((f(r, key) ^ l) >> 32)
}
fn f(input: u64, key: u64) -> u64 {
let mut val = e(input as u64);
val ^= key;
val = apply_sboxes(val);
p(val)
}
/// Applies all eight sboxes to the input
fn apply_sboxes(input: u64) -> u64 {
let mut output: u64 = 0;
for (i, sbox) in SBOXES.iter().enumerate() {
let val = (input >> (58 - (i * 6))) & 0x3F;
output |= u64::from(sbox[val as usize]) << (60 - (i * 4));
}
output
}
impl Des {
pub(crate) fn encrypt(&self, mut data: u64) -> u64 {
data = ip(data);
for key in &self.keys {
data = round(data, *key);
}
fp((data << 32) | (data >> 32))
}
pub(crate) fn decrypt(&self, mut data: u64) -> u64 {
data = ip(data);
for key in self.keys.iter().rev() {
data = round(data, *key);
}
fp((data << 32) | (data >> 32))
}
}
impl BlockCipher for Des {}
impl KeySizeUser for Des {
type KeySize = U8;
}
impl KeyInit for Des {
#[inline]
fn new(key: &Key) -> Self {
let keys = gen_keys(u64::from_be_bytes(key.clone().into()));
Self { keys }
}
}
impl fmt::Debug for Des {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Des { ... }")
}
}
impl AlgorithmName for Des {
fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Des")
}
}
#[cfg(feature = "zeroize")]
#[cfg_attr(docsrs, doc(cfg(feature = "zeroize")))]
impl Drop for Des {
fn drop(&mut self) {
self.keys.zeroize();
}
}
#[cfg(feature = "zeroize")]
#[cfg_attr(docsrs, doc(cfg(feature = "zeroize")))]
impl ZeroizeOnDrop for Des {}
cipher::impl_simple_block_encdec!(
Des, U8, cipher, block,
encrypt: {
let mut data = u64::from_be_bytes(block.clone_in().into());
data = cipher.encrypt(data);
block.get_out().copy_from_slice(&data.to_be_bytes());
}
decrypt: {
let mut data = u64::from_be_bytes(block.clone_in().into());
data = cipher.decrypt(data);
block.get_out().copy_from_slice(&data.to_be_bytes());
}
);
des-0.8.1/src/lib.rs 0000644 0000000 0000000 00000002135 00726746425 0012341 0 ustar 0000000 0000000 //! Pure Rust implementation of the [Data Encryption Standard][DES] (DES),
//! including [Triple DES] (TDES, 3DES) block ciphers.
//!
//! # ⚠️ Security Warning: Hazmat!
//!
//! This crate implements only the low-level block cipher function, and is intended
//! for use for implementing higher-level constructions *only*. It is NOT
//! intended for direct use in applications.
//!
//! USE AT YOUR OWN RISK!
//!
//! [DES]: https://en.wikipedia.org/wiki/Data_Encryption_Standard
//! [Triple DES]: https://en.wikipedia.org/wiki/Triple_DES
#![no_std]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/26acc39f/logo.svg",
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/26acc39f/logo.svg",
html_root_url = "https://docs.rs/des/0.8.1"
)]
#![deny(unsafe_code)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![warn(missing_docs, rust_2018_idioms)]
#![allow(clippy::clone_on_copy)] // TODO: remove on migration to const generics
pub use cipher;
mod consts;
mod des;
mod tdes;
pub use crate::des::Des;
pub use crate::tdes::{TdesEde2, TdesEde3, TdesEee2, TdesEee3};
des-0.8.1/src/tdes.rs 0000644 0000000 0000000 00000015103 00726746425 0012531 0 ustar 0000000 0000000 //! Triple DES (3DES) block ciphers.
use crate::des::{gen_keys, Des};
use cipher::{
consts::{U16, U24, U8},
AlgorithmName, BlockCipher, Key, KeyInit, KeySizeUser,
};
use core::fmt;
#[cfg(feature = "zeroize")]
use cipher::zeroize::ZeroizeOnDrop;
/// Triple DES (3DES) block cipher.
#[derive(Clone)]
pub struct TdesEde3 {
d1: Des,
d2: Des,
d3: Des,
}
impl BlockCipher for TdesEde3 {}
impl KeySizeUser for TdesEde3 {
type KeySize = U24;
}
impl KeyInit for TdesEde3 {
#[inline]
fn new(key: &Key) -> Self {
let k1 = u64::from_be_bytes(key[0..8].try_into().unwrap());
let k2 = u64::from_be_bytes(key[8..16].try_into().unwrap());
let k3 = u64::from_be_bytes(key[16..24].try_into().unwrap());
let d1 = Des { keys: gen_keys(k1) };
let d2 = Des { keys: gen_keys(k2) };
let d3 = Des { keys: gen_keys(k3) };
Self { d1, d2, d3 }
}
}
impl fmt::Debug for TdesEde3 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("TdesEee3 { ... }")
}
}
impl AlgorithmName for TdesEde3 {
fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("TdesEde3")
}
}
#[cfg(feature = "zeroize")]
#[cfg_attr(docsrs, doc(cfg(feature = "zeroize")))]
impl ZeroizeOnDrop for TdesEde3 {}
cipher::impl_simple_block_encdec!(
TdesEde3, U8, cipher, block,
encrypt: {
let mut data = u64::from_be_bytes(block.clone_in().into());
data = cipher.d1.encrypt(data);
data = cipher.d2.decrypt(data);
data = cipher.d3.encrypt(data);
block.get_out().copy_from_slice(&data.to_be_bytes());
}
decrypt: {
let mut data = u64::from_be_bytes(block.clone_in().into());
data = cipher.d3.decrypt(data);
data = cipher.d2.encrypt(data);
data = cipher.d1.decrypt(data);
block.get_out().copy_from_slice(&data.to_be_bytes());
}
);
/// Triple DES (3DES) block cipher.
#[derive(Clone)]
pub struct TdesEee3 {
d1: Des,
d2: Des,
d3: Des,
}
impl BlockCipher for TdesEee3 {}
impl KeySizeUser for TdesEee3 {
type KeySize = U24;
}
impl KeyInit for TdesEee3 {
#[inline]
fn new(key: &Key) -> Self {
let k1 = u64::from_be_bytes(key[0..8].try_into().unwrap());
let k2 = u64::from_be_bytes(key[8..16].try_into().unwrap());
let k3 = u64::from_be_bytes(key[16..24].try_into().unwrap());
let d1 = Des { keys: gen_keys(k1) };
let d2 = Des { keys: gen_keys(k2) };
let d3 = Des { keys: gen_keys(k3) };
Self { d1, d2, d3 }
}
}
impl fmt::Debug for TdesEee3 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("TdesEee3 { ... }")
}
}
impl AlgorithmName for TdesEee3 {
fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("TdesEee3")
}
}
#[cfg(feature = "zeroize")]
#[cfg_attr(docsrs, doc(cfg(feature = "zeroize")))]
impl ZeroizeOnDrop for TdesEee3 {}
cipher::impl_simple_block_encdec!(
TdesEee3, U8, cipher, block,
encrypt: {
let mut data = u64::from_be_bytes(block.clone_in().into());
data = cipher.d1.encrypt(data);
data = cipher.d2.encrypt(data);
data = cipher.d3.encrypt(data);
block.get_out().copy_from_slice(&data.to_be_bytes());
}
decrypt: {
let mut data = u64::from_be_bytes(block.clone_in().into());
data = cipher.d3.decrypt(data);
data = cipher.d2.decrypt(data);
data = cipher.d1.decrypt(data);
block.get_out().copy_from_slice(&data.to_be_bytes());
}
);
/// Triple DES (3DES) block cipher.
#[derive(Clone)]
pub struct TdesEde2 {
d1: Des,
d2: Des,
}
impl BlockCipher for TdesEde2 {}
impl KeySizeUser for TdesEde2 {
type KeySize = U16;
}
impl KeyInit for TdesEde2 {
#[inline]
fn new(key: &Key) -> Self {
let k1 = u64::from_be_bytes(key[0..8].try_into().unwrap());
let k2 = u64::from_be_bytes(key[8..16].try_into().unwrap());
let d1 = Des { keys: gen_keys(k1) };
let d2 = Des { keys: gen_keys(k2) };
Self { d1, d2 }
}
}
impl fmt::Debug for TdesEde2 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("TdesEde2 { ... }")
}
}
impl AlgorithmName for TdesEde2 {
fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("TdesEde2")
}
}
#[cfg(feature = "zeroize")]
#[cfg_attr(docsrs, doc(cfg(feature = "zeroize")))]
impl ZeroizeOnDrop for TdesEde2 {}
cipher::impl_simple_block_encdec!(
TdesEde2, U8, cipher, block,
encrypt: {
let mut data = u64::from_be_bytes(block.clone_in().into());
data = cipher.d1.encrypt(data);
data = cipher.d2.decrypt(data);
data = cipher.d1.encrypt(data);
block.get_out().copy_from_slice(&data.to_be_bytes());
}
decrypt: {
let mut data = u64::from_be_bytes(block.clone_in().into());
data = cipher.d1.decrypt(data);
data = cipher.d2.encrypt(data);
data = cipher.d1.decrypt(data);
block.get_out().copy_from_slice(&data.to_be_bytes());
}
);
/// Triple DES (3DES) block cipher.
#[derive(Clone)]
pub struct TdesEee2 {
d1: Des,
d2: Des,
}
impl BlockCipher for TdesEee2 {}
impl KeySizeUser for TdesEee2 {
type KeySize = U16;
}
impl KeyInit for TdesEee2 {
#[inline]
fn new(key: &Key) -> Self {
let k1 = u64::from_be_bytes(key[0..8].try_into().unwrap());
let k2 = u64::from_be_bytes(key[8..16].try_into().unwrap());
let d1 = Des { keys: gen_keys(k1) };
let d2 = Des { keys: gen_keys(k2) };
Self { d1, d2 }
}
}
impl fmt::Debug for TdesEee2 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("TdesEee2 { ... }")
}
}
impl AlgorithmName for TdesEee2 {
fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("TdesEee2")
}
}
#[cfg(feature = "zeroize")]
#[cfg_attr(docsrs, doc(cfg(feature = "zeroize")))]
impl ZeroizeOnDrop for TdesEee2 {}
cipher::impl_simple_block_encdec!(
TdesEee2, U8, cipher, block,
encrypt: {
let mut data = u64::from_be_bytes(block.clone_in().into());
data = cipher.d1.encrypt(data);
data = cipher.d2.encrypt(data);
data = cipher.d1.encrypt(data);
block.get_out().copy_from_slice(&data.to_be_bytes());
}
decrypt: {
let mut data = u64::from_be_bytes(block.clone_in().into());
data = cipher.d1.decrypt(data);
data = cipher.d2.decrypt(data);
data = cipher.d1.decrypt(data);
block.get_out().copy_from_slice(&data.to_be_bytes());
}
);
des-0.8.1/tests/data/des.blb 0000644 0000000 0000000 00000027310 00726746425 0013747 0 ustar 0000000 0000000 M#//////// TTTTTTTT33333333 66666666 >>>>>>>> uuuuuuuu````````$$$$$$$$qqqqqqqq%%%%%%%% @ @ vvvvvvvv((((((((;;;;;;;;DDDDDDDDMMMMMMMM 11111111XXXXXXXX!!!!!!!!{{{{{{{{dddddddd }}}}}}}}BBBBBBBBiiiiiiiioooooooo @ ||||||||00000000 SSSSSSSSUUUUUUUUEEEEEEEEAAAAAAAA @nnnnnnnn,,,,,,,,gggggggg -------- [[[[[[[[ WWWWWWWW
cccccccc ~~~~~~~~========99999999 RRRRRRRR LLLLLLLL @ ________HHHHHHHHbbbbbbbb \\\\\\\\ssssssss yyyyyyyy zzzzzzzz @ ######## <<<<<<<< KKKKKKKK @ OOOOOOOO"""""""" xxxxxxxxhhhhhhhhttttttttGGGGGGGG
FFFFFFFF&&&&&&&& eeeeeeee88888888))))))))kkkkkkkk mmmmmmmm 77777777ffffffff4444444455555555 '''''''' ^^^^^^^^pppppppp ........ wwwwwwwwPPPPPPPP CCCCCCCC ++++++++::::::::VVVVVVVV 22222222aaaaaaaa@@@@@@@@ YYYYYYYYJJJJJJJJ IIIIIIIIQQQQQQQQZZZZZZZZ???????? ********jjjjjjjj @ ]]]]]]]] rrrrrrrrllllllllNNNNNNNN"Βpj b
o)^+/ lQ/]ߘ'oTK(XF S%KӈlO1
r:+m-6qZ
MMWlRk@JK̍n]V2#í8ܜ?Y^R}Jn݀ru
Rc]#c%W<drL/dUZRy
,[qUWq8ѫOBRU
'r` gVƬEB1N;&/)W
CV\|aTg
- QӗUkBBP|=Q "3DUfwZ-ߐylG\Mhof>F%_!^ Q{3#)$2(C׀Be,kd|0n`hlioTSo/>dS|_sY>NX+&;2+`H"7t#+Eų 'Rc']j](ke(VCye~͚!?mMI2.SO84!(کMa&]16R?ٙTo,;[[
â9$\<]ہ!!zFr.NĂ
m0ccu&fLF55B$bSS!_HiJEuuiaee˿nR({{&%#==v,@ޝ$S][cAA0$MMN>pnQQpPƶAAry$×~7<FRvPjfkkCii7ee`Pi{nbII[6_/2EE&CݻSS8Wl6T쨽yyn GÀ ??KgS''lܯ?g$!!?a@--uYY{45WWdFv kkʍgssS71yyC$\Ѓ99z<;kk HۨH&0##
--y32+រZXSSxV<4L'''({uccѾt7l}}
II$i's˟'##8p;1]'xMc}}6гgWՁ99bs9
;;3NRY1__F'?GGcdo:++⫗+yy3/;aaFG2:p33͖X)^?55 {Sj//"Crj ;;W[҃??v)pOObK$|4ssIY2==rh55OSmD
ZyvL5ƃ##'¶CC7{|廳QQ561_aHuuMKgUU-FyL=77ǐ׃//ShsۇpY0KNJ));Shā[[>O>n,ssp82Z XBXww"4I;<G>1Ts33#RQQ;`^ 33ʿGMMMJ IIhᴂ_a++ %-myyϻ|WWu #{==w⊤d''V*VIfAA&?,ϱ [[
r8ǂWWeʃ))LKHnGG { ڂmmw<''E,B$i==pɱJ<.4%CChۖ));ҒqqYom__Ȅ\DLMM0k,{{i2؇Ta:}}"
@>z%%?Idoo*([ÃSS~
(%SSR77&j?k5EEL&ggJ"1u8]]On0NՀ Xnu[Zuu'__/0Dl@JOO=le[[>UaK}R/X߂MM! BY5@jEpwUii`(s_7UUܭC8R8{{4Ϋ)QQ,)Iaa}z*Pgg0qB{1a;;;{bs^e^(bX_mm9g+$xc828o-(EETLGGVK7u~{99PObΠ@Y9+PwKJ*Zkk%
X6oo?QPՅiiQO7ۃs77`6>EE\Lw-mUݼGG߄UrNs2imuuŏmm5&ĀCCRIThAggRB$HY㛐ł D?%%c\h;;dq+
tmm/L*HH(qqǏZ}}ۖ،4`ssli U%%w-@BWqq
&.AȓAAjOׄ
\e5Ka??U9.B
S@]AA;ae+a
'qqr?|'$W)L'oo@dzn{CC!|vmm5o8ۣ/III'E__n5r\UU:b{_##GhtKK~ |)#P]))vIBTww\f,)`VW]>ώِan
!))faA]&ZZ?:*&ET&}USK!s#+jLV_dm8&QNժ,