2 * Crypto wrapper for Microsoft CryptoAPI
3 * Copyright (c) 2005-2009, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
22 #ifndef MS_ENH_RSA_AES_PROV
24 #define MS_ENH_RSA_AES_PROV \
25 L"Microsoft Enhanced RSA and AES Cryptographic Provider (Prototype)"
27 #define MS_ENH_RSA_AES_PROV \
28 "Microsoft Enhanced RSA and AES Cryptographic Provider (Prototype)"
30 #endif /* MS_ENH_RSA_AES_PROV */
33 #define CALG_HMAC (ALG_CLASS_HASH | ALG_TYPE_ANY | ALG_SID_HMAC)
36 #ifdef __MINGW32_VERSION
38 * MinGW does not yet include all the needed definitions for CryptoAPI, so
39 * define here whatever extra is needed.
43 (*CryptImportPublicKeyInfo)(HCRYPTPROV hCryptProv, DWORD dwCertEncodingType,
44 PCERT_PUBLIC_KEY_INFO pInfo, HCRYPTKEY *phKey)
45 = NULL; /* to be loaded from crypt32.dll */
48 static int mingw_load_crypto_func(void)
52 /* MinGW does not yet have full CryptoAPI support, so load the needed
55 if (CryptImportPublicKeyInfo)
58 dll = LoadLibrary("crypt32");
60 wpa_printf(MSG_DEBUG, "CryptoAPI: Could not load crypt32 "
65 CryptImportPublicKeyInfo = GetProcAddress(
66 dll, "CryptImportPublicKeyInfo");
67 if (CryptImportPublicKeyInfo == NULL) {
68 wpa_printf(MSG_DEBUG, "CryptoAPI: Could not get "
69 "CryptImportPublicKeyInfo() address from "
77 #else /* __MINGW32_VERSION */
79 static int mingw_load_crypto_func(void)
84 #endif /* __MINGW32_VERSION */
87 static void cryptoapi_report_error(const char *msg)
90 DWORD err = GetLastError();
92 if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
93 FORMAT_MESSAGE_FROM_SYSTEM,
94 NULL, err, 0, (LPTSTR) &s, 0, NULL) == 0) {
95 wpa_printf(MSG_DEBUG, "CryptoAPI: %s: %d", msg, (int) err);
100 if (*pos == '\n' || *pos == '\r') {
107 wpa_printf(MSG_DEBUG, "CryptoAPI: %s: %d: (%s)", msg, (int) err, s);
112 int cryptoapi_hash_vector(ALG_ID alg, size_t hash_len, size_t num_elem,
113 const u8 *addr[], const size_t *len, u8 *mac)
121 if (!CryptAcquireContext(&prov, NULL, NULL, PROV_RSA_FULL, 0)) {
122 cryptoapi_report_error("CryptAcquireContext");
126 if (!CryptCreateHash(prov, alg, 0, 0, &hash)) {
127 cryptoapi_report_error("CryptCreateHash");
128 CryptReleaseContext(prov, 0);
132 for (i = 0; i < num_elem; i++) {
133 if (!CryptHashData(hash, (BYTE *) addr[i], len[i], 0)) {
134 cryptoapi_report_error("CryptHashData");
135 CryptDestroyHash(hash);
136 CryptReleaseContext(prov, 0);
141 if (!CryptGetHashParam(hash, HP_HASHVAL, mac, &hlen, 0)) {
142 cryptoapi_report_error("CryptGetHashParam");
146 CryptDestroyHash(hash);
147 CryptReleaseContext(prov, 0);
153 int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
155 return cryptoapi_hash_vector(CALG_MD4, 16, num_elem, addr, len, mac);
159 void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
171 DWORD mode = CRYPT_MODE_ECB;
173 key_blob.hdr.bType = PLAINTEXTKEYBLOB;
174 key_blob.hdr.bVersion = CUR_BLOB_VERSION;
175 key_blob.hdr.reserved = 0;
176 key_blob.hdr.aiKeyAlg = CALG_DES;
179 /* Add parity bits to the key */
181 for (i = 0; i < 7; i++) {
183 key_blob.key[i] = (tmp >> i) | next | 1;
184 next = tmp << (7 - i);
186 key_blob.key[i] = next | 1;
188 if (!CryptAcquireContext(&prov, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL,
189 CRYPT_VERIFYCONTEXT)) {
190 wpa_printf(MSG_DEBUG, "CryptoAPI: CryptAcquireContext failed: "
191 "%d", (int) GetLastError());
195 if (!CryptImportKey(prov, (BYTE *) &key_blob, sizeof(key_blob), 0, 0,
197 wpa_printf(MSG_DEBUG, "CryptoAPI: CryptImportKey failed: %d",
198 (int) GetLastError());
199 CryptReleaseContext(prov, 0);
203 if (!CryptSetKeyParam(ckey, KP_MODE, (BYTE *) &mode, 0)) {
204 wpa_printf(MSG_DEBUG, "CryptoAPI: CryptSetKeyParam(KP_MODE) "
205 "failed: %d", (int) GetLastError());
206 CryptDestroyKey(ckey);
207 CryptReleaseContext(prov, 0);
211 os_memcpy(cypher, clear, 8);
213 if (!CryptEncrypt(ckey, 0, FALSE, 0, cypher, &dlen, 8)) {
214 wpa_printf(MSG_DEBUG, "CryptoAPI: CryptEncrypt failed: %d",
215 (int) GetLastError());
216 os_memset(cypher, 0, 8);
219 CryptDestroyKey(ckey);
220 CryptReleaseContext(prov, 0);
224 int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
226 return cryptoapi_hash_vector(CALG_MD5, 16, num_elem, addr, len, mac);
230 int sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
232 return cryptoapi_hash_vector(CALG_SHA, 20, num_elem, addr, len, mac);
242 void * aes_encrypt_init(const u8 *key, size_t len)
244 struct aes_context *akey;
250 DWORD mode = CRYPT_MODE_ECB;
255 key_blob.hdr.bType = PLAINTEXTKEYBLOB;
256 key_blob.hdr.bVersion = CUR_BLOB_VERSION;
257 key_blob.hdr.reserved = 0;
258 key_blob.hdr.aiKeyAlg = CALG_AES_128;
260 os_memcpy(key_blob.key, key, len);
262 akey = os_zalloc(sizeof(*akey));
266 if (!CryptAcquireContext(&akey->prov, NULL,
267 MS_ENH_RSA_AES_PROV, PROV_RSA_AES,
268 CRYPT_VERIFYCONTEXT)) {
269 wpa_printf(MSG_DEBUG, "CryptoAPI: CryptAcquireContext failed: "
270 "%d", (int) GetLastError());
275 if (!CryptImportKey(akey->prov, (BYTE *) &key_blob, sizeof(key_blob),
276 0, 0, &akey->ckey)) {
277 wpa_printf(MSG_DEBUG, "CryptoAPI: CryptImportKey failed: %d",
278 (int) GetLastError());
279 CryptReleaseContext(akey->prov, 0);
284 if (!CryptSetKeyParam(akey->ckey, KP_MODE, (BYTE *) &mode, 0)) {
285 wpa_printf(MSG_DEBUG, "CryptoAPI: CryptSetKeyParam(KP_MODE) "
286 "failed: %d", (int) GetLastError());
287 CryptDestroyKey(akey->ckey);
288 CryptReleaseContext(akey->prov, 0);
297 void aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
299 struct aes_context *akey = ctx;
302 os_memcpy(crypt, plain, 16);
304 if (!CryptEncrypt(akey->ckey, 0, FALSE, 0, crypt, &dlen, 16)) {
305 wpa_printf(MSG_DEBUG, "CryptoAPI: CryptEncrypt failed: %d",
306 (int) GetLastError());
307 os_memset(crypt, 0, 16);
312 void aes_encrypt_deinit(void *ctx)
314 struct aes_context *akey = ctx;
316 CryptDestroyKey(akey->ckey);
317 CryptReleaseContext(akey->prov, 0);
323 void * aes_decrypt_init(const u8 *key, size_t len)
325 return aes_encrypt_init(key, len);
329 void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
331 struct aes_context *akey = ctx;
334 os_memcpy(plain, crypt, 16);
337 if (!CryptDecrypt(akey->ckey, 0, FALSE, 0, plain, &dlen)) {
338 wpa_printf(MSG_DEBUG, "CryptoAPI: CryptDecrypt failed: %d",
339 (int) GetLastError());
344 void aes_decrypt_deinit(void *ctx)
346 aes_encrypt_deinit(ctx);
351 enum crypto_hash_alg alg;
358 struct crypto_hash * crypto_hash_init(enum crypto_hash_alg alg, const u8 *key,
361 struct crypto_hash *ctx;
369 os_memset(&key_blob, 0, sizeof(key_blob));
371 case CRYPTO_HASH_ALG_MD5:
374 case CRYPTO_HASH_ALG_SHA1:
377 case CRYPTO_HASH_ALG_HMAC_MD5:
378 case CRYPTO_HASH_ALG_HMAC_SHA1:
380 key_blob.hdr.bType = PLAINTEXTKEYBLOB;
381 key_blob.hdr.bVersion = CUR_BLOB_VERSION;
382 key_blob.hdr.reserved = 0;
384 * Note: RC2 is not really used, but that can be used to
385 * import HMAC keys of up to 16 byte long.
386 * CRYPT_IPSEC_HMAC_KEY flag for CryptImportKey() is needed to
387 * be able to import longer keys (HMAC-SHA1 uses 20-byte key).
389 key_blob.hdr.aiKeyAlg = CALG_RC2;
390 key_blob.len = key_len;
391 if (key_len > sizeof(key_blob.key))
393 os_memcpy(key_blob.key, key, key_len);
399 ctx = os_zalloc(sizeof(*ctx));
405 if (!CryptAcquireContext(&ctx->prov, NULL, NULL, PROV_RSA_FULL, 0)) {
406 cryptoapi_report_error("CryptAcquireContext");
411 if (calg == CALG_HMAC) {
412 #ifndef CRYPT_IPSEC_HMAC_KEY
413 #define CRYPT_IPSEC_HMAC_KEY 0x00000100
415 if (!CryptImportKey(ctx->prov, (BYTE *) &key_blob,
416 sizeof(key_blob), 0, CRYPT_IPSEC_HMAC_KEY,
418 cryptoapi_report_error("CryptImportKey");
419 CryptReleaseContext(ctx->prov, 0);
425 if (!CryptCreateHash(ctx->prov, calg, ctx->key, 0, &ctx->hash)) {
426 cryptoapi_report_error("CryptCreateHash");
427 CryptReleaseContext(ctx->prov, 0);
432 if (calg == CALG_HMAC) {
434 os_memset(&info, 0, sizeof(info));
436 case CRYPTO_HASH_ALG_HMAC_MD5:
437 info.HashAlgid = CALG_MD5;
439 case CRYPTO_HASH_ALG_HMAC_SHA1:
440 info.HashAlgid = CALG_SHA;
447 if (!CryptSetHashParam(ctx->hash, HP_HMAC_INFO, (BYTE *) &info,
449 cryptoapi_report_error("CryptSetHashParam");
450 CryptDestroyHash(ctx->hash);
451 CryptReleaseContext(ctx->prov, 0);
461 void crypto_hash_update(struct crypto_hash *ctx, const u8 *data, size_t len)
463 if (ctx == NULL || ctx->error)
466 if (!CryptHashData(ctx->hash, (BYTE *) data, len, 0)) {
467 cryptoapi_report_error("CryptHashData");
473 int crypto_hash_finish(struct crypto_hash *ctx, u8 *mac, size_t *len)
481 if (mac == NULL || len == NULL)
490 if (!CryptGetHashParam(ctx->hash, HP_HASHVAL, mac, &hlen, 0)) {
491 cryptoapi_report_error("CryptGetHashParam");
497 if (ctx->alg == CRYPTO_HASH_ALG_HMAC_SHA1 ||
498 ctx->alg == CRYPTO_HASH_ALG_HMAC_MD5)
499 CryptDestroyKey(ctx->key);
507 struct crypto_cipher {
513 struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
514 const u8 *iv, const u8 *key,
517 struct crypto_cipher *ctx;
523 DWORD mode = CRYPT_MODE_CBC;
525 key_blob.hdr.bType = PLAINTEXTKEYBLOB;
526 key_blob.hdr.bVersion = CUR_BLOB_VERSION;
527 key_blob.hdr.reserved = 0;
528 key_blob.len = key_len;
529 if (key_len > sizeof(key_blob.key))
531 os_memcpy(key_blob.key, key, key_len);
534 case CRYPTO_CIPHER_ALG_AES:
536 key_blob.hdr.aiKeyAlg = CALG_AES_256;
537 else if (key_len == 24)
538 key_blob.hdr.aiKeyAlg = CALG_AES_192;
540 key_blob.hdr.aiKeyAlg = CALG_AES_128;
542 case CRYPTO_CIPHER_ALG_3DES:
543 key_blob.hdr.aiKeyAlg = CALG_3DES;
545 case CRYPTO_CIPHER_ALG_DES:
546 key_blob.hdr.aiKeyAlg = CALG_DES;
548 case CRYPTO_CIPHER_ALG_RC2:
549 key_blob.hdr.aiKeyAlg = CALG_RC2;
551 case CRYPTO_CIPHER_ALG_RC4:
552 key_blob.hdr.aiKeyAlg = CALG_RC4;
558 ctx = os_zalloc(sizeof(*ctx));
562 if (!CryptAcquireContext(&ctx->prov, NULL, MS_ENH_RSA_AES_PROV,
563 PROV_RSA_AES, CRYPT_VERIFYCONTEXT)) {
564 cryptoapi_report_error("CryptAcquireContext");
568 if (!CryptImportKey(ctx->prov, (BYTE *) &key_blob,
569 sizeof(key_blob), 0, 0, &ctx->key)) {
570 cryptoapi_report_error("CryptImportKey");
574 if (!CryptSetKeyParam(ctx->key, KP_MODE, (BYTE *) &mode, 0)) {
575 cryptoapi_report_error("CryptSetKeyParam(KP_MODE)");
579 if (iv && !CryptSetKeyParam(ctx->key, KP_IV, (BYTE *) iv, 0)) {
580 cryptoapi_report_error("CryptSetKeyParam(KP_IV)");
587 CryptDestroyKey(ctx->key);
589 CryptReleaseContext(ctx->prov, 0);
596 int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain,
597 u8 *crypt, size_t len)
601 os_memcpy(crypt, plain, len);
603 if (!CryptEncrypt(ctx->key, 0, FALSE, 0, crypt, &dlen, len)) {
604 cryptoapi_report_error("CryptEncrypt");
605 os_memset(crypt, 0, len);
613 int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt,
614 u8 *plain, size_t len)
618 os_memcpy(plain, crypt, len);
620 if (!CryptDecrypt(ctx->key, 0, FALSE, 0, plain, &dlen)) {
621 cryptoapi_report_error("CryptDecrypt");
629 void crypto_cipher_deinit(struct crypto_cipher *ctx)
631 CryptDestroyKey(ctx->key);
632 CryptReleaseContext(ctx->prov, 0);
637 struct crypto_public_key {
642 struct crypto_private_key {
648 struct crypto_public_key * crypto_public_key_import(const u8 *key, size_t len)
650 /* Use crypto_public_key_from_cert() instead. */
655 struct crypto_private_key * crypto_private_key_import(const u8 *key,
664 struct crypto_public_key * crypto_public_key_from_cert(const u8 *buf,
667 struct crypto_public_key *pk;
670 pk = os_zalloc(sizeof(*pk));
674 cc = CertCreateCertificateContext(X509_ASN_ENCODING |
675 PKCS_7_ASN_ENCODING, buf, len);
677 cryptoapi_report_error("CryptCreateCertificateContext");
682 if (!CryptAcquireContext(&pk->prov, NULL, MS_DEF_PROV, PROV_RSA_FULL,
684 cryptoapi_report_error("CryptAcquireContext");
686 CertFreeCertificateContext(cc);
690 if (!CryptImportPublicKeyInfo(pk->prov, X509_ASN_ENCODING |
692 &cc->pCertInfo->SubjectPublicKeyInfo,
694 cryptoapi_report_error("CryptImportPublicKeyInfo");
695 CryptReleaseContext(pk->prov, 0);
697 CertFreeCertificateContext(cc);
701 CertFreeCertificateContext(cc);
707 int crypto_public_key_encrypt_pkcs1_v15(struct crypto_public_key *key,
708 const u8 *in, size_t inlen,
709 u8 *out, size_t *outlen)
717 tmp = malloc(*outlen);
721 os_memcpy(tmp, in, inlen);
723 if (!CryptEncrypt(key->rsa, 0, TRUE, 0, tmp, &clen, *outlen)) {
724 wpa_printf(MSG_DEBUG, "CryptoAPI: Failed to encrypt using "
725 "public key: %d", (int) GetLastError());
732 /* Reverse the output */
733 for (i = 0; i < *outlen; i++)
734 out[i] = tmp[*outlen - 1 - i];
742 int crypto_private_key_sign_pkcs1(struct crypto_private_key *key,
743 const u8 *in, size_t inlen,
744 u8 *out, size_t *outlen)
751 void crypto_public_key_free(struct crypto_public_key *key)
754 CryptDestroyKey(key->rsa);
755 CryptReleaseContext(key->prov, 0);
761 void crypto_private_key_free(struct crypto_private_key *key)
764 CryptDestroyKey(key->rsa);
765 CryptReleaseContext(key->prov, 0);
771 int crypto_global_init(void)
773 return mingw_load_crypto_func();
777 void crypto_global_deinit(void)
782 int crypto_mod_exp(const u8 *base, size_t base_len,
783 const u8 *power, size_t power_len,
784 const u8 *modulus, size_t modulus_len,
785 u8 *result, size_t *result_len)