Split crypto_internal.c into parts to clean up build
authorJouni Malinen <j@w1.fi>
Sun, 6 Dec 2009 12:37:46 +0000 (14:37 +0200)
committerJouni Malinen <j@w1.fi>
Sun, 6 Dec 2009 12:37:46 +0000 (14:37 +0200)
This makes it easier to make src/libcrypto.a and only link in
code that is really used.

hostapd/Makefile
src/crypto/Makefile
src/crypto/crypto_internal-cipher.c [new file with mode: 0644]
src/crypto/crypto_internal-modexp.c [new file with mode: 0644]
src/crypto/crypto_internal-rsa.c [new file with mode: 0644]
src/crypto/crypto_internal.c
wpa_supplicant/Makefile

index 05961be..6f8eef0 100644 (file)
@@ -329,6 +329,7 @@ OBJS += ../src/eap_common/eap_ikev2_common.o ../src/eap_common/ikev2_common.o
 NEED_DH_GROUPS=y
 NEED_DH_GROUPS_ALL=y
 NEED_MODEXP=y
+NEED_CIPHER=y
 endif
 
 ifdef CONFIG_EAP_TNC
@@ -445,6 +446,7 @@ ifndef CONFIG_CRYPTO
 CONFIG_CRYPTO=internal
 endif
 ifdef TLS_FUNCS
+OBJS += ../src/crypto/crypto_internal-rsa.o
 OBJS += ../src/crypto/tls_internal.o
 OBJS += ../src/tls/tlsv1_common.o
 OBJS += ../src/tls/tlsv1_record.o
@@ -461,10 +463,16 @@ OBJS += ../src/tls/pkcs8.o
 NEED_BASE64=y
 NEED_TLS_PRF=y
 NEED_MODEXP=y
+NEED_CIPHER=y
 CFLAGS += -DCONFIG_TLS_INTERNAL
 CFLAGS += -DCONFIG_TLS_INTERNAL_SERVER
 endif
+ifdef NEED_CIPHER
+NEED_DES=y
+OBJS += ../src/crypto/crypto_internal-cipher.o
+endif
 ifdef NEED_MODEXP
+OBJS += ../src/crypto/crypto_internal-modexp.o
 OBJS += ../src/tls/bignum.o
 endif
 ifeq ($(CONFIG_CRYPTO), libtomcrypt)
@@ -532,10 +540,6 @@ CONFIG_INTERNAL_RC4=y
 endif
 endif
 
-ifdef NEED_MODEXP
-CFLAGS += -DCONFIG_MODEXP
-endif
-
 AESOBJS = # none so far
 ifdef CONFIG_INTERNAL_AES
 AESOBJS += ../src/crypto/aes-internal.o ../src/crypto/aes-internal-enc.o
index d1330a2..901b3a6 100644 (file)
@@ -44,6 +44,9 @@ LIB_OBJS= \
        sha256-internal.o
 
 LIB_OBJS += crypto_internal.o
+LIB_OBJS += crypto_internal-cipher.o
+LIB_OBJS += crypto_internal-modexp.o
+LIB_OBJS += crypto_internal-rsa.o
 LIB_OBJS += tls_internal.o
 LIB_OBJS += fips_prf_internal.o
 
