aboutsummaryrefslogtreecommitdiffstats
path: root/lib/public_key/src/public_key.erl
diff options
context:
space:
mode:
authorHans Nilsson <[email protected]>2017-03-08 12:44:59 +0100
committerHans Nilsson <[email protected]>2017-03-09 15:42:29 +0100
commit123a89c9d6bb1696ee0967c24422a5a8c0961907 (patch)
tree98862a3ad21e369da7c2187e430c0031cfbcf278 /lib/public_key/src/public_key.erl
parentc6270678e0c4297440c2b191ad500b33ed18af5f (diff)
downloadotp-123a89c9d6bb1696ee0967c24422a5a8c0961907.tar.gz
otp-123a89c9d6bb1696ee0967c24422a5a8c0961907.tar.bz2
otp-123a89c9d6bb1696ee0967c24422a5a8c0961907.zip
public_key: Add RSA to public_key:generate_key/1
Diffstat (limited to 'lib/public_key/src/public_key.erl')
-rw-r--r--lib/public_key/src/public_key.erl44
1 files changed, 43 insertions, 1 deletions
diff --git a/lib/public_key/src/public_key.erl b/lib/public_key/src/public_key.erl
index 3d6238d998..21d22d2c94 100644
--- a/lib/public_key/src/public_key.erl
+++ b/lib/public_key/src/public_key.erl
@@ -404,7 +404,49 @@ generate_key(#'DHParameter'{prime = P, base = G}) ->
generate_key({namedCurve, _} = Params) ->
ec_generate_key(Params);
generate_key(#'ECParameters'{} = Params) ->
- ec_generate_key(Params).
+ ec_generate_key(Params);
+generate_key({rsa, ModulusSize, PublicExponent}) ->
+ case crypto:generate_key(rsa, {ModulusSize,PublicExponent}) of
+ {[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)}
+ };
+
+ {[E, N], [E, N, D]} -> % FIXME: what to set the other fields in #'RSAPrivateKey'?
+ % Answer: Miller [Mil76]
+ % G.L. Miller. Riemann's hypothesis and tests for primality.
+ % Journal of Computer and Systems Sciences,
+ % 13(3):300-307,
+ % 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)
+ modulus = Nint,
+ publicExponent = Eint,
+ privateExponent = crypto:bytes_to_integer(D),
+ prime1 = '?',
+ prime2 = '?',
+ exponent1 = '?',
+ exponent2 = '?',
+ coefficient = '?'}
+ };
+
+ Other ->
+ Other
+ end.
%%--------------------------------------------------------------------
-spec compute_key(#'ECPoint'{} , #'ECPrivateKey'{}) -> binary().