Skip to content

Commit

Permalink
Add privateDecrypt().
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Bornstein committed Feb 9, 2012
1 parent 7997222 commit 35374ee
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
28 changes: 25 additions & 3 deletions src/ursaNative.cc
Expand Up @@ -410,7 +410,11 @@ Handle<Value> RsaWrap::GetPublicKeyPem(const Arguments& args) {
return bioToBuffer(bio);
}

// FIXME: Need documentation.
/**
* Perform decryption on the given buffer using the RSA key, which
* must be a private key. This always uses the padding mode
* RSA_PKCS1_OAEP_PADDING.
*/
Handle<Value> RsaWrap::PrivateDecrypt(const Arguments& args) {
HandleScope scope;

Expand All @@ -421,8 +425,26 @@ Handle<Value> RsaWrap::PrivateDecrypt(const Arguments& args) {
void *data = getArg0DataAndLength(args, &length);
if (data == NULL) { return Undefined(); }

// FIXME: Need real implementation.
return scope.Close(String::New("world"));
int rsaLength = RSA_size(obj->rsa);
unsigned char buf[rsaLength];

int bufLength = RSA_private_decrypt(length, (unsigned char *) data,
buf, obj->rsa, RSA_PKCS1_OAEP_PADDING);

if (bufLength < 0) {
scheduleSslException();
return Undefined();
}

node::Buffer *result = node::Buffer::New(bufLength);

if (result == NULL) {
scheduleAllocException();
return Undefined();
}

memcpy(node::Buffer::Data(result), buf, bufLength);
return result->handle_;
}

// FIXME: Need documentation.
Expand Down
10 changes: 9 additions & 1 deletion test/fixture.js
Expand Up @@ -41,7 +41,15 @@ var MODULUS_HEX =
"cfa70934d1c7b9e2e5a3c1897fb10f803af2998495db24511f2b2162f1fd8475";

var PLAINTEXT = "Muffins are tasty.";
var PRIVATE_CIPHERTEXT_HEX = "1234"; // FIXME
var PRIVATE_CIPHERTEXT_HEX =
"98a96084dc8dfad2c4e604dc20def71acbf784b8b34ecafeb2840e238ac8031c" +
"7559004fa8337d20889b8a582af4f7d3707ab41d0a81487f0d80fb82be49537c" +
"2b9cd8dbb3b772fe0306ff9b4b99faa7cc26d5c04b1e8e79505bac1e8f2cdad2" +
"d3d8680eee3c16db8742b61935fca9679070d278f988ce4d414ab49a544c9088" +
"17a0d340a41384f4b8d826e41031ddcd3f72c29dec2fee0355a8203ea0d381a1" +
"a0f0969804d4968fb2e6220db5cf02e2c2200ff9d0a5a5037ac859a55c005ecc" +
"52ce194a6a9624c71547c96cf90d911caa4097f9cdfded71d23c9f8f5551188c" +
"8326357d54224ab25b9f29c1efdbc960a0968e4c9027cd507ffadd8dff93256c";
var PUBLIC_CIPHERTEXT_HEX = "1234"; // FIXME

/*
Expand Down
16 changes: 9 additions & 7 deletions test/native.js
Expand Up @@ -216,28 +216,28 @@ function test_fail_privateDecrypt() {
function f2() {
rsa.privateDecrypt("x");
}
assert.throws(f2, /arg mumble FIXME/);
assert.throws(f2, /Expected a Buffer in args\[0]\./);

function f3() {
rsa.privateDecrypt(new Buffer("x"));
}
assert.throws(f3, /mumble FIXME/);
assert.throws(f3, /decoding error/);
}

function test_publicEncrypt() {
// No other reasonable way to test this than to do a round trip.
var plainBuf = new Buffer(fixture.PLAINTEXT, fixture.UTF8);
var priv = new RsaWrap();
priv.setPrivateKeyPem(fixture.PRIVATE_KEY);

var rsa = new RsaWrap();
rsa.setPublicKeyPem(fixture.PUBLIC_KEY);
var encoded = rsa.publicEncrypt(plainBuf);
var decoded = rsa.privateDecrypt(encoded).toString(fixture.UTF8);
var decoded = priv.privateDecrypt(encoded).toString(fixture.UTF8);
assert.equal(decoded, fixture.PLAINTEXT);

rsa = new RsaWrap();
rsa.setPrivateKeyPem(fixture.PRIVATE_KEY);
encoded = rsa.publicEncrypt(plainBuf);
decoded = rsa.privateDecrypt(encoded).toString(fixture.UTF8);
encoded = priv.publicEncrypt(plainBuf);
decoded = priv.privateDecrypt(encoded).toString(fixture.UTF8);
assert.equal(decoded, fixture.PLAINTEXT);
}

Expand Down Expand Up @@ -286,6 +286,8 @@ function test() {
test_getPublicKeyPem();
test_fail_getPublicKeyPem();

test_publicEncrypt(); // remove!

test_privateDecrypt();
test_fail_privateDecrypt();
test_publicEncrypt();
Expand Down

0 comments on commit 35374ee

Please sign in to comment.