diff --git a/src/crypto/crypto_internal-cipher.c b/src/crypto/crypto_internal-cipher.c
new file mode 100644 (file)
index 0000000..75134f0
--- /dev/null
@@ -0,0 +1,256 @@
+/*
+ * Crypto wrapper for internal crypto implementation - Cipher wrappers
+ * Copyright (c) 2006-2009, Jouni Malinen <j@w1.fi>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * Alternatively, this software may be distributed under the terms of BSD
+ * license.
+ *
+ * See README and COPYING for more details.
+ */
+
+#include "includes.h"
+
+#include "common.h"
+#include "crypto.h"
+#include "aes.h"
+#include "des_i.h"
+
+
+struct crypto_cipher {
+       enum crypto_cipher_alg alg;
+       union {
+               struct {
+                       size_t used_bytes;
+                       u8 key[16];
+                       size_t keylen;
+               } rc4;
+               struct {
+                       u8 cbc[32];
+                       size_t block_size;
+                       void *ctx_enc;
+                       void *ctx_dec;
+               } aes;
+               struct {
+                       struct des3_key_s key;
+                       u8 cbc[8];
+               } des3;
+               struct {
+                       u32 ek[32];
+                       u32 dk[32];
+                       u8 cbc[8];
+               } des;
+       } u;
+};
+
+
+struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
+                                         const u8 *iv, const u8 *key,
+                                         size_t key_len)
+{
+       struct crypto_cipher *ctx;
+
+       ctx = os_zalloc(sizeof(*ctx));
+       if (ctx == NULL)
+               return NULL;
+
+       ctx->alg = alg;
+
+       switch (alg) {
+       case CRYPTO_CIPHER_ALG_RC4:
+               if (key_len > sizeof(ctx->u.rc4.key)) {
+                       os_free(ctx);
+                       return NULL;
+               }
+               ctx->u.rc4.keylen = key_len;
+               os_memcpy(ctx->u.rc4.key, key, key_len);
+               break;
+       case CRYPTO_CIPHER_ALG_AES:
+               if (key_len > sizeof(ctx->u.aes.cbc)) {
+                       os_free(ctx);
+                       return NULL;
+               }
+               ctx->u.aes.ctx_enc = aes_encrypt_init(key, key_len);
+               if (ctx->u.aes.ctx_enc == NULL) {
+                       os_free(ctx);
+                       return NULL;
+               }
+               ctx->u.aes.ctx_dec = aes_decrypt_init(key, key_len);
+               if (ctx->u.aes.ctx_dec == NULL) {
+                       aes_encrypt_deinit(ctx->u.aes.ctx_enc);
+                       os_free(ctx);
+                       return NULL;
+               }
+               ctx->u.aes.block_size = key_len;
+               os_memcpy(ctx->u.aes.cbc, iv, ctx->u.aes.block_size);
+               break;
+       case CRYPTO_CIPHER_ALG_3DES:
+               if (key_len != 24) {
+                       os_free(ctx);
+                       return NULL;
+               }
+               des3_key_setup(key, &ctx->u.des3.key);
+               os_memcpy(ctx->u.des3.cbc, iv, 8);
+               break;
+       case CRYPTO_CIPHER_ALG_DES:
+               if (key_len != 8) {
+                       os_free(ctx);
+                       return NULL;
+               }
+               des_key_setup(key, ctx->u.des.ek, ctx->u.des.dk);
+               os_memcpy(ctx->u.des.cbc, iv, 8);
+               break;
+       default:
+               os_free(ctx);
+               return NULL;
+       }
+
+       return ctx;
+}
+
+
+int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain,
+                         u8 *crypt, size_t len)
+{
+       size_t i, j, blocks;
+
+       switch (ctx->alg) {
+       case CRYPTO_CIPHER_ALG_RC4:
+               if (plain != crypt)
+                       os_memcpy(crypt, plain, len);
+               rc4_skip(ctx->u.rc4.key, ctx->u.rc4.keylen,
+                        ctx->u.rc4.used_bytes, crypt, len);
+               ctx->u.rc4.used_bytes += len;
+               break;
+       case CRYPTO_CIPHER_ALG_AES:
+               if (len % ctx->u.aes.block_size)
+                       return -1;
+               blocks = len / ctx->u.aes.block_size;
+               for (i = 0; i < blocks; i++) {
+                       for (j = 0; j < ctx->u.aes.block_size; j++)
+                               ctx->u.aes.cbc[j] ^= plain[j];
+                       aes_encrypt(ctx->u.aes.ctx_enc, ctx->u.aes.cbc,
+                                   ctx->u.aes.cbc);
+                       os_memcpy(crypt, ctx->u.aes.cbc,
+                                 ctx->u.aes.block_size);
+                       plain += ctx->u.aes.block_size;
+                       crypt += ctx->u.aes.block_size;
+               }
+               break;
+       case CRYPTO_CIPHER_ALG_3DES:
+               if (len % 8)
+                       return -1;
+               blocks = len / 8;
+               for (i = 0; i < blocks; i++) {
+                       for (j = 0; j < 8; j++)
+                               ctx->u.des3.cbc[j] ^= plain[j];
+                       des3_encrypt(ctx->u.des3.cbc, &ctx->u.des3.key,
+                                    ctx->u.des3.cbc);
+                       os_memcpy(crypt, ctx->u.des3.cbc, 8);
+                       plain += 8;
+                       crypt += 8;
+               }
+               break;
+       case CRYPTO_CIPHER_ALG_DES:
+               if (len % 8)
+                       return -1;
+               blocks = len / 8;
+               for (i = 0; i < blocks; i++) {
+                       for (j = 0; j < 8; j++)
+                               ctx->u.des3.cbc[j] ^= plain[j];
+                       des_block_encrypt(ctx->u.des.cbc, ctx->u.des.ek,
+                                         ctx->u.des.cbc);
+                       os_memcpy(crypt, ctx->u.des.cbc, 8);
+                       plain += 8;
+                       crypt += 8;
+               }
+               break;
+       default:
+               return -1;
+       }
+
+       return 0;
+}
+
+
+int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt,
+                         u8 *plain, size_t len)
+{
+       size_t i, j, blocks;
+       u8 tmp[32];
+
+       switch (ctx->alg) {
+       case CRYPTO_CIPHER_ALG_RC4:
+               if (plain != crypt)
+                       os_memcpy(plain, crypt, len);
+               rc4_skip(ctx->u.rc4.key, ctx->u.rc4.keylen,
+                        ctx->u.rc4.used_bytes, plain, len);
+               ctx->u.rc4.used_bytes += len;
+               break;
+       case CRYPTO_CIPHER_ALG_AES:
+               if (len % ctx->u.aes.block_size)
+                       return -1;
+               blocks = len / ctx->u.aes.block_size;
+               for (i = 0; i < blocks; i++) {
+                       os_memcpy(tmp, crypt, ctx->u.aes.block_size);
+                       aes_decrypt(ctx->u.aes.ctx_dec, crypt, plain);
+                       for (j = 0; j < ctx->u.aes.block_size; j++)
+                               plain[j] ^= ctx->u.aes.cbc[j];
+                       os_memcpy(ctx->u.aes.cbc, tmp, ctx->u.aes.block_size);
+                       plain += ctx->u.aes.block_size;
+                       crypt += ctx->u.aes.block_size;
+               }
+               break;
+       case CRYPTO_CIPHER_ALG_3DES:
+               if (len % 8)
+                       return -1;
+               blocks = len / 8;
+               for (i = 0; i < blocks; i++) {
+                       os_memcpy(tmp, crypt, 8);
+                       des3_decrypt(crypt, &ctx->u.des3.key, plain);
+                       for (j = 0; j < 8; j++)
+                               plain[j] ^= ctx->u.des3.cbc[j];
+                       os_memcpy(ctx->u.des3.cbc, tmp, 8);
+                       plain += 8;
+                       crypt += 8;
+               }
+               break;
+       case CRYPTO_CIPHER_ALG_DES:
+               if (len % 8)
+                       return -1;
+               blocks = len / 8;
+               for (i = 0; i < blocks; i++) {
+                       os_memcpy(tmp, crypt, 8);
+                       des_block_decrypt(crypt, ctx->u.des.dk, plain);
+                       for (j = 0; j < 8; j++)
+                               plain[j] ^= ctx->u.des.cbc[j];
+                       os_memcpy(ctx->u.des.cbc, tmp, 8);
+                       plain += 8;
+                       crypt += 8;
+               }
+               break;
+       default:
+               return -1;
+       }
+
+       return 0;
+}
+
+
+void crypto_cipher_deinit(struct crypto_cipher *ctx)
+{
+       switch (ctx->alg) {
+       case CRYPTO_CIPHER_ALG_AES:
+               aes_encrypt_deinit(ctx->u.aes.ctx_enc);
+               aes_decrypt_deinit(ctx->u.aes.ctx_dec);
+               break;
+       case CRYPTO_CIPHER_ALG_3DES:
+               break;
+       default:
+               break;
+       }
+       os_free(ctx);
+}
diff --git a/src/crypto/crypto_internal-modexp.c b/src/crypto/crypto_internal-modexp.c
new file mode 100644 (file)
index 0000000..3124742
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * Crypto wrapper for internal crypto implementation - modexp
+ * Copyright (c) 2006-2009, Jouni Malinen <j@w1.fi>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * Alternatively, this software may be distributed under the terms of BSD
+ * license.
+ *
+ * See README and COPYING for more details.
+ */
+
+#include "includes.h"
+
+#include "common.h"
+#include "tls/bignum.h"
+#include "crypto.h"
+
+
+int crypto_mod_exp(const u8 *base, size_t base_len,
+                  const u8 *power, size_t power_len,
+                  const u8 *modulus, size_t modulus_len,
+                  u8 *result, size_t *result_len)
+{
+       struct bignum *bn_base, *bn_exp, *bn_modulus, *bn_result;
+       int ret = -1;
+
+       bn_base = bignum_init();
+       bn_exp = bignum_init();
+       bn_modulus = bignum_init();
+       bn_result = bignum_init();
+
+       if (bn_base == NULL || bn_exp == NULL || bn_modulus == NULL ||
+           bn_result == NULL)
+               goto error;
+
+       if (bignum_set_unsigned_bin(bn_base, base, base_len) < 0 ||
+           bignum_set_unsigned_bin(bn_exp, power, power_len) < 0 ||
+           bignum_set_unsigned_bin(bn_modulus, modulus, modulus_len) < 0)
+               goto error;
+
+       if (bignum_exptmod(bn_base, bn_exp, bn_modulus, bn_result) < 0)
+               goto error;
+
+       ret = bignum_get_unsigned_bin(bn_result, result, result_len);
+
+error:
+       bignum_deinit(bn_base);
+       bignum_deinit(bn_exp);
+       bignum_deinit(bn_modulus);
+       bignum_deinit(bn_result);
+       return ret;
+}
diff --git a/src/crypto/crypto_internal-rsa.c b/src/crypto/crypto_internal-rsa.c
new file mode 100644 (file)
index 0000000..205042c
--- /dev/null
@@ -0,0 +1,115 @@
+/*
+ * Crypto wrapper for internal crypto implementation - RSA parts
+ * Copyright (c) 2006-2009, Jouni Malinen <j@w1.fi>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * Alternatively, this software may be distributed under the terms of BSD
+ * license.
+ *
+ * See README and COPYING for more details.
+ */
+
+#include "includes.h"
+
+#include "common.h"
+#include "crypto.h"
+#include "tls/rsa.h"
+#include "tls/bignum.h"
+#include "tls/pkcs1.h"
+#include "tls/pkcs8.h"
+
+/* Dummy structures; these are just typecast to struct crypto_rsa_key */
+struct crypto_public_key;
+struct crypto_private_key;
+
+
+struct crypto_public_key * crypto_public_key_import(const u8 *key, size_t len)
+{
+       return (struct crypto_public_key *)
+               crypto_rsa_import_public_key(key, len);
+}
+
+
+struct crypto_private_key * crypto_private_key_import(const u8 *key,
+                                                     size_t len,
+                                                     const char *passwd)
+{
+       struct crypto_private_key *res;
+
+       /* First, check for possible PKCS #8 encoding */
+       res = pkcs8_key_import(key, len);
+       if (res)
+               return res;
+
+       if (passwd) {
+               /* Try to parse as encrypted PKCS #8 */
+               res = pkcs8_enc_key_import(key, len, passwd);
+               if (res)
+                       return res;
+       }
+
+       /* Not PKCS#8, so try to import PKCS #1 encoded RSA private key */
+       wpa_printf(MSG_DEBUG, "Trying to parse PKCS #1 encoded RSA private "
+                  "key");
+       return (struct crypto_private_key *)
+               crypto_rsa_import_private_key(key, len);
+}
+
+
+struct crypto_public_key * crypto_public_key_from_cert(const u8 *buf,
+                                                      size_t len)
+{
+       /* No X.509 support in crypto_internal.c */
+       return NULL;
+}
+
+
+int crypto_public_key_encrypt_pkcs1_v15(struct crypto_public_key *key,
+                                       const u8 *in, size_t inlen,
+                                       u8 *out, size_t *outlen)
+{
+       return pkcs1_encrypt(2, (struct crypto_rsa_key *) key,
+                            0, in, inlen, out, outlen);
+}
+
+
+int crypto_private_key_decrypt_pkcs1_v15(struct crypto_private_key *key,
+                                        const u8 *in, size_t inlen,
+                                        u8 *out, size_t *outlen)
+{
+       return pkcs1_v15_private_key_decrypt((struct crypto_rsa_key *) key,
+                                            in, inlen, out, outlen);
+}
+
+
+int crypto_private_key_sign_pkcs1(struct crypto_private_key *key,
+                                 const u8 *in, size_t inlen,
+                                 u8 *out, size_t *outlen)
+{
+       return pkcs1_encrypt(1, (struct crypto_rsa_key *) key,
+                            1, in, inlen, out, outlen);
+}
+
+
+void crypto_public_key_free(struct crypto_public_key *key)
+{
+       crypto_rsa_free((struct crypto_rsa_key *) key);
+}
+
+
+void crypto_private_key_free(struct crypto_private_key *key)
+{
+       crypto_rsa_free((struct crypto_rsa_key *) key);
+}
+
+
+int crypto_public_key_decrypt_pkcs1(struct crypto_public_key *key,
+                                   const u8 *crypt, size_t crypt_len,
+                                   u8 *plain, size_t *plain_len)
+{
+       return pkcs1_decrypt_public_key((struct crypto_rsa_key *) key,
+                                       crypt, crypt_len, plain, plain_len);
+}
index 84d30bb..8fdba65 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * WPA Supplicant / Crypto wrapper for internal crypto implementation
+ * Crypto wrapper for internal crypto implementation
  * Copyright (c) 2006-2009, Jouni Malinen <j@w1.fi>
  *
  * This program is free software; you can redistribute it and/or modify
 
 #include "common.h"
 #include "crypto.h"
