1/*
2 * Copyright (C) 2017 Apple Inc. All rights reserved.
3 * Copyright (C) 2017 Metrological Group B.V.
4 * Copyright (C) 2017 Igalia S.L.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
19 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
25 * THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "config.h"
29#include "CryptoAlgorithmAES_GCM.h"
30
31#if ENABLE(WEB_CRYPTO)
32
33#include "CryptoAlgorithmAesGcmParams.h"
34#include "CryptoKeyAES.h"
35#include "NotImplemented.h"
36#include <pal/crypto/gcrypt/Handle.h>
37#include <pal/crypto/gcrypt/Utilities.h>
38#include <wtf/CryptographicUtilities.h>
39
40namespace WebCore {
41
42static Optional<Vector<uint8_t>> gcryptEncrypt(const Vector<uint8_t>& key, const Vector<uint8_t>& iv, const Vector<uint8_t>& plainText, const Vector<uint8_t>& additionalData, uint8_t tagLength)
43{
44 // Determine the AES algorithm for the given key size.
45 auto algorithm = PAL::GCrypt::aesAlgorithmForKeySize(key.size() * 8);
46 if (!algorithm)
47 return WTF::nullopt;
48
49 // Create a new GCrypt cipher object for the AES algorithm and the GCM cipher mode.
50 PAL::GCrypt::Handle<gcry_cipher_hd_t> handle;
51 gcry_error_t error = gcry_cipher_open(&handle, *algorithm, GCRY_CIPHER_MODE_GCM, GCRY_CIPHER_SECURE);
52 if (error != GPG_ERR_NO_ERROR) {
53 PAL::GCrypt::logError(error);
54 return WTF::nullopt;
55 }
56
57 // Use the given key for this cipher object.
58 error = gcry_cipher_setkey(handle, key.data(), key.size());
59 if (error != GPG_ERR_NO_ERROR) {
60 PAL::GCrypt::logError(error);
61 return WTF::nullopt;
62 }
63
64 // Use the given IV for this cipher object.
65 error = gcry_cipher_setiv(handle, iv.data(), iv.size());
66 if (error != GPG_ERR_NO_ERROR) {
67 PAL::GCrypt::logError(error);
68 return WTF::nullopt;
69 }
70
71 // Use the given additonal data, if any, as the authentication data for this cipher object.
72 if (!additionalData.isEmpty()) {
73 error = gcry_cipher_authenticate(handle, additionalData.data(), additionalData.size());
74 if (error != GPG_ERR_NO_ERROR) {
75 PAL::GCrypt::logError(error);
76 return WTF::nullopt;
77 }
78 }
79
80 // Finalize the cipher object before performing the encryption.
81 error = gcry_cipher_final(handle);
82 if (error != GPG_ERR_NO_ERROR) {
83 PAL::GCrypt::logError(error);
84 return WTF::nullopt;
85 }
86
87 // Perform the encryption and retrieve the encrypted output.
88 Vector<uint8_t> output(plainText.size());
89 error = gcry_cipher_encrypt(handle, output.data(), output.size(), plainText.data(), plainText.size());
90 if (error != GPG_ERR_NO_ERROR) {
91 PAL::GCrypt::logError(error);
92 return WTF::nullopt;
93 }
94
95 // If tag length was specified, retrieve the tag data and append it to the output vector.
96 if (tagLength) {
97 Vector<uint8_t> tag(tagLength);
98 error = gcry_cipher_gettag(handle, tag.data(), tag.size());
99 if (error != GPG_ERR_NO_ERROR) {
100 PAL::GCrypt::logError(error);
101 return WTF::nullopt;
102 }
103
104 output.appendVector(tag);
105 }
106
107 return output;
108}
109
110static Optional<Vector<uint8_t>> gcryptDecrypt(const Vector<uint8_t>& key, const Vector<uint8_t>& iv, const Vector<uint8_t>& cipherText, const Vector<uint8_t>& additionalData, uint8_t tagLength)
111{
112 // Determine the AES algorithm for the given key size.
113 auto algorithm = PAL::GCrypt::aesAlgorithmForKeySize(key.size() * 8);
114 if (!algorithm)
115 return WTF::nullopt;
116
117 // Create a new GCrypt cipher object for the AES algorithm and the GCM cipher mode.
118 PAL::GCrypt::Handle<gcry_cipher_hd_t> handle;
119 gcry_error_t error = gcry_cipher_open(&handle, *algorithm, GCRY_CIPHER_MODE_GCM, 0);
120 if (error != GPG_ERR_NO_ERROR) {
121 PAL::GCrypt::logError(error);
122 return WTF::nullopt;
123 }
124
125 // Use the given key for this cipher object.
126 error = gcry_cipher_setkey(handle, key.data(), key.size());
127 if (error != GPG_ERR_NO_ERROR) {
128 PAL::GCrypt::logError(error);
129 return WTF::nullopt;
130 }
131
132 // Use the given IV for this cipher object.
133 error = gcry_cipher_setiv(handle, iv.data(), iv.size());
134 if (error != GPG_ERR_NO_ERROR) {
135 PAL::GCrypt::logError(error);
136 return WTF::nullopt;
137 }
138
139 // Use the given additonal data, if any, as the authentication data for this cipher object.
140 if (!additionalData.isEmpty()) {
141 error = gcry_cipher_authenticate(handle, additionalData.data(), additionalData.size());
142 if (error != GPG_ERR_NO_ERROR) {
143 PAL::GCrypt::logError(error);
144 return WTF::nullopt;
145 }
146 }
147
148 // Finalize the cipher object before performing the encryption.
149 error = gcry_cipher_final(handle);
150 if (error != GPG_ERR_NO_ERROR) {
151 PAL::GCrypt::logError(error);
152 return WTF::nullopt;
153 }
154
155 // Account for the specified tag length when performing the decryption and retrieving the decrypted output.
156 size_t cipherLength = cipherText.size() - tagLength;
157 Vector<uint8_t> output(cipherLength);
158 error = gcry_cipher_decrypt(handle, output.data(), output.size(), cipherText.data(), cipherLength);
159 if (error != GPG_ERR_NO_ERROR) {
160 PAL::GCrypt::logError(error);
161 return WTF::nullopt;
162 }
163
164 // If tag length was indeed specified, retrieve the tag data and compare it securely to the tag data that
165 // is in the passed-in cipher text Vector, bailing if there is a mismatch and returning the decrypted
166 // plaintext otherwise.
167 if (tagLength) {
168 Vector<uint8_t> tag(tagLength);
169 error = gcry_cipher_gettag(handle, tag.data(), tagLength);
170 if (error != GPG_ERR_NO_ERROR) {
171 PAL::GCrypt::logError(error);
172 return WTF::nullopt;
173 }
174
175 if (constantTimeMemcmp(tag.data(), cipherText.data() + cipherLength, tagLength))
176 return WTF::nullopt;
177 }
178
179 return output;
180}
181
182ExceptionOr<Vector<uint8_t>> CryptoAlgorithmAES_GCM::platformEncrypt(const CryptoAlgorithmAesGcmParams& parameters, const CryptoKeyAES& key, const Vector<uint8_t>& plainText)
183{
184 auto output = gcryptEncrypt(key.key(), parameters.ivVector(), plainText, parameters.additionalDataVector(), parameters.tagLength.valueOr(0) / 8);
185 if (!output)
186 return Exception { OperationError };
187 return WTFMove(*output);
188}
189
190ExceptionOr<Vector<uint8_t>> CryptoAlgorithmAES_GCM::platformDecrypt(const CryptoAlgorithmAesGcmParams& parameters, const CryptoKeyAES& key, const Vector<uint8_t>& cipherText)
191{
192 auto output = gcryptDecrypt(key.key(), parameters.ivVector(), cipherText, parameters.additionalDataVector(), parameters.tagLength.valueOr(0) / 8);
193 if (!output)
194 return Exception { OperationError };
195 return WTFMove(*output);
196}
197
198} // namespace WebCore
199
200#endif // ENABLE(WEB_CRYPTO)
201