Vector<uint8_t> CryptoDigest::computeHash()
{
    Vector<uint8_t> result;
    switch (m_context->algorithm) {
    case CryptoAlgorithmIdentifier::SHA_1:
        result.resize(CC_SHA1_DIGEST_LENGTH);
        CC_SHA1_Final(result.data(), toSHA1Context(m_context.get()));
        break;
    case CryptoAlgorithmIdentifier::SHA_224:
        result.resize(CC_SHA224_DIGEST_LENGTH);
        CC_SHA224_Final(result.data(), toSHA224Context(m_context.get()));
        break;
    case CryptoAlgorithmIdentifier::SHA_256:
        result.resize(CC_SHA256_DIGEST_LENGTH);
        CC_SHA256_Final(result.data(), toSHA256Context(m_context.get()));
        break;
    case CryptoAlgorithmIdentifier::SHA_384:
        result.resize(CC_SHA384_DIGEST_LENGTH);
        CC_SHA384_Final(result.data(), toSHA384Context(m_context.get()));
        break;
    case CryptoAlgorithmIdentifier::SHA_512:
        result.resize(CC_SHA512_DIGEST_LENGTH);
        CC_SHA512_Final(result.data(), toSHA512Context(m_context.get()));
        break;
    default:
        ASSERT_NOT_REACHED();
    }
    return result;
}
示例#2
0
static NTSTATUS hash_finish( struct hash *hash, UCHAR *output, ULONG size )
{
    switch (hash->alg_id)
    {
    case ALG_ID_SHA1:
        CC_SHA1_Final( output, &hash->u.sha1_ctx );
        break;

    case ALG_ID_SHA256:
        CC_SHA256_Final( output, &hash->u.sha256_ctx );
        break;

    case ALG_ID_SHA384:
        CC_SHA384_Final( output, &hash->u.sha512_ctx );
        break;

    case ALG_ID_SHA512:
        CC_SHA512_Final( output, &hash->u.sha512_ctx );
        break;

    default:
        ERR( "unhandled id %u\n", hash->alg_id );
        break;
    }
    return STATUS_SUCCESS;
}
示例#3
0
文件: c4hash.c 项目: rhardman/C4
int sCCHashFinalSHA384(void *ctx, unsigned char *out)
{
    CC_SHA384_Final(out, ctx);
    
#ifdef LTC_CLEAN_STACK
    zeromem(ctx, sizeof(CC_SHA1_CTX));
#endif
    
    return CRYPT_OK;
}
示例#4
0
static OSStatus HashSHA384Final(SSLBuffer *digestCtx, SSLBuffer *digest)
{
	assert(digestCtx->length >= sizeof(CC_SHA512_CTX));
	CC_SHA512_CTX *ctx = (CC_SHA512_CTX *)digestCtx->data;
	dgprintf(("###HashSHA384Final  ctx %p\n", ctx));
	assert(digest->length >= CC_SHA384_DIGEST_LENGTH);
	//if (digest->length < CC_SHA384_DIGEST_LENGTH)
	//	return errSSLCrypto;
	CC_SHA384_Final(digest->data, ctx);
	digest->length = CC_SHA384_DIGEST_LENGTH;
    return noErr;
}
示例#5
0
static void
md_final(int type, unsigned char *str, void *ctx)
{
  switch (type) {
  case MD_TYPE_MD5:	CC_MD5_Final(str, ctx);		break;
  case MD_TYPE_SHA1:	CC_SHA1_Final(str, ctx);	break;
  case MD_TYPE_SHA256:	CC_SHA256_Final(str, ctx);	break;
  case MD_TYPE_SHA384:	CC_SHA384_Final(str, ctx);	break;
  case MD_TYPE_SHA512:	CC_SHA512_Final(str, ctx);	break;
  default:						break;
  }
}
示例#6
0
extern "C" int32_t AppleCryptoNative_DigestFinal(DigestCtx* ctx, uint8_t* pOutput, int32_t cbOutput)
{
    if (ctx == nullptr || pOutput == nullptr || cbOutput < ctx->cbDigest)
        return -1;

    int32_t ret = 0;

    switch (ctx->algorithm)
    {
        case PAL_MD5:
            ret = CC_MD5_Final(pOutput, &ctx->d.md5);
            break;
        case PAL_SHA1:
            ret = CC_SHA1_Final(pOutput, &ctx->d.sha1);
            break;
        case PAL_SHA256:
            ret = CC_SHA256_Final(pOutput, &ctx->d.sha256);
            break;
        case PAL_SHA384:
            ret = CC_SHA384_Final(pOutput, &ctx->d.sha384);
            break;
        case PAL_SHA512:
            ret = CC_SHA512_Final(pOutput, &ctx->d.sha512);
            break;
        default:
            ret = -1;
            break;
    }

    if (ret != 1)
    {
        return ret;
    }

    switch (ctx->algorithm)
    {
        case PAL_MD5:
            return CC_MD5_Init(&ctx->d.md5);
        case PAL_SHA1:
            return CC_SHA1_Init(&ctx->d.sha1);
        case PAL_SHA256:
            return CC_SHA256_Init(&ctx->d.sha256);
        case PAL_SHA384:
            return CC_SHA384_Init(&ctx->d.sha384);
        case PAL_SHA512:
            return CC_SHA512_Init(&ctx->d.sha512);
        default:
            assert(false);
            return -2;
    }
}
string Crypto::DigestFinish(Digest x) {
  auto d = FromVoid<CCDigest*>(x);
  string ret;
  switch(d->algo) {
    case CCDigestAlgo::MD5:    ret.resize(CC_MD5_DIGEST_LENGTH);    CC_MD5_Final   (MakeUnsigned(&ret[0]), static_cast<CC_MD5_CTX*>   (d->v)); free(d->v); d->v=0; break;
    case CCDigestAlgo::SHA1:   ret.resize(CC_SHA1_DIGEST_LENGTH);   CC_SHA1_Final  (MakeUnsigned(&ret[0]), static_cast<CC_SHA1_CTX*>  (d->v)); free(d->v); d->v=0; break;
    case CCDigestAlgo::SHA256: ret.resize(CC_SHA256_DIGEST_LENGTH); CC_SHA256_Final(MakeUnsigned(&ret[0]), static_cast<CC_SHA256_CTX*>(d->v)); free(d->v); d->v=0; break;
    case CCDigestAlgo::SHA384: ret.resize(CC_SHA384_DIGEST_LENGTH); CC_SHA384_Final(MakeUnsigned(&ret[0]), static_cast<CC_SHA512_CTX*>(d->v)); free(d->v); d->v=0; break;
    case CCDigestAlgo::SHA512: ret.resize(CC_SHA512_DIGEST_LENGTH); CC_SHA512_Final(MakeUnsigned(&ret[0]), static_cast<CC_SHA512_CTX*>(d->v)); free(d->v); d->v=0; break;
    default: break;
  }
  delete d;
  return ret;
}
示例#8
0
static void _wi_sha2_ctx_final(unsigned char *buffer, _wi_sha2_ctx_t *ctx) {
#ifdef WI_SHA2_OPENSSL
    switch(ctx->bits) {
    case WI_SHA2_224:
        SHA224_Final(buffer, &ctx->openssl_256_ctx);
        break;

    case WI_SHA2_256:
        SHA256_Final(buffer, &ctx->openssl_256_ctx);
        break;

    case WI_SHA2_384:
        SHA384_Final(buffer, &ctx->openssl_512_ctx);
        break;

    case WI_SHA2_512:
        SHA512_Final(buffer, &ctx->openssl_512_ctx);
        break;
    }
#endif

#ifdef WI_SHA2_COMMONCRYPTO
    switch(ctx->bits) {
    case WI_SHA2_224:
        CC_SHA224_Final(buffer, &ctx->commondigest_256_ctx);
        break;

    case WI_SHA2_256:
        CC_SHA256_Final(buffer, &ctx->commondigest_256_ctx);
        break;

    case WI_SHA2_384:
        CC_SHA384_Final(buffer, &ctx->commondigest_512_ctx);
        break;

    case WI_SHA2_512:
        CC_SHA512_Final(buffer, &ctx->commondigest_512_ctx);
        break;
    }
#endif
}
示例#9
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);
}
示例#10
0
static int
__archive_libsystem_sha384final(archive_sha384_ctx *ctx, void *md)
{
  CC_SHA384_Final(md, ctx);
  return (ARCHIVE_OK);
}
示例#11
0
void SHA384Object::digestFinal(
	void 		*digest)
{
	CC_SHA384_Final((unsigned char *)digest, &mCtx);
	mIsDone = true;
}
const void* SHA384Digest::Finalize()
{
	CC_SHA384_Final(mDigestBuffer, &mContext);
	return mDigestBuffer;
}