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

BCryptAddContextFunction

関数
CNGコンテキストの指定インターフェイスに暗号機能を追加する。
DLLbcrypt.dll呼出規約winapi対応OSWindows Vista 以降

シグネチャ

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

NTSTATUS BCryptAddContextFunction(
    BCRYPT_TABLE dwTable,
    LPCWSTR pszContext,
    BCRYPT_INTERFACE dwInterface,
    LPCWSTR pszFunction,
    DWORD dwPosition
);

パラメーター

名前方向説明
dwTableBCRYPT_TABLEin

コンテキストが存在する構成テーブルを識別します。次のいずれかの値を指定できます。

意味
CRYPT_LOCAL
コンテキストはローカルマシンの構成テーブルに存在します。
CRYPT_DOMAIN
この値は使用できません。
pszContextLPCWSTRin関数を追加するコンテキストの識別子を含む、null で終わる Unicode 文字列へのポインターです。
dwInterfaceBCRYPT_INTERFACEin

関数を追加する暗号化インターフェイスを識別します。次のいずれかの値を指定できます。

意味
BCRYPT_ASYMMETRIC_ENCRYPTION_INTERFACE
非対称暗号化関数の一覧に関数を追加します。
BCRYPT_CIPHER_INTERFACE
暗号 (cipher) 関数の一覧に関数を追加します。
BCRYPT_HASH_INTERFACE
ハッシュ関数の一覧に関数を追加します。
BCRYPT_RNG_INTERFACE
乱数生成関数の一覧に関数を追加します。
BCRYPT_SECRET_AGREEMENT_INTERFACE
秘密共有 (secret agreement) 関数の一覧に関数を追加します。
BCRYPT_SIGNATURE_INTERFACE
署名関数の一覧に関数を追加します。
NCRYPT_KEY_STORAGE_INTERFACE
キーストレージ関数の一覧に関数を追加します。
NCRYPT_SCHANNEL_INTERFACE
Schannel 関数の一覧に関数を追加します。
NCRYPT_SCHANNEL_SIGNATURE_INTERFACE
Schannel が TLS 1.2 で受け入れる署名スイートの一覧に関数を追加します。

Windows Vista および Windows Server 2008: この値はサポートされていません。

pszFunctionLPCWSTRin追加する暗号化関数の識別子を含む、null で終わる Unicode 文字列へのポインターです。
dwPositionDWORDinこの関数を一覧に挿入する位置を指定します。関数は、既存の関数より前のこの位置に挿入されます。CRYPT_PRIORITY_TOP の値は、一覧の先頭に関数を挿入するために使用します。CRYPT_PRIORITY_BOTTOM の値は、一覧の末尾に関数を挿入するために使用します。

戻り値の型: NTSTATUS

公式ドキュメント

既存の CNG コンテキストでサポートされる関数の一覧に、暗号化関数を追加します。

戻り値

関数の成功または失敗を示すステータスコードを返します。

返される可能性のあるコードには、次のものが含まれますが、これらに限定されません。

戻り値 説明
STATUS_SUCCESS
関数は成功しました。
STATUS_INVALID_PARAMETER
1 つ以上のパラメーターが無効です。
STATUS_NO_MEMORY
メモリ割り当てエラーが発生しました。
STATUS_NOT_FOUND
コンテキストが見つかりませんでした。

解説(Remarks)

追加する関数が既に一覧に存在する場合、その関数は削除され、新しい位置に挿入されます。

BCryptAddContextFunction はユーザーモードでのみ呼び出すことができます。

出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

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

