Add resident.conf(5) and varsym.conf(5) manual pages.
[dragonfly/vkernel-mp.git] / contrib / hostapd-0.4.9 / crypto.c
blob1b136711d10c33182673a85c34d675b2583a3b4e
1 /*
2 * WPA Supplicant / wrapper functions for libcrypto
3 * Copyright (c) 2004-2005, Jouni Malinen <jkmaline@cc.hut.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 <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <sys/types.h>
20 #include <openssl/opensslv.h>
21 #include <openssl/md4.h>
22 #include <openssl/md5.h>
23 #include <openssl/sha.h>
24 #include <openssl/des.h>
25 #include <openssl/aes.h>
27 #include "common.h"
28 #include "crypto.h"
30 #if OPENSSL_VERSION_NUMBER < 0x00907000
31 #define DES_key_schedule des_key_schedule
32 #define DES_cblock des_cblock
33 #define DES_set_key(key, schedule) des_set_key((key), *(schedule))
34 #define DES_ecb_encrypt(input, output, ks, enc) \
35 des_ecb_encrypt((input), (output), *(ks), (enc))
36 #endif /* openssl < 0.9.7 */
39 void md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
41 MD4_CTX ctx;
42 int i;
44 MD4_Init(&ctx);
45 for (i = 0; i < num_elem; i++)
46 MD4_Update(&ctx, addr[i], len[i]);
47 MD4_Final(mac, &ctx);
51 void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
53 u8 pkey[8], next, tmp;
54 int i;
55 DES_key_schedule ks;
57 /* Add parity bits to the key */
58 next = 0;
59 for (i = 0; i < 7; i++) {
60 tmp = key[i];
61 pkey[i] = (tmp >> i) | next | 1;
62 next = tmp << (7 - i);
64 pkey[i] = next | 1;
66 DES_set_key(&pkey, &ks);
67 DES_ecb_encrypt((DES_cblock *) clear, (DES_cblock *) cypher, &ks,
68 DES_ENCRYPT);
72 #ifdef EAP_TLS_FUNCS
73 void md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
75 MD5_CTX ctx;
76 int i;
78 MD5_Init(&ctx);
79 for (i = 0; i < num_elem; i++)
80 MD5_Update(&ctx, addr[i], len[i]);
81 MD5_Final(mac, &ctx);
85 void sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
87 SHA_CTX ctx;
88 int i;
90 SHA1_Init(&ctx);
91 for (i = 0; i < num_elem; i++)
92 SHA1_Update(&ctx, addr[i], len[i]);
93 SHA1_Final(mac, &ctx);
97 void sha1_transform(u8 *state, const u8 data[64])
99 SHA_CTX context;
100 memset(&context, 0, sizeof(context));
101 memcpy(&context.h0, state, 5 * 4);
102 SHA1_Transform(&context, data);
103 memcpy(state, &context.h0, 5 * 4);
107 void * aes_encrypt_init(const u8 *key, size_t len)
109 AES_KEY *ak;
110 ak = malloc(sizeof(*ak));
111 if (ak == NULL)
112 return NULL;
113 if (AES_set_encrypt_key(key, 8 * len, ak) < 0) {
114 free(ak);
115 return NULL;
117 return ak;
121 void aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
123 AES_encrypt(plain, crypt, ctx);
127 void aes_encrypt_deinit(void *ctx)
129 free(ctx);
133 void * aes_decrypt_init(const u8 *key, size_t len)
135 AES_KEY *ak;
136 ak = malloc(sizeof(*ak));
137 if (ak == NULL)
138 return NULL;
139 if (AES_set_decrypt_key(key, 8 * len, ak) < 0) {
140 free(ak);
141 return NULL;
143 return ak;
147 void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
149 AES_decrypt(crypt, plain, ctx);
153 void aes_decrypt_deinit(void *ctx)
155 free(ctx);
157 #endif /* EAP_TLS_FUNCS */