Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save havard/817133 to your computer and use it in GitHub Desktop.
Save havard/817133 to your computer and use it in GitHub Desktop.
Patch to apply on top of ry's rebased DH patch
From a0b367df01ec3da3fd89d263fc58854397c65c4a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?H=C3=A5vard=20Stranden?= <havard.stranden@gmail.com>
Date: Tue, 8 Feb 2011 21:04:44 +0100
Subject: [PATCH] Linted Diffie-Hellman parts of node_crypto.cc
---
src/node_crypto.cc | 133 ++++++++++++++++++++++++++++++++--------------------
1 files changed, 82 insertions(+), 51 deletions(-)
diff --git a/src/node_crypto.cc b/src/node_crypto.cc
index 82bc01f..b46cb24 100644
--- a/src/node_crypto.cc
+++ b/src/node_crypto.cc
@@ -2712,7 +2712,7 @@ class Verify : public ObjectWrap {
class DiffieHellman : public ObjectWrap {
public:
- static void Initialize (v8::Handle<v8::Object> target) {
+ static void Initialize(v8::Handle<v8::Object> target) {
HandleScope scope;
Local<FunctionTemplate> t = FunctionTemplate::New(New);
@@ -2752,7 +2752,7 @@ class DiffieHellman : public ObjectWrap {
}
protected:
- static Handle<Value> New (const Arguments& args) {
+ static Handle<Value> New(const Arguments& args) {
HandleScope scope;
DiffieHellman* diffieHellman = new DiffieHellman();
@@ -2774,7 +2774,8 @@ class DiffieHellman : public ObjectWrap {
if (len == -1) {
delete[] buf;
- return ThrowException(Exception::Error(String::New("Invalid argument")));
+ return ThrowException(Exception::Error(
+ String::New("Invalid argument")));
} else {
diffieHellman->Init(reinterpret_cast<unsigned char*>(buf), len);
delete[] buf;
@@ -2783,7 +2784,7 @@ class DiffieHellman : public ObjectWrap {
} else if (Buffer::HasInstance(args[0])) {
Local<Object> buffer = args[0]->ToObject();
diffieHellman->Init(
- reinterpret_cast<unsigned char*>(Buffer::Data(buffer)),
+ reinterpret_cast<unsigned char*>(Buffer::Data(buffer)),
Buffer::Length(buffer));
initialized = true;
}
@@ -2791,7 +2792,8 @@ class DiffieHellman : public ObjectWrap {
}
if (!initialized) {
- return ThrowException(Exception::Error(String::New("Initialization failed")));
+ return ThrowException(Exception::Error(
+ String::New("Initialization failed")));
}
diffieHellman->Wrap(args.This());
@@ -2799,24 +2801,28 @@ class DiffieHellman : public ObjectWrap {
return args.This();
}
- static Handle<Value> GenerateKeys (const Arguments& args) {
- DiffieHellman* diffieHellman = ObjectWrap::Unwrap<DiffieHellman>(args.This());
+ static Handle<Value> GenerateKeys(const Arguments& args) {
+ DiffieHellman* diffieHellman =
+ ObjectWrap::Unwrap<DiffieHellman>(args.This());
HandleScope scope;
if (!diffieHellman->initialised_) {
- return ThrowException(Exception::Error(String::New("Not initialized")));
+ return ThrowException(Exception::Error(
+ String::New("Not initialized")));
}
if (!DH_generate_key(diffieHellman->dh)) {
- return ThrowException(Exception::Error(String::New("Key generation failed")));
+ return ThrowException(Exception::Error(
+ String::New("Key generation failed")));
}
Local<Value> outString;
int dataSize = BN_num_bytes(diffieHellman->dh->pub_key);
char* data = new char[dataSize];
- BN_bn2bin(diffieHellman->dh->pub_key, reinterpret_cast<unsigned char*>(data));
+ BN_bn2bin(diffieHellman->dh->pub_key,
+ reinterpret_cast<unsigned char*>(data));
if (args.Length() > 0 && args[0]->IsString()) {
outString = EncodeWithEncoding(args[0], data, dataSize);
@@ -2828,8 +2834,9 @@ class DiffieHellman : public ObjectWrap {
return scope.Close(outString);
}
- static Handle<Value> GetPrime (const Arguments& args) {
- DiffieHellman* diffieHellman = ObjectWrap::Unwrap<DiffieHellman>(args.This());
+ static Handle<Value> GetPrime(const Arguments& args) {
+ DiffieHellman* diffieHellman =
+ ObjectWrap::Unwrap<DiffieHellman>(args.This());
HandleScope scope;
@@ -2854,8 +2861,9 @@ class DiffieHellman : public ObjectWrap {
return scope.Close(outString);
}
- static Handle<Value> GetGenerator (const Arguments& args) {
- DiffieHellman* diffieHellman = ObjectWrap::Unwrap<DiffieHellman>(args.This());
+ static Handle<Value> GetGenerator(const Arguments& args) {
+ DiffieHellman* diffieHellman =
+ ObjectWrap::Unwrap<DiffieHellman>(args.This());
HandleScope scope;
@@ -2880,8 +2888,9 @@ class DiffieHellman : public ObjectWrap {
return scope.Close(outString);
}
- static Handle<Value> GetPublicKey (const Arguments& args) {
- DiffieHellman* diffieHellman = ObjectWrap::Unwrap<DiffieHellman>(args.This());
+ static Handle<Value> GetPublicKey(const Arguments& args) {
+ DiffieHellman* diffieHellman =
+ ObjectWrap::Unwrap<DiffieHellman>(args.This());
HandleScope scope;
@@ -2890,12 +2899,14 @@ class DiffieHellman : public ObjectWrap {
}
if (diffieHellman->dh->pub_key == NULL) {
- return ThrowException(Exception::Error(String::New("No public key - did you forget to generate one?")));
+ return ThrowException(Exception::Error(
+ String::New("No public key - did you forget to generate one?")));
}
int dataSize = BN_num_bytes(diffieHellman->dh->pub_key);
char* data = new char[dataSize];
- BN_bn2bin(diffieHellman->dh->pub_key, reinterpret_cast<unsigned char*>(data));
+ BN_bn2bin(diffieHellman->dh->pub_key,
+ reinterpret_cast<unsigned char*>(data));
Local<Value> outString;
@@ -2910,8 +2921,9 @@ class DiffieHellman : public ObjectWrap {
return scope.Close(outString);
}
- static Handle<Value> GetPrivateKey (const Arguments& args) {
- DiffieHellman* diffieHellman = ObjectWrap::Unwrap<DiffieHellman>(args.This());
+ static Handle<Value> GetPrivateKey(const Arguments& args) {
+ DiffieHellman* diffieHellman =
+ ObjectWrap::Unwrap<DiffieHellman>(args.This());
HandleScope scope;
@@ -2920,12 +2932,14 @@ class DiffieHellman : public ObjectWrap {
}
if (diffieHellman->dh->priv_key == NULL) {
- return ThrowException(Exception::Error(String::New("No private key - did you forget to generate one?")));
+ return ThrowException(Exception::Error(
+ String::New("No private key - did you forget to generate one?")));
}
int dataSize = BN_num_bytes(diffieHellman->dh->priv_key);
char* data = new char[dataSize];
- BN_bn2bin(diffieHellman->dh->priv_key, reinterpret_cast<unsigned char*>(data));
+ BN_bn2bin(diffieHellman->dh->priv_key,
+ reinterpret_cast<unsigned char*>(data));
Local<Value> outString;
@@ -2940,10 +2954,11 @@ class DiffieHellman : public ObjectWrap {
return scope.Close(outString);
}
- static Handle<Value> ComputeSecret (const Arguments& args) {
+ static Handle<Value> ComputeSecret(const Arguments& args) {
HandleScope scope;
- DiffieHellman* diffieHellman = ObjectWrap::Unwrap<DiffieHellman>(args.This());
+ DiffieHellman* diffieHellman =
+ ObjectWrap::Unwrap<DiffieHellman>(args.This());
if (!diffieHellman->initialised_) {
return ThrowException(Exception::Error(String::New("Not initialized")));
@@ -2952,7 +2967,8 @@ class DiffieHellman : public ObjectWrap {
BIGNUM* key = 0;
if (args.Length() == 0) {
- return ThrowException(Exception::Error(String::New("First argument must be other party's public key")));
+ return ThrowException(Exception::Error(
+ String::New("First argument must be other party's public key")));
} else {
if (args[0]->IsString()) {
char* buf;
@@ -2964,24 +2980,26 @@ class DiffieHellman : public ObjectWrap {
}
if (len == -1) {
delete[] buf;
- return ThrowException(Exception::Error(String::New("Invalid argument")));
+ return ThrowException(Exception::Error(
+ String::New("Invalid argument")));
}
key = BN_bin2bn(reinterpret_cast<unsigned char*>(buf), len, 0);
delete[] buf;
} else if (Buffer::HasInstance(args[0])) {
Local<Object> buffer = args[0]->ToObject();
key = BN_bin2bn(
- reinterpret_cast<unsigned char*>(Buffer::Data(buffer)),
+ reinterpret_cast<unsigned char*>(Buffer::Data(buffer)),
Buffer::Length(buffer), 0);
} else {
- return ThrowException(Exception::Error(String::New("First argument must be other party's public key")));
+ return ThrowException(Exception::Error(
+ String::New("First argument must be other party's public key")));
}
}
int dataSize = DH_size(diffieHellman->dh);
char* data = new char[dataSize];
- int size = DH_compute_key(reinterpret_cast<unsigned char*>(data),
+ int size = DH_compute_key(reinterpret_cast<unsigned char*>(data),
key, diffieHellman->dh);
BN_free(key);
@@ -2993,9 +3011,11 @@ class DiffieHellman : public ObjectWrap {
return ThrowException(Exception::Error(String::New("Invalid key")));
} else if (checkResult) {
if (checkResult & DH_CHECK_PUBKEY_TOO_SMALL) {
- return ThrowException(Exception::Error(String::New("Supplied key is too small")));
+ return ThrowException(Exception::Error(
+ String::New("Supplied key is too small")));
} else if (checkResult & DH_CHECK_PUBKEY_TOO_LARGE) {
- return ThrowException(Exception::Error(String::New("Supplied key is too large")));
+ return ThrowException(Exception::Error(
+ String::New("Supplied key is too large")));
} else {
return ThrowException(Exception::Error(String::New("Invalid key")));
}
@@ -3019,14 +3039,16 @@ class DiffieHellman : public ObjectWrap {
static Handle<Value> SetPublicKey(const Arguments& args) {
HandleScope scope;
- DiffieHellman* diffieHellman = ObjectWrap::Unwrap<DiffieHellman>(args.This());
+ DiffieHellman* diffieHellman =
+ ObjectWrap::Unwrap<DiffieHellman>(args.This());
if (!diffieHellman->initialised_) {
return ThrowException(Exception::Error(String::New("Not initialized")));
}
if (args.Length() == 0) {
- return ThrowException(Exception::Error(String::New("First argument must be public key")));
+ return ThrowException(Exception::Error(
+ String::New("First argument must be public key")));
} else {
if (args[0]->IsString()) {
char* buf;
@@ -3038,19 +3060,21 @@ class DiffieHellman : public ObjectWrap {
}
if (len == -1) {
delete[] buf;
- return ThrowException(Exception::Error(String::New("Invalid argument")));
+ return ThrowException(Exception::Error(
+ String::New("Invalid argument")));
}
- diffieHellman->dh->pub_key =
+ diffieHellman->dh->pub_key =
BN_bin2bn(reinterpret_cast<unsigned char*>(buf), len, 0);
delete[] buf;
} else if (Buffer::HasInstance(args[0])) {
Local<Object> buffer = args[0]->ToObject();
- diffieHellman->dh->pub_key =
+ diffieHellman->dh->pub_key =
BN_bin2bn(
- reinterpret_cast<unsigned char*>(Buffer::Data(buffer)),
+ reinterpret_cast<unsigned char*>(Buffer::Data(buffer)),
Buffer::Length(buffer), 0);
} else {
- return ThrowException(Exception::Error(String::New("First argument must be public key")));
+ return ThrowException(Exception::Error(
+ String::New("First argument must be public key")));
}
}
@@ -3060,14 +3084,17 @@ class DiffieHellman : public ObjectWrap {
static Handle<Value> SetPrivateKey(const Arguments& args) {
HandleScope scope;
- DiffieHellman* diffieHellman = ObjectWrap::Unwrap<DiffieHellman>(args.This());
+ DiffieHellman* diffieHellman =
+ ObjectWrap::Unwrap<DiffieHellman>(args.This());
if (!diffieHellman->initialised_) {
- return ThrowException(Exception::Error(String::New("Not initialized")));
+ return ThrowException(Exception::Error(
+ String::New("Not initialized")));
}
if (args.Length() == 0) {
- return ThrowException(Exception::Error(String::New("First argument must be private key")));
+ return ThrowException(Exception::Error(
+ String::New("First argument must be private key")));
} else {
if (args[0]->IsString()) {
char* buf;
@@ -3079,31 +3106,33 @@ class DiffieHellman : public ObjectWrap {
}
if (len == -1) {
delete[] buf;
- return ThrowException(Exception::Error(String::New("Invalid argument")));
+ return ThrowException(Exception::Error(
+ String::New("Invalid argument")));
}
- diffieHellman->dh->priv_key =
+ diffieHellman->dh->priv_key =
BN_bin2bn(reinterpret_cast<unsigned char*>(buf), len, 0);
delete[] buf;
} else if (Buffer::HasInstance(args[0])) {
Local<Object> buffer = args[0]->ToObject();
- diffieHellman->dh->priv_key =
+ diffieHellman->dh->priv_key =
BN_bin2bn(
- reinterpret_cast<unsigned char*>(Buffer::Data(buffer)),
+ reinterpret_cast<unsigned char*>(Buffer::Data(buffer)),
Buffer::Length(buffer), 0);
} else {
- return ThrowException(Exception::Error(String::New("First argument must be private key")));
+ return ThrowException(Exception::Error(
+ String::New("First argument must be private key")));
}
}
return args.This();
}
- DiffieHellman () : ObjectWrap () {
+ DiffieHellman() : ObjectWrap() {
initialised_ = false;
dh = NULL;
}
- ~DiffieHellman () {
+ ~DiffieHellman() {
if (dh != NULL) {
DH_free(dh);
}
@@ -3124,13 +3153,14 @@ class DiffieHellman : public ObjectWrap {
int len = DecodeBytes(str);
*buf = new char[len];
int written = DecodeWrite(*buf, len, str, BINARY);
- if(written != len) {
+ if (written != len) {
return -1;
}
return len;
}
- static int DecodeWithEncoding(Handle<Value> str, Handle<Value> enc, char** buf) {
+ static int DecodeWithEncoding(Handle<Value> str, Handle<Value> enc,
+ char** buf) {
int len = DecodeBinary(str, buf);
if (len == -1) {
return len;
@@ -3161,7 +3191,8 @@ class DiffieHellman : public ObjectWrap {
return len;
}
- static Local<Value> EncodeWithEncoding(Handle<Value> enc, char* buf, int len) {
+ static Local<Value> EncodeWithEncoding(Handle<Value> enc, char* buf,
+ int len) {
HandleScope scope;
Local<Value> outString;
--
1.7.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment