@@ -157,28 +157,26 @@ void ECDH::GenerateKeys(const FunctionCallbackInfo<Value>& args){
157157ECPointPointer ECDH::BufferToPoint (Environment* env,
158158const EC_GROUP* group,
159159 Local<Value> buf){
160- int r;
160+ ArrayBufferOrViewContents<unsigned char > input (buf);
161+ if (!input.CheckSizeInt32 ()) [[unlikely]]{
162+ THROW_ERR_OUT_OF_RANGE (env, " buffer is too big" );
163+ return {};
164+ }
161165
162- ECPointPointer pub ( EC_POINT_new ( group) );
166+ auto pub = ECPointPointer::New ( group);
163167if (!pub){
164168THROW_ERR_CRYPTO_OPERATION_FAILED (env,
165169" Failed to allocate EC_POINT for a public key" );
166170return pub;
167171 }
168172
169- ArrayBufferOrViewContents<unsigned char > input (buf);
170- if (!input.CheckSizeInt32 ()) [[unlikely]]{
171- THROW_ERR_OUT_OF_RANGE (env, " buffer is too big" );
172- return ECPointPointer ();
173+ ncrypto::Buffer<const unsigned char > buffer{
174+ .data = input.data (),
175+ .len = input.size (),
176+ };
177+ if (!pub.setFromBuffer (buffer, group)){
178+ return {};
173179 }
174- r = EC_POINT_oct2point (
175- group,
176- pub.get (),
177- input.data (),
178- input.size (),
179- nullptr );
180- if (!r)
181- return ECPointPointer ();
182180
183181return pub;
184182}
@@ -196,10 +194,7 @@ void ECDH::ComputeSecret(const FunctionCallbackInfo<Value>& args){
196194if (!ecdh->IsKeyPairValid ())
197195return THROW_ERR_CRYPTO_INVALID_KEYPAIR (env);
198196
199- ECPointPointer pub (
200- ECDH::BufferToPoint (env,
201- ecdh->group_ ,
202- args[0 ]));
197+ auto pub = ECDH::BufferToPoint (env, ecdh->group_ , args[0 ]);
203198if (!pub){
204199 args.GetReturnValue ().Set (
205200FIXED_ONE_BYTE_STRING (env->isolate (),
@@ -217,7 +212,7 @@ void ECDH::ComputeSecret(const FunctionCallbackInfo<Value>& args){
217212 }
218213
219214if (!ECDH_compute_key (
220- bs->Data (), bs->ByteLength (), pub. get () , ecdh->key_ .get (), nullptr ))
215+ bs->Data (), bs->ByteLength (), pub, ecdh->key_ .get (), nullptr ))
221216return THROW_ERR_CRYPTO_OPERATION_FAILED (env, " Failed to compute ECDH key" );
222217
223218 Local<ArrayBuffer> ab = ArrayBuffer::New (env->isolate (), std::move (bs));
@@ -317,16 +312,15 @@ void ECDH::SetPrivateKey(const FunctionCallbackInfo<Value>& args){
317312const BIGNUM* priv_key = EC_KEY_get0_private_key (new_key.get ());
318313CHECK_NOT_NULL (priv_key);
319314
320- ECPointPointer pub ( EC_POINT_new ( ecdh->group_ ) );
315+ auto pub = ECPointPointer::New ( ecdh->group_ );
321316CHECK (pub);
322317
323- if (!EC_POINT_mul (ecdh->group_ , pub.get (), priv_key,
324- nullptr , nullptr , nullptr )){
318+ if (!pub.mul (ecdh->group_ , priv_key)){
325319return THROW_ERR_CRYPTO_OPERATION_FAILED (env,
326320" Failed to generate ECDH public key" );
327321 }
328322
329- if (!EC_KEY_set_public_key (new_key.get (), pub. get () ))
323+ if (!EC_KEY_set_public_key (new_key.get (), pub))
330324return THROW_ERR_CRYPTO_OPERATION_FAILED (env,
331325" Failed to set generated public key" );
332326
@@ -344,16 +338,13 @@ void ECDH::SetPublicKey(const FunctionCallbackInfo<Value>& args){
344338
345339 MarkPopErrorOnReturn mark_pop_error_on_return;
346340
347- ECPointPointer pub (
348- ECDH::BufferToPoint (env,
349- ecdh->group_ ,
350- args[0 ]));
341+ auto pub = ECDH::BufferToPoint (env, ecdh->group_ , args[0 ]);
351342if (!pub){
352343return THROW_ERR_CRYPTO_OPERATION_FAILED (env,
353344" Failed to convert Buffer to EC_POINT" );
354345 }
355346
356- int r = EC_KEY_set_public_key (ecdh->key_ .get (), pub. get () );
347+ int r = EC_KEY_set_public_key (ecdh->key_ .get (), pub);
357348if (!r){
358349return THROW_ERR_CRYPTO_OPERATION_FAILED (env,
359350" Failed to set EC_POINT as the public key" );
@@ -403,9 +394,8 @@ void ECDH::ConvertKey(const FunctionCallbackInfo<Value>& args){
403394if (!group)
404395return THROW_ERR_CRYPTO_OPERATION_FAILED (env, " Failed to get EC_GROUP" );
405396
406- ECPointPointer pub (ECDH::BufferToPoint (env, group, args[0 ]));
407-
408- if (pub == nullptr ){
397+ auto pub = ECDH::BufferToPoint (env, group, args[0 ]);
398+ if (!pub){
409399return THROW_ERR_CRYPTO_OPERATION_FAILED (env,
410400" Failed to convert Buffer to EC_POINT" );
411401 }
@@ -416,7 +406,7 @@ void ECDH::ConvertKey(const FunctionCallbackInfo<Value>& args){
416406
417407const char * error;
418408 Local<Object> buf;
419- if (!ECPointToBuffer (env, group, pub. get () , form, &error).ToLocal (&buf))
409+ if (!ECPointToBuffer (env, group, pub, form, &error).ToLocal (&buf))
420410return THROW_ERR_CRYPTO_OPERATION_FAILED (env, error);
421411 args.GetReturnValue ().Set (buf);
422412}
@@ -698,14 +688,13 @@ WebCryptoKeyExportStatus ECKeyExportTraits::DoExport(
698688if (have == 0 ) return WebCryptoKeyExportStatus::FAILED;
699689 ECKeyPointer ec (EC_KEY_new ());
700690CHECK_EQ (1 , EC_KEY_set_group (ec.get (), group));
701- ECPointPointer uncompressed (EC_POINT_new (group));
702- CHECK_EQ (1 ,
703- EC_POINT_oct2point (group,
704- uncompressed.get (),
705- data.data <unsigned char >(),
706- data.size (),
707- nullptr ));
708- CHECK_EQ (1 , EC_KEY_set_public_key (ec.get (), uncompressed.get ()));
691+ auto uncompressed = ECPointPointer::New (group);
692+ ncrypto::Buffer<const unsigned char > buffer{
693+ .data = data.data <unsigned char >(),
694+ .len = data.size (),
695+ };
696+ CHECK (uncompressed.setFromBuffer (buffer, group));
697+ CHECK_EQ (1 , EC_KEY_set_public_key (ec.get (), uncompressed));
709698auto pkey = EVPKeyPointer::New ();
710699CHECK_EQ (1 , EVP_PKEY_set1_EC_KEY (pkey.get (), ec.get ()));
711700auto bio = pkey.derPublicKey ();
0 commit comments