Skip to content

Commit

Permalink
public_key: Public RSA key is present in private key
Browse files Browse the repository at this point in the history
Change the return value from {#'RSAPublicKey'{}, #'RSAPrivateKey'{}} to #'RSAPrivateKey'{}
This conforms to the #'ECPrivateKey'{} return value.

Note that DH key will be returned as {Public::integer(), Private::integer()}
as there is no key structure (record) only two integers.

Maybe we would like to add extraction functions for the public key from
the private ones later.
  • Loading branch information
IngelaAndin committed Apr 12, 2017
1 parent 4da32ec commit f606520
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 23 deletions.
5 changes: 3 additions & 2 deletions lib/public_key/doc/src/public_key.xml
Original file line number Diff line number Diff line change
Expand Up @@ -331,14 +331,15 @@
</func>

<func>
<name>generate_key(Params) -> {Public::binary(), Private::binary()} | #'ECPrivateKey'{} | {#'RSAPublicKey'{}, #'RSAPrivateKey'{}}</name>
<name>generate_key(Params) -> {Public::binary(), Private::binary()} | #'ECPrivateKey'{} | #'RSAPrivateKey'{}</name>
<fsummary>Generates a new keypair.</fsummary>
<type>
<v>Params = #'DHParameter'{} | {namedCurve, oid()} | #'ECParameters'{}
| {rsa, Size::integer(), PubExp::integer} </v>
</type>
<desc>
<p>Generates a new keypair. See also
<p>Generates a new keypair. Note that except for Diffie-Hellman
the public key is included in the private key structure. See also
<seealso marker="crypto:crypto#generate_key/2">crypto:generate_key/2</seealso>
</p>
</desc>
Expand Down
32 changes: 13 additions & 19 deletions lib/public_key/src/public_key.erl
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ dh_gex_group(Min, N, Max, Groups) ->
(#'ECParameters'{}) ->
#'ECPrivateKey'{};
({rsa, Size::pos_integer(), PubExp::pos_integer()}) ->
{#'RSAPublicKey'{}, #'RSAPrivateKey'{}}.
#'RSAPrivateKey'{}.

%% Description: Generates a new keypair
%%--------------------------------------------------------------------
Expand All @@ -417,18 +417,15 @@ generate_key({rsa, ModulusSize, PublicExponent}) ->
{[E, N], [E, N, D, P, Q, D_mod_P_1, D_mod_Q_1, InvQ_mod_P]} ->
Nint = crypto:bytes_to_integer(N),
Eint = crypto:bytes_to_integer(E),
{#'RSAPublicKey'{modulus = Nint,
publicExponent = Eint},
#'RSAPrivateKey'{version = 0, % Two-factor (I guess since otherPrimeInfos is not given)
modulus = Nint,
publicExponent = Eint,
privateExponent = crypto:bytes_to_integer(D),
prime1 = crypto:bytes_to_integer(P),
prime2 = crypto:bytes_to_integer(Q),
exponent1 = crypto:bytes_to_integer(D_mod_P_1),
exponent2 = crypto:bytes_to_integer(D_mod_Q_1),
coefficient = crypto:bytes_to_integer(InvQ_mod_P)}
};
#'RSAPrivateKey'{version = 0, % Two-factor (I guess since otherPrimeInfos is not given)
modulus = Nint,
publicExponent = Eint,
privateExponent = crypto:bytes_to_integer(D),
prime1 = crypto:bytes_to_integer(P),
prime2 = crypto:bytes_to_integer(Q),
exponent1 = crypto:bytes_to_integer(D_mod_P_1),
exponent2 = crypto:bytes_to_integer(D_mod_Q_1),
coefficient = crypto:bytes_to_integer(InvQ_mod_P)};

{[E, N], [E, N, D]} -> % FIXME: what to set the other fields in #'RSAPrivateKey'?
% Answer: Miller [Mil76]
Expand All @@ -438,19 +435,16 @@ generate_key({rsa, ModulusSize, PublicExponent}) ->
% 1976.
Nint = crypto:bytes_to_integer(N),
Eint = crypto:bytes_to_integer(E),
{#'RSAPublicKey'{modulus = Nint,
publicExponent = Eint},
#'RSAPrivateKey'{version = 0, % Two-factor (I guess since otherPrimeInfos is not given)
#'RSAPrivateKey'{version = 0, % Two-factor (I guess since otherPrimeInfos is not given)
modulus = Nint,
publicExponent = Eint,
privateExponent = crypto:bytes_to_integer(D),
prime1 = '?',
prime2 = '?',
exponent1 = '?',
exponent2 = '?',
coefficient = '?'}
};

coefficient = '?'};

Other ->
Other
end.
Expand Down
2 changes: 0 additions & 2 deletions lib/public_key/test/erl_make_certs.erl
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,6 @@ gen_rsa2(Size) ->
%% The numbers 2048,17 is choosen to not cause the cryptolib on
%% FIPS-enabled test machines be mad at us.
public_key:generate_key({rsa, 2048, 17})
of
{_Public, Private} -> Private
catch
error:notsup ->
%% Disabled dirty_schedulers => crypto:generate_key not working
Expand Down

0 comments on commit f606520

Please sign in to comment.