Пример #1
0
/* functions
*/
  u2_weak                                                         //  produce
  j2_mbc(Pt5, shax)(u2_wire wir_r, 
                    u2_atom a)                                    //  retain
  {
    c3_w  met_w = u2_met(3, a);
    c3_y* fat_y = malloc(met_w + 1);

    u2_bytes(0, met_w, fat_y, a);
    {
      c3_y dig_y[32];
#if defined(U2_OS_linux)
      SHA256_CTX ctx_h;

      SHA256_Init(&ctx_h);
      SHA256_Update(&ctx_h, fat_y, met_w);
      SHA256_Final(dig_y, &ctx_h);
#elif defined(U2_OS_osx)
      CC_SHA256_CTX ctx_h;

      CC_SHA256_Init(&ctx_h);
      CC_SHA256_Update(&ctx_h, fat_y, met_w);
      CC_SHA256_Final(dig_y, &ctx_h);
#endif
      return u2_rl_bytes(wir_r, 32, dig_y);
    }
  }
Пример #2
0
Файл: shax.c Проект: urbit/urbit
  u3_noun
  u3qe_shay(u3_atom a,
            u3_atom b)
  {
    c3_assert(_(u3a_is_cat(a)));
    c3_y* fat_y = u3a_malloc(a + 1);

    u3r_bytes(0, a, fat_y, b);
    {
      c3_y dig_y[32];
#if defined(U3_OS_osx)
      CC_SHA256_CTX ctx_h;

      CC_SHA256_Init(&ctx_h);
      CC_SHA256_Update(&ctx_h, fat_y, a);
      CC_SHA256_Final(dig_y, &ctx_h);
#else
      SHA256_CTX ctx_h;

      SHA256_Init(&ctx_h);
      SHA256_Update(&ctx_h, fat_y, a);
      SHA256_Final(dig_y, &ctx_h);
#endif
      u3a_free(fat_y);
      return u3i_bytes(32, dig_y);
    }
  }
Пример #3
0
static int
__archive_libsystem_sha256update(archive_sha256_ctx *ctx, const void *indata,
    size_t insize)
{
  CC_SHA256_Update(ctx, indata, insize);
  return (ARCHIVE_OK);
}
Пример #4
0
extern "C" int32_t AppleCryptoNative_DigestUpdate(DigestCtx* ctx, uint8_t* pBuf, int32_t cbBuf)
{
    if (cbBuf == 0)
        return 1;
    if (ctx == nullptr || pBuf == nullptr)
        return -1;

    CC_LONG bufSize = static_cast<CC_LONG>(cbBuf);

    switch (ctx->algorithm)
    {
        case PAL_MD5:
            return CC_MD5_Update(&ctx->d.md5, pBuf, bufSize);
        case PAL_SHA1:
            return CC_SHA1_Update(&ctx->d.sha1, pBuf, bufSize);
        case PAL_SHA256:
            return CC_SHA256_Update(&ctx->d.sha256, pBuf, bufSize);
        case PAL_SHA384:
            return CC_SHA384_Update(&ctx->d.sha384, pBuf, bufSize);
        case PAL_SHA512:
            return CC_SHA512_Update(&ctx->d.sha512, pBuf, bufSize);
        default:
            return -1;
    }
}
Пример #5
0
Файл: shax.c Проект: urbit/urbit
//   u3_noun
//   u3qe_shax(
//                     u3_atom a)
//   {
//     c3_w  met_w = u3r_met(3, a);
//     return u3qe_shay(met_w, a);
//   }
//  XX  preformance
u3_noun
  u3qe_shax(u3_atom a)
  {
    c3_w  met_w = u3r_met(3, a);
    c3_y* fat_y = u3a_malloc(met_w + 1);

    u3r_bytes(0, met_w, fat_y, a);
    {
      c3_y dig_y[32];
#if defined(U3_OS_osx)
      CC_SHA256_CTX ctx_h;

      CC_SHA256_Init(&ctx_h);
      CC_SHA256_Update(&ctx_h, fat_y, met_w);
      CC_SHA256_Final(dig_y, &ctx_h);
#else
      SHA256_CTX ctx_h;

      SHA256_Init(&ctx_h);
      SHA256_Update(&ctx_h, fat_y, met_w);
      SHA256_Final(dig_y, &ctx_h);
#endif
      u3a_free(fat_y);
      return u3i_bytes(32, dig_y);
    }
  }
