Skip to content

Commit a591e02

Browse files
author
John Bland
committed
add ecc keys bindings and interface, update license header
currently the shared secret, sign/verify and import export functions are tested working, encrypt/decrypt is currently not working, I tried to follow the example in tests/api.c but the result of decrypt still doesn't match the plaintext also added wolfSSL copyright information to the top of files
1 parent 384b7ae commit a591e02

File tree

23 files changed

+1094
-76
lines changed

23 files changed

+1094
-76
lines changed

addon/wolfcrypt/ecc.cpp

Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
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_init(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+
ecc->rng = NULL;
44+
ret = wc_ecc_init( ecc );
45+
46+
return Napi::Number::New( env, ret );
47+
}
48+
49+
Napi::Number bind_wc_ecc_make_key(const Napi::CallbackInfo& info)
50+
{
51+
Napi::Env env = info.Env();
52+
int ret;
53+
int key_size = info[0].As<Napi::Number>().Int32Value();
54+
ecc_key* ecc = (ecc_key*)( info[1].As<Napi::Uint8Array>().Data() );
55+
56+
ecc->rng = wc_rng_new( NULL, 0, NULL );
57+
58+
ret = wc_ecc_make_key( ecc->rng, key_size, ecc );
59+
60+
return Napi::Number::New( env, ret );
61+
}
62+
63+
Napi::Number bind_wc_ecc_export_x963(const Napi::CallbackInfo& info)
64+
{
65+
Napi::Env env = info.Env();
66+
int ret;
67+
ecc_key* ecc = (ecc_key*)( info[0].As<Napi::Uint8Array>().Data() );
68+
uint8_t* out = (uint8_t*)( info[1].As<Napi::Uint8Array>().Data() );
69+
unsigned int out_len = info[2].As<Napi::Number>().Int32Value();
70+
71+
ret = wc_ecc_export_x963( ecc, out, &out_len );
72+
73+
if ( ret < 0 )
74+
{
75+
out_len = ret;
76+
}
77+
78+
return Napi::Number::New( env, (int)out_len );
79+
}
80+
81+
Napi::Number bind_wc_ecc_import_x963(const Napi::CallbackInfo& info)
82+
{
83+
Napi::Env env = info.Env();
84+
int ret;
85+
uint8_t* in = (uint8_t*)( info[0].As<Napi::Uint8Array>().Data() );
86+
unsigned int in_len = info[1].As<Napi::Number>().Int32Value();
87+
ecc_key* ecc = (ecc_key*)( info[2].As<Napi::Uint8Array>().Data() );
88+
89+
ret = wc_ecc_import_x963( in, in_len, ecc );
90+
91+
return Napi::Number::New( env, ret );
92+
}
93+
94+
Napi::Number bind_wc_ecc_set_curve(const Napi::CallbackInfo& info)
95+
{
96+
Napi::Env env = info.Env();
97+
int ret;
98+
ecc_key* ecc = (ecc_key*)( info[0].As<Napi::Uint8Array>().Data() );
99+
int key_size = info[1].As<Napi::Number>().Int32Value();
100+
int curve_id = info[2].As<Napi::Number>().Int32Value();
101+
102+
ret = wc_ecc_set_curve( ecc, key_size, curve_id );
103+
104+
return Napi::Number::New( env, ret );
105+
}
106+
107+
Napi::Number bind_wc_ecc_shared_secret(const Napi::CallbackInfo& info)
108+
{
109+
Napi::Env env = info.Env();
110+
int ret;
111+
ecc_key* private_key = (ecc_key*)( info[0].As<Napi::Uint8Array>().Data() );
112+
ecc_key* public_key = (ecc_key*)( info[1].As<Napi::Uint8Array>().Data() );
113+
uint8_t* out = info[2].As<Napi::Uint8Array>().Data();
114+
unsigned int out_len = info[3].As<Napi::Number>().Uint32Value();
115+
116+
ret = wc_ecc_shared_secret( private_key, public_key, out, &out_len );
117+
118+
if ( ret < 0 )
119+
{
120+
out_len = ret;
121+
}
122+
123+
return Napi::Number::New( env, (int)out_len );
124+
}
125+
126+
Napi::Number bind_wc_ecc_sig_size(const Napi::CallbackInfo& info)
127+
{
128+
Napi::Env env = info.Env();
129+
int ret;
130+
ecc_key* ecc = (ecc_key*)( info[0].As<Napi::Uint8Array>().Data() );
131+
132+
ret = wc_ecc_sig_size( ecc );
133+
134+
return Napi::Number::New( env, ret );
135+
}
136+
137+
Napi::Number bind_wc_ecc_sign_hash(const Napi::CallbackInfo& info)
138+
{
139+
Napi::Env env = info.Env();
140+
int ret;
141+
WC_RNG rng;
142+
uint8_t* in = (uint8_t*)( info[0].As<Napi::Uint8Array>().Data() );
143+
int in_len = info[1].As<Napi::Number>().Int32Value();
144+
uint8_t* out = (uint8_t*)( info[2].As<Napi::Uint8Array>().Data() );
145+
unsigned int out_len = info[3].As<Napi::Number>().Int32Value();
146+
ecc_key* ecc = (ecc_key*)( info[4].As<Napi::Uint8Array>().Data() );
147+
148+
wc_InitRng( &rng );
149+
150+
ret = wc_ecc_sign_hash( in, in_len, out, &out_len, &rng, ecc );
151+
152+
if ( ret < 0 )
153+
{
154+
out_len = ret;
155+
}
156+
157+
return Napi::Number::New( env, (int)out_len );
158+
}
159+
160+
Napi::Number bind_wc_ecc_verify_hash(const Napi::CallbackInfo& info)
161+
{
162+
Napi::Env env = info.Env();
163+
int ret;
164+
uint8_t* sig = (uint8_t*)( info[0].As<Napi::Uint8Array>().Data() );
165+
int sig_len = info[1].As<Napi::Number>().Int32Value();
166+
uint8_t* hash = (uint8_t*)( info[2].As<Napi::Uint8Array>().Data() );
167+
int hash_len = info[3].As<Napi::Number>().Int32Value();
168+
ecc_key* ecc = (ecc_key*)( info[4].As<Napi::Uint8Array>().Data() );
169+
int res;
170+
171+
ret = wc_ecc_verify_hash( sig, sig_len, hash, hash_len, &res, ecc );
172+
173+
if ( ret < 0 )
174+
{
175+
res = ret;
176+
}
177+
178+
return Napi::Number::New( env, res );
179+
}
180+
181+
Napi::Number bind_wc_ecc_encrypt(const Napi::CallbackInfo& info)
182+
{
183+
int ret;
184+
Napi::Env env = info.Env();
185+
ecc_key* private_key = (ecc_key*)( info[0].As<Napi::Uint8Array>().Data() );
186+
ecc_key* public_key = (ecc_key*)( info[1].As<Napi::Uint8Array>().Data() );
187+
uint8_t* msg = info[2].As<Napi::Uint8Array>().Data();
188+
unsigned int msg_len = info[3].As<Napi::Number>().Uint32Value();
189+
uint8_t* out = info[4].As<Napi::Uint8Array>().Data();
190+
unsigned int out_len = info[5].As<Napi::Number>().Uint32Value();
191+
192+
ret = wc_ecc_encrypt( private_key, public_key, msg, msg_len, out, &out_len, NULL );
193+
194+
if ( ret < 0 )
195+
{
196+
out_len = ret;
197+
}
198+
199+
return Napi::Number::New( env, (int)out_len );
200+
}
201+
202+
Napi::Number bind_wc_ecc_decrypt(const Napi::CallbackInfo& info)
203+
{
204+
int ret;
205+
Napi::Env env = info.Env();
206+
ecc_key* private_key = (ecc_key*)( info[0].As<Napi::Uint8Array>().Data() );
207+
ecc_key* public_key = (ecc_key*)( info[1].As<Napi::Uint8Array>().Data() );
208+
uint8_t* msg = info[2].As<Napi::Uint8Array>().Data();
209+
unsigned int msg_len = info[3].As<Napi::Number>().Uint32Value();
210+
uint8_t* out = info[4].As<Napi::Uint8Array>().Data();
211+
unsigned int out_len = info[5].As<Napi::Number>().Uint32Value();
212+
213+
ret = wc_ecc_decrypt( private_key, public_key, msg, msg_len, out, &out_len, NULL );
214+
215+
if ( ret < 0 )
216+
{
217+
out_len = ret;
218+
}
219+
220+
return Napi::Number::New( env, (int)out_len );
221+
}
222+
223+
Napi::Number bind_wc_ecc_free(const Napi::CallbackInfo& info)
224+
{
225+
Napi::Env env = info.Env();
226+
int ret;
227+
ecc_key* ecc = (ecc_key*)( info[0].As<Napi::Uint8Array>().Data() );
228+
229+
if ( ecc->rng != NULL )
230+
{
231+
wc_rng_free( ecc->rng );
232+
}
233+
234+
ret = wc_ecc_free( ecc );
235+
236+
return Napi::Number::New( env, ret );
237+
}

