ホーム › Security.Cryptography › BCryptDeriveKeyPBKDF2
BCryptDeriveKeyPBKDF2
関数PBKDF2アルゴリズムでパスワードから鍵を導出する。
シグネチャ
// 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
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| hPrf | BCRYPT_ALG_HANDLE | in | 擬似乱数関数を提供するアルゴリズムプロバイダーのハンドル。これは Message Authentication Code の計算を実行するアルゴリズムプロバイダーである必要があります。既定の Microsoft アルゴリズムプロバイダーを使用する場合、BCRYPT_ALG_HANDLE_HMAC_FLAG フラグを使用して開いた任意のハッシュアルゴリズムを使用できます。 注 このパラメーターに指定できるのは、BCRYPT_IS_KEYED_HASH プロパティを実装するアルゴリズムのみです。
|
| pbPassword | BYTE* | inoptional | PBKDF2 鍵導出アルゴリズムのパスワードパラメーターを格納するバッファーへのポインター。 注 鍵の導出に使用される秘密情報は、このバッファーで渡す必要があります。
|
| cbPassword | DWORD | in | pbPassword パラメーターが指すバッファー内のデータの長さ(バイト単位)。 |
| pbSalt | BYTE* | inoptional | PBKDF2 鍵導出アルゴリズムのソルト引数を格納するバッファーへのポインター。 注 秘密ではなく、鍵の導出に使用される情報は、このバッファーで渡す必要があります。
|
| cbSalt | DWORD | in | pbSalt パラメーターが指すソルト引数の長さ(バイト単位)。 |
| cIterations | ULONGLONG | in | PBKDF2 鍵導出アルゴリズムの反復回数。 |
| pbDerivedKey | BYTE* | out | 導出された鍵を受け取るバッファーへのポインター。 |
| cbDerivedKey | DWORD | in | pbDerivedKey パラメーターが指すバッファーに返される、導出された鍵の長さ(バイト単位)。 |
| dwFlags | DWORD | in | このパラメーターは予約されており、ゼロに設定する必要があります。 |
戻り値の型: NTSTATUS
公式ドキュメント
RFC 2898 で定義されている PBKDF2 鍵導出アルゴリズムを使用して、ハッシュ値から鍵を導出します。
戻り値
関数の成功または失敗を示すステータスコードを返します。
返される可能性のあるコードには、以下が含まれますが、これらに限定されません。
| 戻り値 | 説明 |
|---|---|
| 関数は成功しました。 | |
| hPrf パラメーターのハンドルが有効ではありません。 | |
| 1 つ以上のパラメーターが有効ではありません。 | |
| メモリの割り当てに失敗しました。 |
出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// 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 は未対応)。#uselib "bcrypt.dll" #cfunc global BCryptDeriveKeyPBKDF2 "BCryptDeriveKeyPBKDF2" sptr, sptr, int, sptr, int, int64, sptr, int, int ; res = BCryptDeriveKeyPBKDF2(hPrf, varptr(pbPassword), cbPassword, varptr(pbSalt), cbSalt, cIterations, varptr(pbDerivedKey), cbDerivedKey, dwFlags) ; hPrf : BCRYPT_ALG_HANDLE -> "sptr" ; pbPassword : BYTE* optional -> "sptr" ; cbPassword : DWORD -> "int" ; pbSalt : BYTE* optional -> "sptr" ; cbSalt : DWORD -> "int" ; cIterations : ULONGLONG -> "int64" ; pbDerivedKey : BYTE* out -> "sptr" ; cbDerivedKey : DWORD -> "int" ; dwFlags : DWORD -> "int" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは 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 方式にも切替可。; 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, intptr, int, intptr, int, int64, intptr, int, int ; res = BCryptDeriveKeyPBKDF2(hPrf, varptr(pbPassword), cbPassword, varptr(pbSalt), cbSalt, cIterations, varptr(pbDerivedKey), cbDerivedKey, dwFlags) ; hPrf : BCRYPT_ALG_HANDLE -> "intptr" ; pbPassword : BYTE* optional -> "intptr" ; cbPassword : DWORD -> "int" ; pbSalt : BYTE* optional -> "intptr" ; cbSalt : DWORD -> "int" ; cIterations : ULONGLONG -> "int64" ; pbDerivedKey : BYTE* out -> "intptr" ; cbDerivedKey : DWORD -> "int" ; dwFlags : DWORD -> "int" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは 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 // NTSTATUSfunction 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)。const std = @import("std");
extern "bcrypt" fn BCryptDeriveKeyPBKDF2(
hPrf: ?*anyopaque, // BCRYPT_ALG_HANDLE
pbPassword: [*c]u8, // BYTE* optional
cbPassword: u32, // DWORD
pbSalt: [*c]u8, // BYTE* optional
cbSalt: u32, // DWORD
cIterations: u64, // ULONGLONG
pbDerivedKey: [*c]u8, // BYTE* out
cbDerivedKey: u32, // DWORD
dwFlags: u32 // DWORD
) callconv(std.os.windows.WINAPI) i32;proc BCryptDeriveKeyPBKDF2(
hPrf: pointer, # BCRYPT_ALG_HANDLE
pbPassword: ptr uint8, # BYTE* optional
cbPassword: uint32, # DWORD
pbSalt: ptr uint8, # BYTE* optional
cbSalt: uint32, # DWORD
cIterations: uint64, # ULONGLONG
pbDerivedKey: ptr uint8, # BYTE* out
cbDerivedKey: uint32, # DWORD
dwFlags: uint32 # DWORD
): int32 {.importc: "BCryptDeriveKeyPBKDF2", stdcall, dynlib: "bcrypt.dll".}pragma(lib, "bcrypt");
extern(Windows)
int BCryptDeriveKeyPBKDF2(
void* hPrf, // BCRYPT_ALG_HANDLE
ubyte* pbPassword, // BYTE* optional
uint cbPassword, // DWORD
ubyte* pbSalt, // BYTE* optional
uint cbSalt, // DWORD
ulong cIterations, // ULONGLONG
ubyte* pbDerivedKey, // BYTE* out
uint cbDerivedKey, // DWORD
uint dwFlags // DWORD
);ccall((:BCryptDeriveKeyPBKDF2, "bcrypt.dll"), stdcall, Int32,
(Ptr{Cvoid}, Ptr{UInt8}, UInt32, Ptr{UInt8}, UInt32, UInt64, Ptr{UInt8}, UInt32, UInt32),
hPrf, pbPassword, cbPassword, pbSalt, cbSalt, cIterations, pbDerivedKey, cbDerivedKey, dwFlags)
# hPrf : BCRYPT_ALG_HANDLE -> Ptr{Cvoid}
# pbPassword : BYTE* optional -> Ptr{UInt8}
# cbPassword : DWORD -> UInt32
# pbSalt : BYTE* optional -> Ptr{UInt8}
# cbSalt : DWORD -> UInt32
# cIterations : ULONGLONG -> UInt64
# pbDerivedKey : BYTE* out -> Ptr{UInt8}
# cbDerivedKey : DWORD -> UInt32
# dwFlags : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t BCryptDeriveKeyPBKDF2(
void* hPrf,
uint8_t* pbPassword,
uint32_t cbPassword,
uint8_t* pbSalt,
uint32_t cbSalt,
uint64_t cIterations,
uint8_t* pbDerivedKey,
uint32_t cbDerivedKey,
uint32_t dwFlags);
]]
local bcrypt = ffi.load("bcrypt")
-- bcrypt.BCryptDeriveKeyPBKDF2(hPrf, pbPassword, cbPassword, pbSalt, cbSalt, cIterations, pbDerivedKey, cbDerivedKey, dwFlags)
-- hPrf : BCRYPT_ALG_HANDLE
-- pbPassword : BYTE* optional
-- cbPassword : DWORD
-- pbSalt : BYTE* optional
-- cbSalt : DWORD
-- cIterations : ULONGLONG
-- pbDerivedKey : BYTE* out
-- cbDerivedKey : DWORD
-- dwFlags : DWORD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('bcrypt.dll');
const BCryptDeriveKeyPBKDF2 = lib.func('__stdcall', 'BCryptDeriveKeyPBKDF2', 'int32_t', ['void *', 'uint8_t *', 'uint32_t', 'uint8_t *', 'uint32_t', 'uint64_t', 'uint8_t *', 'uint32_t', 'uint32_t']);
// BCryptDeriveKeyPBKDF2(hPrf, pbPassword, cbPassword, pbSalt, cbSalt, cIterations, pbDerivedKey, cbDerivedKey, dwFlags)
// hPrf : BCRYPT_ALG_HANDLE -> 'void *'
// pbPassword : BYTE* optional -> 'uint8_t *'
// cbPassword : DWORD -> 'uint32_t'
// pbSalt : BYTE* optional -> 'uint8_t *'
// cbSalt : DWORD -> 'uint32_t'
// cIterations : ULONGLONG -> 'uint64_t'
// pbDerivedKey : BYTE* out -> 'uint8_t *'
// cbDerivedKey : DWORD -> 'uint32_t'
// dwFlags : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("bcrypt.dll", {
BCryptDeriveKeyPBKDF2: { parameters: ["pointer", "pointer", "u32", "pointer", "u32", "u64", "pointer", "u32", "u32"], result: "i32" },
});
// lib.symbols.BCryptDeriveKeyPBKDF2(hPrf, pbPassword, cbPassword, pbSalt, cbSalt, cIterations, pbDerivedKey, cbDerivedKey, dwFlags)
// hPrf : BCRYPT_ALG_HANDLE -> "pointer"
// pbPassword : BYTE* optional -> "pointer"
// cbPassword : DWORD -> "u32"
// pbSalt : BYTE* optional -> "pointer"
// cbSalt : DWORD -> "u32"
// cIterations : ULONGLONG -> "u64"
// pbDerivedKey : BYTE* out -> "pointer"
// cbDerivedKey : DWORD -> "u32"
// dwFlags : DWORD -> "u32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t BCryptDeriveKeyPBKDF2(
void* hPrf,
uint8_t* pbPassword,
uint32_t cbPassword,
uint8_t* pbSalt,
uint32_t cbSalt,
uint64_t cIterations,
uint8_t* pbDerivedKey,
uint32_t cbDerivedKey,
uint32_t dwFlags);
C, "bcrypt.dll");
// $ffi->BCryptDeriveKeyPBKDF2(hPrf, pbPassword, cbPassword, pbSalt, cbSalt, cIterations, pbDerivedKey, cbDerivedKey, dwFlags);
// hPrf : BCRYPT_ALG_HANDLE
// pbPassword : BYTE* optional
// cbPassword : DWORD
// pbSalt : BYTE* optional
// cbSalt : DWORD
// cIterations : ULONGLONG
// pbDerivedKey : BYTE* out
// cbDerivedKey : DWORD
// dwFlags : DWORD
// 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
// WINAPI(stdcall): x64 では呼出規約が統一されるため問題なし。x86 では __stdcall 対応のラッパが必要な場合あり。import com.sun.jna.*;
import com.sun.jna.ptr.*;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;
public interface Bcrypt extends StdCallLibrary {
Bcrypt INSTANCE = Native.load("bcrypt", Bcrypt.class);
int BCryptDeriveKeyPBKDF2(
Pointer hPrf, // BCRYPT_ALG_HANDLE
byte[] pbPassword, // BYTE* optional
int cbPassword, // DWORD
byte[] pbSalt, // BYTE* optional
int cbSalt, // DWORD
long cIterations, // ULONGLONG
byte[] pbDerivedKey, // BYTE* out
int cbDerivedKey, // DWORD
int dwFlags // DWORD
);
}@[Link("bcrypt")]
lib Libbcrypt
fun BCryptDeriveKeyPBKDF2 = BCryptDeriveKeyPBKDF2(
hPrf : Void*, # BCRYPT_ALG_HANDLE
pbPassword : UInt8*, # BYTE* optional
cbPassword : UInt32, # DWORD
pbSalt : UInt8*, # BYTE* optional
cbSalt : UInt32, # DWORD
cIterations : UInt64, # ULONGLONG
pbDerivedKey : UInt8*, # BYTE* out
cbDerivedKey : UInt32, # DWORD
dwFlags : UInt32 # DWORD
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef BCryptDeriveKeyPBKDF2Native = Int32 Function(Pointer<Void>, Pointer<Uint8>, Uint32, Pointer<Uint8>, Uint32, Uint64, Pointer<Uint8>, Uint32, Uint32);
typedef BCryptDeriveKeyPBKDF2Dart = int Function(Pointer<Void>, Pointer<Uint8>, int, Pointer<Uint8>, int, int, Pointer<Uint8>, int, int);
final BCryptDeriveKeyPBKDF2 = DynamicLibrary.open('bcrypt.dll')
.lookupFunction<BCryptDeriveKeyPBKDF2Native, BCryptDeriveKeyPBKDF2Dart>('BCryptDeriveKeyPBKDF2');
// hPrf : BCRYPT_ALG_HANDLE -> Pointer<Void>
// pbPassword : BYTE* optional -> Pointer<Uint8>
// cbPassword : DWORD -> Uint32
// pbSalt : BYTE* optional -> Pointer<Uint8>
// cbSalt : DWORD -> Uint32
// cIterations : ULONGLONG -> Uint64
// pbDerivedKey : BYTE* out -> Pointer<Uint8>
// cbDerivedKey : DWORD -> Uint32
// dwFlags : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
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';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "BCryptDeriveKeyPBKDF2"
c_BCryptDeriveKeyPBKDF2 :: Ptr () -> Ptr Word8 -> Word32 -> Ptr Word8 -> Word32 -> Word64 -> Ptr Word8 -> Word32 -> Word32 -> IO Int32
-- hPrf : BCRYPT_ALG_HANDLE -> Ptr ()
-- pbPassword : BYTE* optional -> Ptr Word8
-- cbPassword : DWORD -> Word32
-- pbSalt : BYTE* optional -> Ptr Word8
-- cbSalt : DWORD -> Word32
-- cIterations : ULONGLONG -> Word64
-- pbDerivedKey : BYTE* out -> Ptr Word8
-- cbDerivedKey : DWORD -> Word32
-- dwFlags : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let bcryptderivekeypbkdf2 =
foreign "BCryptDeriveKeyPBKDF2"
((ptr void) @-> (ptr uint8_t) @-> uint32_t @-> (ptr uint8_t) @-> uint32_t @-> uint64_t @-> (ptr uint8_t) @-> uint32_t @-> uint32_t @-> returning int32_t)
(* hPrf : BCRYPT_ALG_HANDLE -> (ptr void) *)
(* pbPassword : BYTE* optional -> (ptr uint8_t) *)
(* cbPassword : DWORD -> uint32_t *)
(* pbSalt : BYTE* optional -> (ptr uint8_t) *)
(* cbSalt : DWORD -> uint32_t *)
(* cIterations : ULONGLONG -> uint64_t *)
(* pbDerivedKey : BYTE* out -> (ptr uint8_t) *)
(* cbDerivedKey : DWORD -> uint32_t *)
(* dwFlags : DWORD -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library bcrypt (t "bcrypt.dll"))
(cffi:use-foreign-library bcrypt)
(cffi:defcfun ("BCryptDeriveKeyPBKDF2" bcrypt-derive-key-pbkdf2 :convention :stdcall) :int32
(h-prf :pointer) ; BCRYPT_ALG_HANDLE
(pb-password :pointer) ; BYTE* optional
(cb-password :uint32) ; DWORD
(pb-salt :pointer) ; BYTE* optional
(cb-salt :uint32) ; DWORD
(c-iterations :uint64) ; ULONGLONG
(pb-derived-key :pointer) ; BYTE* out
(cb-derived-key :uint32) ; DWORD
(dw-flags :uint32)) ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $BCryptDeriveKeyPBKDF2 = Win32::API::More->new('bcrypt',
'int BCryptDeriveKeyPBKDF2(HANDLE hPrf, LPVOID pbPassword, DWORD cbPassword, LPVOID pbSalt, DWORD cbSalt, UINT64 cIterations, LPVOID pbDerivedKey, DWORD cbDerivedKey, DWORD dwFlags)');
# my $ret = $BCryptDeriveKeyPBKDF2->Call($hPrf, $pbPassword, $cbPassword, $pbSalt, $cbSalt, $cIterations, $pbDerivedKey, $cbDerivedKey, $dwFlags);
# hPrf : BCRYPT_ALG_HANDLE -> HANDLE
# pbPassword : BYTE* optional -> LPVOID
# cbPassword : DWORD -> DWORD
# pbSalt : BYTE* optional -> LPVOID
# cbSalt : DWORD -> DWORD
# cIterations : ULONGLONG -> UINT64
# pbDerivedKey : BYTE* out -> LPVOID
# cbDerivedKey : DWORD -> DWORD
# dwFlags : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。