Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
Rust Openssl
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Model registry
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Public Repositories
Rust Openssl
Commits
339d09fb
Commit
339d09fb
authored
Jun 23, 2018
by
Moritz Wanzenböck
Browse files
Options
Downloads
Patches
Plain Diff
Simplify DSA from private components
parent
c624427e
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
openssl/src/dsa.rs
+17
-8
17 additions, 8 deletions
openssl/src/dsa.rs
with
17 additions
and
8 deletions
openssl/src/dsa.rs
+
17
−
8
View file @
339d09fb
...
@@ -12,7 +12,7 @@ use std::fmt;
...
@@ -12,7 +12,7 @@ use std::fmt;
use
std
::
ptr
;
use
std
::
ptr
;
use
std
::
mem
;
use
std
::
mem
;
use
bn
::{
BigNum
,
BigNumRef
,
BigNumContext
};
use
bn
::{
BigNum
,
BigNumRef
};
use
error
::
ErrorStack
;
use
error
::
ErrorStack
;
use
pkey
::{
HasParams
,
HasPrivate
,
HasPublic
,
Private
,
Public
};
use
pkey
::{
HasParams
,
HasPrivate
,
HasPublic
,
Private
,
Public
};
use
{
cvt
,
cvt_p
};
use
{
cvt
,
cvt_p
};
...
@@ -182,18 +182,16 @@ impl Dsa<Private> {
...
@@ -182,18 +182,16 @@ impl Dsa<Private> {
///
///
/// `p`, `q` and `g` are the common parameters.
/// `p`, `q` and `g` are the common parameters.
/// `priv_key` is the private component of the key pair.
/// `priv_key` is the private component of the key pair.
///
The corresponding
public component
is calculated from the private component.
///
`pub_key` is the
public component
of the key. Can be computed via `g^(priv_key) mod p`
pub
fn
from_private_components
(
pub
fn
from_private_components
(
p
:
BigNum
,
p
:
BigNum
,
q
:
BigNum
,
q
:
BigNum
,
g
:
BigNum
,
g
:
BigNum
,
priv_key
:
BigNum
,
priv_key
:
BigNum
,
pub_key
:
BigNum
,
)
->
Result
<
Dsa
<
Private
>
,
ErrorStack
>
{
)
->
Result
<
Dsa
<
Private
>
,
ErrorStack
>
{
ffi
::
init
();
ffi
::
init
();
unsafe
{
unsafe
{
let
mut
bn_ctx
=
BigNumContext
::
new
()
?
;
let
mut
pub_key
=
BigNum
::
new
()
?
;
pub_key
.mod_exp
(
&
g
,
&
priv_key
,
&
p
,
&
mut
bn_ctx
)
?
;
let
dsa
=
Dsa
::
from_ptr
(
cvt_p
(
ffi
::
DSA_new
())
?
);
let
dsa
=
Dsa
::
from_ptr
(
cvt_p
(
ffi
::
DSA_new
())
?
);
cvt
(
DSA_set0_pqg
(
dsa
.0
,
p
.as_ptr
(),
q
.as_ptr
(),
g
.as_ptr
()))
?
;
cvt
(
DSA_set0_pqg
(
dsa
.0
,
p
.as_ptr
(),
q
.as_ptr
(),
g
.as_ptr
()))
?
;
mem
::
forget
((
p
,
q
,
g
));
mem
::
forget
((
p
,
q
,
g
));
...
@@ -322,6 +320,7 @@ cfg_if! {
...
@@ -322,6 +320,7 @@ cfg_if! {
#[cfg(test)]
#[cfg(test)]
mod
test
{
mod
test
{
use
super
::
*
;
use
super
::
*
;
use
bn
::
BigNumContext
;
use
sign
::{
Signer
,
Verifier
};
use
sign
::{
Signer
,
Verifier
};
use
hash
::
MessageDigest
;
use
hash
::
MessageDigest
;
use
pkey
::
PKey
;
use
pkey
::
PKey
;
...
@@ -350,9 +349,14 @@ mod test {
...
@@ -350,9 +349,14 @@ mod test {
let
q
=
BigNum
::
from_u32
(
47
)
.unwrap
();
let
q
=
BigNum
::
from_u32
(
47
)
.unwrap
();
let
g
=
BigNum
::
from_u32
(
60
)
.unwrap
();
let
g
=
BigNum
::
from_u32
(
60
)
.unwrap
();
let
priv_key
=
BigNum
::
from_u32
(
15
)
.unwrap
();
let
priv_key
=
BigNum
::
from_u32
(
15
)
.unwrap
();
let
pub_key
=
BigNum
::
from_u32
(
207
)
.unwrap
();
let
dsa
=
Dsa
::
from_private_components
(
p
,
q
,
g
,
priv_key
)
.unwrap
();
let
dsa
=
Dsa
::
from_private_components
(
p
,
q
,
g
,
priv_key
,
pub_key
)
.unwrap
();
assert_eq!
(
dsa
.pub_key
(),
&
BigNum
::
from_u32
(
207
)
.unwrap
());
assert_eq!
(
dsa
.pub_key
(),
&
BigNum
::
from_u32
(
207
)
.unwrap
());
assert_eq!
(
dsa
.priv_key
(),
&
BigNum
::
from_u32
(
15
)
.unwrap
());
assert_eq!
(
dsa
.p
(),
&
BigNum
::
from_u32
(
283
)
.unwrap
());
assert_eq!
(
dsa
.q
(),
&
BigNum
::
from_u32
(
47
)
.unwrap
());
assert_eq!
(
dsa
.g
(),
&
BigNum
::
from_u32
(
60
)
.unwrap
());
}
}
#[test]
#[test]
...
@@ -362,7 +366,11 @@ mod test {
...
@@ -362,7 +366,11 @@ mod test {
let
g
=
BigNum
::
from_u32
(
60
)
.unwrap
();
let
g
=
BigNum
::
from_u32
(
60
)
.unwrap
();
let
pub_key
=
BigNum
::
from_u32
(
207
)
.unwrap
();
let
pub_key
=
BigNum
::
from_u32
(
207
)
.unwrap
();
Dsa
::
from_private_components
(
p
,
q
,
g
,
pub_key
)
.unwrap
();
let
dsa
=
Dsa
::
from_public_components
(
p
,
q
,
g
,
pub_key
)
.unwrap
();
assert_eq!
(
dsa
.pub_key
(),
&
BigNum
::
from_u32
(
207
)
.unwrap
());
assert_eq!
(
dsa
.p
(),
&
BigNum
::
from_u32
(
283
)
.unwrap
());
assert_eq!
(
dsa
.q
(),
&
BigNum
::
from_u32
(
47
)
.unwrap
());
assert_eq!
(
dsa
.g
(),
&
BigNum
::
from_u32
(
60
)
.unwrap
());
}
}
#[test]
#[test]
...
@@ -381,7 +389,8 @@ mod test {
...
@@ -381,7 +389,8 @@ mod test {
BigNumRef
::
to_owned
(
p
)
.unwrap
(),
BigNumRef
::
to_owned
(
p
)
.unwrap
(),
BigNumRef
::
to_owned
(
q
)
.unwrap
(),
BigNumRef
::
to_owned
(
q
)
.unwrap
(),
BigNumRef
::
to_owned
(
g
)
.unwrap
(),
BigNumRef
::
to_owned
(
g
)
.unwrap
(),
BigNumRef
::
to_owned
(
priv_key
)
.unwrap
())
.unwrap
();
BigNumRef
::
to_owned
(
priv_key
)
.unwrap
(),
BigNumRef
::
to_owned
(
pub_key
)
.unwrap
())
.unwrap
();
let
priv_key
=
PKey
::
from_dsa
(
priv_key
)
.unwrap
();
let
priv_key
=
PKey
::
from_dsa
(
priv_key
)
.unwrap
();
let
pub_key
=
Dsa
::
from_public_components
(
let
pub_key
=
Dsa
::
from_public_components
(
...
...
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