Skip to content

Commit 1da63d1

Browse files
committed
Pass 2
1 parent 183e726 commit 1da63d1

File tree

8 files changed

+334
-16
lines changed

8 files changed

+334
-16
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ Use npm to install and build:
2525
```
2626
npm i
2727
npm run build
28-
node app.js
28+
npm run tsrun
2929
```

WolfSSLDecryptor.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"use strict";
2+
exports.__esModule = true;
3+
exports.WolfSSLDecryptor = void 0;
4+
var wolfcrypt = require('./build/Release/wolfcrypt');
5+
//export class WolfSSLDecryptor implements Decryptor {
6+
var WolfSSLDecryptor = /** @class */ (function () {
7+
/**
8+
* Initializes a new instance of the WolfSSLDecryptor class.
9+
*
10+
* @param cipher The cipher name to use.
11+
* @param key The decryption key to use.
12+
* @param iv The initialization vector.
13+
*
14+
* @throws {Error} If cipher is not available or unknown.
15+
* @throws {Error} If the creation of the Decryption object failed.
16+
*/
17+
function WolfSSLDecryptor(cipher, key, iv) {
18+
//this.enableFips();
19+
this.evp = Buffer.alloc(wolfcrypt.sizeof_EVP_CIPHER_CTX());
20+
this.totalInputLength = 0;
21+
wolfcrypt.EVP_CipherInit(this.evp, cipher, key, iv);
22+
}
23+
/**
24+
* Updates the internal state with data for decryption.
25+
*
26+
* @param data The data that will be added for decryption.
27+
*
28+
* @returns The decrypted data if possible.
29+
*
30+
* @throws {Error} If the decryption fails.
31+
*
32+
* @remarks This function should be called multiple times.
33+
*/
34+
WolfSSLDecryptor.prototype.update = function (data) {
35+
this.totalInputLength += data.length;
36+
var outBuffer = Buffer.alloc(this.totalInputLength);
37+
var ret = wolfcrypt.EVP_CipherUpdate(this.evp, outBuffer, data, data.length);
38+
if (ret > 0) {
39+
return outBuffer;
40+
}
41+
return Buffer.alloc(0);
42+
};
43+
/**
44+
* Finalize the decryption process.
45+
*
46+
* @returns The last block of decrypted data.
47+
*
48+
* @throws {Error} If the decryption fails.
49+
*
50+
* @remarks This function should be called once to finalize the decryption
51+
* process.
52+
*/
53+
WolfSSLDecryptor.prototype.finalize = function () {
54+
var outBuffer = Buffer.alloc(this.totalInputLength);
55+
var ret = wolfcrypt.EVP_CipherFinal(this.evp, outBuffer);
56+
if (ret > 0) {
57+
return outBuffer;
58+
}
59+
return Buffer.alloc(0);
60+
};
61+
return WolfSSLDecryptor;
62+
}());
63+
exports.WolfSSLDecryptor = WolfSSLDecryptor;

