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

CryptDeriveKey

関数
ハッシュデータから暗号鍵を導出して生成する。
DLLADVAPI32.dll呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

BOOL CryptDeriveKey(
    UINT_PTR hProv,
    ALG_ID Algid,
    UINT_PTR hBaseData,
    DWORD dwFlags,
    UINT_PTR* phKey
);

パラメーター

名前方向
hProvUINT_PTRin
AlgidALG_IDin
hBaseDataUINT_PTRin
dwFlagsDWORDin
phKeyUINT_PTR*out

戻り値の型: BOOL

各言語での呼び出し定義

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

BOOL CryptDeriveKey(
    UINT_PTR hProv,
    ALG_ID Algid,
    UINT_PTR hBaseData,
    DWORD dwFlags,
    UINT_PTR* phKey
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("ADVAPI32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool CryptDeriveKey(
    UIntPtr hProv,   // UINT_PTR
    uint Algid,   // ALG_ID
    UIntPtr hBaseData,   // UINT_PTR
    uint dwFlags,   // DWORD
    out UIntPtr phKey   // UINT_PTR* out
);
<DllImport("ADVAPI32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function CryptDeriveKey(
    hProv As UIntPtr,   ' UINT_PTR
    Algid As UInteger,   ' ALG_ID
    hBaseData As UIntPtr,   ' UINT_PTR
    dwFlags As UInteger,   ' DWORD
    <Out> ByRef phKey As UIntPtr   ' UINT_PTR* out
) As Boolean
End Function
' hProv : UINT_PTR
' Algid : ALG_ID
' hBaseData : UINT_PTR
' dwFlags : DWORD
' phKey : UINT_PTR* out
Declare PtrSafe Function CryptDeriveKey Lib "advapi32" ( _
    ByVal hProv As LongPtr, _
    ByVal Algid As Long, _
    ByVal hBaseData As LongPtr, _
    ByVal dwFlags As Long, _
    ByRef phKey As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

CryptDeriveKey = ctypes.windll.advapi32.CryptDeriveKey
CryptDeriveKey.restype = wintypes.BOOL
CryptDeriveKey.argtypes = [
    ctypes.c_size_t,  # hProv : UINT_PTR
    wintypes.DWORD,  # Algid : ALG_ID
    ctypes.c_size_t,  # hBaseData : UINT_PTR
    wintypes.DWORD,  # dwFlags : DWORD
    ctypes.POINTER(ctypes.c_size_t),  # phKey : UINT_PTR* out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('ADVAPI32.dll')
CryptDeriveKey = Fiddle::Function.new(
  lib['CryptDeriveKey'],
  [
    Fiddle::TYPE_UINTPTR_T,  # hProv : UINT_PTR
    -Fiddle::TYPE_INT,  # Algid : ALG_ID
    Fiddle::TYPE_UINTPTR_T,  # hBaseData : UINT_PTR
    -Fiddle::TYPE_INT,  # dwFlags : DWORD
    Fiddle::TYPE_VOIDP,  # phKey : UINT_PTR* out
  ],
  Fiddle::TYPE_INT)
#[link(name = "advapi32")]
extern "system" {
    fn CryptDeriveKey(
        hProv: usize,  // UINT_PTR
        Algid: u32,  // ALG_ID
        hBaseData: usize,  // UINT_PTR
        dwFlags: u32,  // DWORD
        phKey: *mut usize  // UINT_PTR* out
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("ADVAPI32.dll", SetLastError = true)]
public static extern bool CryptDeriveKey(UIntPtr hProv, uint Algid, UIntPtr hBaseData, uint dwFlags, out UIntPtr phKey);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ADVAPI32_CryptDeriveKey' -Namespace Win32 -PassThru
# $api::CryptDeriveKey(hProv, Algid, hBaseData, dwFlags, phKey)
#uselib "ADVAPI32.dll"
#func global CryptDeriveKey "CryptDeriveKey" sptr, sptr, sptr, sptr, sptr
; CryptDeriveKey hProv, Algid, hBaseData, dwFlags, varptr(phKey)   ; 戻り値は stat
; hProv : UINT_PTR -> "sptr"
; Algid : ALG_ID -> "sptr"
; hBaseData : UINT_PTR -> "sptr"
; dwFlags : DWORD -> "sptr"
; phKey : UINT_PTR* out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "ADVAPI32.dll"
#cfunc global CryptDeriveKey "CryptDeriveKey" sptr, int, sptr, int, var
; res = CryptDeriveKey(hProv, Algid, hBaseData, dwFlags, phKey)
; hProv : UINT_PTR -> "sptr"
; Algid : ALG_ID -> "int"
; hBaseData : UINT_PTR -> "sptr"
; dwFlags : DWORD -> "int"
; phKey : UINT_PTR* out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL CryptDeriveKey(UINT_PTR hProv, ALG_ID Algid, UINT_PTR hBaseData, DWORD dwFlags, UINT_PTR* phKey)
#uselib "ADVAPI32.dll"
#cfunc global CryptDeriveKey "CryptDeriveKey" intptr, int, intptr, int, var
; res = CryptDeriveKey(hProv, Algid, hBaseData, dwFlags, phKey)
; hProv : UINT_PTR -> "intptr"
; Algid : ALG_ID -> "int"
; hBaseData : UINT_PTR -> "intptr"
; dwFlags : DWORD -> "int"
; phKey : UINT_PTR* out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
	procCryptDeriveKey = advapi32.NewProc("CryptDeriveKey")
)

// hProv (UINT_PTR), Algid (ALG_ID), hBaseData (UINT_PTR), dwFlags (DWORD), phKey (UINT_PTR* out)
r1, _, err := procCryptDeriveKey.Call(
	uintptr(hProv),
	uintptr(Algid),
	uintptr(hBaseData),
	uintptr(dwFlags),
	uintptr(phKey),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function CryptDeriveKey(
  hProv: NativeUInt;   // UINT_PTR
  Algid: DWORD;   // ALG_ID
  hBaseData: NativeUInt;   // UINT_PTR
  dwFlags: DWORD;   // DWORD
  phKey: Pointer   // UINT_PTR* out
): BOOL; stdcall;
  external 'ADVAPI32.dll' name 'CryptDeriveKey';
result := DllCall("ADVAPI32\CryptDeriveKey"
    , "UPtr", hProv   ; UINT_PTR
    , "UInt", Algid   ; ALG_ID
    , "UPtr", hBaseData   ; UINT_PTR
    , "UInt", dwFlags   ; DWORD
    , "Ptr", phKey   ; UINT_PTR* out
    , "Int")   ; return: BOOL
●CryptDeriveKey(hProv, Algid, hBaseData, dwFlags, phKey) = DLL("ADVAPI32.dll", "bool CryptDeriveKey(int, dword, int, dword, void*)")
# 呼び出し: CryptDeriveKey(hProv, Algid, hBaseData, dwFlags, phKey)
# hProv : UINT_PTR -> "int"
# Algid : ALG_ID -> "dword"
# hBaseData : UINT_PTR -> "int"
# dwFlags : DWORD -> "dword"
# phKey : UINT_PTR* out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。