Jump to content
Setting up an AI agent? ReverseEngineering.wiki skill (works from the command line, no configuration): https://reverseengineering.wiki/skill/rewiki/SKILL.md · setup for every client, including MCP: https://reverseengineering.wiki/llms.txt · REWiki:Connect

Library:Windows CNG

From ReverseEngineering.wiki
Library
Developer Microsoft
Platforms Windows Vista and later
License proprietary (OS component)
On this wiki Facts · Code · Notes · Topics

Cryptography API: Next Generation (bcrypt.dll BCrypt* and ncrypt.dll NCrypt*), Windows' crypto API: algorithms are chosen by wide-string identifiers such as L"AES" and L"SHA256", opened with BCryptOpenAlgorithmProvider.

Overview

CNG replaces the old CryptoAPI (advapi32 Crypt*, CALG_* ids). Primitives are in bcrypt.dll (BCryptOpenAlgorithmProvider, BCryptSetProperty, BCryptGenerateSymmetricKey, BCryptImportKey, BCryptEncrypt/BCryptDecrypt, BCryptCreateHash, BCryptHashData, BCryptFinishHash, BCryptGenRandom); key storage with providers is ncrypt.dll (NCrypt*). Part of Technique:Identifying cryptographic libraries; imports are read as in Format:PE (Portable Executable) and Platform:Windows.

How it shows up

A program picks an algorithm by passing a wide string as pszAlgId: BCryptOpenAlgorithmProvider(&hAlg, L"AES", NULL, 0). The identifier strings are UTF-16 in .rdata (Fact:Windows CNG/string/Algorithm identifiers); the cross-reference from the call site names the algorithm. Chaining mode is set afterwards with BCryptSetProperty(hAlg, L"ChainingMode", L"ChainingModeGCM", ...) (also CBC, CCM, CFB, ECB). Flag 0x8 (BCRYPT_ALG_HANDLE_HMAC_FLAG) makes a hash provider an HMAC; BCRYPT_USE_SYSTEM_PREFERRED_RNG 0x2 lets BCryptGenRandom run with a NULL handle. The provider names are L"Microsoft Primitive Provider" and L"Microsoft Platform Crypto Provider". Source: MinGW-w64 bcrypt.h.

Key blobs

Imported or exported blobs start with a 4-byte magic that is readable ASCII (Fact:Windows CNG/magic/RSA public key blob, Fact:Windows CNG/enum/Key blob magics): RSA1 public, RSA2 private, RSA3 full private; ECC blobs ECK1/ECS1 families; KDBM for key data blobs.

Code & repositories

  • https://github.com/mirror/mingw-w64 @ 93f3505a758fe70e56678f00e753af3bc4f640bb · license: ZPL-2.1 (declared by the contributor for the files used; GitHub reports BSD-2-Clause (read from COPYING; GitHub's detector did not classify it) for the repository)
    MinGW-w64 headers: mingw-w64-headers/include/bcrypt.h (CNG identifiers, blob magics, prototypes).
    Tested on: source read, not run: bcrypt.h read at this commit. · checked 2026-10-18; last activity 2024-09-25

Pitfalls

  • Property names (L"ObjectLength", L"BlockLength", L"HashDigestLength") are queried before keys are created; they are a good anchor for the surrounding code.
  • Unverified: Windows system DLLs may carry additional algorithm ids not in the MinGW header.