Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
dup-crypto-rs
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
libs
dup-crypto-rs
Commits
9cd41756
Commit
9cd41756
authored
5 years ago
by
Éloïs
Browse files
Options
Downloads
Patches
Plain Diff
[feat] private_message: return sender public key
parent
358766b3
No related branches found
No related tags found
1 merge request
!11
Elois/pm
Pipeline
#8324
passed
5 years ago
Stage: fmt
Stage: tests
Stage: quality
Changes
2
Pipelines
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/private_message.rs
+35
-11
35 additions, 11 deletions
src/private_message.rs
src/private_message/authentication.rs
+2
-2
2 additions, 2 deletions
src/private_message/authentication.rs
with
37 additions
and
13 deletions
src/private_message.rs
+
35
−
11
View file @
9cd41756
...
...
@@ -83,7 +83,7 @@
//!
//! ```
//! use dup_crypto::keys::{KeyPair, ed25519::KeyPairFromSeed32Generator};
//! use dup_crypto::private_message::{Aad, Algorithm};
//! use dup_crypto::private_message::{Aad, Algorithm
, DecryptedMessage
};
//! use dup_crypto::seeds::Seed32;
//!
//! let receiver_key_pair = KeyPairFromSeed32Generator::generate(
...
...
@@ -103,7 +103,8 @@
//! # 247, 151, 148, 197, 162, 88, 254, 185, 149, 108, 2, 137, 139, 66, 82, 168, 213, 118, 218, 188, 238,
//! # 147, 89, 156];
//!
//! let (message, signature_opt) = dup_crypto::private_message::decrypt_private_message(
//! let DecryptedMessage { message, sender_public_key, signature_opt } =
//! dup_crypto::private_message::decrypt_private_message(
//! Aad::from(b"service name - currency name"),
//! Algorithm::Chacha20Poly1305,
//! &mut encrypted_message,
...
...
@@ -114,6 +115,10 @@
//! message,
//! &b"This is a secret message, which can only be read by the recipient."[..],
//! );
//! assert_eq!{
//! "4HbjoXtWu9C2Q5LMu1RcWHS66k4dnvHspBxKWagFG5rJ",
//! &sender_public_key.to_string(),
//! }
//! assert_eq!(
//! signature_opt,
//! None
...
...
@@ -289,6 +294,16 @@ where
Ok
(())
}
/// Decrypted message
pub
struct
DecryptedMessage
<
'm
>
{
/// decrypted message content
pub
message
:
&
'm
[
u8
],
/// Sender public key
pub
sender_public_key
:
Ed25519PublicKey
,
/// Optional signature
pub
signature_opt
:
Option
<
Signature
>
,
}
/// Decrypt private message.
/// Return a reference to decrypted bytes and an optional signature.
/// If the authentication method chosen by the sender is `Signature`,
...
...
@@ -299,7 +314,7 @@ pub fn decrypt_private_message<'m, A: AsRef<[u8]>>(
algorithm
:
Algorithm
,
encrypted_message
:
&
'm
mut
[
u8
],
receiver_key_pair
:
&
Ed25519KeyPair
,
)
->
Result
<
(
&
'm
[
u8
],
Option
<
Signature
>
)
,
PrivateMessageError
>
{
)
->
Result
<
DecryptedMessage
<
'm
>
,
PrivateMessageError
>
{
// Get ephemeral public key
let
len
=
encrypted_message
.len
();
let
ephemeral_public_key
=
&
encrypted_message
[(
len
-
EPHEMERAL_PUBLIC_KEY_LEN
)
..
];
...
...
@@ -326,13 +341,17 @@ pub fn decrypt_private_message<'m, A: AsRef<[u8]>>(
let
tag_len
=
algorithm
.to_ring_algo
()
.tag_len
();
let
authent_end
=
len
-
EPHEMERAL_PUBLIC_KEY_LEN
-
tag_len
;
let
authent_begin
=
authent_end
-
AUTHENTICATION_DATAS_LEN
;
let
sig_opt
=
verify_authentication_proof
(
let
(
sender_public_key
,
sig_opt
)
=
verify_authentication_proof
(
receiver_key_pair
,
&
encrypted_message
[
..
authent_begin
],
&
encrypted_message
[
authent_begin
..
authent_end
],
)
?
;
Ok
((
&
encrypted_message
[
..
authent_begin
],
sig_opt
))
Ok
(
DecryptedMessage
{
message
:
&
encrypted_message
[
..
authent_begin
],
sender_public_key
,
signature_opt
:
sig_opt
,
})
}
fn
generate_symmetric_key_and_nonce
(
...
...
@@ -483,7 +502,11 @@ mod tests {
println!
(
"encrypted message={:?}"
,
encrypted_message
);
let
(
decrypted_message
,
sig_opt
)
=
decrypt_private_message
(
let
DecryptedMessage
{
message
:
decrypted_message
,
sender_public_key
,
signature_opt
,
}
=
decrypt_private_message
(
Aad
::
from
(
AAD
),
Algorithm
::
Chacha20Poly1305
,
&
mut
encrypted_message
,
...
...
@@ -493,7 +516,8 @@ mod tests {
println!
(
"decrypted message={:?}"
,
decrypted_message
);
assert_eq!
(
decrypted_message
,
message
);
assert_eq!
(
sig_opt
,
None
);
assert_eq!
(
sender_public_key
,
sender_key_pair
.public_key
());
assert_eq!
(
signature_opt
,
None
);
Ok
(())
}
...
...
This diff is collapsed.
Click to expand it.
src/private_message/authentication.rs
+
2
−
2
View file @
9cd41756
...
...
@@ -114,7 +114,7 @@ pub(crate) fn verify_authentication_proof(
receiver_key_pair
:
&
Ed25519KeyPair
,
message
:
&
[
u8
],
authentication_datas
:
&
[
u8
],
)
->
Result
<
Option
<
Signature
>
,
PrivateMessageError
>
{
)
->
Result
<
(
Ed25519PublicKey
,
Option
<
Signature
>
)
,
PrivateMessageError
>
{
let
sender_public_key
=
Ed25519PublicKey
::
try_from
(
&
authentication_datas
[
..
SENDER_PUBLIC_KEY_LEN
])
.map_err
(
PrivateMessageError
::
InvalidSenderPubkey
)
?
;
...
...
@@ -151,7 +151,7 @@ pub(crate) fn verify_authentication_proof(
.map_err
(|
_
|
PrivateMessageError
::
InvalidAuthenticationProof
)
?
;
}
}
Ok
(
signature_opt
)
Ok
(
(
sender_public_key
,
signature_opt
)
)
}
#[cfg(test)]
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment