Skip to content

Commit 0e47e0e

Browse files
author
John Bland
committed
add binding for wc_PBKDF2
1 parent f314c4a commit 0e47e0e

File tree

7 files changed

+176
-1
lines changed

7 files changed

+176
-1
lines changed

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: 3 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

3031
using namespace Napi;
3132

@@ -113,6 +114,8 @@ Napi::Object Init(Napi::Env env, Napi::Object exports)
113114
exports.Set(Napi::String::New(env, "wc_ecc_verify_hash"), Napi::Function::New(env, bind_wc_ecc_verify_hash));
114115
exports.Set(Napi::String::New(env, "wc_ecc_free"), Napi::Function::New(env, bind_wc_ecc_free));
115116

117+
exports.Set(Napi::String::New(env, "wc_PBKDF2"), Napi::Function::New(env, bind_wc_PBKDF2));
118+
116119
return exports;
117120
}
118121

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

2728
(async function() {
2829
for ( const key of Object.keys( evp_tests ) )
@@ -49,4 +50,9 @@ const ecc_tests = require( './tests/ecc' );
4950
{
5051
await ecc_tests[key]()
5152
}
53+
54+
for ( const key of Object.keys( pbkdf2_tests ) )
55+
{
56+
await pbkdf2_tests[key]()
57+
}
5258
})()

binding.gyp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"addon/wolfcrypt/hmac.cpp",
1010
"addon/wolfcrypt/rsa.cpp",
1111
"addon/wolfcrypt/sha.cpp",
12-
"addon/wolfcrypt/ecc.cpp"
12+
"addon/wolfcrypt/ecc.cpp",
13+
"addon/wolfcrypt/pbkdf2.cpp"
1314
],
1415
'include_dirs': [
1516
"<!@(node -p \"require('node-addon-api').include\")"

interfaces/pbkdf2.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/* pbkdf2.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+
22+
const wolfcrypt = require( '../build/Release/wolfcrypt' )
23+
24+
const WolfSSL_PBDKF2 = function( password, salt, iterations, keyLen, hash_type )
25+
{
26+
if ( !Buffer.isBuffer( password ) )
27+
{
28+
throw 'password must be Buffer'
29+
}
30+
31+
if ( !Buffer.isBuffer( salt ) )
32+
{
33+
throw 'salt must be Buffer'
34+
}
35+
36+
let type = wolfcrypt.typeof_Hmac( hash_type )
37+
38+
if ( type < 0 )
39+
{
40+
throw 'Invalid hash_type'
41+
}
42+
43+
let key = Buffer.alloc( keyLen )
44+
45+
let ret = wolfcrypt.wc_PBKDF2( key, password, password.length, salt, salt.length, iterations, keyLen, type )
46+
47+
if ( ret != 0 )
48+
{
49+
throw `Failed to wc_PBKDF2 ${ ret }`
50+
}
51+
52+
return key
53+
}
54+
55+
exports.WolfSSL_PBDKF2 = WolfSSL_PBDKF2

tests/pbkdf2.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/* pbkdf2.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+
22+
const { WolfSSL_PBDKF2 } = require( '../interfaces/pbkdf2' )
23+
24+
const pbkdf2_tests =
25+
{
26+
pbkdf2: function()
27+
{
28+
const password = Buffer.from( 'super secret password' )
29+
const salt = Buffer.from( 'super secret salt' )
30+
31+
const key1 = WolfSSL_PBDKF2( password, salt, 2048, 64, 'SHA512' )
32+
const key2 = WolfSSL_PBDKF2( password, salt, 2048, 64, 'SHA512' )
33+
34+
if ( key1.toString( 'hex' ) == key2.toString( 'hex' ) )
35+
{
36+
console.log( 'PASS pbkdf2' );
37+
}
38+
else
39+
{
40+
console.log( 'FAIL pbkdf2', key1.toString( 'hex' ), key2.toString( 'hex' ) );
41+
}
42+
}
43+
}
44+
45+
module.exports = pbkdf2_tests

0 commit comments

Comments
 (0)