WolfSSLDecryptor.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
const wolfcrypt = require('./build/Release/wolfcrypt');
2+
3+
//export class WolfSSLDecryptor implements Decryptor {
4+
export class WolfSSLDecryptor {
5+
private evp: Buffer
6+
private totalInputLength: number
7+
8+
/**
9+
* Initializes a new instance of the WolfSSLDecryptor class.
10+
*
11+
* @param cipher The cipher name to use.
12+
* @param key The decryption key to use.
13+
* @param iv The initialization vector.
14+
*
15+
* @throws {Error} If cipher is not available or unknown.
16+
* @throws {Error} If the creation of the Decryption object failed.
17+
*/
18+
public constructor(cipher: string, key: Buffer, iv: Buffer) {
19+
//this.enableFips();
20+
this.evp = Buffer.alloc( wolfcrypt.sizeof_EVP_CIPHER_CTX() )
21+
this.totalInputLength = 0
22+
wolfcrypt.EVP_CipherInit( this.evp, cipher, key, iv )
23+
}
24+
25+
/**
26+
* Updates the internal state with data for decryption.
27+
*
28+
* @param data The data that will be added for decryption.
29+
*
30+
* @returns The decrypted data if possible.
31+
*
32+
* @throws {Error} If the decryption fails.
33+
*
34+
* @remarks This function should be called multiple times.
35+
*/
36+
public update(data: Buffer): Buffer {
37+
this.totalInputLength += data.length
38+
39+
let outBuffer = Buffer.alloc( this.totalInputLength )
40+
41+
let ret = wolfcrypt.EVP_CipherUpdate( this.evp, outBuffer, data, data.length )
42+
43+
if ( ret > 0 )
44+
{
45+
return outBuffer
46+
}
47+
48+
return Buffer.alloc( 0 )
49+
}
50+
51+
/**
52+
* Finalize the decryption process.
53+
*
54+
* @returns The last block of decrypted data.
55+
*
56+
* @throws {Error} If the decryption fails.
57+
*
58+
* @remarks This function should be called once to finalize the decryption
59+
* process.
60+
*/
61+
public finalize(): Buffer {
62+
let outBuffer = Buffer.alloc( this.totalInputLength )
63+
64+
let ret = wolfcrypt.EVP_CipherFinal( this.evp, outBuffer )
65+
66+
if ( ret > 0 )
67+
{
68+
return outBuffer
69+
}
70+
71+
return Buffer.alloc( 0 )
72+
}
73+
74+
/**
75+
* Enables the FIPS mode.
76+
*/
77+
/*
78+
private enableFips(): void {
79+
if (!wolfssl.isFipsEnabled() && !wolfssl.enableFips()) {
80+
logger.logWarning('FIPS mode not available.');
81+
}
82+
}
83+
*/
84+
}

app.js

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,77 @@
1-
const wolfcrypt = require('./build/Release/wolfcrypt');
1+
//const wolfcrypt = require('./build/Release/wolfcrypt');
2+
const { WolfSSLDecryptor } = require( './WolfSSLDecryptor.ts' )
23

3-
let aes = Buffer.alloc( 300 )
4+
/*
5+
let aes = Buffer.alloc( 1152 )
46
let key = Buffer.from( '12345678901234567890123456789012' )
57
let iv = Buffer.from( '1234567890123456' )
68
let plainText = Buffer.from( 'testtesttesttest' )
79
let cipherText = Buffer.alloc( 16 )
810
let plainAgain = Buffer.alloc( 16 )
11+
let length = 16
912
1013
let ret = wolfcrypt.MakeAes( aes, key, iv )
1114
1215
if ( ret == 0 )
16+
ret = wolfcrypt.Encrypt( aes, cipherText, plainText, length )
17+
18+
if ( ret == 0 )
19+
ret = wolfcrypt.Decrypt( aes, plainAgain, cipherText, length )
20+
21+
console.log( plainText.toString() )
22+
console.log( cipherText.toString( 'hex' ) )
23+
console.log( plainAgain.toString() )
24+
*/
25+
/*
26+
let length = 0
27+
let finalOutput = []
28+
const key = Buffer.from('12345678901234567890123456789012')
29+
const iv = Buffer.from('1234567890123456');
30+
31+
console.log( wolfcrypt.GetDecryptionSize() )
32+
33+
let decryption = Buffer.alloc( wolfcrypt.GetDecryptionSize() )
34+
wolfcrypt.NewDecryption( decryption, 'AES-256-CBC', key, iv )
35+
36+
let outBuf = Buffer.alloc( 8 )
37+
length = wolfcrypt.UpdateCipher( decryption, outBuf, Buffer.from('24d31b1e41fc8c40', 'hex'), 8 )
38+
console.log( length )
39+
40+
if ( length > 0 )
1341
{
14-
ret = wolfcrypt.Encrypt( aes, cipherText, plainText, 16 )
42+
finalOutput.push( outBuf )
1543
}
1644
17-
if ( ret == 0 )
45+
outBuf = Buffer.alloc( 8 )
46+
length = wolfcrypt.UpdateCipher( decryption, outBuf, Buffer.from('e521531d67c72c20', 'hex'), 8 )
47+
console.log( length )
48+
49+
if ( length > 0 )
50+
{
51+
finalOutput.push( outBuf )
52+
}
53+
54+
outBuf = Buffer.alloc( 16 )
55+
length = wolfcrypt.FinalizeCipher( decryption, outBuf )
56+
console.log( length )
57+
58+
if ( length > 0 )
1859
{
19-
ret = wolfcrypt.Decrypt( aes, plainAgain, cipherText, 16 )
60+
finalOutput.push( outBuf )
2061
}
2162
22-
console.log( plainText.toString() );
23-
console.log( cipherText.toString( 'hex' ) );
24-
console.log( plainAgain.toString() );
63+
console.log( Buffer.concat( finalOutput ).toString() )
64+
*/
65+
66+
const key = Buffer.from('12345678901234567890123456789012');
67+
const iv = Buffer.from('1234567890123456');
68+
const decrypt = new WolfSSLDecryptor('AES-256-CBC', key, iv);
69+
const expected = 'test';
70+
71+
const actual = Buffer.concat([
72+
decrypt.update(Buffer.from('24d31b1e41fc8c40', 'hex')),
73+
decrypt.update(Buffer.from('e521531d67c72c20', 'hex')),
74+
decrypt.finalize()
75+
]);
76+
77+
console.log( actual.toString() )