-#include "md5.h"
-#include "sha1.h"
-#include "aes.h"
-#include "tls/rsa.h"
-#include "tls/bignum.h"
-#include "tls/pkcs1.h"
-#include "tls/pkcs8.h"
 #include "sha1_i.h"
 #include "md5_i.h"
-#include "des_i.h"
 
 struct crypto_hash {
        enum crypto_hash_alg alg;
@@ -202,336 +194,6 @@ int crypto_hash_finish(struct crypto_hash *ctx, u8 *mac, size_t *len)
 }
 
 
-struct crypto_cipher {
-       enum crypto_cipher_alg alg;
-       union {
-               struct {
-                       size_t used_bytes;
-                       u8 key[16];
-                       size_t keylen;
-               } rc4;
-               struct {
-                       u8 cbc[32];
-                       size_t block_size;
-                       void *ctx_enc;
-                       void *ctx_dec;
-               } aes;
-               struct {
-                       struct des3_key_s key;
-                       u8 cbc[8];
-               } des3;
-               struct {
-                       u32 ek[32];
-                       u32 dk[32];
-                       u8 cbc[8];
-               } des;
-       } u;
-};
-
-
-struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
-                                         const u8 *iv, const u8 *key,
-                                         size_t key_len)
-{
-       struct crypto_cipher *ctx;
-
-       ctx = os_zalloc(sizeof(*ctx));
-       if (ctx == NULL)
-               return NULL;
-
-       ctx->alg = alg;
-
-       switch (alg) {
-       case CRYPTO_CIPHER_ALG_RC4:
-               if (key_len > sizeof(ctx->u.rc4.key)) {
-                       os_free(ctx);
-                       return NULL;
-               }
-               ctx->u.rc4.keylen = key_len;
-               os_memcpy(ctx->u.rc4.key, key, key_len);
-               break;
-       case CRYPTO_CIPHER_ALG_AES:
-               if (key_len > sizeof(ctx->u.aes.cbc)) {
-                       os_free(ctx);
-                       return NULL;
-               }
-               ctx->u.aes.ctx_enc = aes_encrypt_init(key, key_len);
-               if (ctx->u.aes.ctx_enc == NULL) {
-                       os_free(ctx);
-                       return NULL;
-               }
-               ctx->u.aes.ctx_dec = aes_decrypt_init(key, key_len);
-               if (ctx->u.aes.ctx_dec == NULL) {
-                       aes_encrypt_deinit(ctx->u.aes.ctx_enc);
-                       os_free(ctx);
-                       return NULL;
-               }
-               ctx->u.aes.block_size = key_len;
-               os_memcpy(ctx->u.aes.cbc, iv, ctx->u.aes.block_size);
-               break;
-       case CRYPTO_CIPHER_ALG_3DES:
-               if (key_len != 24) {
-                       os_free(ctx);
-                       return NULL;
-               }
-               des3_key_setup(key, &ctx->u.des3.key);
-               os_memcpy(ctx->u.des3.cbc, iv, 8);
-               break;
-       case CRYPTO_CIPHER_ALG_DES:
-               if (key_len != 8) {
-                       os_free(ctx);
-                       return NULL;
-               }
-               des_key_setup(key, ctx->u.des.ek, ctx->u.des.dk);
-               os_memcpy(ctx->u.des.cbc, iv, 8);
-               break;
-       default:
-               os_free(ctx);
-               return NULL;
-       }
-
-       return ctx;
-}
-
-
-int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain,
-                         u8 *crypt, size_t len)
-{
-       size_t i, j, blocks;
-
-       switch (ctx->alg) {
-       case CRYPTO_CIPHER_ALG_RC4:
-               if (plain != crypt)
-                       os_memcpy(crypt, plain, len);
-               rc4_skip(ctx->u.rc4.key, ctx->u.rc4.keylen,
-                        ctx->u.rc4.used_bytes, crypt, len);
-               ctx->u.rc4.used_bytes += len;
-               break;
-       case CRYPTO_CIPHER_ALG_AES:
-               if (len % ctx->u.aes.block_size)
-                       return -1;
-               blocks = len / ctx->u.aes.block_size;
-               for (i = 0; i < blocks; i++) {
-                       for (j = 0; j < ctx->u.aes.block_size; j++)
-                               ctx->u.aes.cbc[j] ^= plain[j];
-                       aes_encrypt(ctx->u.aes.ctx_enc, ctx->u.aes.cbc,
-                                   ctx->u.aes.cbc);
-                       os_memcpy(crypt, ctx->u.aes.cbc,
-                                 ctx->u.aes.block_size);
-                       plain += ctx->u.aes.block_size;
-                       crypt += ctx->u.aes.block_size;
-               }
-               break;
-       case CRYPTO_CIPHER_ALG_3DES:
-               if (len % 8)
-                       return -1;
-               blocks = len / 8;
-               for (i = 0; i < blocks; i++) {
-                       for (j = 0; j < 8; j++)
-                               ctx->u.des3.cbc[j] ^= plain[j];
-                       des3_encrypt(ctx->u.des3.cbc, &ctx->u.des3.key,
-                                    ctx->u.des3.cbc);
-                       os_memcpy(crypt, ctx->u.des3.cbc, 8);
-                       plain += 8;
-                       crypt += 8;
-               }
-               break;
-       case CRYPTO_CIPHER_ALG_DES:
-               if (len % 8)
-                       return -1;
-               blocks = len / 8;
-               for (i = 0; i < blocks; i++) {
-                       for (j = 0; j < 8; j++)
-                               ctx->u.des3.cbc[j] ^= plain[j];
-                       des_block_encrypt(ctx->u.des.cbc, ctx->u.des.ek,
-                                         ctx->u.des.cbc);
-                       os_memcpy(crypt, ctx->u.des.cbc, 8);
-                       plain += 8;
-                       crypt += 8;
-               }
-               break;
-       default:
-               return -1;
-       }
-
-       return 0;
-}
-
-
-int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt,
-                         u8 *plain, size_t len)
-{
-       size_t i, j, blocks;
-       u8 tmp[32];
-
-       switch (ctx->alg) {
-       case CRYPTO_CIPHER_ALG_RC4:
-               if (plain != crypt)
-                       os_memcpy(plain, crypt, len);
-               rc4_skip(ctx->u.rc4.key, ctx->u.rc4.keylen,
-                        ctx->u.rc4.used_bytes, plain, len);
-               ctx->u.rc4.used_bytes += len;
-               break;
-       case CRYPTO_CIPHER_ALG_AES:
-               if (len % ctx->u.aes.block_size)
-                       return -1;
-               blocks = len / ctx->u.aes.block_size;
-               for (i = 0; i < blocks; i++) {
-                       os_memcpy(tmp, crypt, ctx->u.aes.block_size);
-                       aes_decrypt(ctx->u.aes.ctx_dec, crypt, plain);
-                       for (j = 0; j < ctx->u.aes.block_size; j++)
-                               plain[j] ^= ctx->u.aes.cbc[j];
-                       os_memcpy(ctx->u.aes.cbc, tmp, ctx->u.aes.block_size);
-                       plain += ctx->u.aes.block_size;
-                       crypt += ctx->u.aes.block_size;
-               }
-               break;
-       case CRYPTO_CIPHER_ALG_3DES:
-               if (len % 8)
-                       return -1;
-               blocks = len / 8;
-               for (i = 0; i < blocks; i++) {
-                       os_memcpy(tmp, crypt, 8);
-                       des3_decrypt(crypt, &ctx->u.des3.key, plain);
-                       for (j = 0; j < 8; j++)
-                               plain[j] ^= ctx->u.des3.cbc[j];
-                       os_memcpy(ctx->u.des3.cbc, tmp, 8);
-                       plain += 8;
-                       crypt += 8;
-               }
-               break;
-       case CRYPTO_CIPHER_ALG_DES:
-               if (len % 8)
-                       return -1;
-               blocks = len / 8;
-               for (i = 0; i < blocks; i++) {
-                       os_memcpy(tmp, crypt, 8);
-                       des_block_decrypt(crypt, ctx->u.des.dk, plain);
-                       for (j = 0; j < 8; j++)
-                               plain[j] ^= ctx->u.des.cbc[j];
-                       os_memcpy(ctx->u.des.cbc, tmp, 8);
-                       plain += 8;
-                       crypt += 8;
-               }
-               break;
-       default:
-               return -1;
-       }
-
-       return 0;
-}
-
-
-void crypto_cipher_deinit(struct crypto_cipher *ctx)
-{
-       switch (ctx->alg) {
-       case CRYPTO_CIPHER_ALG_AES:
-               aes_encrypt_deinit(ctx->u.aes.ctx_enc);
-               aes_decrypt_deinit(ctx->u.aes.ctx_dec);
-               break;
-       case CRYPTO_CIPHER_ALG_3DES:
-               break;
-       default:
-               break;
-       }
-       os_free(ctx);
-}
-
-
-/* Dummy structures; these are just typecast to struct crypto_rsa_key */
-struct crypto_public_key;
-struct crypto_private_key;
-
-
-struct crypto_public_key * crypto_public_key_import(const u8 *key, size_t len)
-{
-       return (struct crypto_public_key *)
-               crypto_rsa_import_public_key(key, len);
-}
-
-
-struct crypto_private_key * crypto_private_key_import(const u8 *key,
-                                                     size_t len,
-                                                     const char *passwd)
-{
-       struct crypto_private_key *res;
-
-       /* First, check for possible PKCS #8 encoding */
-       res = pkcs8_key_import(key, len);
-       if (res)
-               return res;
-
-       if (passwd) {
-               /* Try to parse as encrypted PKCS #8 */
-               res = pkcs8_enc_key_import(key, len, passwd);
-               if (res)
-                       return res;
-       }
-
-       /* Not PKCS#8, so try to import PKCS #1 encoded RSA private key */
-       wpa_printf(MSG_DEBUG, "Trying to parse PKCS #1 encoded RSA private "
-                  "key");
-       return (struct crypto_private_key *)
-               crypto_rsa_import_private_key(key, len);
-}
-
-
-struct crypto_public_key * crypto_public_key_from_cert(const u8 *buf,
-                                                      size_t len)
-{
-       /* No X.509 support in crypto_internal.c */
-       return NULL;
-}
-
-
-int crypto_public_key_encrypt_pkcs1_v15(struct crypto_public_key *key,
-                                       const u8 *in, size_t inlen,
-                                       u8 *out, size_t *outlen)
-{
-       return pkcs1_encrypt(2, (struct crypto_rsa_key *) key,
-                            0, in, inlen, out, outlen);
-}
-
-
-int crypto_private_key_decrypt_pkcs1_v15(struct crypto_private_key *key,
-                                        const u8 *in, size_t inlen,
-                                        u8 *out, size_t *outlen)
-{
-       return pkcs1_v15_private_key_decrypt((struct crypto_rsa_key *) key,
-                                            in, inlen, out, outlen);
-}
-
-
-int crypto_private_key_sign_pkcs1(struct crypto_private_key *key,
-                                 const u8 *in, size_t inlen,
-                                 u8 *out, size_t *outlen)
-{
-       return pkcs1_encrypt(1, (struct crypto_rsa_key *) key,
-                            1, in, inlen, out, outlen);
-}
-
-
-void crypto_public_key_free(struct crypto_public_key *key)
-{
-       crypto_rsa_free((struct crypto_rsa_key *) key);
-}
-
-
-void crypto_private_key_free(struct crypto_private_key *key)
-{
-       crypto_rsa_free((struct crypto_rsa_key *) key);
-}
-
-
-int crypto_public_key_decrypt_pkcs1(struct crypto_public_key *key,
-                                   const u8 *crypt, size_t crypt_len,
-                                   u8 *plain, size_t *plain_len)
-{
-       return pkcs1_decrypt_public_key((struct crypto_rsa_key *) key,
-                                       crypt, crypt_len, plain, plain_len);
-}
-
-
 int crypto_global_init(void)
 {
        return 0;
@@ -541,43 +203,3 @@ int crypto_global_init(void)
 void crypto_global_deinit(void)
 {
 }
-
-
-#ifdef CONFIG_MODEXP
-
-int crypto_mod_exp(const u8 *base, size_t base_len,
-                  const u8 *power, size_t power_len,
-                  const u8 *modulus, size_t modulus_len,
-                  u8 *result, size_t *result_len)
-{
-       struct bignum *bn_base, *bn_exp, *bn_modulus, *bn_result;
-       int ret = -1;
-
-       bn_base = bignum_init();
-       bn_exp = bignum_init();
-       bn_modulus = bignum_init();
-       bn_result = bignum_init();
-
-       if (bn_base == NULL || bn_exp == NULL || bn_modulus == NULL ||
-           bn_result == NULL)
-               goto error;
-
-       if (bignum_set_unsigned_bin(bn_base, base, base_len) < 0 ||
-           bignum_set_unsigned_bin(bn_exp, power, power_len) < 0 ||
-           bignum_set_unsigned_bin(bn_modulus, modulus, modulus_len) < 0)
-               goto error;
-
-       if (bignum_exptmod(bn_base, bn_exp, bn_modulus, bn_result) < 0)
-               goto error;
-
-       ret = bignum_get_unsigned_bin(bn_result, result, result_len);
-
-error:
-       bignum_deinit(bn_base);
-       bignum_deinit(bn_exp);
-       bignum_deinit(bn_modulus);
-       bignum_deinit(bn_result);
-       return ret;
-}
-
-#endif /* CONFIG_MODEXP */
index efda353..40a1d11 100644 (file)
@@ -523,6 +523,7 @@ CONFIG_IEEE8021X_EAPOL=y
 NEED_DH_GROUPS=y
 NEED_DH_GROUPS_ALL=y
 NEED_MODEXP=y
+NEED_CIPHER=y
 endif
 
 ifdef CONFIG_EAP_VENDOR_TEST
@@ -772,6 +773,7 @@ ifndef CONFIG_CRYPTO
 CONFIG_CRYPTO=internal
 endif
 ifdef TLS_FUNCS
+OBJS += ../src/crypto/crypto_internal-rsa.o
 OBJS += ../src/crypto/tls_internal.o
 OBJS += ../src/tls/tlsv1_common.o
 OBJS += ../src/tls/tlsv1_record.o
@@ -785,20 +787,19 @@ OBJS += ../src/tls/x509v3.o
 OBJS += ../src/tls/pkcs1.o
 OBJS += ../src/tls/pkcs5.o
 OBJS += ../src/tls/pkcs8.o
-OBJS_p += ../src/tls/asn1.o
-OBJS_p += ../src/tls/rsa.o
-OBJS_p += ../src/tls/pkcs1.o
-OBJS_p += ../src/tls/pkcs5.o
-OBJS_p += ../src/tls/pkcs8.o
-OBJS_p += ../src/crypto/rc4.o
 NEED_BASE64=y
 NEED_TLS_PRF=y
 NEED_MODEXP=y
+NEED_CIPHER=y
 CFLAGS += -DCONFIG_TLS_INTERNAL_CLIENT
 endif
+ifdef NEED_CIPHER
+NEED_DES=y
+OBJS += ../src/crypto/crypto_internal-cipher.o
+endif
 ifdef NEED_MODEXP
+OBJS += ../src/crypto/crypto_internal-modexp.o
 OBJS += ../src/tls/bignum.o
-OBJS_p += ../src/tls/bignum.o
 endif
 ifeq ($(CONFIG_CRYPTO), libtomcrypt)
 CFLAGS += -DCONFIG_INTERNAL_X509
@@ -877,10 +878,6 @@ CONFIG_INTERNAL_RC4=y
 endif
 endif
 
-ifdef NEED_MODEXP
-CFLAGS += -DCONFIG_MODEXP
-endif
-
 AESOBJS = # none so far (see below)
 ifdef CONFIG_INTERNAL_AES
 AESOBJS += ../src/crypto/aes-internal.o ../src/crypto/aes-internal-dec.o
@@ -916,9 +913,6 @@ endif
 endif
 ifdef NEED_AES
 OBJS += $(AESOBJS)
-ifdef CONFIG_INTERNAL_AES
-OBJS_p += $(AESOBJS)
-endif
 endif
 
 ifdef NEED_SHA1
@@ -1133,7 +1127,7 @@ endif
 OBJS += ../src/drivers/scan_helpers.o
 OBJS += $(SHA1OBJS) $(DESOBJS)
 
-OBJS_p += $(SHA1OBJS) $(DESOBJS)
+OBJS_p += $(SHA1OBJS)
 
 ifdef CONFIG_BGSCAN_SIMPLE
 CFLAGS += -DCONFIG_BGSCAN_SIMPLE