| 1 | /* |
| 2 | * Copyright (C) 2014 Igalia S.L. |
| 3 | * Copyright (C) 2016 SoftAtHome |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 15 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 16 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 17 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 18 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 19 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 20 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 21 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 22 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 23 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 24 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | */ |
| 26 | |
| 27 | #include "config.h" |
| 28 | #include "CryptoDigest.h" |
| 29 | |
| 30 | #include <gcrypt.h> |
| 31 | |
| 32 | namespace PAL { |
| 33 | |
| 34 | struct CryptoDigestContext { |
| 35 | int algorithm; |
| 36 | gcry_md_hd_t md; |
| 37 | }; |
| 38 | |
| 39 | CryptoDigest::CryptoDigest() |
| 40 | : m_context(new CryptoDigestContext) |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | CryptoDigest::~CryptoDigest() = default; |
| 45 | |
| 46 | std::unique_ptr<CryptoDigest> CryptoDigest::create(CryptoDigest::Algorithm algorithm) |
| 47 | { |
| 48 | int gcryptAlgorithm; |
| 49 | |
| 50 | switch (algorithm) { |
| 51 | case CryptoDigest::Algorithm::SHA_1: |
| 52 | gcryptAlgorithm = GCRY_MD_SHA1; |
| 53 | break; |
| 54 | case CryptoDigest::Algorithm::SHA_224: |
| 55 | gcryptAlgorithm = GCRY_MD_SHA224; |
| 56 | break; |
| 57 | case CryptoDigest::Algorithm::SHA_256: |
| 58 | gcryptAlgorithm = GCRY_MD_SHA256; |
| 59 | break; |
| 60 | case CryptoDigest::Algorithm::SHA_384: |
| 61 | gcryptAlgorithm = GCRY_MD_SHA384; |
| 62 | break; |
| 63 | case CryptoDigest::Algorithm::SHA_512: |
| 64 | gcryptAlgorithm = GCRY_MD_SHA512; |
| 65 | break; |
| 66 | } |
| 67 | |
| 68 | std::unique_ptr<CryptoDigest> digest(new CryptoDigest); |
| 69 | digest->m_context->algorithm = gcryptAlgorithm; |
| 70 | |
| 71 | gcry_md_open(&digest->m_context->md, gcryptAlgorithm, 0); |
| 72 | if (!digest->m_context->md) |
| 73 | return nullptr; |
| 74 | |
| 75 | return digest; |
| 76 | } |
| 77 | |
| 78 | void CryptoDigest::addBytes(const void* input, size_t length) |
| 79 | { |
| 80 | gcry_md_write(m_context->md, input, length); |
| 81 | } |
| 82 | |
| 83 | Vector<uint8_t> CryptoDigest::computeHash() |
| 84 | { |
| 85 | int digestLen = gcry_md_get_algo_dlen(m_context->algorithm); |
| 86 | Vector<uint8_t> result(digestLen); |
| 87 | |
| 88 | gcry_md_final(m_context->md); |
| 89 | memcpy(result.data(), gcry_md_read(m_context->md, 0), digestLen); |
| 90 | gcry_md_close(m_context->md); |
| 91 | |
| 92 | return result; |
| 93 | } |
| 94 | |
| 95 | } // namespace PAL |
| 96 | |