Skip to content

Commit f314c4a

Browse files
authored
Merge pull request #3 from jpbland1/main
Switch to javascript classes, organize tests, implement sha
2 parents 4039839 + 5a6a935 commit f314c4a

35 files changed

+3240
-217
lines changed

README.md

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,38 @@ Use npm to install and build:
2525
```
2626
npm i
2727
npm run build
28-
npm run tsrun
28+
node app.js
2929
```
3030

3131
## Example Output
3232

3333
```
34-
$ npm run tsrun
35-
> [email protected] tsrun /home/davidgarske/GitHub/wolfcrypt_nodejs
36-
> npx tsc main.ts && node main.js
37-
38-
PASS
34+
$ node app.js
35+
PASS evp encrypt
36+
PASS evp decrypt
37+
PASS evp encrypt_decrypt_odd
38+
PASS evp encryptionStream
39+
PASS evp decryptionStream
40+
PASS evp encryptDecryptPipes
41+
PASS hmac hmac
42+
PASS hmac hmacStream
43+
PASS hmac hmacPipe
44+
PASS rsa keyToDer
45+
PASS rsa keyToPublicDer
46+
PASS rsa privateKeyDecode
47+
PASS rsa publicKeyDecode
48+
PASS rsa encryptDecrypt
49+
PASS rsa signVerify
50+
PASS sha sha
51+
PASS sha sha224
52+
PASS sha sha256
53+
PASS sha sha384
54+
PASS sha sha512
55+
PASS sha sha512_224
56+
PASS sha sha512_256
57+
PASS ecc makeKey
58+
PASS ecc sharedSecret32
59+
PASS ecc sharedSecret64
60+
PASS ecc signVerify
61+
PASS ecc importExport
3962
```