main.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"use strict";
2+
exports.__esModule = true;
3+
var WolfSSLDecryptor_1 = require("./WolfSSLDecryptor");
4+
var key = Buffer.from('12345678901234567890123456789012');
5+
var iv = Buffer.from('1234567890123456');
6+
var decrypt = new WolfSSLDecryptor_1.WolfSSLDecryptor('AES-256-CBC', key, iv);
7+
var expected = 'test';
8+
var actual = Buffer.concat([
9+
decrypt.update(Buffer.from('24d31b1e41fc8c40', 'hex')),
10+
decrypt.update(Buffer.from('e521531d67c72c20', 'hex')),
11+
decrypt.finalize()
12+
]).toString();
13+
if (actual == expected) {
14+
console.log('PASS');
15+
}
16+
else {
17+
console.log('FAIL', expected, expected.length, actual, actual.length);
18+
}

main.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { WolfSSLDecryptor } from './WolfSSLDecryptor'
2+
3+
const key = Buffer.from('12345678901234567890123456789012')
4+
const iv = Buffer.from('1234567890123456')
5+
const decrypt = new WolfSSLDecryptor('AES-256-CBC', key, iv)
6+
const expected = 'test'
7+
8+
const actual = Buffer.concat([
9+
decrypt.update(Buffer.from('24d31b1e41fc8c40', 'hex')),
10+
decrypt.update(Buffer.from('e521531d67c72c20', 'hex')),
11+
decrypt.finalize()
12+
]).toString()
13+
14+
if ( actual == expected )
15+
{
16+
console.log( 'PASS' )
17+
}
18+
else
19+
{
20+
console.log( 'FAIL', expected, expected.length, actual, actual.length )
21+
}

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66
"gypfile": true,
77
"scripts": {
88
"run": "node app.js",
9+
"tsrun": "npx tsc main.ts && node main.js",
910
"build": "node-gyp rebuild",
1011
"clean": "node-gyp clean"
1112
},
1213
"author": "John Bland",
1314
"license": "GPL-2.0",
1415
"devDependencies": {
15-
"node-gyp": "^9.1.0"
16+
"@types/node": "^18.0.6",
17+
"node-gyp": "^9.1.0",
18+
"typescript": "^4.7.4"
1619
},
1720
"dependencies": {
1821
"node-addon-api": "^5.0.0"

0 commit comments

Comments
 (0)