00001
00016 #include "includes.h"
00017 #include <gcrypt.h>
00018
00019 #include "common.h"
00020 #include "crypto.h"
00021
00022 int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
00023 {
00024 gcry_md_hd_t hd;
00025 unsigned char *p;
00026 size_t i;
00027
00028 if (gcry_md_open(&hd, GCRY_MD_MD4, 0) != GPG_ERR_NO_ERROR)
00029 return -1;
00030 for (i = 0; i < num_elem; i++)
00031 gcry_md_write(hd, addr[i], len[i]);
00032 p = gcry_md_read(hd, GCRY_MD_MD4);
00033 if (p)
00034 memcpy(mac, p, gcry_md_get_algo_dlen(GCRY_MD_MD4));
00035 gcry_md_close(hd);
00036 return 0;
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 int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
00063 {
00064 gcry_md_hd_t hd;
00065 unsigned char *p;
00066 size_t i;
00067
00068 if (gcry_md_open(&hd, GCRY_MD_MD5, 0) != GPG_ERR_NO_ERROR)
00069 return -1;
00070 for (i = 0; i < num_elem; i++)
00071 gcry_md_write(hd, addr[i], len[i]);
00072 p = gcry_md_read(hd, GCRY_MD_MD5);
00073 if (p)
00074 memcpy(mac, p, gcry_md_get_algo_dlen(GCRY_MD_MD5));
00075 gcry_md_close(hd);
00076 return 0;
00077 }
00078
00079
00080 int 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 size_t i;
00085
00086 if (gcry_md_open(&hd, GCRY_MD_SHA1, 0) != GPG_ERR_NO_ERROR)
00087 return -1;
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 return 0;
00095 }
00096
00097
00098 void * aes_encrypt_init(const u8 *key, size_t len)
00099 {
00100 gcry_cipher_hd_t hd;
00101
00102 if (gcry_cipher_open(&hd, GCRY_CIPHER_AES, GCRY_CIPHER_MODE_ECB, 0) !=
00103 GPG_ERR_NO_ERROR) {
00104 printf("cipher open failed\n");
00105 return NULL;
00106 }
00107 if (gcry_cipher_setkey(hd, key, len) != GPG_ERR_NO_ERROR) {
00108 printf("setkey failed\n");
00109 gcry_cipher_close(hd);
00110 return NULL;
00111 }
00112
00113 return hd;
00114 }
00115
00116
00117 void aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
00118 {
00119 gcry_cipher_hd_t hd = ctx;
00120 gcry_cipher_encrypt(hd, crypt, 16, plain, 16);
00121 }
00122
00123
00124 void aes_encrypt_deinit(void *ctx)
00125 {
00126 gcry_cipher_hd_t hd = ctx;
00127 gcry_cipher_close(hd);
00128 }
00129
00130
00131 void * aes_decrypt_init(const u8 *key, size_t len)
00132 {
00133 gcry_cipher_hd_t hd;
00134
00135 if (gcry_cipher_open(&hd, GCRY_CIPHER_AES, GCRY_CIPHER_MODE_ECB, 0) !=
00136 GPG_ERR_NO_ERROR)
00137 return NULL;
00138 if (gcry_cipher_setkey(hd, key, len) != GPG_ERR_NO_ERROR) {
00139 gcry_cipher_close(hd);
00140 return NULL;
00141 }
00142
00143 return hd;
00144 }
00145
00146
00147 void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
00148 {
00149 gcry_cipher_hd_t hd = ctx;
00150 gcry_cipher_decrypt(hd, plain, 16, crypt, 16);
00151 }
00152
00153
00154 void aes_decrypt_deinit(void *ctx)
00155 {
00156 gcry_cipher_hd_t hd = ctx;
00157 gcry_cipher_close(hd);
00158 }
00159
00160
00161 int crypto_mod_exp(const u8 *base, size_t base_len,
00162 const u8 *power, size_t power_len,
00163 const u8 *modulus, size_t modulus_len,
00164 u8 *result, size_t *result_len)
00165 {
00166 gcry_mpi_t bn_base = NULL, bn_exp = NULL, bn_modulus = NULL,
00167 bn_result = NULL;
00168 int ret = -1;
00169
00170 if (gcry_mpi_scan(&bn_base, GCRYMPI_FMT_USG, base, base_len, NULL) !=
00171 GPG_ERR_NO_ERROR ||
00172 gcry_mpi_scan(&bn_exp, GCRYMPI_FMT_USG, power, power_len, NULL) !=
00173 GPG_ERR_NO_ERROR ||
00174 gcry_mpi_scan(&bn_modulus, GCRYMPI_FMT_USG, modulus, modulus_len,
00175 NULL) != GPG_ERR_NO_ERROR)
00176 goto error;
00177 bn_result = gcry_mpi_new(modulus_len * 8);
00178
00179 gcry_mpi_powm(bn_result, bn_base, bn_exp, bn_modulus);
00180
00181 if (gcry_mpi_print(GCRYMPI_FMT_USG, result, *result_len, result_len,
00182 bn_result) != GPG_ERR_NO_ERROR)
00183 goto error;
00184
00185 ret = 0;
00186
00187 error:
00188 gcry_mpi_release(bn_base);
00189 gcry_mpi_release(bn_exp);
00190 gcry_mpi_release(bn_modulus);
00191 gcry_mpi_release(bn_result);
00192 return ret;
00193 }
00194
00195
00196 struct crypto_cipher {
00197 gcry_cipher_hd_t enc;
00198 gcry_cipher_hd_t dec;
00199 };
00200
00201
00202 struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
00203 const u8 *iv, const u8 *key,
00204 size_t key_len)
00205 {
00206 struct crypto_cipher *ctx;
00207 gcry_error_t res;
00208 enum gcry_cipher_algos a;
00209 int ivlen;
00210
00211 ctx = os_zalloc(sizeof(*ctx));
00212 if (ctx == NULL)
00213 return NULL;
00214
00215 switch (alg) {
00216 case CRYPTO_CIPHER_ALG_RC4:
00217 a = GCRY_CIPHER_ARCFOUR;
00218 res = gcry_cipher_open(&ctx->enc, a, GCRY_CIPHER_MODE_STREAM,
00219 0);
00220 gcry_cipher_open(&ctx->dec, a, GCRY_CIPHER_MODE_STREAM, 0);
00221 break;
00222 case CRYPTO_CIPHER_ALG_AES:
00223 if (key_len == 24)
00224 a = GCRY_CIPHER_AES192;
00225 else if (key_len == 32)
00226 a = GCRY_CIPHER_AES256;
00227 else
00228 a = GCRY_CIPHER_AES;
00229 res = gcry_cipher_open(&ctx->enc, a, GCRY_CIPHER_MODE_CBC, 0);
00230 gcry_cipher_open(&ctx->dec, a, GCRY_CIPHER_MODE_CBC, 0);
00231 break;
00232 case CRYPTO_CIPHER_ALG_3DES:
00233 a = GCRY_CIPHER_3DES;
00234 res = gcry_cipher_open(&ctx->enc, a, GCRY_CIPHER_MODE_CBC, 0);
00235 gcry_cipher_open(&ctx->dec, a, GCRY_CIPHER_MODE_CBC, 0);
00236 break;
00237 case CRYPTO_CIPHER_ALG_DES:
00238 a = GCRY_CIPHER_DES;
00239 res = gcry_cipher_open(&ctx->enc, a, GCRY_CIPHER_MODE_CBC, 0);
00240 gcry_cipher_open(&ctx->dec, a, GCRY_CIPHER_MODE_CBC, 0);
00241 break;
00242 case CRYPTO_CIPHER_ALG_RC2:
00243 if (key_len == 5)
00244 a = GCRY_CIPHER_RFC2268_40;
00245 else
00246 a = GCRY_CIPHER_RFC2268_128;
00247 res = gcry_cipher_open(&ctx->enc, a, GCRY_CIPHER_MODE_CBC, 0);
00248 gcry_cipher_open(&ctx->dec, a, GCRY_CIPHER_MODE_CBC, 0);
00249 break;
00250 default:
00251 os_free(ctx);
00252 return NULL;
00253 }
00254
00255 if (res != GPG_ERR_NO_ERROR) {
00256 os_free(ctx);
00257 return NULL;
00258 }
00259
00260 if (gcry_cipher_setkey(ctx->enc, key, key_len) != GPG_ERR_NO_ERROR ||
00261 gcry_cipher_setkey(ctx->dec, key, key_len) != GPG_ERR_NO_ERROR) {
00262 gcry_cipher_close(ctx->enc);
00263 gcry_cipher_close(ctx->dec);
00264 os_free(ctx);
00265 return NULL;
00266 }
00267
00268 ivlen = gcry_cipher_get_algo_blklen(a);
00269 if (gcry_cipher_setiv(ctx->enc, iv, ivlen) != GPG_ERR_NO_ERROR ||
00270 gcry_cipher_setiv(ctx->dec, iv, ivlen) != GPG_ERR_NO_ERROR) {
00271 gcry_cipher_close(ctx->enc);
00272 gcry_cipher_close(ctx->dec);
00273 os_free(ctx);
00274 return NULL;
00275 }
00276
00277 return ctx;
00278 }
00279
00280
00281 int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain,
00282 u8 *crypt, size_t len)
00283 {
00284 if (gcry_cipher_encrypt(ctx->enc, crypt, len, plain, len) !=
00285 GPG_ERR_NO_ERROR)
00286 return -1;
00287 return 0;
00288 }
00289
00290
00291 int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt,
00292 u8 *plain, size_t len)
00293 {
00294 if (gcry_cipher_decrypt(ctx->dec, plain, len, crypt, len) !=
00295 GPG_ERR_NO_ERROR)
00296 return -1;
00297 return 0;
00298 }
00299
00300
00301 void crypto_cipher_deinit(struct crypto_cipher *ctx)
00302 {
00303 gcry_cipher_close(ctx->enc);
00304 gcry_cipher_close(ctx->dec);
00305 os_free(ctx);
00306 }
00307