29 NTSTATUS status = STATUS_SUCCESS;
30 BCRYPT_ALG_HANDLE hAlgCBC = 0;
31 BCRYPT_ALG_HANDLE hAlgECB = 0;
36 return STATUS_INVALID_PARAMETER;
40 return STATUS_SUCCESS;
47 if (!BCRYPT_SUCCESS(status = BCryptOpenAlgorithmProvider(&hAlgCBC,
53 if (!BCRYPT_SUCCESS(status =
54 BCryptSetProperty(hAlgCBC, BCRYPT_CHAINING_MODE,
55 (PBYTE) BCRYPT_CHAIN_MODE_CBC,
56 sizeof(BCRYPT_CHAIN_MODE_CBC), 0))) {
60 if (!BCRYPT_SUCCESS(status = BCryptOpenAlgorithmProvider(&hAlgECB,
66 if (!BCRYPT_SUCCESS(status =
67 BCryptSetProperty(hAlgECB, BCRYPT_CHAINING_MODE,
68 (PBYTE) BCRYPT_CHAIN_MODE_ECB,
69 sizeof(BCRYPT_CHAIN_MODE_ECB), 0))) {
73 if (!BCRYPT_SUCCESS(status = BCryptGetProperty(hAlgCBC, BCRYPT_OBJECT_LENGTH,
75 sizeof(DWORD), &cbData, 0))) {
79 ctx->hAlgCBC = hAlgCBC;
81 ctx->hAlgECB = hAlgECB;
83 ctx->cbKeyObj = cbKeyObj;
88 BCryptCloseAlgorithmProvider(hAlgCBC, 0);
91 BCryptCloseAlgorithmProvider(hAlgECB, 0);
97static NTSTATUS import_key(BCRYPT_ALG_HANDLE hAlg, BCRYPT_KEY_HANDLE *
phKey,
98 PBYTE *ppbKeyObj, DWORD cbKeyObj,
const uint8_t *key,
100 NTSTATUS status = STATUS_SUCCESS;
101 PBYTE pbKeyObj = NULL;
102 BCRYPT_KEY_HANDLE
hKey = 0;
103 PBYTE pbKeyBlob = NULL;
106 if (!
phKey || !ppbKeyObj) {
107 return STATUS_INVALID_PARAMETER;
112 BCryptDestroyKey(*
phKey);
123 if (!(pbKeyObj = (PBYTE) malloc(cbKeyObj))) {
124 status = STATUS_NO_MEMORY;
128 cbKeyBlob = (DWORD)(
sizeof(BCRYPT_KEY_DATA_BLOB_HEADER) +
key_len);
130 if (!(pbKeyBlob = (PBYTE) malloc(cbKeyBlob))) {
131 status = STATUS_NO_MEMORY;
136 ((BCRYPT_KEY_DATA_BLOB_HEADER *) pbKeyBlob)->dwMagic =
137 BCRYPT_KEY_DATA_BLOB_MAGIC;
138 ((BCRYPT_KEY_DATA_BLOB_HEADER *) pbKeyBlob)->dwVersion =
139 BCRYPT_KEY_DATA_BLOB_VERSION1;
140 ((BCRYPT_KEY_DATA_BLOB_HEADER *) pbKeyBlob)->cbKeyData = (DWORD)
key_len;
141 memcpy(pbKeyBlob +
sizeof(BCRYPT_KEY_DATA_BLOB_HEADER), key,
key_len);
143 if (!BCRYPT_SUCCESS(status = BCryptImportKey(hAlg, NULL, BCRYPT_KEY_DATA_BLOB,
144 &
hKey, pbKeyObj, cbKeyObj,
145 pbKeyBlob, cbKeyBlob, 0))) {
152 *ppbKeyObj = pbKeyObj;
158 BCryptDestroyKey(
hKey);
167 return !BCRYPT_SUCCESS(status);
174 NTSTATUS status = STATUS_SUCCESS;
176 if (!BCRYPT_SUCCESS(status = init_ctx(ctx))) {
180 if (!BCRYPT_SUCCESS(status = import_key(ctx->hAlgCBC, &(ctx->hKeyCBC),
181 &(ctx->pbKeyCBCObj), ctx->cbKeyObj,
186 if (!BCRYPT_SUCCESS(status = import_key(ctx->hAlgECB, &(ctx->hKeyECB),
187 &(ctx->pbKeyECBObj), ctx->cbKeyObj,
193 AES_set_encrypt_key(key,
key_len * 8, &ctx->
key);
203 NTSTATUS status = STATUS_SUCCESS;
205 if (!BCRYPT_SUCCESS(status = init_ctx(ctx))) {
209 if (!BCRYPT_SUCCESS(status = import_key(ctx->hAlgCBC, &(ctx->hKeyCBC),
210 &(ctx->pbKeyCBCObj), ctx->cbKeyObj,
215 if (!BCRYPT_SUCCESS(status = import_key(ctx->hAlgECB, &(ctx->hKeyECB),
216 &(ctx->pbKeyECBObj), ctx->cbKeyObj,
222 AES_set_decrypt_key(key,
key_len * 8, &ctx->
key);
231 NTSTATUS status = STATUS_SUCCESS;
234 if (!BCRYPT_SUCCESS(status = BCryptEncrypt(ctx->hKeyECB, in,
AES_BLOCK_SIZE,
242 AES_ecb_encrypt(in, out, &ctx->
key, AES_ENCRYPT);
251 NTSTATUS status = STATUS_SUCCESS;
254 if (!BCRYPT_SUCCESS(status = BCryptDecrypt(ctx->hKeyECB, in,
AES_BLOCK_SIZE,
264 AES_ecb_encrypt(in, out, &ctx->
key, AES_DECRYPT);
274 NTSTATUS status = STATUS_SUCCESS;
277 if (!BCRYPT_SUCCESS(status = BCryptEncrypt(ctx->hKeyCBC, in,
len, NULL, iv,
285 AES_cbc_encrypt(in, out,
len, &ctx->
key, iv, AES_ENCRYPT);
295 NTSTATUS status = STATUS_SUCCESS;
298 if (!BCRYPT_SUCCESS(status = BCryptDecrypt(ctx->hKeyCBC, in,
len, NULL, iv,
306 AES_cbc_encrypt(in, out,
len, &ctx->
key, iv, AES_DECRYPT);
323 while (in[(*
len) - 1] == 0) {
338 BCryptDestroyKey(ctx->hKeyCBC);
340 if (ctx->pbKeyCBCObj) {
341 free(ctx->pbKeyCBCObj);
344 BCryptDestroyKey(ctx->hKeyECB);
346 if (ctx->pbKeyECBObj) {
347 free(ctx->pbKeyECBObj);
350 BCryptCloseAlgorithmProvider(ctx->hAlgCBC, 0);
353 BCryptCloseAlgorithmProvider(ctx->hAlgECB, 0);
void aes_remove_padding(uint8_t *in, uint16_t *len)
uint8_t aes_set_encrypt_key(uint8_t *key, uint16_t key_len, aes_context *ctx)
void aes_destroy(aes_context *ctx)
uint8_t aes_encrypt(uint8_t *in, uint8_t *out, const aes_context *ctx)
uint8_t aes_decrypt(uint8_t *in, uint8_t *out, const aes_context *ctx)
uint8_t aes_cbc_decrypt(uint8_t *in, uint8_t *out, uint16_t len, uint8_t *iv, aes_context *ctx)
void aes_add_padding(uint8_t *in, uint16_t *len)
uint8_t aes_set_decrypt_key(uint8_t *key, uint16_t key_len, aes_context *ctx)
uint8_t aes_cbc_encrypt(uint8_t *in, uint8_t *out, uint16_t len, uint8_t *iv, aes_context *ctx)
#define insecure_memzero(buf, len)
CK_SESSION_HANDLE CK_MECHANISM_PTR CK_OBJECT_HANDLE hKey
session operation op sign key_len
memcpy((char *) pInfo->slotDescription, s, l)
CK_SESSION_HANDLE CK_MECHANISM_PTR CK_ATTRIBUTE_PTR CK_ULONG CK_OBJECT_HANDLE_PTR phKey