Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix curve25519 endianness #89

Merged
merged 3 commits into from
Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ SPDX-License-Identifier: CC0-1.0

- Fix the length of the Digital signature counter DO 0x93 ([#76][])
- PSO:CDS: Increment the signature counter ([#78][])
- Fix endianness of curve25519 key import([#89][])

[#64]: https://github.com/Nitrokey/opcard-rs/pull/64
[#60]: https://github.com/Nitrokey/opcard-rs/pull/60
[#63]: https://github.com/Nitrokey/opcard-rs/pull/63
[#76]: https://github.com/Nitrokey/opcard-rs/pull/76
[#78]: https://github.com/Nitrokey/opcard-rs/pull/78
[#89]: https://github.com/Nitrokey/opcard-rs/pull/89

## v0.1.0 (2022-10-12)

Expand Down
20 changes: 19 additions & 1 deletion src/command/private_key_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,27 @@ fn put_ec<const R: usize, T: trussed::Client>(
Status::IncorrectDataParameter
})?;

// GPG stores scalars as big endian when X25519 specifies them to be little endian
// See https://lists.gnupg.org/pipermail/gnupg-devel/2018-February/033437.html
let mut data: [u8; 32];
let message;
if matches!(curve, CurveAlgo::X255) {
data = private_key_data.try_into().map_err(|_| {
warn!(
"Bad private key length for x25519: {}",
private_key_data.len()
);
Status::IncorrectDataParameter
})?;
data.reverse();
message = data.as_slice();
} else {
message = private_key_data;
}

let key = try_syscall!(ctx.backend.client_mut().unsafe_inject_key(
curve.mechanism(),
private_key_data,
message,
Location::Internal,
KeySerialization::Raw
))
Expand Down
29 changes: 18 additions & 11 deletions tests/crypto-gpg-import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,17 +187,24 @@ fn gpg_255() {
let custom2 = format!(r"{temp_name} \(no comment\) <{temp_email}>");
gnupg_test(
&[DEFAULT_PW1],
&[vec![
r"\[GNUPG:\] ENC_TO [a-fA-F0-9]{16} \d* \d*",
r"\[GNUPG:\] DECRYPTION_KEY [a-fA-F0-9]{40} [a-fA-F0-9]{40} u",
r"\[GNUPG:\] BEGIN_DECRYPTION",
r"\[GNUPG:\] DECRYPTION_INFO \d \d \d",
r"\[GNUPG:\] PLAINTEXT \d* \d* Cargo.toml",
r"\[GNUPG:\] PLAINTEXT_LENGTH \d*",
r"\[GNUPG:\] DECRYPTION_OKAY",
r"\[GNUPG:\] GOODMDC",
r"\[GNUPG:\] END_DECRYPTION",
]]
&[
vec![
r"\[GNUPG:\] ENC_TO [a-fA-F0-9]{16} \d* \d*",
&custom1,
r"\[GNUPG:\] NEED_PASSPHRASE [a-fA-F0-9]{16} [a-fA-F0-9]{16} 18 0",
],
virt::gpg_inquire_pin(),
vec![
r"\[GNUPG:\] DECRYPTION_KEY [a-fA-F0-9]{40} [a-fA-F0-9]{40} u",
r"\[GNUPG:\] BEGIN_DECRYPTION",
r"\[GNUPG:\] DECRYPTION_INFO \d \d \d",
r"\[GNUPG:\] PLAINTEXT \d* \d* Cargo.toml",
r"\[GNUPG:\] PLAINTEXT_LENGTH \d*",
r"\[GNUPG:\] DECRYPTION_OKAY",
r"\[GNUPG:\] GOODMDC",
r"\[GNUPG:\] END_DECRYPTION",
],
]
.into_iter()
.flatten()
.collect::<Vec<&str>>(),
Expand Down