Index of every page on this site: llms.txt
All skills
mcollina avatar

/nodejs-core

@c75d70d
by Matteo Collinamcollina/skills2k stars
146
Run it yourself

Contributes to and debugs Node.js core, including nodejs/node commit and PR tone, contribution workflows, native crashes, V8 performance, node-gyp builds, N-API bindings, and libuv issues. Use when drafting or reviewing a Node.js core commit or pull request, working in nodejs/node, or diagnosing C++ addons, binding.gyp failures, segfaults, native leaks, V8 deoptimizations, and event-loop internals.

Use this Skill: https://skilld.dev/gh/mcollina/skills/nodejs-core

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

rulescrypto-internals.md

≈3k tokens on demand. Your agent reads this file only when SKILL.md points to it.

Node.js Crypto Internals

Node.js crypto module wraps OpenSSL (or BoringSSL in some builds). Understanding the internals helps optimize cryptographic operations and debug security issues.

Architecture

JavaScript Layer (lib/crypto.js)
├── createHash(), createCipher()
├── randomBytes(), pbkdf2()
└── generateKeyPair(), sign(), verify()
        │
        ▼
C++ Bindings (src/crypto/)
├── crypto_hash.cc
├── crypto_cipher.cc
├── crypto_random.cc
└── crypto_keys.cc
        │
        ▼
OpenSSL
├── EVP_* (high-level crypto)
├── RAND_* (random number generation)
└── X509_* (certificates)

Thread Pool vs Main Thread

Operations Using Thread Pool

// These use the thread pool (libuv):
crypto.pbkdf2(password, salt, iterations, keylen, 'sha512', callback);
crypto.scrypt(password, salt, keylen, callback);
crypto.randomBytes(256, callback);
crypto.generateKeyPair('rsa', options, callback);

// Thread pool can become a bottleneck!

Operations on Main Thread

// These run synchronously on the main thread:
const hash = crypto.createHash('sha256').update(data).digest();
const cipher = crypto.createCipheriv(algorithm, key, iv);
const hmac = crypto.createHmac('sha256', key).update(data).digest();

Hash Operations

JavaScript API

const crypto = require('node:crypto');

// Stream-based hashing
const hash = crypto.createHash('sha256');
hash.update('data1');
hash.update('data2');
const digest = hash.digest('hex');

// One-shot (Node.js 15+)
const digest = crypto.hash('sha256', data, 'hex');

C++ Implementation

// From src/crypto/crypto_hash.cc

void Hash::HashUpdate(const FunctionCallbackInfo<Value>& args) {
  Environment* env = Environment::GetCurrent(args);
  Hash* hash;
  ASSIGN_OR_RETURN_UNWRAP(&hash, args.This());

  // Get data
  ArrayBufferOrViewContents<unsigned char> buf(args[0]);

  // Update OpenSSL context
  int r = EVP_DigestUpdate(hash->mdctx_.get(), buf.data(), buf.size());

  if (r != 1) {
    return ThrowCryptoError(env, ERR_get_error(), "EVP_DigestUpdate");
  }
}

void Hash::HashDigest(const FunctionCallbackInfo<Value>& args) {
  Environment* env = Environment::GetCurrent(args);
  Hash* hash;
  ASSIGN_OR_RETURN_UNWRAP(&hash, args.This());

  unsigned char md_value[EVP_MAX_MD_SIZE];
  unsigned int md_len;

  // Finalize hash
  int r = EVP_DigestFinal_ex(hash->mdctx_.get(), md_value, &md_len);

  if (r != 1) {
    return ThrowCryptoError(env, ERR_get_error(), "EVP_DigestFinal_ex");
  }

  // Return as buffer or encoded string
  Local<Value> result = StringBytes::Encode(
      env->isolate(),
      reinterpret_cast<const char*>(md_value),
      md_len,
      encoding);

  args.GetReturnValue().Set(result);
}

Cipher Operations

Encryption

const crypto = require('node:crypto');

function encrypt(text, key, iv) {
  // Create cipher with specific algorithm
  const cipher = crypto.createCipheriv('aes-256-gcm', key, iv);

  // Set additional authenticated data (for AEAD modes)
  cipher.setAAD(Buffer.from('additional data'));

  // Encrypt
  let encrypted = cipher.update(text, 'utf8', 'hex');
  encrypted += cipher.final('hex');

  // Get auth tag (for AEAD modes)
  const authTag = cipher.getAuthTag();

  return { encrypted, authTag };
}

C++ Implementation

// From src/crypto/crypto_cipher.cc

