an, awi, cue and kue don't use the miibus interface.
[dragonfly.git] / contrib / wpa_supplicant-0.4.9 / crypto_gnutls.c
1 /*
2  * WPA Supplicant / wrapper functions for libgcrypt
3  * Copyright (c) 2004-2005, Jouni Malinen <jkmaline@cc.hut.fi>
4  *
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.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include <stdio.h>
16 #include <sys/types.h>
17 #include <gcrypt.h>
18
19 #include "common.h"
20 #include "crypto.h"
21
22 void md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
23 {
24         gcry_md_hd_t hd;
25         unsigned char *p;
26         int i;
27
28         if (gcry_md_open(&hd, GCRY_MD_MD4, 0) != GPG_ERR_NO_ERROR)
29                 return;
30         for (i = 0; i < num_elem; i++)
31                 gcry_md_write(hd, addr[i], len[i]);
32         p = gcry_md_read(hd, GCRY_MD_MD4);
33         if (p)
34                 memcpy(mac, p, gcry_md_get_algo_dlen(GCRY_MD_MD4));
35         gcry_md_close(hd);
36 }
37
38
39 void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
40 {
41         gcry_cipher_hd_t hd;
42         u8 pkey[8], next, tmp;
43         int i;
44
45         /* Add parity bits to the key */
46         next = 0;
47         for (i = 0; i < 7; i++) {
48                 tmp = key[i];
49                 pkey[i] = (tmp >> i) | next | 1;
50                 next = tmp << (7 - i);
51         }
52         pkey[i] = next | 1;
53
54         gcry_cipher_open(&hd, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
55         gcry_err_code(gcry_cipher_setkey(hd, pkey, 8));
56         gcry_cipher_encrypt(hd, cypher, 8, clear, 8);
57         gcry_cipher_close(hd);
58 }
59
60
61 #ifdef EAP_TLS_FUNCS
62 void md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
63 {
64         gcry_md_hd_t hd;
65         unsigned char *p;
66         int i;
67
68         if (gcry_md_open(&hd, GCRY_MD_MD5, 0) != GPG_ERR_NO_ERROR)
69                 return;
70         for (i = 0; i < num_elem; i++)
71                 gcry_md_write(hd, addr[i], len[i]);
72         p = gcry_md_read(hd, GCRY_MD_MD5);
73         if (p)
74                 memcpy(mac, p, gcry_md_get_algo_dlen(GCRY_MD_MD5));
75         gcry_md_close(hd);
76 }
77
78
79 void sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
80 {
81         gcry_md_hd_t hd;
82         unsigned char *p;
83         int i;
84
85         if (gcry_md_open(&hd, GCRY_MD_SHA1, 0) != GPG_ERR_NO_ERROR)
86                 return;
87         for (i = 0; i < num_elem; i++)
88                 gcry_md_write(hd, addr[i], len[i]);
89         p = gcry_md_read(hd, GCRY_MD_SHA1);
90         if (p)
91                 memcpy(mac, p, gcry_md_get_algo_dlen(GCRY_MD_SHA1));
92         gcry_md_close(hd);
93 }
94
95
96 void sha1_transform(u8 *state, const u8 data[64])
97 {
98         /* FIX: how to do this with libgcrypt? */
99 }
100
101
102 void * aes_encrypt_init(const u8 *key, size_t len)
103 {
104         gcry_cipher_hd_t hd;
105
106         if (gcry_cipher_open(&hd, GCRY_CIPHER_AES, GCRY_CIPHER_MODE_ECB, 0) !=
107             GPG_ERR_NO_ERROR) {
108                 printf("cipher open failed\n");
109                 return NULL;
110         }
111         if (gcry_cipher_setkey(hd, key, len) != GPG_ERR_NO_ERROR) {
112                 printf("setkey failed\n");
113                 gcry_cipher_close(hd);
114                 return NULL;
115         }
116
117         return hd;
118 }
119
120
121 void aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
122 {
123         gcry_cipher_hd_t hd = ctx;
124         gcry_cipher_encrypt(hd, crypt, 16, plain, 16);
125 }
126
127
128 void aes_encrypt_deinit(void *ctx)
129 {
130         gcry_cipher_hd_t hd = ctx;
131         gcry_cipher_close(hd);
132 }
133
134
135 void * aes_decrypt_init(const u8 *key, size_t len)
136 {
137         gcry_cipher_hd_t hd;
138
139         if (gcry_cipher_open(&hd, GCRY_CIPHER_AES, GCRY_CIPHER_MODE_ECB, 0) !=
140             GPG_ERR_NO_ERROR)
141                 return NULL;
142         if (gcry_cipher_setkey(hd, key, len) != GPG_ERR_NO_ERROR) {
143                 gcry_cipher_close(hd);
144                 return NULL;
145         }
146
147         return hd;
148 }
149
150
151 void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
152 {
153         gcry_cipher_hd_t hd = ctx;
154         gcry_cipher_decrypt(hd, plain, 16, crypt, 16);
155 }
156
157
158 void aes_decrypt_deinit(void *ctx)
159 {
160         gcry_cipher_hd_t hd = ctx;
161         gcry_cipher_close(hd);
162 }
163 #endif /* EAP_TLS_FUNCS */