NTSTATUS BCryptAddContextFunction(
    BCRYPT_TABLE dwTable,
    LPCWSTR pszContext,
    BCRYPT_INTERFACE dwInterface,
    LPCWSTR pszFunction,
    DWORD dwPosition
);
[DllImport("bcrypt.dll", ExactSpelling = true)]
static extern int BCryptAddContextFunction(
    uint dwTable,   // BCRYPT_TABLE
    [MarshalAs(UnmanagedType.LPWStr)] string pszContext,   // LPCWSTR
    uint dwInterface,   // BCRYPT_INTERFACE
    [MarshalAs(UnmanagedType.LPWStr)] string pszFunction,   // LPCWSTR
    uint dwPosition   // DWORD
);
<DllImport("bcrypt.dll", ExactSpelling:=True)>
Public Shared Function BCryptAddContextFunction(
    dwTable As UInteger,   ' BCRYPT_TABLE
    <MarshalAs(UnmanagedType.LPWStr)> pszContext As String,   ' LPCWSTR
    dwInterface As UInteger,   ' BCRYPT_INTERFACE
    <MarshalAs(UnmanagedType.LPWStr)> pszFunction As String,   ' LPCWSTR
    dwPosition As UInteger   ' DWORD
) As Integer
End Function
' dwTable : BCRYPT_TABLE
' pszContext : LPCWSTR
' dwInterface : BCRYPT_INTERFACE
' pszFunction : LPCWSTR
' dwPosition : DWORD
Declare PtrSafe Function BCryptAddContextFunction Lib "bcrypt" ( _
    ByVal dwTable As Long, _
    ByVal pszContext As LongPtr, _
    ByVal dwInterface As Long, _
    ByVal pszFunction As LongPtr, _
    ByVal dwPosition As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

BCryptAddContextFunction = ctypes.windll.bcrypt.BCryptAddContextFunction
BCryptAddContextFunction.restype = ctypes.c_int
BCryptAddContextFunction.argtypes = [
    wintypes.DWORD,  # dwTable : BCRYPT_TABLE
    wintypes.LPCWSTR,  # pszContext : LPCWSTR
    wintypes.DWORD,  # dwInterface : BCRYPT_INTERFACE
    wintypes.LPCWSTR,  # pszFunction : LPCWSTR
    wintypes.DWORD,  # dwPosition : DWORD
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('bcrypt.dll')
BCryptAddContextFunction = Fiddle::Function.new(
  lib['BCryptAddContextFunction'],
  [
    -Fiddle::TYPE_INT,  # dwTable : BCRYPT_TABLE
    Fiddle::TYPE_VOIDP,  # pszContext : LPCWSTR
    -Fiddle::TYPE_INT,  # dwInterface : BCRYPT_INTERFACE
    Fiddle::TYPE_VOIDP,  # pszFunction : LPCWSTR
    -Fiddle::TYPE_INT,  # dwPosition : DWORD
  ],
  Fiddle::TYPE_INT)
#[link(name = "bcrypt")]
extern "system" {
    fn BCryptAddContextFunction(
        dwTable: u32,  // BCRYPT_TABLE
        pszContext: *const u16,  // LPCWSTR
        dwInterface: u32,  // BCRYPT_INTERFACE
        pszFunction: *const u16,  // LPCWSTR
        dwPosition: u32  // DWORD
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("bcrypt.dll")]
public static extern int BCryptAddContextFunction(uint dwTable, [MarshalAs(UnmanagedType.LPWStr)] string pszContext, uint dwInterface, [MarshalAs(UnmanagedType.LPWStr)] string pszFunction, uint dwPosition);
"@
$api = Add-Type -MemberDefinition $sig -Name 'bcrypt_BCryptAddContextFunction' -Namespace Win32 -PassThru
# $api::BCryptAddContextFunction(dwTable, pszContext, dwInterface, pszFunction, dwPosition)
#uselib "bcrypt.dll"
#func global BCryptAddContextFunction "BCryptAddContextFunction" sptr, sptr, sptr, sptr, sptr
; BCryptAddContextFunction dwTable, pszContext, dwInterface, pszFunction, dwPosition   ; 戻り値は stat
; dwTable : BCRYPT_TABLE -> "sptr"
; pszContext : LPCWSTR -> "sptr"
; dwInterface : BCRYPT_INTERFACE -> "sptr"
; pszFunction : LPCWSTR -> "sptr"
; dwPosition : DWORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "bcrypt.dll"
#cfunc global BCryptAddContextFunction "BCryptAddContextFunction" int, wstr, int, wstr, int
; res = BCryptAddContextFunction(dwTable, pszContext, dwInterface, pszFunction, dwPosition)
; dwTable : BCRYPT_TABLE -> "int"
; pszContext : LPCWSTR -> "wstr"
; dwInterface : BCRYPT_INTERFACE -> "int"
; pszFunction : LPCWSTR -> "wstr"
; dwPosition : DWORD -> "int"
; NTSTATUS BCryptAddContextFunction(BCRYPT_TABLE dwTable, LPCWSTR pszContext, BCRYPT_INTERFACE dwInterface, LPCWSTR pszFunction, DWORD dwPosition)
#uselib "bcrypt.dll"
#cfunc global BCryptAddContextFunction "BCryptAddContextFunction" int, wstr, int, wstr, int
; res = BCryptAddContextFunction(dwTable, pszContext, dwInterface, pszFunction, dwPosition)
; dwTable : BCRYPT_TABLE -> "int"
; pszContext : LPCWSTR -> "wstr"
; dwInterface : BCRYPT_INTERFACE -> "int"
; pszFunction : LPCWSTR -> "wstr"
; dwPosition : DWORD -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	bcrypt = windows.NewLazySystemDLL("bcrypt.dll")
	procBCryptAddContextFunction = bcrypt.NewProc("BCryptAddContextFunction")
)

// dwTable (BCRYPT_TABLE), pszContext (LPCWSTR), dwInterface (BCRYPT_INTERFACE), pszFunction (LPCWSTR), dwPosition (DWORD)
r1, _, err := procBCryptAddContextFunction.Call(
	uintptr(dwTable),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pszContext))),
	uintptr(dwInterface),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pszFunction))),
	uintptr(dwPosition),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // NTSTATUS
