Win32 API 日本語リファレンス
ホームSecurity.Cryptography › BCryptDeriveKeyPBKDF2

BCryptDeriveKeyPBKDF2

関数
PBKDF2アルゴリズムでパスワードから鍵を導出する。
DLLbcrypt.dll呼出規約winapi対応OSWindows 7 以降

シグネチャ

// bcrypt.dll
#include <windows.h>

NTSTATUS BCryptDeriveKeyPBKDF2(
    BCRYPT_ALG_HANDLE hPrf,
    BYTE* pbPassword,   // optional
    DWORD cbPassword,
    BYTE* pbSalt,   // optional
    DWORD cbSalt,
    ULONGLONG cIterations,
    BYTE* pbDerivedKey,
    DWORD cbDerivedKey,
    DWORD dwFlags
);

パラメーター

名前方向
hPrfBCRYPT_ALG_HANDLEin
pbPasswordBYTE*inoptional
cbPasswordDWORDin
pbSaltBYTE*inoptional
cbSaltDWORDin
cIterationsULONGLONGin
pbDerivedKeyBYTE*out
cbDerivedKeyDWORDin
dwFlagsDWORDin

戻り値の型: NTSTATUS

各言語での呼び出し定義

// bcrypt.dll
#include <windows.h>

NTSTATUS BCryptDeriveKeyPBKDF2(
    BCRYPT_ALG_HANDLE hPrf,
    BYTE* pbPassword,   // optional
    DWORD cbPassword,
    BYTE* pbSalt,   // optional
    DWORD cbSalt,
    ULONGLONG cIterations,
    BYTE* pbDerivedKey,
    DWORD cbDerivedKey,
    DWORD dwFlags
);
[DllImport("bcrypt.dll", ExactSpelling = true)]
static extern int BCryptDeriveKeyPBKDF2(
    IntPtr hPrf,   // BCRYPT_ALG_HANDLE
    IntPtr pbPassword,   // BYTE* optional
    uint cbPassword,   // DWORD
    IntPtr pbSalt,   // BYTE* optional
    uint cbSalt,   // DWORD
    ulong cIterations,   // ULONGLONG
    IntPtr pbDerivedKey,   // BYTE* out
    uint cbDerivedKey,   // DWORD
    uint dwFlags   // DWORD
);
<DllImport("bcrypt.dll", ExactSpelling:=True)>
Public Shared Function BCryptDeriveKeyPBKDF2(
    hPrf As IntPtr,   ' BCRYPT_ALG_HANDLE
    pbPassword As IntPtr,   ' BYTE* optional
    cbPassword As UInteger,   ' DWORD
    pbSalt As IntPtr,   ' BYTE* optional
    cbSalt As UInteger,   ' DWORD
    cIterations As ULong,   ' ULONGLONG
    pbDerivedKey As IntPtr,   ' BYTE* out
    cbDerivedKey As UInteger,   ' DWORD
    dwFlags As UInteger   ' DWORD
) As Integer
End Function
' hPrf : BCRYPT_ALG_HANDLE
' pbPassword : BYTE* optional
' cbPassword : DWORD
' pbSalt : BYTE* optional
' cbSalt : DWORD
' cIterations : ULONGLONG
' pbDerivedKey : BYTE* out
' cbDerivedKey : DWORD
' dwFlags : DWORD
Declare PtrSafe Function BCryptDeriveKeyPBKDF2 Lib "bcrypt" ( _
    ByVal hPrf As LongPtr, _
    ByVal pbPassword As LongPtr, _
    ByVal cbPassword As Long, _
    ByVal pbSalt As LongPtr, _
    ByVal cbSalt As Long, _
    ByVal cIterations As LongLong, _
    ByVal pbDerivedKey As LongPtr, _
    ByVal cbDerivedKey As Long, _
    ByVal dwFlags As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

BCryptDeriveKeyPBKDF2 = ctypes.windll.bcrypt.BCryptDeriveKeyPBKDF2
BCryptDeriveKeyPBKDF2.restype = ctypes.c_int
BCryptDeriveKeyPBKDF2.argtypes = [
    wintypes.HANDLE,  # hPrf : BCRYPT_ALG_HANDLE
    ctypes.POINTER(ctypes.c_ubyte),  # pbPassword : BYTE* optional
    wintypes.DWORD,  # cbPassword : DWORD
    ctypes.POINTER(ctypes.c_ubyte),  # pbSalt : BYTE* optional
    wintypes.DWORD,  # cbSalt : DWORD
    ctypes.c_ulonglong,  # cIterations : ULONGLONG
    ctypes.POINTER(ctypes.c_ubyte),  # pbDerivedKey : BYTE* out
    wintypes.DWORD,  # cbDerivedKey : DWORD
    wintypes.DWORD,  # dwFlags : DWORD
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('bcrypt.dll')
BCryptDeriveKeyPBKDF2 = Fiddle::Function.new(
  lib['BCryptDeriveKeyPBKDF2'],
  [
    Fiddle::TYPE_VOIDP,  # hPrf : BCRYPT_ALG_HANDLE
    Fiddle::TYPE_VOIDP,  # pbPassword : BYTE* optional
    -Fiddle::TYPE_INT,  # cbPassword : DWORD
    Fiddle::TYPE_VOIDP,  # pbSalt : BYTE* optional
    -Fiddle::TYPE_INT,  # cbSalt : DWORD
    -Fiddle::TYPE_LONG_LONG,  # cIterations : ULONGLONG
    Fiddle::TYPE_VOIDP,  # pbDerivedKey : BYTE* out
    -Fiddle::TYPE_INT,  # cbDerivedKey : DWORD
    -Fiddle::TYPE_INT,  # dwFlags : DWORD
  ],
  Fiddle::TYPE_INT)
#[link(name = "bcrypt")]
extern "system" {
    fn BCryptDeriveKeyPBKDF2(
        hPrf: *mut core::ffi::c_void,  // BCRYPT_ALG_HANDLE
        pbPassword: *mut u8,  // BYTE* optional
        cbPassword: u32,  // DWORD
        pbSalt: *mut u8,  // BYTE* optional
        cbSalt: u32,  // DWORD
        cIterations: u64,  // ULONGLONG
        pbDerivedKey: *mut u8,  // BYTE* out
        cbDerivedKey: u32,  // DWORD
        dwFlags: u32  // DWORD
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("bcrypt.dll")]
public static extern int BCryptDeriveKeyPBKDF2(IntPtr hPrf, IntPtr pbPassword, uint cbPassword, IntPtr pbSalt, uint cbSalt, ulong cIterations, IntPtr pbDerivedKey, uint cbDerivedKey, uint dwFlags);
"@
$api = Add-Type -MemberDefinition $sig -Name 'bcrypt_BCryptDeriveKeyPBKDF2' -Namespace Win32 -PassThru
# $api::BCryptDeriveKeyPBKDF2(hPrf, pbPassword, cbPassword, pbSalt, cbSalt, cIterations, pbDerivedKey, cbDerivedKey, dwFlags)
#uselib "bcrypt.dll"
#func global BCryptDeriveKeyPBKDF2 "BCryptDeriveKeyPBKDF2" sptr, sptr, sptr, sptr, sptr, sptr, sptr, sptr, sptr
; BCryptDeriveKeyPBKDF2 hPrf, varptr(pbPassword), cbPassword, varptr(pbSalt), cbSalt, cIterations, varptr(pbDerivedKey), cbDerivedKey, dwFlags   ; 戻り値は stat
; hPrf : BCRYPT_ALG_HANDLE -> "sptr"
; pbPassword : BYTE* optional -> "sptr"
; cbPassword : DWORD -> "sptr"
; pbSalt : BYTE* optional -> "sptr"
; cbSalt : DWORD -> "sptr"
; cIterations : ULONGLONG -> "sptr"
; pbDerivedKey : BYTE* out -> "sptr"
; cbDerivedKey : DWORD -> "sptr"
; dwFlags : DWORD -> "sptr"
; ※HSP3.7は int64 引数(64bit値渡し)に非対応です。
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "bcrypt.dll"
#cfunc global BCryptDeriveKeyPBKDF2 "BCryptDeriveKeyPBKDF2" sptr, var, int, var, int, int64, var, int, int
; res = BCryptDeriveKeyPBKDF2(hPrf, pbPassword, cbPassword, pbSalt, cbSalt, cIterations, pbDerivedKey, cbDerivedKey, dwFlags)
; hPrf : BCRYPT_ALG_HANDLE -> "sptr"
; pbPassword : BYTE* optional -> "var"
; cbPassword : DWORD -> "int"
; pbSalt : BYTE* optional -> "var"
; cbSalt : DWORD -> "int"
; cIterations : ULONGLONG -> "int64"
; pbDerivedKey : BYTE* out -> "var"
; cbDerivedKey : DWORD -> "int"
; dwFlags : DWORD -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
; ※int64 引数の DLL 値渡しは x64 ランタイム(hsp3_64)のみ対応(x86 は未対応)。
出力引数:
; NTSTATUS BCryptDeriveKeyPBKDF2(BCRYPT_ALG_HANDLE hPrf, BYTE* pbPassword, DWORD cbPassword, BYTE* pbSalt, DWORD cbSalt, ULONGLONG cIterations, BYTE* pbDerivedKey, DWORD cbDerivedKey, DWORD dwFlags)
#uselib "bcrypt.dll"
#cfunc global BCryptDeriveKeyPBKDF2 "BCryptDeriveKeyPBKDF2" intptr, var, int, var, int, int64, var, int, int
; res = BCryptDeriveKeyPBKDF2(hPrf, pbPassword, cbPassword, pbSalt, cbSalt, cIterations, pbDerivedKey, cbDerivedKey, dwFlags)
; hPrf : BCRYPT_ALG_HANDLE -> "intptr"
; pbPassword : BYTE* optional -> "var"
; cbPassword : DWORD -> "int"
; pbSalt : BYTE* optional -> "var"
; cbSalt : DWORD -> "int"
; cIterations : ULONGLONG -> "int64"
; pbDerivedKey : BYTE* out -> "var"
; cbDerivedKey : DWORD -> "int"
; dwFlags : DWORD -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	bcrypt = windows.NewLazySystemDLL("bcrypt.dll")
	procBCryptDeriveKeyPBKDF2 = bcrypt.NewProc("BCryptDeriveKeyPBKDF2")
)

// hPrf (BCRYPT_ALG_HANDLE), pbPassword (BYTE* optional), cbPassword (DWORD), pbSalt (BYTE* optional), cbSalt (DWORD), cIterations (ULONGLONG), pbDerivedKey (BYTE* out), cbDerivedKey (DWORD), dwFlags (DWORD)
r1, _, err := procBCryptDeriveKeyPBKDF2.Call(
	uintptr(hPrf),
	uintptr(pbPassword),
	uintptr(cbPassword),
	uintptr(pbSalt),
	uintptr(cbSalt),
	uintptr(cIterations),
	uintptr(pbDerivedKey),
	uintptr(cbDerivedKey),
	uintptr(dwFlags),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // NTSTATUS
function BCryptDeriveKeyPBKDF2(
  hPrf: THandle;   // BCRYPT_ALG_HANDLE
  pbPassword: Pointer;   // BYTE* optional
  cbPassword: DWORD;   // DWORD
  pbSalt: Pointer;   // BYTE* optional
  cbSalt: DWORD;   // DWORD
  cIterations: UInt64;   // ULONGLONG
  pbDerivedKey: Pointer;   // BYTE* out
  cbDerivedKey: DWORD;   // DWORD
  dwFlags: DWORD   // DWORD
): Integer; stdcall;
  external 'bcrypt.dll' name 'BCryptDeriveKeyPBKDF2';
result := DllCall("bcrypt\BCryptDeriveKeyPBKDF2"
    , "Ptr", hPrf   ; BCRYPT_ALG_HANDLE
    , "Ptr", pbPassword   ; BYTE* optional
    , "UInt", cbPassword   ; DWORD
    , "Ptr", pbSalt   ; BYTE* optional
    , "UInt", cbSalt   ; DWORD
    , "Int64", cIterations   ; ULONGLONG
    , "Ptr", pbDerivedKey   ; BYTE* out
    , "UInt", cbDerivedKey   ; DWORD
    , "UInt", dwFlags   ; DWORD
    , "Int")   ; return: NTSTATUS
●BCryptDeriveKeyPBKDF2(hPrf, pbPassword, cbPassword, pbSalt, cbSalt, cIterations, pbDerivedKey, cbDerivedKey, dwFlags) = DLL("bcrypt.dll", "int BCryptDeriveKeyPBKDF2(void*, void*, dword, void*, dword, qword, void*, dword, dword)")
# 呼び出し: BCryptDeriveKeyPBKDF2(hPrf, pbPassword, cbPassword, pbSalt, cbSalt, cIterations, pbDerivedKey, cbDerivedKey, dwFlags)
# hPrf : BCRYPT_ALG_HANDLE -> "void*"
# pbPassword : BYTE* optional -> "void*"
# cbPassword : DWORD -> "dword"
# pbSalt : BYTE* optional -> "void*"
# cbSalt : DWORD -> "dword"
# cIterations : ULONGLONG -> "qword"
# pbDerivedKey : BYTE* out -> "void*"
# cbDerivedKey : DWORD -> "dword"
# dwFlags : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。