hasher512& hasher512::operator=(hasher512 const& h) & { if (this == &h) return *this; gcry_md_close(m_context); gcry_md_copy(&m_context, h.m_context); return *this; }
hasher& hasher::operator=(hasher const& h) { if (this == &h) return; gcry_md_close(m_context); gcry_md_copy(&m_context, h.m_context); return *this; }
/* deterministically generate from seed/idx a string of buflen pseudorandom bytes */ static void det_randomize(void *buf, size_t buflen, const void *seed, size_t seedlen, uint32_t idx) { gcry_md_hd_t hd, hd2; size_t olen, cpylen; uint32_t ctr; olen = gcry_md_get_algo_dlen(RND_HASH); gcry_md_open(&hd, RND_HASH, 0); gcry_md_write(hd, seed, seedlen); gcry_md_putc(hd, (idx >> 24) & 0xff); gcry_md_putc(hd, (idx >> 16) & 0xff); gcry_md_putc(hd, (idx >> 8) & 0xff); gcry_md_putc(hd, (idx >> 0) & 0xff); for (ctr = 0; buflen; ctr++) { gcry_md_copy(&hd2, hd); gcry_md_putc(hd2, (ctr >> 24) & 0xff); gcry_md_putc(hd2, (ctr >> 16) & 0xff); gcry_md_putc(hd2, (ctr >> 8) & 0xff); gcry_md_putc(hd2, (ctr >> 0) & 0xff); gcry_md_final(hd2); cpylen = (buflen < olen) ? buflen : olen; memcpy(buf, gcry_md_read(hd2, RND_HASH), cpylen); gcry_md_close(hd2); buf += cpylen; buflen -= cpylen; } gcry_md_close(hd); }
static void check_one_md (int algo, char *data, int len, char *expect) { gcry_md_hd_t hd, hd2; unsigned char *p; int mdlen; int i; gcry_error_t err = 0; err = gcry_md_open (&hd, algo, 0); if (err) { fail ("algo %d, grcy_md_open failed: %s\n", algo, gpg_strerror (err)); return; } mdlen = gcry_md_get_algo_dlen (algo); if (mdlen < 1 || mdlen > 500) { fail ("algo %d, grcy_md_get_algo_dlen failed: %d\n", algo, mdlen); return; } if (*data == '!' && !data[1]) { /* hash one million times a "a" */ char aaa[1000]; memset (aaa, 'a', 1000); for (i = 0; i < 1000; i++) gcry_md_write (hd, aaa, 1000); } else gcry_md_write (hd, data, len); err = gcry_md_copy (&hd2, hd); if (err) { fail ("algo %d, gcry_md_copy failed: %s\n", algo, gpg_strerror (err)); } gcry_md_close (hd); p = gcry_md_read (hd2, algo); if (memcmp (p, expect, mdlen)) { printf ("computed: "); for (i = 0; i < mdlen; i++) printf ("%02x ", p[i] & 0xFF); printf ("\nexpected: "); for (i = 0; i < mdlen; i++) printf ("%02x ", expect[i] & 0xFF); printf ("\n"); fail ("algo %d, digest mismatch\n", algo); } gcry_md_close (hd2); }
static int _md_copy (md_ctx *xdst, md_ctx *xsrc) { gcry_error_t e; if ((e = gcry_md_copy (&(xdst->ctx), xsrc->ctx)) != 0) { log_msg (LOG_DEBUG, "gcry_md_copy failed: %s", gcry_strerror (e)); return (-1); } return (0); }
/* Hmm. We need to have a new_kex structure so that the old kex data can be used until we have send the NEWKEYs msg Well, doesn't matter for now. */ static gsti_bstr_t construct_one_key (gsti_ctx_t ctx, gcry_md_hd_t md1, int algo, const byte * letter, size_t size) { gsti_error_t err; gsti_bstr_t hash; gcry_md_hd_t md; size_t n, n1; if (gcry_md_copy (&md, md1)) abort (); err = gsti_bstr_make (&hash, NULL, size); if (err) { gcry_md_close (md); return NULL; } gcry_md_write (md, letter, 1); gcry_md_write (md, gsti_bstr_data (ctx->session_id), gsti_bstr_length (ctx->session_id)); n = gcry_md_get_algo_dlen (algo); if (n > size) n = size; memcpy (gsti_bstr_data (hash), gcry_md_read (md, algo), n); while (n < size) { gcry_md_close (md); if (gcry_md_copy (&md, md1)) abort (); /* Fixme: add an error return to this fucntion. */ gcry_md_write (md, gsti_bstr_data (hash), n); n1 = gcry_md_get_algo_dlen (algo); if (n1 > size - n) n1 = size - n; memcpy (gsti_bstr_data (hash) + n, gcry_md_read (md, algo), n1); n += n1; } gcry_md_close (md); return hash; }
static int hmac_clone(lua_State *L) { HANDLER_HMAC *c = hmac_pget(L, 1); HANDLER_HMAC *d = hmac_pnew(L); #if CRYPTO_OPENSSL *d = *c; #elif CRYPTO_GCRYPT gcry_md_copy(d, *c); #endif return 1; }
static int evp_clone(lua_State *L) { HANDLER_EVP *c = evp_pget(L, 1); HANDLER_EVP *d = evp_pnew(L); #if CRYPTO_OPENSSL EVP_MD_CTX_init(d); EVP_MD_CTX_copy_ex(d, c); #elif CRYPTO_GCRYPT gcry_md_copy(d, *c); #endif return 1; }
static int evp_digest(lua_State *L) { HANDLER_EVP *c = evp_pget(L, 1); #if CRYPTO_OPENSSL HANDLER_EVP *d = NULL; unsigned char digest[EVP_MAX_MD_SIZE]; #elif CRYPTO_GCRYPT HANDLER_EVP d = NULL; unsigned char *digest; int algo; #endif size_t written = 0; if (lua_isstring(L, 2)) { size_t s_len; const char *s = luaL_checklstring(L, 2, &s_len); EVP_UPDATE(c, s, s_len); } #if CRYPTO_OPENSSL d = EVP_MD_CTX_create(); EVP_MD_CTX_copy_ex(d, c); EVP_DigestFinal_ex(d, digest, &written); EVP_MD_CTX_destroy(d); #elif CRYPTO_GCRYPT algo = gcry_md_get_algo(*c); gcry_md_copy(&d, *c); gcry_md_final(d); digest = gcry_md_read(d, algo); written = gcry_md_get_algo_dlen(algo); #endif if (lua_toboolean(L, 3)) lua_pushlstring(L, (char *)digest, written); else { char *hex = bin2hex(digest, written); lua_pushlstring(L, hex, written*2); free(hex); } #if CRYPTO_GCRYPT gcry_md_close(d); #endif return 1; }
static int hmac_digest(lua_State *L) { HANDLER_HMAC *c = hmac_pget(L, 1); size_t written = 0; #if CRYPTO_OPENSSL unsigned char digest[EVP_MAX_MD_SIZE]; #elif CRYPTO_GCRYPT HANDLER_HMAC d; unsigned char *digest; int algo; #endif if (lua_isstring(L, 2)) { size_t s_len; const char *s = luaL_checklstring(L, 2, &s_len); HMAC_UPDATE(c, s, s_len); } #if CRYPTO_OPENSSL HMAC_Final(c, digest, &written); #elif CRYPTO_GCRYPT algo = gcry_md_get_algo(*c); gcry_md_copy(&d, *c); gcry_md_final(d); digest = gcry_md_read(d, algo); written = gcry_md_get_algo_dlen(algo); #endif if (lua_toboolean(L, 3)) lua_pushlstring(L, (char *)digest, written); else { char *hex = bin2hex(digest, written); lua_pushlstring(L, hex, written*2); free(hex); } #if CRYPTO_GCRYPT gcry_md_close(d); #endif return 1; }
Gc_rc gc_hash_clone (gc_hash_handle handle, gc_hash_handle * outhandle) { _gc_hash_ctx *in = handle; _gc_hash_ctx *out; int err; *outhandle = out = calloc (sizeof (*out), 1); if (!out) return GC_MALLOC_ERROR; memcpy (out, in, sizeof (*out)); err = gcry_md_copy (&out->gch, in->gch); if (err) { free (out); return GC_INVALID_HASH; } return GC_OK; }
/** * ntfs_desx_key_expand - expand a 128-bit desx key to the needed 192-bit key * @src: source buffer containing 128-bit key * * Expands the on-disk 128-bit desx key to the needed des key, the in-, and the * out-whitening keys required to perform desx {de,en}cryption. */ static gcry_error_t ntfs_desx_key_expand(const u8 *src, u32 *des_key, u64 *out_whitening, u64 *in_whitening) { static const u8 *salt1 = (const u8*)"Dan Simon "; static const u8 *salt2 = (const u8*)"Scott Field"; static const int salt_len = 12; gcry_md_hd_t hd1, hd2; u32 *md; gcry_error_t err; err = gcry_md_open(&hd1, GCRY_MD_MD5, 0); if (err != GPG_ERR_NO_ERROR) { ntfs_log_error("Failed to open MD5 digest.\n"); return err; } /* Hash the on-disk key. */ gcry_md_write(hd1, src, 128 / 8); /* Copy the current hash for efficiency. */ err = gcry_md_copy(&hd2, hd1); if (err != GPG_ERR_NO_ERROR) { ntfs_log_error("Failed to copy MD5 digest object.\n"); goto out; } /* Hash with the first salt and store the result. */ gcry_md_write(hd1, salt1, salt_len); md = (u32*)gcry_md_read(hd1, 0); des_key[0] = md[0] ^ md[1]; des_key[1] = md[2] ^ md[3]; /* Hash with the second salt and store the result. */ gcry_md_write(hd2, salt2, salt_len); md = (u32*)gcry_md_read(hd2, 0); *out_whitening = *(u64*)md; *in_whitening = *(u64*)(md + 2); gcry_md_close(hd2); out: gcry_md_close(hd1); return err; }
static int wrap_gcry_md_copy (void **bhd, void *ahd) { return gcry_md_copy ((gcry_md_hd_t *) bhd, (gcry_md_hd_t) ahd); }
hasher512::hasher512(hasher512 const& h) { gcry_md_copy(&m_context, h.m_context); }
static void run_longtest (int algo, int gigs) { gpg_error_t err; gcry_md_hd_t hd; gcry_md_hd_t hd_pre = NULL; gcry_md_hd_t hd_pre2 = NULL; gcry_md_hd_t hd_post = NULL; gcry_md_hd_t hd_post2 = NULL; char pattern[1024]; int i, g; const unsigned char *digest; unsigned int digestlen; memset (pattern, 'a', sizeof pattern); err = gcry_md_open (&hd, algo, 0); if (err) { fail ("gcry_md_open failed for %s (%d): %s", gcry_md_algo_name (algo), algo, gpg_strerror (err)); return; } digestlen = gcry_md_get_algo_dlen (algo); for (g=0; g < gigs; g++) { if (g == gigs - 1) { for (i = 0; i < 1024*1023; i++) gcry_md_write (hd, pattern, sizeof pattern); for (i = 0; i < 1023; i++) gcry_md_write (hd, pattern, sizeof pattern); err = gcry_md_copy (&hd_pre, hd); if (!err) err = gcry_md_copy (&hd_pre2, hd); if (err) die ("gcry_md_copy failed for %s (%d): %s", gcry_md_algo_name (algo), algo, gpg_strerror (err)); gcry_md_write (hd, pattern, sizeof pattern); } else { for (i = 0; i < 1024*1024; i++) gcry_md_write (hd, pattern, sizeof pattern); } if (g && !(g % 16)) show_note ("%d GiB so far hashed with %s", g, gcry_md_algo_name (algo)); } if (g >= 16) show_note ("%d GiB hashed with %s", g, gcry_md_algo_name (algo)); err = gcry_md_copy (&hd_post, hd); if (err) die ("gcry_md_copy failed for %s (%d): %s", gcry_md_algo_name (algo), algo, gpg_strerror (err)); err = gcry_md_copy (&hd_post2, hd); if (err) die ("gcry_md_copy failed for %s (%d): %s", gcry_md_algo_name (algo), algo, gpg_strerror (err)); gcry_md_write (hd_pre2, pattern, sizeof pattern - 64); gcry_md_write (hd_pre, pattern, sizeof pattern - 1); gcry_md_write (hd_post, pattern, 1); gcry_md_write (hd_post2, pattern, 64); digest = gcry_md_read (hd_pre2, algo); if (cmp_digest (digest, digestlen, algo, gigs, -64) || verbose) showhex (digest, digestlen, "%d GiB %+3d %-10s ", gigs, -64, gcry_md_algo_name (algo)); digest = gcry_md_read (hd_pre, algo); if (cmp_digest (digest, digestlen, algo, gigs, -1) || verbose) showhex (digest, digestlen, "%d GiB %+3d %-10s ", gigs, -1, gcry_md_algo_name (algo)); digest = gcry_md_read (hd, algo); if (cmp_digest (digest, digestlen, algo, gigs, 0) || verbose) showhex (digest, digestlen, "%d GiB %+3d %-10s ", gigs, 0, gcry_md_algo_name (algo)); digest = gcry_md_read (hd_post, algo); if (cmp_digest (digest, digestlen, algo, gigs, 1) || verbose) showhex (digest, digestlen, "%d GiB %+3d %-10s ", gigs, 1, gcry_md_algo_name (algo)); digest = gcry_md_read (hd_post2, algo); if (cmp_digest (digest, digestlen, algo, gigs, 64) || verbose) showhex (digest, digestlen, "%d GiB %+3d %-10s ", gigs, 64, gcry_md_algo_name (algo)); gcry_md_close (hd); gcry_md_close (hd_pre); gcry_md_close (hd_pre2); gcry_md_close (hd_post); gcry_md_close (hd_post2); }
hasher& operator=(hasher const& h) { gcry_md_close(m_context); gcry_md_copy(&m_context, h.m_context); return *this; }
hasher(hasher const& h) { gcry_md_copy(&m_context, h.m_context); }