nl80211: Fix a typo
[hostap-gosc2009.git] / src / crypto / crypto_cryptoapi.c
blob2a8d2001b173287b5dbc6c287e72c2f890828ac9
1 /*
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
10 * license.
12 * See README and COPYING for more details.
15 #include "includes.h"
16 #include <windows.h>
17 #include <wincrypt.h>
19 #include "common.h"
20 #include "crypto.h"
22 #ifndef MS_ENH_RSA_AES_PROV
23 #ifdef UNICODE
24 #define MS_ENH_RSA_AES_PROV \
25 L"Microsoft Enhanced RSA and AES Cryptographic Provider (Prototype)"
26 #else
27 #define MS_ENH_RSA_AES_PROV \
28 "Microsoft Enhanced RSA and AES Cryptographic Provider (Prototype)"
29 #endif
30 #endif /* MS_ENH_RSA_AES_PROV */
32 #ifndef CALG_HMAC
33 #define CALG_HMAC (ALG_CLASS_HASH | ALG_TYPE_ANY | ALG_SID_HMAC)
34 #endif
36 #ifdef __MINGW32_VERSION
38 * MinGW does not yet include all the needed definitions for CryptoAPI, so
39 * define here whatever extra is needed.
42 static BOOL WINAPI
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)
50 HINSTANCE dll;
52 /* MinGW does not yet have full CryptoAPI support, so load the needed
53 * function here. */
55 if (CryptImportPublicKeyInfo)
56 return 0;
58 dll = LoadLibrary("crypt32");
59 if (dll == NULL) {
60 wpa_printf(MSG_DEBUG, "CryptoAPI: Could not load crypt32 "
61 "library");
62 return -1;
65 CryptImportPublicKeyInfo = GetProcAddress(
66 dll, "CryptImportPublicKeyInfo");
67 if (CryptImportPublicKeyInfo == NULL) {
68 wpa_printf(MSG_DEBUG, "CryptoAPI: Could not get "
69 "CryptImportPublicKeyInfo() address from "
70 "crypt32 library");
71 return -1;
74 return 0;
77 #else /* __MINGW32_VERSION */
79 static int mingw_load_crypto_func(void)
81 return 0;
84 #endif /* __MINGW32_VERSION */
87 static void cryptoapi_report_error(const char *msg)
89 char *s, *pos;
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);
98 pos = s;
99 while (*pos) {
100 if (*pos == '\n' || *pos == '\r') {
101 *pos = '\0';
102 break;
104 pos++;
107 wpa_printf(MSG_DEBUG, "CryptoAPI: %s: %d: (%s)", msg, (int) err, s);
108 LocalFree(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)
115 HCRYPTPROV prov;
116 HCRYPTHASH hash;
117 size_t i;
118 DWORD hlen;
119 int ret = 0;
121 if (!CryptAcquireContext(&prov, NULL, NULL, PROV_RSA_FULL, 0)) {
122 cryptoapi_report_error("CryptAcquireContext");
123 return -1;
126 if (!CryptCreateHash(prov, alg, 0, 0, &hash)) {
127 cryptoapi_report_error("CryptCreateHash");
128 CryptReleaseContext(prov, 0);
129 return -1;
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);
140 hlen = hash_len;
141 if (!CryptGetHashParam(hash, HP_HASHVAL, mac, &hlen, 0)) {
142 cryptoapi_report_error("CryptGetHashParam");
143 ret = -1;
146 CryptDestroyHash(hash);
147 CryptReleaseContext(prov, 0);
149 return ret;
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)
161 u8 next, tmp;
162 int i;
163 HCRYPTPROV prov;
164 HCRYPTKEY ckey;
165 DWORD dlen;
166 struct {
167 BLOBHEADER hdr;
168 DWORD len;
169 BYTE key[8];
170 } key_blob;
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;
177 key_blob.len = 8;
179 /* Add parity bits to the key */
180 next = 0;
181 for (i = 0; i < 7; i++) {
182 tmp = key[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());
192 return;
195 if (!CryptImportKey(prov, (BYTE *) &key_blob, sizeof(key_blob), 0, 0,
196 &ckey)) {
197 wpa_printf(MSG_DEBUG, "CryptoAPI: CryptImportKey failed: %d",
198 (int) GetLastError());
199 CryptReleaseContext(prov, 0);
200 return;
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);
208 return;
211 os_memcpy(cypher, clear, 8);
212 dlen = 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);
236 struct aes_context {
237 HCRYPTPROV prov;
238 HCRYPTKEY ckey;
242 void * aes_encrypt_init(const u8 *key, size_t len)
244 struct aes_context *akey;
245 struct {
246 BLOBHEADER hdr;
247 DWORD len;
248 BYTE key[16];
249 } key_blob;
250 DWORD mode = CRYPT_MODE_ECB;
252 if (len != 16)
253 return NULL;
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;
259 key_blob.len = len;
260 os_memcpy(key_blob.key, key, len);
262 akey = os_zalloc(sizeof(*akey));
263 if (akey == NULL)
264 return NULL;
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());
271 os_free(akey);
272 return NULL;
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);
280 os_free(akey);
281 return NULL;
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);
289 os_free(akey);
290 return NULL;
293 return akey;
297 void aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
299 struct aes_context *akey = ctx;
300 DWORD dlen;
302 os_memcpy(crypt, plain, 16);
303 dlen = 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;
315 if (akey) {
316 CryptDestroyKey(akey->ckey);
317 CryptReleaseContext(akey->prov, 0);
318 os_free(akey);
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;
332 DWORD dlen;
334 os_memcpy(plain, crypt, 16);
335 dlen = 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);
350 struct crypto_hash {
351 enum crypto_hash_alg alg;
352 int error;
353 HCRYPTPROV prov;
354 HCRYPTHASH hash;
355 HCRYPTKEY key;
358 struct crypto_hash * crypto_hash_init(enum crypto_hash_alg alg, const u8 *key,
359 size_t key_len)
361 struct crypto_hash *ctx;
362 ALG_ID calg;
363 struct {
364 BLOBHEADER hdr;
365 DWORD len;
366 BYTE key[32];
367 } key_blob;
369 os_memset(&key_blob, 0, sizeof(key_blob));
370 switch (alg) {
371 case CRYPTO_HASH_ALG_MD5:
372 calg = CALG_MD5;
373 break;
374 case CRYPTO_HASH_ALG_SHA1:
375 calg = CALG_SHA;
376 break;
377 case CRYPTO_HASH_ALG_HMAC_MD5:
378 case CRYPTO_HASH_ALG_HMAC_SHA1:
379 calg = CALG_HMAC;
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))
392 return NULL;
393 os_memcpy(key_blob.key, key, key_len);
394 break;
395 default:
396 return NULL;
399 ctx = os_zalloc(sizeof(*ctx));
400 if (ctx == NULL)
401 return NULL;
403 ctx->alg = alg;
405 if (!CryptAcquireContext(&ctx->prov, NULL, NULL, PROV_RSA_FULL, 0)) {
406 cryptoapi_report_error("CryptAcquireContext");
407 os_free(ctx);
408 return NULL;
411 if (calg == CALG_HMAC) {
412 #ifndef CRYPT_IPSEC_HMAC_KEY
413 #define CRYPT_IPSEC_HMAC_KEY 0x00000100
414 #endif
415 if (!CryptImportKey(ctx->prov, (BYTE *) &key_blob,
416 sizeof(key_blob), 0, CRYPT_IPSEC_HMAC_KEY,
417 &ctx->key)) {
418 cryptoapi_report_error("CryptImportKey");
419 CryptReleaseContext(ctx->prov, 0);
420 os_free(ctx);
421 return NULL;
425 if (!CryptCreateHash(ctx->prov, calg, ctx->key, 0, &ctx->hash)) {
426 cryptoapi_report_error("CryptCreateHash");
427 CryptReleaseContext(ctx->prov, 0);
428 os_free(ctx);
429 return NULL;
432 if (calg == CALG_HMAC) {
433 HMAC_INFO info;
434 os_memset(&info, 0, sizeof(info));
435 switch (alg) {
436 case CRYPTO_HASH_ALG_HMAC_MD5:
437 info.HashAlgid = CALG_MD5;
438 break;
439 case CRYPTO_HASH_ALG_HMAC_SHA1:
440 info.HashAlgid = CALG_SHA;
441 break;
442 default:
443 /* unreachable */
444 break;
447 if (!CryptSetHashParam(ctx->hash, HP_HMAC_INFO, (BYTE *) &info,
448 0)) {
449 cryptoapi_report_error("CryptSetHashParam");
450 CryptDestroyHash(ctx->hash);
451 CryptReleaseContext(ctx->prov, 0);
452 os_free(ctx);
453 return NULL;
457 return ctx;
461 void crypto_hash_update(struct crypto_hash *ctx, const u8 *data, size_t len)
463 if (ctx == NULL || ctx->error)
464 return;
466 if (!CryptHashData(ctx->hash, (BYTE *) data, len, 0)) {
467 cryptoapi_report_error("CryptHashData");
468 ctx->error = 1;
473 int crypto_hash_finish(struct crypto_hash *ctx, u8 *mac, size_t *len)
475 int ret = 0;
476 DWORD hlen;
478 if (ctx == NULL)
479 return -2;
481 if (mac == NULL || len == NULL)
482 goto done;
484 if (ctx->error) {
485 ret = -2;
486 goto done;
489 hlen = *len;
490 if (!CryptGetHashParam(ctx->hash, HP_HASHVAL, mac, &hlen, 0)) {
491 cryptoapi_report_error("CryptGetHashParam");
492 ret = -2;
494 *len = hlen;
496 done:
497 if (ctx->alg == CRYPTO_HASH_ALG_HMAC_SHA1 ||
498 ctx->alg == CRYPTO_HASH_ALG_HMAC_MD5)
499 CryptDestroyKey(ctx->key);
501 os_free(ctx);
503 return ret;
507 struct crypto_cipher {
508 HCRYPTPROV prov;
509 HCRYPTKEY key;
513 struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
514 const u8 *iv, const u8 *key,
515 size_t key_len)
517 struct crypto_cipher *ctx;
518 struct {
519 BLOBHEADER hdr;
520 DWORD len;
521 BYTE key[32];
522 } key_blob;
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))
530 return NULL;
531 os_memcpy(key_blob.key, key, key_len);
533 switch (alg) {
534 case CRYPTO_CIPHER_ALG_AES:
535 if (key_len == 32)
536 key_blob.hdr.aiKeyAlg = CALG_AES_256;
537 else if (key_len == 24)
538 key_blob.hdr.aiKeyAlg = CALG_AES_192;
539 else
540 key_blob.hdr.aiKeyAlg = CALG_AES_128;
541 break;
542 case CRYPTO_CIPHER_ALG_3DES:
543 key_blob.hdr.aiKeyAlg = CALG_3DES;
544 break;
545 case CRYPTO_CIPHER_ALG_DES:
546 key_blob.hdr.aiKeyAlg = CALG_DES;
547 break;
548 case CRYPTO_CIPHER_ALG_RC2:
549 key_blob.hdr.aiKeyAlg = CALG_RC2;
550 break;
551 case CRYPTO_CIPHER_ALG_RC4:
552 key_blob.hdr.aiKeyAlg = CALG_RC4;
553 break;
554 default:
555 return NULL;
558 ctx = os_zalloc(sizeof(*ctx));
559 if (ctx == NULL)
560 return NULL;
562 if (!CryptAcquireContext(&ctx->prov, NULL, MS_ENH_RSA_AES_PROV,
563 PROV_RSA_AES, CRYPT_VERIFYCONTEXT)) {
564 cryptoapi_report_error("CryptAcquireContext");
565 goto fail1;
568 if (!CryptImportKey(ctx->prov, (BYTE *) &key_blob,
569 sizeof(key_blob), 0, 0, &ctx->key)) {
570 cryptoapi_report_error("CryptImportKey");
571 goto fail2;
574 if (!CryptSetKeyParam(ctx->key, KP_MODE, (BYTE *) &mode, 0)) {
575 cryptoapi_report_error("CryptSetKeyParam(KP_MODE)");
576 goto fail3;
579 if (iv && !CryptSetKeyParam(ctx->key, KP_IV, (BYTE *) iv, 0)) {
580 cryptoapi_report_error("CryptSetKeyParam(KP_IV)");
581 goto fail3;
584 return ctx;
586 fail3:
587 CryptDestroyKey(ctx->key);
588 fail2:
589 CryptReleaseContext(ctx->prov, 0);
590 fail1:
591 os_free(ctx);
592 return NULL;
596 int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain,
597 u8 *crypt, size_t len)
599 DWORD dlen;
601 os_memcpy(crypt, plain, len);
602 dlen = len;
603 if (!CryptEncrypt(ctx->key, 0, FALSE, 0, crypt, &dlen, len)) {
604 cryptoapi_report_error("CryptEncrypt");
605 os_memset(crypt, 0, len);
606 return -1;
609 return 0;
613 int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt,
614 u8 *plain, size_t len)
616 DWORD dlen;
618 os_memcpy(plain, crypt, len);
619 dlen = len;
620 if (!CryptDecrypt(ctx->key, 0, FALSE, 0, plain, &dlen)) {
621 cryptoapi_report_error("CryptDecrypt");
622 return -1;
625 return 0;
629 void crypto_cipher_deinit(struct crypto_cipher *ctx)
631 CryptDestroyKey(ctx->key);
632 CryptReleaseContext(ctx->prov, 0);
633 os_free(ctx);
637 struct crypto_public_key {
638 HCRYPTPROV prov;
639 HCRYPTKEY rsa;
642 struct crypto_private_key {
643 HCRYPTPROV prov;
644 HCRYPTKEY rsa;
648 struct crypto_public_key * crypto_public_key_import(const u8 *key, size_t len)
650 /* Use crypto_public_key_from_cert() instead. */
651 return NULL;
655 struct crypto_private_key * crypto_private_key_import(const u8 *key,
656 size_t len,
657 const char *passwd)
659 /* TODO */
660 return NULL;
664 struct crypto_public_key * crypto_public_key_from_cert(const u8 *buf,
665 size_t len)
667 struct crypto_public_key *pk;
668 PCCERT_CONTEXT cc;
670 pk = os_zalloc(sizeof(*pk));
671 if (pk == NULL)
672 return NULL;
674 cc = CertCreateCertificateContext(X509_ASN_ENCODING |
675 PKCS_7_ASN_ENCODING, buf, len);
676 if (!cc) {
677 cryptoapi_report_error("CryptCreateCertificateContext");
678 os_free(pk);
679 return NULL;
682 if (!CryptAcquireContext(&pk->prov, NULL, MS_DEF_PROV, PROV_RSA_FULL,
683 0)) {
684 cryptoapi_report_error("CryptAcquireContext");
685 os_free(pk);
686 CertFreeCertificateContext(cc);
687 return NULL;
690 if (!CryptImportPublicKeyInfo(pk->prov, X509_ASN_ENCODING |
691 PKCS_7_ASN_ENCODING,
692 &cc->pCertInfo->SubjectPublicKeyInfo,
693 &pk->rsa)) {
694 cryptoapi_report_error("CryptImportPublicKeyInfo");
695 CryptReleaseContext(pk->prov, 0);
696 os_free(pk);
697 CertFreeCertificateContext(cc);
698 return NULL;
701 CertFreeCertificateContext(cc);
703 return pk;
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)
711 DWORD clen;
712 u8 *tmp;
713 size_t i;
715 if (*outlen < inlen)
716 return -1;
717 tmp = malloc(*outlen);
718 if (tmp == NULL)
719 return -1;
721 os_memcpy(tmp, in, inlen);
722 clen = 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());
726 os_free(tmp);
727 return -1;
730 *outlen = clen;
732 /* Reverse the output */
733 for (i = 0; i < *outlen; i++)
734 out[i] = tmp[*outlen - 1 - i];
736 os_free(tmp);
738 return 0;
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)
746 /* TODO */
747 return -1;
751 void crypto_public_key_free(struct crypto_public_key *key)
753 if (key) {
754 CryptDestroyKey(key->rsa);
755 CryptReleaseContext(key->prov, 0);
756 os_free(key);
761 void crypto_private_key_free(struct crypto_private_key *key)
763 if (key) {
764 CryptDestroyKey(key->rsa);
765 CryptReleaseContext(key->prov, 0);
766 os_free(key);
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)
787 /* TODO */
788 return -1;