void cyon_store_current_state(u_int8_t *hash) { SHA_CTX sctx; u_int8_t *buf; struct store_header header; u_int32_t len, blen; memset(&header, 0, sizeof(header)); if (store_passphrase != NULL) header.flags |= STORE_HAS_PASSPHRASE; len = 0; blen = 128 * 1024 * 1024; buf = cyon_malloc(blen); memcpy(buf, &header, sizeof(header)); len += sizeof(header); if (header.flags & STORE_HAS_PASSPHRASE) { memcpy(buf + len, store_passphrase, SHA256_DIGEST_LENGTH); len += SHA256_DIGEST_LENGTH; } store_validation = 1; SHA_Init(&sctx); cyon_store_writenode(-1, rnode, buf, blen, &len, &sctx); if (len > 0) SHA_Update(&sctx, buf, len); cyon_mem_free(buf); SHA_Final(hash, &sctx); store_validation = 0; }
static void* sha1_thread(void* arg) { char code[41]; while (1) { struct chunk* c = sync_queue_pop(chunk_queue); if (c == NULL) { sync_queue_term(hash_queue); break; } if (CHECK_CHUNK(c, CHUNK_FILE_START) || CHECK_CHUNK(c, CHUNK_FILE_END)) { sync_queue_push(hash_queue, c); continue; } TIMER_DECLARE(1); TIMER_BEGIN(1); SHA_CTX ctx; SHA_Init(&ctx); SHA_Update(&ctx, c->data, c->size); SHA_Final(c->fp, &ctx); TIMER_END(1, jcr.hash_time); hash2code(c->fp, code); code[40] = 0; VERBOSE("Hash phase: %ldth chunk identified by %s", chunk_num++, code); sync_queue_push(hash_queue, c); } return NULL; }
std::string SHA0Stream::hash() const { SHA_CTX copy(m_ctx); std::string result; result.resize(SHA_DIGEST_LENGTH); SHA_Final((unsigned char *)&result[0], ©); return result; }
static void cyon_store_map(void) { struct stat st; int fd; struct store_header header; char fpath[MAXPATHLEN], *hex; u_char hash[SHA_DIGEST_LENGTH]; u_char ohash[SHA_DIGEST_LENGTH]; if (store_nopersist) return; snprintf(fpath, sizeof(fpath), CYON_STORE_FILE, storepath, storename); if ((fd = open(fpath, O_RDONLY)) == -1) { if (errno != ENOENT) fatal("open(%s): %s", fpath, errno_s); cyon_storelog_replay_all(); return; } if (fstat(fd, &st) == -1) fatal("cyon_store_map(): fstat(): %s", errno_s); SHA_Init(&shactx); memset(&header, 0, sizeof(header)); cyon_atomic_read(fd, &header, sizeof(header), &shactx, 0); if (header.flags & STORE_HAS_PASSPHRASE) { store_passphrase = cyon_malloc(SHA256_DIGEST_LENGTH); cyon_atomic_read(fd, store_passphrase, SHA256_DIGEST_LENGTH, &shactx, 0); } rnode = cyon_malloc(sizeof(struct node)); cyon_atomic_read(fd, rnode, sizeof(struct node), &shactx, 0); cyon_store_mapnode(fd, rnode); SHA_Final(hash, &shactx); cyon_atomic_read(fd, ohash, sizeof(ohash), NULL, 0); close(fd); if (memcmp(hash, ohash, SHA_DIGEST_LENGTH)) fatal("SHA1 checksum mismatch, store corrupted?"); memcpy(store_state, hash, SHA_DIGEST_LENGTH); if (store_retain_logs) { cyon_sha_hex(store_state, &hex); cyon_log(LOG_NOTICE, "store state is %s", hex); cyon_mem_free(hex); } cyon_storelog_replay_all(); }
static int crypt_all(int *pcount, struct db_salt *salt) { int count = *pcount; SHA_Init( &ctx ); SHA_Update( &ctx, (unsigned char *) saved_key, strlen( saved_key ) ); SHA_Final( (unsigned char *) crypt_key, &ctx); return count; }
int main(int argc, char *argv[]) { int i,err=0; unsigned char **P,**R; static unsigned char buf[1000]; char *p,*r; SHA_CTX c; unsigned char md[SHA_DIGEST_LENGTH]; #ifdef CHARSET_EBCDIC ebcdic2ascii(test[0], test[0], strlen(test[0])); ebcdic2ascii(test[1], test[1], strlen(test[1])); #endif P=(unsigned char **)test; R=(unsigned char **)ret; i=1; while (*P != NULL) { p=pt(SHA(*P,(unsigned long)strlen((char *)*P),NULL)); if (strcmp(p,(char *)*R) != 0) { printf("error calculating SHA on '%s'\n",*P); printf("got %s instead of %s\n",p,*R); err++; } else printf("test %d ok\n",i); i++; R++; P++; } memset(buf,'a',1000); #ifdef CHARSET_EBCDIC ebcdic2ascii(buf, buf, 1000); #endif /*CHARSET_EBCDIC*/ SHA_Init(&c); for (i=0; i<1000; i++) SHA_Update(&c,buf,1000); SHA_Final(md,&c); p=pt(md); r=bigret; if (strcmp(p,r) != 0) { printf("error calculating SHA on '%s'\n",p); printf("got %s instead of %s\n",p,r); err++; } else printf("test 3 ok\n"); exit(err); return(0); }
std::string Digestor::Sha1str(const std::string &data) { unsigned char sha1hash[20] = {0}; SHA_CTX sha1ctx; SHA_Init(&sha1ctx); SHA_Update(&sha1ctx, data.c_str(), data.length()); SHA_Final(sha1hash, &sha1ctx); std::string digest = BinaryHashToHexString(sha1hash, 20); return digest; }
unsigned char *SHA(const unsigned char *d, unsigned long n, unsigned char *md) { SHA_CTX c; static unsigned char m[SHA_DIGEST_LENGTH]; if (md == NULL) md=m; SHA_Init(&c); SHA_Update(&c,d,n); SHA_Final(md,&c); memset(&c,0,sizeof(c)); return(md); }
static void *dss_createkey(unsigned char *pub_blob, int pub_len, unsigned char *priv_blob, int priv_len) { dss_key *dss; char *pb = (char *) priv_blob; char *hash; int hashlen; SHA_State s; unsigned char digest[20]; Bignum ytest; dss = dss_newkey((char *) pub_blob, pub_len); if (!dss) return NULL; dss->x = getmp(&pb, &priv_len); if (!dss->x) { dss_freekey(dss); return NULL; } /* * Check the obsolete hash in the old DSS key format. */ hashlen = -1; getstring(&pb, &priv_len, &hash, &hashlen); if (hashlen == 20) { SHA_Init(&s); sha_mpint(&s, dss->p); sha_mpint(&s, dss->q); sha_mpint(&s, dss->g); SHA_Final(&s, digest); if (0 != memcmp(hash, digest, 20)) { dss_freekey(dss); return NULL; } } /* * Now ensure g^x mod p really is y. */ ytest = modpow(dss->g, dss->x, dss->p); if (0 != bignum_cmp(ytest, dss->y)) { dss_freekey(dss); freebn(ytest); return NULL; } freebn(ytest); return dss; }
unsigned char *SHA(const unsigned char *d, unsigned long n, unsigned char *md) { SHA_CTX c; static unsigned char m[SHA_DIGEST_LENGTH]; if (md == NULL) md=m; if (!SHA_Init(&c)) return NULL; SHA_Update(&c,d,n); SHA_Final(md,&c); OPENSSL_cleanse(&c,sizeof(c)); return(md); }
bool xr_sha256::continue_calculate () { u32 const to_calc = m_data_size >= calc_chunk_size ? calc_chunk_size : m_data_size; SHA_Update (m_sha_ctx, m_data_src, to_calc); m_data_src += to_calc; m_data_size -= to_calc; if (!m_data_size) { SHA_Final(m_result, m_sha_ctx); return true; } return false; }
void cyon_store_init(void) { char *hex; lfd = -1; dfd = -1; rnode = NULL; key_count = 0; store_errors = 0; store_log_offset = 0; store_passphrase = NULL; SHA_Init(&shactx); SHA_Final(store_state, &shactx); pthread_rwlock_init(&store_lock, NULL); cyon_store_map(); if (rnode == NULL) { cyon_log(LOG_NOTICE, "store is empty, starting a new one"); if (store_retain_logs) { cyon_sha_hex(store_state, &hex); cyon_log(LOG_NOTICE, "new state is %s", hex); cyon_mem_free(hex); } rnode = cyon_malloc(sizeof(struct node)); memset(rnode, 0, sizeof(struct node)); } else { cyon_log(LOG_NOTICE, "store loaded from disk with %ld keys", key_count); } if (store_errors) { cyon_log(LOG_ERR, "INCONSISTENCIES IN STORE LOG/DATA FILE"); cyon_log(LOG_ERR, "THESE MUST BE REPAIRED. FORCING READONLY"); cyon_readonly_mode = 1; } if (!store_nopersist && !cyon_readonly_mode) cyon_storelog_reopen(0); if (cyon_readonly_mode) cyon_log(LOG_NOTICE, "Cyon is in read-only mode"); }
void do_fp(TINYCLR_SSL_FILE *f) { SHA_CTX c; unsigned char md[SHA_DIGEST_LENGTH]; int fd; int i; unsigned char buf[BUFSIZE]; fd=TINYCLR_SSL_FILENO(f); SHA_Init(&c); for (;;) { i=read(fd,buf,BUFSIZE); if (i <= 0) break; SHA_Update(&c,buf,(unsigned long)i); } SHA_Final(&(md[0]),&c); pt(md); }
static void SHA_File(FILE *file, unsigned char **output, int *outlength) { *output = new unsigned char[SHA_DIGEST_LENGTH]; *outlength = SHA_DIGEST_LENGTH; SHA_CTX c; int i; unsigned char buf[SHA_FILE_BUFFER_SIZE]; SHA_Init(&c); for (;;) { i = fread(buf,1,SHA_FILE_BUFFER_SIZE,file); if(i <= 0) break; SHA_Update(&c,buf,(unsigned long)i); } SHA_Final(*output, &c); }
void cyon_storelog_write(u_int8_t op, u_int8_t *key, u_int32_t klen, u_int8_t *data, u_int32_t dlen, u_int32_t flags) { u_int32_t len; struct store_log *slog; u_int8_t *buf, *p; if (store_nopersist) return; len = sizeof(struct store_log) + klen + dlen; buf = cyon_malloc(len); slog = (struct store_log *)buf; memset(slog, 0, sizeof(*slog)); slog->op = op; slog->klen = klen; slog->dlen = dlen; slog->flags = flags; memcpy(slog->magic, store_log_magic, 4); p = buf + sizeof(*slog); memcpy(p, key, slog->klen); if (dlen > 0) memcpy(p + slog->klen, data, dlen); SHA_Init(&shactx); SHA_Update(&shactx, buf, len); SHA_Final(slog->hash, &shactx); cyon_atomic_write(lfd, buf, len, NULL); cyon_mem_free(buf); log_modified = 1; store_modified = 1; store_log_offset += len; }
static void crypt_all(int count) { SHA_Init( &ctx ); SHA_Update( &ctx, (unsigned char *) saved_key, strlen( saved_key ) ); SHA_Final( (unsigned char *) crypt_key, &ctx); }
pid_t cyon_store_write(void) { pid_t pid; u_int8_t *buf; struct store_header header; int fd, ret; u_int32_t len, blen; u_char hash[SHA_DIGEST_LENGTH]; char fpath[MAXPATHLEN], tpath[MAXPATHLEN]; if (rnode == NULL || store_modified == 0 || store_nopersist) { cyon_log(LOG_NOTICE, "store is clean, not writing"); return (CYON_RESULT_OK); } /* * The write lock protects us from getting new entries in the log * so it is safe to reopen the logs after the fork. */ cyon_store_lock(1); pid = fork(); if (pid == -1) { cyon_store_unlock(); cyon_log(LOG_NOTICE, "store write not started (fork: %s)", errno_s); return (CYON_RESULT_ERROR); } if (pid != 0) { store_modified = 0; if (!cyon_readonly_mode) cyon_storelog_reopen(1); cyon_store_unlock(); cyon_log(LOG_NOTICE, "store write started (%d)", pid); return (pid); } if (!cyon_readonly_mode) close(lfd); snprintf(fpath, sizeof(fpath), CYON_STORE_FILE, storepath, storename); snprintf(tpath, sizeof(tpath), CYON_STORE_TMPFILE, storepath, storename); fd = open(tpath, O_CREAT | O_TRUNC | O_WRONLY, 0700); if (fd == -1) fatal("open(%s): %s", tpath, errno_s); memset(&header, 0, sizeof(header)); if (store_passphrase != NULL) header.flags |= STORE_HAS_PASSPHRASE; len = 0; blen = 128 * 1024 * 1024; buf = cyon_malloc(blen); memcpy(buf, &header, sizeof(header)); len += sizeof(header); if (header.flags & STORE_HAS_PASSPHRASE) { memcpy(buf + len, store_passphrase, SHA256_DIGEST_LENGTH); len += SHA256_DIGEST_LENGTH; } SHA_Init(&shactx); cyon_store_writenode(fd, rnode, buf, blen, &len, NULL); if (len > 0) cyon_atomic_write(fd, buf, len, &shactx); cyon_mem_free(buf); SHA_Final(hash, &shactx); cyon_atomic_write(fd, hash, SHA_DIGEST_LENGTH, NULL); for (;;) { ret = fsync(fd); if (ret == -1 && errno == EINTR) continue; if (ret == -1) fatal("store write failed %s", errno_s); break; } close(fd); if (rename(tpath, fpath) == -1) fatal("cannot move store into place: %s", errno_s); exit(0); }
void plSHAChecksum::Finish() { SHA_Final(fChecksum, &fContext); fValid = true; memset(&fContext, 0, sizeof(fContext)); }
int cyon_storelog_replay(char *state, int when) { struct stat st; long offset; u_int64_t len, olen; struct store_log slog, *plog; u_int64_t added, removed; char fpath[MAXPATHLEN], *hex; u_char hash[SHA_DIGEST_LENGTH]; u_int8_t *buf, err, ch, *key, *data; snprintf(fpath, sizeof(fpath), CYON_LOG_FILE, storepath, storename, state); if ((lfd = open(fpath, O_RDONLY)) == -1) { if (errno == ENOENT) return (CYON_RESULT_ERROR); fatal("open(%s): %s", fpath, errno_s); } if (fstat(lfd, &st) == -1) fatal("fstat(): %s", errno_s); if (st.st_size == 0) { close(lfd); if (when == CYON_REPLAY_STARTUP) return (CYON_RESULT_ERROR); return (CYON_RESULT_OK); } olen = 0; buf = NULL; offset = 0; replaying_log = 1; added = removed = 0; if (when == CYON_REPLAY_REQUEST) store_errors = 0; cyon_log(LOG_NOTICE, "applying log %s", fpath); for (;;) { while (offset < st.st_size) { cyon_atomic_read(lfd, &ch, 1, NULL, 0); offset++; if (ch != store_log_magic[0]) continue; if ((long)(offset + sizeof(slog) - 1) >= st.st_size) break; cyon_atomic_read(lfd, &slog.magic[1], sizeof(slog) - 1, NULL, 0); offset += sizeof(slog) - 1; slog.magic[0] = ch; if (!memcmp(slog.magic, store_log_magic, 4)) break; } if (offset >= st.st_size) break; if ((offset + slog.dlen + slog.klen) > st.st_size) { store_errors++; cyon_log(LOG_NOTICE, "LOG CORRUPTED, would read past at %ld", offset); continue; } len = slog.klen + slog.dlen + sizeof(slog); if (len > olen) { if (buf != NULL) cyon_mem_free(buf); buf = cyon_malloc(len); } olen = len; memcpy(buf, &slog, sizeof(slog)); cyon_atomic_read(lfd, buf + sizeof(slog), len - sizeof(slog), NULL, 0); offset += slog.klen + slog.dlen; plog = (struct store_log *)buf; memcpy(hash, plog->hash, SHA_DIGEST_LENGTH); memset(plog->hash, '\0', SHA_DIGEST_LENGTH); SHA_Init(&shactx); SHA_Update(&shactx, buf, len); SHA_Final(plog->hash, &shactx); if (memcmp(hash, plog->hash, SHA_DIGEST_LENGTH)) { store_errors++; cyon_log(LOG_NOTICE, "INCORRECT CHECKSUM for log @ %ld, skipping", offset); continue; } key = buf + sizeof(slog); if (slog.dlen > 0) data = buf + sizeof(slog) + slog.klen; else data = NULL; if (rnode == NULL) { rnode = cyon_malloc(sizeof(struct node)); memset(rnode, 0, sizeof(struct node)); } store_modified = 1; switch (slog.op) { case CYON_OP_SETAUTH: if (slog.klen != SHA256_DIGEST_LENGTH) { cyon_log(LOG_NOTICE, "replay of setauth log entry failed"); break; } if (store_passphrase != NULL) cyon_mem_free(store_passphrase); store_passphrase = cyon_malloc(slog.klen); memcpy(store_passphrase, key, slog.klen); break; case CYON_OP_PUT: if (!cyon_store_put(key, slog.klen, data, slog.dlen, slog.flags, &err)) { if (when != CYON_REPLAY_REQUEST) fatal("replay failed at this stage?"); } added++; break; case CYON_OP_DEL: if (!cyon_store_del(key, slog.klen, &err)) { if (when != CYON_REPLAY_REQUEST) fatal("replay failed at this stage?"); } removed++; break; case CYON_OP_REPLACE: if (!cyon_store_replace(key, slog.klen, data, slog.dlen, &err)) { if (when != CYON_REPLAY_REQUEST) fatal("replay failed at this stage?"); } break; default: store_errors++; printf("unknown log operation %d\n", slog.op); break; } } if (buf != NULL) cyon_mem_free(buf); if (store_errors) { cyon_log(LOG_NOTICE, "LOG REPLAY *FAILED*, SEE ERRORS ABOVE"); if (when == CYON_REPLAY_REQUEST) { cyon_readonly_mode = 1; cyon_log(LOG_NOTICE, "FORCING READONLY MODE"); } } else { cyon_log(LOG_NOTICE, "log replay completed: %ld added, %ld removed", added, removed); } close(lfd); replaying_log = 0; if (!store_errors && store_retain_logs) { cyon_store_current_state(store_state); cyon_sha_hex(store_state, &hex); cyon_log(LOG_NOTICE, "store state is %s", hex); } return ((store_errors) ? CYON_RESULT_ERROR : CYON_RESULT_OK); }
int main(void) { struct MD5Context md5c; SHA_State sha1s; unsigned char keybuf[20], testbuf[64]; int i, j; char *p; static char *test[]={ "", "a", "abc", "message digest", "abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", "12345678901234567890123456789012345678901234567890123456789012345678901234567890", }; static char *md5[]={ "d41d8cd98f00b204e9800998ecf8427e", "0cc175b9c0f1b6a831c399e269772661", "900150983cd24fb0d6963f7d28e17f72", "f96b697d7cb7938d525a2f31aaf161d0", "c3fcd3d76192e4007dfb496cca67e13b", "d174ab98d277d9f5a5611c2c9f419d9f", "57edf4a22be3c955ac49da2e2107b67a", }; static char *sha1[]={ "da39a3ee5e6b4b0d3255bfef95601890afd80709", "86f7e437faa5a7fce15d1ddcb9eaeaea377667b8", "a9993e364706816aba3e25717850c26c9cd0d89d", "c12252ceda8be8994d5fa0290a47231c1d16aae3", "32d10c7b8cf96570ca04ce37f2a19d84240d3a89", "761c457bf73b14d27e9e9265c46f4b4dda11f940", "50abf5706a150990a08b2c5ea40fa0e585554732", }; printf("testing MD5...\n"); for (i = 0; i < sizeof(test) / sizeof(char *); i++) { MD5Init(&md5c); MD5Update(&md5c, test[i], strlen(test[i])); MD5Final(keybuf, &md5c); for (j = 0, p = testbuf; j < 16; j++, p += 2) sprintf(p, "%02x", keybuf[j]); printf("test %d %s!\n", i + 1, strncmp(md5[i], testbuf, sizeof(testbuf)) ? "failed" : "ok"); } printf("\ntesting SHA1...\n"); for (i = 0; i < sizeof(test) / sizeof(char *); i++) { SHA_Init(&sha1s); SHA_Bytes(&sha1s, test[i], strlen(test[i])); SHA_Final(&sha1s, keybuf); for (j = 0, p = testbuf; j < 20; j++, p += 2) sprintf(p, "%02x", keybuf[j]); printf("test %d %s!\n", i + 1, strncmp(sha1[i], testbuf, sizeof(testbuf)) ? "failed" : "ok"); } return 0; }