Skip to content

Commit eb9d1da

Browse files
author
John Bland
committed
switch to using function calls to determine the size of signatures and key export buffers
ecc_key_size is a new convenience function that returns the size of the ecc key, it is used in shared_secret to determine the size of the secret buffer sizeof_ecc_x963 is a new convenience function that returns the size the exported ecc key will be, just calls wc_ecc_export_x963 with NULL and returns the length it gets back RsaPrivateDerSize is a new convenience function that returns the size the exported private key will be, just calls wc_RsaKeyToDer with NULL RsaPublicDerSize is a new convenience function that returns the size the exported public key will be, just calls wc_RsaKeyToPublicDer with NULL
1 parent 193c317 commit eb9d1da

File tree

8 files changed

+79
-30
lines changed

8 files changed

+79
-30
lines changed

addon/wolfcrypt/ecc.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ Napi::Number sizeof_ecc_point(const Napi::CallbackInfo& info)
3434
return Napi::Number::New( env, sizeof( ecc_point ) );
3535
}
3636

37+
Napi::Number ecc_key_size(const Napi::CallbackInfo& info)
38+
{
39+
Napi::Env env = info.Env();
40+
ecc_key* ecc = (ecc_key*)( info[0].As<Napi::Uint8Array>().Data() );
41+
42+
return Napi::Number::New( env, ecc->dp->size );
43+
}
44+
3745
Napi::Number bind_wc_ecc_init(const Napi::CallbackInfo& info)
3846
{
3947
int ret;
@@ -60,6 +68,17 @@ Napi::Number bind_wc_ecc_make_key(const Napi::CallbackInfo& info)
6068
return Napi::Number::New( env, ret );
6169
}
6270

