Skip to content

Commit 4793bb2

Browse files
tniessenRafaelGSS
authored andcommitted
crypto: remove CipherBase::Init
As far as I can tell, the `iv` parameter can never be `undefined` (but it can be `null`!), so this code appears to have been dead since Node.js 22. This change removes dead code and adds a tiny test case for passing `undefined` as the IV. Refs: #50973 PR-URL: #57787 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Filip Skokan <panva.ip@gmail.com>
1 parent deb434e commit 4793bb2

File tree

4 files changed

+9
-74
lines changed

4 files changed

+9
-74
lines changed

lib/internal/crypto/cipher.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,7 @@ function getUIntOption(options, key) {
115115
function createCipherBase(cipher, credential, options, decipher, iv) {
116116
const authTagLength = getUIntOption(options, 'authTagLength');
117117
this[kHandle] = new CipherBase(decipher);
118-
if (iv === undefined) {
119-
this[kHandle].init(cipher, credential, authTagLength);
120-
} else {
121-
this[kHandle].initiv(cipher, credential, iv, authTagLength);
122-
}
118+
this[kHandle].initiv(cipher, credential, iv, authTagLength);
123119
this._decoder = null;
124120

125121
ReflectApply(LazyTransform, this, [options]);

src/crypto/crypto_cipher.cc

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,6 @@ void CipherBase::Initialize(Environment* env, Local<Object> target) {
231231

232232
t->InstanceTemplate()->SetInternalFieldCount(CipherBase::kInternalFieldCount);
233233

234-
SetProtoMethod(isolate, t, "init", Init);
235234
SetProtoMethod(isolate, t, "initiv", InitIv);
236235
SetProtoMethod(isolate, t, "update", Update);
237236
SetProtoMethod(isolate, t, "final", Final);
@@ -275,7 +274,6 @@ void CipherBase::RegisterExternalReferences(
275274
ExternalReferenceRegistry* registry) {
276275
registry->Register(New);
277276

278-
registry->Register(Init);
279277
registry->Register(InitIv);
280278
registry->Register(Update);
281279
registry->Register(Final);
@@ -347,69 +345,6 @@ void CipherBase::CommonInit(std::string_view cipher_type,
347345
}
348346
}
349347

350-
void CipherBase::Init(std::string_view cipher_type,
351-
const ArrayBufferOrViewContents<unsigned char>& key_buf,
352-
unsigned int auth_tag_len) {
353-
HandleScope scope(env()->isolate());
354-
MarkPopErrorOnReturn mark_pop_error_on_return;
355-
const auto cipher = Cipher::FromName(cipher_type);
356-
if (!cipher) {
357-
return THROW_ERR_CRYPTO_UNKNOWN_CIPHER(env());
358-
}
359-
360-
unsigned char key[Cipher::MAX_KEY_LENGTH];
361-
unsigned char iv[Cipher::MAX_IV_LENGTH];
362-
363-
ncrypto::Buffer<const unsigned char> keyBuf{
364-
.data = key_buf.data(),
365-
.len = key_buf.size(),
366-
};
367-
int key_len = cipher.bytesToKey(Digest::MD5, keyBuf, key, iv);
368-
CHECK_NE(key_len, 0);
369-
370-
if (kind_ == kCipher &&
371-
(cipher.isCtrMode() || cipher.isGcmMode() || cipher.isCcmMode())) {
372-
// Ignore the return value (i.e. possible exception) because we are
373-
// not calling back into JS anyway.
374-
ProcessEmitWarning(env(),
375-
"Use Cipheriv for counter mode of %s",
376-
cipher_type);
377-
}
378-
379-
CommonInit(cipher_type,
380-
cipher,
381-
key,
382-
key_len,
383-
iv,
384-
cipher.getIvLength(),
385-
auth_tag_len);
386-
}
387-
388-
void CipherBase::Init(const FunctionCallbackInfo<Value>& args) {
389-
CipherBase* cipher;
390-
ASSIGN_OR_RETURN_UNWRAP(&cipher, args.This());
391-
Environment* env = Environment::GetCurrent(args);
392-
393-
CHECK_GE(args.Length(), 3);
394-
395-
const Utf8Value cipher_type(args.GetIsolate(), args[0]);
396-
ArrayBufferOrViewContents<unsigned char> key_buf(args[1]);
397-
if (!key_buf.CheckSizeInt32())
398-
return THROW_ERR_OUT_OF_RANGE(env, "password is too large");
399-
400-
// Don't assign to cipher->auth_tag_len_ directly; the value might not
401-
// represent a valid length at this point.
402-
unsigned int auth_tag_len;
403-
if (args[2]->IsUint32()) {
404-
auth_tag_len = args[2].As<Uint32>()->Value();
405-
} else {
406-
CHECK(args[2]->IsInt32() && args[2].As<Int32>()->Value() == -1);
407-
auth_tag_len = kNoAuthTagLength;
408-
}
409-
410-
cipher->Init(cipher_type.ToStringView(), key_buf, auth_tag_len);
411-
}
412-
413348
void CipherBase::InitIv(std::string_view cipher_type,
414349
const ByteSource& key_buf,
415350
const ArrayBufferOrViewContents<unsigned char>& iv_buf,

src/crypto/crypto_cipher.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@ class CipherBase : public BaseObject {
5050
const unsigned char* iv,
5151
int iv_len,
5252
unsigned int auth_tag_len);
53-
void Init(std::string_view cipher_type,
54-
const ArrayBufferOrViewContents<unsigned char>& key_buf,
55-
unsigned int auth_tag_len);
5653
void InitIv(std::string_view cipher_type,
5754
const ByteSource& key_buf,
5855
const ArrayBufferOrViewContents<unsigned char>& iv_buf,
@@ -73,7 +70,6 @@ class CipherBase : public BaseObject {
7370
bool MaybePassAuthTagToOpenSSL();
7471

7572
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
76-
static void Init(const v8::FunctionCallbackInfo<v8::Value>& args);
7773
static void InitIv(const v8::FunctionCallbackInfo<v8::Value>& args);
7874
static void Update(const v8::FunctionCallbackInfo<v8::Value>& args);
7975
static void Final(const v8::FunctionCallbackInfo<v8::Value>& args);

test/parallel/test-crypto-cipheriv-decipheriv.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,14 @@ for (let n = 1; n < 256; n += 1) {
171171
errMessage);
172172
}
173173

174+
// And so should undefined be (regardless of mode).
175+
assert.throws(
176+
() => crypto.createCipheriv('aes-128-ecb', Buffer.alloc(16)),
177+
{ code: 'ERR_INVALID_ARG_TYPE' });
178+
assert.throws(
179+
() => crypto.createCipheriv('aes-128-ecb', Buffer.alloc(16), undefined),
180+
{ code: 'ERR_INVALID_ARG_TYPE' });
181+
174182
// Correctly sized IV should be accepted in CBC mode.
175183
crypto.createCipheriv('aes-128-cbc', Buffer.alloc(16), Buffer.alloc(16));
176184

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy