Beispiel #1
0
/*! \brief Compute SHA512 checksum */
void compute_sha512(char *dst, u_int8_t *src, int src_len)
{
	SHA512_CTX ctx512;
	SHA512_Init(&ctx512);
	SHA512_Update(&ctx512, src, src_len);
	SHA512_End(&ctx512, dst);
}
Beispiel #2
0
static mrb_value
sha512_hexdigest(mrb_state *mrb, mrb_value self)
{
  char *hexdigest = (char*)mrb_malloc(mrb, SHA512_DIGEST_STRING_LENGTH);
  SHA512_CTX ctx = *(SHA512_CTX*)DATA_PTR(self);
  SHA512_End(&ctx, hexdigest);
  return mrb_str_new_cstr(mrb, hexdigest);
}
Beispiel #3
0
	ByteArray HashSHA512::End()
	{
		UInt8 digest[SHA512_DIGEST_LENGTH];

		SHA512_End(m_state, digest);

		return ByteArray(digest, SHA512_DIGEST_LENGTH);
	}
Beispiel #4
0
NzHashDigest NzHashSHA512::End()
{
	nzUInt8 digest[SHA512_DIGEST_LENGTH];

	SHA512_End(m_state, digest);

	return NzHashDigest(GetHashName(), digest, SHA512_DIGEST_LENGTH);
}
Beispiel #5
0
/*! \brief Compute SHA512 checksum */
void compute_sha512(char *dst, u_int8_t *src, int src_len)
{
	SHA512_CTX ctx512;
	char buf[128];
	SHA512_Init(&ctx512);
	SHA512_Update(&ctx512, src, src_len);
	SHA512_End(&ctx512, buf);
	strncpy(dst, buf, 128);
}
Beispiel #6
0
//return 0 on success.
//checks if filesize and/or sha512 matches, if used.
int verify_tarball(pkgconfig* cfg, pkgdata* package) {
	char buf[4096];
	char* error;
	SHA512_CTX ctx;
	int fd;
	uint64_t pos, len = 0, nread;
	stringptr hash;
	get_tarball_filename_with_path(cfg, package, buf, sizeof(buf));
	if(package->filesize) {
		len = getfilesize(buf);
		if(len < package->filesize) {
			log_put(2, VARISL("WARNING: "), VARIC(buf), VARISL(" filesize too small!"), NULL);
			return 1;
		} else if (len > package->filesize) {
			log_put(2, VARISL("WARNING: "), VARIC(buf), VARISL(" filesize too big!"), NULL);
			return 2;
		}
	}
	if(package->sha512) {
		if(!len) len = getfilesize(buf);
			
		fd = open(buf, O_RDONLY);
		if(fd == -1) {
			error = strerror(errno);
			log_put(2, VARISL("WARNING: "), VARIC(buf), VARISL(" failed to open: "), VARIC(error), NULL);
			return 3;
		}
		SHA512_Init(&ctx);
		pos = 0;
		while(pos < len) {
			nread = read(fd, buf, sizeof(buf));
			SHA512_Update(&ctx, (const uint8_t*) buf, nread);
			pos += nread;
		}
		close(fd);
		SHA512_End(&ctx, (char*) buf);
		hash.ptr = buf; hash.size = strlen(buf);
		if(!EQ(&hash, package->sha512)) {
			log_put(2, VARISL("WARNING: "), VARIS(package->name), VARISL(" sha512 mismatch, got "), 
				VARIS(&hash), VARISL(", expected "), VARIS(package->sha512), NULL);
			return 4;
		}
	}
	return 0;
}
Beispiel #7
0
static char *
digest_end(DIGEST_CTX *c, char *buf)
{

	switch (digesttype) {
	case DIGEST_MD5:
		return (MD5End(&(c->MD5), buf));
	case DIGEST_RIPEMD160:
		return (RIPEMD160_End(&(c->RIPEMD160), buf));
	case DIGEST_SHA1:
		return (SHA1_End(&(c->SHA1), buf));
	case DIGEST_SHA256:
		return (SHA256_End(&(c->SHA256), buf));
	case DIGEST_SHA512:
		return (SHA512_End(&(c->SHA512), buf));
	default:
		return (NULL);
	}
}
Beispiel #8
0
int main(int argc, char **argv) {
	int		kl, l, fd, ac;
	int		quiet = 0, hash = 0;
	char		*av, *file = (char*)0;
	FILE		*IN = (FILE*)0;
	SHA256_CTX	ctx256;
	SHA384_CTX	ctx384;
	SHA512_CTX	ctx512;
	unsigned char	buf[BUFLEN];

	SHA256_Init(&ctx256);
	SHA384_Init(&ctx384);
	SHA512_Init(&ctx512);

	/* Read data from STDIN by default */
	fd = fileno(stdin);

	ac = 1;
	while (ac < argc) {
		if (*argv[ac] == '-') {
			av = argv[ac] + 1;
			if (!strcmp(av, "q")) {
				quiet = 1;
			} else if (!strcmp(av, "256")) {
				hash |= 1;
			} else if (!strcmp(av, "384")) {
				hash |= 2;
			} else if (!strcmp(av, "512")) {
				hash |= 4;
			} else if (!strcmp(av, "ALL")) {
				hash = 7;
			} else {
				usage(argv[0], "Invalid option.");
			}
			ac++;
		} else {
			file = argv[ac++];
			if (ac != argc) {
				usage(argv[0], "Too many arguments.");
			}
			if ((IN = fopen(file, "r")) == NULL) {
				perror(argv[0]);
				exit(-1);
			}
			fd = fileno(IN);
		}
	}
	if (hash == 0)
		hash = 7;	/* Default to ALL */

	kl = 0;
	while ((l = read(fd,buf,BUFLEN)) > 0) {
		kl += l;
		SHA256_Update(&ctx256, (unsigned char*)buf, l);
		SHA384_Update(&ctx384, (unsigned char*)buf, l);
		SHA512_Update(&ctx512, (unsigned char*)buf, l);
	}
	if (file) {
		fclose(IN);
	}

	if (hash & 1) {
		SHA256_End(&ctx256, buf);
		if (!quiet)
			printf("SHA-256 (%s) = ", file);
		printf("%s\n", buf);
	}
	if (hash & 2) {
		SHA384_End(&ctx384, buf);
		if (!quiet)
			printf("SHA-384 (%s) = ", file);
		printf("%s\n", buf);
	}
	if (hash & 4) {
		SHA512_End(&ctx512, buf);
		if (!quiet)
			printf("SHA-512 (%s) = ", file);
		printf("%s\n", buf);
	}

	return 1;
}
Beispiel #9
0
/* The following function just stores the file (or silently returns if it already
   exists).
   The relationships of manifests to this file are the responsibility of the
   caller. */