71+
Napi::Number sizeof_ecc_x963(const Napi::CallbackInfo& info)
72+
{
73+
Napi::Env env = info.Env();
74+
ecc_key* ecc = (ecc_key*)( info[0].As<Napi::Uint8Array>().Data() );
75+
unsigned int out_len;
76+
77+
wc_ecc_export_x963( ecc, NULL, &out_len );
78+
79+
return Napi::Number::New( env, out_len );
80+
}
81+
6382
Napi::Number bind_wc_ecc_export_x963(const Napi::CallbackInfo& info)
6483
{
6584
Napi::Env env = info.Env();

addon/wolfcrypt/h/ecc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@
2525

2626
Napi::Number sizeof_ecc_key(const Napi::CallbackInfo& info);
2727
Napi::Number sizeof_ecc_point(const Napi::CallbackInfo& info);
28+
Napi::Number ecc_key_size(const Napi::CallbackInfo& info);
2829
Napi::Number bind_wc_ecc_init(const Napi::CallbackInfo& info);
2930
Napi::Number bind_wc_ecc_make_key(const Napi::CallbackInfo& info);
31+
Napi::Number sizeof_ecc_x963(const Napi::CallbackInfo& info);
3032
Napi::Number bind_wc_ecc_export_x963(const Napi::CallbackInfo& info);
3133
Napi::Number bind_wc_ecc_import_x963(const Napi::CallbackInfo& info);
3234
Napi::Number bind_wc_ecc_set_curve(const Napi::CallbackInfo& info);

addon/wolfcrypt/h/rsa.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ Napi::Number sizeof_RsaKey(const Napi::CallbackInfo& info);
2828
Napi::Number bind_wc_RsaEncryptSize(const Napi::CallbackInfo& info);
2929
Napi::Number bind_wc_InitRsaKey(const Napi::CallbackInfo& info);
3030
Napi::Number bind_wc_MakeRsaKey(const Napi::CallbackInfo& info);
31+
Napi::Number RsaPrivateDerSize(const Napi::CallbackInfo& info);
3132
Napi::Number bind_wc_RsaKeyToDer(const Napi::CallbackInfo& info);
33+
Napi::Number RsaPublicDerSize(const Napi::CallbackInfo& info);
3234
Napi::Number bind_wc_RsaKeyToPublicDer(const Napi::CallbackInfo& info);
3335
Napi::Number bind_wc_RsaPrivateKeyDecode(const Napi::CallbackInfo& info);
3436
Napi::Number bind_wc_RsaPublicKeyDecode(const Napi::CallbackInfo& info);

addon/wolfcrypt/main.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ Napi::Object Init(Napi::Env env, Napi::Object exports)
4949
exports.Set(Napi::String::New(env, "wc_RsaEncryptSize"), Napi::Function::New(env, bind_wc_RsaEncryptSize));
5050
exports.Set(Napi::String::New(env, "wc_InitRsaKey"), Napi::Function::New(env, bind_wc_InitRsaKey));
5151
exports.Set(Napi::String::New(env, "wc_MakeRsaKey"), Napi::Function::New(env, bind_wc_MakeRsaKey));
52+
exports.Set(Napi::String::New(env, "RsaPrivateDerSize"), Napi::Function::New(env, RsaPrivateDerSize));
5253
exports.Set(Napi::String::New(env, "wc_RsaKeyToDer"), Napi::Function::New(env, bind_wc_RsaKeyToDer));
54+
exports.Set(Napi::String::New(env, "RsaPublicDerSize"), Napi::Function::New(env, RsaPublicDerSize));
5355
exports.Set(Napi::String::New(env, "wc_RsaKeyToPublicDer"), Napi::Function::New(env, bind_wc_RsaKeyToPublicDer));
5456
exports.Set(Napi::String::New(env, "wc_RsaPrivateKeyDecode"), Napi::Function::New(env, bind_wc_RsaPrivateKeyDecode));
5557
exports.Set(Napi::String::New(env, "wc_RsaPublicKeyDecode"), Napi::Function::New(env, bind_wc_RsaPublicKeyDecode));
@@ -98,8 +100,10 @@ Napi::Object Init(Napi::Env env, Napi::Object exports)
98100

99101
exports.Set(Napi::String::New(env, "sizeof_ecc_key"), Napi::Function::New(env, sizeof_ecc_key));
100102
exports.Set(Napi::String::New(env, "sizeof_ecc_point"), Napi::Function::New(env, sizeof_ecc_point));
103+
exports.Set(Napi::String::New(env, "ecc_key_size"), Napi::Function::New(env, ecc_key_size));
101104
exports.Set(Napi::String::New(env, "wc_ecc_init"), Napi::Function::New(env, bind_wc_ecc_init));
102105
exports.Set(Napi::String::New(env, "wc_ecc_make_key"), Napi::Function::New(env, bind_wc_ecc_make_key));
106+
exports.Set(Napi::String::New(env, "sizeof_ecc_x963"), Napi::Function::New(env, sizeof_ecc_x963));
103107
exports.Set(Napi::String::New(env, "wc_ecc_export_x963"), Napi::Function::New(env, bind_wc_ecc_export_x963));
104108
exports.Set(Napi::String::New(env, "wc_ecc_import_x963"), Napi::Function::New(env, bind_wc_ecc_import_x963));
105109
exports.Set(Napi::String::New(env, "wc_ecc_set_curve"), Napi::Function::New(env, bind_wc_ecc_set_curve));

addon/wolfcrypt/rsa.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,17 @@ Napi::Number bind_wc_MakeRsaKey(const Napi::CallbackInfo& info)
6464
return Napi::Number::New( env, ret );
6565
}
6666

67+
Napi::Number RsaPrivateDerSize(const Napi::CallbackInfo& info)
68+
{
69+
int ret;
70+
Napi::Env env = info.Env();
71+
RsaKey* rsa = (RsaKey*)( info[0].As<Napi::Uint8Array>().Data() );
72+
73+
ret = wc_RsaKeyToDer( rsa, NULL, 0 );
74+
75+
return Napi::Number::New( env, ret );
76+
}
77+
6778
Napi::Number bind_wc_RsaKeyToDer(const Napi::CallbackInfo& info)
6879
{
6980
int ret;
@@ -77,6 +88,17 @@ Napi::Number bind_wc_RsaKeyToDer(const Napi::CallbackInfo& info)
7788
return Napi::Number::New( env, ret );
7889
}
7990

