/* * Copyright (C) 2006 Richard Kotal * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Library General Public License as published * by the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. */ static int ns_mhash_KeygenCreate (Tcl_Interp * interp, int bin, char *algoname, char *pass, mutils_word32 keysize, char *salt, int count, int hashc, Tcl_Obj * hashv[]) { int hashid = -1; mutils_word32 passlen = 0; mutils_word32 tmp = 0; mutils_word32 salt_size = 0; mutils_word32 salt_size_str = 0; mutils_word8 *salt2 = NULL; mutils_word8 *key = NULL; mutils_word8 *buf = NULL; KEYGEN data; int i = 0; if (interp == NULL) return TCL_ERROR; hashid = ns_mhash_GetKeygenId (algoname); if (hashid == -1) { Tcl_SetResult (interp, "Bad keygen algorithm name.", TCL_STATIC); return TCL_ERROR; } if (pass == NULL) { Tcl_SetResult (interp, "Password is NULL.", TCL_STATIC); return TCL_ERROR; } passlen = strlen (pass); if ((tmp = mhash_get_keygen_max_key_size (hashid)) != 0) keysize = tmp; key = (mutils_word8 *) mutils_malloc (keysize); if (salt != NULL) { if ((tmp = mhash_get_keygen_salt_size (hashid)) != 0) salt_size = tmp; salt_size_str = strlen (salt); if (salt_size == 0) salt_size = salt_size_str; salt2 = (mutils_word8 *) mutils_malloc (salt_size + 1); bzero (salt2, salt_size + 1); memcpy (salt2, salt, (salt_size > salt_size_str) ? salt_size_str : salt_size); } data.count = count; data.salt = salt2; data.salt_size = salt_size; for (i = 0; i < MAX_KEYGEN_HASH_ALGO && hashc > i; i++) { tmp = -1; if (hashc > i) tmp = ns_mhash_GetHashId (Tcl_GetString (hashv[i])); if (tmp == -1) continue; data.hash_algorithm[i] = tmp; } mhash_keygen_ext (hashid, data, key, keysize, pass, passlen); switch (bin) { case 2: { // int len = (1 + (keysize * 4) / 3); int len = (1 + (keysize * 2) ); unsigned char b64[len]; bzero(b64,len); Ns_HtuuEncode ((unsigned char *) key, keysize, b64); Tcl_AppendResult (interp, b64, NULL); break; } case 1: { Tcl_Obj *bytearray = NULL; bytearray = Tcl_NewByteArrayObj (key, keysize); Tcl_SetObjResult (interp, bytearray); break; } default: { buf = mutils_asciify (key, keysize); Tcl_AppendResult (interp, buf, NULL); mutils_free (buf); break; } } mutils_free (key); if (salt2 != NULL) mutils_free (salt2); return TCL_OK; } #define N 1024 static int ns_mhash_KeygenCapa (Tcl_Interp * interp, char *name) { int hashid = -1; int algo_hash_count = 0; int algo_salt = 0; int algo_salt_size = 0; int algo_key_size = 0; int algo_count = 0; char buf[N]; if (interp == NULL) return TCL_ERROR; if (name == NULL) { Tcl_SetResult (interp, "Keygen algorithm name is NULL.", TCL_STATIC); return TCL_ERROR; } hashid = ns_mhash_GetKeygenId (name); if (hashid == -1) { Tcl_SetResult (interp, "Bad keygen algorithm name.", TCL_STATIC); return TCL_ERROR; } algo_salt = mhash_keygen_uses_salt (hashid); memset(buf,'\0',N); sprintf(buf, "%d", algo_salt); Tcl_AppendElement (interp, buf); algo_salt_size = mhash_get_keygen_salt_size (hashid); memset(buf,'\0',N); sprintf(buf, "%d", algo_salt_size); Tcl_AppendElement (interp, buf); algo_key_size = mhash_get_keygen_max_key_size (hashid); memset(buf,'\0',N); sprintf(buf, "%d", algo_key_size); Tcl_AppendElement (interp, buf); algo_count = mhash_keygen_uses_count (hashid); memset(buf,'\0',N); sprintf(buf, "%d", algo_count); Tcl_AppendElement (interp, buf); algo_hash_count = mhash_keygen_uses_hash_algorithm (hashid); memset(buf,'\0',N); sprintf(buf, "%d", algo_hash_count); Tcl_AppendElement (interp, buf); return TCL_OK; } static int ns_mhash_KeygenAlgo (Tcl_Interp * interp) { size_t len = 0; int i = 0; __const mutils_word8 *algo = NULL; if (interp == NULL) return TCL_ERROR; len = mhash_keygen_count (); for (i = 0; i <= len; i++) { algo = mhash_get_keygen_name_static (i); if (algo == NULL) continue; Tcl_AppendElement (interp, (char *) algo); } return TCL_OK; }