uint64_t shrand(void) { char buf[MAX_RANDOM_SEED_SIZE]; int i; memset(buf, 0, sizeof(buf)); FILE *ran = fopen("/dev/urandom", "r"); if (ran) { fread(buf, 1, sizeof(buf), ran); fclose(ran); } else { static int init; if (!init) { init = 1; srand((unsigned int)shtime()); } unsigned int rval; for (i = 0; i < MAX_RANDOM_SEED_SIZE; i += 4) { rval = rand(); memcpy(buf + i, &rval, sizeof(rval)); } } return (shcrc(buf, sizeof(buf))); }
int _shfs_ref_raw_write(shfs_ino_t *file, shbuf_t *buff) { shfs_ino_t *inode; int err; if (!file) return (SHERR_INVAL); inode = shfs_inode(file, NULL, SHINODE_REFERENCE); if (!inode) return (SHERR_IO); if (shbuf_size(buff) > SHFS_BLOCK_DATA_SIZE) { return (SHERR_TOOMANYREFS); } memset((char *)inode->blk.raw, 0, SHFS_BLOCK_DATA_SIZE); memcpy((char *)inode->blk.raw, shbuf_data(buff), shbuf_size(buff)); inode->blk.hdr.size = shbuf_size(buff); inode->blk.hdr.crc = shcrc(shbuf_data(buff), shbuf_size(buff)); err = shfs_inode_write_entity(inode); if (err) return (err); /* copy aux stats to file inode. */ file->blk.hdr.mtime = inode->blk.hdr.mtime; file->blk.hdr.size = inode->blk.hdr.size; file->blk.hdr.crc = inode->blk.hdr.crc; file->blk.hdr.format = SHINODE_REFERENCE; file->blk.hdr.attr |= SHATTR_LINK; return (0); }
/** * Store supplementary credential data in a file. */ int shfs_cred_store(shfs_ino_t *file, shkey_t *key, unsigned char *data, size_t data_len) { shfs_ino_t *cred; shbuf_t *buff; char key_buf[MAX_SHARE_HASH_LENGTH]; int err; memset(key_buf, 0, sizeof(key_buf)); sprintf(key_buf, "%s", shkey_hex(key)); cred = shfs_inode(file, key_buf, SHINODE_ACCESS); if (!cred) return (SHERR_IO); cred->blk.hdr.format = SHINODE_ACCESS; buff = shbuf_map(data, data_len); err = shfs_aux_write(cred, buff); free(buff); if (err) return (err); file->blk.hdr.attr |= SHATTR_CRED; file->blk.hdr.crc = shcrc(data, data_len); file->blk.hdr.size = data_len; err = shfs_inode_write_entity(file); if (err) return (err); return (0); }
static void generate_bond_signature(tx_bond_t *bond) { shkey_t *sig_key; uint64_t crc; crc = (uint64_t)shcrc(bond->bond_sink, strlen(bond->bond_sink)); sig_key = shkey_cert(&bond->bond_key, crc, bond->bond_expire); memcpy(&bond->bond_sig, sig_key, sizeof(shkey_t)); shkey_free(&sig_key); }
static int validate_bond_signature(tx_bond_t *bond) { uint64_t crc; int err; crc = (uint64_t)shcrc(bond->bond_sink, strlen(bond->bond_sink)); err = shkey_verify(&bond->bond_sig, crc, &bond->bond_key, bond->bond_expire); if (err) return (err); return (0); }
int verify_asset_signature(tx_asset_t *asset, shpeer_t *peer) { uint64_t crc; int err; crc = shcrc((unsigned char *)asset->ass_data, asset->ass_size); err = shkey_verify(shpeer_kpriv(peer), crc, &asset->ass.ass_sig, asset->ass.ass_expire); if (err) return (err); return (0); }
void generate_asset_signature(tx_asset_t *asset, shpeer_t *peer) { shkey_t *sig_key; uint64_t crc; if (asset->ass.ass_expire == SHTIME_UNDEFINED) asset->ass.ass_expire = shtime_adj(shtime(), SHARE_DEFAULT_EXPIRE_TIME); crc = shcrc((unsigned char *)asset->ass_data, asset->ass_size); sig_key = shkey_cert(shpeer_kpriv(peer), crc, asset->ass.ass_expire); memcpy(&asset->ass.ass_sig, sig_key, sizeof(shkey_t)); shkey_free(&sig_key); }
void shmap_print(shmap_t *h, shbuf_t *ret_buff) { shmap_entry_t *ent; shmap_value_t mval; shmap_index_t *hi; shkey_t *key; char buf[256]; char *val; char str[4096]; ssize_t len; int flag; int idx; int i; if (!h || !ret_buff) return; /* all done */ i = 0; for (hi = shmap_first(h); hi; hi = shmap_next(hi)) { shmap_self(hi, &key, &val, &len, &flag); if (!len || !val) continue; flag &= ~SHMAP_ALLOC; memset(&mval, 0, sizeof(mval)); memcpy(&mval.name, key, sizeof(mval.name)); mval.magic = SHMEM32_MAGIC; mval.stamp = shtime(); mval.crc = shcrc(val, len); mval.pf = flag; mval.sz = len; shbuf_cat(ret_buff, &mval, sizeof(shmap_value_t)); shbuf_cat(ret_buff, val, len); #if 0 hdr = (shmap_value_t *)val; memcpy(&hdr->name, key, sizeof(shkey_t)); shbuf_cat(ret_buff, hdr, sizeof(shmap_value_t)); shbuf_cat(ret_buff, ((char *)val + sizeof(shmap_value_t)), hdr->sz); #endif i++; } }
static int validate_asset_signature(tx_asset_t *asset) { shpeer_t *peer; uint64_t crc; int err; peer = load_asset_peer(&asset->ass.ass_id); if (!peer) return (SHERR_INVAL); crc = shcrc((unsigned char *)asset->ass_data, asset->ass_size); err = shkey_verify(shpeer_kpriv(peer), crc, &asset->ass.ass_sig, asset->ass.ass_expire); if (err) return (err); return (0); }
/* faster version */ static void shkey_bin_r(void *data, size_t data_len, shkey_t *key) { uint64_t val; size_t step; size_t len; size_t of; int i; memset(key, 0, sizeof(shkey_t)); val = 0; step = data_len / SHKEY_WORDS; for (i = 0; i < SHKEY_WORDS; i++) { /* add block to sha hash */ of = step * i; len = MIN(data_len - of, step + 8); val += shcrc((char *)data + of, len); key->code[i] = (uint32_t)val; } }
int shfs_ext_set(shfs_ino_t *file, const char *path) { struct stat st; shfs_ino_t *ext; char fs_path[SHFS_PATH_MAX]; int err; if (file == NULL || !path || !*path) return (SHERR_INVAL); err = stat(path, &st); if (err) { return (errno2sherr()); } ext = shfs_inode(file, NULL, SHINODE_EXTERNAL); if (!ext) return (SHERR_IO); memset(fs_path, 0, sizeof(fs_path)); strncpy(fs_path, path, sizeof(fs_path) - 1); /* save local hard-drive path as inode data. */ memset(ext->blk.raw, 0, SHFS_PATH_MAX); strcpy((char *)ext->blk.raw, fs_path); ext->blk.hdr.size = SHFS_PATH_MAX; ext->blk.hdr.crc = shcrc(fs_path, SHFS_PATH_MAX); err = shfs_inode_write_entity(ext); if (err) return (err); /* copy ext stats to file inode. */ file->blk.hdr.mtime = ext->blk.hdr.mtime; file->blk.hdr.size = ext->blk.hdr.size; file->blk.hdr.crc = ext->blk.hdr.crc; file->blk.hdr.format = SHINODE_EXTERNAL; file->blk.hdr.attr |= SHATTR_LINK_EXT; return (0); }
static int shproc_write(shproc_t *proc, shproc_req_t *req) { int w_len; int err; int of; req->data_len = shbuf_size(proc->proc_buff); req->crc = shcrc(shbuf_data(proc->proc_buff), shbuf_size(proc->proc_buff)); err = write(proc->proc_fd, req, sizeof(shproc_req_t)); if (err == -1) return (errno2sherr()); if (err == 0) return (SHERR_AGAIN); of = 0; while (of < shbuf_size(proc->proc_buff)) { /* * On i386 the buffer is 4096 and 65k otherwise. * Parent is granted 100ms to 'poll' when size exceeds buffer limit. */ err = shproc_write_wait(proc, 100); if (err) return (err); w_len = write(proc->proc_fd, shbuf_data(proc->proc_buff) + of, shbuf_size(proc->proc_buff) - of); if (w_len == -1) return (errno2sherr()); if (w_len == 0) return (SHERR_AGAIN); of += w_len; } return (0); }
uint64_t shkey_crc(shkey_t *key) { if (!key) return (0); return (shcrc(key, sizeof(shkey_t))); }