@@ -157,28 +157,26 @@ void ECDH::GenerateKeys(const FunctionCallbackInfo<Value>& args) {
157
157
ECPointPointer ECDH::BufferToPoint (Environment* env,
158
158
const EC_GROUP* group,
159
159
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
+ }
161
165
162
- ECPointPointer pub ( EC_POINT_new ( group) );
166
+ auto pub = ECPointPointer::New ( group);
163
167
if (!pub) {
164
168
THROW_ERR_CRYPTO_OPERATION_FAILED (env,
165
169
" Failed to allocate EC_POINT for a public key" );
166
170
return pub;
167
171
}
168
172
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 {};
173
179
}
174
- r = EC_POINT_oct2point (
175
- group,
176
- pub.get (),
177
- input.data (),
178
- input.size (),
179
- nullptr );
180
- if (!r)
181
- return ECPointPointer ();
182
180
183
181
return pub;
184
182
}
@@ -196,10 +194,7 @@ void ECDH::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
196
194
if (!ecdh->IsKeyPairValid ())
197
195
return THROW_ERR_CRYPTO_INVALID_KEYPAIR (env);
198
196
199
- ECPointPointer pub (
200
- ECDH::BufferToPoint (env,
201
- ecdh->group_ ,
202
- args[0 ]));
197
+ auto pub = ECDH::BufferToPoint (env, ecdh->group_ , args[0 ]);
203
198
if (!pub) {
204
199
args.GetReturnValue ().Set (
205
200
FIXED_ONE_BYTE_STRING (env->isolate (),
@@ -217,7 +212,7 @@ void ECDH::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
217
212
}
218
213
219
214
if (!ECDH_compute_key (
220
- bs->Data (), bs->ByteLength (), pub. get () , ecdh->key_ .get (), nullptr ))
215
+ bs->Data (), bs->ByteLength (), pub, ecdh->key_ .get (), nullptr ))
221
216
return THROW_ERR_CRYPTO_OPERATION_FAILED (env, " Failed to compute ECDH key" );
222
217
223
218
Local<ArrayBuffer> ab = ArrayBuffer::New (env->isolate (), std::move (bs));
@@ -317,16 +312,15 @@ void ECDH::SetPrivateKey(const FunctionCallbackInfo<Value>& args) {
317
312
const BIGNUM* priv_key = EC_KEY_get0_private_key (new_key.get ());
318
313
CHECK_NOT_NULL (priv_key);
319
314
320
- ECPointPointer pub ( EC_POINT_new ( ecdh->group_ ) );
315
+ auto pub = ECPointPointer::New ( ecdh->group_ );
321
316
CHECK (pub);
322
317
323
- if (!EC_POINT_mul (ecdh->group_ , pub.get (), priv_key,
324
- nullptr , nullptr , nullptr )) {
318
+ if (!pub.mul (ecdh->group_ , priv_key)) {
325
319
return THROW_ERR_CRYPTO_OPERATION_FAILED (env,
326
320
" Failed to generate ECDH public key" );
327
321
}
328
322
329
- if (!EC_KEY_set_public_key (new_key.get (), pub. get () ))
323
+ if (!EC_KEY_set_public_key (new_key.get (), pub))
330
324
return THROW_ERR_CRYPTO_OPERATION_FAILED (env,
331
325
" Failed to set generated public key" );
332
326
@@ -344,16 +338,13 @@ void ECDH::SetPublicKey(const FunctionCallbackInfo<Value>& args) {
344
338
345
339
MarkPopErrorOnReturn mark_pop_error_on_return;
346
340
347
- ECPointPointer pub (
348
- ECDH::BufferToPoint (env,
349
- ecdh->group_ ,
350
- args[0 ]));
341
+ auto pub = ECDH::BufferToPoint (env, ecdh->group_ , args[0 ]);
351
342
if (!pub) {
352
343
return THROW_ERR_CRYPTO_OPERATION_FAILED (env,
353
344
" Failed to convert Buffer to EC_POINT" );
354
345
}
355
346
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);
357
348
if (!r) {
358
349
return THROW_ERR_CRYPTO_OPERATION_FAILED (env,
359
350
" Failed to set EC_POINT as the public key" );
@@ -403,9 +394,8 @@ void ECDH::ConvertKey(const FunctionCallbackInfo<Value>& args) {
403
394
if (!group)
404
395
return THROW_ERR_CRYPTO_OPERATION_FAILED (env, " Failed to get EC_GROUP" );
405
396
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) {
409
399
return THROW_ERR_CRYPTO_OPERATION_FAILED (env,
410
400
" Failed to convert Buffer to EC_POINT" );
411
401
}
@@ -416,7 +406,7 @@ void ECDH::ConvertKey(const FunctionCallbackInfo<Value>& args) {
416
406
417
407
const char * error;
418
408
Local<Object> buf;
419
- if (!ECPointToBuffer (env, group, pub. get () , form, &error).ToLocal (&buf))
409
+ if (!ECPointToBuffer (env, group, pub, form, &error).ToLocal (&buf))
420
410
return THROW_ERR_CRYPTO_OPERATION_FAILED (env, error);
421
411
args.GetReturnValue ().Set (buf);
422
412
}
@@ -698,14 +688,13 @@ WebCryptoKeyExportStatus ECKeyExportTraits::DoExport(
698
688
if (have == 0 ) return WebCryptoKeyExportStatus::FAILED;
699
689
ECKeyPointer ec (EC_KEY_new ());
700
690
CHECK_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));
709
698
auto pkey = EVPKeyPointer::New ();
710
699
CHECK_EQ (1 , EVP_PKEY_set1_EC_KEY (pkey.get (), ec.get ()));
711
700
auto bio = pkey.derPublicKey ();
0 commit comments