void CipherBase::Update(const FunctionCallbackInfo<Value>& args) {
  Environment* env = Environment::GetCurrent(args);
  CipherBase* cipher;
  ASSIGN_OR_RETURN_UNWRAP(&cipher, args.This());

  ArrayBufferOrViewContents<unsigned char> buf(args[0]);

  // Allocate output buffer
  int out_len = buf.size() + EVP_CIPHER_CTX_block_size(cipher->ctx_.get());
  AllocatedBuffer out = AllocatedBuffer::AllocateManaged(env, out_len);

  // Call OpenSSL
  int r = EVP_CipherUpdate(cipher->ctx_.get(),
                           reinterpret_cast<unsigned char*>(out.data()),
                           &out_len,
                           buf.data(),
                           buf.size());

  if (r != 1) {
    return ThrowCryptoError(env, ERR_get_error(), "EVP_CipherUpdate");
  }

  // Resize and return
  out.Resize(out_len);
  args.GetReturnValue().Set(out.ToBuffer().ToLocalChecked());
}

Random Number Generation

Secure Random

const crypto = require('node:crypto');

// Async (uses thread pool)
crypto.randomBytes(256, (err, buf) => {
  console.log(buf);
});

// Sync (blocks main thread)
const buf = crypto.randomBytes(256);

// Fill existing buffer
const existing = Buffer.alloc(256);
crypto.randomFillSync(existing);

C++ Implementation

// From src/crypto/crypto_random.cc

void RandomBytes(const FunctionCallbackInfo<Value>& args) {
  Environment* env = Environment::GetCurrent(args);

  const int64_t size = args[0].As<Integer>()->Value();

  // Check if callback provided (async)
  if (args[1]->IsFunction()) {
    // Async: queue to thread pool
    RandomBytesJob* job = new RandomBytesJob(env);
    job->size = size;
    job->callback.Reset(env->isolate(), args[1].As<Function>());

    uv_queue_work(env->event_loop(),
                  &job->work_req_,
                  [](uv_work_t* req) {
                    RandomBytesJob* job = ContainerOf(&RandomBytesJob::work_req_, req);
                    // Run on thread pool
                    RAND_bytes(job->data, job->size);
                  },
                  [](uv_work_t* req, int status) {
                    // Complete on main thread
                    RandomBytesJob* job = ContainerOf(&RandomBytesJob::work_req_, req);
                    job->Complete();
                  });
  } else {
    // Sync: run immediately
    AllocatedBuffer buf = AllocatedBuffer::AllocateManaged(env, size);
    int r = RAND_bytes(reinterpret_cast<unsigned char*>(buf.data()), size);

    if (r != 1) {
      return ThrowCryptoError(env, ERR_get_error(), "RAND_bytes");
    }

    args.GetReturnValue().Set(buf.ToBuffer().ToLocalChecked());
  }
}

Key Derivation

PBKDF2

const crypto = require('node:crypto');

// Async (thread pool) - recommended
crypto.pbkdf2('password', 'salt', 100000, 64, 'sha512', (err, key) => {
  console.log(key.toString('hex'));
});

// Sync - blocks event loop!
const key = crypto.pbkdf2Sync('password', 'salt', 100000, 64, 'sha512');

Scrypt (Memory-Hard)

// More resistant to hardware attacks
crypto.scrypt('password', 'salt', 64, {
  N: 16384,  // CPU/memory cost
  r: 8,      // Block size
  p: 1       // Parallelization
}, (err, key) => {
  console.log(key.toString('hex'));
});

Performance Comparison

const crypto = require('node:crypto');

// Benchmark different KDFs
async function benchmark() {
  const password = 'test-password';
  const salt = crypto.randomBytes(16);

  // PBKDF2
  console.time('pbkdf2');
  for (let i = 0; i < 100; i++) {
    await new Promise(resolve =>
      crypto.pbkdf2(password, salt, 100000, 64, 'sha512', resolve)
    );
  }
  console.timeEnd('pbkdf2');

  // Scrypt
  console.time('scrypt');
  for (let i = 0; i < 100; i++) {
    await new Promise(resolve =>
      crypto.scrypt(password, salt, 64, resolve)
    );
  }
  console.timeEnd('scrypt');
}

Performance Optimization

Reuse Crypto Contexts

// BAD: Create new hash for each piece of data
function hashMany(items) {
  return items.map(item =>
    crypto.createHash('sha256').update(item).digest('hex')
  );
}

// GOOD: Stream data through single hash
function hashCombined(items) {
  const hash = crypto.createHash('sha256');
  for (const item of items) {
    hash.update(item);
    hash.update('\n');  // Separator
  }
  return hash.digest('hex');
}

Buffer Reuse

