Пример #1
0
int
rasqal_digest_buffer(rasqal_digest_type type, const unsigned char *output,
                     const unsigned char * const input, size_t len)
{
  hashid hash_type;
  unsigned int output_len;
  MHASH m;
  
  if(type > RASQAL_DIGEST_LAST)
    return -1;
  
  hash_type = rasqal_digest_to_hashid[type];
  if(hash_type == (hashid)-1)
    return -1;
  
  output_len = mhash_get_block_size(hash_type);
  if(!input)
    return output_len;
  
  m = mhash_init(hash_type);
  if(m == MHASH_FAILED)
    return -1;

  mhash(m, (const void *)input, len);
  
  mhash_deinit(m, (void*)output);

  return output_len;
}
Пример #2
0
void hash_given_data( void* data, size_t data_size) {
MHASH td;
byte _res[20];
int i;

	td = mhash_init( MHASH_SHA1);
	if (td==MHASH_FAILED) {
		err_quit(_("mhash_init() failed."));
	}

	/* also hash the pool
	 */
	mhash( td, rnd_pool, 20);
	
	mhash(td, data, data_size);

	mhash_deinit( td, _res);

	/* addition may do as well as xor
	 */
	for(i=0;i<20;i++) rnd_pool[i] ^= _res[i];


	/* Step 1 was completed. The pool was updated.
	 */
	 
}
Пример #3
0
void checksum_block(char *buf, int len, unsigned char *digest)
{
	td = mhash_init(HASH_FUNC);
	if (td == MHASH_FAILED)
		abort();

	mhash(td, buf, len);
	mhash_deinit(td, digest);
}
/*
 * ******************************************* 
 *  Function: get_crc 
 *
 *  Description: gets the crc of a packet   
 *   
 *  Parameters : 
 *     buffer - link layer buffer 
 *     len    - length of buffer 
 *  
 * ******************************************* 
 */
