Skip to content

Commit 87d2b88

Browse files
author
John Bland
committed
Merge branch 'main' into pkcs7
2 parents 5e611ed + d57fc88 commit 87d2b88

File tree

12 files changed

+386
-14
lines changed

12 files changed

+386
-14
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,7 @@ PASS ecc makeKey
5858
PASS ecc sharedSecret32
5959
PASS ecc sharedSecret64
6060
PASS ecc signVerify
61-
PASS ecc importExport
61+
PASS ecc importExportx963
62+
PASS ecc importExportDer
63+
PASS pbkdf2
6264
```

addon/wolfcrypt/ecc.cpp

Lines changed: 81 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,10 @@ Napi::Number bind_wc_ecc_init(const Napi::CallbackInfo& info)
5151
Napi::Env env = info.Env();
5252
ecc_key* ecc = (ecc_key*)( info[0].As<Napi::Uint8Array>().Data() );
5353

54-
ecc->rng = NULL;
5554
ret = wc_ecc_init( ecc );
5655

56+
ecc->rng = wc_rng_new( NULL, 0, NULL );
57+
5758
return Napi::Number::New( env, ret );
5859
}
5960

@@ -64,8 +65,6 @@ Napi::Number bind_wc_ecc_make_key(const Napi::CallbackInfo& info)
6465
int key_size = info[0].As<Napi::Number>().Int32Value();
6566
ecc_key* ecc = (ecc_key*)( info[1].As<Napi::Uint8Array>().Data() );
6667

67-
ecc->rng = wc_rng_new( NULL, 0, NULL );
68-
6968
ret = wc_ecc_make_key( ecc->rng, key_size, ecc );
7069

7170
return Napi::Number::New( env, ret );
@@ -113,6 +112,84 @@ Napi::Number bind_wc_ecc_import_x963(const Napi::CallbackInfo& info)
113112
return Napi::Number::New( env, ret );
114113
}
115114

115+
Napi::Number bind_wc_EccKeyDerSize(const Napi::CallbackInfo& info)
116+
{
117+
Napi::Env env = info.Env();
118+
int ret;
119+
ecc_key* ecc = (ecc_key*)( info[0].As<Napi::Uint8Array>().Data() );
120+
int pub = info[1].As<Napi::Number>().Int32Value();
121+
122+
ret = wc_EccKeyDerSize( ecc, pub );
123+
124+
return Napi::Number::New( env, ret );
125+
}
126+
127+
Napi::Number bind_wc_EccPublicKeyDerSize(const Napi::CallbackInfo& info)
128+
{
129+
Napi::Env env = info.Env();
130+
int ret;
131+
ecc_key* ecc = (ecc_key*)( info[0].As<Napi::Uint8Array>().Data() );
132+
133+
ret = wc_EccPublicKeyDerSize( ecc, 1 );
134+
135+
return Napi::Number::New( env, ret );
136+
}
137+
138+
Napi::Number bind_wc_EccPublicKeyToDer(const Napi::CallbackInfo& info)
139+
{
140+
Napi::Env env = info.Env();
141+
int ret;
142+
ecc_key* ecc = (ecc_key*)( info[0].As<Napi::Uint8Array>().Data() );
143+
uint8_t* out = (uint8_t*)( info[1].As<Napi::Uint8Array>().Data() );
144+
unsigned int out_len = info[2].As<Napi::Number>().Int32Value();
145+
146+
/* 1=export with ASN.1/DER header (which includes curve info) */
147+
ret = wc_EccPublicKeyToDer( ecc, out, out_len, 1 );
148+
149+
return Napi::Number::New( env, ret );
150+
}
151+
152+
Napi::Number bind_wc_EccPublicKeyDecode(const Napi::CallbackInfo& info)
153+
{
154+
Napi::Env env = info.Env();
155+
int ret;
156+
uint8_t* in = (uint8_t*)( info[0].As<Napi::Uint8Array>().Data() );
157+
ecc_key* ecc = (ecc_key*)( info[1].As<Napi::Uint8Array>().Data() );
158+
unsigned int in_len = info[2].As<Napi::Number>().Int32Value();
159+
unsigned int idx = 0;
160+
161+
ret = wc_EccPublicKeyDecode( in, &idx, ecc, in_len );
162+
163+
return Napi::Number::New( env, ret );
164+
}
165+
166+
Napi::Number bind_wc_EccPrivateKeyToDer(const Napi::CallbackInfo& info)
167+
{
168+
Napi::Env env = info.Env();
169+
int ret;
170+
ecc_key* ecc = (ecc_key*)( info[0].As<Napi::Uint8Array>().Data() );
171+
uint8_t* out = (uint8_t*)( info[1].As<Napi::Uint8Array>().Data() );
172+
unsigned int out_len = info[2].As<Napi::Number>().Int32Value();
173+
174+
ret = wc_EccPrivateKeyToDer( ecc, out, out_len );
175+
176+
return Napi::Number::New( env, ret );
177+
}
178+
179+
Napi::Number bind_wc_EccPrivateKeyDecode(const Napi::CallbackInfo& info)
180+
{
181+
Napi::Env env = info.Env();
182+
int ret;
183+
uint8_t* in = (uint8_t*)( info[0].As<Napi::Uint8Array>().Data() );
184+
ecc_key* ecc = (ecc_key*)( info[1].As<Napi::Uint8Array>().Data() );
185+
unsigned int in_len = info[2].As<Napi::Number>().Int32Value();
186+
unsigned int idx = 0;
187+
188+
ret = wc_EccPrivateKeyDecode( in, &idx, ecc, in_len );
189+
190+
return Napi::Number::New( env, ret );
191+
}
192+
116193
Napi::Number bind_wc_ecc_set_curve(const Napi::CallbackInfo& info)
117194
{
118195
Napi::Env env = info.Env();
@@ -160,18 +237,13 @@ Napi::Number bind_wc_ecc_sign_hash(const Napi::CallbackInfo& info)
160237
{
161238
Napi::Env env = info.Env();
162239
int ret;
163-
WC_RNG rng;
164240
uint8_t* in = (uint8_t*)( info[0].As<Napi::Uint8Array>().Data() );
165241
int in_len = info[1].As<Napi::Number>().Int32Value();
166242
uint8_t* out = (uint8_t*)( info[2].As<Napi::Uint8Array>().Data() );
167243
unsigned int out_len = info[3].As<Napi::Number>().Int32Value();
168244
ecc_key* ecc = (ecc_key*)( info[4].As<Napi::Uint8Array>().Data() );
169245

170-
ret = wc_InitRng( &rng );
171-
if (ret == 0)
172-
{
173-
ret = wc_ecc_sign_hash( in, in_len, out, &out_len, &rng, ecc );
174-
}
246+
ret = wc_ecc_sign_hash( in, in_len, out, &out_len, ecc->rng, ecc );
175247

176248
if ( ret < 0 )
177249
{

addon/wolfcrypt/h/ecc.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "wolfssl/options.h"
2323
#include <wolfssl/wolfcrypt/settings.h>
2424
#include <wolfssl/wolfcrypt/ecc.h>
25+
#include <wolfssl/wolfcrypt/asn.h>
2526

2627
Napi::Number sizeof_ecc_key(const Napi::CallbackInfo& info);
2728
Napi::Number sizeof_ecc_point(const Napi::CallbackInfo& info);
@@ -31,6 +32,12 @@ Napi::Number bind_wc_ecc_make_key(const Napi::CallbackInfo& info);
3132
Napi::Number sizeof_ecc_x963(const Napi::CallbackInfo& info);
3233
Napi::Number bind_wc_ecc_export_x963(const Napi::CallbackInfo& info);
3334
Napi::Number bind_wc_ecc_import_x963(const Napi::CallbackInfo& info);
35+
Napi::Number bind_wc_EccKeyDerSize(const Napi::CallbackInfo& info);
36+
Napi::Number bind_wc_EccPublicKeyToDer(const Napi::CallbackInfo& info);
37+
Napi::Number bind_wc_EccPublicKeyDecode(const Napi::CallbackInfo& info);
38+
Napi::Number bind_wc_EccPublicKeyDerSize(const Napi::CallbackInfo& info);
39+
Napi::Number bind_wc_EccPrivateKeyToDer(const Napi::CallbackInfo& info);
40+
Napi::Number bind_wc_EccPrivateKeyDecode(const Napi::CallbackInfo& info);
3441
Napi::Number bind_wc_ecc_set_curve(const Napi::CallbackInfo& info);
3542
Napi::Number bind_wc_ecc_shared_secret(const Napi::CallbackInfo& info);
3643
Napi::Number bind_wc_ecc_sig_size(const Napi::CallbackInfo& info);

addon/wolfcrypt/h/pbkdf2.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* pbkdf2.h
2+
*
3+
* Copyright (C) 2006-2022 wolfSSL Inc.
4+
*
5+
* This file is part of wolfSSL.
6+
*
7+
* wolfSSL is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation; either version 2 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* wolfSSL is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20+
*/
21+
#include <napi.h>
22+
#include "wolfssl/options.h"
23+
#include <wolfssl/wolfcrypt/settings.h>
24+
#include <wolfssl/wolfcrypt/pwdbased.h>
25+
26+
Napi::Number bind_wc_PBKDF2(const Napi::CallbackInfo& info);

addon/wolfcrypt/main.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "./h/rsa.h"
2727
#include "./h/sha.h"
2828
#include "./h/ecc.h"
29+
#include "./h/pbkdf2.h"
2930
#include "./h/pkcs7.h"
3031

3132
using namespace Napi;
@@ -107,13 +108,21 @@ Napi::Object Init(Napi::Env env, Napi::Object exports)
107108
exports.Set(Napi::String::New(env, "sizeof_ecc_x963"), Napi::Function::New(env, sizeof_ecc_x963));
108109
exports.Set(Napi::String::New(env, "wc_ecc_export_x963"), Napi::Function::New(env, bind_wc_ecc_export_x963));
109110
exports.Set(Napi::String::New(env, "wc_ecc_import_x963"), Napi::Function::New(env, bind_wc_ecc_import_x963));
111+
exports.Set(Napi::String::New(env, "wc_EccKeyDerSize"), Napi::Function::New(env, bind_wc_EccKeyDerSize));
112+
exports.Set(Napi::String::New(env, "wc_EccPublicKeyToDer"), Napi::Function::New(env, bind_wc_EccPublicKeyToDer));
113+
exports.Set(Napi::String::New(env, "wc_EccPublicKeyDecode"), Napi::Function::New(env, bind_wc_EccPublicKeyDecode));
114+
exports.Set(Napi::String::New(env, "wc_EccPublicKeyDerSize"), Napi::Function::New(env, bind_wc_EccPublicKeyDerSize));
115+
exports.Set(Napi::String::New(env, "wc_EccPrivateKeyToDer"), Napi::Function::New(env, bind_wc_EccPrivateKeyToDer));
116+
exports.Set(Napi::String::New(env, "wc_EccPrivateKeyDecode"), Napi::Function::New(env, bind_wc_EccPrivateKeyDecode));
110117
exports.Set(Napi::String::New(env, "wc_ecc_set_curve"), Napi::Function::New(env, bind_wc_ecc_set_curve));
111118
exports.Set(Napi::String::New(env, "wc_ecc_shared_secret"), Napi::Function::New(env, bind_wc_ecc_shared_secret));
112119
exports.Set(Napi::String::New(env, "wc_ecc_sig_size"), Napi::Function::New(env, bind_wc_ecc_sig_size));
113120
exports.Set(Napi::String::New(env, "wc_ecc_sign_hash"), Napi::Function::New(env, bind_wc_ecc_sign_hash));
114121
exports.Set(Napi::String::New(env, "wc_ecc_verify_hash"), Napi::Function::New(env, bind_wc_ecc_verify_hash));
115122
exports.Set(Napi::String::New(env, "wc_ecc_free"), Napi::Function::New(env, bind_wc_ecc_free));
116123

124+
exports.Set(Napi::String::New(env, "wc_PBKDF2"), Napi::Function::New(env, bind_wc_PBKDF2));
125+
117126
exports.Set(Napi::String::New(env, "sizeof_PKCS7"), Napi::Function::New(env, sizeof_PKCS7));
118127
exports.Set(Napi::String::New(env, "typeof_Key_Sum"), Napi::Function::New(env, typeof_Key_Sum));
119128
exports.Set(Napi::String::New(env, "typeof_Hash_Sum"), Napi::Function::New(env, typeof_Hash_Sum));

addon/wolfcrypt/pbkdf2.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* pbkdf2.cpp
2+
*
3+
* Copyright (C) 2006-2022 wolfSSL Inc.
4+
*
5+
* This file is part of wolfSSL.
6+
*
7+
* wolfSSL is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation; either version 2 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* wolfSSL is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20+
*/
21+
#include "./h/pbkdf2.h"
22+
23+
Napi::Number bind_wc_PBKDF2(const Napi::CallbackInfo& info)
24+
{
25+
Napi::Env env = info.Env();
26+
int ret;
27+
uint8_t* out = info[0].As<Napi::Uint8Array>().Data();
28+
uint8_t* passwd = info[1].As<Napi::Uint8Array>().Data();
29+
int p_len = info[2].As<Napi::Number>().Int32Value();
30+
uint8_t* salt = info[3].As<Napi::Uint8Array>().Data();
31+
int s_len = info[4].As<Napi::Number>().Int32Value();
32+
int iterations = info[5].As<Napi::Number>().Int32Value();
33+
int k_len = info[6].As<Napi::Number>().Int32Value();
34+
int type_h = info[7].As<Napi::Number>().Int32Value();
35+
36+
ret = wc_PBKDF2( out, passwd, p_len, salt, s_len, iterations, k_len, type_h );
37+
38+
return Napi::Number::New( env, ret );
39+
}

app.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const hmac_tests = require( './tests/hmac' );
2323
const rsa_tests = require( './tests/rsa' );
2424
const sha_tests = require( './tests/sha' );
2525
const ecc_tests = require( './tests/ecc' );
26+
const pbkdf2_tests = require( './tests/pbkdf2' );
2627
const pkcs7_tests = require( './tests/pkcs7' );
2728

2829
(async function() {
@@ -51,6 +52,11 @@ const pkcs7_tests = require( './tests/pkcs7' );
5152
await ecc_tests[key]()
5253
}
5354

55+
for ( const key of Object.keys( pbkdf2_tests ) )
56+
{
57+
await pbkdf2_tests[key]()
58+
}
59+
5460
for ( const key of Object.keys( pkcs7_tests ) )
5561
{
5662
await pkcs7_tests[key]()

binding.gyp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"addon/wolfcrypt/rsa.cpp",
1111
"addon/wolfcrypt/sha.cpp",
1212
"addon/wolfcrypt/ecc.cpp",
13+
"addon/wolfcrypt/pbkdf2.cpp",
1314
"addon/wolfcrypt/pkcs7.cpp"
1415
],
1516
'include_dirs': [

interfaces/ecc.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,84 @@ class WolfSSLEcc
9191
}
9292
}
9393

94+
PublicKeyToDer()
95+
{
96+
if ( this.ecc == null )
97+
{
98+
throw 'Ecc not allocated'
99+
}
100+
101+
let derBuf = Buffer.alloc( wolfcrypt.wc_EccPublicKeyDerSize( this.ecc ) )
102+
103+
let ret = wolfcrypt.wc_EccPublicKeyToDer( this.ecc, derBuf, derBuf.length )
104+
105+
if ( ret <= 0 )
106+
{
107+
throw `Failed to wc_EccPublicKeyToDer ${ ret }`
108+
}
109+
110+
return derBuf
111+
}
112+
113+
PublicKeyDecode( derBuf )
114+
{
115+
if ( this.ecc == null )
116+
{
117+
throw 'Ecc not allocated'
118+
}
119+
120+
if ( !Buffer.isBuffer( derBuf ) )
121+
{
122+
throw 'Public key der must be a Buffer'
123+
}
124+
125+
let ret = wolfcrypt.wc_EccPublicKeyDecode( derBuf, this.ecc, derBuf.length )
126+
127+
if ( ret != 0 )
128+
{
129+
throw `Failed to wc_EccPublicKeyDecode ${ ret }`
130+
}
131+
}
132+
133+
PrivateKeyToDer()
134+
{
135+
if ( this.ecc == null )
136+
{
137+
throw 'Ecc not allocated'
138+
}
139+
140+
let derBuf = Buffer.alloc( wolfcrypt.wc_EccKeyDerSize( this.ecc, 0 ) )
141+
142+
let ret = wolfcrypt.wc_EccPrivateKeyToDer( this.ecc, derBuf, derBuf.length )
143+
144+
if ( ret <= 0 )
145+
{
146+
throw `Failed to wc_EccPrivateKeyToDer ${ ret }`
147+
}
148+
149+
return derBuf
150+
}
151+
152+
PrivateKeyDecode( derBuf )
153+
{
154+
if ( this.ecc == null )
155+
{
156+
throw 'Ecc not allocated'
157+
}
158+
159+
if ( !Buffer.isBuffer( derBuf ) )
160+
{
161+
throw 'Private key der must be a Buffer'
162+
}
163+
164+
let ret = wolfcrypt.wc_EccPrivateKeyDecode( derBuf, this.ecc, derBuf.length )
165+
166+
if ( ret != 0 )
167+
{
168+
throw `Failed to wc_EccPrivateKeyDecode ${ ret }`
169+
}
170+
}
171+
94172
set_curve( keySize, curveId )
95173
{
96174
if ( this.ecc == null )

0 commit comments

Comments
 (0)