| 1 |
/* |
| 2 |
* Copyright (c) 2015, Secure Endpoints Inc. |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions |
| 7 |
* are met: |
| 8 |
* |
| 9 |
* - Redistributions of source code must retain the above copyright |
| 10 |
* notice, this list of conditions and the following disclaimer. |
| 11 |
* |
| 12 |
* - Redistributions in binary form must reproduce the above copyright |
| 13 |
* notice, this list of conditions and the following disclaimer in |
| 14 |
* the documentation and/or other materials provided with the |
| 15 |
* distribution. |
| 16 |
* |
| 17 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 18 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 19 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 20 |
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 21 |
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 22 |
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 23 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 24 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 25 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 26 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 28 |
* OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
*/ |
| 30 |
|
| 31 |
/* Windows CNG provider */ |
| 32 |
|
| 33 |
#include <config.h> |
| 34 |
#include <roken.h> |
| 35 |
#include <assert.h> |
| 36 |
|
| 37 |
#include <evp.h> |
| 38 |
#include <evp-wincng.h> |
| 39 |
|
| 40 |
#include <bcrypt.h> |
| 41 |
|
| 42 |
/* |
| 43 |
* CNG cipher provider |
| 44 |
*/ |
| 45 |
|
| 46 |
struct wincng_key { |
| 47 |
BCRYPT_KEY_HANDLE hKey; |
| 48 |
UCHAR rgbKeyObject[1]; |
| 49 |
}; |
| 50 |
|
| 51 |
#define WINCNG_KEY_OBJECT_SIZE(ctx) \ |
| 52 |
((ctx)->cipher->ctx_size - sizeof(struct wincng_key) + 1) |
| 53 |
|
| 54 |
static int |
| 55 |
wincng_do_cipher(EVP_CIPHER_CTX *ctx, |
| 56 |
unsigned char *out, |
| 57 |
const unsigned char *in, |
| 58 |
unsigned int size) |
| 59 |
{ |
| 60 |
struct wincng_key *cng = ctx->cipher_data; |
| 61 |
NTSTATUS status; |
| 62 |
ULONG cbResult; |
| 63 |
|
| 64 |
assert(EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_STREAM_CIPHER || |
| 65 |
(size % ctx->cipher->block_size) == 0); |
| 66 |
|
| 67 |
if (ctx->encrypt) { |
| 68 |
status = BCryptEncrypt(cng->hKey, |
| 69 |
(PUCHAR)in, |
| 70 |
size, |
| 71 |
NULL, /* pPaddingInfo */ |
| 72 |
ctx->cipher->iv_len ? ctx->iv : NULL, |
| 73 |
ctx->cipher->iv_len, |
| 74 |
out, |
| 75 |
size, |
| 76 |
&cbResult, |
| 77 |
0); |
| 78 |
} else { |
| 79 |
status = BCryptDecrypt(cng->hKey, |
| 80 |
(PUCHAR)in, |
| 81 |
size, |
| 82 |
NULL, /* pPaddingInfo */ |
| 83 |
ctx->cipher->iv_len ? ctx->iv : NULL, |
| 84 |
ctx->cipher->iv_len, |
| 85 |
out, |
| 86 |
size, |
| 87 |
&cbResult, |
| 88 |
0); |
| 89 |
} |
| 90 |
|
| 91 |
return BCRYPT_SUCCESS(status) && cbResult == size; |
| 92 |
} |
| 93 |
|
| 94 |
static int |
| 95 |
wincng_cleanup(EVP_CIPHER_CTX *ctx) |
| 96 |
{ |
| 97 |
struct wincng_key *cng = ctx->cipher_data; |
| 98 |
|
| 99 |
if (cng->hKey) |
| 100 |
BCryptDestroyKey(cng->hKey); |
| 101 |
SecureZeroMemory(cng->rgbKeyObject, WINCNG_KEY_OBJECT_SIZE(ctx)); |
| 102 |
|
| 103 |
return 1; |
| 104 |
} |
| 105 |
|
| 106 |
static int |
| 107 |
wincng_cipher_algorithm_init(EVP_CIPHER *cipher, |
| 108 |
LPWSTR pszAlgId) |
| 109 |
{ |
| 110 |
BCRYPT_ALG_HANDLE hAlgorithm = NULL; |
| 111 |
NTSTATUS status; |
| 112 |
LPCWSTR pszChainingMode; |
| 113 |
ULONG cbKeyObject, cbChainingMode, cbData; |
| 114 |
|
| 115 |
if (cipher->app_data) |
| 116 |
return 1; |
| 117 |
|
| 118 |
status = BCryptOpenAlgorithmProvider(&hAlgorithm, |
| 119 |
pszAlgId, |
| 120 |
NULL, |
| 121 |
0); |
| 122 |
if (!BCRYPT_SUCCESS(status)) |
| 123 |
return 0; |
| 124 |
|
| 125 |
status = BCryptGetProperty(hAlgorithm, |
| 126 |
BCRYPT_OBJECT_LENGTH, |
| 127 |
(PUCHAR)&cbKeyObject, |
| 128 |
sizeof(ULONG), |
| 129 |
&cbData, |
| 130 |
0); |
| 131 |
if (!BCRYPT_SUCCESS(status)) { |
| 132 |
BCryptCloseAlgorithmProvider(hAlgorithm, 0); |
| 133 |
return 0; |
| 134 |
} |
| 135 |
|
| 136 |
cipher->ctx_size = sizeof(struct wincng_key) + cbKeyObject - 1; |
| 137 |
|
| 138 |
switch (cipher->flags & EVP_CIPH_MODE) { |
| 139 |
case EVP_CIPH_CBC_MODE: |
| 140 |
pszChainingMode = BCRYPT_CHAIN_MODE_CBC; |
| 141 |
cbChainingMode = sizeof(BCRYPT_CHAIN_MODE_CBC); |
| 142 |
break; |
| 143 |
case EVP_CIPH_CFB8_MODE: |
| 144 |
pszChainingMode = BCRYPT_CHAIN_MODE_CFB; |
| 145 |
cbChainingMode = sizeof(BCRYPT_CHAIN_MODE_CFB); |
| 146 |
break; |
| 147 |
default: |
| 148 |
pszChainingMode = NULL; |
| 149 |
cbChainingMode = 0; |
| 150 |
break; |
| 151 |
} |
| 152 |
|
| 153 |
if (cbChainingMode) { |
| 154 |
status = BCryptSetProperty(hAlgorithm, |
| 155 |
BCRYPT_CHAINING_MODE, |
| 156 |
(PUCHAR)pszChainingMode, |
| 157 |
cbChainingMode, |
| 158 |
0); |
| 159 |
if (!BCRYPT_SUCCESS(status)) { |
| 160 |
BCryptCloseAlgorithmProvider(hAlgorithm, 0); |
| 161 |
return 0; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
if (wcscmp(pszAlgId, BCRYPT_RC2_ALGORITHM) == 0) { |
| 166 |
ULONG cbEffectiveKeyLength = EVP_CIPHER_key_length(cipher) * 8; |
| 167 |
|
| 168 |
status = BCryptSetProperty(hAlgorithm, |
| 169 |
BCRYPT_EFFECTIVE_KEY_LENGTH, |
| 170 |
(PUCHAR)&cbEffectiveKeyLength, |
| 171 |
sizeof(cbEffectiveKeyLength), |
| 172 |
0); |
| 173 |
if (!BCRYPT_SUCCESS(status)) { |
| 174 |
BCryptCloseAlgorithmProvider(hAlgorithm, 0); |
| 175 |
return 0; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
InterlockedCompareExchangePointerRelease(&cipher->app_data, |
| 180 |
hAlgorithm, NULL); |
| 181 |
return 1; |
| 182 |
} |
| 183 |
|
| 184 |
static int |
| 185 |
wincng_key_init(EVP_CIPHER_CTX *ctx, |
| 186 |
const unsigned char *key, |
| 187 |
const unsigned char *iv, |
| 188 |
int encp) |
| 189 |
{ |
| 190 |
struct wincng_key *cng = ctx->cipher_data; |
| 191 |
NTSTATUS status; |
| 192 |
|
| 193 |
assert(cng != NULL); |
| 194 |
assert(ctx->cipher != NULL); |
| 195 |
|
| 196 |
if (ctx->cipher->app_data == NULL) |
| 197 |
return 0; |
| 198 |
|
| 199 |
if (cng->hKey) { |
| 200 |
BCryptDestroyKey(cng->hKey); /* allow reinitialization */ |
| 201 |
cng->hKey = (BCRYPT_KEY_HANDLE)0; |
| 202 |
} |
| 203 |
|
| 204 |
/* |
| 205 |
* Note: ctx->key_len not EVP_CIPHER_CTX_key_length() for |
| 206 |
* variable length key support. |
| 207 |
*/ |
| 208 |
status = BCryptGenerateSymmetricKey(ctx->cipher->app_data, |
| 209 |
&cng->hKey, |
| 210 |
cng->rgbKeyObject, |
| 211 |
WINCNG_KEY_OBJECT_SIZE(ctx), |
| 212 |
(PUCHAR)key, |
| 213 |
ctx->key_len, |
| 214 |
0); |
| 215 |
|
| 216 |
return BCRYPT_SUCCESS(status); |
| 217 |
} |
| 218 |
|
| 219 |
#define WINCNG_CIPHER_ALGORITHM(name, alg_id, block_size, key_len, \ |
| 220 |
iv_len, flags) \ |
| 221 |
\ |
| 222 |
static EVP_CIPHER \ |
| 223 |
wincng_##name = { \ |
| 224 |
0, \ |
| 225 |
block_size, \ |
| 226 |
key_len, \ |
| 227 |
iv_len, \ |
| 228 |
flags, \ |
| 229 |
wincng_key_init, \ |
| 230 |
wincng_do_cipher, \ |
| 231 |
wincng_cleanup, \ |
| 232 |
0, \ |
| 233 |
NULL, \ |
| 234 |
NULL, \ |
| 235 |
NULL, \ |
| 236 |
NULL \ |
| 237 |
}; \ |
| 238 |
\ |
| 239 |
const EVP_CIPHER * \ |
| 240 |
hc_EVP_wincng_##name(void) \ |
| 241 |
{ \ |
| 242 |
wincng_cipher_algorithm_init(&wincng_##name, alg_id); \ |
| 243 |
return wincng_##name.app_data ? &wincng_##name : NULL; \ |
| 244 |
} |
| 245 |
|
| 246 |
#define WINCNG_CIPHER_ALGORITHM_CLEANUP(name) do { \ |
| 247 |
if (wincng_##name.app_data) { \ |
| 248 |
BCryptCloseAlgorithmProvider(wincng_##name.app_data, 0); \ |
| 249 |
wincng_##name.app_data = NULL; \ |
| 250 |
} \ |
| 251 |
} while (0) |
| 252 |
|
| 253 |
#define WINCNG_CIPHER_ALGORITHM_UNAVAILABLE(name) \ |
| 254 |
\ |
| 255 |
const EVP_CIPHER * \ |
| 256 |
hc_EVP_wincng_##name(void) \ |
| 257 |
{ \ |
| 258 |
return NULL; \ |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* The triple DES cipher type (Windows CNG provider) |
| 263 |
* |
| 264 |
* @return the DES-EDE3-CBC EVP_CIPHER pointer. |
| 265 |
* |
| 266 |
* @ingroup hcrypto_evp |
| 267 |
*/ |
| 268 |
|
| 269 |
WINCNG_CIPHER_ALGORITHM(des_ede3_cbc, |
| 270 |
BCRYPT_3DES_ALGORITHM, |
| 271 |
8, |
| 272 |
24, |
| 273 |
8, |
| 274 |
EVP_CIPH_CBC_MODE); |
| 275 |
|
| 276 |
/** |
| 277 |
* The DES cipher type (Windows CNG provider) |
| 278 |
* |
| 279 |
* @return the DES-CBC EVP_CIPHER pointer. |
| 280 |
* |
| 281 |
* @ingroup hcrypto_evp |
| 282 |
*/ |
| 283 |
|
| 284 |
WINCNG_CIPHER_ALGORITHM(des_cbc, |
| 285 |
BCRYPT_DES_ALGORITHM, |
| 286 |
8, |
| 287 |
8, |
| 288 |
8, |
| 289 |
EVP_CIPH_CBC_MODE); |
| 290 |
|
| 291 |
/** |
| 292 |
* The AES-128 cipher type (Windows CNG provider) |
| 293 |
* |
| 294 |
* @return the AES-128-CBC EVP_CIPHER pointer. |
| 295 |
* |
| 296 |
* @ingroup hcrypto_evp |
| 297 |
*/ |
| 298 |
|
| 299 |
WINCNG_CIPHER_ALGORITHM(aes_128_cbc, |
| 300 |
BCRYPT_AES_ALGORITHM, |
| 301 |
16, |
| 302 |
16, |
| 303 |
16, |
| 304 |
EVP_CIPH_CBC_MODE); |
| 305 |
|
| 306 |
/** |
| 307 |
* The AES-192 cipher type (Windows CNG provider) |
| 308 |
* |
| 309 |
* @return the AES-192-CBC EVP_CIPHER pointer. |
| 310 |
* |
| 311 |
* @ingroup hcrypto_evp |
| 312 |
*/ |
| 313 |
|
| 314 |
WINCNG_CIPHER_ALGORITHM(aes_192_cbc, |
| 315 |
BCRYPT_AES_ALGORITHM, |
| 316 |
16, |
| 317 |
24, |
| 318 |
16, |
| 319 |
EVP_CIPH_CBC_MODE); |
| 320 |
|
| 321 |
/** |
| 322 |
* The AES-256 cipher type (Windows CNG provider) |
| 323 |
* |
| 324 |
* @return the AES-256-CBC EVP_CIPHER pointer. |
| 325 |
* |
| 326 |
* @ingroup hcrypto_evp |
| 327 |
*/ |
| 328 |
|
| 329 |
WINCNG_CIPHER_ALGORITHM(aes_256_cbc, |
| 330 |
BCRYPT_AES_ALGORITHM, |
| 331 |
16, |
| 332 |
32, |
| 333 |
16, |
| 334 |
EVP_CIPH_CBC_MODE); |
| 335 |
|
| 336 |
/** |
| 337 |
* The AES-128 CFB8 cipher type (Windows CNG provider) |
| 338 |
* |
| 339 |
* @return the AES-128-CFB8 EVP_CIPHER pointer. |
| 340 |
* |
| 341 |
* @ingroup hcrypto_evp |
| 342 |
*/ |
| 343 |
|
| 344 |
WINCNG_CIPHER_ALGORITHM(aes_128_cfb8, |
| 345 |
BCRYPT_AES_ALGORITHM, |
| 346 |
16, |
| 347 |
16, |
| 348 |
16, |
| 349 |
EVP_CIPH_CFB8_MODE); |
| 350 |
|
| 351 |
/** |
| 352 |
* The AES-192 CFB8 cipher type (Windows CNG provider) |
| 353 |
* |
| 354 |
* @return the AES-192-CFB8 EVP_CIPHER pointer. |
| 355 |
* |
| 356 |
* @ingroup hcrypto_evp |
| 357 |
*/ |
| 358 |
|
| 359 |
WINCNG_CIPHER_ALGORITHM(aes_192_cfb8, |
| 360 |
BCRYPT_AES_ALGORITHM, |
| 361 |
16, |
| 362 |
24, |
| 363 |
16, |
| 364 |
EVP_CIPH_CFB8_MODE); |
| 365 |
|
| 366 |
/** |
| 367 |
* The AES-256 CFB8 cipher type (Windows CNG provider) |
| 368 |
* |
| 369 |
* @return the AES-256-CFB8 EVP_CIPHER pointer. |
| 370 |
* |
| 371 |
* @ingroup hcrypto_evp |
| 372 |
*/ |
| 373 |
|
| 374 |
WINCNG_CIPHER_ALGORITHM(aes_256_cfb8, |
| 375 |
BCRYPT_AES_ALGORITHM, |
| 376 |
16, |
| 377 |
32, |
| 378 |
16, |
| 379 |
EVP_CIPH_CFB8_MODE); |
| 380 |
|
| 381 |
/** |
| 382 |
* The RC2 cipher type - Windows CNG |
| 383 |
* |
| 384 |
* @return the RC2 EVP_CIPHER pointer. |
| 385 |
* |
| 386 |
* @ingroup hcrypto_evp |
| 387 |
*/ |
| 388 |
|
| 389 |
WINCNG_CIPHER_ALGORITHM(rc2_cbc, |
| 390 |
BCRYPT_RC2_ALGORITHM, |
| 391 |
8, |
| 392 |
16, |
| 393 |
8, |
| 394 |
EVP_CIPH_CBC_MODE); |
| 395 |
|
| 396 |
/** |
| 397 |
* The RC2-40 cipher type - Windows CNG |
| 398 |
* |
| 399 |
* @return the RC2-40 EVP_CIPHER pointer. |
| 400 |
* |
| 401 |
* @ingroup hcrypto_evp |
| 402 |
*/ |
| 403 |
|
| 404 |
WINCNG_CIPHER_ALGORITHM(rc2_40_cbc, |
| 405 |
BCRYPT_RC2_ALGORITHM, |
| 406 |
8, |
| 407 |
5, |
| 408 |
8, |
| 409 |
EVP_CIPH_CBC_MODE); |
| 410 |
|
| 411 |
/** |
| 412 |
* The RC2-64 cipher type - Windows CNG |
| 413 |
* |
| 414 |
* @return the RC2-64 EVP_CIPHER pointer. |
| 415 |
* |
| 416 |
* @ingroup hcrypto_evp |
| 417 |
*/ |
| 418 |
|
| 419 |
WINCNG_CIPHER_ALGORITHM(rc2_64_cbc, |
| 420 |
BCRYPT_RC2_ALGORITHM, |
| 421 |
8, |
| 422 |
8, |
| 423 |
8, |
| 424 |
EVP_CIPH_CBC_MODE); |
| 425 |
|
| 426 |
/** |
| 427 |
* The Camellia-128 cipher type - CommonCrypto |
| 428 |
* |
| 429 |
* @return the Camellia-128 EVP_CIPHER pointer. |
| 430 |
* |
| 431 |
* @ingroup hcrypto_evp |
| 432 |
*/ |
| 433 |
|
| 434 |
WINCNG_CIPHER_ALGORITHM_UNAVAILABLE(camellia_128_cbc); |
| 435 |
|
| 436 |
/** |
| 437 |
* The Camellia-198 cipher type - CommonCrypto |
| 438 |
* |
| 439 |
* @return the Camellia-198 EVP_CIPHER pointer. |
| 440 |
* |
| 441 |
* @ingroup hcrypto_evp |
| 442 |
*/ |
| 443 |
|
| 444 |
WINCNG_CIPHER_ALGORITHM_UNAVAILABLE(camellia_192_cbc); |
| 445 |
|
| 446 |
/** |
| 447 |
* The Camellia-256 cipher type - CommonCrypto |
| 448 |
* |
| 449 |
* @return the Camellia-256 EVP_CIPHER pointer. |
| 450 |
* |
| 451 |
* @ingroup hcrypto_evp |
| 452 |
*/ |
| 453 |
|
| 454 |
WINCNG_CIPHER_ALGORITHM_UNAVAILABLE(camellia_256_cbc); |
| 455 |
|
| 456 |
/** |
| 457 |
* The RC4 cipher type (Windows CNG provider) |
| 458 |
* |
| 459 |
* @return the RC4 EVP_CIPHER pointer. |
| 460 |
* |
| 461 |
* @ingroup hcrypto_evp |
| 462 |
*/ |
| 463 |
|
| 464 |
WINCNG_CIPHER_ALGORITHM(rc4, |
| 465 |
BCRYPT_RC4_ALGORITHM, |
| 466 |
1, |
| 467 |
16, |
| 468 |
0, |
| 469 |
EVP_CIPH_STREAM_CIPHER | EVP_CIPH_VARIABLE_LENGTH); |
| 470 |
|
| 471 |
/** |
| 472 |
* The RC4-40 cipher type (Windows CNG provider) |
| 473 |
* |
| 474 |
* @return the RC4 EVP_CIPHER pointer. |
| 475 |
* |
| 476 |
* @ingroup hcrypto_evp |
| 477 |
*/ |
| 478 |
|
| 479 |
WINCNG_CIPHER_ALGORITHM(rc4_40, |
| 480 |
BCRYPT_RC4_ALGORITHM, |
| 481 |
1, |
| 482 |
5, |
| 483 |
0, |
| 484 |
EVP_CIPH_STREAM_CIPHER | EVP_CIPH_VARIABLE_LENGTH); |
| 485 |
|
| 486 |
static void |
| 487 |
wincng_cipher_algorithm_cleanup(void) |
| 488 |
{ |
| 489 |
WINCNG_CIPHER_ALGORITHM_CLEANUP(des_ede3_cbc); |
| 490 |
WINCNG_CIPHER_ALGORITHM_CLEANUP(des_cbc); |
| 491 |
WINCNG_CIPHER_ALGORITHM_CLEANUP(aes_128_cbc); |
| 492 |
WINCNG_CIPHER_ALGORITHM_CLEANUP(aes_192_cbc); |
| 493 |
WINCNG_CIPHER_ALGORITHM_CLEANUP(aes_256_cbc); |
| 494 |
WINCNG_CIPHER_ALGORITHM_CLEANUP(aes_128_cfb8); |
| 495 |
WINCNG_CIPHER_ALGORITHM_CLEANUP(aes_192_cfb8); |
| 496 |
WINCNG_CIPHER_ALGORITHM_CLEANUP(aes_256_cfb8); |
| 497 |
WINCNG_CIPHER_ALGORITHM_CLEANUP(rc2_cbc); |
| 498 |
WINCNG_CIPHER_ALGORITHM_CLEANUP(rc2_40_cbc); |
| 499 |
WINCNG_CIPHER_ALGORITHM_CLEANUP(rc2_64_cbc); |
| 500 |
WINCNG_CIPHER_ALGORITHM_CLEANUP(rc4); |
| 501 |
WINCNG_CIPHER_ALGORITHM_CLEANUP(rc4_40); |
| 502 |
} |
| 503 |
|
| 504 |
/* |
| 505 |
* CNG digest provider |
| 506 |
*/ |
| 507 |
|
| 508 |
struct wincng_md_ctx { |
| 509 |
BCRYPT_HASH_HANDLE hHash; |
| 510 |
ULONG cbHashObject; |
| 511 |
UCHAR rgbHashObject[1]; |
| 512 |
}; |
| 513 |
|
| 514 |
static BCRYPT_ALG_HANDLE |
| 515 |
wincng_md_algorithm_init(EVP_MD *md, |
| 516 |
LPCWSTR pszAlgId) |
| 517 |
{ |
| 518 |
BCRYPT_ALG_HANDLE hAlgorithm; |
| 519 |
NTSTATUS status; |
| 520 |
ULONG cbHashObject, cbData; |
| 521 |
ULONG cbHash = 0, cbBlock = 0; |
| 522 |
|
| 523 |
status = BCryptOpenAlgorithmProvider(&hAlgorithm, |
| 524 |
pszAlgId, |
| 525 |
NULL, |
| 526 |
0); |
| 527 |
if (!BCRYPT_SUCCESS(status)) |
| 528 |
return NULL; |
| 529 |
|
| 530 |
status = BCryptGetProperty(hAlgorithm, |
| 531 |
BCRYPT_HASH_LENGTH, |
| 532 |
(PUCHAR)&cbHash, |
| 533 |
sizeof(ULONG), |
| 534 |
&cbData, |
| 535 |
0); |
| 536 |
if (!BCRYPT_SUCCESS(status)) { |
| 537 |
BCryptCloseAlgorithmProvider(hAlgorithm, 0); |
| 538 |
return NULL; |
| 539 |
} |
| 540 |
|
| 541 |
status = BCryptGetProperty(hAlgorithm, |
| 542 |
BCRYPT_HASH_BLOCK_LENGTH, |
| 543 |
(PUCHAR)&cbBlock, |
| 544 |
sizeof(ULONG), |
| 545 |
&cbData, |
| 546 |
0); |
| 547 |
if (!BCRYPT_SUCCESS(status)) { |
| 548 |
BCryptCloseAlgorithmProvider(hAlgorithm, 0); |
| 549 |
return NULL; |
| 550 |
} |
| 551 |
|
| 552 |
status = BCryptGetProperty(hAlgorithm, |
| 553 |
BCRYPT_OBJECT_LENGTH, |
| 554 |
(PUCHAR)&cbHashObject, |
| 555 |
sizeof(ULONG), |
| 556 |
&cbData, |
| 557 |
0); |
| 558 |
if (!BCRYPT_SUCCESS(status)) { |
| 559 |
BCryptCloseAlgorithmProvider(hAlgorithm, 0); |
| 560 |
return NULL; |
| 561 |
} |
| 562 |
|
| 563 |
md->hash_size = cbHash; |
| 564 |
md->block_size = cbBlock; |
| 565 |
md->ctx_size = sizeof(struct wincng_md_ctx) + cbHashObject - 1; |
| 566 |
|
| 567 |
return hAlgorithm; |
| 568 |
} |
| 569 |
|
| 570 |
static int |
| 571 |
wincng_md_hash_init(BCRYPT_ALG_HANDLE hAlgorithm, |
| 572 |
EVP_MD_CTX *ctx) |
| 573 |
{ |
| 574 |
struct wincng_md_ctx *cng = (struct wincng_md_ctx *)ctx; |
| 575 |
NTSTATUS status; |
| 576 |
ULONG cbData; |
| 577 |
|
| 578 |
if (cng->hHash) { |
| 579 |
BCryptDestroyHash(cng->hHash); /* allow reinitialization */ |
| 580 |
cng->hHash = (BCRYPT_HASH_HANDLE)0; |
| 581 |
} |
| 582 |
|
| 583 |
status = BCryptGetProperty(hAlgorithm, |
| 584 |
BCRYPT_OBJECT_LENGTH, |
| 585 |
(PUCHAR)&cng->cbHashObject, |
| 586 |
sizeof(ULONG), |
| 587 |
&cbData, |
| 588 |
0); |
| 589 |
if (!BCRYPT_SUCCESS(status)) |
| 590 |
return 0; |
| 591 |
|
| 592 |
status = BCryptCreateHash(hAlgorithm, |
| 593 |
&cng->hHash, |
| 594 |
cng->rgbHashObject, |
| 595 |
cng->cbHashObject, |
| 596 |
NULL, |
| 597 |
0, |
| 598 |
0); |
| 599 |
|
| 600 |
return BCRYPT_SUCCESS(status); |
| 601 |
} |
| 602 |
|
| 603 |
static int |
| 604 |
wincng_md_update(EVP_MD_CTX *ctx, |
| 605 |
const void *data, |
| 606 |
size_t length) |
| 607 |
{ |
| 608 |
struct wincng_md_ctx *cng = (struct wincng_md_ctx *)ctx; |
| 609 |
NTSTATUS status; |
| 610 |
|
| 611 |
status = BCryptHashData(cng->hHash, (PUCHAR)data, length, 0); |
| 612 |
|
| 613 |
return BCRYPT_SUCCESS(status); |
| 614 |
} |
| 615 |
|
| 616 |
static int |
| 617 |
wincng_md_final(void *digest, |
| 618 |
EVP_MD_CTX *ctx) |
| 619 |
{ |
| 620 |
struct wincng_md_ctx *cng = (struct wincng_md_ctx *)ctx; |
| 621 |
NTSTATUS status; |
| 622 |
ULONG cbHash, cbData; |
| 623 |
|
| 624 |
status = BCryptGetProperty(cng->hHash, |
| 625 |
BCRYPT_HASH_LENGTH, |
| 626 |
(PUCHAR)&cbHash, |
| 627 |
sizeof(DWORD), |
| 628 |
&cbData, |
| 629 |
0); |
| 630 |
if (!BCRYPT_SUCCESS(status)) |
| 631 |
return 0; |
| 632 |
|
| 633 |
status = BCryptFinishHash(cng->hHash, |
| 634 |
digest, |
| 635 |
cbHash, |
| 636 |
0); |
| 637 |
|
| 638 |
return BCRYPT_SUCCESS(status); |
| 639 |
} |
| 640 |
|
| 641 |
static int |
| 642 |
wincng_md_cleanup(EVP_MD_CTX *ctx) |
| 643 |
{ |
| 644 |
struct wincng_md_ctx *cng = (struct wincng_md_ctx *)ctx; |
| 645 |
|
| 646 |
if (cng->hHash) |
| 647 |
BCryptDestroyHash(cng->hHash); |
| 648 |
SecureZeroMemory(cng->rgbHashObject, cng->cbHashObject); |
| 649 |
|
| 650 |
return 1; |
| 651 |
} |
| 652 |
|
| 653 |
#define WINCNG_MD_ALGORITHM(name, alg_id) \ |
| 654 |
\ |
| 655 |
static BCRYPT_ALG_HANDLE wincng_hAlgorithm_##name; \ |
| 656 |
\ |
| 657 |
static int wincng_##name##_init(EVP_MD_CTX *ctx) \ |
| 658 |
{ \ |
| 659 |
return wincng_md_hash_init(wincng_hAlgorithm_##name, ctx); \ |
| 660 |
} \ |
| 661 |
\ |
| 662 |
const EVP_MD * \ |
| 663 |
hc_EVP_wincng_##name(void) \ |
| 664 |
{ \ |
| 665 |
static struct hc_evp_md name = { \ |
| 666 |
0, \ |
| 667 |
0, \ |
| 668 |
0, \ |
| 669 |
wincng_##name##_init, \ |
| 670 |
wincng_md_update, \ |
| 671 |
wincng_md_final, \ |
| 672 |
wincng_md_cleanup \ |
| 673 |
}; \ |
| 674 |
\ |
| 675 |
if (wincng_hAlgorithm_##name == NULL) { \ |
| 676 |
BCRYPT_ALG_HANDLE hAlgorithm = \ |
| 677 |
wincng_md_algorithm_init(&name, alg_id); \ |
| 678 |
InterlockedCompareExchangePointerRelease( \ |
| 679 |
&wincng_hAlgorithm_##name, hAlgorithm, NULL); \ |
| 680 |
} \ |
| 681 |
return wincng_hAlgorithm_##name ? &name : NULL; \ |
| 682 |
} |
| 683 |
|
| 684 |
#define WINCNG_MD_ALGORITHM_CLEANUP(name) do { \ |
| 685 |
if (wincng_hAlgorithm_##name) { \ |
| 686 |
BCryptCloseAlgorithmProvider(wincng_hAlgorithm_##name, 0); \ |
| 687 |
wincng_hAlgorithm_##name = NULL; \ |
| 688 |
} \ |
| 689 |
} while (0) |
| 690 |
|
| 691 |
WINCNG_MD_ALGORITHM(md4, BCRYPT_MD4_ALGORITHM); |
| 692 |
WINCNG_MD_ALGORITHM(md5, BCRYPT_MD5_ALGORITHM); |
| 693 |
WINCNG_MD_ALGORITHM(sha1, BCRYPT_SHA1_ALGORITHM); |
| 694 |
WINCNG_MD_ALGORITHM(sha256, BCRYPT_SHA256_ALGORITHM); |
| 695 |
WINCNG_MD_ALGORITHM(sha384, BCRYPT_SHA384_ALGORITHM); |
| 696 |
WINCNG_MD_ALGORITHM(sha512, BCRYPT_SHA512_ALGORITHM); |
| 697 |
|
| 698 |
static void |
| 699 |
wincng_md_algorithm_cleanup(void) |
| 700 |
{ |
| 701 |
WINCNG_MD_ALGORITHM_CLEANUP(md4); |
| 702 |
WINCNG_MD_ALGORITHM_CLEANUP(md5); |
| 703 |
WINCNG_MD_ALGORITHM_CLEANUP(sha1); |
| 704 |
WINCNG_MD_ALGORITHM_CLEANUP(sha256); |
| 705 |
WINCNG_MD_ALGORITHM_CLEANUP(sha384); |
| 706 |
WINCNG_MD_ALGORITHM_CLEANUP(sha512); |
| 707 |
} |
| 708 |
|
| 709 |
void _hc_wincng_cleanup(void) |
| 710 |
{ |
| 711 |
wincng_md_algorithm_cleanup(); |
| 712 |
wincng_cipher_algorithm_cleanup(); |
| 713 |
} |