File size: 9,692 Bytes
efadae0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#include "unified_crypto_interface.h"
#include "algorithm_registry.h"
#include "openssl_adapter.h"
#include "classic_crypto_adapter.h"
#include "pqc_adapter.h"
#include "hybrid_crypto.h"
#include <string.h>
#include <stdlib.h>

int uci_init(void) {
    int ret;
    
    ret = registry_init();
    if (ret != UCI_SUCCESS) {
        return ret;
    }
    
    ret = openssl_adapter_init();
    if (ret != UCI_SUCCESS) {
        registry_cleanup();
        return ret;
    }
    
    ret = classic_adapter_init();
    if (ret != UCI_SUCCESS) {
        openssl_adapter_cleanup();
        registry_cleanup();
        return ret;
    }
    
    ret = pqc_adapter_init();
    if (ret != UCI_SUCCESS) {
        classic_adapter_cleanup();
        openssl_adapter_cleanup();
        registry_cleanup();
        return ret;
    }
    
    ret = hybrid_adapter_init();
    if (ret != UCI_SUCCESS) {
        pqc_adapter_cleanup();
        classic_adapter_cleanup();
        openssl_adapter_cleanup();
        registry_cleanup();
        return ret;
    }
    
    return UCI_SUCCESS;
}

int uci_cleanup(void) {
    hybrid_adapter_cleanup();
    pqc_adapter_cleanup();
    classic_adapter_cleanup();
    openssl_adapter_cleanup();
    registry_cleanup();
    return UCI_SUCCESS;
}

int uci_get_algorithm_info(uci_algorithm_id_t algorithm, uci_algorithm_info_t *info) {
    if (!info) {
        return UCI_ERROR_INVALID_PARAM;
    }
    
    const uci_algorithm_impl_t *impl = registry_get_algorithm(algorithm);
    if (!impl) {
        return UCI_ERROR_ALGORITHM_NOT_FOUND;
    }
    
    memcpy(info, &impl->info, sizeof(uci_algorithm_info_t));
    return UCI_SUCCESS;
}

int uci_list_algorithms(uci_algorithm_type_t type, uci_algorithm_id_t *algorithms, size_t *count) {
    if (!count) {
        return UCI_ERROR_INVALID_PARAM;
    }
    
    return registry_list_algorithms(type, algorithms, count);
}

int uci_keygen(uci_algorithm_id_t algorithm, uci_keypair_t *keypair) {
    if (!keypair) {
        return UCI_ERROR_INVALID_PARAM;
    }
    
    const uci_algorithm_impl_t *impl = registry_get_algorithm(algorithm);
    if (!impl) {
        return UCI_ERROR_ALGORITHM_NOT_FOUND;
    }
    
    if (!impl->keygen) {
        return UCI_ERROR_NOT_SUPPORTED;
    }
    
    keypair->algorithm = algorithm;
    return impl->keygen(keypair);
}

int uci_keypair_free(uci_keypair_t *keypair) {
    if (!keypair) {
        return UCI_ERROR_INVALID_PARAM;
    }
    
    if (keypair->public_key) {
        free(keypair->public_key);
        keypair->public_key = NULL;
    }
    
    if (keypair->private_key) {
        free(keypair->private_key);
        keypair->private_key = NULL;
    }
    
    keypair->public_key_len = 0;
    keypair->private_key_len = 0;
    
    return UCI_SUCCESS;
}

int uci_sign(const uci_keypair_t *keypair, const uint8_t *message, size_t message_len,
             uci_signature_t *signature) {
    if (!keypair || !message || !signature) {
        return UCI_ERROR_INVALID_PARAM;
    }
    
    const uci_algorithm_impl_t *impl = registry_get_algorithm(keypair->algorithm);
    if (!impl) {
        return UCI_ERROR_ALGORITHM_NOT_FOUND;
    }
    
    if (!impl->sign) {
        return UCI_ERROR_NOT_SUPPORTED;
    }
    
    signature->algorithm = keypair->algorithm;
    return impl->sign(keypair, message, message_len, signature);
}

int uci_verify(const uci_keypair_t *keypair, const uint8_t *message, size_t message_len,
               const uci_signature_t *signature) {
    if (!keypair || !message || !signature) {
        return UCI_ERROR_INVALID_PARAM;
    }
    
    const uci_algorithm_impl_t *impl = registry_get_algorithm(keypair->algorithm);
    if (!impl) {
        return UCI_ERROR_ALGORITHM_NOT_FOUND;
    }
    
    if (!impl->verify) {
        return UCI_ERROR_NOT_SUPPORTED;
    }
    
    return impl->verify(keypair, message, message_len, signature);
}

int uci_signature_free(uci_signature_t *signature) {
    if (!signature) {
        return UCI_ERROR_INVALID_PARAM;
    }
    
    if (signature->data) {
        free(signature->data);
        signature->data = NULL;
    }
    
    signature->data_len = 0;
    
    return UCI_SUCCESS;
}

int uci_encrypt(const uci_keypair_t *keypair, const uint8_t *plaintext, size_t plaintext_len,
                uci_ciphertext_t *ciphertext) {
    if (!keypair || !plaintext || !ciphertext) {
        return UCI_ERROR_INVALID_PARAM;
    }
    
    const uci_algorithm_impl_t *impl = registry_get_algorithm(keypair->algorithm);
    if (!impl) {
        return UCI_ERROR_ALGORITHM_NOT_FOUND;
    }
    
    if (!impl->encrypt) {
        return UCI_ERROR_NOT_SUPPORTED;
    }
    
    ciphertext->algorithm = keypair->algorithm;
    return impl->encrypt(keypair, plaintext, plaintext_len, ciphertext);
}

