ホーム › Security.Cryptography › NCryptEncapsulate
NCryptEncapsulate
関数キーカプセル化により共有秘密と暗号文を生成する。
シグネチャ
// ncrypt.dll
#include <windows.h>
HRESULT NCryptEncapsulate(
NCRYPT_KEY_HANDLE hKey,
BYTE* pbSecretKey, // optional
DWORD cbSecretKey,
DWORD* pcbSecretKey,
BYTE* pbCipherText, // optional
DWORD cbCipherText,
DWORD* pcbCipherText,
DWORD dwFlags
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| hKey | NCRYPT_KEY_HANDLE | in | カプセル化に使う公開鍵ハンドル(KEM)。 |
| pbSecretKey | BYTE* | outoptional | 生成された共有秘密鍵を受け取るバッファへのポインタ。NULLで必要サイズのみ取得できる。 |
| cbSecretKey | DWORD | in | 共有秘密鍵バッファのバイトサイズ。 |
| pcbSecretKey | DWORD* | out | 実際の、または必要な共有秘密鍵バイト数を受け取るポインタ。 |
| pbCipherText | BYTE* | outoptional | 生成された暗号文(カプセル)を受け取るバッファへのポインタ。NULLでサイズ取得。 |
| cbCipherText | DWORD | in | 暗号文バッファのバイトサイズ。 |
| pcbCipherText | DWORD* | out | 実際の、または必要な暗号文バイト数を受け取るポインタ。 |
| dwFlags | DWORD | in | 動作フラグ。通常は0を指定する。 |
戻り値の型: HRESULT
各言語での呼び出し定義
// ncrypt.dll
#include <windows.h>
HRESULT NCryptEncapsulate(
NCRYPT_KEY_HANDLE hKey,
BYTE* pbSecretKey, // optional
DWORD cbSecretKey,
DWORD* pcbSecretKey,
BYTE* pbCipherText, // optional
DWORD cbCipherText,
DWORD* pcbCipherText,
DWORD dwFlags
);[DllImport("ncrypt.dll", ExactSpelling = true)]
static extern int NCryptEncapsulate(
UIntPtr hKey, // NCRYPT_KEY_HANDLE
IntPtr pbSecretKey, // BYTE* optional, out
uint cbSecretKey, // DWORD
out uint pcbSecretKey, // DWORD* out
IntPtr pbCipherText, // BYTE* optional, out
uint cbCipherText, // DWORD
out uint pcbCipherText, // DWORD* out
uint dwFlags // DWORD
);<DllImport("ncrypt.dll", ExactSpelling:=True)>
Public Shared Function NCryptEncapsulate(
hKey As UIntPtr, ' NCRYPT_KEY_HANDLE
pbSecretKey As IntPtr, ' BYTE* optional, out
cbSecretKey As UInteger, ' DWORD
<Out> ByRef pcbSecretKey As UInteger, ' DWORD* out
pbCipherText As IntPtr, ' BYTE* optional, out
cbCipherText As UInteger, ' DWORD
<Out> ByRef pcbCipherText As UInteger, ' DWORD* out
dwFlags As UInteger ' DWORD
) As Integer
End Function' hKey : NCRYPT_KEY_HANDLE
' pbSecretKey : BYTE* optional, out
' cbSecretKey : DWORD
' pcbSecretKey : DWORD* out
' pbCipherText : BYTE* optional, out
' cbCipherText : DWORD
' pcbCipherText : DWORD* out
' dwFlags : DWORD
Declare PtrSafe Function NCryptEncapsulate Lib "ncrypt" ( _
ByVal hKey As LongPtr, _
ByVal pbSecretKey As LongPtr, _
ByVal cbSecretKey As Long, _
ByRef pcbSecretKey As Long, _
ByVal pbCipherText As LongPtr, _
ByVal cbCipherText As Long, _
ByRef pcbCipherText 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
NCryptEncapsulate = ctypes.windll.ncrypt.NCryptEncapsulate
NCryptEncapsulate.restype = ctypes.c_int
NCryptEncapsulate.argtypes = [
ctypes.c_size_t, # hKey : NCRYPT_KEY_HANDLE
ctypes.POINTER(ctypes.c_ubyte), # pbSecretKey : BYTE* optional, out
wintypes.DWORD, # cbSecretKey : DWORD
ctypes.POINTER(wintypes.DWORD), # pcbSecretKey : DWORD* out
ctypes.POINTER(ctypes.c_ubyte), # pbCipherText : BYTE* optional, out
wintypes.DWORD, # cbCipherText : DWORD
ctypes.POINTER(wintypes.DWORD), # pcbCipherText : DWORD* out
wintypes.DWORD, # dwFlags : DWORD
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('ncrypt.dll')
NCryptEncapsulate = Fiddle::Function.new(
lib['NCryptEncapsulate'],
[
Fiddle::TYPE_UINTPTR_T, # hKey : NCRYPT_KEY_HANDLE
Fiddle::TYPE_VOIDP, # pbSecretKey : BYTE* optional, out
-Fiddle::TYPE_INT, # cbSecretKey : DWORD
Fiddle::TYPE_VOIDP, # pcbSecretKey : DWORD* out
Fiddle::TYPE_VOIDP, # pbCipherText : BYTE* optional, out
-Fiddle::TYPE_INT, # cbCipherText : DWORD
Fiddle::TYPE_VOIDP, # pcbCipherText : DWORD* out
-Fiddle::TYPE_INT, # dwFlags : DWORD
],
Fiddle::TYPE_INT)#[link(name = "ncrypt")]
extern "system" {
fn NCryptEncapsulate(
hKey: usize, // NCRYPT_KEY_HANDLE
pbSecretKey: *mut u8, // BYTE* optional, out
cbSecretKey: u32, // DWORD
pcbSecretKey: *mut u32, // DWORD* out
pbCipherText: *mut u8, // BYTE* optional, out
cbCipherText: u32, // DWORD
pcbCipherText: *mut u32, // DWORD* out
dwFlags: u32 // DWORD
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("ncrypt.dll")]
public static extern int NCryptEncapsulate(UIntPtr hKey, IntPtr pbSecretKey, uint cbSecretKey, out uint pcbSecretKey, IntPtr pbCipherText, uint cbCipherText, out uint pcbCipherText, uint dwFlags);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ncrypt_NCryptEncapsulate' -Namespace Win32 -PassThru
# $api::NCryptEncapsulate(hKey, pbSecretKey, cbSecretKey, pcbSecretKey, pbCipherText, cbCipherText, pcbCipherText, dwFlags)#uselib "ncrypt.dll"
#func global NCryptEncapsulate "NCryptEncapsulate" sptr, sptr, sptr, sptr, sptr, sptr, sptr, sptr
; NCryptEncapsulate hKey, varptr(pbSecretKey), cbSecretKey, varptr(pcbSecretKey), varptr(pbCipherText), cbCipherText, varptr(pcbCipherText), dwFlags ; 戻り値は stat
; hKey : NCRYPT_KEY_HANDLE -> "sptr"
; pbSecretKey : BYTE* optional, out -> "sptr"
; cbSecretKey : DWORD -> "sptr"
; pcbSecretKey : DWORD* out -> "sptr"
; pbCipherText : BYTE* optional, out -> "sptr"
; cbCipherText : DWORD -> "sptr"
; pcbCipherText : DWORD* out -> "sptr"
; dwFlags : DWORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "ncrypt.dll" #cfunc global NCryptEncapsulate "NCryptEncapsulate" sptr, var, int, var, var, int, var, int ; res = NCryptEncapsulate(hKey, pbSecretKey, cbSecretKey, pcbSecretKey, pbCipherText, cbCipherText, pcbCipherText, dwFlags) ; hKey : NCRYPT_KEY_HANDLE -> "sptr" ; pbSecretKey : BYTE* optional, out -> "var" ; cbSecretKey : DWORD -> "int" ; pcbSecretKey : DWORD* out -> "var" ; pbCipherText : BYTE* optional, out -> "var" ; cbCipherText : DWORD -> "int" ; pcbCipherText : DWORD* out -> "var" ; dwFlags : DWORD -> "int" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "ncrypt.dll" #cfunc global NCryptEncapsulate "NCryptEncapsulate" sptr, sptr, int, sptr, sptr, int, sptr, int ; res = NCryptEncapsulate(hKey, varptr(pbSecretKey), cbSecretKey, varptr(pcbSecretKey), varptr(pbCipherText), cbCipherText, varptr(pcbCipherText), dwFlags) ; hKey : NCRYPT_KEY_HANDLE -> "sptr" ; pbSecretKey : BYTE* optional, out -> "sptr" ; cbSecretKey : DWORD -> "int" ; pcbSecretKey : DWORD* out -> "sptr" ; pbCipherText : BYTE* optional, out -> "sptr" ; cbCipherText : DWORD -> "int" ; pcbCipherText : DWORD* out -> "sptr" ; dwFlags : DWORD -> "int" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; HRESULT NCryptEncapsulate(NCRYPT_KEY_HANDLE hKey, BYTE* pbSecretKey, DWORD cbSecretKey, DWORD* pcbSecretKey, BYTE* pbCipherText, DWORD cbCipherText, DWORD* pcbCipherText, DWORD dwFlags) #uselib "ncrypt.dll" #cfunc global NCryptEncapsulate "NCryptEncapsulate" intptr, var, int, var, var, int, var, int ; res = NCryptEncapsulate(hKey, pbSecretKey, cbSecretKey, pcbSecretKey, pbCipherText, cbCipherText, pcbCipherText, dwFlags) ; hKey : NCRYPT_KEY_HANDLE -> "intptr" ; pbSecretKey : BYTE* optional, out -> "var" ; cbSecretKey : DWORD -> "int" ; pcbSecretKey : DWORD* out -> "var" ; pbCipherText : BYTE* optional, out -> "var" ; cbCipherText : DWORD -> "int" ; pcbCipherText : DWORD* out -> "var" ; dwFlags : DWORD -> "int" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; HRESULT NCryptEncapsulate(NCRYPT_KEY_HANDLE hKey, BYTE* pbSecretKey, DWORD cbSecretKey, DWORD* pcbSecretKey, BYTE* pbCipherText, DWORD cbCipherText, DWORD* pcbCipherText, DWORD dwFlags) #uselib "ncrypt.dll" #cfunc global NCryptEncapsulate "NCryptEncapsulate" intptr, intptr, int, intptr, intptr, int, intptr, int ; res = NCryptEncapsulate(hKey, varptr(pbSecretKey), cbSecretKey, varptr(pcbSecretKey), varptr(pbCipherText), cbCipherText, varptr(pcbCipherText), dwFlags) ; hKey : NCRYPT_KEY_HANDLE -> "intptr" ; pbSecretKey : BYTE* optional, out -> "intptr" ; cbSecretKey : DWORD -> "int" ; pcbSecretKey : DWORD* out -> "intptr" ; pbCipherText : BYTE* optional, out -> "intptr" ; cbCipherText : DWORD -> "int" ; pcbCipherText : DWORD* out -> "intptr" ; dwFlags : DWORD -> "int" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
ncrypt = windows.NewLazySystemDLL("ncrypt.dll")
procNCryptEncapsulate = ncrypt.NewProc("NCryptEncapsulate")
)
// hKey (NCRYPT_KEY_HANDLE), pbSecretKey (BYTE* optional, out), cbSecretKey (DWORD), pcbSecretKey (DWORD* out), pbCipherText (BYTE* optional, out), cbCipherText (DWORD), pcbCipherText (DWORD* out), dwFlags (DWORD)
r1, _, err := procNCryptEncapsulate.Call(
uintptr(hKey),
uintptr(pbSecretKey),
uintptr(cbSecretKey),
uintptr(pcbSecretKey),
uintptr(pbCipherText),
uintptr(cbCipherText),
uintptr(pcbCipherText),
uintptr(dwFlags),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // HRESULTfunction NCryptEncapsulate(
hKey: NativeUInt; // NCRYPT_KEY_HANDLE
pbSecretKey: Pointer; // BYTE* optional, out
cbSecretKey: DWORD; // DWORD
pcbSecretKey: Pointer; // DWORD* out
pbCipherText: Pointer; // BYTE* optional, out
cbCipherText: DWORD; // DWORD
pcbCipherText: Pointer; // DWORD* out
dwFlags: DWORD // DWORD
): Integer; stdcall;
external 'ncrypt.dll' name 'NCryptEncapsulate';result := DllCall("ncrypt\NCryptEncapsulate"
, "UPtr", hKey ; NCRYPT_KEY_HANDLE
, "Ptr", pbSecretKey ; BYTE* optional, out
, "UInt", cbSecretKey ; DWORD
, "Ptr", pcbSecretKey ; DWORD* out
, "Ptr", pbCipherText ; BYTE* optional, out
, "UInt", cbCipherText ; DWORD
, "Ptr", pcbCipherText ; DWORD* out
, "UInt", dwFlags ; DWORD
, "Int") ; return: HRESULT●NCryptEncapsulate(hKey, pbSecretKey, cbSecretKey, pcbSecretKey, pbCipherText, cbCipherText, pcbCipherText, dwFlags) = DLL("ncrypt.dll", "int NCryptEncapsulate(int, void*, dword, void*, void*, dword, void*, dword)")
# 呼び出し: NCryptEncapsulate(hKey, pbSecretKey, cbSecretKey, pcbSecretKey, pbCipherText, cbCipherText, pcbCipherText, dwFlags)
# hKey : NCRYPT_KEY_HANDLE -> "int"
# pbSecretKey : BYTE* optional, out -> "void*"
# cbSecretKey : DWORD -> "dword"
# pcbSecretKey : DWORD* out -> "void*"
# pbCipherText : BYTE* optional, out -> "void*"
# cbCipherText : DWORD -> "dword"
# pcbCipherText : DWORD* out -> "void*"
# dwFlags : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "ncrypt" fn NCryptEncapsulate(
hKey: usize, // NCRYPT_KEY_HANDLE
pbSecretKey: [*c]u8, // BYTE* optional, out
cbSecretKey: u32, // DWORD
pcbSecretKey: [*c]u32, // DWORD* out
pbCipherText: [*c]u8, // BYTE* optional, out
cbCipherText: u32, // DWORD
pcbCipherText: [*c]u32, // DWORD* out
dwFlags: u32 // DWORD
) callconv(std.os.windows.WINAPI) i32;proc NCryptEncapsulate(
hKey: uint, # NCRYPT_KEY_HANDLE
pbSecretKey: ptr uint8, # BYTE* optional, out
cbSecretKey: uint32, # DWORD
pcbSecretKey: ptr uint32, # DWORD* out
pbCipherText: ptr uint8, # BYTE* optional, out
cbCipherText: uint32, # DWORD
pcbCipherText: ptr uint32, # DWORD* out
dwFlags: uint32 # DWORD
): int32 {.importc: "NCryptEncapsulate", stdcall, dynlib: "ncrypt.dll".}pragma(lib, "ncrypt");
extern(Windows)
int NCryptEncapsulate(
size_t hKey, // NCRYPT_KEY_HANDLE
ubyte* pbSecretKey, // BYTE* optional, out
uint cbSecretKey, // DWORD
uint* pcbSecretKey, // DWORD* out
ubyte* pbCipherText, // BYTE* optional, out
uint cbCipherText, // DWORD
uint* pcbCipherText, // DWORD* out
uint dwFlags // DWORD
);ccall((:NCryptEncapsulate, "ncrypt.dll"), stdcall, Int32,
(Csize_t, Ptr{UInt8}, UInt32, Ptr{UInt32}, Ptr{UInt8}, UInt32, Ptr{UInt32}, UInt32),
hKey, pbSecretKey, cbSecretKey, pcbSecretKey, pbCipherText, cbCipherText, pcbCipherText, dwFlags)
# hKey : NCRYPT_KEY_HANDLE -> Csize_t
# pbSecretKey : BYTE* optional, out -> Ptr{UInt8}
# cbSecretKey : DWORD -> UInt32
# pcbSecretKey : DWORD* out -> Ptr{UInt32}
# pbCipherText : BYTE* optional, out -> Ptr{UInt8}
# cbCipherText : DWORD -> UInt32
# pcbCipherText : DWORD* out -> Ptr{UInt32}
# dwFlags : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t NCryptEncapsulate(
uintptr_t hKey,
uint8_t* pbSecretKey,
uint32_t cbSecretKey,
uint32_t* pcbSecretKey,
uint8_t* pbCipherText,
uint32_t cbCipherText,
uint32_t* pcbCipherText,
uint32_t dwFlags);
]]
local ncrypt = ffi.load("ncrypt")
-- ncrypt.NCryptEncapsulate(hKey, pbSecretKey, cbSecretKey, pcbSecretKey, pbCipherText, cbCipherText, pcbCipherText, dwFlags)
-- hKey : NCRYPT_KEY_HANDLE
-- pbSecretKey : BYTE* optional, out
-- cbSecretKey : DWORD
-- pcbSecretKey : DWORD* out
-- pbCipherText : BYTE* optional, out
-- cbCipherText : DWORD
-- pcbCipherText : DWORD* out
-- dwFlags : DWORD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('ncrypt.dll');
const NCryptEncapsulate = lib.func('__stdcall', 'NCryptEncapsulate', 'int32_t', ['uintptr_t', 'uint8_t *', 'uint32_t', 'uint32_t *', 'uint8_t *', 'uint32_t', 'uint32_t *', 'uint32_t']);
// NCryptEncapsulate(hKey, pbSecretKey, cbSecretKey, pcbSecretKey, pbCipherText, cbCipherText, pcbCipherText, dwFlags)
// hKey : NCRYPT_KEY_HANDLE -> 'uintptr_t'
// pbSecretKey : BYTE* optional, out -> 'uint8_t *'
// cbSecretKey : DWORD -> 'uint32_t'
// pcbSecretKey : DWORD* out -> 'uint32_t *'
// pbCipherText : BYTE* optional, out -> 'uint8_t *'
// cbCipherText : DWORD -> 'uint32_t'
// pcbCipherText : DWORD* out -> 'uint32_t *'
// dwFlags : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("ncrypt.dll", {
NCryptEncapsulate: { parameters: ["usize", "pointer", "u32", "pointer", "pointer", "u32", "pointer", "u32"], result: "i32" },
});
// lib.symbols.NCryptEncapsulate(hKey, pbSecretKey, cbSecretKey, pcbSecretKey, pbCipherText, cbCipherText, pcbCipherText, dwFlags)
// hKey : NCRYPT_KEY_HANDLE -> "usize"
// pbSecretKey : BYTE* optional, out -> "pointer"
// cbSecretKey : DWORD -> "u32"
// pcbSecretKey : DWORD* out -> "pointer"
// pbCipherText : BYTE* optional, out -> "pointer"
// cbCipherText : DWORD -> "u32"
// pcbCipherText : DWORD* out -> "pointer"
// dwFlags : DWORD -> "u32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t NCryptEncapsulate(
size_t hKey,
uint8_t* pbSecretKey,
uint32_t cbSecretKey,
uint32_t* pcbSecretKey,
uint8_t* pbCipherText,
uint32_t cbCipherText,
uint32_t* pcbCipherText,
uint32_t dwFlags);
C, "ncrypt.dll");
// $ffi->NCryptEncapsulate(hKey, pbSecretKey, cbSecretKey, pcbSecretKey, pbCipherText, cbCipherText, pcbCipherText, dwFlags);
// hKey : NCRYPT_KEY_HANDLE
// pbSecretKey : BYTE* optional, out
// cbSecretKey : DWORD
// pcbSecretKey : DWORD* out
// pbCipherText : BYTE* optional, out
// cbCipherText : DWORD
// pcbCipherText : DWORD* out
// 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 Ncrypt extends StdCallLibrary {
Ncrypt INSTANCE = Native.load("ncrypt", Ncrypt.class);
int NCryptEncapsulate(
long hKey, // NCRYPT_KEY_HANDLE
byte[] pbSecretKey, // BYTE* optional, out
int cbSecretKey, // DWORD
IntByReference pcbSecretKey, // DWORD* out
byte[] pbCipherText, // BYTE* optional, out
int cbCipherText, // DWORD
IntByReference pcbCipherText, // DWORD* out
int dwFlags // DWORD
);
}@[Link("ncrypt")]
lib Libncrypt
fun NCryptEncapsulate = NCryptEncapsulate(
hKey : LibC::SizeT, # NCRYPT_KEY_HANDLE
pbSecretKey : UInt8*, # BYTE* optional, out
cbSecretKey : UInt32, # DWORD
pcbSecretKey : UInt32*, # DWORD* out
pbCipherText : UInt8*, # BYTE* optional, out
cbCipherText : UInt32, # DWORD
pcbCipherText : UInt32*, # DWORD* out
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 NCryptEncapsulateNative = Int32 Function(UintPtr, Pointer<Uint8>, Uint32, Pointer<Uint32>, Pointer<Uint8>, Uint32, Pointer<Uint32>, Uint32);
typedef NCryptEncapsulateDart = int Function(int, Pointer<Uint8>, int, Pointer<Uint32>, Pointer<Uint8>, int, Pointer<Uint32>, int);
final NCryptEncapsulate = DynamicLibrary.open('ncrypt.dll')
.lookupFunction<NCryptEncapsulateNative, NCryptEncapsulateDart>('NCryptEncapsulate');
// hKey : NCRYPT_KEY_HANDLE -> UintPtr
// pbSecretKey : BYTE* optional, out -> Pointer<Uint8>
// cbSecretKey : DWORD -> Uint32
// pcbSecretKey : DWORD* out -> Pointer<Uint32>
// pbCipherText : BYTE* optional, out -> Pointer<Uint8>
// cbCipherText : DWORD -> Uint32
// pcbCipherText : DWORD* out -> Pointer<Uint32>
// dwFlags : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function NCryptEncapsulate(
hKey: NativeUInt; // NCRYPT_KEY_HANDLE
pbSecretKey: Pointer; // BYTE* optional, out
cbSecretKey: DWORD; // DWORD
pcbSecretKey: Pointer; // DWORD* out
pbCipherText: Pointer; // BYTE* optional, out
cbCipherText: DWORD; // DWORD
pcbCipherText: Pointer; // DWORD* out
dwFlags: DWORD // DWORD
): Integer; stdcall;
external 'ncrypt.dll' name 'NCryptEncapsulate';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "NCryptEncapsulate"
c_NCryptEncapsulate :: CUIntPtr -> Ptr Word8 -> Word32 -> Ptr Word32 -> Ptr Word8 -> Word32 -> Ptr Word32 -> Word32 -> IO Int32
-- hKey : NCRYPT_KEY_HANDLE -> CUIntPtr
-- pbSecretKey : BYTE* optional, out -> Ptr Word8
-- cbSecretKey : DWORD -> Word32
-- pcbSecretKey : DWORD* out -> Ptr Word32
-- pbCipherText : BYTE* optional, out -> Ptr Word8
-- cbCipherText : DWORD -> Word32
-- pcbCipherText : DWORD* out -> Ptr Word32
-- dwFlags : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let ncryptencapsulate =
foreign "NCryptEncapsulate"
(size_t @-> (ptr uint8_t) @-> uint32_t @-> (ptr uint32_t) @-> (ptr uint8_t) @-> uint32_t @-> (ptr uint32_t) @-> uint32_t @-> returning int32_t)
(* hKey : NCRYPT_KEY_HANDLE -> size_t *)
(* pbSecretKey : BYTE* optional, out -> (ptr uint8_t) *)
(* cbSecretKey : DWORD -> uint32_t *)
(* pcbSecretKey : DWORD* out -> (ptr uint32_t) *)
(* pbCipherText : BYTE* optional, out -> (ptr uint8_t) *)
(* cbCipherText : DWORD -> uint32_t *)
(* pcbCipherText : DWORD* out -> (ptr uint32_t) *)
(* dwFlags : DWORD -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library ncrypt (t "ncrypt.dll"))
(cffi:use-foreign-library ncrypt)
(cffi:defcfun ("NCryptEncapsulate" ncrypt-encapsulate :convention :stdcall) :int32
(h-key :uint64) ; NCRYPT_KEY_HANDLE
(pb-secret-key :pointer) ; BYTE* optional, out
(cb-secret-key :uint32) ; DWORD
(pcb-secret-key :pointer) ; DWORD* out
(pb-cipher-text :pointer) ; BYTE* optional, out
(cb-cipher-text :uint32) ; DWORD
(pcb-cipher-text :pointer) ; DWORD* out
(dw-flags :uint32)) ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $NCryptEncapsulate = Win32::API::More->new('ncrypt',
'int NCryptEncapsulate(WPARAM hKey, LPVOID pbSecretKey, DWORD cbSecretKey, LPVOID pcbSecretKey, LPVOID pbCipherText, DWORD cbCipherText, LPVOID pcbCipherText, DWORD dwFlags)');
# my $ret = $NCryptEncapsulate->Call($hKey, $pbSecretKey, $cbSecretKey, $pcbSecretKey, $pbCipherText, $cbCipherText, $pcbCipherText, $dwFlags);
# hKey : NCRYPT_KEY_HANDLE -> WPARAM
# pbSecretKey : BYTE* optional, out -> LPVOID
# cbSecretKey : DWORD -> DWORD
# pcbSecretKey : DWORD* out -> LPVOID
# pbCipherText : BYTE* optional, out -> LPVOID
# cbCipherText : DWORD -> DWORD
# pcbCipherText : DWORD* out -> LPVOID
# dwFlags : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。