int rhizome_store_file(char *file,char *hash,int priority) {

  int fd=open(file,O_RDONLY);
  if (fd<0) return WHY("Could not open associated file");
  
  struct stat stat;
  if (fstat(fd,&stat)) {
    close(fd);
    return WHY("Could not stat() associated file");
  }

  unsigned char *addr =
    mmap(NULL, stat.st_size, PROT_READ, MAP_FILE|MAP_SHARED, fd, 0);
  if (addr==MAP_FAILED) {
    close(fd);
    return WHY("mmap() of associated file failed.");
  }

  /* Get hash of file if not supplied */
  char hexhash[SHA512_DIGEST_STRING_LENGTH];
  if (!hash)
    {
      /* Hash the file */
      SHA512_CTX c;
      SHA512_Init(&c);
      SHA512_Update(&c,addr,stat.st_size);
      SHA512_End(&c,hexhash);
      hash=hexhash;
    }

  /* INSERT INTO FILES(id as text, data blob, length integer, highestpriority integer).
   BUT, we have to do this incrementally so that we can handle blobs larger than available memory. 
  This is possible using: 
     int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);
  That binds an all zeroes blob to a field.  We can then populate the data by
  opening a handle to the blob using:
     int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset);
*/
  
  char sqlcmd[1024];
  const char *cmdtail;

  /* See if the file is already stored, and if so, don't bother storing it again */
  int count=sqlite_exec_int64("SELECT COUNT(*) FROM FILES WHERE id='%s' AND datavalid<>0;",hash); 
  if (count==1) {
    /* File is already stored, so just update the highestPriority field if required. */
    long long storedPriority = sqlite_exec_int64("SELECT highestPriority FROM FILES WHERE id='%s' AND datavalid!=0",hash);
    if (storedPriority<priority)
      {
	snprintf(sqlcmd,1024,"UPDATE FILES SET highestPriority=%d WHERE id='%s';",
		 priority,hash);
	if (sqlite3_exec(rhizome_db,sqlcmd,NULL,NULL,NULL)!=SQLITE_OK) {
	  close(fd);
	  WHY(sqlite3_errmsg(rhizome_db));
	  return WHY("SQLite failed to update highestPriority field for stored file.");
	}
      }
    close(fd);
    return 0;
  } else if (count>1) {
    /* This should never happen! */
    return WHY("Duplicate records for a file in the rhizome database.  Database probably corrupt.");
  }

  /* Okay, so there are no records that match, but we should delete any half-baked record (with datavalid=0) so that the insert below doesn't fail.
   Don't worry about the return result, since it might not delete any records. */
  sqlite3_exec(rhizome_db,"DELETE FROM FILES WHERE datavalid=0;",NULL,NULL,NULL);

  snprintf(sqlcmd,1024,"INSERT INTO FILES(id,data,length,highestpriority,datavalid) VALUES('%s',?,%lld,%d,0);",
	   hash,(long long)stat.st_size,priority);
  sqlite3_stmt *statement;
  if (sqlite3_prepare_v2(rhizome_db,sqlcmd,strlen(sqlcmd)+1,&statement,&cmdtail) 
      != SQLITE_OK)
    {
      close(fd);
      sqlite3_finalize(statement);
      return WHY(sqlite3_errmsg(rhizome_db));
    }
  
  /* Bind appropriate sized zero-filled blob to data field */
  int dud=0;
  int r;
  if ((r=sqlite3_bind_zeroblob(statement,1,stat.st_size))!=SQLITE_OK)
    {
      dud++;
      WHY("sqlite3_bind_zeroblob() failed");
      WHY(sqlite3_errmsg(rhizome_db));   
    }

  /* Do actual insert, and abort if it fails */
  if (!dud)
    switch(sqlite3_step(statement)) {
    case SQLITE_OK: case SQLITE_ROW: case SQLITE_DONE:
      break;
    default:
      dud++;
      WHY("sqlite3_step() failed");
      WHY(sqlite3_errmsg(rhizome_db));   
    }

  if (sqlite3_finalize(statement)) dud++;
  if (dud) {
    close(fd);
    if (sqlite3_finalize(statement)!=SQLITE_OK)
      {
	WHY("sqlite3_finalize() failed");
	WHY(sqlite3_errmsg(rhizome_db));
      }
    return WHY("SQLite3 failed to insert row for file");
  }

  /* Get rowid for inserted row, so that we can modify the blob */
  int rowid=sqlite3_last_insert_rowid(rhizome_db);
  if (rowid<1) {
    close(fd);
    WHY(sqlite3_errmsg(rhizome_db));
    return WHY("SQLite3 failed return rowid of inserted row");
  }

  sqlite3_blob *blob;
  if (sqlite3_blob_open(rhizome_db,"main","FILES","data",rowid,
		    1 /* read/write */,
			&blob) != SQLITE_OK)
    {
      WHY(sqlite3_errmsg(rhizome_db));
      close(fd);
      sqlite3_blob_close(blob);
      return WHY("SQLite3 failed to open file blob for writing");
    }

  {
    long long i;
    for(i=0;i<stat.st_size;i+=65536)
      {
	int n=65536;
	if (i+n>stat.st_size) n=stat.st_size-i;
	if (sqlite3_blob_write(blob,&addr[i],n,i) !=SQLITE_OK) dud++;
      }
  }
  
  close(fd);
  sqlite3_blob_close(blob);

  /* Mark file as up-to-date */
  sqlite_exec_int64("UPDATE FILES SET datavalid=1 WHERE id='%s';",
	   hash);


  if (dud) {
      WHY(sqlite3_errmsg(rhizome_db));
      return WHY("SQLite3 failed write all blob data");
  }

  printf("stored file\n");
  return 0;
}