// For high-throughput encryption
class CipherStream {
  constructor(key, iv) {
    this.cipher = crypto.createCipheriv('aes-256-ctr', key, iv);
    this.outputBuffer = Buffer.allocUnsafe(65536);
  }

  encrypt(data) {
    // Reuse output buffer when possible
    const encrypted = this.cipher.update(data);
    if (encrypted.length <= this.outputBuffer.length) {
      encrypted.copy(this.outputBuffer);
      return this.outputBuffer.slice(0, encrypted.length);
    }
    return encrypted;
  }
}

Avoid Sync in Hot Paths

// BAD: Sync operations block event loop
app.post('/login', (req, res) => {
  const hash = crypto.pbkdf2Sync(password, salt, 100000, 64, 'sha512');
  // Server blocked for ~100ms!
});

// GOOD: Use async
app.post('/login', async (req, res) => {
  const hash = await promisify(crypto.pbkdf2)(password, salt, 100000, 64, 'sha512');
});

WebCrypto API

Node.js 15+ includes the Web Crypto API:

const { subtle } = require('node:crypto').webcrypto;

async function encryptWithWebCrypto(data, password) {
  // Derive key from password
  const keyMaterial = await subtle.importKey(
    'raw',
    Buffer.from(password),
    'PBKDF2',
    false,
    ['deriveBits', 'deriveKey']
  );

  const key = await subtle.deriveKey(
    {
      name: 'PBKDF2',
      salt: Buffer.from('salt'),
      iterations: 100000,
      hash: 'SHA-256'
    },
    keyMaterial,
    { name: 'AES-GCM', length: 256 },
    false,
    ['encrypt', 'decrypt']
  );

  const iv = crypto.randomBytes(12);

  const encrypted = await subtle.encrypt(
    { name: 'AES-GCM', iv },
    key,
    Buffer.from(data)
  );

  return { encrypted: Buffer.from(encrypted), iv };
}

OpenSSL Providers

Node.js 17+ supports OpenSSL 3.0 providers:

const crypto = require('node:crypto');

// Check available algorithms
console.log(crypto.getHashes());
console.log(crypto.getCiphers());

// Check if FIPS mode is available
console.log('FIPS available:', crypto.getFips());

// Enable FIPS mode (if compiled with FIPS support)
crypto.setFips(1);

Debugging Crypto Issues

OpenSSL Errors

const crypto = require('node:crypto');

try {
  const cipher = crypto.createCipheriv('aes-256-gcm', 'short-key', 'short-iv');
} catch (err) {
  console.error('Crypto error:', err.message);
  console.error('OpenSSL error:', err.opensslErrorStack);
}

Checking Algorithm Support

function isAlgorithmSupported(name) {
  return crypto.getHashes().includes(name) ||
         crypto.getCiphers().includes(name);
}

// Check before use
if (!isAlgorithmSupported('sha3-256')) {
  console.warn('SHA3-256 not available, using SHA-256');
}

Common Pitfalls

Timing Attacks

// BAD: Vulnerable to timing attack
function checkSignature(expected, actual) {
  return expected === actual;  // Short-circuits on first difference
}

// GOOD: Constant-time comparison
function checkSignature(expected, actual) {
  return crypto.timingSafeEqual(
    Buffer.from(expected),
    Buffer.from(actual)
  );
}

IV Reuse

// BAD: Reusing IV
const iv = crypto.randomBytes(12);
messages.forEach(msg => encrypt(msg, key, iv));  // Security vulnerability!

// GOOD: Unique IV per message
messages.forEach(msg => {
  const iv = crypto.randomBytes(12);
  encrypt(msg, key, iv);
});

Key in Memory

// Clear sensitive data after use
function secureEncrypt(data, password) {
  const key = crypto.scryptSync(password, salt, 32);
  try {
    return encrypt(data, key);
  } finally {
    key.fill(0);  // Overwrite key in memory
  }
}

References

Source: SKILL.md on GitHub

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

1 alert2mo5 checks · Risk SAFE
  • Gen Agent Trust Hub2mo

    The skill provides comprehensive guidance, documentation, and best practices for developing, building, testing, and debugging Node.js core and its native C++ addons. It contains no security risks, malicious code, or prompt injections.

  • Socket2mo

    No alerts

  • Snyk2mo

    Risk: LOW · No issues

  • Runlayer8mo

    13/23 files flagged

  • ZeroLeaks7mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at c75d70d. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub last month.

Activeupdated 4 months ago
Other metadata
metadata
{
  "tags": "nodejs, nodejs-core, contributing, commit-message, pull-request, v8, libuv, cpp, native-addons, performance, debugging, internals"
}

README badge

README badge for mcollina/skills/nodejs-core