function BCryptAddContextFunction(
  dwTable: DWORD;   // BCRYPT_TABLE
  pszContext: PWideChar;   // LPCWSTR
  dwInterface: DWORD;   // BCRYPT_INTERFACE
  pszFunction: PWideChar;   // LPCWSTR
  dwPosition: DWORD   // DWORD
): Integer; stdcall;
  external 'bcrypt.dll' name 'BCryptAddContextFunction';
result := DllCall("bcrypt\BCryptAddContextFunction"
    , "UInt", dwTable   ; BCRYPT_TABLE
    , "WStr", pszContext   ; LPCWSTR
    , "UInt", dwInterface   ; BCRYPT_INTERFACE
    , "WStr", pszFunction   ; LPCWSTR
    , "UInt", dwPosition   ; DWORD
    , "Int")   ; return: NTSTATUS
●BCryptAddContextFunction(dwTable, pszContext, dwInterface, pszFunction, dwPosition) = DLL("bcrypt.dll", "int BCryptAddContextFunction(dword, char*, dword, char*, dword)")
# 呼び出し: BCryptAddContextFunction(dwTable, pszContext, dwInterface, pszFunction, dwPosition)
# dwTable : BCRYPT_TABLE -> "dword"
# pszContext : LPCWSTR -> "char*"
# dwInterface : BCRYPT_INTERFACE -> "dword"
# pszFunction : LPCWSTR -> "char*"
# dwPosition : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "bcrypt" fn BCryptAddContextFunction(
    dwTable: u32, // BCRYPT_TABLE
    pszContext: [*c]const u16, // LPCWSTR
    dwInterface: u32, // BCRYPT_INTERFACE
    pszFunction: [*c]const u16, // LPCWSTR
    dwPosition: u32 // DWORD
) callconv(std.os.windows.WINAPI) i32;
proc BCryptAddContextFunction(
    dwTable: uint32,  # BCRYPT_TABLE
    pszContext: WideCString,  # LPCWSTR
    dwInterface: uint32,  # BCRYPT_INTERFACE
    pszFunction: WideCString,  # LPCWSTR
    dwPosition: uint32  # DWORD
): int32 {.importc: "BCryptAddContextFunction", stdcall, dynlib: "bcrypt.dll".}
pragma(lib, "bcrypt");
extern(Windows)
int BCryptAddContextFunction(
    uint dwTable,   // BCRYPT_TABLE
    const(wchar)* pszContext,   // LPCWSTR
    uint dwInterface,   // BCRYPT_INTERFACE
    const(wchar)* pszFunction,   // LPCWSTR
    uint dwPosition   // DWORD
);
ccall((:BCryptAddContextFunction, "bcrypt.dll"), stdcall, Int32,
      (UInt32, Cwstring, UInt32, Cwstring, UInt32),
      dwTable, pszContext, dwInterface, pszFunction, dwPosition)
# dwTable : BCRYPT_TABLE -> UInt32
# pszContext : LPCWSTR -> Cwstring
# dwInterface : BCRYPT_INTERFACE -> UInt32
# pszFunction : LPCWSTR -> Cwstring
# dwPosition : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t BCryptAddContextFunction(
    uint32_t dwTable,
    const uint16_t* pszContext,
    uint32_t dwInterface,
    const uint16_t* pszFunction,
    uint32_t dwPosition);
]]
local bcrypt = ffi.load("bcrypt")
-- bcrypt.BCryptAddContextFunction(dwTable, pszContext, dwInterface, pszFunction, dwPosition)
-- dwTable : BCRYPT_TABLE
-- pszContext : LPCWSTR
-- dwInterface : BCRYPT_INTERFACE
-- pszFunction : LPCWSTR
-- dwPosition : DWORD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('bcrypt.dll');
const BCryptAddContextFunction = lib.func('__stdcall', 'BCryptAddContextFunction', 'int32_t', ['uint32_t', 'str16', 'uint32_t', 'str16', 'uint32_t']);
// BCryptAddContextFunction(dwTable, pszContext, dwInterface, pszFunction, dwPosition)
// dwTable : BCRYPT_TABLE -> 'uint32_t'
// pszContext : LPCWSTR -> 'str16'
// dwInterface : BCRYPT_INTERFACE -> 'uint32_t'
// pszFunction : LPCWSTR -> 'str16'
// dwPosition : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("bcrypt.dll", {
  BCryptAddContextFunction: { parameters: ["u32", "buffer", "u32", "buffer", "u32"], result: "i32" },
});
// lib.symbols.BCryptAddContextFunction(dwTable, pszContext, dwInterface, pszFunction, dwPosition)
// dwTable : BCRYPT_TABLE -> "u32"
// pszContext : LPCWSTR -> "buffer"
// dwInterface : BCRYPT_INTERFACE -> "u32"
// pszFunction : LPCWSTR -> "buffer"
// dwPosition : DWORD -> "u32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t BCryptAddContextFunction(
    uint32_t dwTable,
    const uint16_t* pszContext,
    uint32_t dwInterface,
    const uint16_t* pszFunction,
    uint32_t dwPosition);