int uci_decrypt(const uci_keypair_t *keypair, const uci_ciphertext_t *ciphertext,
                uint8_t *plaintext, size_t *plaintext_len) {
    if (!keypair || !ciphertext || !plaintext || !plaintext_len) {
        return UCI_ERROR_INVALID_PARAM;
    }
    
    const uci_algorithm_impl_t *impl = registry_get_algorithm(keypair->algorithm);
    if (!impl) {
        return UCI_ERROR_ALGORITHM_NOT_FOUND;
    }
    
    if (!impl->decrypt) {
        return UCI_ERROR_NOT_SUPPORTED;
    }
    
    return impl->decrypt(keypair, ciphertext, plaintext, plaintext_len);
}

int uci_ciphertext_free(uci_ciphertext_t *ciphertext) {
    if (!ciphertext) {
        return UCI_ERROR_INVALID_PARAM;
    }
    
    if (ciphertext->ciphertext) {
        free(ciphertext->ciphertext);
        ciphertext->ciphertext = NULL;
    }
    
    ciphertext->ciphertext_len = 0;
    
    return UCI_SUCCESS;
}

int uci_kem_keygen(uci_algorithm_id_t algorithm, uci_keypair_t *keypair) {
    if (!keypair) {
        return UCI_ERROR_INVALID_PARAM;
    }
    
    const uci_algorithm_impl_t *impl = registry_get_algorithm(algorithm);
    if (!impl) {
        return UCI_ERROR_ALGORITHM_NOT_FOUND;
    }
    
    if (!impl->kem_keygen) {
        return UCI_ERROR_NOT_SUPPORTED;
    }
    
    keypair->algorithm = algorithm;
    return impl->kem_keygen(keypair);
}

int uci_kem_encaps(const uci_keypair_t *keypair, uci_kem_encaps_result_t *result) {
    if (!keypair || !result) {
        return UCI_ERROR_INVALID_PARAM;
    }
    
    const uci_algorithm_impl_t *impl = registry_get_algorithm(keypair->algorithm);
    if (!impl) {
        return UCI_ERROR_ALGORITHM_NOT_FOUND;
    }
    
    if (!impl->kem_encaps) {
        return UCI_ERROR_NOT_SUPPORTED;
    }
    
    return impl->kem_encaps(keypair, result);
}

int uci_kem_decaps(const uci_keypair_t *keypair, const uint8_t *ciphertext, size_t ciphertext_len,
                   uint8_t *shared_secret, size_t *shared_secret_len) {
    if (!keypair || !ciphertext || !shared_secret || !shared_secret_len) {
        return UCI_ERROR_INVALID_PARAM;
    }
    
    const uci_algorithm_impl_t *impl = registry_get_algorithm(keypair->algorithm);
    if (!impl) {
        return UCI_ERROR_ALGORITHM_NOT_FOUND;
    }
    
    if (!impl->kem_decaps) {
        return UCI_ERROR_NOT_SUPPORTED;
    }
    
    return impl->kem_decaps(keypair, ciphertext, ciphertext_len, shared_secret, shared_secret_len);
}

int uci_kem_encaps_result_free(uci_kem_encaps_result_t *result) {
    if (!result) {
        return UCI_ERROR_INVALID_PARAM;
    }
    
    if (result->shared_secret) {
        free(result->shared_secret);
        result->shared_secret = NULL;
    }
    
    if (result->ciphertext) {
        free(result->ciphertext);
        result->ciphertext = NULL;
    }
    
    result->shared_secret_len = 0;
    result->ciphertext_len = 0;
    
    return UCI_SUCCESS;
}

int uci_hybrid_sign(uci_algorithm_id_t algorithm, 
                    const uci_keypair_t *classic_keypair,
                    const uci_keypair_t *pq_keypair,
                    const uint8_t *message, size_t message_len,
                    uci_signature_t *signature) {
    if (!classic_keypair || !pq_keypair || !message || !signature) {
        return UCI_ERROR_INVALID_PARAM;
    }
    
    const uci_algorithm_impl_t *impl = registry_get_algorithm(algorithm);
    if (!impl) {
        return UCI_ERROR_ALGORITHM_NOT_FOUND;
    }
    
    if (!impl->sign) {
        return UCI_ERROR_NOT_SUPPORTED;
    }
    
    return UCI_ERROR_NOT_SUPPORTED;
}

int uci_hybrid_verify(uci_algorithm_id_t algorithm,
                      const uci_keypair_t *classic_keypair,
                      const uci_keypair_t *pq_keypair,
                      const uint8_t *message, size_t message_len,
                      const uci_signature_t *signature) {
    if (!classic_keypair || !pq_keypair || !message || !signature) {
        return UCI_ERROR_INVALID_PARAM;
    }
    
    const uci_algorithm_impl_t *impl = registry_get_algorithm(algorithm);
    if (!impl) {
        return UCI_ERROR_ALGORITHM_NOT_FOUND;
    }
    
    if (!impl->verify) {
        return UCI_ERROR_NOT_SUPPORTED;
    }
    
    return UCI_ERROR_NOT_SUPPORTED;
}

const char *uci_get_error_string(int error_code) {
    switch (error_code) {
        case UCI_SUCCESS:
            return "Success";
        case UCI_ERROR_INVALID_PARAM:
            return "Invalid parameter";
        case UCI_ERROR_NOT_SUPPORTED:
            return "Operation not supported";
        case UCI_ERROR_BUFFER_TOO_SMALL:
            return "Buffer too small";
        case UCI_ERROR_ALGORITHM_NOT_FOUND:
            return "Algorithm not found";
        case UCI_ERROR_INTERNAL:
            return "Internal error";
        case UCI_ERROR_SIGNATURE_INVALID:
            return "Signature verification failed";
        default:
            return "Unknown error";
    }
}