uint32_t get_crc(void *buffer , int len)
{
     
	MHASH td;
	uint32_t crc;
	td = mhash_init(MHASH_CRC32);
        mhash(td,buffer,len); 
        mhash_deinit(td,&crc);
	return crc; 
 

}
Пример #5
0
char * compute_hash(FILE* file)
{
	MHASH td;
	unsigned char buffer;
	unsigned char hash[16];
	
	td = mhash_init (MHASH_MD5);
	
	while (fread (&buffer, 1, 1, file) == 1)
    {
		mhash (td, &buffer, 1);
    }
	mhash_deinit (td, hash);
	return hash;
}
Пример #6
0
int sha1passwdok(const char *pw) {
	unsigned char out[mhash_get_block_size(MHASH_SHA1)];
	char outstr[mhash_get_block_size(MHASH_SHA1)*2+1];
	int i;
	MHASH td;
	td=mhash_init(MHASH_SHA1);
	mhash(td, pw, strlen(pw));
	mhash_deinit(td, out);
	for (i=0; i<mhash_get_block_size(MHASH_SHA1); i++) {
		outstr[2*i]=hex[out[i] >> 4];
		outstr[2*i+1]=hex[out[i] & 0xf];
	}
	outstr[2*i]=0;
	return (memcmp(outstr,passwd,mhash_get_block_size(MHASH_SHA1))==0);
}
Пример #7
0
bool gtkhash_hash_lib_mhash_is_supported(const enum hash_func_e id)
{
	struct hash_lib_mhash_s data;

	if (!gtkhash_hash_lib_mhash_set_type(id, &data.type))
		return false;

	if (G_UNLIKELY((data.thread = mhash_init(data.type)) == MHASH_FAILED)) {
		g_warning("mhash_init failed (%d)", id);
		return false;
	}

	mhash_deinit(data.thread, NULL);

	return true;
}
Пример #8
0
int ddfs_calculate_hash(char * dst, char * src, int len) {
	//memcpy(dst, src, DDFS_HASH_LEN);
	//return 0;

    /*
	md5_state_t state;
	md5_init(&state);
	md5_append(&state, src , len);
	md5_finish(&state, dst);
    */

    MHASH td = mhash_init(MHASH_TIGER);
    mhash(td, src, len);
    mhash_deinit(td, dst);
	return 0;

}
Пример #9
0
static int MD4BlockChecksum( void *buffer, int length )
{
	MHASH	mh;
	int		digest[ 4 ], checksum;
	
	
	/* make md4 hash */
	mh = mhash_init( MHASH_MD4 );
	if( !mh )
		Error( "Unable to initialize MD4 hash context" );
	mhash( mh, buffer, length );
	mhash_deinit( mh, digest );
	
	/* xor the bits and return */
	checksum = digest[ 0 ] ^ digest[ 1 ] ^ digest[ 2 ] ^ digest[ 3 ];
	return checksum;
}
Пример #10
0
WIN32DLL_DEFINE
void *mhash_end_m(MHASH td, void *(*hash_malloc) (mutils_word32))
{
    void *digest;
    mutils_word32 size;

    size = mhash_get_block_size( td->algorithm_given);

    digest = mutils_malloc(size);

    if (digest==NULL)
    {
        return NULL;
    }

    mhash_deinit( td, digest);

    return(digest);
}
Пример #11
0
static const char *
vmod_hash_sha256(const struct vrt_ctx *ctx, const char *msg)
{
	MHASH td;
	hashid hash = MHASH_SHA256;
	unsigned char h[mhash_get_block_size(hash)];
	int i;
	char *p;
	char *ptmp;
	td = mhash_init(hash);
	mhash(td, msg, strlen(msg));
	mhash_deinit(td, h);
	p = WS_Alloc(ctx->ws,mhash_get_block_size(hash)*2 + 1);
	ptmp = p;
	for (i = 0; i<mhash_get_block_size(hash);i++) {
		sprintf(ptmp,"%.2x",h[i]);
		ptmp+=2;
	}
	return p;
}
Пример #12
0
std::string
MD5::md5_file(const std::string& filename)
{
  unsigned char hash[16]; /* enough size for MD5 */
  MHASH td = mhash_init(MHASH_MD5);
  if (td == MHASH_FAILED) 
  {
    throw std::runtime_error("Failed to init MHash");
  }
  else
  {  
    const unsigned int buf_size = 32768;
    char buf[buf_size];
    std::ifstream in(filename.c_str(), std::ios::in | std::ios::binary); 
    if (!in)
    {
      throw std::runtime_error("MD5::md5_file(): Couldn't open file " + filename);
    }
    else
    {
    
      while(!in.eof())
      {
        in.read(buf, buf_size);
        mhash(td, buf, in.gcount());
      }

      in.close();
    
      mhash_deinit(td, hash);

      // Convert to string representation
      std::ostringstream out;
      for (int i = 0; i < 16; i++)
        out << std::setfill('0') << std::setw(2) << std::hex << int(hash[i]);

      return out.str();  
    }
  }
}
Пример #13
0
std::string
MD5::md5_string(const std::string& str)
{
  unsigned char hash[16]; /* enough size for MD5 */
  MHASH td = mhash_init(MHASH_MD5);
  if (td == MHASH_FAILED)
  {
    throw std::runtime_error("MD5::md5_string(): Failed to init MHash");
  }
  else
  {  
    mhash(td, str.c_str(), str.length());
  
    mhash_deinit(td, hash);

    // Convert to string representation
    std::ostringstream out;
    for (int i = 0; i < 16; i++) 
      out << std::setfill('0') << std::setw(2) << std::hex << int(hash[i]);

    return out.str();
  }
}
Пример #14
0
char * map_calculate_hash(struct map * map)
{
  char * rv;

  rv = NULL;
  assert(map != NULL);

  unsigned char * hash;
  int             hash_size;

  hash      = NULL;
  hash_size = 0;
  
#if HAVE_LIBMHASH
  hash_size = mhash_get_block_size(MHASH_MD5);
  hash = malloc(hash_size);
  assert(hash != NULL);
  if(hash != NULL)
    {
      MHASH td;

      td = mhash_init(MHASH_MD5);
      if(td != MHASH_FAILED)
        { /* Hash together all the relevant data. */
          mhash(td, map->cave_name,        strlen(map->cave_name)     );
          mhash(td, &map->level,           sizeof map->level          );
          mhash(td, &map->is_intermission, sizeof map->is_intermission);
          mhash(td, &map->width,           sizeof map->width          );
          mhash(td, &map->height,          sizeof map->height         );
          mhash(td, &map->start_x,         sizeof map->start_x        );
          mhash(td, &map->start_y,         sizeof map->start_y        );
          mhash(td, &map->exit_x,          sizeof map->exit_x         );
          mhash(td, &map->exit_y,          sizeof map->exit_y         );
          mhash(td, &map->diamonds,        sizeof map->diamonds       );
          mhash(td, &map->diamonds_needed, sizeof map->diamonds_needed);
          mhash(td, &map->diamond_score,   sizeof map->diamond_score  );
          mhash(td, &map->time_score,      sizeof map->time_score     );
          mhash(td, map->data,             map->width * map->height   );
          mhash(td, &map->time,            sizeof map->time           );
          mhash(td, &map->ameba_time,      sizeof map->ameba_time     );
          mhash(td, &map->game_speed,      sizeof map->game_speed     );
          
          mhash_deinit(td, hash);
        }
      else
        {
          free(hash);
          hash = NULL;
        }
    }

#elif HAVE_LIBCRYPTO
  MD5_CTX context;

  hash_size = MD5_DIGEST_LENGTH;
  hash = malloc(hash_size);
  assert(hash != NULL);
  if(hash != NULL)
    {
      if(MD5_Init(&context))
        {
          int success;
          
          success             = MD5_Update(&context, map->cave_name,        strlen(map->cave_name)     );
          if(success) success = MD5_Update(&context, &map->level,           sizeof map->level          );
          if(success) success = MD5_Update(&context, &map->is_intermission, sizeof map->is_intermission);
          if(success) success = MD5_Update(&context, &map->width,           sizeof map->width          );
          if(success) success = MD5_Update(&context, &map->height,          sizeof map->height         );
          if(success) success = MD5_Update(&context, &map->start_x,         sizeof map->start_x        );
          if(success) success = MD5_Update(&context, &map->start_y,         sizeof map->start_y        );
          if(success) success = MD5_Update(&context, &map->exit_x,          sizeof map->exit_x         );
          if(success) success = MD5_Update(&context, &map->exit_y,          sizeof map->exit_y         );
          if(success) success = MD5_Update(&context, &map->diamonds,        sizeof map->diamonds       );
          if(success) success = MD5_Update(&context, &map->diamonds_needed, sizeof map->diamonds_needed);
          if(success) success = MD5_Update(&context, &map->diamond_score,   sizeof map->diamond_score  );
          if(success) success = MD5_Update(&context, &map->time_score,      sizeof map->time_score     );
          if(success) success = MD5_Update(&context, map->data,             map->width * map->height   );
          if(success) success = MD5_Update(&context, &map->time,            sizeof map->time           );
          if(success) success = MD5_Update(&context, &map->ameba_time,      sizeof map->ameba_time     );
          if(success) success = MD5_Update(&context, &map->game_speed,      sizeof map->game_speed     );
          
          if(success) success = MD5_Final(hash, &context);

          if(!success)
            {
              free(hash);
              hash = NULL;
            }
        }
      else
        {
          free(hash);
          hash = NULL;
        }
    }
#endif

  if(hash != NULL)
    {
      rv = malloc(hash_size * 2 + 1);
      assert(rv != NULL);
      if(rv != NULL)
        for(int i = 0; i < hash_size; i++)
          sprintf(rv + i * 2, "%.2x", hash[i]);
      free(hash);
    }
  
  return rv;
}
Пример #15
0
void finish_running_checksum(struct running_checksum *c, unsigned char *digest)
{
	mhash_deinit(c->td, digest);
	free(c);
}
static ngx_int_t ngx_http_secure_download_check_hash(ngx_http_request_t *r, ngx_http_secure_download_split_uri_t *sdsu, ngx_str_t *secret)
{
  int i;
  unsigned char generated_hash[16];
  char hash[33];
  MHASH td;
  char *hash_data, *str;
  int data_len;

  static const char xtoc[] = "0123456789abcdef";

  /* rel_path_to_hash/secret/timestamp\0 */

  data_len = sdsu->path_to_hash_len + secret->len + 10;

  hash_data = malloc(data_len + 1);
  if (hash_data == NULL)
  {
    ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "securedownload: error in allocating memory for string_to_hash.data", 0);
    return NGX_ERROR;
  }

  str = hash_data;
  memcpy(str, sdsu->path, sdsu->path_to_hash_len);
  str += sdsu->path_to_hash_len;
  *str++ = '/';
  memcpy(str, secret->data, secret->len);
  str += secret->len;
  *str++ = '/';
  memcpy(str, sdsu->timestamp, 8);
  str[8] = 0;

  td = mhash_init(MHASH_MD5);

  if (td == MHASH_FAILED)
  {
    free(hash_data);
    return NGX_ERROR;
  }
  ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "securedownload: hashing string \"%s\" with len %i", hash_data, data_len);
  mhash(td, hash_data, data_len);
  mhash_deinit(td, generated_hash);

  free(hash_data);

  for (i = 0; i < 16; ++i) {
    hash[2 * i + 0] = xtoc[generated_hash[i] >> 4];
    hash[2 * i + 1] = xtoc[generated_hash[i] & 0xf];
  }

  hash[32] = 0; //because %.32 doesn't work
  ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "securedownload: computed hash: %32s", hash); 
  // ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "hash from uri: %.32s", sdsu->md5);

  if(memcmp(hash, sdsu->md5, 32) != 0) {
    ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "securedownload: hash mismatch", 0);
    return NGX_ERROR;
  }

  return NGX_OK;
}
Пример #17
0
int main (int argc, char **argv)
{
  // establish necessary variables here
  int sockfd, n;		// socket and received buffer length

  if (argc != 4)
    {
      printf ("Incorrect Arguments!\n");
      printf ("usage: client <host> <port> <filename>\n");
      exit (1);
    }

  if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
    {
      printf ("Error creating socket\n");
      exit (1);
    }

  // to convert host name (returns original IP or hostname converted to IP)
  char *host = hostname_to_ip (argv[1]);

  // set up all the network stuff
  struct sockaddr_in servaddr, cliaddr;
  bzero (&servaddr, sizeof (servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = inet_addr (host);
  servaddr.sin_port = htons (atoi (argv[2]));

  if (connect (sockfd, (struct sockaddr *) &servaddr, sizeof (servaddr)) ==
      -1)
    {
      printf ("Error creating a connection with the server\n");
      exit (1);
    }

  /* send the message to the server */
  short int length = htons (strlen (argv[3]));
  n = write (sockfd, &length, sizeof (length));
  n = write (sockfd, argv[3], strlen (argv[3]));

  int file_size;
  n = read (sockfd, &file_size, sizeof (file_size));
  if (n < 0)
    error ("ERROR reading from socket");
  file_size = ntohl (file_size);
  //printf("Response read from the server: %d\n", file_size);

  if (file_size == 0)
    {
      printf ("File does not exist on the server\n");
      exit (1);
    }

  unsigned char server_hash[16];
  n = read (sockfd, &server_hash, sizeof (server_hash));
  if (n < 0)
    error ("ERROR reading from socket");

  int i;
	/*
  printf ("Server Hash: ");
  for (i = 0; i < mhash_get_block_size (MHASH_MD5); i++)
    {
      printf ("%.2x", server_hash[i]);
    }
  printf ("\n");
	 */

  FILE *file;
  file = fopen (argv[3], "w+");
  if (file == NULL)
    {
      printf ("Could not open file\n");
      exit (1);
    }

  char output[BUF_LEN];
  bzero (output, BUF_LEN);
  int downloaded = 0;
  int buffer_size;
  struct timeval start;
  struct timeval finish;
  gettimeofday(&start,NULL);
  while (downloaded < file_size)
    {
	  if ((file_size-downloaded)>=BUF_LEN)
	  {
		  buffer_size=BUF_LEN;
	  }else{
		  buffer_size=(file_size-downloaded);
	  }
      n = read (sockfd, output, buffer_size);
      fwrite (output, sizeof (char), buffer_size, file);
      bzero (output, buffer_size);
      downloaded += buffer_size;
    }
  gettimeofday(&finish,NULL);
	long microsecs_elapsed = ((finish.tv_sec - start.tv_sec)*1000000L
						  +finish.tv_usec) - start.tv_usec;
	float time_elapsed = (float)microsecs_elapsed/1000000;

  rewind (file);

	MHASH td;
	unsigned char buffer;
	unsigned char client_hash[16];
	
	td = mhash_init (MHASH_MD5);
	
	while (fread (&buffer, 1, 1, file) == 1)
    {
		mhash (td, &buffer, 1);
    }
	mhash_deinit (td, client_hash);

	/*
  printf ("Client Hash: ");
  for (i = 0; i < mhash_get_block_size (MHASH_MD5); i++)
    {
      printf ("%.2x", client_hash[i]);
    }
  printf ("\n");
	 */
	 
  fclose (file);

  if (compare_hash (server_hash, client_hash) == 0)
    {
      printf ("File transfer was unsuccessful\n");
      remove (argv[3]);
      exit (1);
    }
	
  printf("%d bytes transferred in %.6f seconds: %.6f MB/sec\n",file_size,time_elapsed,
		 (float)(file_size/1048576)/time_elapsed);
	printf("File MD5sum: ");
	for (i = 0; i < mhash_get_block_size (MHASH_MD5); i++)
    {
		printf ("%.2x", client_hash[i]);
    }
	printf ("\n");

  close (sockfd);
  return 0;
}
Пример #18
0
//Main function: 
int main(int argc, char**argv){


//loop to receive multiple messages:
	while(1){
	
		//Necessary data elements
		int mysock; 
		int sr; 
		int numBytes; 
		int accepted = 1; 
		struct addrinfo hints; 
		struct addrinfo *res; 
		char * port; 
		struct sockaddr_storage their_address; 
		socklen_t address_length; 
		char buf[BUFFER_LEN]; 
		char s[50];
		
		//Get the port.  Default is 9499
		if(argc == 2){
			port = argv[1]; 
		}
		else{
			port = "9499"; 
		}
	
		
	
		//Set the addrinfo as empty before starting
		memset(&hints,0,sizeof(hints)); 
	
		hints.ai_family = AF_UNSPEC; //IP Agnostic
		hints.ai_socktype = SOCK_STREAM; //TCP
		hints.ai_flags = AI_PASSIVE; //Use my IP address

		
		
		sr = getaddrinfo(NULL,port,&hints,&res);  //Get address information, and set the port as the first argument
		if(sr != 0){
			printf("ERROR: COULD NOT GET ADDRESS INFORMATION!\n"); 
			//exit(1); 
		}
	
	



		//make the socket
		mysock = socket(res->ai_family, res->ai_socktype,res->ai_protocol); 
		if(mysock==-1){
			printf("ERROR: SOCKET COULD NOT BE MADE.\n"); 
			//exit(1); 
		}
		
		//bind
		bind(mysock, res->ai_addr, res->ai_addrlen); 

		//Listen to anything we can: 
		listen(mysock,1);
	
		//Accept someone: 
		address_length = sizeof their_address;
		sr = accept(mysock,(struct sockaddr*) &their_address, &address_length); 
		
		struct sockaddr *info = (struct sockaddr*) &their_address; 
		struct sockaddr_in * info_in = (struct sockaddr_in *) info; 
		
		//While accepted, keep interacting with that client: 
		while(accepted){ 
			memset(buf,0,sizeof(buf));
			while(read(sr,buf,sizeof(buf))==-1);  //get the command  

			if(!strcmp(buf,"get")){ 
				short fileNameLen;   //Length of filename, as 16 bit int
				int size;   		//Size of file, as 32 bit int
				char * fileName = malloc(sizeof(char)*100);  //FileName c string
				memset(fileName,0,sizeof(fileName)); 	
				
				//Read in the length of the filename and the filename
				read(sr,&fileNameLen,sizeof(fileNameLen));  
				fileNameLen = ntohs(fileNameLen); 
				read(sr,fileName,fileNameLen+1); 
				
			
				//Compute the size of the file: 
				FILE *fp; 
				if(fp = fopen(fileName,"r")){
					struct stat st; 
					stat(fileName, &st); 
					size = st.st_size;  
					fclose(fp); 
				} else{
					size = -1; 
				}
				//Send the size of the file
				size = htonl(size); 
				write(sr,&size,sizeof(size)); 
				size = ntohl(size); 
				
				//If the file exists:
				if(size != -1){
				
					//Calculate the MD5 Hash here: 
					MHASH hash; 
					hash = mhash_init(MHASH_MD5); 
					char hashValue[16]; 
					char buffer; 
					fp = fopen(fileName,"r"); 
					while(fread(&buffer,1,1,fp)){
						mhash(hash,&buffer,1); 
					}
					mhash_deinit(hash,hashValue);
					fclose(fp); 
	
				        //SEND HASH HERE.
				        write(sr,hashValue,16); 
					
					
					//Send the file: 
					char fileBuffer[101]; 
					int sent = 0; 
					fp = fopen(fileName,"r");
					while(sent < size){
						size_t bytes_read; 
						memset(fileBuffer,0,sizeof(fileBuffer)); 
						bytes_read = fread(fileBuffer,1,sizeof(char)*100,fp); 
						sent+= (int) bytes_read;  
						write(sr,&sent,sizeof(sent)); 
						write(sr,fileBuffer,100); 
					}
					fclose(fp); 
				}
			} else if(!strcmp(buf,"put")){
	 
				//Get the length of the file name, followed by the file name
				short fileNameLen;  
				int timeElapsed;
				FILE * saveFile; 
				int si=0; 
				char * fileName = malloc(sizeof(char)*100);
				memset(fileName,0,sizeof(fileName)); 
				
				//Read in the file name length, the file name, and the size of the file. 
				read(sr,&fileNameLen,sizeof(fileNameLen)); 
				fileNameLen = ntohs(fileNameLen); 
				read(sr,fileName,100); 
				read(sr,&si,sizeof(si)); 
				si = ntohl(si); 
				
				
				//If the file exists: 
				if(si!=-1){
					char fileBuffer[100];  	//C String to read each packet
					int received = 0; 	//Number of bytes received so far
					saveFile = fopen(fileName,"w");  //Open file for writing.
				
					//While we have not received all of the bytes, continue getting data. 
					while(received < si){
						int nread; //Number of bytes read
						read(sr,&nread,sizeof(nread)); 
						received = nread;  
						read(sr,fileBuffer,100);   
						fputs(fileBuffer,saveFile);  //Save each buffer to the file.  
					}
					fclose(saveFile); 
					
					
					//Compute the MD5 Hash: 
					MHASH hash; 
					hash = mhash_init(MHASH_MD5); 
					char hashValue[16]; 
					char buffer; 
					saveFile = fopen(fileName,"r"); 
					while(fread(&buffer,1,1,saveFile)){
						mhash(hash,&buffer,1); 
					}
					mhash_deinit(hash,hashValue);
					fclose(saveFile); 
					
					
					//Send the MD5 Hash: 
					write(sr,hashValue,16); 
				}

			} else if(!strcmp(buf,"dir")){ //Working directory command 
				int num = 0; int i; int len = 100; 
				char *files = malloc(sizeof(char)*len); 
				char fileNames[NUM][len]; 
				
				FILE *fp = popen("ls *","r"); 
				while(fgets(files,len,fp) != NULL){  
					strcpy(fileNames[num],files); 	
					num++; 	
				}
				pclose(fp);
				num = htonl(num); 
				write(sr,&num,sizeof(num));
				num = ntohl(num); 
	
				for(i = 0; i<num ;i++){ 
					files = fileNames[i]; 
					write(sr,files,len); 
				}
			} else if(!strcmp(buf,"xit")){  //Working Exit Command
				accepted = 0; 		 	
			} 
		
		
		
		} 
		//close the socket
		close(mysock); 
	}
}
Пример #19
0
WIN32DLL_DEFINE
mutils_error mhash_hmac_deinit(MHASH td, void *result)
{
    mutils_word8 *opad;
    mutils_word8 _opad[MAX_BLOCK_SIZE];
    MHASH tmptd;
    mutils_word32 i;
    mutils_word32 opad_alloc = 0;

    if (td->hmac_block > MAX_BLOCK_SIZE)
    {
        opad = mutils_malloc(td->hmac_block);
        if (opad == NULL)
        {
            return(-MUTILS_SYSTEM_RESOURCE_ERROR);
        }
        opad_alloc = 1;
    }
    else
    {
        opad = _opad;
    }


    for (i = 0; i < td->hmac_key_size; i++)
    {
        opad[i] = (0x5C) ^ td->hmac_key[i];
    }

    for (; i < td->hmac_block; i++)
    {
        opad[i] = (0x5C);
    }

    tmptd = mhash_init(td->algorithm_given);
    mhash(tmptd, opad, td->hmac_block);

    if (td->final_func != NULL)
    {
        td->final_func(td->state);
    }

    if (td->deinit_func != NULL)
    {
        td->deinit_func(td->state, result);
    }

    if (result != NULL)
    {
        mhash(tmptd, result,
              mhash_get_block_size(td->algorithm_given));
    }

    mutils_free(td->state);

    if (opad_alloc!=0)
    {
        mutils_free(opad);
    }

    mutils_bzero(td->hmac_key, td->hmac_key_size);
    mutils_free(td->hmac_key);
    mutils_free(td);

    mhash_deinit(tmptd, result);

    return(MUTILS_OK);
}
Пример #20
0
int frag_test(hashid hashid)
{
  unsigned char digest1[MAX_DIGEST_SIZE]; /* enough space to hold digests */
  unsigned char digest2[MAX_DIGEST_SIZE];
  unsigned char buf1[MAX_INPUT_SIZE + 1];
  unsigned char buf2[MAX_INPUT_SIZE];
  MHASH td1, td2;
  size_t input_size, digest_size;
  int i, offs, left;
  unsigned char val = 0;

  input_size = mhash_get_hash_pblock(hashid);
  assert(input_size <= MAX_INPUT_SIZE);

  digest_size = mhash_get_block_size(hashid);
  assert(digest_size <= MAX_DIGEST_SIZE);

  td1 = mhash_init(hashid);               /* get two mhash instances */
  td2 = mhash_init(hashid);

  for(i = offs = 0; i < 2 * input_size; i++, val++) /* first part */
    {
      memset(buf1, val, input_size + 1);  /* the first instance gets framgments */
      mhash(td1, buf1, input_size + 1);   /* of size (input_size+1)             */

      left = input_size - offs;           /* the second instance gets fragments */
      memset(buf2 + offs, val, left);     /* of size input_size                 */
      mhash(td2, buf2, input_size);

      offs = (input_size + 1) - left;
      memset(buf2, val, offs);
      if (offs == input_size)
	{
	  mhash(td2, buf2, input_size);
	  offs = 0;
	}
    }
  mhash(td2, buf2, offs);

  for(i = offs = 0; i < 2 * input_size; i++, val++) /* second part */
    {
      memset(buf1, val, input_size - 1);  /* the first instance gets framgments */
      mhash(td1, buf1, input_size - 1);   /* of size (input_size-1)             */
      
      if (offs == 0)                      /* the second instance gets fragments */
	{                                 /* of size input_size                 */
	  offs = input_size - 1;
	  memset(buf2, val, offs);
	}
      else
	{
	  left = input_size - offs;
	  memset(buf2 + offs, val, left);

	  mhash(td2, buf2, input_size);
	  offs = (input_size - 1) - left;
	  memset(buf2, val, offs);
	}
    }
  mhash(td2, buf2, offs);

  mhash_deinit(td1, digest1);   /* return 1 if the calculated hash values match */
  mhash_deinit(td2, digest2);
  return ! strncmp(digest1, digest2, digest_size);
}