Move CryptoDigest to WebCore/platform
[WebKit-https.git] / Source / WebCore / platform / crypto / mac / CryptoDigestMac.cpp
1 /*
2  * Copyright (C) 2013 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "CryptoDigest.h"
28
29 #include <CommonCrypto/CommonCrypto.h>
30
31 namespace WebCore {
32
33 struct CryptoDigestContext {
34     CryptoDigest::Algorithm algorithm;
35     void* ccContext;
36 };
37
38 inline CC_SHA1_CTX* toSHA1Context(CryptoDigestContext* context)
39 {
40     ASSERT(context->algorithm == CryptoDigest::Algorithm::SHA_1);
41     return static_cast<CC_SHA1_CTX*>(context->ccContext);
42 }
43 inline CC_SHA256_CTX* toSHA224Context(CryptoDigestContext* context)
44 {
45     ASSERT(context->algorithm == CryptoDigest::Algorithm::SHA_224);
46     return static_cast<CC_SHA256_CTX*>(context->ccContext);
47 }
48 inline CC_SHA256_CTX* toSHA256Context(CryptoDigestContext* context)
49 {
50     ASSERT(context->algorithm == CryptoDigest::Algorithm::SHA_256);
51     return static_cast<CC_SHA256_CTX*>(context->ccContext);
52 }
53 inline CC_SHA512_CTX* toSHA384Context(CryptoDigestContext* context)
54 {
55     ASSERT(context->algorithm == CryptoDigest::Algorithm::SHA_384);
56     return static_cast<CC_SHA512_CTX*>(context->ccContext);
57 }
58 inline CC_SHA512_CTX* toSHA512Context(CryptoDigestContext* context)
59 {
60     ASSERT(context->algorithm == CryptoDigest::Algorithm::SHA_512);
61     return static_cast<CC_SHA512_CTX*>(context->ccContext);
62 }
63
64 CryptoDigest::CryptoDigest()
65     : m_context(new CryptoDigestContext)
66 {
67 }
68
69 CryptoDigest::~CryptoDigest()
70 {
71     switch (m_context->algorithm) {
72     case CryptoDigest::Algorithm::SHA_1:
73         delete toSHA1Context(m_context.get());
74         return;
75     case CryptoDigest::Algorithm::SHA_224:
76         delete toSHA224Context(m_context.get());
77         return;
78     case CryptoDigest::Algorithm::SHA_256:
79         delete toSHA256Context(m_context.get());
80         return;
81     case CryptoDigest::Algorithm::SHA_384:
82         delete toSHA384Context(m_context.get());
83         return;
84     case CryptoDigest::Algorithm::SHA_512:
85         delete toSHA512Context(m_context.get());
86         return;
87     }
88 }
89
90
91 std::unique_ptr<CryptoDigest> CryptoDigest::create(CryptoDigest::Algorithm algorithm)
92 {
93     std::unique_ptr<CryptoDigest> digest(new CryptoDigest);
94     digest->m_context->algorithm = algorithm;
95
96     switch (algorithm) {
97     case CryptoDigest::Algorithm::SHA_1: {
98         CC_SHA1_CTX* context = new CC_SHA1_CTX;
99         digest->m_context->ccContext = context;
100         CC_SHA1_Init(context);
101         return digest;
102     }
103     case CryptoDigest::Algorithm::SHA_224: {
104         CC_SHA256_CTX* context = new CC_SHA256_CTX;
105         digest->m_context->ccContext = context;
106         CC_SHA224_Init(context);
107         return digest;
108     }
109     case CryptoDigest::Algorithm::SHA_256: {
110         CC_SHA256_CTX* context = new CC_SHA256_CTX;
111         digest->m_context->ccContext = context;
112         CC_SHA256_Init(context);
113         return digest;
114     }
115     case CryptoDigest::Algorithm::SHA_384: {
116         CC_SHA512_CTX* context = new CC_SHA512_CTX;
117         digest->m_context->ccContext = context;
118         CC_SHA384_Init(context);
119         return digest;
120     }
121     case CryptoDigest::Algorithm::SHA_512: {
122         CC_SHA512_CTX* context = new CC_SHA512_CTX;
123         digest->m_context->ccContext = context;
124         CC_SHA512_Init(context);
125         return digest;
126     }
127     }
128 }
129
130 void CryptoDigest::addBytes(const void* input, size_t length)
131 {
132     switch (m_context->algorithm) {
133     case CryptoDigest::Algorithm::SHA_1:
134         CC_SHA1_Update(toSHA1Context(m_context.get()), input, length);
135         return;
136     case CryptoDigest::Algorithm::SHA_224:
137         CC_SHA224_Update(toSHA224Context(m_context.get()), input, length);
138         return;
139     case CryptoDigest::Algorithm::SHA_256:
140         CC_SHA256_Update(toSHA256Context(m_context.get()), input, length);
141         return;
142     case CryptoDigest::Algorithm::SHA_384:
143         CC_SHA384_Update(toSHA384Context(m_context.get()), input, length);
144         return;
145     case CryptoDigest::Algorithm::SHA_512:
146         CC_SHA512_Update(toSHA512Context(m_context.get()), input, length);
147         return;
148     }
149 }
150
151 Vector<uint8_t> CryptoDigest::computeHash()
152 {
153     Vector<uint8_t> result;
154     switch (m_context->algorithm) {
155     case CryptoDigest::Algorithm::SHA_1:
156         result.resize(CC_SHA1_DIGEST_LENGTH);
157         CC_SHA1_Final(result.data(), toSHA1Context(m_context.get()));
158         break;
159     case CryptoDigest::Algorithm::SHA_224:
160         result.resize(CC_SHA224_DIGEST_LENGTH);
161         CC_SHA224_Final(result.data(), toSHA224Context(m_context.get()));
162         break;
163     case CryptoDigest::Algorithm::SHA_256:
164         result.resize(CC_SHA256_DIGEST_LENGTH);
165         CC_SHA256_Final(result.data(), toSHA256Context(m_context.get()));
166         break;
167     case CryptoDigest::Algorithm::SHA_384:
168         result.resize(CC_SHA384_DIGEST_LENGTH);
169         CC_SHA384_Final(result.data(), toSHA384Context(m_context.get()));
170         break;
171     case CryptoDigest::Algorithm::SHA_512:
172         result.resize(CC_SHA512_DIGEST_LENGTH);
173         CC_SHA512_Final(result.data(), toSHA512Context(m_context.get()));
174         break;
175     }
176     return result;
177 }
178
179 } // namespace WebCore