00001
00016 #include <stdio.h>
00017 #include <sys/types.h>
00018 #include <gcrypt.h>
00019
00020 #include "common.h"
00021 #include "crypto.h"
00022
00023 void md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
00024 {
00025 gcry_md_hd_t hd;
00026 unsigned char *p;
00027 int i;
00028
00029 if (gcry_md_open(&hd, GCRY_MD_MD4, 0) != GPG_ERR_NO_ERROR)
00030 return;
00031 for (i = 0; i < num_elem; i++)
00032 gcry_md_write(hd, addr[i], len[i]);
00033 p = gcry_md_read(hd, GCRY_MD_MD4);
00034 if (p)
00035 memcpy(mac, p, gcry_md_get_algo_dlen(GCRY_MD_MD4));
00036 gcry_md_close(hd);
00037 }
00038
00039
00040 void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
00041 {
00042 gcry_cipher_hd_t hd;
00043 u8 pkey[8], next, tmp;
00044 int i;
00045
00046
00047 next = 0;
00048 for (i = 0; i < 7; i++) {
00049 tmp = key[i];
00050 pkey[i] = (tmp >> i) | next | 1;
00051 next = tmp << (7 - i);
00052 }
00053 pkey[i] = next | 1;
00054
00055 gcry_cipher_open(&hd, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
00056 gcry_err_code(gcry_cipher_setkey(hd, pkey, 8));
00057 gcry_cipher_encrypt(hd, cypher, 8, clear, 8);
00058 gcry_cipher_close(hd);
00059 }
00060
00061
00062 #ifdef EAP_TLS_FUNCS
00063 void md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
00064 {
00065 gcry_md_hd_t hd;
00066 unsigned char *p;
00067 int i;
00068
00069 if (gcry_md_open(&hd, GCRY_MD_MD5, 0) != GPG_ERR_NO_ERROR)
00070 return;
00071 for (i = 0; i < num_elem; i++)
00072 gcry_md_write(hd, addr[i], len[i]);
00073 p = gcry_md_read(hd, GCRY_MD_MD5);
00074 if (p)
00075 memcpy(mac, p, gcry_md_get_algo_dlen(GCRY_MD_MD5));
00076 gcry_md_close(hd);
00077 }
00078
00079
00080 void sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
00081 {
00082 gcry_md_hd_t hd;
00083 unsigned char *p;
00084 int i;
00085
00086 if (gcry_md_open(&hd, GCRY_MD_SHA1, 0) != GPG_ERR_NO_ERROR)
00087 return;
00088 for (i = 0; i < num_elem; i++)
00089 gcry_md_write(hd, addr[i], len[i]);
00090 p = gcry_md_read(hd, GCRY_MD_SHA1);
00091 if (p)
00092 memcpy(mac, p, gcry_md_get_algo_dlen(GCRY_MD_SHA1));
00093 gcry_md_close(hd);
00094 }
00095
00096
00097 void sha1_transform(u8 *state, const u8 data[64])
00098 {
00099
00100 }
00101
00102
00103 void * aes_encrypt_init(const u8 *key, size_t len)
00104 {
00105 gcry_cipher_hd_t hd;
00106
00107 if (gcry_cipher_open(&hd, GCRY_CIPHER_AES, GCRY_CIPHER_MODE_ECB, 0) !=
00108 GPG_ERR_NO_ERROR) {
00109 printf("cipher open failed\n");
00110 return NULL;
00111 }
00112 if (gcry_cipher_setkey(hd, key, len) != GPG_ERR_NO_ERROR) {
00113 printf("setkey failed\n");
00114 gcry_cipher_close(hd);
00115 return NULL;
00116 }
00117
00118 return hd;
00119 }
00120
00121
00122 void aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
00123 {
00124 gcry_cipher_hd_t hd = ctx;
00125 gcry_cipher_encrypt(hd, crypt, 16, plain, 16);
00126 }
00127
00128
00129 void aes_encrypt_deinit(void *ctx)
00130 {
00131 gcry_cipher_hd_t hd = ctx;
00132 gcry_cipher_close(hd);
00133 }
00134
00135
00136 void * aes_decrypt_init(const u8 *key, size_t len)
00137 {
00138 gcry_cipher_hd_t hd;
00139
00140 if (gcry_cipher_open(&hd, GCRY_CIPHER_AES, GCRY_CIPHER_MODE_ECB, 0) !=
00141 GPG_ERR_NO_ERROR)
00142 return NULL;
00143 if (gcry_cipher_setkey(hd, key, len) != GPG_ERR_NO_ERROR) {
00144 gcry_cipher_close(hd);
00145 return NULL;
00146 }
00147
00148 return hd;
00149 }
00150
00151
00152 void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
00153 {
00154 gcry_cipher_hd_t hd = ctx;
00155 gcry_cipher_decrypt(hd, plain, 16, crypt, 16);
00156 }
00157
00158
00159 void aes_decrypt_deinit(void *ctx)
00160 {
00161 gcry_cipher_hd_t hd = ctx;
00162 gcry_cipher_close(hd);
00163 }
00164 #endif
00165