1 | /* |
2 | * Copyright (C) 2017 Metrological Group B.V. |
3 | * Copyright (C) 2017 Igalia S.L. |
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 | #pragma once |
28 | |
29 | #include <gcrypt.h> |
30 | #include <wtf/Assertions.h> |
31 | #include <wtf/Optional.h> |
32 | |
33 | namespace PAL { |
34 | namespace GCrypt { |
35 | |
36 | // Copied from gcrypt.h. This macro is new in version 1.7.0, which we do not |
37 | // want to require yet. Remove this in summer 2019. |
38 | #ifndef gcry_cipher_final |
39 | #define gcry_cipher_final(a) \ |
40 | gcry_cipher_ctl ((a), GCRYCTL_FINALIZE, NULL, 0) |
41 | #endif |
42 | |
43 | using CipherOperation = gcry_error_t(gcry_cipher_hd_t, void*, size_t, const void*, size_t); |
44 | |
45 | static inline void logError(gcry_error_t error) |
46 | { |
47 | // FIXME: Use a WebCrypto WTF log channel here once those are moved down to PAL. |
48 | #if !LOG_DISABLED |
49 | WTFLogAlways("libgcrypt error: source '%s', description '%s'" , |
50 | gcry_strsource(error), gcry_strerror(error)); |
51 | #else |
52 | UNUSED_PARAM(error); |
53 | #endif |
54 | } |
55 | |
56 | static inline Optional<int> aesAlgorithmForKeySize(size_t keySize) |
57 | { |
58 | switch (keySize) { |
59 | case 128: |
60 | return GCRY_CIPHER_AES128; |
61 | case 192: |
62 | return GCRY_CIPHER_AES192; |
63 | case 256: |
64 | return GCRY_CIPHER_AES256; |
65 | default: |
66 | return WTF::nullopt; |
67 | } |
68 | } |
69 | |
70 | } // namespace GCrypt |
71 | } // namespace PAL |
72 | |