Пример #6
0
static NTSTATUS hash_update( struct hash *hash, UCHAR *input, ULONG size )
{
    switch (hash->alg_id)
    {
    case ALG_ID_SHA1:
        CC_SHA1_Update( &hash->u.sha1_ctx, input, size );
        break;

    case ALG_ID_SHA256:
        CC_SHA256_Update( &hash->u.sha256_ctx, input, size );
        break;

    case ALG_ID_SHA384:
        CC_SHA384_Update( &hash->u.sha512_ctx, input, size );
        break;

    case ALG_ID_SHA512:
        CC_SHA512_Update( &hash->u.sha512_ctx, input, size );
        break;

    default:
        ERR( "unhandled id %u\n", hash->alg_id );
        return STATUS_NOT_IMPLEMENTED;
    }
    return STATUS_SUCCESS;
}
Пример #7
0
            HashResult Sha256CommonCryptoImpl::Calculate(Aws::IStream& stream)
            {
                CC_SHA256_CTX sha256;
                CC_SHA256_Init(&sha256);

                auto currentPos = stream.tellg();
                stream.seekg(0, stream.beg);

                char streamBuffer[Aws::Utils::Crypto::Hash::INTERNAL_HASH_STREAM_BUFFER_SIZE];
                while(stream.good())
                {
                    stream.read(streamBuffer, Aws::Utils::Crypto::Hash::INTERNAL_HASH_STREAM_BUFFER_SIZE);
                    auto bytesRead = stream.gcount();

                    if(bytesRead > 0)
                    {
                        CC_SHA256_Update(&sha256, streamBuffer, static_cast<CC_LONG>(bytesRead));
                    }
                }

                stream.clear();
                stream.seekg(currentPos, stream.beg);

                ByteBuffer hash(CC_SHA256_DIGEST_LENGTH);
                CC_SHA256_Final(hash.GetUnderlyingData(), &sha256);

                return HashResult(std::move(hash));
            }
