00001
00016 #include "includes.h"
00017 #include <nspr/prtypes.h>
00018 #include <nspr/plarenas.h>
00019 #include <nspr/plhash.h>
00020 #include <nspr/prtime.h>
00021 #include <nspr/prinrval.h>
00022 #include <nspr/prclist.h>
00023 #include <nspr/prlock.h>
00024 #include <nss/sechash.h>
00025 #include <nss/pk11pub.h>
00026
00027 #include "common.h"
00028 #include "crypto.h"
00029
00030
00031 static int nss_hash(HASH_HashType type, unsigned int max_res_len,
00032 size_t num_elem, const u8 *addr[], const size_t *len,
00033 u8 *mac)
00034 {
00035 HASHContext *ctx;
00036 size_t i;
00037 unsigned int reslen;
00038
00039 ctx = HASH_Create(type);
00040 if (ctx == NULL)
00041 return -1;
00042
00043 HASH_Begin(ctx);
00044 for (i = 0; i < num_elem; i++)
00045 HASH_Update(ctx, addr[i], len[i]);
00046 HASH_End(ctx, mac, &reslen, max_res_len);
00047 HASH_Destroy(ctx);
00048
00049 return 0;
00050 }
00051
00052
00053 void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
00054 {
00055 PK11Context *ctx = NULL;
00056 PK11SlotInfo *slot;
00057 SECItem *param = NULL;
00058 PK11SymKey *symkey = NULL;
00059 SECItem item;
00060 int olen;
00061 u8 pkey[8], next, tmp;
00062 int i;
00063
00064
00065 next = 0;
00066 for (i = 0; i < 7; i++) {
00067 tmp = key[i];
00068 pkey[i] = (tmp >> i) | next | 1;
00069 next = tmp << (7 - i);
00070 }
00071 pkey[i] = next | 1;
00072
00073 slot = PK11_GetBestSlot(CKM_DES_ECB, NULL);
00074 if (slot == NULL) {
00075 wpa_printf(MSG_ERROR, "NSS: PK11_GetBestSlot failed");
00076 goto out;
00077 }
00078
00079 item.type = siBuffer;
00080 item.data = pkey;
00081 item.len = 8;
00082 symkey = PK11_ImportSymKey(slot, CKM_DES_ECB, PK11_OriginDerive,
00083 CKA_ENCRYPT, &item, NULL);
00084 if (symkey == NULL) {
00085 wpa_printf(MSG_ERROR, "NSS: PK11_ImportSymKey failed");
00086 goto out;
00087 }
00088
00089 param = PK11_GenerateNewParam(CKM_DES_ECB, symkey);
00090 if (param == NULL) {
00091 wpa_printf(MSG_ERROR, "NSS: PK11_GenerateNewParam failed");
00092 goto out;
00093 }
00094
00095 ctx = PK11_CreateContextBySymKey(CKM_DES_ECB, CKA_ENCRYPT,
00096 symkey, param);
00097 if (ctx == NULL) {
00098 wpa_printf(MSG_ERROR, "NSS: PK11_CreateContextBySymKey("
00099 "CKM_DES_ECB) failed");
00100 goto out;
00101 }
00102
00103 if (PK11_CipherOp(ctx, cypher, &olen, 8, (void *) clear, 8) !=
00104 SECSuccess) {
00105 wpa_printf(MSG_ERROR, "NSS: PK11_CipherOp failed");
00106 goto out;
00107 }
00108
00109 out:
00110 if (ctx)
00111 PK11_DestroyContext(ctx, PR_TRUE);
00112 if (symkey)
00113 PK11_FreeSymKey(symkey);
00114 if (param)
00115 SECITEM_FreeItem(param, PR_TRUE);
00116 }
00117
00118
00119 int rc4_skip(const u8 *key, size_t keylen, size_t skip,
00120 u8 *data, size_t data_len)
00121 {
00122 return -1;
00123 }
00124
00125
00126 int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
00127 {
00128 return nss_hash(HASH_AlgMD5, 16, num_elem, addr, len, mac);
00129 }
00130
00131
00132 int sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
00133 {
00134 return nss_hash(HASH_AlgSHA1, 20, num_elem, addr, len, mac);
00135 }
00136
00137
00138 int sha256_vector(size_t num_elem, const u8 *addr[], const size_t *len,
00139 u8 *mac)
00140 {
00141 return nss_hash(HASH_AlgSHA256, 32, num_elem, addr, len, mac);
00142 }
00143
00144
00145 void * aes_encrypt_init(const u8 *key, size_t len)
00146 {
00147 return NULL;
00148 }
00149
00150
00151 void aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
00152 {
00153 }
00154
00155
00156 void aes_encrypt_deinit(void *ctx)
00157 {
00158 }
00159
00160
00161 void * aes_decrypt_init(const u8 *key, size_t len)
00162 {
00163 return NULL;
00164 }
00165
00166
00167 void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
00168 {
00169 }
00170
00171
00172 void aes_decrypt_deinit(void *ctx)
00173 {
00174 }
00175
00176
00177 int crypto_mod_exp(const u8 *base, size_t base_len,
00178 const u8 *power, size_t power_len,
00179 const u8 *modulus, size_t modulus_len,
00180 u8 *result, size_t *result_len)
00181 {
00182 return -1;
00183 }
00184
00185
00186 struct crypto_cipher {
00187 };
00188
00189
00190 struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
00191 const u8 *iv, const u8 *key,
00192 size_t key_len)
00193 {
00194 return NULL;
00195 }
00196
00197
00198 int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain,
00199 u8 *crypt, size_t len)
00200 {
00201 return -1;
00202 }
00203
00204
00205 int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt,
00206 u8 *plain, size_t len)
00207 {
00208 return -1;
00209 }
00210
00211
00212 void crypto_cipher_deinit(struct crypto_cipher *ctx)
00213 {
00214 }
00215