Coverage Report

Created: 2024-11-22 20:18

/root/doris/be/src/util/encryption_util.h
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#pragma once
19
20
#include <stdint.h>
21
22
namespace doris {
23
24
enum class EncryptionMode {
25
    AES_128_ECB,
26
    AES_192_ECB,
27
    AES_256_ECB,
28
    AES_128_CBC,
29
    AES_192_CBC,
30
    AES_256_CBC,
31
    AES_128_CFB,
32
    AES_192_CFB,
33
    AES_256_CFB,
34
    AES_128_CFB1,
35
    AES_192_CFB1,
36
    AES_256_CFB1,
37
    AES_128_CFB8,
38
    AES_192_CFB8,
39
    AES_256_CFB8,
40
    AES_128_CFB128,
41
    AES_192_CFB128,
42
    AES_256_CFB128,
43
    AES_128_CTR,
44
    AES_192_CTR,
45
    AES_256_CTR,
46
    AES_128_OFB,
47
    AES_192_OFB,
48
    AES_256_OFB,
49
    AES_128_GCM,
50
    AES_192_GCM,
51
    AES_256_GCM,
52
    SM4_128_ECB,
53
    SM4_128_CBC,
54
    SM4_128_CFB128,
55
    SM4_128_OFB,
56
    SM4_128_CTR
57
};
58
59
enum EncryptionState { AES_SUCCESS = 0, AES_BAD_DATA = -1 };
60
61
class EncryptionUtil {
62
public:
63
900
    static bool is_gcm_mode(EncryptionMode mode) {
64
900
        return mode == EncryptionMode::AES_128_GCM || mode == EncryptionMode::AES_192_GCM ||
65
900
               mode == EncryptionMode::AES_256_GCM;
66
900
    }
67
68
    // https://tools.ietf.org/html/rfc5116#section-5.1
69
    static const int GCM_TAG_SIZE = 16;
70
71
    static int encrypt(EncryptionMode mode, const unsigned char* source, uint32_t source_length,
72
                       const unsigned char* key, uint32_t key_length, const char* iv_str,
73
                       int iv_input_length, bool padding, unsigned char* encrypt,
74
                       const unsigned char* aad = nullptr, uint32_t aad_length = 0);
75
76
    static int decrypt(EncryptionMode mode, const unsigned char* encrypt, uint32_t encrypt_length,
77
                       const unsigned char* key, uint32_t key_length, const char* iv_str,
78
                       int iv_input_length, bool padding, unsigned char* decrypt_content,
79
                       const unsigned char* aad = nullptr, uint32_t aad_length = 0);
80
};
81
82
} // namespace doris