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; }
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 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); }
int main() { char *data = "Hello, world"; size_t length = strlen(data); printf("%d\n", length); unsigned char hash[SHA_DIGEST_LENGTH]; SHA1(data, length, hash); unsigned char hash2[SHA_DIGEST_LENGTH]; SHA1(data, length, hash2); SHA_CTX ctx; SHA1_Init(&ctx); printf("%s\n", data+length-1); SHA_Update(&ctx, data, 3); SHA_Update(&ctx, data+3, length-3); SHA1_Final(hash, &ctx); printf("%d\n", strncmp(hash, hash2, 10)); }
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); }
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; }
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 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); }
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; }
void plSHAChecksum::AddTo(size_t size, const uint8_t* buffer) { SHA_Update(&fContext, buffer, size); }
static int update(EVP_MD_CTX *ctx,const void *data,size_t count) { return SHA_Update(ctx->md_data,data,count); }
static int update(EVP_MD_CTX *ctx,const void *data,unsigned long count) { return SHA_Update(ctx->md_data,data,count); }
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); }
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); }
static int LAME_ssh2_load_userkey(char *passphrase) { int passlen = strlen(passphrase); unsigned char out[sizeof(cur_salt->private_blob)]; AES_KEY akey; unsigned char iv[32]; /* Decrypt the private blob. */ if (cur_salt->cipher) { unsigned char key[40]; SHA_CTX s; if (cur_salt->private_blob_len % cur_salt->cipherblk) goto error; SHA1_Init(&s); SHA1_Update(&s, (void*)"\0\0\0\0", 4); SHA1_Update(&s, passphrase, passlen); SHA1_Final(key + 0, &s); SHA1_Init(&s); SHA1_Update(&s, (void*)"\0\0\0\1", 4); SHA1_Update(&s, passphrase, passlen); SHA1_Final(key + 20, &s); memset(iv, 0, 32); memset(&akey, 0, sizeof(AES_KEY)); if(AES_set_decrypt_key(key, 256, &akey) < 0) { fprintf(stderr, "AES_set_decrypt_key failed!\n"); } AES_cbc_encrypt(cur_salt->private_blob, out , cur_salt->private_blob_len, &akey, iv, AES_DECRYPT); } /* Verify the MAC. */ { char realmac[41]; unsigned char binary[20]; unsigned char *macdata; unsigned char macdata_ar[4*5+sizeof(cur_salt->alg)+sizeof(cur_salt->encryption)+sizeof(cur_salt->comment)+sizeof(cur_salt->public_blob_len)+sizeof(cur_salt->private_blob_len)+1]; int maclen; int i; if (cur_salt->old_fmt) { /* MAC (or hash) only covers the private blob. */ macdata = out; maclen = cur_salt->private_blob_len; } else { unsigned char *p; int namelen = strlen(cur_salt->alg); int enclen = strlen(cur_salt->encryption); int commlen = strlen(cur_salt->comment); maclen = (4 + namelen + 4 + enclen + 4 + commlen + 4 + cur_salt->public_blob_len + 4 + cur_salt->private_blob_len); p = macdata_ar; #define DO_STR(s,len) PUT_32BIT(p,(len));memcpy(p+4,(s),(len));p+=4+(len) DO_STR(cur_salt->alg, namelen); DO_STR(cur_salt->encryption, enclen); DO_STR(cur_salt->comment, commlen); DO_STR(cur_salt->public_blob, cur_salt->public_blob_len); DO_STR(out, cur_salt->private_blob_len); macdata = macdata_ar; } if (cur_salt->is_mac) { SHA_CTX s; unsigned char mackey[20]; unsigned int length = 20; // HMAC_CTX ctx; char header[] = "putty-private-key-file-mac-key"; SHA1_Init(&s); SHA1_Update(&s, header, sizeof(header)-1); if (cur_salt->cipher && passphrase) SHA_Update(&s, passphrase, passlen); SHA1_Final(mackey, &s); hmac_sha1(mackey, 20, macdata, maclen, binary, length); /* HMAC_Init(&ctx, mackey, 20, EVP_sha1()); * HMAC_Update(&ctx, macdata, maclen); * HMAC_Final(&ctx, binary, &length); * HMAC_CTX_cleanup(&ctx); */ } else { SHA_Simple(macdata, maclen, binary); } for (i = 0; i < 20; i++) sprintf(realmac + 2 * i, "%02x", binary[i]); if (strcmp(cur_salt->mac, realmac) == 0) return 1; } error: return 0; }
static void cyon_store_writenode(int fd, struct node *p, u_int8_t *buf, u_int32_t blen, u_int32_t *len, SHA_CTX *sctx) { struct node *np; u_int32_t offset, i, rlen, tlen, slen; if (p->flags & NODE_FLAG_HASDATA) NODE_REGION_OFFSET(offset, p->region); else offset = 0; if (p->rbase == 0 && p->rtop == 0) { tlen = offset; } else { NODE_REGION_RANGE(tlen, p); tlen += offset; } if (p == rnode) tlen += sizeof(struct node); if ((*len + tlen) >= blen) { if (store_validation) SHA_Update(sctx, buf, *len); else cyon_atomic_write(fd, buf, *len, &shactx); *len = 0; } if (p == rnode) { tlen -= sizeof(struct node); memcpy(buf + *len, p, sizeof(struct node)); /* Same hack as lower down for checksum. */ np = (struct node *)(buf + *len); np->region = NULL; *len += sizeof(struct node); } if (p->region == NULL) return; slen = *len; memcpy(buf + *len, p->region, tlen); *len += tlen; if (p->rtop == 0 && p->rbase == 0) return; rlen = (p->rtop - p->rbase) + 1; for (i = 0; i < rlen; i++) { /* * Set the region to NULL for the data we are going to * write otherwise the checksum will be weird per machine. */ np = (struct node *)((u_int8_t *)(buf + slen) + offset + (i * sizeof(struct node))); np->region = NULL; np = (struct node *)((u_int8_t *)p->region + offset + (i * sizeof(struct node))); cyon_store_writenode(fd, np, buf, blen, len, sctx); } }
void SHA0Stream::updateHash(const void *buffer, size_t length) { SHA_Update(&m_ctx, buffer, length); }