2 * WPA Supplicant / wrapper functions for libgcrypt
3 * Copyright (c) 2004-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.
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);
60 void md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
66 if (gcry_md_open(&hd, GCRY_MD_MD5, 0) != GPG_ERR_NO_ERROR)
68 for (i = 0; i < num_elem; i++)
69 gcry_md_write(hd, addr[i], len[i]);
70 p = gcry_md_read(hd, GCRY_MD_MD5);
72 memcpy(mac, p, gcry_md_get_algo_dlen(GCRY_MD_MD5));
77 void sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
83 if (gcry_md_open(&hd, GCRY_MD_SHA1, 0) != GPG_ERR_NO_ERROR)
85 for (i = 0; i < num_elem; i++)
86 gcry_md_write(hd, addr[i], len[i]);
87 p = gcry_md_read(hd, GCRY_MD_SHA1);
89 memcpy(mac, p, gcry_md_get_algo_dlen(GCRY_MD_SHA1));
94 void * aes_encrypt_init(const u8 *key, size_t len)
98 if (gcry_cipher_open(&hd, GCRY_CIPHER_AES, GCRY_CIPHER_MODE_ECB, 0) !=
100 printf("cipher open failed\n");
103 if (gcry_cipher_setkey(hd, key, len) != GPG_ERR_NO_ERROR) {
104 printf("setkey failed\n");
105 gcry_cipher_close(hd);
113 void aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
115 gcry_cipher_hd_t hd = ctx;
116 gcry_cipher_encrypt(hd, crypt, 16, plain, 16);
120 void aes_encrypt_deinit(void *ctx)
122 gcry_cipher_hd_t hd = ctx;
123 gcry_cipher_close(hd);
127 void * aes_decrypt_init(const u8 *key, size_t len)
131 if (gcry_cipher_open(&hd, GCRY_CIPHER_AES, GCRY_CIPHER_MODE_ECB, 0) !=
134 if (gcry_cipher_setkey(hd, key, len) != GPG_ERR_NO_ERROR) {
135 gcry_cipher_close(hd);
143 void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
145 gcry_cipher_hd_t hd = ctx;
146 gcry_cipher_decrypt(hd, plain, 16, crypt, 16);
150 void aes_decrypt_deinit(void *ctx)
152 gcry_cipher_hd_t hd = ctx;
153 gcry_cipher_close(hd);
157 int crypto_mod_exp(const u8 *base, size_t base_len,
158 const u8 *power, size_t power_len,
159 const u8 *modulus, size_t modulus_len,
160 u8 *result, size_t *result_len)
162 gcry_mpi_t bn_base = NULL, bn_exp = NULL, bn_modulus = NULL,
166 if (gcry_mpi_scan(&bn_base, GCRYMPI_FMT_USG, base, base_len, NULL) !=
168 gcry_mpi_scan(&bn_exp, GCRYMPI_FMT_USG, power, power_len, NULL) !=
170 gcry_mpi_scan(&bn_modulus, GCRYMPI_FMT_USG, modulus, modulus_len,
171 NULL) != GPG_ERR_NO_ERROR)
173 bn_result = gcry_mpi_new(modulus_len * 8);
175 gcry_mpi_powm(bn_result, bn_base, bn_exp, bn_modulus);
177 if (gcry_mpi_print(GCRYMPI_FMT_USG, result, *result_len, result_len,
178 bn_result) != GPG_ERR_NO_ERROR)
184 gcry_mpi_release(bn_base);
185 gcry_mpi_release(bn_exp);
186 gcry_mpi_release(bn_modulus);
187 gcry_mpi_release(bn_result);
192 struct crypto_cipher {
193 gcry_cipher_hd_t enc;
194 gcry_cipher_hd_t dec;
198 struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
199 const u8 *iv, const u8 *key,
202 struct crypto_cipher *ctx;
204 enum gcry_cipher_algos a;
207 ctx = os_zalloc(sizeof(*ctx));
212 case CRYPTO_CIPHER_ALG_RC4:
213 a = GCRY_CIPHER_ARCFOUR;
214 res = gcry_cipher_open(&ctx->enc, a, GCRY_CIPHER_MODE_STREAM,
216 gcry_cipher_open(&ctx->dec, a, GCRY_CIPHER_MODE_STREAM, 0);
218 case CRYPTO_CIPHER_ALG_AES:
220 a = GCRY_CIPHER_AES192;
221 else if (key_len == 32)
222 a = GCRY_CIPHER_AES256;
225 res = gcry_cipher_open(&ctx->enc, a, GCRY_CIPHER_MODE_CBC, 0);
226 gcry_cipher_open(&ctx->dec, a, GCRY_CIPHER_MODE_CBC, 0);
228 case CRYPTO_CIPHER_ALG_3DES:
229 a = GCRY_CIPHER_3DES;
230 res = gcry_cipher_open(&ctx->enc, a, GCRY_CIPHER_MODE_CBC, 0);
231 gcry_cipher_open(&ctx->dec, a, GCRY_CIPHER_MODE_CBC, 0);
233 case CRYPTO_CIPHER_ALG_DES:
235 res = gcry_cipher_open(&ctx->enc, a, GCRY_CIPHER_MODE_CBC, 0);
236 gcry_cipher_open(&ctx->dec, a, GCRY_CIPHER_MODE_CBC, 0);
238 case CRYPTO_CIPHER_ALG_RC2:
240 a = GCRY_CIPHER_RFC2268_40;
242 a = GCRY_CIPHER_RFC2268_128;
243 res = gcry_cipher_open(&ctx->enc, a, GCRY_CIPHER_MODE_CBC, 0);
244 gcry_cipher_open(&ctx->dec, a, GCRY_CIPHER_MODE_CBC, 0);
251 if (res != GPG_ERR_NO_ERROR) {
256 if (gcry_cipher_setkey(ctx->enc, key, key_len) != GPG_ERR_NO_ERROR ||
257 gcry_cipher_setkey(ctx->dec, key, key_len) != GPG_ERR_NO_ERROR) {
258 gcry_cipher_close(ctx->enc);
259 gcry_cipher_close(ctx->dec);
264 ivlen = gcry_cipher_get_algo_blklen(a);
265 if (gcry_cipher_setiv(ctx->enc, iv, ivlen) != GPG_ERR_NO_ERROR ||
266 gcry_cipher_setiv(ctx->dec, iv, ivlen) != GPG_ERR_NO_ERROR) {
267 gcry_cipher_close(ctx->enc);
268 gcry_cipher_close(ctx->dec);
277 int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain,
278 u8 *crypt, size_t len)
280 if (gcry_cipher_encrypt(ctx->enc, crypt, len, plain, len) !=
287 int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt,
288 u8 *plain, size_t len)
290 if (gcry_cipher_decrypt(ctx->dec, plain, len, crypt, len) !=
297 void crypto_cipher_deinit(struct crypto_cipher *ctx)
299 gcry_cipher_close(ctx->enc);
300 gcry_cipher_close(ctx->dec);