uint8_t *SHA512(const uint8_t *data, size_t len, uint8_t *out) { SHA512_CTX ctx; static uint8_t buf[SHA512_DIGEST_LENGTH]; /* TODO(fork): remove this static buffer. */ if (out == NULL) { out = buf; } SHA512_Init(&ctx); SHA512_Update(&ctx, data, len); SHA512_Final(out, &ctx); OPENSSL_cleanse(&ctx, sizeof(ctx)); return out; }
static void calc_checksum(uint8_t version, uint8_t stream, const uint8_t *ripe, size_t ripe_length, uint8_t *output) { SHA512_CTX sha_ctx; uint8_t hash1[SHA512_DIGEST_LENGTH]; uint8_t hash2[SHA512_DIGEST_LENGTH]; SHA512_Init(&sha_ctx); SHA512_Update(&sha_ctx, &version, 1); SHA512_Update(&sha_ctx, &stream, 1); SHA512_Update(&sha_ctx, ripe, ripe_length); SHA512_Final(hash1, &sha_ctx); SHA512_Init(&sha_ctx); SHA512_Update(&sha_ctx, hash1, SHA512_DIGEST_LENGTH); SHA512_Final(hash2, &sha_ctx); memcpy(output, hash2, 4); }
/* Begin SHA512 HMAC functions */ static void hmac_sha512_init(hmac_sha512_ctx *ctx, const char *key, const int key_len) { unsigned char final_key[MAX_DIGEST_BLOCK_LEN] = {0}; int final_len = key_len; if(key_len > MAX_DIGEST_BLOCK_LEN) final_len = MAX_DIGEST_BLOCK_LEN; /* When we eventually support arbitrary key sizes, take the digest * of the key with: sha512(final_key, init_key, final_len); */ memcpy(final_key, key, final_len); pad_init(ctx->block_inner_pad, ctx->block_outer_pad, final_key, final_len); SHA512_Init(&ctx->ctx_inside); SHA512_Update(&ctx->ctx_inside, ctx->block_inner_pad, SHA512_BLOCK_LEN); SHA512_Init(&ctx->ctx_outside); SHA512_Update(&ctx->ctx_outside, ctx->block_outer_pad, SHA512_BLOCK_LEN); return; }
static void crypt_all(int count) { int index = 0; #ifdef _OPENMP #pragma omp parallel for for (index = 0; index < count; index++) #endif { SHA512_CTX ctx; SHA512_Init(&ctx); SHA512_Update(&ctx, saved_key[index], saved_key_length[index]); SHA512_Final((unsigned char *)crypt_out[index], &ctx); } }
static std::string sha512(std::istream &stream, size_t iterations = 1) noexcept { SHA512_CTX context; SHA512_Init(&context); std::streamsize read_length; std::vector<char> buffer(buffer_size); while((read_length = stream.read(&buffer[0], buffer_size).gcount()) > 0) SHA512_Update(&context, buffer.data(), static_cast<size_t>(read_length)); std::string hash; hash.resize(512 / 8); SHA512_Final(reinterpret_cast<unsigned char *>(&hash[0]), &context); for(size_t c = 1; c < iterations; ++c) SHA512(reinterpret_cast<const unsigned char *>(&hash[0]), hash.size(), reinterpret_cast<unsigned char *>(&hash[0])); return hash; }
void sha512(char *string, char *salt, char outputBuffer[SHA512_OUTPUT]) { unsigned char hash[SHA512_DIGEST_LENGTH]; SHA512_CTX sha512; SHA512_Init(&sha512); SHA512_Update(&sha512, string, strlen(string)); /* Concatenate salt fo the password */ SHA512_Update(&sha512, salt, strlen(salt)); SHA512_Final(hash, &sha512); int i = 0; for(i = 0; i < SHA512_DIGEST_LENGTH; i++) { sprintf(outputBuffer + (i * 2), "%02x", hash[i]); } outputBuffer[SHA512_OUTPUT-1] = 0; }
void digidoc::Digest::reset() { int result = 1; switch(d->method) { case NID_sha1: result = SHA1_Init(&d->sha1); break; case NID_sha224: result = SHA224_Init(&d->sha256); break; case NID_sha256: result = SHA256_Init(&d->sha256); break; case NID_sha384: result = SHA384_Init(&d->sha512); break; case NID_sha512: result = SHA512_Init(&d->sha512); break; default: break; } d->digest.clear(); if(result != 1) THROW_IOEXCEPTION("Failed to initialize %s digest calculator: %s", getName().c_str(), ERR_reason_error_string(ERR_get_error())); }
bool msl::hash_sha512(const std::string& plain,std::string& hash) { std::string temp_hash; temp_hash.resize(SHA512_DIGEST_LENGTH); SHA512_CTX ctx; if(SHA512_Init(&ctx)==1&&SHA512_Update(&ctx,(unsigned char*)plain.c_str(),plain.size())==1&& SHA512_Final((unsigned char*)temp_hash.data(),&ctx)==1) { hash=temp_hash; return true; } return false; }
string FileHash::generate() { char buf[512]; SHA512_CTX sha; unsigned char out[SHA512_DIGEST_LENGTH]; SHA512_Init(&sha); while (!file_.eof()) { memset(buf, 0x00, 512); file_.read(buf, 512); SHA512_Update(&sha, buf, 512); } SHA512_Final(out, &sha); return hashToStr(out); }
static int compress_sha512 (uint8_t *out, const uint8_t *blocks[], unsigned int blocks_to_comp) { SHA512_CTX ctx; if (!SHA512_Init (&ctx)) return ERROR_OPENSSL_HASH; for (unsigned int i = 0; i < blocks_to_comp; i++) { if (!SHA512_Update (&ctx, blocks[i], SHA512_DIGEST_LENGTH)) return ERROR_OPENSSL_HASH; } if (!SHA512_Final (out, &ctx)) return ERROR_OPENSSL_HASH; return ERROR_NONE; }
static mrb_value sha512_initialize(mrb_state *mrb, mrb_value self) { char *str; int len; SHA512_CTX *ctx = (SHA512_CTX*)mrb_malloc(mrb, sizeof(SHA512_CTX)); SHA512_Init(ctx); DATA_TYPE(self) = &sha512_type; DATA_PTR(self) = ctx; if (mrb_get_args(mrb, "|s", &str, &len) == 1) { SHA512_Update(ctx, (const u_int8_t *)str, len); } return self; }
//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; }
static void kprintf_rnd_get(size_t bytes, void *priv) { if (kprnd_added) { KASSERT(kprintf_inited); if (mutex_tryenter(&kprintf_mtx)) { SHA512_Final(kprnd_accum, &kprnd_sha); rnd_add_data(&rnd_printf_source, kprnd_accum, sizeof(kprnd_accum), 0); kprnd_added = 0; /* This, we must do, since we called _Final. */ SHA512_Init(&kprnd_sha); /* This is optional but seems useful. */ SHA512_Update(&kprnd_sha, kprnd_accum, sizeof(kprnd_accum)); mutex_exit(&kprintf_mtx); } } }
core::data sha2::digest512(core::data const& da) { core::data tmp = da.shadow(); SHA512_CTX ctx; SHA512_Init(&ctx); while (tmp.length()) { SHA512_Update(&ctx, tmp.bytes(), tmp.limit(SHA512_DIGEST_LENGTH)); tmp.offset(tmp.limit(SHA512_DIGEST_LENGTH)); } core::data ret(SHA512_DIGEST_LENGTH); SHA512_Final((byte*)ret.bytes(), &ctx); return ret; }
static int hash_sha512( const struct berval *scheme, const struct berval *passwd, struct berval *hash, const char **text ) { SHA512_CTX ct; unsigned char hash512[SHA512_DIGEST_LENGTH]; struct berval digest; digest.bv_val = (char *) hash512; digest.bv_len = sizeof(hash512); SHA512_Init(&ct); SHA512_Update(&ct, (const uint8_t*)passwd->bv_val, passwd->bv_len); SHA512_Final(hash512, &ct); return lutil_passwd_string64(scheme, &digest, hash, NULL); }
void g_eli_crypto_hmac_final(struct hmac_ctx *ctx, uint8_t *md, size_t mdsize) { u_char digest[SHA512_MDLEN]; SHA512_CTX lctx; SHA512_Final(digest, &ctx->shactx); /* Perform outer SHA512. */ SHA512_Init(&lctx); SHA512_Update(&lctx, ctx->k_opad, sizeof(ctx->k_opad)); bzero(ctx, sizeof(*ctx)); SHA512_Update(&lctx, digest, sizeof(digest)); SHA512_Final(digest, &lctx); /* mdsize == 0 means "Give me the whole hash!" */ if (mdsize == 0) mdsize = SHA512_MDLEN; bcopy(digest, md, mdsize); }
std::string transformToSHA512(std::string plainText, bool upperCase) { SHA512_CTX c; SHA512_Init(&c); SHA512_Update(&c, plainText.c_str(), plainText.length()); uint8_t md[SHA512_DIGEST_LENGTH]; SHA512_Final(md, &c); char output[SHA512_DIGEST_LENGTH * 2 + 1] = ""; for(int32_t i = 0; i < (int32_t)sizeof(md); i++) sprintf(output + i*2, "%.2X", md[i]); if(upperCase) return std::string(output); return asLowerCaseString(std::string(output)); }
static void _wi_sha2_ctx_init(wi_sha2_bits_t bits, _wi_sha2_ctx_t *ctx) { ctx->bits = bits; #ifdef WI_SHA2_OPENSSL switch(ctx->bits) { case WI_SHA2_224: SHA224_Init(&ctx->openssl_256_ctx); break; case WI_SHA2_256: SHA256_Init(&ctx->openssl_256_ctx); break; case WI_SHA2_384: SHA384_Init(&ctx->openssl_512_ctx); break; case WI_SHA2_512: SHA512_Init(&ctx->openssl_512_ctx); break; } #endif #ifdef WI_SHA2_COMMONCRYPTO switch(ctx->bits) { case WI_SHA2_224: CC_SHA224_Init(&ctx->commondigest_256_ctx); break; case WI_SHA2_256: CC_SHA256_Init(&ctx->commondigest_256_ctx); break; case WI_SHA2_384: CC_SHA384_Init(&ctx->commondigest_512_ctx); break; case WI_SHA2_512: CC_SHA512_Init(&ctx->commondigest_512_ctx); break; } #endif }
static int chk_sha512( const struct berval *scheme, /* Scheme of hashed reference password */ const struct berval *passwd, /* Hashed reference password to check against */ const struct berval *cred, /* user-supplied password to check */ const char **text ) { SHA512_CTX SHAcontext; unsigned char SHAdigest[SHA512_DIGEST_LENGTH]; int rc; unsigned char *orig_pass = NULL; size_t decode_len = LUTIL_BASE64_DECODE_LEN(passwd->bv_len); /* safety check */ if (decode_len < sizeof(SHAdigest)) { return LUTIL_PASSWD_ERR; } /* base64 un-encode password */ orig_pass = (unsigned char *) ber_memalloc(decode_len + 1); if( orig_pass == NULL ) return LUTIL_PASSWD_ERR; rc = lutil_b64_pton(passwd->bv_val, orig_pass, decode_len); if( rc != sizeof(SHAdigest) ) { ber_memfree(orig_pass); return LUTIL_PASSWD_ERR; } /* hash credentials with salt */ SHA512_Init(&SHAcontext); SHA512_Update(&SHAcontext, (const unsigned char *) cred->bv_val, cred->bv_len); SHA512_Final(SHAdigest, &SHAcontext); /* compare */ rc = memcmp((char *)orig_pass, (char *)SHAdigest, sizeof(SHAdigest)); #ifdef SLAPD_SHA2_DEBUG chk_sha_debug(scheme, passwd, cred, (char *)SHAdigest, sizeof(SHAdigest), rc); #endif ber_memfree(orig_pass); return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK; }
void GetRandomBytes(unsigned char *buffer, int size) { #if defined(_MSC_VER) HCRYPTPROV hcp; CryptAcquireContext(&hcp, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT); CryptGenRandom(hcp, size, buffer); CryptReleaseContext(hcp, 0); #else FILE *fp = fopen("/dev/urandom", "r"); fread(buffer, sizeof(unsigned char), size, fp); fclose(fp); #endif // -- paranoia ---------------------------------------------------------- // // System level RNG's could be compromized, monitored, hacked, hooked,... // // We are putting a custom layer of transformation on top which includes // a secret key // // ---------------------------------------------------------------------- while (size > 0) { SHA512_CTX hash; SHA512_Init(&hash); SHA512_Update(&hash, my_secret_key, sizeof(my_secret_key)); SHA512_Update(&hash, buffer, size); if (size <= SHA512_DIGEST_LENGTH) { unsigned char digest[SHA512_DIGEST_LENGTH]; SHA512_Final(digest, &hash); memcpy(buffer, digest, size); break; } SHA512_Final(buffer, &hash); buffer += SHA512_DIGEST_LENGTH; size -= SHA512_DIGEST_LENGTH; } }
static void SHA512_File(FILE *file, unsigned char **output, int *outlength) { *output = new unsigned char[SHA512_DIGEST_LENGTH]; *outlength = SHA512_DIGEST_LENGTH; SHA512_CTX c; int i; unsigned char buf[SHA384_FILE_BUFFER_SIZE]; SHA512_Init(&c); for (;;) { i = fread(buf,1,SHA512_FILE_BUFFER_SIZE,file); if(i <= 0) break; SHA512_Update(&c,buf,(unsigned long)i); } SHA512_Final(*output, &c); }
static void S2KItSaltedSHA512Generator(char *password, unsigned char *key, int length) { unsigned char keybuf[KEYBUFFER_LENGTH]; SHA512_CTX ctx; int i, j; int32_t tl; int32_t mul; int32_t bs; uint8_t *bptr; int32_t n; uint32_t numHashes = (length + SHA512_DIGEST_LENGTH - 1) / SHA512_DIGEST_LENGTH; memcpy(keybuf, cur_salt->salt, SALT_LENGTH); // TODO: This is not very efficient with multiple hashes for (i = 0; i < numHashes; i++) { SHA512_Init(&ctx); for (j = 0; j < i; j++) { SHA512_Update(&ctx, "\0", 1); } // Find multiplicator tl = strlen(password) + SALT_LENGTH; mul = 1; while (mul < tl && ((64 * mul) % tl)) { ++mul; } // Try to feed the hash function with 64-byte blocks bs = mul * 64; bptr = keybuf + tl; n = bs / tl; memcpy(keybuf + SALT_LENGTH, password, strlen(password)); while (n-- > 1) { memcpy(bptr, keybuf, tl); bptr += tl; } n = cur_salt->count / bs; while (n-- > 0) { SHA512_Update(&ctx, keybuf, bs); } SHA512_Update(&ctx, keybuf, cur_salt->count % bs); SHA512_Final(key + (i * SHA512_DIGEST_LENGTH), &ctx); } }
/* -- Blinding ------------------------------------------------------------- // // Blinding is a measure to protect against side channel attacks. // Blinding randomizes the scalar multiplier. // // Instead of calculating a*P, calculate (a+b mod BPO)*P + B // // Where b = random blinding and B = -b*P // // ------------------------------------------------------------------------- */ void *ed25519_Blinding_Init( void *context, /* IO: null or ptr blinding context */ const unsigned char *seed, /* IN: [size bytes] random blinding seed */ size_t size) /* IN: size of blinding seed */ { struct { Ext_POINT T; U_WORD t[K_WORDS]; SHA512_CTX H; U8 digest[SHA512_DIGEST_LENGTH]; } d; EDP_BLINDING_CTX *ctx = (EDP_BLINDING_CTX*)context; if (ctx == 0) { ctx = (EDP_BLINDING_CTX*)mem_alloc(sizeof(EDP_BLINDING_CTX)); if (ctx == 0) return 0; } /* Use edp_custom_blinding to protect generation of the new blinder */ SHA512_Init(&d.H); SHA512_Update(&d.H, edp_custom_blinding.zr, 32); SHA512_Update(&d.H, seed, size); SHA512_Final(d.digest, &d.H); ecp_BytesToWords(ctx->zr, d.digest+32); ecp_BytesToWords(d.t, d.digest); eco_Mod(d.t); ecp_Sub(ctx->bl, _w_BPO, d.t); eco_AddReduce(d.t, d.t, edp_custom_blinding.bl); edp_BasePointMult(&d.T, d.t, edp_custom_blinding.zr); edp_AddPoint(&d.T, &d.T, &edp_custom_blinding.BP); edp_ExtPoint2PE(&ctx->BP, &d.T); /* clear potentially sensitive data */ mem_clear (&d, sizeof(d)); return ctx; }
ikptr ikrt_openssl_sha512_init (ikpcb * pcb) { #ifdef HAVE_SHA512_INIT SHA512_CTX * ctx; int rv; ctx = malloc(sizeof(SHA512_CTX)); if (ctx) { rv = SHA512_Init(ctx); if (rv) return ika_pointer_alloc(pcb, (long)ctx); else free(ctx); } return IK_FALSE; #else feature_failure(__func__); #endif }
void Session::genPasswdHash(char *outHash, const char *passwd) { SHA512_CTX sha; unsigned char hash[SHA512_DIGEST_LENGTH]; SHA512_Init(&sha); SHA512_Update(&sha, passwd, strlen(passwd)); SHA512_Final(hash, &sha); memset(outHash, 0x00, 515); strcpy(outHash, ""); for (unsigned i = 0; i < SHA512_DIGEST_LENGTH; i++) { char sym[4]; sprintf(sym, "%02X", hash[i]); strcat(outHash, sym); } }
void calc_hash(const char *filename) { int n; unsigned char data[BUFFSIZE]; FILE *file = fopen(filename, "rb"); if (file == NULL) { fprintf(stderr, "'%s' ", filename); perror("文件打开失败"); return; } unsigned char md5[MD5_DIGEST_LENGTH]; MD5_CTX md5_c; unsigned char sha1[SHA_DIGEST_LENGTH]; SHA_CTX sha1_c; unsigned char sha512[SHA_DIGEST_LENGTH]; SHA512_CTX sha512_c; MD5_Init(&md5_c); SHA1_Init(&sha1_c); SHA512_Init(&sha512_c); while ((n = fread(data, 1, BUFFSIZE, file)) != 0) { MD5_Update(&md5_c, data, n); SHA1_Update(&sha1_c, data, n); SHA512_Update(&sha512_c, data, n); } MD5_Final(md5, &md5_c); SHA1_Final(sha1, &sha1_c); SHA512_Final(sha512, &sha512_c); int i; printf("%s", filename); printf("\n%8s: ", "MD5"); for (i = 0; i < MD5_DIGEST_LENGTH; i++) printf("%02x", md5[i]); printf("\n%8s: ", "SHA1"); for (i = 0; i < SHA_DIGEST_LENGTH; i++) printf("%02x", sha1[i]); printf("\n%8s: ", "SHA512"); for (i = 0; i < SHA_DIGEST_LENGTH; i++) printf("%02x", sha512[i]); printf("\n"); fclose(file); }
int sha_file(int md_alg, const char *filename, unsigned char *md) { FILE *inFile; SHA256_CTX shaContext; SHA512_CTX sha512Context; int bytes; unsigned char data[BUFFER_SIZE]; if ((filename == NULL) || (md == NULL)) { ERROR("%s(): NULL argument\n", __FUNCTION__); return 0; } inFile = fopen(filename, "rb"); if (inFile == NULL) { ERROR("Cannot read %s\n", filename); return 0; } if (md_alg == HASH_ALG_SHA384) { SHA384_Init(&sha512Context); while ((bytes = fread(data, 1, BUFFER_SIZE, inFile)) != 0) { SHA384_Update(&sha512Context, data, bytes); } SHA384_Final(md, &sha512Context); } else if (md_alg == HASH_ALG_SHA512) { SHA512_Init(&sha512Context); while ((bytes = fread(data, 1, BUFFER_SIZE, inFile)) != 0) { SHA512_Update(&sha512Context, data, bytes); } SHA512_Final(md, &sha512Context); } else { SHA256_Init(&shaContext); while ((bytes = fread(data, 1, BUFFER_SIZE, inFile)) != 0) { SHA256_Update(&shaContext, data, bytes); } SHA256_Final(md, &shaContext); } fclose(inFile); return 1; }
static int32_t sha512(char *in, char **out) { SHA512_CTX ctx; uint32_t i = 0; int32_t rtrn = 0; unsigned char hash[SHA512_DIGEST_LENGTH]; rtrn = SHA512_Init(&ctx); if(rtrn < 0) { output->write(ERROR,"Sha init\n"); return (-1); } rtrn = SHA512_Update(&ctx, in, strlen(in)); if(rtrn < 0) { output->write(ERROR,"Can't update input string\n"); return (-1); } rtrn = SHA512_Final(hash, &ctx); if(rtrn < 0) { output->write(ERROR,"Can't do this\n"); return (-1); } (*out) = allocator->alloc((SHA512_DIGEST_LENGTH * 2) + 1); if((*out) == NULL) { output->write(ERROR,"Can't allocate output buf\n"); return (-1); } for(i = 0; i < SHA512_DIGEST_LENGTH; i++) sprintf(*out + (i * 2), "%02x", hash[i]); (*out)[128] = '\0'; return (0); }
char * sha512_hex_hash(const char * passwd) { SHA512_CTX ct; unsigned char hash[SHA512_DIGEST_LENGTH]; static char real_hash[LUTIL_BASE64_ENCODE_LEN(SHA512_DIGEST_LENGTH)+1]; // extra char for \0 SHA512_Init(&ct); SHA512_Update(&ct, (const uint8_t*)passwd, strlen(passwd)); SHA512_Final(hash, &ct); /* base64 encode it */ lutil_b64_ntop( hash, SHA512_DIGEST_LENGTH, real_hash, LUTIL_BASE64_ENCODE_LEN(SHA512_DIGEST_LENGTH)+1 ); return real_hash; }
bytes OsslSha512::hash(const bytes &in) { int len; unsigned char *arr = Conversion::bytes2array(in, &len); SHA512_CTX context; unsigned char md[SHA512_DIGEST_LENGTH]; SHA512_Init(&context); SHA512_Update(&context, arr, len); SHA512_Final(md, &context); bytes ret = Conversion::array2bytes(md, SHA512_DIGEST_LENGTH); free(arr); return ret; }