Skip to content

Commit 5f169c8

Browse files
author
s.vychegzhanin
committed
fixed no padding on node env
fixed tests
1 parent ea5c17d commit 5f169c8

File tree

6 files changed

+63
-19
lines changed

6 files changed

+63
-19
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,9 @@ Questions, comments, bug reports, and pull requests are all welcome.
237237

238238
## Changelog
239239

240+
### 0.4.2
241+
* `no padding` scheme will padded data with zeros on all environments.
242+
240243
### 0.4.1
241244
* `PKCS1 no padding` scheme support.
242245

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-rsa",
3-
"version": "0.4.1",
3+
"version": "0.4.2",
44
"description": "Node.js RSA library",
55
"main": "src/NodeRSA.js",
66
"scripts": {

src/encryptEngines/io.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
var crypto = require('crypto');
22
var constants = require('constants');
3+
var schemes = require('../schemes/schemes.js');
34

45
module.exports = function (keyPair, options) {
6+
var pkcs1Scheme = schemes.pkcs1.makeScheme(keyPair, options);
7+
58
return {
69
encrypt: function (buffer, usePrivate) {
710
if (usePrivate) {
@@ -21,10 +24,16 @@ module.exports = function (keyPair, options) {
2124
if (options.encryptionSchemeOptions && options.encryptionSchemeOptions.padding) {
2225
padding = options.encryptionSchemeOptions.padding;
2326
}
27+
28+
var data = buffer;
29+
if (padding === constants.RSA_NO_PADDING) {
30+
data = pkcs1Scheme.pkcs0pad(buffer);
31+
}
32+
2433
return crypto.publicEncrypt({
2534
key: options.rsaUtils.exportKey('public'),
2635
padding: padding
27-
}, buffer);
36+
}, data);
2837
}
2938
},
3039

@@ -46,10 +55,15 @@ module.exports = function (keyPair, options) {
4655
if (options.encryptionSchemeOptions && options.encryptionSchemeOptions.padding) {
4756
padding = options.encryptionSchemeOptions.padding;
4857
}
49-
return crypto.privateDecrypt({
58+
var res = crypto.privateDecrypt({
5059
key: options.rsaUtils.exportKey('private'),
5160
padding: padding
5261
}, buffer);
62+
63+
if (padding === constants.RSA_NO_PADDING) {
64+
return pkcs1Scheme.pkcs0unpad(res);
65+
}
66+
return res;
5367
}
5468
}
5569
};

src/encryptEngines/node12.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
var crypto = require('crypto');
22
var constants = require('constants');
3+
var schemes = require('../schemes/schemes.js');
34

45
module.exports = function (keyPair, options) {
56
var jsEngine = require('./js.js')(keyPair, options);
7+
var pkcs1Scheme = schemes.pkcs1.makeScheme(keyPair, options);
68

79
return {
810
encrypt: function (buffer, usePrivate) {
@@ -17,10 +19,15 @@ module.exports = function (keyPair, options) {
1719
padding = options.encryptionSchemeOptions.padding;
1820
}
1921

22+
var data = buffer;
23+
if (padding === constants.RSA_NO_PADDING) {
24+
data = pkcs1Scheme.pkcs0pad(buffer);
25+
}
26+
2027
return crypto.publicEncrypt({
2128
key: options.rsaUtils.exportKey('public'),
2229
padding: padding
23-
}, buffer);
30+
}, data);
2431
},
2532

2633
decrypt: function (buffer, usePublic) {
@@ -35,10 +42,15 @@ module.exports = function (keyPair, options) {
3542
padding = options.encryptionSchemeOptions.padding;
3643
}
3744

38-
return crypto.privateDecrypt({
45+
var res = crypto.privateDecrypt({
3946
key: options.rsaUtils.exportKey('private'),
4047
padding: padding
4148
}, buffer);
49+
50+
if (padding === constants.RSA_NO_PADDING) {
51+
return pkcs1Scheme.pkcs0unpad(res);
52+
}
53+
return res;
4254
}
4355
};
4456
};

src/schemes/pkcs1.js

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@ module.exports.makeScheme = function (key, options) {
5555
}
5656
if (this.options.encryptionSchemeOptions && this.options.encryptionSchemeOptions.padding == constants.RSA_NO_PADDING) {
5757
//RSA_NO_PADDING treated like JAVA left pad with zero character
58-
filled = new Buffer(this.key.maxMessageLength - buffer.length);
59-
filled.fill(0);
60-
return Buffer.concat([filled, buffer]);
58+
return this.pkcs0pad(buffer);
6159
}
6260

6361
/* Type 1: zeros padding for private key encrypt */
@@ -98,13 +96,7 @@ module.exports.makeScheme = function (key, options) {
9896

9997
if (this.options.encryptionSchemeOptions && this.options.encryptionSchemeOptions.padding == constants.RSA_NO_PADDING) {
10098
//RSA_NO_PADDING treated like JAVA left pad with zero character
101-
var unPad;
102-
if (typeof buffer.lastIndexOf == "function") { //patch for old node version
103-
unPad = buffer.slice(buffer.lastIndexOf('\0') + 1, buffer.length);
104-
} else {
105-
unPad = buffer.slice(String.prototype.lastIndexOf.call(buffer, '\0') + 1, buffer.length);
106-
}
107-
return unPad;
99+
return this.pkcs0unpad(buffer);
108100
}
109101

110102
if (buffer.length < 4) {
@@ -181,6 +173,31 @@ module.exports.makeScheme = function (key, options) {
181173
}
182174
};
183175

176+
/**
177+
* PKCS#1 zero pad input buffer to max data length
178+
* @param hashBuf
179+
* @param hashAlgorithm
180+
* @returns {*}
181+
*/
182+
Scheme.prototype.pkcs0pad = function (buffer) {
183+
var filled = new Buffer(this.key.maxMessageLength - buffer.length);
184+
filled.fill(0);
185+
return Buffer.concat([filled, buffer]);
186+
187+
return filled;
188+
};
189+
190+
Scheme.prototype.pkcs0unpad = function (buffer) {
191+
var unPad;
192+
if (typeof buffer.lastIndexOf == "function") { //patch for old node version
193+
unPad = buffer.slice(buffer.lastIndexOf('\0') + 1, buffer.length);
194+
} else {
195+
unPad = buffer.slice(String.prototype.lastIndexOf.call(buffer, '\0') + 1, buffer.length);
196+
}
197+
198+
return unPad;
199+
};
200+
184201
/**
185202
* PKCS#1 pad input buffer to max data length
186203
* @param hashBuf

test/tests.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ describe('NodeRSA', function () {
2222
'pkcs1_oaep',
2323
{
2424
scheme:'pkcs1',
25-
encryptionScheme:{
26-
padding: constants.RSA_NO_PADDING
27-
},
25+
padding: constants.RSA_NO_PADDING,
2826
toString: function() {
2927
return 'pkcs1-nopadding';
3028
}
@@ -178,7 +176,7 @@ describe('NodeRSA', function () {
178176
}
179177
});
180178

181-
describe('Imprt/Export keys', function () {
179+
describe('Import/Export keys', function () {
182180
var privateKeyPKCS1 = '-----BEGIN RSA PRIVATE KEY-----\n' +
183181
'MIIFwgIBAAKCAUEAsE1edyfToZRv6cFOkB0tAJ5qJor4YF5CccJAL0fS/o1Yk10V\n' +
184182
'SXH4Xx4peSJgYQKkO0HqO1hAz6k9dFQB4U1CnWtRjtNEcIfycqrZrhu6you5syb6\n' +

0 commit comments

Comments
 (0)