Skip to content

Commit 183e726

Browse files
committed
Pass 1
1 parent b2a45bf commit 183e726

File tree

5 files changed

+162
-0
lines changed

5 files changed

+162
-0
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# wolfCrypt Node.JS support
2+
3+
Wrappers for wolfCrypt ciphers.
4+
5+
## Building wolfSSL
6+
7+
```
8+
./configure --enable-all
9+
make
10+
sudo make install
11+
```
12+
13+
To link wolfcrypt you need to run `export LD_LIBRARY_PATH=/usr/local/lib` or wherever you have your wolfssl installed:
14+
15+
Verify the .so (shared object) path and version in `binding.gyp`:
16+
17+
```
18+
'libraries': [
19+
"/usr/local/lib/libwolfssl.so.34"
20+
],
21+
```
22+
23+
Use npm to install and build:
24+
25+
```
26+
npm i
27+
npm run build
28+
node app.js
29+
```

app.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const wolfcrypt = require('./build/Release/wolfcrypt');
2+
3+
let aes = Buffer.alloc( 300 )
4+
let key = Buffer.from( '12345678901234567890123456789012' )
5+
let iv = Buffer.from( '1234567890123456' )
6+
let plainText = Buffer.from( 'testtesttesttest' )
7+
let cipherText = Buffer.alloc( 16 )
8+
let plainAgain = Buffer.alloc( 16 )
9+
10+
let ret = wolfcrypt.MakeAes( aes, key, iv )
11+
12+
if ( ret == 0 )
13+
{
14+
ret = wolfcrypt.Encrypt( aes, cipherText, plainText, 16 )
15+
}
16+
17+
if ( ret == 0 )
18+
{
19+
ret = wolfcrypt.Decrypt( aes, plainAgain, cipherText, 16 )
20+
}
21+
22+
console.log( plainText.toString() );
23+
console.log( cipherText.toString( 'hex' ) );
24+
console.log( plainAgain.toString() );

binding.gyp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"targets": [{
3+
"target_name": "wolfcrypt",
4+
"cflags!": [ "-fno-exceptions" ],
5+
"cflags_cc!": [ "-fno-exceptions" ],
6+
"sources": [
7+
"wolfbind/main.cpp"
8+
],
9+
'include_dirs': [
10+
"<!@(node -p \"require('node-addon-api').include\")"
11+
],
12+
'libraries': [
13+
"/usr/local/lib/libwolfssl.so.34"
14+
],
15+
'dependencies': [
16+
"<!(node -p \"require('node-addon-api').gyp\")"
17+
],
18+
'defines': [ 'NAPI_DISABLE_C_EXCEPTIONS' ]
19+
}]
20+
}

package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "wolfcrypt_binding",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "app.js",
6+
"gypfile": true,
7+
"scripts": {
8+
"run": "node app.js",
9+
"build": "node-gyp rebuild",
10+
"clean": "node-gyp clean"
11+
},
12+
"author": "John Bland",
13+
"license": "GPL-2.0",
14+
"devDependencies": {
15+
"node-gyp": "^9.1.0"
16+
},
17+
"dependencies": {
18+
"node-addon-api": "^5.0.0"
19+
}
20+
}

wolfbind/main.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include <napi.h>
2+
#include <stdio.h>
3+
#include "wolfssl/options.h"
4+
#include <wolfssl/wolfcrypt/settings.h>
5+
#include <wolfssl/wolfcrypt/aes.h>
6+
7+
using namespace Napi;
8+
9+
typedef struct WrappedKey
10+
{
11+
Aes aes[1];
12+
uint8_t key[32];
13+
uint8_t iv[16];
14+
} WrappedKey;
15+
16+
Napi::Number MakeAes(const Napi::CallbackInfo& info) {
17+
int ret = 0;
18+
Napi::Env env = info.Env();
19+
WrappedKey* key = (WrappedKey*)(info[0].As<Napi::Uint8Array>().Data());
20+
memcpy( key->key, (info[1].As<Napi::Uint8Array>().Data()), 32 );
21+
memcpy( key->iv, (info[2].As<Napi::Uint8Array>().Data()), 16 );
22+
23+
//ret = wc_AesInit( key->aes, NULL, INVALID_DEVID );
24+
25+
return Napi::Number::New( env, ret );
26+
}
27+
28+
Napi::Number Encrypt(const Napi::CallbackInfo& info) {
29+
int ret;
30+
Napi::Env env = info.Env();
31+
WrappedKey* key = (WrappedKey*)(info[0].As<Napi::Uint8Array>().Data());
32+
uint8_t* out = info[1].As<Napi::Uint8Array>().Data();
33+
uint8_t* in = info[2].As<Napi::Uint8Array>().Data();
34+
int length = info[3].As<Napi::Number>().Int32Value();
35+
36+
ret = wc_AesSetKey( key->aes, key->key, AES_256_KEY_SIZE, key->iv, AES_ENCRYPTION );
37+
38+
if ( ret == 0 )
39+
ret = wc_AesCbcEncrypt( key->aes, out, in, length );
40+
41+
return Napi::Number::New( env, ret );
42+
}
43+
44+
Napi::Number Decrypt(const Napi::CallbackInfo& info)
45+
{
46+
int ret;
47+
Napi::Env env = info.Env();
48+
WrappedKey* key = (WrappedKey*)(info[0].As<Napi::Uint8Array>().Data());
49+
uint8_t* out = info[1].As<Napi::Uint8Array>().Data();
50+
uint8_t* in = info[2].As<Napi::Uint8Array>().Data();
51+
int length = info[3].As<Napi::Number>().Int32Value();
52+
53+
ret = wc_AesSetKey( key->aes, key->key, AES_256_KEY_SIZE, key->iv, AES_DECRYPTION );
54+
55+
if ( ret == 0 )
56+
ret = wc_AesCbcDecrypt( key->aes, out, in, length );
57+
58+
return Napi::Number::New( env, ret );
59+
}
60+
61+
Napi::Object Init(Napi::Env env, Napi::Object exports) {
62+
exports.Set(Napi::String::New(env, "MakeAes"), Napi::Function::New(env, MakeAes));
63+
exports.Set(Napi::String::New(env, "Encrypt"), Napi::Function::New(env, Encrypt));
64+
exports.Set(Napi::String::New(env, "Decrypt"), Napi::Function::New(env, Decrypt));
65+
66+
return exports;
67+
}
68+
69+
NODE_API_MODULE( addon, Init )

0 commit comments

Comments
 (0)