libmongocrypt
mc-fle2-insert-update-payload-private-v2.h
1 /*
2  * Copyright 2022-present MongoDB, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef MC_FLE2_INSERT_UPDATE_PAYLOAD_PRIVATE_V2_H
18 #define MC_FLE2_INSERT_UPDATE_PAYLOAD_PRIVATE_V2_H
19 
20 #include <bson/bson.h>
21 
22 #include "mc-array-private.h"
23 #include "mc-optional-private.h"
24 #include "mongocrypt-buffer-private.h"
25 #include "mongocrypt-private.h"
26 #include "mongocrypt.h"
27 
28 #define DEF_TEXT_SEARCH_TOKEN_SET(Type) \
29  typedef struct { \
30  _mongocrypt_buffer_t edcDerivedToken; \
31  _mongocrypt_buffer_t escDerivedToken; \
32  _mongocrypt_buffer_t serverDerivedFromDataToken; \
33  _mongocrypt_buffer_t encryptedTokens; \
34  } mc_Text##Type##TokenSet_t; \
35  void mc_Text##Type##TokenSet_init(mc_Text##Type##TokenSet_t *); \
36  void mc_Text##Type##TokenSet_cleanup(mc_Text##Type##TokenSet_t *); \
37  void mc_Text##Type##TokenSet_shallow_copy(const mc_Text##Type##TokenSet_t *src, mc_Text##Type##TokenSet_t *dest)
38 
39 DEF_TEXT_SEARCH_TOKEN_SET(Exact);
40 DEF_TEXT_SEARCH_TOKEN_SET(Substring);
41 DEF_TEXT_SEARCH_TOKEN_SET(Suffix);
42 DEF_TEXT_SEARCH_TOKEN_SET(Prefix);
43 
44 typedef struct {
45  mc_TextExactTokenSet_t exact; // e
46  mc_array_t substringArray; // s
47  mc_array_t suffixArray; // u
48  mc_array_t prefixArray; // p
50 
51 void mc_TextSearchTokenSets_init(mc_TextSearchTokenSets_t *);
52 void mc_TextSearchTokenSets_cleanup(mc_TextSearchTokenSets_t *);
53 
96 typedef struct {
97  _mongocrypt_buffer_t edcDerivedToken; // d
98  _mongocrypt_buffer_t escDerivedToken; // s
99  _mongocrypt_buffer_t encryptedTokens; // p
100  _mongocrypt_buffer_t indexKeyId; // u
101  bson_type_t valueType; // t
102  _mongocrypt_buffer_t value; // v
103  _mongocrypt_buffer_t serverEncryptionToken; // e
104  _mongocrypt_buffer_t serverDerivedFromDataToken; // l
105  int64_t contentionFactor; // k
106  mc_array_t edgeTokenSetArray; // g
107  mc_optional_int64_t sparsity; // sp
108  mc_optional_int32_t precision; // pn
109  mc_optional_int32_t trimFactor; // tf
110  bson_value_t indexMin; // mn
111  bson_value_t indexMax; // mx
112 
113  struct {
115  bool set;
116  } textSearchTokenSets; // b
117 
118  _mongocrypt_buffer_t plaintext;
119  _mongocrypt_buffer_t userKeyId;
121 
122 // `mc_FLE2InsertUpdatePayloadV2_t` inherits extended alignment from libbson. To dynamically allocate, use
123 // aligned allocation (e.g. BSON_ALIGNED_ALLOC)
124 BSON_STATIC_ASSERT2(alignof_mc_FLE2InsertUpdatePayloadV2_t,
125  BSON_ALIGNOF(mc_FLE2InsertUpdatePayloadV2_t) >= BSON_ALIGNOF(bson_value_t));
126 
138 typedef struct {
139  _mongocrypt_buffer_t edcDerivedToken; // d
140  _mongocrypt_buffer_t escDerivedToken; // s
141  _mongocrypt_buffer_t serverDerivedFromDataToken; // l
142  _mongocrypt_buffer_t encryptedTokens; // p
144 
145 void mc_FLE2InsertUpdatePayloadV2_init(mc_FLE2InsertUpdatePayloadV2_t *payload);
146 
147 bool mc_FLE2InsertUpdatePayloadV2_parse(mc_FLE2InsertUpdatePayloadV2_t *out,
148  const _mongocrypt_buffer_t *in,
149  mongocrypt_status_t *status);
150 
151 /* mc_FLE2InsertUpdatePayloadV2_decrypt decrypts ciphertext.
152  * Returns NULL and sets @status on error. It is an error to call before
153  * mc_FLE2InsertUpdatePayloadV2_parse. */
154 const _mongocrypt_buffer_t *mc_FLE2InsertUpdatePayloadV2_decrypt(_mongocrypt_crypto_t *crypto,
156  const _mongocrypt_buffer_t *user_key,
157  mongocrypt_status_t *status);
158 
159 bool mc_FLE2InsertUpdatePayloadV2_serialize(const mc_FLE2InsertUpdatePayloadV2_t *payload, bson_t *out);
160 
161 bool mc_FLE2InsertUpdatePayloadV2_serializeForRange(const mc_FLE2InsertUpdatePayloadV2_t *payload,
162  bson_t *out,
163  bool use_range_v2);
164 
165 bool mc_FLE2InsertUpdatePayloadV2_serializeForTextSearch(const mc_FLE2InsertUpdatePayloadV2_t *payload, bson_t *out);
166 
167 void mc_FLE2InsertUpdatePayloadV2_cleanup(mc_FLE2InsertUpdatePayloadV2_t *payload);
168 
169 #endif /* MC_FLE2_INSERT_UPDATE_PAYLOAD_PRIVATE_V2_H */
struct _mongocrypt_status_t mongocrypt_status_t
Definition: mongocrypt.h:152
Definition: mc-fle2-insert-update-payload-private-v2.h:138
Definition: mc-fle2-insert-update-payload-private-v2.h:96
Definition: mc-fle2-insert-update-payload-private-v2.h:44