Repository navigation
Expand file tree
/
Copy pathzupt_pybind.cpp
More file actions
1377 lines (1362 loc) · 75.4 KB
/
Copy pathzupt_pybind.cpp
File metadata and controls
1377 lines (1362 loc) · 75.4 KB
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* libzupt - Python bindings with pybind11
* SPDX-License-Identifier: MIT
*/
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/iostream.h>
#include "zupt.hpp"
/**
* @cond PORT
* @file examples_python/zupt_pybind.cpp
* @brief Binding Python da camada C++ da libzupt, usando pybind11.
* @details Este TU compila o módulo nativo `zupt` (target CMake
* `zupt_python_bindings`, `OUTPUT_NAME zupt` em
* `examples_python/CMakeLists.txt`) e espelha a API de
* `include/zupt.hpp` sem adicionar lógica própria de criptografia.
* São exportados: o enum `ErrorCode`, a exceção `ZuptError`,
* onze constantes de tamanho, as classes `SecureBuffer`, `KeyPair`,
* `KeyGenerator`, `Encryptor` e `Decryptor`, e as funções de módulo
* `random_bytes`, `sha256`, `sha3_512` e `secure_wipe`.
*
* <b>Fronteira de tipos</b>: a maioria dos dados binários entra e sai
* como `bytes` (via `py::bytes`), convertida para `std::string` e
* reinterpretada como `const uint8_t*`. `pybind11/stl.h` converte
* automaticamente `std::vector<uint8_t>` para `list` de `int`, de modo
* que nem todo dado binário chega como `bytes`: o que o método devolve
* sem conversão explícita para `py::bytes` chega como `list` de inteiros
* (por exemplo `KeyPair.public_key`, `load_public_key`, `encrypt_secure`
* e `decrypt_memory`). Ver os blocos de cada método e o arquivo de
* achados.
*
* <b>Erros</b>: este TU não captura exceções de libzupt fora dos dois
* `catch (...)` dos construtores de `SecureBuffer` e de `decrypt`.
* `zupt::ZuptError` é registrada por `py::register_exception` e o
* pybind11 a converte em exceção Python da classe `zupt.ZuptError`,
* derivada de `RuntimeError`. Qualquer outra exceção C++ não
* registrada se converte em `RuntimeError` genérico do pybind11, e
* `py::error_already_set` (erro já pendente em Python) é devolvida ao
* interpretador.
*
* <b>Propriedade de memória</b>: as classes C++ são gerenciadas pelo
* pybind11, que destrói o objeto C++ quando o objeto Python morre
* (referência forte do Python para a instância C++). `SecureBuffer`
* zera o próprio conteúdo no destrutor. Os ponteiros expostos por
* `data_ptr` e `data_ptr_const` são ponteiros internos e ficam
* inválidos quando o objeto Python é destruído ou movido.
* @endcond
* @cond INGLES
* @file examples_python/zupt_pybind.cpp
* @brief Python binding for the libzupt C++ layer, using pybind11.
* @details This TU compiles the native `zupt` module (CMake target
* `zupt_python_bindings`, `OUTPUT_NAME zupt` in
* `examples_python/CMakeLists.txt`) and mirrors the API of
* `include/zupt.hpp` without adding encryption logic of its own.
* The following are exported: the `ErrorCode` enum, the `ZuptError`
* exception, eleven size constants, the classes `SecureBuffer`, `KeyPair`,
* `KeyGenerator`, `Encryptor` and `Decryptor`, and the module functions
* `random_bytes`, `sha256`, `sha3_512` and `secure_wipe`.
*
* <b>Type boundary</b>: most binary data enters and leaves as `bytes`
* (via `py::bytes`), converted to `std::string` and reinterpreted as
* `const uint8_t*`. `pybind11/stl.h` automatically converts
* `std::vector<uint8_t>` to a `list` of `int`, so not all binary data
* arrives as `bytes`: what a method returns without an explicit
* conversion to `py::bytes` arrives as a `list` of integers (for example
* `KeyPair.public_key`, `load_public_key`, `encrypt_secure` and
* `decrypt_memory`). See the block of each method and the findings file.
*
* <b>Errors</b>: this TU does not catch libzupt exceptions outside the two
* `catch (...)` of the `SecureBuffer` constructors and of `decrypt`.
* `zupt::ZuptError` is registered by `py::register_exception` and
* pybind11 converts it into a Python exception of class
* `zupt.ZuptError`, derived from `RuntimeError`. Any other unregistered
* C++ exception becomes a generic pybind11 `RuntimeError`, and
* `py::error_already_set` (an error already pending in Python) is
* returned to the interpreter.
*
* <b>Memory ownership</b>: the C++ classes are managed by pybind11, which
* destroys the C++ object when the Python object dies (strong Python
* reference to the C++ instance). `SecureBuffer` zeroes its own content
* in the destructor. The pointers exposed by `data_ptr` and
* `data_ptr_const` are internal pointers and become invalid when the
* Python object is destroyed or moved.
* @endcond */
/**
* @cond PORT
* @namespace py
* @brief Alias de namespace para `pybind11`, usado em todo o arquivo.
* @details É apenas um apelido; não introduz símbolos próprios.
* @endcond
* @cond INGLES
* @namespace py
* @brief Namespace alias for `pybind11`, used throughout the file.
* @details It is only a nickname; it introduces no symbols of its own.
* @endcond */
namespace py = pybind11;
/**
* @cond PORT
* @brief Macro de definição do módulo nativo `zupt`.
* @details `PYBIND11_MODULE(zupt, m)` expande para a função de registro
* chamada pelo interpretador no `import zupt`. O primeiro argumento
* (`zupt`) deve coincidir com o nome do módulo importável (o
* `OUTPUT_NAME` do target CMake); o segundo é a referência ao objeto
* de módulo. Todo o restante deste arquivo acontece dentro desta
* função: registro de tipos, constantes e funções livres.
* <b>Ordem de registro</b>: primeiro o enum `ErrorCode`, depois a
* exceção, depois as constantes, depois as classes e por fim as
* funções e atributos de versão. A ordem não é apenas cosmética: um
* tipo referenciado por assinatura de função precisa estar registrado
* antes do uso, o que aqui é garantido porque as classes precedem as
* funções que usam `SecureBuffer`.
* <b>Erros de registro</b>: `m.def`/`py::class_` lançam se o nome já
* existe ou se há incompatibilidade de tipo; não há tratamento de erro
* e o `import` simplesmente falha.
* - `m`: Objeto de módulo Python em construção (`py::module_&`). * @note O docstring do módulo é definido como
* "libzupt - Hybrid Post-Quantum Encryption Library".
* @endcond
* @cond INGLES
* @brief Definition macro of the native `zupt` module.
* @details `PYBIND11_MODULE(zupt, m)` expands to the registration
* function called by the interpreter on `import zupt`. The first
* argument (`zupt`) must match the name of the importable module (the
* `OUTPUT_NAME` of the CMake target); the second is the reference to
* the module object. Everything else in this file happens inside this
* function: registration of types, constants and free functions.
* <b>Registration order</b>: first the `ErrorCode` enum, then the
* exception, then the constants, then the classes and finally the
* functions and version attributes. The order is not merely cosmetic: a
* type referenced by a function signature must be registered before use,
* which here is guaranteed because the classes precede the functions that
* use `SecureBuffer`.
* <b>Registration errors</b>: `m.def`/`py::class_` throw if the name
* already exists or if there is a type mismatch; there is no error
* handling and the `import` simply fails.
* - `m`: Python module object under construction (`py::module_&`). * @note The module docstring is defined as
* "libzupt - Hybrid Post-Quantum Encryption Library".
* @endcond */
PYBIND11_MODULE(zupt, m) {
m.doc() = "libzupt - Hybrid Post-Quantum Encryption Library";
// Error codes
/**
* @cond PORT
* @brief Espelho Python do enum `zupt::ErrorCode`.
* @details Registra as 12 constantes com os mesmos nomes usados em C++ e
* aplica `export_values()`, o que coloca cada constante também no
* namespace do módulo (`zupt.ERR_IO`, além de
* `zupt.ErrorCode.ERR_IO`). Os valores numéricos são os da
* definição em `include/zupt.hpp`: `OK = 0` e os demais negativos,
* de `ERR_IO = -1` até `ERR_AUTH_FAIL = -11`.
* <b>Mapeamento</b>:
* - `OK` (0): sucesso;
* - `ERR_IO` (-1): falha de abrir/ler/escrever/arquivo;
* - `ERR_CORRUPT` (-2): estrutura interna inconsistente;
* - `ERR_BAD_MAGIC` (-3): assinatura de formato ausente/diferente;
* - `ERR_BAD_VERSION` (-4): versão de formato não suportada;
* - `ERR_BAD_CHECKSUM` (-5): soma de verificação divergente;
* - `ERR_NOMEM` (-6): alocação falhou;
* - `ERR_OVERFLOW` (-7): tamanho acima do suportado;
* - `ERR_INVALID` (-8): argumento/buffer inválido;
* - `ERR_NOT_FOUND` (-9): item inexistente;
* - `ERR_UNSUPPORTED` (-10): operação não suportada nesta build;
* - `ERR_AUTH_FAIL` (-11): MAC/GCM do envelope não validou.
* @note Nenhum caminho deste binding entrega um `ErrorCode`
* numérico ao chamador: a camada C++ sinaliza falha por exceção e
* `py::register_exception` não expõe o método `code()` de
* `zupt::ZuptError`, de modo que em Python a exceção carrega apenas
* a mensagem. As constantes registradas aqui, portanto, só têm uso
* comparativo explícito (ver arquivo de achados).
* \see include/zupt.hpp (enum `zupt::ErrorCode`)
* @endcond
* @cond INGLES
* @brief Python mirror of the `zupt::ErrorCode` enum.
* @details Registers the 12 constants with the same names used in C++ and
* applies `export_values()`, which places each constant also in the
* module namespace (`zupt.ERR_IO`, in addition to
* `zupt.ErrorCode.ERR_IO`). The numeric values are those of the
* definition in `include/zupt.hpp`: `OK = 0` and the others negative,
* from `ERR_IO = -1` to `ERR_AUTH_FAIL = -11`.
* <b>Mapping</b>:
* - `OK` (0): success;
* - `ERR_IO` (-1): open/read/write/file failure;
* - `ERR_CORRUPT` (-2): inconsistent internal structure;
* - `ERR_BAD_MAGIC` (-3): missing/different format signature;
* - `ERR_BAD_VERSION` (-4): unsupported format version;
* - `ERR_BAD_CHECKSUM` (-5): mismatching checksum;
* - `ERR_NOMEM` (-6): allocation failed;
* - `ERR_OVERFLOW` (-7): size above the supported limit;
* - `ERR_INVALID` (-8): invalid argument/buffer;
* - `ERR_NOT_FOUND` (-9): item does not exist;
* - `ERR_UNSUPPORTED` (-10): operation not supported in this build;
* - `ERR_AUTH_FAIL` (-11): envelope MAC/GCM did not validate.
* @note No path of this binding delivers a numeric `ErrorCode` to the
* caller: the C++ layer signals failure by exception and
* `py::register_exception` does not expose the `code()` method of
* `zupt::ZuptError`, so in Python the exception carries only the
* message. The constants registered here therefore have only explicit
* comparative use (see the findings file).
* \see include/zupt.hpp (enum `zupt::ErrorCode`)
* @endcond */
py::enum_<zupt::ErrorCode>(m, "ErrorCode")
.value("OK", zupt::ErrorCode::OK)
.value("ERR_IO", zupt::ErrorCode::ERR_IO)
.value("ERR_CORRUPT", zupt::ErrorCode::ERR_CORRUPT)
.value("ERR_BAD_MAGIC", zupt::ErrorCode::ERR_BAD_MAGIC)
.value("ERR_BAD_VERSION", zupt::ErrorCode::ERR_BAD_VERSION)
.value("ERR_BAD_CHECKSUM", zupt::ErrorCode::ERR_BAD_CHECKSUM)
.value("ERR_NOMEM", zupt::ErrorCode::ERR_NOMEM)
.value("ERR_OVERFLOW", zupt::ErrorCode::ERR_OVERFLOW)
.value("ERR_INVALID", zupt::ErrorCode::ERR_INVALID)
.value("ERR_NOT_FOUND", zupt::ErrorCode::ERR_NOT_FOUND)
.value("ERR_UNSUPPORTED", zupt::ErrorCode::ERR_UNSUPPORTED)
.value("ERR_AUTH_FAIL", zupt::ErrorCode::ERR_AUTH_FAIL)
.export_values();
// A real Python exception type, also catchable as RuntimeError.
/**
* @cond PORT
* @brief Registra `zupt.ZuptError` como tipo de exceção Python.
* @details `py::register_exception` cria, no módulo, uma classe de exceção
* que o pybind11 usa para lançar `std::exception` derivada de
* `zupt::ZuptError`. O terceiro argumento, `PyExc_RuntimeError`, é
* a base da nova classe: `except RuntimeError` continua
* capturando as falhas de libzupt. O texto da exceção é o que
* `std::runtime_error::what()` devolve.
* \warning o atributo `code()` de `zupt::ZuptError` não é
* transportado para Python: o registro não gera bindings da classe,
* apenas da exceção. Em Python, o código de erro (enum
* `ErrorCode` acima) não é consultável.
* @endcond
* @cond INGLES
* @brief Registers `zupt.ZuptError` as a Python exception type.
* @details `py::register_exception` creates, in the module, an exception
* class that pybind11 uses to throw `std::exception` derived from
* `zupt::ZuptError`. The third argument, `PyExc_RuntimeError`, is the
* base of the new class: `except RuntimeError` still catches libzupt
* failures. The exception text is what `std::runtime_error::what()`
* returns.
* \warning the `code()` attribute of `zupt::ZuptError` is not
* carried over to Python: the registration generates no bindings for the
* class, only for the exception. In Python, the error code (the
* `ErrorCode` enum above) is not queryable.
* @endcond */
py::register_exception<zupt::ZuptError>(m, "ZuptError", PyExc_RuntimeError);
// Constants
/**
* @cond PORT
* @brief Expõe as constantes `constexpr` de `include/zupt.hpp` como
* atributos de módulo.
* @details Cada linha atribui um `size_t` do C++ a um atributo Python, que
* o pybind11 converte em `int` imutável. Os valores são:
* `MLKEM_PUBLICKEYBYTES = 1184`, `MLKEM_SECRETKEYBYTES = 2400`,
* `MLKEM_CIPHERTEXTBYTES = 1088`, `MLKEM_SSBYTES = 32`,
* `X25519_KEYBYTES = 32`, `HYBRID_PUB_KEY_SIZE = 1224`,
* `HYBRID_PRIV_KEY_SIZE = 3656`, `HYBRID_ENC_HEADER_SIZE = 1137`,
* `AES_KEY_SIZE = 32`, `AES_NONCE_SIZE = 16` e `HMAC_SIZE = 32`.
* @note a atribuição cria cópias: mudar o valor em Python não afeta
* a biblioteca, e a biblioteca não valida que o chamador use os
* tamanhos corretos — o erro aparece mais tarde, como exceção.
* @endcond
* @cond INGLES
* @brief Exposes the `constexpr` constants of `include/zupt.hpp` as
* module attributes.
* @details Each line assigns a C++ `size_t` to a Python attribute, which
* pybind11 converts into an immutable `int`. The values are:
* `MLKEM_PUBLICKEYBYTES = 1184`, `MLKEM_SECRETKEYBYTES = 2400`,
* `MLKEM_CIPHERTEXTBYTES = 1088`, `MLKEM_SSBYTES = 32`,
* `X25519_KEYBYTES = 32`, `HYBRID_PUB_KEY_SIZE = 1224`,
* `HYBRID_PRIV_KEY_SIZE = 3656`, `HYBRID_ENC_HEADER_SIZE = 1137`,
* `AES_KEY_SIZE = 32`, `AES_NONCE_SIZE = 16` and `HMAC_SIZE = 32`.
* @note the assignment creates copies: changing the value in Python does
* not affect the library, and the library does not validate that the
* caller uses the correct sizes — the error appears later, as an
* exception.
* @endcond */
m.attr("MLKEM_PUBLICKEYBYTES") = zupt::MLKEM_PUBLICKEYBYTES;
m.attr("MLKEM_SECRETKEYBYTES") = zupt::MLKEM_SECRETKEYBYTES;
m.attr("MLKEM_CIPHERTEXTBYTES") = zupt::MLKEM_CIPHERTEXTBYTES;
m.attr("MLKEM_SSBYTES") = zupt::MLKEM_SSBYTES;
m.attr("X25519_KEYBYTES") = zupt::X25519_KEYBYTES;
m.attr("HYBRID_PUB_KEY_SIZE") = zupt::HYBRID_PUB_KEY_SIZE;
m.attr("HYBRID_PRIV_KEY_SIZE") = zupt::HYBRID_PRIV_KEY_SIZE;
m.attr("HYBRID_ENC_HEADER_SIZE") = zupt::HYBRID_ENC_HEADER_SIZE;
m.attr("AES_KEY_SIZE") = zupt::AES_KEY_SIZE;
m.attr("AES_NONCE_SIZE") = zupt::AES_NONCE_SIZE;
m.attr("HMAC_SIZE") = zupt::HMAC_SIZE;
// SecureBuffer class
/**
* @cond PORT
* @brief Registra a classe C++ `zupt::SecureBuffer` como `SecureBuffer` em Python.
* @details Sem `py::buffer_protocol`: o objeto não é conversível de/para
* `memoryview` nem aceita `numpy`, de modo que toda entrada e saída
* binária passa pelos métodos `to_bytes`, `to_string`, `__len__` e
* pelos dois construtores. O pybind11 é quem destrói o objeto C++
* quando a referência Python morre, e o destrutor de
* `zupt::SecureBuffer` chama `zeroize()`.
* \note não há `__eq__`, `__bool__` nem `__getstate__` registrados:
* dois objetos nunca comparam iguais por valor, e o objeto não é
* serializável com `pickle` (o pybind11 gera erro para tipos sem
* suporte).
* \see include/zupt.hpp (classe `zupt::SecureBuffer`)
* @endcond
* @cond INGLES
* @brief Registers the C++ class `zupt::SecureBuffer` as `SecureBuffer` in Python.
* @details No `py::buffer_protocol`: the object is not convertible
* to/from `memoryview` and does not accept `numpy`, so all binary input
* and output goes through the `to_bytes`, `to_string`, `__len__` methods
* and through the two constructors. It is pybind11 that destroys the C++
* object when the Python reference dies, and the destructor of
* `zupt::SecureBuffer` calls `zeroize()`.
* \note there is no registered `__eq__`, `__bool__` or `__getstate__`:
* two objects never compare equal by value, and the object is not
* serializable with `pickle` (pybind11 raises an error for unsupported
* types).
* \see include/zupt.hpp (class `zupt::SecureBuffer`)
* @endcond */
py::class_<zupt::SecureBuffer>(m, "SecureBuffer")
/**
* @cond PORT
* @brief Construtor `SecureBuffer(size)`.
* @details Encaminha para `zupt::SecureBuffer(size_t)`, que aloca o
* buffer sem inicializar o conteúdo: os `size` bytes ficam com
* valores indefinidos até serem escritos. A conversão do
* argumento é feita pelo pybind11, que rejeita valores
* negativos e acima de `sys.maxsize` com `OverflowError` ou
* `TypeError`. Falha de alocação se manifesta como
* `std::bad_alloc` convertido pelo pybind11 em exceção
* `RuntimeError` (nenhuma conversão própria para `MemoryError`
* é registrada neste arquivo).
* - `size`: Quantidade de bytes a alocar.
* @endcond
* @cond INGLES
* @brief `SecureBuffer(size)` constructor.
* @details Forwards to `zupt::SecureBuffer(size_t)`, which allocates the
* buffer without initializing the content: the `size` bytes hold
* undefined values until they are written. Conversion of the argument is
* done by pybind11, which rejects negative values and values above
* `sys.maxsize` with `OverflowError` or `TypeError`. Allocation failure
* manifests as `std::bad_alloc`, converted by pybind11 into a
* `RuntimeError` exception (no conversion of its own to `MemoryError` is
* registered in this file).
* - `size`: Number of bytes to allocate.
* @endcond */
.def(py::init<size_t>())
/**
* @cond PORT
* @brief Construtor flexível `SecureBuffer(data)`.
* @details Lambda de fábrica com dois ramos encadeados por `try` /
* `catch (...)`:
* - primeiro ramo: `data.cast<py::bytes>()` seguido de
* `.cast<std::string>()`, com os bytes passados a
* `zupt::SecureBuffer(const uint8_t*, size_t)` por
* `reinterpret_cast`;
* - segundo ramo (somente se o primeiro lançou qualquer
* coisa): `data.cast<py::list>()` e elemento a elemento
* `py::cast<uint8_t>`, o que rejeita valores fora de
* `0..255`, seguido de `zupt::SecureBuffer(std::vector)`.
* <b>Cópia</b>: os dois caminhos copiam os bytes para o buffer
* interno; o objeto Python de origem pode ser alterado depois
* sem afetar o `SecureBuffer`.
* \warning o `catch (...)` engolfa todas as causas: um erro
* Python já pendente (por exemplo `MemoryError`) ou um
* `bytes` válido que falhe na etapa seguinte cai no ramo de
* lista e produz mensagem enganosa.
* \warning o `std::string` intermediário do primeiro ramo não
* é zerado, de modo que permanece uma cópia do material em
* claro no heap até a liberação da string C++.
* - `data`: Conteúdo inicial: o código aceita `bytes` (primeiro ramo) * ou sequência de inteiros convertível a `list` (segundo
* ramo).
* @endcond
* @cond INGLES
* @brief Flexible `SecureBuffer(data)` constructor.
* @details Factory lambda with two branches chained by `try` /
* `catch (...)`:
* - first branch: `data.cast<py::bytes>()` followed by
* `.cast<std::string>()`, with the bytes passed to
* `zupt::SecureBuffer(const uint8_t*, size_t)` via
* `reinterpret_cast`;
* - second branch (only if the first threw anything):
* `data.cast<py::list>()` and element by element
* `py::cast<uint8_t>`, which rejects values outside `0..255`,
* followed by `zupt::SecureBuffer(std::vector)`.
* <b>Copy</b>: both paths copy the bytes into the internal buffer; the
* source Python object can be modified afterwards without affecting the
* `SecureBuffer`.
* \warning the `catch (...)` swallows all causes: a Python error already
* pending (for example `MemoryError`), or a valid `bytes` that fails in
* the next step, falls into the list branch and produces a misleading
* message.
* \warning the intermediate `std::string` of the first branch is not
* zeroed, so a copy of the plaintext material remains on the heap until
* the C++ string is released.
* - `data`: Initial content: the code accepts `bytes` (first branch) * or a sequence of integers convertible to `list` (second
* branch).
* @endcond */
.def(py::init([](py::object data) {
// Try to handle bytes first
try {
py::bytes data_bytes = data.cast<py::bytes>();
std::string data_str = data_bytes.cast<std::string>();
return new zupt::SecureBuffer(
reinterpret_cast<const uint8_t*>(data_str.data()),
data_str.size()
);
} catch (...) {
// Fall back to list of integers
py::list data_list = data.cast<py::list>();
std::vector<uint8_t> vec(data_list.size());
for (size_t i = 0; i < data_list.size(); ++i) {
vec[i] = py::cast<uint8_t>(data_list[i]);
}
return new zupt::SecureBuffer(vec);
}
}))
/**
* @cond PORT
* @brief `size()` — devolve o tamanho do buffer em bytes.
* @details Encaminhamento direto para `zupt::SecureBuffer::size()`, que
* devolve `size_` (zero depois de um movimento). Equivalente a
* `len(objeto)`, pois `__len__` aponta para o mesmo método.
* @return Inteiro com o tamanho em bytes.
* @endcond
* @cond INGLES
* @brief `size()` — returns the size of the buffer in bytes.
* @details Direct forwarding to `zupt::SecureBuffer::size()`, which
* returns `size_` (zero after a move). Equivalent to `len(object)`,
* since `__len__` points to the same method.
* @return Integer with the size in bytes.
* @endcond */
.def("size", &zupt::SecureBuffer::size)
/**
* @cond PORT
* @brief `data_ptr()` — devolve o ponteiro interno como inteiro.
* @details O cast `(uint8_t*(zupt::SecureBuffer::*)())` seleciona a
* sobrecarga não-const de `data()`. O pybind11 converte o
* `uint8_t*` retornado em um `int` de endereço.
* \warning expor o endereço interno da chave/Texto em claro é
* um vazamento de endereço e permite interação com outras
* APIs que aceitam inteiros como ponteiros (por exemplo
* `secure_wipe` deste mesmo módulo). O endereço vale apenas
* enquanto o objeto Python estiver vivo e não for movido.
* @return Endereço de memória do buffer interno como inteiro.
* @endcond
* @cond INGLES
* @brief `data_ptr()` — returns the internal pointer as an integer.
* @details The cast `(uint8_t*(zupt::SecureBuffer::*)())` selects the
* non-const overload of `data()`. pybind11 converts the returned
* `uint8_t*` into an address `int`.
* \warning exposing the internal address of the key/plaintext is an
* address leak and allows interaction with other APIs that accept
* integers as pointers (for example `secure_wipe` of this same module).
* The address is valid only while the Python object is alive and is not
* moved.
* @return Memory address of the internal buffer, as an integer.
* @endcond */
.def("data_ptr", (uint8_t*(zupt::SecureBuffer::*)()) &zupt::SecureBuffer::data)
/**
* @cond PORT
* @brief `data_ptr_const()` — mesmo ponteiro, via sobrecarga const.
* @details O cast
* `(const uint8_t*(zupt::SecureBuffer::*)() const)` seleciona a
* sobrecarga `const` de `data()`. Em Python não há diferença
* observável de mutabilidade: o retorno é apenas um inteiro, e
* o resultado é o mesmo endereço devolvido por `data_ptr()`.
* @return Endereço de memória do buffer interno como inteiro.
* @see data_ptr
* @endcond
* @cond INGLES
* @brief `data_ptr_const()` — same pointer, via the const overload.
* @details The cast
* `(const uint8_t*(zupt::SecureBuffer::*)() const)` selects the
* `const` overload of `data()`. In Python there is no observable
* difference in mutability: the return value is just an integer, and the
* result is the same address returned by `data_ptr()`.
* @return Memory address of the internal buffer, as an integer.
* @see data_ptr
* @endcond */
.def("data_ptr_const", (const uint8_t*(zupt::SecureBuffer::*)() const) &zupt::SecureBuffer::data)
/**
* @cond PORT
* @brief `to_bytes()` — cópia do conteúdo como `bytes`.
* @details Lambda que constrói `py::bytes` a partir de
* `self.data()` e `self.size()`, o que copia os bytes para um
* novo objeto imutável de Python. O conteúdo devolvido é
* independente do buffer interno.
* \warning o objeto `bytes` devolvido é exatamente o texto em
* claro e não pode ser zerado pelo chamador; `zeroize()` ou a
* destruição do `SecureBuffer` não o alcança.
* @return Objeto `bytes` com os `size()` bytes do buffer.
* @endcond
* @cond INGLES
* @brief `to_bytes()` — copy of the content as `bytes`.
* @details Lambda that constructs `py::bytes` from `self.data()` and
* `self.size()`, which copies the bytes into a new immutable Python
* object. The returned content is independent of the internal buffer.
* \warning the returned `bytes` object is exactly the plaintext and
* cannot be zeroed by the caller; `zeroize()` or the destruction of the
* `SecureBuffer` does not reach it.
* @return `bytes` object with the `size()` bytes of the buffer.
* @endcond */
.def("to_bytes", [](const zupt::SecureBuffer& self) {
return py::bytes(reinterpret_cast<const char*>(self.data()), self.size());
})
/**
* @cond PORT
* @brief `to_string()` — mesmo conteúdo que `to_bytes()`.
* @details Encaminhamento para `zupt::SecureBuffer::toString()`, que
* copia os bytes para um `std::string`; o pybind11 converte esse
* `std::string` em `str` de Python decodificando como UTF-8.
* \warning conteúdo binário aleatório quase sempre não é UTF-8
* válido, então o uso normal desta função lança `UnicodeDecodeError`.
* Para bytes use `to_bytes()`.
* @return `str` com o conteúdo, quando decodificável como UTF-8.
* @see to_bytes
* @endcond
* @cond INGLES
* @brief `to_string()` — same content as `to_bytes()`.
* @details Forwarding to `zupt::SecureBuffer::toString()`, which copies
* the bytes into a `std::string`; pybind11 converts that `std::string`
* into a Python `str` by decoding it as UTF-8.
* \warning random binary content is almost never valid UTF-8, so normal
* use of this function raises `UnicodeDecodeError`. For bytes use
* `to_bytes()`.
* @return `str` with the content, when decodable as UTF-8.
* @see to_bytes
* @endcond */
.def("to_string", &zupt::SecureBuffer::toString)
/**
* @cond PORT
* @brief `zeroize()` — zera o conteúdo do buffer.
* @details Encaminhamento para `zupt::SecureBuffer::zeroize()`, que
* aplica `zupt::secureWipe` (escrita via `volatile`). Não afeta
* cópias já devolvidas por `to_bytes()`/`to_string()`.
* @endcond
* @cond INGLES
* @brief `zeroize()` — zeroes the content of the buffer.
* @details Forwarding to `zupt::SecureBuffer::zeroize()`, which applies
* `zupt::secureWipe` (write via `volatile`). Does not affect copies
* already returned by `to_bytes()`/`to_string()`.
* @endcond */
.def("zeroize", &zupt::SecureBuffer::zeroize)
/**
* @cond PORT
* @brief `len(objeto)` — mesmo que `size()`.
* @details Aponta o protocolo de comprimento para
* `zupt::SecureBuffer::size`, de modo que `len(buf)` e
* `buf.size()` são equivalentes. Não há `__getitem__` nem
* `__iter__` registrados, então `len()` não implica indexação.
* @return Inteiro com o tamanho em bytes.
* @endcond
* @cond INGLES
* @brief `len(object)` — same as `size()`.
* @details Points the length protocol to `zupt::SecureBuffer::size`, so
* that `len(buf)` and `buf.size()` are equivalent. There is no
* registered `__getitem__` or `__iter__`, so `len()` does not imply
* indexing.
* @return Integer with the size in bytes.
* @endcond */
.def("__len__", &zupt::SecureBuffer::size)
/**
* @cond PORT
* @brief `repr(objeto)` — descrição textual do objeto.
* @details Devolve a cadeia `<SecureBuffer size=N>`, onde N é
* `self.size()`. Nenhum byte do conteúdo aparece no resultado,
* o que evita exposição acidental em logs e `print`.
* @return `str` com o formato `<SecureBuffer size=N>`.
* @endcond
* @cond INGLES
* @brief `repr(object)` — textual description of the object.
* @details Returns the string `<SecureBuffer size=N>`, where N is
* `self.size()`. No byte of the content appears in the result, which
* avoids accidental exposure in logs and `print`.
* @return `str` in the format `<SecureBuffer size=N>`.
* @endcond */
.def("__repr__", [](const zupt::SecureBuffer& self) {
return "<SecureBuffer size=" + std::to_string(self.size()) + ">";
});
// KeyPair struct
/**
* @cond PORT
* @brief Registra a struct `zupt::KeyPair` como `KeyPair` em Python.
* @details Expõe os dois membros como atributos somente leitura
* (`def_readonly`). Como `public_key` e `secret_key` são
* `std::vector<uint8_t>`, o pybind11 (via `pybind11/stl.h`) os
* converte em `list` de `int`, e não em `bytes`: cada acesso
* produz uma lista nova, copiada dos vetores C++.
* \warning atributos `def_readonly` não impedem mutação do objeto
* Python recebido (`list.append` funciona), mas a mudança não é
* refletida no `KeyPair` C++, pois a lista é uma cópia.
* \warning o `std::vector` de chave privada permanece em claro no
* objeto C++ e o `list` de Python não tem como ser zerado.
* @endcond
* @cond INGLES
* @brief Registers the C++ struct `zupt::KeyPair` as `KeyPair` in Python.
* @details Exposes the two members as read-only attributes
* (`def_readonly`). Since `public_key` and `secret_key` are
* `std::vector<uint8_t>`, pybind11 (via `pybind11/stl.h`) converts them
* into a `list` of `int`, and not into `bytes`: each access produces a
* new list, copied from the C++ vectors.
* \warning `def_readonly` attributes do not prevent mutation of the
* Python object received (`list.append` works), but the change is not
* reflected in the C++ `KeyPair`, because the list is a copy.
* \warning the private key `std::vector` remains plaintext in the C++
* object, and the Python `list` has no way to be zeroed.
* @endcond */
py::class_<zupt::KeyPair>(m, "KeyPair")
/**
* @cond PORT
* @brief `KeyPair()` — construtor padrão.
* @details Encaminha para `zupt::KeyPair()`. Os dois vetores ficam
* vazios, de modo que `len(kp.public_key) == 0` logo após a
* criação.
* @endcond
* @cond INGLES
* @brief `KeyPair()` — default constructor.
* @details Forwards to `zupt::KeyPair()`. The two vectors are left empty,
* so `len(kp.public_key) == 0` right after creation.
* @endcond */
.def(py::init<>())
/**
* @cond PORT
* Chave pública híbrida, devolvida como `list` de inteiros.
* @endcond
* @cond INGLES
* Hybrid public key, returned as a `list` of integers.
* @endcond */
.def_readonly("public_key", &zupt::KeyPair::public_key)
/**
* @cond PORT
* Chave secreta híbrida, devolvida como `list` de inteiros.
* @endcond
* @cond INGLES
* Hybrid secret key, returned as a `list` of integers.
* @endcond */
.def_readonly("secret_key", &zupt::KeyPair::secret_key);
// KeyGenerator class
/**
* @cond PORT
* @brief Registra a classe C++ `zupt::KeyGenerator` como `KeyGenerator`.
* @details As assinaturas Python divergem do nome C++ apenas na convenção
* (`snake_case` contra `camelCase`): `generate_keypair`,
* `load_keypair`, `load_public_key`, `export_public_key` e
* `save_keypair`. Nenhum `py::arg` nomeia os parâmetros, então o
* chamador só pode passar valores posicionalmente: tentar usar
* palavras-chave levanta `TypeError`.
* \note `save_keypair` e `export_public_key` escrevem em disco no
* caminho informado pelo chamador; não há validação deste lado.
* @endcond
* @cond INGLES
* @brief Registers the C++ class `zupt::KeyGenerator` as `KeyGenerator`.
* @details The Python signatures differ from the C++ names only in
* convention (`snake_case` versus `camelCase`): `generate_keypair`,
* `load_keypair`, `load_public_key`, `export_public_key` and
* `save_keypair`. No `py::arg` names the parameters, so the caller can
* only pass values positionally: trying to use keywords raises
* `TypeError`.
* \note `save_keypair` and `export_public_key` write to disk at the path
* given by the caller; there is no validation on this side.
* @endcond */
py::class_<zupt::KeyGenerator>(m, "KeyGenerator")
/**
* @cond PORT
* @brief `KeyGenerator()` — construtor vazio.
* @details Encaminha para `zupt::KeyGenerator()`, que não recebe
* parâmetros.
* @endcond
* @cond INGLES
* @brief `KeyGenerator()` — empty constructor.
* @details Forwards to `zupt::KeyGenerator()`, which takes no parameters.
* @endcond */
.def(py::init<>())
/**
* @cond PORT
* @brief `generate_keypair()` — gera um par de chaves híbrido.
* @details Encaminhamento direto. O retorno é uma cópia da struct C++
* convertida em objeto `KeyPair` de Python.
* \warning ao contrário do binding Node.js, nenhum
* `secureWipe` é aplicado ao `zupt::KeyPair` C++ local: ele é
* convertido e depois destruído sem limpeza explícita.
* @return Objeto `KeyPair` com as chaves geradas.
* @endcond
* @cond INGLES
* @brief `generate_keypair()` — generates a hybrid key pair.
* @details Direct forwarding. The return value is a copy of the C++
* struct converted into a Python `KeyPair` object.
* \warning unlike the Node.js binding, no `secureWipe` is applied to the
* local `zupt::KeyPair` C++ object: it is converted and then destroyed
* without explicit wiping.
* @return `KeyPair` object with the generated keys.
* @endcond */
.def("generate_keypair", &zupt::KeyGenerator::generateKeyPair)
/**
* @cond PORT
* @brief `load_keypair(filename)` — lê keypair de arquivo.
* @details Encaminhamento direto para `zupt::KeyGenerator::loadKeyPair`.
* Falhas de leitura/versão/magic chegam como exceção
* `ZuptError` em Python.
* - `filename`: Caminho do arquivo de chave privada híbrida. * @return Objeto `KeyPair` carregado.
* @endcond
* @cond INGLES
* @brief `load_keypair(filename)` — reads a keypair from a file.
* @details Direct forwarding to `zupt::KeyGenerator::loadKeyPair`.
* Read/version/magic failures arrive as a `ZuptError` exception in
* Python.
* - `filename`: Path of the hybrid private key file. * @return Loaded `KeyPair` object.
* @endcond */
.def("load_keypair", &zupt::KeyGenerator::loadKeyPair)
/**
* @cond PORT
* @brief `load_public_key(filename)` — lê chave pública de arquivo.
* @details Encaminhamento direto. Como o retorno é `std::vector<uint8_t>`,
* o pybind11 o entrega como `list` de inteiros, e não como
* `bytes`.
* \warning inconsistência com os métodos de cifra, que devolvem
* `bytes`: aqui o chamador precisa de `bytes(load_public_key(...))`
* para obter um `bytes`, e `bytes(list_de_ints)` funciona apenas
* se todos os valores estiverem em `0..255`.
* - `filename`: Caminho do arquivo de chave pública híbrida. * @return `list` de inteiros com a chave pública lida.
* @endcond
* @cond INGLES
* @brief `load_public_key(filename)` — reads a public key from a file.
* @details Direct forwarding. Since the return value is
* `std::vector<uint8_t>`, pybind11 delivers it as a `list` of integers,
* and not as `bytes`.
* \warning inconsistency with the cipher methods, which return `bytes`:
* here the caller needs `bytes(load_public_key(...))` to obtain a
* `bytes`, and `bytes(list_of_ints)` works only if all the values are
* within `0..255`.
* - `filename`: Path of the hybrid public key file. * @return `list` of integers with the public key read.
* @endcond */
.def("load_public_key", &zupt::KeyGenerator::loadPublicKey)
/**
* @cond PORT
* @brief `export_public_key(privfile, pubfile)` — extrai a chave pública.
* @details Encaminhamento para
* `zupt::KeyGenerator::exportPublicKey(privfile, pubfile)`: lê a
* keypair privada de `privfile` e escreve apenas a parte pública
* em `pubfile`. Nenhum valor é devolvido.
* \warning sobrescreve `pubfile` sem aviso, e ambos os
* caminhos vêm direto do chamador, sem validação.
* - `privfile`: Caminho do arquivo de chave privada híbrida. * - `pubfile`: Caminho do arquivo público de saída.
* @endcond
* @cond INGLES
* @brief `export_public_key(privfile, pubfile)` — extracts the public key.
* @details Forwarding to
* `zupt::KeyGenerator::exportPublicKey(privfile, pubfile)`: it reads the
* private keypair from `privfile` and writes only the public part to
* `pubfile`. No value is returned.
* \warning overwrites `pubfile` without warning, and both paths come
* directly from the caller, without validation.
* - `privfile`: Path of the hybrid private key file. * - `pubfile`: Path of the public output file.
* @endcond */
.def("export_public_key", &zupt::KeyGenerator::exportPublicKey)
/**
* @cond PORT
* @brief `save_keypair(keypair, filename)` — persiste a keypair.
* @details Encaminhamento para
* `zupt::KeyGenerator::saveKeyPair(const KeyPair&, const std::string&)`.
* O primeiro argumento é o objeto `KeyPair` registrado pelo
* pybind11, de modo que a função C++ recebe a instância
* original, e não uma cópia convertida.
* \warning não há checagem de que o objeto foi gerado ou
* carregado: um `KeyPair()` vazio chega ao serializador com
* vetores vazios.
* - `keypair`: Objeto `KeyPair` a serializar. * - `filename`: Caminho do arquivo de saída.
* @endcond
* @cond INGLES
* @brief `save_keypair(keypair, filename)` — persists the keypair.
* @details Forwarding to
* `zupt::KeyGenerator::saveKeyPair(const KeyPair&, const std::string&)`.
* The first argument is the `KeyPair` object registered by pybind11, so
* the C++ function receives the original instance, and not a converted
* copy.
* \warning there is no check that the object was generated or loaded: an
* empty `KeyPair()` reaches the serializer with empty vectors.
* - `keypair`: `KeyPair` object to serialize. * - `filename`: Path of the output file.
* @endcond */
.def("save_keypair", &zupt::KeyGenerator::saveKeyPair);
// Encryptor class
/**
* @cond PORT
* @brief Registra a classe C++ `zupt::Encryptor` como `Encryptor`.
* @details Cinco pontos de entrada de cifra com semânticas diferentes de
* conversão de entrada e de saída, o que obriga o chamador a
* conhecer qual método está usando:
* - `encrypt` e `encrypt_memory` devolvem `tuple(bytes, bytes)`;
* - `encrypt_secure` devolve `tuple(list, list)` porque o retorno
* passa direto pelo conversor STL de `std::vector<uint8_t>`;
* - `encrypt_file` devolve `tuple(bytes, bytes)`;
* - `HEADER_SIZE` é uma propriedade somente leitura de classe.
* Nenhuma operação salva o arquivo cifrado em disco:
* `encrypt_file` devolve o resultado em memória.
* \note nenhum método é liberado do GIL explicitamente (`py::call_guard`
* ausente), então a cifra bloqueia o interpretador durante a chamada.
* @endcond
* @cond INGLES
* @brief Registers the C++ class `zupt::Encryptor` as `Encryptor`.
* @details Five cipher entry points with different input-conversion and
* output semantics, which forces the caller to know which method is
* being used:
* - `encrypt` and `encrypt_memory` return `tuple(bytes, bytes)`;
* - `encrypt_secure` returns `tuple(list, list)` because the return
* value passes straight through the STL converter of
* `std::vector<uint8_t>`;
* - `encrypt_file` returns `tuple(bytes, bytes)`;
* - `HEADER_SIZE` is a read-only class property.
* No operation saves the encrypted file to disk: `encrypt_file` returns
* the result in memory.
* \note no method explicitly releases the GIL (no `py::call_guard`), so
* encryption blocks the interpreter during the call.
* @endcond */
py::class_<zupt::Encryptor>(m, "Encryptor")
/**
* @cond PORT
* @brief `Encryptor(public_key)` — constrói o criptógrafo.
* @details O argumento é `const std::vector<uint8_t>&`: o pybind11 aceita
* `list`/`tuple` de inteiros e converte para um vetor temporário,
* que é copiado para dentro do `zupt::Encryptor`.
* \warning um `bytes` NÃO satisfaz essa assinatura: não existe
* no pybind11 conversão de `bytes` para
* `std::vector<uint8_t>`, de modo que a chamada falha com
* `TypeError` de argumentos incompatíveis. O docstring de
* `examples_python/__init__.py` mostra
* `zupt.Encryptor(keypair.public_key)`, que só funciona porque
* `public_key` chega como `list` de inteiros.
* - `public_key`: Chave pública híbrida como sequência de inteiros.
* @endcond
* @cond INGLES
* @brief `Encryptor(public_key)` — builds the encryptor.
* @details The argument is `const std::vector<uint8_t>&`: pybind11
* accepts a `list`/`tuple` of integers and converts it into a temporary
* vector, which is copied into the `zupt::Encryptor`.
* \warning a `bytes` does NOT satisfy this signature: pybind11 has no
* conversion from `bytes` to `std::vector<uint8_t>`, so the call fails
* with a `TypeError` of incompatible arguments. The docstring of
* `examples_python/__init__.py` shows
* `zupt.Encryptor(keypair.public_key)`, which only works because
* `public_key` arrives as a `list` of integers.
* - `public_key`: Hybrid public key, as a sequence of integers.
* @endcond */
.def(py::init<const std::vector<uint8_t>&>())
/**
* @cond PORT
* @brief `encrypt(data)` — cifra texto claro vindo de `bytes`.
* @details Converte `data` em `bytes` e depois em `std::string`, e chama
* `zupt::Encryptor::encryptMemory(const uint8_t*, size_t)`. O
* par `{ciphertext, enc_header}` é convertido em
* `tuple(bytes, bytes)`.
* <b>Cópia</b>: o texto claro passa por `std::string`
* intermediário; nenhuma cópia é zerada.
* \warning `data.cast<py::bytes>()` não está protegido por `try`:
* se `data` não for conversível, o pybind11 levanta `TypeError`.
* \warning este método e `encrypt_memory` fazem a mesma coisa com
* assinaturas diferentes, e `encrypt_secure` faz a mesma cifra com
* tipo de saída distinto.
* - `data`: Texto claro como `bytes`. * @return `tuple` `(ciphertext, enc_header)`, ambos `bytes`.
* @see encrypt_memory, encrypt_secure
* @endcond
* @cond INGLES
* @brief `encrypt(data)` — encrypts plaintext coming from `bytes`.
* @details Converts `data` into `bytes` and then into `std::string`, and
* calls `zupt::Encryptor::encryptMemory(const uint8_t*, size_t)`. The
* `{ciphertext, enc_header}` pair is converted into
* `tuple(bytes, bytes)`.
* <b>Copy</b>: the plaintext passes through an intermediate
* `std::string`; no copy is zeroed.
* \warning `data.cast<py::bytes>()` is not protected by `try`: if `data`
* is not convertible, pybind11 raises `TypeError`.
* \warning this method and `encrypt_memory` do the same thing with
* different signatures, and `encrypt_secure` performs the same
* encryption with a different output type.
* - `data`: Plaintext as `bytes`. * @return `tuple` `(ciphertext, enc_header)`, both `bytes`.
* @see encrypt_memory, encrypt_secure
* @endcond */
.def("encrypt", [](zupt::Encryptor& self, py::object data) {
py::bytes data_bytes = data.cast<py::bytes>();
std::string data_str = data_bytes.cast<std::string>();
auto result = self.encryptMemory(reinterpret_cast<const uint8_t*>(data_str.data()), data_str.size());
return py::make_tuple(
py::bytes(reinterpret_cast<const char*>(result.first.data()), result.first.size()),
py::bytes(reinterpret_cast<const char*>(result.second.data()), result.second.size())
);
})
/**
* @cond PORT
* @brief `encrypt_memory(data, size)` — cifra a partir de endereço e contagem.
* @details A assinatura C++ é `(const uint8_t* data, size_t size)`. O
* pybind11 converte o primeiro parâmetro a partir de um
* `int`/`None` interpretado como endereço, e o segundo a partir
* de um inteiro.
* \warning perigoso: qualquer número inteiro vale como
* `data`, e o pybind11 não valida o endereço nem o comprimento.
* Um chamador Python pode ler memória fora de qualquer buffer
* controlado, inclusive fora do processo. Para texto comum use
* `encrypt()`.
* <b>Conversões</b>: o retorno é `tuple(bytes, bytes)`, como em
* `encrypt()`.
* - `data`: Endereço de memória inicial do texto claro, como inteiro. * - `size`: Quantidade de bytes a cifrar. * @return `tuple` `(ciphertext, enc_header)`, ambos `bytes`.
* @see encrypt
* @endcond
* @cond INGLES
* @brief `encrypt_memory(data, size)` — encrypts from an address and a count.
* @details The C++ signature is `(const uint8_t* data, size_t size)`.
* pybind11 converts the first parameter from an `int`/`None`
* interpreted as an address, and the second from an integer.
* \warning dangerous: any integer is accepted as `data`, and pybind11
* validates neither the address nor the length. A Python caller can read
* memory outside any controlled buffer, including outside the process.
* For ordinary text use `encrypt()`.
* <b>Conversions</b>: the return value is `tuple(bytes, bytes)`, as in
* `encrypt()`.
* - `data`: Starting memory address of the plaintext, as an integer. * - `size`: Number of bytes to encrypt. * @return `tuple` `(ciphertext, enc_header)`, both `bytes`.
* @see encrypt
* @endcond */
.def("encrypt_memory", [](zupt::Encryptor& self, const uint8_t* data, size_t size) {
auto result = self.encryptMemory(data, size);
return py::make_tuple(
py::bytes(reinterpret_cast<const char*>(result.first.data()), result.first.size()),
py::bytes(reinterpret_cast<const char*>(result.second.data()), result.second.size())
);
})
/**
* @cond PORT
* @brief `encrypt_secure(buffer)` — cifra o conteúdo de um `SecureBuffer`.
* @details Chama `zupt::Encryptor::encryptMemory(const SecureBuffer&)`,
* que lê o buffer internamente sem cópia adicional. O par de
* vetores C++ é devolvido sem conversão para `py::bytes`, de modo
* que o resultado chega como `tuple(list, list)` de inteiros.
* \warning inconsistência de tipo de saída frente a `encrypt()` e
* `encrypt_memory()`, que devolvem `bytes`. A conversão manual
* `bytes(x)` para cada elemento da tupla é necessária para
* interoperar com os métodos de decifragem que esperam `bytes`.
* - `buffer`: Objeto `SecureBuffer` com o texto claro. * @return `tuple` `(ciphertext, enc_header)`, ambos `list` de inteiros.
* @see encrypt
* @endcond
* @cond INGLES
* @brief `encrypt_secure(buffer)` — encrypts the content of a `SecureBuffer`.
* @details Calls `zupt::Encryptor::encryptMemory(const SecureBuffer&)`,
* which reads the buffer internally without an additional copy. The pair
* of C++ vectors is returned without conversion to `py::bytes`, so the
* result arrives as a `tuple(list, list)` of integers.
* \warning output type inconsistency with respect to `encrypt()` and
* `encrypt_memory()`, which return `bytes`. The manual conversion
* `bytes(x)` for each element of the tuple is needed to interoperate
* with the decryption methods that expect `bytes`.
* - `buffer`: `SecureBuffer` object with the plaintext. * @return `tuple` `(ciphertext, enc_header)`, both a `list` of integers.
* @see encrypt
* @endcond */
.def("encrypt_secure", [](zupt::Encryptor& self, const zupt::SecureBuffer& buffer) {
return self.encryptMemory(buffer);
})
/**
* @cond PORT
* @brief `encrypt_file(filename)` — cifra o conteúdo de um arquivo.
* @details Lê o arquivo, cifra e devolve `tuple(bytes, bytes)`. Não
* sobrescreve nem cria arquivo cifrado em disco.
* \warning `filename` vem direto do chamador, sem validação;
* falhas de leitura chegam como exceção `ZuptError`.
* - `filename`: Caminho do arquivo em texto claro. * @return `tuple` `(ciphertext, enc_header)`, ambos `bytes`.
* @endcond
* @cond INGLES
* @brief `encrypt_file(filename)` — encrypts the content of a file.
* @details Reads the file, encrypts it and returns
* `tuple(bytes, bytes)`. It does not overwrite nor create an encrypted
* file on disk.
* \warning `filename` comes directly from the caller, without
* validation; read failures arrive as a `ZuptError` exception.
* - `filename`: Path of the plaintext file. * @return `tuple` `(ciphertext, enc_header)`, both `bytes`.
* @endcond */
.def("encrypt_file", [](zupt::Encryptor& self, const std::string& filename) {
auto [ciphertext, enc_header] = self.encryptFile(filename);
return py::make_tuple(
py::bytes(reinterpret_cast<const char*>(ciphertext.data()), ciphertext.size()),
py::bytes(reinterpret_cast<const char*>(enc_header.data()), enc_header.size())
);
})
/**
* @cond PORT
* @brief Propriedade de classe `Encryptor.HEADER_SIZE`.
* @details Getter estático que devolve o que
* `zupt::Encryptor::getEncryptionHeaderSize()` retorna — a
* constante `HYBRID_ENC_HEADER_SIZE`, 1137 bytes.
* `def_property_readonly_static` a apresenta como atributo de
* classe, e não como método: `zupt.Encryptor.HEADER_SIZE` é um
* `int`.
* \note o valor é lido a cada acesso, mas é constante; não há
* estado envolvido.
* @return Inteiro com o tamanho do cabeçalho de cifra, em bytes.
* @endcond
* @cond INGLES