WolfSSLEVP.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
/* WolfSSLEVP.js
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+
122
"use strict";
223
var __extends = (this && this.__extends) || (function () {
324
var extendStatics = function (d, b) {

WolfSSLEVP.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
/* WolfSSLEVP.ts
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+
122
const wolfcrypt = require( './build/Release/wolfcrypt' );
223
const stream = require( 'stream' );
324

WolfSSLHmac.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
/* WolfSSLHmac.js
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+
*/
121
"use strict";
222
var __extends = (this && this.__extends) || (function () {
323
var extendStatics = function (d, b) {

WolfSSLHmac.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
/* WolfSSLHmac.ts
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+
*/
121
const wolfcrypt = require( './build/Release/wolfcrypt' );
222
const stream = require( 'stream' );
323

addon/wolfcrypt/ecc.cpp

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
/* ecc.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/ecc.h"
22+
23+
Napi::Number sizeof_ecc_key(const Napi::CallbackInfo& info)
24+
{
25+
Napi::Env env = info.Env();
26+
27+
return Napi::Number::New( env, sizeof( ecc_key ) );
28+
}
29+
30+
Napi::Number sizeof_ecc_point(const Napi::CallbackInfo& info)
31+
{
32+
Napi::Env env = info.Env();
33+
34+
return Napi::Number::New( env, sizeof( ecc_point ) );
35+
}
36+
37+
Napi::Number bind_wc_ecc_size(const Napi::CallbackInfo& info)
38+
{
39+
int ret;
40+
Napi::Env env = info.Env();
41+
ecc_key* ecc = (ecc_key*)( info[0].As<Napi::Uint8Array>().Data() );
42+
43+
ret = wc_ecc_size( ecc );
44+
45+
return Napi::Number::New( env, ret );
46+
}
47+
48+
Napi::Number bind_wc_ecc_init(const Napi::CallbackInfo& info)
49+
{
50+
int ret;
51+
Napi::Env env = info.Env();
52+
ecc_key* ecc = (ecc_key*)( info[0].As<Napi::Uint8Array>().Data() );
53+
54+
ecc->rng = NULL;
55+
ret = wc_ecc_init( ecc );
56+
57+
return Napi::Number::New( env, ret );
58+
}
59+
60+
Napi::Number bind_wc_ecc_make_key(const Napi::CallbackInfo& info)
61+
{
62+
Napi::Env env = info.Env();
63+
int ret;
64+
int key_size = info[0].As<Napi::Number>().Int32Value();
65+
ecc_key* ecc = (ecc_key*)( info[1].As<Napi::Uint8Array>().Data() );
66+
67+
ecc->rng = wc_rng_new( NULL, 0, NULL );
68+
69+
ret = wc_ecc_make_key( ecc->rng, key_size, ecc );
70+
71+
return Napi::Number::New( env, ret );
72+
}
73+
74+
Napi::Number sizeof_ecc_x963(const Napi::CallbackInfo& info)
75+
{
76+
Napi::Env env = info.Env();
77+
ecc_key* ecc = (ecc_key*)( info[0].As<Napi::Uint8Array>().Data() );
78+
unsigned int out_len;
79+
80+
wc_ecc_export_x963( ecc, NULL, &out_len );
81+
82+
return Napi::Number::New( env, out_len );
83+
}
84+
85+
Napi::Number bind_wc_ecc_export_x963(const Napi::CallbackInfo& info)
86+
{
87+
Napi::Env env = info.Env();
88+
int ret;
89+
ecc_key* ecc = (ecc_key*)( info[0].As<Napi::Uint8Array>().Data() );
90+
uint8_t* out = (uint8_t*)( info[1].As<Napi::Uint8Array>().Data() );
91+
unsigned int out_len = info[2].As<Napi::Number>().Int32Value();
92+
93+
ret = wc_ecc_export_x963( ecc, out, &out_len );
94+
95+
if ( ret < 0 )
96+
{
97+
out_len = ret;
98+
}
99+
100+
return Napi::Number::New( env, (int)out_len );
101+
}
102+
103+
Napi::Number bind_wc_ecc_import_x963(const Napi::CallbackInfo& info)
104+
{
105+
Napi::Env env = info.Env();
106+
int ret;
107+
uint8_t* in = (uint8_t*)( info[0].As<Napi::Uint8Array>().Data() );
108+
unsigned int in_len = info[1].As<Napi::Number>().Int32Value();
109+
ecc_key* ecc = (ecc_key*)( info[2].As<Napi::Uint8Array>().Data() );
110+
111+
ret = wc_ecc_import_x963( in, in_len, ecc );
112+
113+
return Napi::Number::New( env, ret );
114+
}
115+
116+
Napi::Number bind_wc_ecc_set_curve(const Napi::CallbackInfo& info)
117+
{
118+
Napi::Env env = info.Env();
119+
int ret;
120+
ecc_key* ecc = (ecc_key*)( info[0].As<Napi::Uint8Array>().Data() );
121+
int key_size = info[1].As<Napi::Number>().Int32Value();
122+
int curve_id = info[2].As<Napi::Number>().Int32Value();
123+
124+
ret = wc_ecc_set_curve( ecc, key_size, curve_id );
125+
126+
return Napi::Number::New( env, ret );
127+
}
128+
129+
Napi::Number bind_wc_ecc_shared_secret(const Napi::CallbackInfo& info)
130+
{
131+
Napi::Env env = info.Env();
132+
int ret;
133+
ecc_key* private_key = (ecc_key*)( info[0].As<Napi::Uint8Array>().Data() );
134+
ecc_key* public_key = (ecc_key*)( info[1].As<Napi::Uint8Array>().Data() );
135+
uint8_t* out = info[2].As<Napi::Uint8Array>().Data();
136+
unsigned int out_len = info[3].As<Napi::Number>().Uint32Value();
137+
138+
ret = wc_ecc_shared_secret( private_key, public_key, out, &out_len );
139+
140+
if ( ret < 0 )
141+
{
142+
out_len = ret;
143+
}
144+
145+
return Napi::Number::New( env, (int)out_len );
146+
}
147+
148+
Napi::Number bind_wc_ecc_sig_size(const Napi::CallbackInfo& info)
149+
{
150+
Napi::Env env = info.Env();
151+
int ret;
152+
ecc_key* ecc = (ecc_key*)( info[0].As<Napi::Uint8Array>().Data() );
153+
154+
ret = wc_ecc_sig_size( ecc );
155+
156+
return Napi::Number::New( env, ret );
157+
}
158+
159+
Napi::Number bind_wc_ecc_sign_hash(const Napi::CallbackInfo& info)
160+
{
161+
Napi::Env env = info.Env();
162+
int ret;
163+
WC_RNG rng;
164+
uint8_t* in = (uint8_t*)( info[0].As<Napi::Uint8Array>().Data() );
165+
int in_len = info[1].As<Napi::Number>().Int32Value();
166+
uint8_t* out = (uint8_t*)( info[2].As<Napi::Uint8Array>().Data() );
167+
unsigned int out_len = info[3].As<Napi::Number>().Int32Value();
168+
ecc_key* ecc = (ecc_key*)( info[4].As<Napi::Uint8Array>().Data() );
169+
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+
}
175+
176+
if ( ret < 0 )
177+
{
178+
out_len = ret;
179+
}
180+
181+
return Napi::Number::New( env, (int)out_len );
182+
}
183+
184+
Napi::Number bind_wc_ecc_verify_hash(const Napi::CallbackInfo& info)
185+
{
186+
Napi::Env env = info.Env();
187+
int ret;
188+
uint8_t* sig = (uint8_t*)( info[0].As<Napi::Uint8Array>().Data() );
189+
int sig_len = info[1].As<Napi::Number>().Int32Value();
190+
uint8_t* hash = (uint8_t*)( info[2].As<Napi::Uint8Array>().Data() );
191+
int hash_len = info[3].As<Napi::Number>().Int32Value();
192+
ecc_key* ecc = (ecc_key*)( info[4].As<Napi::Uint8Array>().Data() );
193+
int res;
194+
195+
ret = wc_ecc_verify_hash( sig, sig_len, hash, hash_len, &res, ecc );
196+
if ( ret < 0 )
197+
{
198+
res = ret;
199+
}
200+
201+
return Napi::Number::New( env, res );
202+
}
203+
204+
Napi::Number bind_wc_ecc_free(const Napi::CallbackInfo& info)
205+
{
206+
Napi::Env env = info.Env();
207+
int ret;
208+
ecc_key* ecc = (ecc_key*)( info[0].As<Napi::Uint8Array>().Data() );
209+
210+
if ( ecc->rng != NULL )
211+
{
212+
wc_rng_free( ecc->rng );
213+
}
214+
215+
ret = wc_ecc_free( ecc );
216+
217+
return Napi::Number::New( env, ret );
218+
}
Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
1+
/* evp.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+
*/
121
#include "./h/evp.h"
222

3-
Napi::Number sizeof_EVP_CIPHER_CTX(const Napi::CallbackInfo& info)
4-
{
5-
Napi::Env env = info.Env();
6-
7-
return Napi::Number::New( env, sizeof( EVP_CIPHER_CTX ) );
8-
}
9-
1023
Napi::Value bind_EVP_CIPHER_CTX_new(const Napi::CallbackInfo& info)
1124
{
1225
Napi::Env env = info.Env();

0 commit comments

Comments
 (0)