C, "bcrypt.dll");
// $ffi->BCryptAddContextFunction(dwTable, pszContext, dwInterface, pszFunction, dwPosition);
// dwTable : BCRYPT_TABLE
// pszContext : LPCWSTR
// dwInterface : BCRYPT_INTERFACE
// pszFunction : LPCWSTR
// dwPosition : 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 BCryptAddContextFunction(
        int dwTable,   // BCRYPT_TABLE
        WString pszContext,   // LPCWSTR
        int dwInterface,   // BCRYPT_INTERFACE
        WString pszFunction,   // LPCWSTR
        int dwPosition   // DWORD
    );
}
@[Link("bcrypt")]
lib Libbcrypt
  fun BCryptAddContextFunction = BCryptAddContextFunction(
    dwTable : UInt32,   # BCRYPT_TABLE
    pszContext : UInt16*,   # LPCWSTR
    dwInterface : UInt32,   # BCRYPT_INTERFACE
    pszFunction : UInt16*,   # LPCWSTR
    dwPosition : 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 BCryptAddContextFunctionNative = Int32 Function(Uint32, Pointer<Utf16>, Uint32, Pointer<Utf16>, Uint32);
typedef BCryptAddContextFunctionDart = int Function(int, Pointer<Utf16>, int, Pointer<Utf16>, int);
final BCryptAddContextFunction = DynamicLibrary.open('bcrypt.dll')
    .lookupFunction<BCryptAddContextFunctionNative, BCryptAddContextFunctionDart>('BCryptAddContextFunction');
// dwTable : BCRYPT_TABLE -> Uint32
// pszContext : LPCWSTR -> Pointer<Utf16>
// dwInterface : BCRYPT_INTERFACE -> Uint32
// pszFunction : LPCWSTR -> Pointer<Utf16>
// dwPosition : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function BCryptAddContextFunction(
  dwTable: DWORD;   // BCRYPT_TABLE
  pszContext: PWideChar;   // LPCWSTR
  dwInterface: DWORD;   // BCRYPT_INTERFACE
  pszFunction: PWideChar;   // LPCWSTR
  dwPosition: DWORD   // DWORD
): Integer; stdcall;
  external 'bcrypt.dll' name 'BCryptAddContextFunction';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "BCryptAddContextFunction"
  c_BCryptAddContextFunction :: Word32 -> CWString -> Word32 -> CWString -> Word32 -> IO Int32
-- dwTable : BCRYPT_TABLE -> Word32
-- pszContext : LPCWSTR -> CWString
-- dwInterface : BCRYPT_INTERFACE -> Word32
-- pszFunction : LPCWSTR -> CWString
-- dwPosition : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let bcryptaddcontextfunction =
  foreign "BCryptAddContextFunction"
    (uint32_t @-> (ptr uint16_t) @-> uint32_t @-> (ptr uint16_t) @-> uint32_t @-> returning int32_t)
(* dwTable : BCRYPT_TABLE -> uint32_t *)
(* pszContext : LPCWSTR -> (ptr uint16_t) *)
(* dwInterface : BCRYPT_INTERFACE -> uint32_t *)
(* pszFunction : LPCWSTR -> (ptr uint16_t) *)
(* dwPosition : 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 ("BCryptAddContextFunction" bcrypt-add-context-function :convention :stdcall) :int32
  (dw-table :uint32)   ; BCRYPT_TABLE
  (psz-context (:string :encoding :utf-16le))   ; LPCWSTR
  (dw-interface :uint32)   ; BCRYPT_INTERFACE
  (psz-function (:string :encoding :utf-16le))   ; LPCWSTR
  (dw-position :uint32))   ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $BCryptAddContextFunction = Win32::API::More->new('bcrypt',
    'int BCryptAddContextFunction(DWORD dwTable, LPCWSTR pszContext, DWORD dwInterface, LPCWSTR pszFunction, DWORD dwPosition)');
# my $ret = $BCryptAddContextFunction->Call($dwTable, $pszContext, $dwInterface, $pszFunction, $dwPosition);
# dwTable : BCRYPT_TABLE -> DWORD
# pszContext : LPCWSTR -> LPCWSTR
# dwInterface : BCRYPT_INTERFACE -> DWORD
# pszFunction : LPCWSTR -> LPCWSTR
# dwPosition : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

公式の関連項目
使用する型