91+
Napi::Number RsaPublicDerSize(const Napi::CallbackInfo& info)
92+
{
93+
int ret;
94+
Napi::Env env = info.Env();
95+
RsaKey* rsa = (RsaKey*)( info[0].As<Napi::Uint8Array>().Data() );
96+
97+
ret = wc_RsaKeyToPublicDer( rsa, NULL, 0 );
98+
99+
return Napi::Number::New( env, ret );
100+
}
101+
80102
Napi::Number bind_wc_RsaKeyToPublicDer(const Napi::CallbackInfo& info)
81103
{
82104
int ret;

interfaces/ecc.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ class WolfSSLEcc
2525
constructor()
2626
{
2727
this.ecc = Buffer.alloc( wolfcrypt.sizeof_ecc_key() )
28-
this.size = -1
2928

3029
let ret = wolfcrypt.wc_ecc_init( this.ecc )
3130

@@ -48,8 +47,6 @@ class WolfSSLEcc
4847
{
4948
throw `Failed to wc_ecc_make_key ${ ret }`
5049
}
51-
52-
this.size = size
5350
}
5451

5552
export_x963()
@@ -59,8 +56,8 @@ class WolfSSLEcc
5956
throw 'Ecc not allocated'
6057
}
6158

62-
// TODO is there a way to know this ahead of time to make sure our asnBuf is big enough
63-
let asnBuf = Buffer.alloc( 2048 )
59+
// passing null will return the size
60+
let asnBuf = Buffer.alloc( wolfcrypt.sizeof_ecc_x963( this.ecc ) )
6461

6562
let ret = wolfcrypt.wc_ecc_export_x963( this.ecc, asnBuf, asnBuf.length )
6663

@@ -116,11 +113,18 @@ class WolfSSLEcc
116113
throw 'Ecc not allocated'
117114
}
118115

119-
let secret = Buffer.alloc( this.size )
116+
const keySize = wolfcrypt.ecc_key_size( this.ecc )
117+
118+
if ( keySize <= 0 )
119+
{
120+
throw `Failed to ecc_key_size ${ keySize }`
121+
}
122+
123+
let secret = Buffer.alloc( keySize )
120124

121-
let ret = wolfcrypt.wc_ecc_shared_secret( this.ecc, pubEcc.ecc, secret, this.size )
125+
let ret = wolfcrypt.wc_ecc_shared_secret( this.ecc, pubEcc.ecc, secret, keySize )
122126

123-
if ( ret != this.size )
127+
if ( ret != keySize )
124128
{
125129
throw `Failed to wc_ecc_shared_secret ${ ret }`
126130
}

interfaces/rsa.js

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,11 @@ class WolfSSLRsa
2525
constructor()
2626
{
2727
this.rsa = Buffer.alloc( wolfcrypt.sizeof_RsaKey() )
28-
this.size = -1
2928
wolfcrypt.wc_InitRsaKey( this.rsa )
3029
}
3130