Пример #8
0
void Download::UpdateWithData (CFDataRef data)
{
	// figure out how much data to hash
	CFIndex dataLength = CFDataGetLength (data);
	const UInt8* finger = CFDataGetBytePtr (data);
	
	while (dataLength > 0)
	{
		// figure out how many bytes are left to hash
		size_t bytesLeftToHash = mSectorSize - mBytesInCurrentDigest;
		size_t bytesToHash = MinSizeT (bytesLeftToHash, dataLength);
		
		// hash the data
		CC_SHA256_Update (&mSHA256Context, finger, bytesToHash);
		
		// update the pointers
		mBytesInCurrentDigest += bytesToHash;
		bytesLeftToHash -= bytesToHash;
		finger += bytesToHash;
		dataLength -= bytesToHash;
		
		if (bytesLeftToHash == 0) // is our digest "full"?
		{
			FinalizeDigestAndCompare ();
		}
	}
}
Пример #9
0
static OSStatus HashSHA256Update(SSLBuffer *digestCtx, const SSLBuffer *data)
{
    /* 64 bits cast: safe, SSL records are always smaller than 2^32 bytes */
    assert(digestCtx->length >= sizeof(CC_SHA256_CTX));
	CC_SHA256_CTX *ctx = (CC_SHA256_CTX *)digestCtx->data;
	CC_SHA256_Update(ctx, data->data, (CC_LONG)data->length);
    return noErr;
}
Пример #10
0
void ocspdSHA256(
	const void		*data,
	CC_LONG			len,
	unsigned char	*md)		// allocd by caller, CC_SHA256_DIGEST_LENGTH bytes
{
	CC_SHA256_CTX ctx;
	CC_SHA256_Init(&ctx);
	CC_SHA256_Update(&ctx, data, len);
	CC_SHA256_Final(md, &ctx);
}
Пример #11
0
void Crypto::DigestUpdate(Digest x, const StringPiece &in) {
  auto d = FromVoid<CCDigest*>(x);
  switch(d->algo) {
    case CCDigestAlgo::MD5:    CC_MD5_Update   (static_cast<CC_MD5_CTX*>   (d->v), in.data(), in.size()); break;
    case CCDigestAlgo::SHA1:   CC_SHA1_Update  (static_cast<CC_SHA1_CTX*>  (d->v), in.data(), in.size()); break;
    case CCDigestAlgo::SHA256: CC_SHA256_Update(static_cast<CC_SHA256_CTX*>(d->v), in.data(), in.size()); break;
    case CCDigestAlgo::SHA384: CC_SHA384_Update(static_cast<CC_SHA512_CTX*>(d->v), in.data(), in.size()); break;
    case CCDigestAlgo::SHA512: CC_SHA512_Update(static_cast<CC_SHA512_CTX*>(d->v), in.data(), in.size()); break;
    default: break;
  }
}
Пример #12
0
const static void signatura_hash(const char *input, char *result)
{
  unsigned char hash[CC_SHA256_DIGEST_LENGTH];
  CC_SHA256_CTX context;
  int i;

  CC_SHA256_Init(&context);
  CC_SHA256_Update(&context, input, strlen(input));
  CC_SHA256_Final(hash, &context);

  for(i = 1; i < CC_SHA256_DIGEST_LENGTH; i++)
  {
    sprintf(result + (i * 2), "%02x", hash[i]);
  }
  result[64] = 0;
}
Пример #13
0
static void
lib_md_update(mrb_state *mrb, struct mrb_md *md, unsigned char *data, mrb_int len)
{
  if (sizeof(len) > sizeof(CC_LONG)) {
    if (len != (CC_LONG)len) {
      mrb_raise(mrb, E_ARGUMENT_ERROR, "too long string (not supported yet)");
    }
  }
  switch (md->type) {
  case MD_TYPE_MD5:	CC_MD5_Update(md->ctx, data, len);	break;
  case MD_TYPE_SHA1:	CC_SHA1_Update(md->ctx, data, len);	break;
  case MD_TYPE_SHA256:	CC_SHA256_Update(md->ctx, data, len);	break;
  case MD_TYPE_SHA384:	CC_SHA384_Update(md->ctx, data, len);	break;
  case MD_TYPE_SHA512:	CC_SHA512_Update(md->ctx, data, len);	break;
  default: break;
  }
}
Пример #14
0
int32_t mz_crypt_sha_update(void *handle, const void *buf, int32_t size)
{
    mz_crypt_sha *sha = (mz_crypt_sha *)handle;

    if (sha == NULL || buf == NULL || !sha->initialized)
        return MZ_PARAM_ERROR;

    if (sha->algorithm == MZ_HASH_SHA1)
        sha->error = CC_SHA1_Update(&sha->ctx1, buf, size);
    else
        sha->error = CC_SHA256_Update(&sha->ctx256, buf, size);
        
    if (!sha->error)
        return MZ_HASH_ERROR;

    return size;
}
void CryptoDigest::addBytes(const void* input, size_t length)
{
    switch (m_context->algorithm) {
    case CryptoDigest::Algorithm::SHA_1:
        CC_SHA1_Update(toSHA1Context(m_context.get()), input, length);
        return;
    case CryptoDigest::Algorithm::SHA_224:
        CC_SHA224_Update(toSHA224Context(m_context.get()), input, length);
        return;
    case CryptoDigest::Algorithm::SHA_256:
        CC_SHA256_Update(toSHA256Context(m_context.get()), input, length);
        return;
    case CryptoDigest::Algorithm::SHA_384:
        CC_SHA384_Update(toSHA384Context(m_context.get()), input, length);
        return;
    case CryptoDigest::Algorithm::SHA_512:
        CC_SHA512_Update(toSHA512Context(m_context.get()), input, length);
        return;
    }
}
Пример #16
0
static void _wi_sha2_ctx_update(_wi_sha2_ctx_t *ctx, const void *data, unsigned long length) {
#ifdef WI_SHA2_OPENSSL
    switch(ctx->bits) {
    case WI_SHA2_224:
        SHA224_Update(&ctx->openssl_256_ctx, data, length);
        break;

    case WI_SHA2_256:
        SHA256_Update(&ctx->openssl_256_ctx, data, length);
        break;

    case WI_SHA2_384:
        SHA384_Update(&ctx->openssl_512_ctx, data, length);
        break;

    case WI_SHA2_512:
        SHA512_Update(&ctx->openssl_512_ctx, data, length);
        break;
    }
#endif

#ifdef WI_SHA2_COMMONCRYPTO
    switch(ctx->bits) {
    case WI_SHA2_224:
        CC_SHA224_Update(&ctx->commondigest_256_ctx, data, length);
        break;

    case WI_SHA2_256:
        CC_SHA256_Update(&ctx->commondigest_256_ctx, data, length);
        break;

    case WI_SHA2_384:
        CC_SHA384_Update(&ctx->commondigest_512_ctx, data, length);
        break;

    case WI_SHA2_512:
        CC_SHA512_Update(&ctx->commondigest_512_ctx, data, length);
        break;
    }
#endif
}
Пример #17
0
void CryptoDigest::addBytes(const void* input, size_t length)
{
    switch (m_context->algorithm) {
    case CryptoAlgorithmIdentifier::SHA_1:
        CC_SHA1_Update(toSHA1Context(m_context.get()), input, length);
        return;
    case CryptoAlgorithmIdentifier::SHA_224:
        CC_SHA224_Update(toSHA224Context(m_context.get()), input, length);
        return;
    case CryptoAlgorithmIdentifier::SHA_256:
        CC_SHA256_Update(toSHA256Context(m_context.get()), input, length);
        return;
    case CryptoAlgorithmIdentifier::SHA_384:
        CC_SHA384_Update(toSHA384Context(m_context.get()), input, length);
        return;
    case CryptoAlgorithmIdentifier::SHA_512:
        CC_SHA512_Update(toSHA512Context(m_context.get()), input, length);
        return;
    default:
        ASSERT_NOT_REACHED();
    }
}
Пример #18
0
int sCCHashUpdateSHA256(void *ctx, const unsigned char *in, unsigned long inlen)
{
    CC_SHA256_Update(ctx, in, (CC_LONG)inlen);
    return CRYPT_OK;
}
Пример #19
0
Файл: hash.c Проект: apple/cups
ssize_t					/* O - Size of hash or -1 on error */
cupsHashData(const char    *algorithm,	/* I - Algorithm name */
             const void    *data,	/* I - Data to hash */
             size_t        datalen,	/* I - Length of data to hash */
             unsigned char *hash,	/* I - Hash buffer */
             size_t        hashsize)	/* I - Size of hash buffer */
{
  if (!algorithm || !data || datalen == 0 || !hash || hashsize == 0)
  {
    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad arguments to function"), 1);
    return (-1);
  }

#ifdef __APPLE__
  if (!strcmp(algorithm, "sha"))
  {
   /*
    * SHA-1...
    */

    CC_SHA1_CTX	ctx;			/* SHA-1 context */

    if (hashsize < CC_SHA1_DIGEST_LENGTH)
      goto too_small;

    CC_SHA1_Init(&ctx);
    CC_SHA1_Update(&ctx, data, (CC_LONG)datalen);
    CC_SHA1_Final(hash, &ctx);

    return (CC_SHA1_DIGEST_LENGTH);
  }
  else if (!strcmp(algorithm, "sha2-224"))
  {
    CC_SHA256_CTX	ctx;		/* SHA-224 context */

    if (hashsize < CC_SHA224_DIGEST_LENGTH)
      goto too_small;

    CC_SHA224_Init(&ctx);
    CC_SHA224_Update(&ctx, data, (CC_LONG)datalen);
    CC_SHA224_Final(hash, &ctx);

    return (CC_SHA224_DIGEST_LENGTH);
  }
  else if (!strcmp(algorithm, "sha2-256"))
  {
    CC_SHA256_CTX	ctx;		/* SHA-256 context */

    if (hashsize < CC_SHA256_DIGEST_LENGTH)
      goto too_small;

    CC_SHA256_Init(&ctx);
    CC_SHA256_Update(&ctx, data, (CC_LONG)datalen);
    CC_SHA256_Final(hash, &ctx);

    return (CC_SHA256_DIGEST_LENGTH);
  }
  else if (!strcmp(algorithm, "sha2-384"))
  {
    CC_SHA512_CTX	ctx;		/* SHA-384 context */

    if (hashsize < CC_SHA384_DIGEST_LENGTH)
      goto too_small;

    CC_SHA384_Init(&ctx);
    CC_SHA384_Update(&ctx, data, (CC_LONG)datalen);
    CC_SHA384_Final(hash, &ctx);

    return (CC_SHA384_DIGEST_LENGTH);
  }
  else if (!strcmp(algorithm, "sha2-512"))
  {
    CC_SHA512_CTX	ctx;		/* SHA-512 context */

    if (hashsize < CC_SHA512_DIGEST_LENGTH)
      goto too_small;

    CC_SHA512_Init(&ctx);
    CC_SHA512_Update(&ctx, data, (CC_LONG)datalen);
    CC_SHA512_Final(hash, &ctx);

    return (CC_SHA512_DIGEST_LENGTH);
  }
  else if (!strcmp(algorithm, "sha2-512_224"))
  {
    CC_SHA512_CTX	ctx;		/* SHA-512 context */
    unsigned char	temp[CC_SHA512_DIGEST_LENGTH];
                                        /* SHA-512 hash */

   /*
    * SHA2-512 truncated to 224 bits (28 bytes)...
    */

    if (hashsize < CC_SHA224_DIGEST_LENGTH)
      goto too_small;

    CC_SHA512_Init(&ctx);
    CC_SHA512_Update(&ctx, data, (CC_LONG)datalen);
    CC_SHA512_Final(temp, &ctx);

    memcpy(hash, temp, CC_SHA224_DIGEST_LENGTH);

    return (CC_SHA224_DIGEST_LENGTH);
  }
  else if (!strcmp(algorithm, "sha2-512_256"))
  {
    CC_SHA512_CTX	ctx;		/* SHA-512 context */
    unsigned char	temp[CC_SHA512_DIGEST_LENGTH];
                                        /* SHA-512 hash */

   /*
    * SHA2-512 truncated to 256 bits (32 bytes)...
    */

    if (hashsize < CC_SHA256_DIGEST_LENGTH)
      goto too_small;

    CC_SHA512_Init(&ctx);
    CC_SHA512_Update(&ctx, data, (CC_LONG)datalen);
    CC_SHA512_Final(temp, &ctx);

    memcpy(hash, temp, CC_SHA256_DIGEST_LENGTH);

    return (CC_SHA256_DIGEST_LENGTH);
  }

#elif defined(HAVE_GNUTLS)
  gnutls_digest_algorithm_t alg = GNUTLS_DIG_UNKNOWN;
					/* Algorithm */
  unsigned char	temp[64];		/* Temporary hash buffer */
  size_t	tempsize = 0;		/* Truncate to this size? */

  if (!strcmp(algorithm, "sha"))
    alg = GNUTLS_DIG_SHA1;
  else if (!strcmp(algorithm, "sha2-224"))
    alg = GNUTLS_DIG_SHA224;
  else if (!strcmp(algorithm, "sha2-256"))
    alg = GNUTLS_DIG_SHA256;
  else if (!strcmp(algorithm, "sha2-384"))
    alg = GNUTLS_DIG_SHA384;
  else if (!strcmp(algorithm, "sha2-512"))
    alg = GNUTLS_DIG_SHA512;
  else if (!strcmp(algorithm, "sha2-512_224"))
  {
    alg      = GNUTLS_DIG_SHA512;
    tempsize = 28;
  }
  else if (!strcmp(algorithm, "sha2-512_256"))
  {
    alg      = GNUTLS_DIG_SHA512;
    tempsize = 32;
  }

  if (alg != GNUTLS_DIG_UNKNOWN)
  {
    if (tempsize > 0)
    {
     /*
      * Truncate result to tempsize bytes...
      */

      if (hashsize < tempsize)
        goto too_small;

      gnutls_hash_fast(alg, data, datalen, temp);
      memcpy(hash, temp, tempsize);

      return ((ssize_t)tempsize);
    }

    if (hashsize < gnutls_hash_get_len(alg))
      goto too_small;

    gnutls_hash_fast(alg, data, datalen, hash);

    return (gnutls_hash_get_len(alg));
  }

#else
 /*
  * No hash support without CommonCrypto or GNU TLS...
  */

  if (hashsize < 64)
    goto too_small;
#endif /* __APPLE__ */

 /*
  * Unknown hash algorithm...
  */

  _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unknown hash algorithm."), 1);

  return (-1);

 /*
  * We get here if the buffer is too small.
  */

  too_small:

  _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Hash buffer too small."), 1);
  return (-1);
}
Пример #20
0
void SHA256Object::digestUpdate(
	const void 	*data, 
	size_t 		len)
{
	CC_SHA256_Update(&mCtx, (const unsigned char *)data, (CC_LONG)len);
}
Пример #21
0
void SHA256Digest::Update(const void* buffer, size_t length)
{
	CC_SHA256_Update(&mContext, buffer, (CC_LONG)length);
}