Imported Upstream version 2.6.1
[platform/upstream/cryptsetup.git] / tests / fuzz / proto_to_luks2_converter.h
1 /*
2  * cryptsetup LUKS2 custom mutator fuzz target
3  *
4  * Copyright (C) 2022-2023 Daniel Zatovic <daniel.zatovic@gmail.com>
5  * Copyright (C) 2022-2023 Red Hat, Inc. All rights reserved.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #ifndef LUKS2_PROTO_CONVERTER_H_
23 #define LUKS2_PROTO_CONVERTER_H_
24
25 #include <sstream>
26 #include <string>
27 #include <json-c/json.h>
28
29 #include "LUKS2.pb.h"
30 extern "C" {
31 #include "crypto_backend/crypto_backend.h"
32 }
33
34 namespace LUKS2_proto {
35
36 class LUKS2ProtoConverter {
37   public:
38     ~LUKS2ProtoConverter();
39     std::string string_uint64_to_string(const string_uint64 &str_u64);
40     std::string hash_algorithm_to_string(const hash_algorithm type);
41     std::string object_id_to_string(const object_id &oid);
42
43     std::string keyslot_area_type_to_string(const keyslot_area_type type);
44     std::string keyslot_kdf_type_to_string(const keyslot_kdf_type type);
45     std::string reencrypt_keyslot_mode_to_string(const reencrypt_keyslot_mode mode);
46     std::string keyslot_type_to_string(const keyslot_type type);
47     std::string reencrypt_keyslot_direction_to_string(const reencrypt_keyslot_direction direction);
48     std::string keyslot_af_type_to_string(const keyslot_af_type type);
49
50     std::string config_flag_to_string(config_flag flag);
51     std::string config_requirement_to_string(config_requirement requirements);
52
53     std::string segment_type_to_string(segment_type type);
54     std::string segment_flag_to_string(segment_flag flag);
55
56     void generate_keyslot(struct json_object *jobj_keyslots, const keyslot_description &keyslot_desc);
57     void generate_keyslot_area(struct json_object *jobj_area, const keyslot_area_description &keyslot_area_desc);
58     void generate_keyslot_kdf(struct json_object *jobj_kdf, const keyslot_kdf_description &keyslot_kdf_desc);
59     void generate_keyslot_af(struct json_object *jobj_af, const keyslot_af_description &keyslot_af_desc);
60
61     void generate_token(struct json_object *jobj_tokens, const token_description &token_desc);
62
63     void generate_digest(struct json_object *jobj_digests, const digest_description &digest_desc);
64
65     void generate_segment_integrity(struct json_object *jobj_integrity, const segment_integrity_description &segment_integrity_desc);
66     void generate_segment(struct json_object *jobj_segments, const segment_description &segment_desc);
67
68     void generate_config(const config_description &config_desc, uint64_t json_size, uint64_t keyslots_size);
69
70     void create_jobj(const LUKS2_both_headers &headers, uint64_t hdr_size);
71     void emit_luks2_binary_header(uint64_t offset, uint64_t seqid, bool is_primary, uint64_t hdr_size);
72     void convert(const LUKS2_both_headers &headers, int fd);
73     void create_jobj(const LUKS2_both_headers &headers);
74     void emit_luks2_binary_header(const LUKS2_header &header_proto, int fd, uint64_t offset, uint64_t seqid);
75
76     void set_write_headers_only(bool headers_only);
77
78     const uint8_t *get_out_buffer();
79     size_t get_out_size();
80
81     static const uint64_t KEYSLOTS_SIZE = 3 * 1024 * 1024;
82     static const uint64_t DATA_SIZE = 16 * 1024 * 1024;
83   private:
84     bool write_headers_only = false;
85     struct crypt_hash *hd = NULL;
86     struct ::json_object *jobj = NULL;
87 };
88
89 }  // namespace LUKS2_proto
90
91 #endif  // LUKS2_PROTO_CONVERTER_H_