3231
MakeRsaKey( size, e )
3332
{
34-
this.size = size
35-
3633
let ret = wolfcrypt.wc_MakeRsaKey( this.rsa, size, e )
3734

3835
if ( ret != 0 )
@@ -43,15 +40,14 @@ class WolfSSLRsa
4340

4441
KeyToDer()
4542
{
46-
if ( this.size == -1 || this.rsa == null )
43+
if ( this.rsa == null )
4744
{
4845
throw 'Invalid rsa key'
4946
}
5047

51-
// TODO is there a way to know this ahead of time or shrink afterwards?
52-
let derBuf = Buffer.alloc( this.size )
48+
let derBuf = Buffer.alloc( wolfcrypt.RsaPrivateDerSize( this.rsa ) )
5349

54-
let ret = wolfcrypt.wc_RsaKeyToDer( this.rsa, derBuf, this.size )
50+
let ret = wolfcrypt.wc_RsaKeyToDer( this.rsa, derBuf, derBuf.length )
5551

5652
if ( ret <= 0 )
5753
{
@@ -63,13 +59,12 @@ class WolfSSLRsa
6359

6460
KeyToPublicDer()
6561
{
66-
if ( this.size == -1 || this.rsa == null )
62+
if ( this.rsa == null )
6763
{
6864
throw 'Invalid rsa key'
6965
}
7066

71-
// TODO is there a way to know this ahead of time or shrink afterwards?
72-
let derBuf = Buffer.alloc( this.size )
67+
let derBuf = Buffer.alloc( wolfcrypt.RsaPublicDerSize( this.rsa ) )
7368

7469
let ret = wolfcrypt.wc_RsaKeyToPublicDer( this.rsa, derBuf, derBuf.length )
7570

@@ -81,7 +76,7 @@ class WolfSSLRsa
8176
return derBuf
8277
}
8378

84-
PrivateKeyDecode( derBuf, size )
79+
PrivateKeyDecode( derBuf )
8580
{
8681
if ( this.rsa == null )
8782
{
@@ -99,25 +94,26 @@ class WolfSSLRsa
9994
{
10095
throw `Failed to wc_RsaPrivateKeyDecode ${ ret }`
10196
}
102-
103-
this.size = size
10497
}
10598

106-
PublicKeyDecode( derBuf, size )
99+
PublicKeyDecode( derBuf )
107100
{
108101
if ( this.rsa == null )
109102
{
110103
throw 'Invalid rsa key'
111104
}
112105

106+
if ( !Buffer.isBuffer( derBuf ) )
107+
{
108+
throw 'Public key der must be Buffer'
109+
}
110+
113111
let ret = wolfcrypt.wc_RsaPublicKeyDecode( derBuf, this.rsa, derBuf.length )
114112

115113
if ( ret != 0 )
116114
{
117115
throw `Failed to wc_RsaPublicKeyDecode ${ ret }`
118116
}
119-
120-
this.size = size
121117
}
122118

123119
PublicEncrypt( data )
@@ -184,7 +180,7 @@ class WolfSSLRsa
184180
data = Buffer.from( data )
185181
}
186182

187-
let sig = Buffer.alloc( this.size / 8 )
183+
let sig = Buffer.alloc( wolfcrypt.wc_RsaEncryptSize( this.rsa ) )
188184

189185
let ret = wolfcrypt.wc_RsaSSL_Sign( data, data.length, sig, sig.length, this.rsa )
190186

tests/rsa.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const rsa_tests =
4646
{
4747
let rsa = new WolfSSLRsa()
4848

49-
rsa.PrivateKeyDecode( Buffer.from( privateDerHex, 'hex' ), 2048 )
49+
rsa.PrivateKeyDecode( Buffer.from( privateDerHex, 'hex' ) )
5050

5151
const derHex = rsa.KeyToPublicDer().toString( 'hex' )
5252

@@ -59,7 +59,7 @@ const rsa_tests =
5959
{
6060
let rsa = new WolfSSLRsa()
6161

62-
rsa.PrivateKeyDecode( Buffer.from( privateDerHex, 'hex' ), 2048 )
62+
rsa.PrivateKeyDecode( Buffer.from( privateDerHex, 'hex' ) )
6363

6464
const sigHex = rsa.SSL_Sign( message ).toString( 'hex' )
6565

@@ -79,7 +79,7 @@ const rsa_tests =
7979
{
8080
let rsa = new WolfSSLRsa()
8181

82-
rsa.PublicKeyDecode( Buffer.from( publicDerHex, 'hex' ), 2048 )
82+
rsa.PublicKeyDecode( Buffer.from( publicDerHex, 'hex' ) )
8383

8484
if ( rsa.SSL_Verify( Buffer.from( rsaSigHex, 'hex' ), message ) )
8585
{
@@ -97,7 +97,7 @@ const rsa_tests =
9797
{
9898
let rsa = new WolfSSLRsa()
9999

100-
rsa.PrivateKeyDecode( Buffer.from( privateDerHex, 'hex' ), 2048 )
100+
rsa.PrivateKeyDecode( Buffer.from( privateDerHex, 'hex' ) )
101101

102102
const ciphertext = rsa.PublicEncrypt( message )
103103
const plaintext = rsa.PrivateDecrypt( ciphertext ).toString()
@@ -118,7 +118,7 @@ const rsa_tests =
118118
{
119119
let rsa = new WolfSSLRsa()
120120

121-
rsa.PrivateKeyDecode( Buffer.from( privateDerHex, 'hex' ), 2048 )
121+
rsa.PrivateKeyDecode( Buffer.from( privateDerHex, 'hex' ) )
122122

123123
const sig = rsa.SSL_Sign( message )
124124

0 commit comments

Comments
 (0)