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