2 * WPA Supplicant / wrapper functions for libgcrypt
3 * Copyright (c) 2004-2005, 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.
21 void md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
27 if (gcry_md_open(&hd, GCRY_MD_MD4, 0) != GPG_ERR_NO_ERROR)
29 for (i = 0; i < num_elem; i++)
30 gcry_md_write(hd, addr[i], len[i]);
31 p = gcry_md_read(hd, GCRY_MD_MD4);
33 memcpy(mac, p, gcry_md_get_algo_dlen(GCRY_MD_MD4));
38 void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
41 u8 pkey[8], next, tmp;
44 /* Add parity bits to the key */
46 for (i = 0; i < 7; i++) {
48 pkey[i] = (tmp >> i) | next | 1;
49 next = tmp << (7 - i);
53 gcry_cipher_open(&hd, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
54 gcry_err_code(gcry_cipher_setkey(hd, pkey, 8));
55 gcry_cipher_encrypt(hd, cypher, 8, clear, 8);
56 gcry_cipher_close(hd);
61 void md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
67 if (gcry_md_open(&hd, GCRY_MD_MD5, 0) != GPG_ERR_NO_ERROR)
69 for (i = 0; i < num_elem; i++)
70 gcry_md_write(hd, addr[i], len[i]);
71 p = gcry_md_read(hd, GCRY_MD_MD5);
73 memcpy(mac, p, gcry_md_get_algo_dlen(GCRY_MD_MD5));
78 void sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
84 if (gcry_md_open(&hd, GCRY_MD_SHA1, 0) != GPG_ERR_NO_ERROR)
86 for (i = 0; i < num_elem; i++)
87 gcry_md_write(hd, addr[i], len[i]);
88 p = gcry_md_read(hd, GCRY_MD_SHA1);
90 memcpy(mac, p, gcry_md_get_algo_dlen(GCRY_MD_SHA1));
95 int fips186_2_prf(const u8 *seed, size_t seed_len, u8 *x, size_t xlen)
97 /* FIX: how to do this with libgcrypt? */
102 void * aes_encrypt_init(const u8 *key, size_t len)
106 if (gcry_cipher_open(&hd, GCRY_CIPHER_AES, GCRY_CIPHER_MODE_ECB, 0) !=
108 printf("cipher open failed\n");
111 if (gcry_cipher_setkey(hd, key, len) != GPG_ERR_NO_ERROR) {
112 printf("setkey failed\n");
113 gcry_cipher_close(hd);
121 void aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
123 gcry_cipher_hd_t hd = ctx;
124 gcry_cipher_encrypt(hd, crypt, 16, plain, 16);
128 void aes_encrypt_deinit(void *ctx)
130 gcry_cipher_hd_t hd = ctx;
131 gcry_cipher_close(hd);
135 void * aes_decrypt_init(const u8 *key, size_t len)
139 if (gcry_cipher_open(&hd, GCRY_CIPHER_AES, GCRY_CIPHER_MODE_ECB, 0) !=
142 if (gcry_cipher_setkey(hd, key, len) != GPG_ERR_NO_ERROR) {
143 gcry_cipher_close(hd);
151 void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
153 gcry_cipher_hd_t hd = ctx;
154 gcry_cipher_decrypt(hd, plain, 16, crypt, 16);
158 void aes_decrypt_deinit(void *ctx)
160 gcry_cipher_hd_t hd = ctx;
161 gcry_cipher_close(hd);
163 #endif /* EAP_TLS_FUNCS */