addon/wolfcrypt/evp.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
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

323
Napi::Value bind_EVP_CIPHER_CTX_new(const Napi::CallbackInfo& info)

addon/wolfcrypt/h/ecc.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* ecc.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/ecc.h>
25+
26+
Napi::Number sizeof_ecc_key(const Napi::CallbackInfo& info);
27+
Napi::Number sizeof_ecc_point(const Napi::CallbackInfo& info);
28+
Napi::Number bind_wc_ecc_init(const Napi::CallbackInfo& info);
29+
Napi::Number bind_wc_ecc_make_key(const Napi::CallbackInfo& info);
30+
Napi::Number bind_wc_ecc_export_x963(const Napi::CallbackInfo& info);
31+
Napi::Number bind_wc_ecc_import_x963(const Napi::CallbackInfo& info);
32+
Napi::Number bind_wc_ecc_set_curve(const Napi::CallbackInfo& info);
33+
Napi::Number bind_wc_ecc_shared_secret(const Napi::CallbackInfo& info);
34+
Napi::Number bind_wc_ecc_sig_size(const Napi::CallbackInfo& info);
35+
Napi::Number bind_wc_ecc_sign_hash(const Napi::CallbackInfo& info);
36+
Napi::Number bind_wc_ecc_verify_hash(const Napi::CallbackInfo& info);
37+
Napi::Number bind_wc_ecc_encrypt(const Napi::CallbackInfo& info);
38+
Napi::Number bind_wc_ecc_decrypt(const Napi::CallbackInfo& info);
39+
Napi::Number bind_wc_ecc_free(const Napi::CallbackInfo& info);

addon/wolfcrypt/h/evp.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
/* evp.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+
*/
121
#include <napi.h>
222
#include <stdio.h>
323
#include <cstring>

addon/wolfcrypt/h/hmac.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
/* hmac.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+
*/
121
#include <napi.h>
222
#include "wolfssl/options.h"
323
#include <wolfssl/wolfcrypt/settings.h>

addon/wolfcrypt/h/rsa.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
/* rsa.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+
*/
121
#include <napi.h>
222
#include <wolfssl/options.h>
323
#include <wolfssl/wolfcrypt/settings.h>

addon/wolfcrypt/h/sha.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
/* sha.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+
*/
121
#include <napi.h>
222
#include "wolfssl/options.h"
323
#include <wolfssl/wolfcrypt/settings.h>

0 commit comments

Comments
 (0)