/cutkey

An Erlang app for generating RSA keys

Primary LanguageErlangApache License 2.0Apache-2.0

cutkey

License: Apache 2

cutkey is an Erlang app for generating RSA keys. It is implemented as a port driver that calls OpenSSL's RSA_generate_key on a thread from the async pool.

Prerequistes

  • OpenSSL
  • rebar - builds with rebar compile

Example use

1> ok=application:start(cutkey).
ok

cutkey:rsa(Bits,E) returns a key as a list of binary mpints (multi-precision integers) in [E,N,D] form.

2> {ok,PrivateKey}=cutkey:rsa(256,257).
{ok,[<<0,0,0,2,1,1>>,
     <<0,0,0,33,0,184,58,27,70,113,93,46,160,17,179,202,121,155,74,140,105,125,
       164,87,246,48,58,55,175,112,135,102,92,80,15,236,97>>,
     <<0,0,0,32,38,181,140,52,167,66,101,112,83,104,132,49,117,76,109,48,179,
       172,203,180,47,102,200,166,186,111,50,26,47,152,179,57>>]}

This format can be used directly with the crypto apps RSA functions:

3> CipherText=crypto:rsa_private_encrypt(<<"cutkey">>,PrivateKey,rsa_pkcs1_padding).
<<134,120,39,3,213,239,186,21,27,242,10,16,26,102,146,243,4,143,188,238,3,30,
  240,150,19,223,154,74,199,48,154,197>>
4> PublicKey=lists:sublist(PrivateKey,2).
[<<0,0,0,2,1,1>>,
 <<0,0,0,33,0,184,58,27,70,113,93,46,160,17,179,202,121,155,74,140,105,125,
   164,87,246,48,58,55,175,112,135,102,92,80,15,236,97>>]
5> crypto:rsa_public_decrypt(CipherText,PublicKey,rsa_pkcs1_padding).
<<"cutkey">>

To return integers rather than mpints call cutkey:rsa(Bits,E,[erlint]).

6> cutkey:rsa(256,257,[erlint]).
{ok,[257,
     81040612260148359711450770363513587443757830700983826150935466538658883589563,
     31217979041847033507523837610847646524807898528276781830409485449373786718609]}

cutkey:rsa(Bits, E,[{return,full}] returns all the keys component as [E,N,D,P,Q,DMP1,DMQ1,IQMP].

7> cutkey:rsa(256,257,[{return,full}]).
{ok,[<<0,0,0,2,1,1>>,
     <<0,0,0,33,0,240,231,72,86,111,118,146,45,179,63,3,49,72,87,111,86,228,
       131,250,214,124,204,53,53,254,100,219,138,180,58,86,231>>,
     <<0,0,0,33,0,221,55,254,176,253,195,139,54,234,83,195,46,64,112,42,119,
       216,240,109,95,129,1,241,117,42,60,157,132,227,247,251,225>>,
     <<0,0,0,17,0,249,121,72,4,17,181,255,168,37,109,73,224,218,120,106,229>>,
     <<0,0,0,17,0,247,52,155,44,149,235,245,30,135,44,225,201,242,220,1,219>>,
     <<0,0,0,16,1,241,1,142,121,169,194,61,19,55,162,240,208,228,12,201>>,
     <<0,0,0,17,0,133,179,220,100,208,150,135,144,3,97,252,156,10,210,162,95>>,
     <<0,0,0,17,0,149,72,170,189,76,224,117,152,59,238,165,53,76,252,110,233>>]}
8> cutkey:rsa(256,257,[{return,full},erlint]).
{ok,[257,
     109549169109036970922386319245226322238073399290988142385256540168335335659911,
     109122907750636048856540458080848009699522455718273028067445131967597817493249,
     339620574607409222205340586186625001907,
     322563405458201079717610067802464190173,
     2642961670096569822609654367211089509,
     161909257992637896045026065161548173277,
     259850288476005258479681772928406365975]}

An #'RSAPrivateKey'{} for use with the public_key app can be generated by calling cutkey:rsa(Bits,E,[{return,key}]).

9> cutkey:rsa(256,257,[{return,key}]).
{ok,{'RSAPrivateKey','two-prime',
                     67442873603378210235654996389332405831056421752343658837112098839583535920857,
                     257,
                     15482994329180211688341030299496544529189433726742121153918813045597184345585,
                     260334672416336976965392123449277359149,
                     259062202423505986296961754804150480093,
                     193478297398911916733034613147128309717,
                     256038130021675177118397998911495027017,
                     44049718128637397108461906529834191896,asn1_NOVALUE}}