Browse files

Set padding default in js, not C++; pub-en/pri-de

  • Loading branch information...
1 parent 5aa7b5d commit b56c1698a6c74e873a5697a1bf05cc43b05a451e @tylerneylon committed Aug 22, 2012
Showing with 20 additions and 29 deletions.
  1. +2 −0 lib/ursa.js
  2. +4 −15 src/ursaNative.cc
  3. +14 −14 test/native.js
View
2 lib/ursa.js
@@ -231,6 +231,7 @@ function PublicKey(rsa) {
function encrypt(buf, bufEncoding, outEncoding, padding) {
buf = decodeString(buf, bufEncoding);
+ padding = padding || ursaNative.RSA_PKCS1_OAEP_PADDING;
return encodeBuffer(rsa.publicEncrypt(buf, padding), outEncoding);
}
@@ -286,6 +287,7 @@ function PrivateKey(rsa) {
function decrypt(buf, bufEncoding, outEncoding, padding) {
buf = decodeString(buf, bufEncoding);
+ padding = padding || ursaNative.RSA_PKCS1_OAEP_PADDING;
return encodeBuffer(rsa.privateDecrypt(buf, padding), outEncoding);
}
View
19 src/ursaNative.cc
@@ -278,17 +278,6 @@ static bool getArgInt(const Arguments& args, int index, int *resultPtr) {
return true;
}
-/**
- * If an int is present at the given index, store it in the given pointer;
- * if no arg is present or it's undefined, do nothing. Return true in either
- * case. If it is present and is neither an int nor undefined, schedule an
- * exception and return false.
- */
-static bool getArgIntIfExists(const Arguments& args, int index, int *resultPtr) {
- if (args.Length() < index || args[index]->IsUndefined()) { return true; }
- return getArgInt(args, index, resultPtr);
-}
-
/*
* Utility function implementation
@@ -573,8 +562,8 @@ Handle<Value> RsaWrap::PrivateDecrypt(const Arguments& args) {
int rsaLength = RSA_size(obj->rsa);
unsigned char buf[rsaLength];
- int padding = RSA_PKCS1_OAEP_PADDING;
- if (!getArgIntIfExists(args, 1, &padding)) { return Undefined(); }
+ int padding;
+ if (!getArgInt(args, 1, &padding)) { return Undefined(); }
int bufLength = RSA_private_decrypt(length, (unsigned char *) data,
buf, obj->rsa, padding);
@@ -689,8 +678,8 @@ Handle<Value> RsaWrap::PublicEncrypt(const Arguments& args) {
return Undefined();
}
- int padding = RSA_PKCS1_OAEP_PADDING;
- if (!getArgIntIfExists(args, 1, &padding)) { return Undefined(); }
+ int padding;
+ if (!getArgInt(args, 1, &padding)) { return Undefined(); }
int ret = RSA_public_encrypt(length, (unsigned char *) data,
(unsigned char *) node::Buffer::Data(result),
View
28 test/native.js
@@ -199,7 +199,7 @@ function test_privateDecrypt() {
rsa.setPrivateKeyPem(fixture.PRIVATE_KEY);
var encoded = new Buffer(fixture.PRIVATE_CIPHERTEXT_HEX, fixture.HEX);
- var decoded = rsa.privateDecrypt(encoded).toString(fixture.UTF8);
+ var decoded = rsa.privateDecrypt(encoded, ursaNative.RSA_PKCS1_OAEP_PADDING).toString(fixture.UTF8);
assert.equal(decoded, fixture.PLAINTEXT);
var encoded = new Buffer(fixture.PRIVATE_OLD_PAD_CIPHER_HEX, fixture.HEX);
@@ -222,12 +222,12 @@ function test_fail_privateDecrypt() {
rsa.setPrivateKeyPem(fixture.PRIVATE_KEY);
function f2() {
- rsa.privateDecrypt("x");
+ rsa.privateDecrypt("x", ursaNative.RSA_PKCS1_OAEP_PADDING);
}
assert.throws(f2, /Expected a Buffer in args\[0]\./);
function f3() {
- rsa.privateDecrypt(new Buffer("x"));
+ rsa.privateDecrypt(new Buffer("x"), ursaNative.RSA_PKCS1_OAEP_PADDING);
}
assert.throws(f3, /decoding error/);
@@ -245,12 +245,12 @@ function test_publicEncrypt() {
var rsa = new RsaWrap();
rsa.setPublicKeyPem(fixture.PUBLIC_KEY);
- var encoded = rsa.publicEncrypt(plainBuf);
- var decoded = priv.privateDecrypt(encoded).toString(fixture.UTF8);
+ var encoded = rsa.publicEncrypt(plainBuf, ursaNative.RSA_PKCS1_OAEP_PADDING);
+ var decoded = priv.privateDecrypt(encoded, ursaNative.RSA_PKCS1_OAEP_PADDING).toString(fixture.UTF8);
assert.equal(decoded, fixture.PLAINTEXT);
- encoded = priv.publicEncrypt(plainBuf);
- decoded = priv.privateDecrypt(encoded).toString(fixture.UTF8);
+ encoded = priv.publicEncrypt(plainBuf, ursaNative.RSA_PKCS1_OAEP_PADDING);
+ decoded = priv.privateDecrypt(encoded, ursaNative.RSA_PKCS1_OAEP_PADDING).toString(fixture.UTF8);
assert.equal(decoded, fixture.PLAINTEXT);
// Test with old-style padding.
@@ -273,12 +273,12 @@ function test_fail_publicEncrypt() {
rsa.setPublicKeyPem(fixture.PUBLIC_KEY);
function f2() {
- rsa.publicEncrypt("x");
+ rsa.publicEncrypt("x", ursaNative.RSA_PKCS1_OAEP_PADDING);
}
assert.throws(f2, /Expected a Buffer in args\[0]\./);
function f3() {
- rsa.publicEncrypt(new Buffer(2048));
+ rsa.publicEncrypt(new Buffer(2048), ursaNative.RSA_PKCS1_OAEP_PADDING);
}
assert.throws(f3, /too large/);
@@ -366,21 +366,21 @@ function test_generatePrivateKey() {
// Do a round trip check.
var plainBuf = new Buffer(fixture.PLAINTEXT, fixture.UTF8);
- var encoded = rsa.publicEncrypt(plainBuf);
- var decoded = rsa.privateDecrypt(encoded).toString(fixture.UTF8);
+ var encoded = rsa.publicEncrypt(plainBuf, ursaNative.RSA_PKCS1_OAEP_PADDING);
+ var decoded = rsa.privateDecrypt(encoded, ursaNative.RSA_PKCS1_OAEP_PADDING).toString(fixture.UTF8);
assert.equal(decoded, fixture.PLAINTEXT);
// Extract the public key, and try using it for a round trip.
var pubKey = new RsaWrap();
pubKey.setPublicKeyPem(rsa.getPublicKeyPem());
- encoded = pubKey.publicEncrypt(plainBuf);
- decoded = rsa.privateDecrypt(encoded).toString(fixture.UTF8);
+ encoded = pubKey.publicEncrypt(plainBuf, ursaNative.RSA_PKCS1_OAEP_PADDING);
+ decoded = rsa.privateDecrypt(encoded, ursaNative.RSA_PKCS1_OAEP_PADDING).toString(fixture.UTF8);
assert.equal(decoded, fixture.PLAINTEXT);
// Similarly, try decoding with an extracted private key.
var privKey = new RsaWrap();
privKey.setPrivateKeyPem(rsa.getPrivateKeyPem());
- decoded = privKey.privateDecrypt(encoded).toString(fixture.UTF8);
+ decoded = privKey.privateDecrypt(encoded, ursaNative.RSA_PKCS1_OAEP_PADDING).toString(fixture.UTF8);
assert.equal(decoded, fixture.PLAINTEXT);
}

0 comments on commit b56c169

Please sign in to comment.