charm-crypto-framework 0.61.1__cp313-cp313-macosx_10_13_universal2.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (323) hide show
  1. charm/__init__.py +5 -0
  2. charm/adapters/__init__.py +0 -0
  3. charm/adapters/abenc_adapt_hybrid.py +90 -0
  4. charm/adapters/dabenc_adapt_hybrid.py +145 -0
  5. charm/adapters/ibenc_adapt_hybrid.py +72 -0
  6. charm/adapters/ibenc_adapt_identityhash.py +80 -0
  7. charm/adapters/kpabenc_adapt_hybrid.py +91 -0
  8. charm/adapters/pkenc_adapt_bchk05.py +121 -0
  9. charm/adapters/pkenc_adapt_chk04.py +91 -0
  10. charm/adapters/pkenc_adapt_hybrid.py +98 -0
  11. charm/adapters/pksig_adapt_naor01.py +89 -0
  12. charm/config.py +7 -0
  13. charm/core/__init__.py +0 -0
  14. charm/core/benchmark/benchmark_util.c +353 -0
  15. charm/core/benchmark/benchmark_util.h +61 -0
  16. charm/core/benchmark/benchmarkmodule.c +476 -0
  17. charm/core/benchmark/benchmarkmodule.h +162 -0
  18. charm/core/benchmark.cpython-313-darwin.so +0 -0
  19. charm/core/crypto/AES/AES.c +1464 -0
  20. charm/core/crypto/AES.cpython-313-darwin.so +0 -0
  21. charm/core/crypto/DES/DES.c +113 -0
  22. charm/core/crypto/DES.cpython-313-darwin.so +0 -0
  23. charm/core/crypto/DES3/DES3.c +26 -0
  24. charm/core/crypto/DES3.cpython-313-darwin.so +0 -0
  25. charm/core/crypto/__init__.py +0 -0
  26. charm/core/crypto/cryptobase/XOR.c +80 -0
  27. charm/core/crypto/cryptobase/_counter.c +496 -0
  28. charm/core/crypto/cryptobase/_counter.h +54 -0
  29. charm/core/crypto/cryptobase/block_template.c +900 -0
  30. charm/core/crypto/cryptobase/block_template.h +69 -0
  31. charm/core/crypto/cryptobase/cryptobasemodule.c +220 -0
  32. charm/core/crypto/cryptobase/libtom/tomcrypt.h +90 -0
  33. charm/core/crypto/cryptobase/libtom/tomcrypt_argchk.h +44 -0
  34. charm/core/crypto/cryptobase/libtom/tomcrypt_cfg.h +186 -0
  35. charm/core/crypto/cryptobase/libtom/tomcrypt_cipher.h +941 -0
  36. charm/core/crypto/cryptobase/libtom/tomcrypt_custom.h +556 -0
  37. charm/core/crypto/cryptobase/libtom/tomcrypt_des.c +1912 -0
  38. charm/core/crypto/cryptobase/libtom/tomcrypt_hash.h +407 -0
  39. charm/core/crypto/cryptobase/libtom/tomcrypt_mac.h +496 -0
  40. charm/core/crypto/cryptobase/libtom/tomcrypt_macros.h +435 -0
  41. charm/core/crypto/cryptobase/libtom/tomcrypt_math.h +534 -0
  42. charm/core/crypto/cryptobase/libtom/tomcrypt_misc.h +103 -0
  43. charm/core/crypto/cryptobase/libtom/tomcrypt_pk.h +653 -0
  44. charm/core/crypto/cryptobase/libtom/tomcrypt_pkcs.h +90 -0
  45. charm/core/crypto/cryptobase/libtom/tomcrypt_prng.h +199 -0
  46. charm/core/crypto/cryptobase/stream_template.c +271 -0
  47. charm/core/crypto/cryptobase/strxor.c +229 -0
  48. charm/core/crypto/cryptobase.cpython-313-darwin.so +0 -0
  49. charm/core/engine/__init__.py +5 -0
  50. charm/core/engine/protocol.py +293 -0
  51. charm/core/engine/util.py +174 -0
  52. charm/core/math/__init__.py +0 -0
  53. charm/core/math/elliptic_curve/ecmodule.c +1986 -0
  54. charm/core/math/elliptic_curve/ecmodule.h +230 -0
  55. charm/core/math/elliptic_curve.cpython-313-darwin.so +0 -0
  56. charm/core/math/elliptic_curve.pyi +63 -0
  57. charm/core/math/integer/integermodule.c +2539 -0
  58. charm/core/math/integer/integermodule.h +145 -0
  59. charm/core/math/integer.cpython-313-darwin.so +0 -0
  60. charm/core/math/integer.pyi +76 -0
  61. charm/core/math/pairing/miracl/miracl_config.h +37 -0
  62. charm/core/math/pairing/miracl/miracl_interface.h +118 -0
  63. charm/core/math/pairing/miracl/miracl_interface2.h +126 -0
  64. charm/core/math/pairing/miracl/pairingmodule2.c +2094 -0
  65. charm/core/math/pairing/miracl/pairingmodule2.h +307 -0
  66. charm/core/math/pairing/pairingmodule.c +2230 -0
  67. charm/core/math/pairing/pairingmodule.h +241 -0
  68. charm/core/math/pairing/relic/pairingmodule3.c +1853 -0
  69. charm/core/math/pairing/relic/pairingmodule3.h +233 -0
  70. charm/core/math/pairing/relic/relic_interface.c +1337 -0
  71. charm/core/math/pairing/relic/relic_interface.h +217 -0
  72. charm/core/math/pairing/relic/test_relic.c +171 -0
  73. charm/core/math/pairing.cpython-313-darwin.so +0 -0
  74. charm/core/math/pairing.pyi +69 -0
  75. charm/core/utilities/base64.c +248 -0
  76. charm/core/utilities/base64.h +15 -0
  77. charm/schemes/__init__.py +0 -0
  78. charm/schemes/abenc/__init__.py +0 -0
  79. charm/schemes/abenc/abenc_accountability_jyjxgd20.py +647 -0
  80. charm/schemes/abenc/abenc_bsw07.py +146 -0
  81. charm/schemes/abenc/abenc_ca_cpabe_ar17.py +684 -0
  82. charm/schemes/abenc/abenc_dacmacs_yj14.py +298 -0
  83. charm/schemes/abenc/abenc_lsw08.py +159 -0
  84. charm/schemes/abenc/abenc_maabe_rw15.py +236 -0
  85. charm/schemes/abenc/abenc_maabe_yj14.py +297 -0
  86. charm/schemes/abenc/abenc_tbpre_lww14.py +309 -0
  87. charm/schemes/abenc/abenc_unmcpabe_yahk14.py +223 -0
  88. charm/schemes/abenc/abenc_waters09.py +144 -0
  89. charm/schemes/abenc/abenc_yct14.py +208 -0
  90. charm/schemes/abenc/abenc_yllc15.py +178 -0
  91. charm/schemes/abenc/ac17.py +248 -0
  92. charm/schemes/abenc/bsw07.py +141 -0
  93. charm/schemes/abenc/cgw15.py +277 -0
  94. charm/schemes/abenc/dabe_aw11.py +204 -0
  95. charm/schemes/abenc/dfa_fe12.py +144 -0
  96. charm/schemes/abenc/pk_hve08.py +179 -0
  97. charm/schemes/abenc/waters11.py +143 -0
  98. charm/schemes/aggrsign_MuSig.py +150 -0
  99. charm/schemes/aggrsign_bls.py +267 -0
  100. charm/schemes/blindsig_ps16.py +654 -0
  101. charm/schemes/chamhash_adm05.py +113 -0
  102. charm/schemes/chamhash_rsa_hw09.py +100 -0
  103. charm/schemes/commit/__init__.py +0 -0
  104. charm/schemes/commit/commit_gs08.py +77 -0
  105. charm/schemes/commit/commit_pedersen92.py +53 -0
  106. charm/schemes/encap_bchk05.py +62 -0
  107. charm/schemes/grpsig/__init__.py +0 -0
  108. charm/schemes/grpsig/groupsig_bgls04.py +114 -0
  109. charm/schemes/grpsig/groupsig_bgls04_var.py +115 -0
  110. charm/schemes/hibenc/__init__.py +0 -0
  111. charm/schemes/hibenc/hibenc_bb04.py +105 -0
  112. charm/schemes/hibenc/hibenc_lew11.py +193 -0
  113. charm/schemes/ibenc/__init__.py +0 -0
  114. charm/schemes/ibenc/clpkc_rp03.py +119 -0
  115. charm/schemes/ibenc/ibenc_CW13_z.py +168 -0
  116. charm/schemes/ibenc/ibenc_bb03.py +94 -0
  117. charm/schemes/ibenc/ibenc_bf01.py +121 -0
  118. charm/schemes/ibenc/ibenc_ckrs09.py +120 -0
  119. charm/schemes/ibenc/ibenc_cllww12_z.py +172 -0
  120. charm/schemes/ibenc/ibenc_lsw08.py +120 -0
  121. charm/schemes/ibenc/ibenc_sw05.py +238 -0
  122. charm/schemes/ibenc/ibenc_waters05.py +144 -0
  123. charm/schemes/ibenc/ibenc_waters05_z.py +164 -0
  124. charm/schemes/ibenc/ibenc_waters09.py +107 -0
  125. charm/schemes/ibenc/ibenc_waters09_z.py +147 -0
  126. charm/schemes/joye_scheme.py +106 -0
  127. charm/schemes/lem_scheme.py +207 -0
  128. charm/schemes/pk_fre_ccv11.py +107 -0
  129. charm/schemes/pk_vrf.py +127 -0
  130. charm/schemes/pkenc/__init__.py +0 -0
  131. charm/schemes/pkenc/pkenc_cs98.py +108 -0
  132. charm/schemes/pkenc/pkenc_elgamal85.py +122 -0
  133. charm/schemes/pkenc/pkenc_gm82.py +98 -0
  134. charm/schemes/pkenc/pkenc_paillier99.py +118 -0
  135. charm/schemes/pkenc/pkenc_rabin.py +254 -0
  136. charm/schemes/pkenc/pkenc_rsa.py +186 -0
  137. charm/schemes/pksig/__init__.py +0 -0
  138. charm/schemes/pksig/pksig_CW13_z.py +135 -0
  139. charm/schemes/pksig/pksig_bls04.py +87 -0
  140. charm/schemes/pksig/pksig_boyen.py +156 -0
  141. charm/schemes/pksig/pksig_chch.py +97 -0
  142. charm/schemes/pksig/pksig_chp.py +70 -0
  143. charm/schemes/pksig/pksig_cl03.py +150 -0
  144. charm/schemes/pksig/pksig_cl04.py +87 -0
  145. charm/schemes/pksig/pksig_cllww12_z.py +142 -0
  146. charm/schemes/pksig/pksig_cyh.py +132 -0
  147. charm/schemes/pksig/pksig_dsa.py +76 -0
  148. charm/schemes/pksig/pksig_ecdsa.py +71 -0
  149. charm/schemes/pksig/pksig_hess.py +104 -0
  150. charm/schemes/pksig/pksig_hw.py +110 -0
  151. charm/schemes/pksig/pksig_lamport.py +63 -0
  152. charm/schemes/pksig/pksig_ps01.py +135 -0
  153. charm/schemes/pksig/pksig_ps02.py +124 -0
  154. charm/schemes/pksig/pksig_ps03.py +119 -0
  155. charm/schemes/pksig/pksig_rsa_hw09.py +206 -0
  156. charm/schemes/pksig/pksig_schnorr91.py +77 -0
  157. charm/schemes/pksig/pksig_waters.py +115 -0
  158. charm/schemes/pksig/pksig_waters05.py +121 -0
  159. charm/schemes/pksig/pksig_waters09.py +121 -0
  160. charm/schemes/pre_mg07.py +150 -0
  161. charm/schemes/prenc/pre_afgh06.py +126 -0
  162. charm/schemes/prenc/pre_bbs98.py +123 -0
  163. charm/schemes/prenc/pre_nal16.py +216 -0
  164. charm/schemes/protocol_a01.py +272 -0
  165. charm/schemes/protocol_ao00.py +215 -0
  166. charm/schemes/protocol_cns07.py +274 -0
  167. charm/schemes/protocol_schnorr91.py +125 -0
  168. charm/schemes/sigma1.py +64 -0
  169. charm/schemes/sigma2.py +129 -0
  170. charm/schemes/sigma3.py +126 -0
  171. charm/schemes/threshold/__init__.py +59 -0
  172. charm/schemes/threshold/dkls23_dkg.py +556 -0
  173. charm/schemes/threshold/dkls23_presign.py +1089 -0
  174. charm/schemes/threshold/dkls23_sign.py +761 -0
  175. charm/schemes/threshold/xrpl_wallet.py +967 -0
  176. charm/test/__init__.py +0 -0
  177. charm/test/adapters/__init__.py +0 -0
  178. charm/test/adapters/abenc_adapt_hybrid_test.py +29 -0
  179. charm/test/adapters/dabenc_adapt_hybrid_test.py +56 -0
  180. charm/test/adapters/ibenc_adapt_hybrid_test.py +36 -0
  181. charm/test/adapters/ibenc_adapt_identityhash_test.py +32 -0
  182. charm/test/adapters/kpabenc_adapt_hybrid_test.py +30 -0
  183. charm/test/benchmark/abenc_yllc15_bench.py +92 -0
  184. charm/test/benchmark/benchmark_test.py +148 -0
  185. charm/test/benchmark_threshold.py +260 -0
  186. charm/test/conftest.py +38 -0
  187. charm/test/fuzz/__init__.py +1 -0
  188. charm/test/fuzz/conftest.py +5 -0
  189. charm/test/fuzz/fuzz_policy_parser.py +76 -0
  190. charm/test/fuzz/fuzz_serialization.py +83 -0
  191. charm/test/schemes/__init__.py +0 -0
  192. charm/test/schemes/abenc/__init__.py +0 -0
  193. charm/test/schemes/abenc/abenc_bsw07_test.py +39 -0
  194. charm/test/schemes/abenc/abenc_dacmacs_yj14_test.py +16 -0
  195. charm/test/schemes/abenc/abenc_lsw08_test.py +33 -0
  196. charm/test/schemes/abenc/abenc_maabe_yj14_test.py +16 -0
  197. charm/test/schemes/abenc/abenc_tbpre_lww14_test.py +16 -0
  198. charm/test/schemes/abenc/abenc_waters09_test.py +38 -0
  199. charm/test/schemes/abenc/abenc_yllc15_test.py +74 -0
  200. charm/test/schemes/chamhash_adm05_test.py +31 -0
  201. charm/test/schemes/chamhash_rsa_hw09_test.py +29 -0
  202. charm/test/schemes/commit/__init__.py +0 -0
  203. charm/test/schemes/commit/commit_gs08_test.py +24 -0
  204. charm/test/schemes/commit/commit_pedersen92_test.py +26 -0
  205. charm/test/schemes/dabe_aw11_test.py +45 -0
  206. charm/test/schemes/encap_bchk05_test.py +21 -0
  207. charm/test/schemes/grpsig/__init__.py +0 -0
  208. charm/test/schemes/grpsig/groupsig_bgls04_test.py +35 -0
  209. charm/test/schemes/grpsig/groupsig_bgls04_var_test.py +39 -0
  210. charm/test/schemes/hibenc/__init__.py +0 -0
  211. charm/test/schemes/hibenc/hibenc_bb04_test.py +28 -0
  212. charm/test/schemes/ibenc/__init__.py +0 -0
  213. charm/test/schemes/ibenc/ibenc_bb03_test.py +26 -0
  214. charm/test/schemes/ibenc/ibenc_bf01_test.py +24 -0
  215. charm/test/schemes/ibenc/ibenc_ckrs09_test.py +25 -0
  216. charm/test/schemes/ibenc/ibenc_lsw08_test.py +31 -0
  217. charm/test/schemes/ibenc/ibenc_sw05_test.py +32 -0
  218. charm/test/schemes/ibenc/ibenc_waters05_test.py +31 -0
  219. charm/test/schemes/ibenc/ibenc_waters09_test.py +27 -0
  220. charm/test/schemes/pk_vrf_test.py +29 -0
  221. charm/test/schemes/pkenc/__init__.py +0 -0
  222. charm/test/schemes/pkenc_test.py +255 -0
  223. charm/test/schemes/pksig/__init__.py +0 -0
  224. charm/test/schemes/pksig_test.py +376 -0
  225. charm/test/schemes/rsa_alg_test.py +340 -0
  226. charm/test/schemes/threshold_test.py +1792 -0
  227. charm/test/serialize/__init__.py +0 -0
  228. charm/test/serialize/serialize_test.py +40 -0
  229. charm/test/toolbox/__init__.py +0 -0
  230. charm/test/toolbox/conversion_test.py +30 -0
  231. charm/test/toolbox/ecgroup_test.py +53 -0
  232. charm/test/toolbox/integer_arithmetic_test.py +441 -0
  233. charm/test/toolbox/paddingschemes_test.py +238 -0
  234. charm/test/toolbox/policy_parser_stress_test.py +969 -0
  235. charm/test/toolbox/secretshare_test.py +28 -0
  236. charm/test/toolbox/symcrypto_test.py +108 -0
  237. charm/test/toolbox/test_policy_expression.py +16 -0
  238. charm/test/vectors/__init__.py +1 -0
  239. charm/test/vectors/test_bls_vectors.py +289 -0
  240. charm/test/vectors/test_pedersen_vectors.py +315 -0
  241. charm/test/vectors/test_schnorr_vectors.py +368 -0
  242. charm/test/zkp_compiler/__init__.py +9 -0
  243. charm/test/zkp_compiler/benchmark_zkp.py +258 -0
  244. charm/test/zkp_compiler/test_and_proof.py +240 -0
  245. charm/test/zkp_compiler/test_batch_verify.py +248 -0
  246. charm/test/zkp_compiler/test_dleq_proof.py +264 -0
  247. charm/test/zkp_compiler/test_or_proof.py +231 -0
  248. charm/test/zkp_compiler/test_proof_serialization.py +121 -0
  249. charm/test/zkp_compiler/test_range_proof.py +241 -0
  250. charm/test/zkp_compiler/test_representation_proof.py +325 -0
  251. charm/test/zkp_compiler/test_schnorr_proof.py +221 -0
  252. charm/test/zkp_compiler/test_thread_safety.py +169 -0
  253. charm/test/zkp_compiler/test_zkp_parser.py +139 -0
  254. charm/toolbox/ABEnc.py +26 -0
  255. charm/toolbox/ABEncMultiAuth.py +66 -0
  256. charm/toolbox/ABEnumeric.py +800 -0
  257. charm/toolbox/Commit.py +24 -0
  258. charm/toolbox/DFA.py +89 -0
  259. charm/toolbox/FSA.py +1254 -0
  260. charm/toolbox/Hash.py +39 -0
  261. charm/toolbox/IBEnc.py +62 -0
  262. charm/toolbox/IBSig.py +64 -0
  263. charm/toolbox/PKEnc.py +66 -0
  264. charm/toolbox/PKSig.py +56 -0
  265. charm/toolbox/PREnc.py +32 -0
  266. charm/toolbox/ZKProof.py +289 -0
  267. charm/toolbox/__init__.py +0 -0
  268. charm/toolbox/bitstring.py +49 -0
  269. charm/toolbox/broadcast.py +220 -0
  270. charm/toolbox/conversion.py +100 -0
  271. charm/toolbox/eccurve.py +149 -0
  272. charm/toolbox/ecgroup.py +143 -0
  273. charm/toolbox/enum.py +60 -0
  274. charm/toolbox/hash_module.py +91 -0
  275. charm/toolbox/integergroup.py +323 -0
  276. charm/toolbox/iterate.py +22 -0
  277. charm/toolbox/matrixops.py +76 -0
  278. charm/toolbox/mpc_utils.py +296 -0
  279. charm/toolbox/msp.py +175 -0
  280. charm/toolbox/mta.py +985 -0
  281. charm/toolbox/node.py +120 -0
  282. charm/toolbox/ot/__init__.py +22 -0
  283. charm/toolbox/ot/base_ot.py +374 -0
  284. charm/toolbox/ot/dpf.py +642 -0
  285. charm/toolbox/ot/mpfss.py +228 -0
  286. charm/toolbox/ot/ot_extension.py +589 -0
  287. charm/toolbox/ot/silent_ot.py +378 -0
  288. charm/toolbox/paddingschemes.py +423 -0
  289. charm/toolbox/paddingschemes_test.py +238 -0
  290. charm/toolbox/pairingcurves.py +85 -0
  291. charm/toolbox/pairinggroup.py +186 -0
  292. charm/toolbox/policy_expression_spec.py +70 -0
  293. charm/toolbox/policytree.py +189 -0
  294. charm/toolbox/reCompiler.py +346 -0
  295. charm/toolbox/redundancyschemes.py +65 -0
  296. charm/toolbox/schemebase.py +188 -0
  297. charm/toolbox/secretshare.py +104 -0
  298. charm/toolbox/secretutil.py +174 -0
  299. charm/toolbox/securerandom.py +73 -0
  300. charm/toolbox/sigmaprotocol.py +46 -0
  301. charm/toolbox/specialprimes.py +45 -0
  302. charm/toolbox/symcrypto.py +279 -0
  303. charm/toolbox/threshold_sharing.py +553 -0
  304. charm/toolbox/xmlserialize.py +94 -0
  305. charm/toolbox/zknode.py +105 -0
  306. charm/zkp_compiler/__init__.py +89 -0
  307. charm/zkp_compiler/and_proof.py +460 -0
  308. charm/zkp_compiler/batch_verify.py +324 -0
  309. charm/zkp_compiler/dleq_proof.py +423 -0
  310. charm/zkp_compiler/or_proof.py +305 -0
  311. charm/zkp_compiler/range_proof.py +417 -0
  312. charm/zkp_compiler/representation_proof.py +466 -0
  313. charm/zkp_compiler/schnorr_proof.py +273 -0
  314. charm/zkp_compiler/thread_safe.py +150 -0
  315. charm/zkp_compiler/zk_demo.py +489 -0
  316. charm/zkp_compiler/zkp_factory.py +330 -0
  317. charm/zkp_compiler/zkp_generator.py +370 -0
  318. charm/zkp_compiler/zkparser.py +269 -0
  319. charm_crypto_framework-0.61.1.dist-info/METADATA +337 -0
  320. charm_crypto_framework-0.61.1.dist-info/RECORD +323 -0
  321. charm_crypto_framework-0.61.1.dist-info/WHEEL +5 -0
  322. charm_crypto_framework-0.61.1.dist-info/licenses/LICENSE.txt +165 -0
  323. charm_crypto_framework-0.61.1.dist-info/top_level.txt +1 -0
@@ -0,0 +1,217 @@
1
+ /*
2
+ * Charm-Crypto is a framework for rapidly prototyping cryptosystems.
3
+ *
4
+ * Charm-Crypto is free software; you can redistribute it and/or
5
+ * modify it under the terms of the GNU Lesser General Public
6
+ * License as published by the Free Software Foundation; either
7
+ * version 2.1 of the License, or (at your option) any later version.
8
+ *
9
+ * Charm-Crypto is distributed in the hope that it will be useful,
10
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
+ * Lesser General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU Lesser General Public License
15
+ * along with Charm-Crypto. If not, see <http://www.gnu.org/licenses/>.
16
+ *
17
+ * Please contact the charm-crypto dev team at support@charm-crypto.com
18
+ * for any questions.
19
+ */
20
+
21
+ /*
22
+ * @file relic_interface.h
23
+ *
24
+ * @brief charm interface over RELIC's pairing-based crypto module
25
+ *
26
+ * @author jakinye3@jhu.edu
27
+ *
28
+ ************************************************************************/
29
+
30
+ #ifndef RELIC_INTERFACE_H
31
+ #define RELIC_INTERFACE_H
32
+
33
+ #include <stdlib.h>
34
+ #include <math.h>
35
+ #include "relic.h"
36
+ /* make sure error checking enabled in relic_conf.h, ALLOC should be dynamic */
37
+
38
+ //#define DISABLE_CHECK 1
39
+ #define TRUE 1
40
+ #define FALSE 0
41
+ #define DBASE 16
42
+ #define MAX_BUF 1024
43
+ #define SHA_LEN 32
44
+ #define SHA_FUNC md_map_sh256
45
+ typedef enum _status_t { ELEMENT_OK = 2,
46
+ ELEMENT_INVALID_ARG,
47
+ ELEMENT_INVALID_ARG_LEN,
48
+ ELEMENT_INVALID_TYPES,
49
+ ELEMENT_INVALID_RESULT,
50
+ ELEMENT_PAIRING_INIT_FAILED,
51
+ ELEMENT_UNINITIALIZED,
52
+ ELEMENT_DIV_ZERO,
53
+ ELEMENT_INITIALIZED_ALRDY,
54
+ } status_t;
55
+
56
+ enum Group {ZR, G1, G2, GT, NONE_G};
57
+ typedef enum Group GroupType;
58
+
59
+ //new macros to fix Relic renaming
60
+ #define FP_BYTES RLC_FP_BYTES
61
+ #define fp_write fp_write_str
62
+ #define BN_BYTES (RLC_BN_DIGS * sizeof(dig_t))
63
+ #define BN_NEG RLC_NEG
64
+ #define BN_POS RLC_POS
65
+ #define STS_OK RLC_OK
66
+ #define G1_TABLE RLC_G1_TABLE
67
+ #define G2_TABLE RLC_G2_TABLE
68
+ #define CMP_GT RLC_GT
69
+ #define CMP_EQ RLC_EQ
70
+ //end new macros
71
+
72
+ #define FP_STR FP_BYTES * 2 + 1
73
+ #define G1_LEN (FP_BYTES * 2) + 2
74
+ #define G2_LEN (FP_BYTES * 4) + 4
75
+ #define GT_LEN (FP_BYTES * 12) + 12
76
+
77
+ struct element {
78
+ int isInitialized;
79
+ bn_t order;
80
+ GroupType type;
81
+ bn_t bn;
82
+ g1_t g1;
83
+ g2_t g2;
84
+ gt_t gt;
85
+ };
86
+
87
+ struct object {
88
+ int isInitialized;
89
+ g1_t *t1;
90
+ g2_t *t2;
91
+ };
92
+
93
+ typedef struct element element_t[1];
94
+ typedef struct element *element_ptr;
95
+ typedef struct object element_pp_t[1];
96
+ typedef bn_t integer_t;
97
+ int bn_is_one(bn_t a);
98
+
99
+ /* Initialize 'e' to be an element of the ring Z_r of pairing.
100
+ * r is the order of the groups G1, G2 and GT that are involved in the pairing.
101
+ */
102
+ status_t pairing_init(void); // must be able to set curve parameters dynamically
103
+ status_t pairing_clear(void);
104
+
105
+ status_t element_init_Zr(element_t e, int init_value);
106
+ /* Initialize 'e' to be an element of the group G1, G2 or GT of pairing. */
107
+ status_t element_init_G1(element_t e);
108
+ status_t element_init_G2(element_t e);
109
+ status_t element_init_GT(element_t e);
110
+ /* selects random element from a uniform distribution */
111
+ status_t element_random(element_t e);
112
+ /* print contents of ane element structure */
113
+ status_t element_printf(const char *msg, element_t e);
114
+ status_t element_to_str(char *data, int len, element_t e);
115
+ /* free mem. associated with a */
116
+ status_t element_clear(element_t e);
117
+
118
+ status_t element_pp_init(element_pp_t e_pp, element_t e);
119
+ status_t element_pp_pow(element_t o, element_pp_t e_pp, GroupType type, element_t e);
120
+ status_t element_pp_pow_int(element_t o, element_pp_t e_pp, GroupType type, integer_t bn);
121
+ status_t element_pp_clear(element_pp_t e_pp, GroupType type);
122
+
123
+ /* arithmetic operators over elements */
124
+ // c = a + b
125
+ status_t element_add(element_t c, element_t a, element_t b);
126
+ // c = a - b
127
+ status_t element_sub(element_t c, element_t a, element_t b);
128
+ // c = a * b
129
+ status_t element_mul(element_t c, element_t a, element_t b);
130
+ // c = a * Zr
131
+ status_t element_mul_zr(element_t c, element_t a, element_t b);
132
+ // c = a * int
133
+ status_t element_mul_int(element_t c, element_t a, integer_t b);
134
+ // c = a / b
135
+ status_t element_div(element_t c, element_t a, element_t b);
136
+ status_t element_div_int(element_t c, element_t a, integer_t b);
137
+ status_t element_int_div(element_t c, integer_t a, element_t b);
138
+
139
+ // c = -a
140
+ status_t element_neg(element_t c, element_t a);
141
+ // c = 1 / a
142
+ status_t element_invert(element_t c, element_t a);
143
+ // c = a ^ b ( where b is ZR)
144
+ status_t element_pow_zr(element_t c, element_t a, element_t b);
145
+ // c = a ^ b ( where b is int)
146
+ status_t element_pow_int(element_t c, element_t a, integer_t b);
147
+ // compare a & b returns 0 if a==b, -1 if a < b and 1 if a > b
148
+ int element_cmp(element_t a, element_t b);
149
+ // check if element is zero
150
+ int element_is_member(element_t e);
151
+ int get_order(integer_t x);
152
+
153
+ status_t pairing_apply(element_t et, element_t e1, element_t e2);
154
+
155
+ // adjustable key derivation function
156
+ status_t element_to_key(element_t e, uint8_t *data, int data_len, uint8_t label);
157
+ status_t hash_buffer_to_bytes(uint8_t *input, int input_len, uint8_t *output, int output_len, uint8_t label);
158
+
159
+ /* copy method: e = a */
160
+ status_t element_set(element_t e, element_t a);
161
+ status_t element_set_int(element_t e, integer_t x);
162
+ status_t element_to_int(integer_t x, element_t e);
163
+ status_t element_set_si(element_t e, unsigned int x);
164
+ // void element_init_same_as(element_t e, element_t e2)
165
+ int element_length(element_t e);
166
+ /* generate an element of a particular type from data */
167
+ status_t element_from_hash(element_t e, unsigned char *data, int len);
168
+ /* serialize to bytes */
169
+ status_t element_to_bytes(unsigned char *data, int data_len, element_t e);
170
+ /* de-serialize from bytes */
171
+ status_t element_from_bytes(element_t e, unsigned char *data, int data_len);
172
+
173
+ void print_as_hex(uint8_t *data, size_t len);
174
+ status_t charm_g1_read_bin(g1_t g, uint8_t *data, int data_len);
175
+ status_t charm_g1_write_bin(g1_t g, uint8_t *data, int data_len);
176
+ status_t charm_g1_write_str(g1_t g, uint8_t *data, int data_len);
177
+
178
+ status_t charm_g2_read_bin(g2_t g, uint8_t *data, int data_len);
179
+ status_t charm_g2_write_bin(g2_t g, uint8_t *data, int data_len);
180
+ status_t charm_g2_write_str(g2_t g, uint8_t *data, int data_len);
181
+
182
+ status_t charm_gt_read_bin(gt_t g, uint8_t *data, int data_len);
183
+ status_t charm_gt_write_bin(gt_t g, uint8_t *data, int data_len);
184
+ status_t charm_gt_write_str(gt_t g, uint8_t *data, int data_len);
185
+
186
+ #define bn_inits(b) \
187
+ bn_null(b); \
188
+ bn_new(b);
189
+
190
+ #define g1_inits(g) \
191
+ g1_null(g); \
192
+ g1_new(g);
193
+
194
+ #define g2_inits(g) \
195
+ g2_null(g); \
196
+ g2_new(g);
197
+
198
+ #define gt_inits(g) \
199
+ gt_null(g); \
200
+ gt_new(g);
201
+
202
+ #ifdef DISABLE_CHECK
203
+ #define LEAVE_IF(a, m) /* empty */
204
+ #define EXIT_IF_NOT_SAME(a, b) /* empty */
205
+ #else
206
+ #define EXIT_IF_NOT_SAME(a, b) \
207
+ if(a->type != b->type) { \
208
+ return ELEMENT_INVALID_TYPES; }
209
+
210
+ #define LEAVE_IF(a, m) \
211
+ if(a) { \
212
+ fprintf(stderr, "%s", m); \
213
+ return ELEMENT_INVALID_ARG; }
214
+
215
+ #endif
216
+
217
+ #endif
@@ -0,0 +1,171 @@
1
+ #include <stdio.h>
2
+ #include <sys/time.h>
3
+ #include "relic_interface.h"
4
+
5
+ /* prime curves */
6
+ #define DEBUG 1
7
+ #define TRIALS 1
8
+ #define HEX 16
9
+
10
+ //void printf_as_hex(unsigned char *buf, size_t len)
11
+ //{
12
+ // size_t i;
13
+ // for(i = 0; i < len; i += 4)
14
+ // printf("%02X%02X%02X%02X ", buf[i], buf[i+1], buf[i+2], buf[i+3]);
15
+ // printf("\n");
16
+ //}
17
+
18
+ //double calc_usecs(bench_t *start, bench_t *stop) {
19
+ // double usec_per_second = 1000000; // 1 000 000
20
+ // double result = usec_per_second * (stop->tv_sec - start->tv_sec);
21
+ //
22
+ // if(stop->tv_usec >= stop->tv_usec) {
23
+ // result += (stop->tv_usec - start->tv_usec);
24
+ // }
25
+ // else {
26
+ // result -= (start->tv_usec - stop->tv_usec);
27
+ // }
28
+ //
29
+ // return result / usec_per_second;
30
+ //}
31
+
32
+ /*
33
+ double calc_msecs(bench_t *start, bench_t *stop) {
34
+ double msec_per_second = 1000; // 1 000 000
35
+ double usec_per_second = 1000000; // 1 000 000
36
+ double result1 = start->tv_sec + (start->tv_usec / usec_per_second);
37
+ double result2 = stop->tv_sec + (stop->tv_usec / usec_per_second);
38
+ return (result2 - result1) * msec_per_second;
39
+ }
40
+ */
41
+
42
+ int main(int argc, char *argv[])
43
+ {
44
+ status_t result;
45
+ result = pairing_init();
46
+
47
+ if(result == ELEMENT_OK)
48
+ printf("pairing init: SUCCESS.\n");
49
+ else
50
+ printf("pairing init: FAILED!\n");
51
+
52
+
53
+ element_ptr e0 = (element_ptr) malloc(sizeof(element_t));
54
+ element_t e; // , e1, e2, e3;
55
+
56
+ /* Start ZR init & operations */
57
+ element_init_Zr(e, 0);
58
+ element_init_Zr(e0, 1234567890);
59
+
60
+ element_random(e);
61
+
62
+ element_printf("e -> ZR :=> ", e);
63
+ element_printf("e0 -> ZR :=> ", e0);
64
+
65
+ element_add(e, e, e0);
66
+ element_printf("Add ZR :=> ", e);
67
+ element_sub(e0, e, e0);
68
+ element_printf("Sub ZR :=> ", e);
69
+
70
+ element_clear(e);
71
+ element_clear(e0);
72
+
73
+ /* End of ZR operations */
74
+
75
+
76
+ element_t g1_0, g1_1, g1_2, g2_1, g2_2, g2_nil, gt_1, gt_2;
77
+ /* Start G1 init & operations */
78
+ element_init_G1(g1_0);
79
+ element_printf("Identity G1 :=> \n", g1_0);
80
+ element_init_G1(g1_1);
81
+ element_init_G1(g1_2);
82
+ element_init_G2(g2_1);
83
+ element_init_G2(g2_2);
84
+ element_init_G2(g2_nil);
85
+ element_init_GT(gt_1);
86
+ element_init_GT(gt_2);
87
+ element_init_Zr(e0, 0);
88
+
89
+ // element_random(g1_0);
90
+ // element_random(g1_1);
91
+ //
92
+ // element_add(g1_2, g1_0, g1_1);
93
+ // element_printf("Add G1 :=> \n", g1_2);
94
+ //
95
+ // element_sub(g1_2, g1_2, g1_0);
96
+ // element_printf("Sub G1 :=> \n", g1_2);
97
+ //
98
+ //
99
+ unsigned char *msg = "hello world!";
100
+ element_from_hash(e0, msg, strlen((char *) msg));
101
+ element_from_hash(g1_2, msg, strlen((char *) msg));
102
+ element_from_hash(g2_2, msg, strlen((char *) msg));
103
+
104
+ element_printf("Hash into e0 :=> \n", e0);
105
+ element_printf("Hash into g1 :=> \n", g1_2);
106
+ element_printf("Hash into g2 :=> \n", g2_2);
107
+
108
+ printf("cmp elements ok! :=> '%d'\n", element_cmp(g1_2, g1_2));
109
+ printf("cmp elements fail:=> '%d'\n", element_cmp(g1_1, g1_2));
110
+
111
+ int d_len = element_length(g1_1);
112
+ if(d_len > 0) {
113
+ printf("%s: g1 d_len :=> '%d'\n", __FUNCTION__, d_len);
114
+ uint8_t data[d_len + 1];
115
+ element_to_bytes(data, d_len, g1_1);
116
+
117
+ element_from_bytes(g1_2, data, d_len);
118
+ element_printf("rec g1 :=> \n", g1_2);
119
+ printf("cmp elements after deserialize :=> '%d'\n", element_cmp(g1_1, g1_2));
120
+ }
121
+
122
+ d_len = element_length(g2_2);
123
+ if(d_len > 0) {
124
+ element_printf("g2 write bin :=> \n", g2_2);
125
+ printf("%s: g2 d_len :=> '%d'\n", __FUNCTION__, d_len);
126
+ uint8_t data[d_len + 1];
127
+ element_to_bytes(data, d_len, g2_2);
128
+
129
+ element_from_bytes(g2_1, data, d_len);
130
+ element_printf("Rec g2 :=> \n", g2_1);
131
+ printf("cmp elements after deserialize :=> '%d'\n", element_cmp(g2_1, g2_2));
132
+
133
+ }
134
+
135
+ element_random(gt_1);
136
+ d_len = element_length(gt_1);
137
+ if(d_len > 0) {
138
+ element_printf("gt print :=> \n", gt_1);
139
+ printf("%s: g2 d_len :=> '%d'\n", __FUNCTION__, d_len);
140
+ uint8_t data[d_len + 1];
141
+ element_to_bytes(data, d_len, gt_1);
142
+
143
+ element_from_bytes(gt_2, data, d_len);
144
+
145
+ element_printf("Rec gt :=> \n", gt_2);
146
+ printf("cmp elements after deserialize :=> '%d'\n", element_cmp(gt_1, gt_2));
147
+
148
+ }
149
+
150
+ element_pairing(gt_1, g1_1, g2_1);
151
+ element_printf("Pairing result :=> \n", gt_1);
152
+
153
+ element_printf("g2 :=> \n", g2_nil);
154
+ element_pairing(gt_2, g1_1, g2_nil);
155
+ element_printf("Pairing result :=> \n", gt_2);
156
+
157
+ element_clear(e0);
158
+ element_clear(g1_0);
159
+ element_clear(g1_1);
160
+ element_clear(g1_2);
161
+ element_clear(g2_1);
162
+ element_clear(g2_2);
163
+ element_clear(g2_nil);
164
+ element_clear(gt_1);
165
+ element_clear(gt_2);
166
+ /* End of G1 operations */
167
+
168
+ pairing_clear();
169
+ return 0;
170
+ }
171
+
@@ -0,0 +1,69 @@
1
+ """Type stubs for charm.core.math.pairing C extension module."""
2
+
3
+ from typing import overload
4
+
5
+ # Module-level constants (group types)
6
+ ZR: int
7
+ G1: int
8
+ G2: int
9
+ GT: int
10
+
11
+ class Pairing:
12
+ """Pairing group context for bilinear pairings."""
13
+
14
+ @overload
15
+ def __init__(self) -> None: ...
16
+ @overload
17
+ def __init__(self, params: str) -> None: ...
18
+ @overload
19
+ def __init__(self, params: bytes) -> None: ...
20
+
21
+ class Element:
22
+ """Element in a pairing-based group (ZR, G1, G2, or GT)."""
23
+
24
+ type: int
25
+ initialized: int
26
+ preproc: int
27
+
28
+ def __init__(self) -> None: ...
29
+ def initPP(self) -> None: ...
30
+ def set(self, value: int | Element) -> int: ...
31
+
32
+ # Arithmetic operations
33
+ def __add__(self, other: Element) -> Element: ...
34
+ def __radd__(self, other: Element) -> Element: ...
35
+ def __sub__(self, other: Element) -> Element: ...
36
+ def __rsub__(self, other: Element) -> Element: ...
37
+ def __mul__(self, other: Element) -> Element: ...
38
+ def __rmul__(self, other: Element) -> Element: ...
39
+ def __pow__(self, other: Element | int) -> Element: ...
40
+ def __rpow__(self, other: Element | int) -> Element: ...
41
+ def __neg__(self) -> Element: ...
42
+ def __invert__(self) -> Element: ...
43
+
44
+ # Comparison operations
45
+ def __eq__(self, other: object) -> bool: ...
46
+ def __ne__(self, other: object) -> bool: ...
47
+ def __lt__(self, other: Element) -> bool: ...
48
+ def __le__(self, other: Element) -> bool: ...
49
+ def __gt__(self, other: Element) -> bool: ...
50
+ def __ge__(self, other: Element) -> bool: ...
51
+
52
+ # Conversion and hashing
53
+ def __int__(self) -> int: ...
54
+ def __hash__(self) -> int: ...
55
+
56
+ # Module-level functions
57
+ @overload
58
+ def init(group: Pairing, type: int) -> Element: ...
59
+ @overload
60
+ def init(group: Pairing, type: int, value: int) -> Element: ...
61
+
62
+ def pair(g1: Element, g2: Element) -> Element: ...
63
+ def hashPair(element: Element) -> bytes: ...
64
+ def H(group: Pairing, data: bytes | str, type: int) -> Element: ...
65
+ def random(group: Pairing, type: int) -> Element: ...
66
+ def serialize(element: Element) -> bytes: ...
67
+ def deserialize(group: Pairing, data: bytes, type: int) -> Element: ...
68
+ def ismember(element: Element) -> bool: ...
69
+
@@ -0,0 +1,248 @@
1
+ #include "base64.h"
2
+
3
+ //
4
+ // NSData+Base64.m
5
+ // base64
6
+ //
7
+ // Created by Matt Gallagher on 2009/06/03.
8
+ // Copyright 2009 Matt Gallagher. All rights reserved.
9
+ //
10
+ // Permission is given to use this source code file, free of charge, in any
11
+ // project, commercial or otherwise, entirely at your risk, with the condition
12
+ // that any redistribution (in part or whole) of source code must retain
13
+ // this copyright and permission notice. Attribution in compiled projects is
14
+ // appreciated but not required.
15
+
16
+ //
17
+ // Mapping from 6 bit pattern to ASCII character.
18
+ //
19
+ static unsigned char base64EncodeLookup[65] =
20
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
21
+
22
+ //
23
+ // Definition for "masked-out" areas of the base64DecodeLookup mapping
24
+ //
25
+ #define xx 65
26
+
27
+ //
28
+ // Mapping from ASCII character to 6 bit pattern.
29
+ //
30
+ static unsigned char base64DecodeLookup[256] =
31
+ {
32
+ xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx,
33
+ xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx,
34
+ xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, 62, xx, xx, xx, 63,
35
+ 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, xx, xx, xx, xx, xx, xx,
36
+ xx, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
37
+ 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, xx, xx, xx, xx, xx,
38
+ xx, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
39
+ 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, xx, xx, xx, xx, xx,
40
+ xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx,
41
+ xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx,
42
+ xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx,
43
+ xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx,
44
+ xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx,
45
+ xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx,
46
+ xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx,
47
+ xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx,
48
+ };
49
+
50
+ //
51
+ // Fundamental sizes of the binary and base64 encode/decode units in bytes
52
+ //
53
+ #define BINARY_UNIT_SIZE 3
54
+ #define BASE64_UNIT_SIZE 4
55
+
56
+ //
57
+ // NewBase64Decode
58
+ //
59
+ // Decodes the base64 ASCII string in the inputBuffer to a newly malloced
60
+ // output buffer.
61
+ //
62
+ // inputBuffer - the source ASCII string for the decode
63
+ // length - the length of the string or -1 (to specify strlen should be used)
64
+ // outputLength - if not-NULL, on output will contain the decoded length
65
+ //
66
+ // returns the decoded buffer. Must be free'd by caller. Length is given by
67
+ // outputLength.
68
+ //
69
+ void *NewBase64Decode(
70
+ const char *inputBuffer,
71
+ size_t length,
72
+ size_t *outputLength)
73
+ {
74
+ if ((int) length == -1)
75
+ {
76
+ length = strlen(inputBuffer);
77
+ }
78
+
79
+ size_t outputBufferSize =
80
+ ((length+BASE64_UNIT_SIZE-1) / BASE64_UNIT_SIZE) * BINARY_UNIT_SIZE;
81
+ unsigned char *outputBuffer = (unsigned char *)malloc(outputBufferSize);
82
+
83
+ size_t i = 0;
84
+ size_t j = 0;
85
+ while (i < length)
86
+ {
87
+ //
88
+ // Accumulate 4 valid characters (ignore everything else)
89
+ //
90
+ unsigned char accumulated[BASE64_UNIT_SIZE];
91
+ size_t accumulateIndex = 0;
92
+ while (i < length)
93
+ {
94
+ unsigned char decode = base64DecodeLookup[(unsigned char)inputBuffer[i++]];
95
+ if (decode != xx)
96
+ {
97
+ accumulated[accumulateIndex] = decode;
98
+ accumulateIndex++;
99
+
100
+ if (accumulateIndex == BASE64_UNIT_SIZE)
101
+ {
102
+ break;
103
+ }
104
+ }
105
+ }
106
+
107
+ //
108
+ // Store the 6 bits from each of the 4 characters as 3 bytes
109
+ //
110
+ outputBuffer[j] = (accumulated[0] << 2) | (accumulated[1] >> 4);
111
+ outputBuffer[j + 1] = (accumulated[1] << 4) | (accumulated[2] >> 2);
112
+ outputBuffer[j + 2] = (accumulated[2] << 6) | accumulated[3];
113
+ j += accumulateIndex - 1;
114
+ }
115
+
116
+ if (outputLength)
117
+ {
118
+ *outputLength = j;
119
+ }
120
+ return outputBuffer;
121
+ }
122
+
123
+ //
124
+ // NewBase64Decode
125
+ //
126
+ // Encodes the arbitrary data in the inputBuffer as base64 into a newly malloced
127
+ // output buffer.
128
+ //
129
+ // inputBuffer - the source data for the encode
130
+ // length - the length of the input in bytes
131
+ // separateLines - if zero, no CR/LF characters will be added. Otherwise
132
+ // a CR/LF pair will be added every 64 encoded chars.
133
+ // outputLength - if not-NULL, on output will contain the encoded length
134
+ // (not including terminating 0 char)
135
+ //
136
+ // returns the encoded buffer. Must be free'd by caller. Length is given by
137
+ // outputLength.
138
+ //
139
+ char *NewBase64Encode(
140
+ const void *buffer,
141
+ size_t length,
142
+ int separateLines,
143
+ size_t *outputLength)
144
+ {
145
+ const unsigned char *inputBuffer = (const unsigned char *)buffer;
146
+
147
+ #define MAX_NUM_PADDING_CHARS 2
148
+ #define OUTPUT_LINE_LENGTH 64
149
+ #define INPUT_LINE_LENGTH ((OUTPUT_LINE_LENGTH / BASE64_UNIT_SIZE) * BINARY_UNIT_SIZE)
150
+ #define CR_LF_SIZE 2
151
+
152
+ //
153
+ // Byte accurate calculation of final buffer size
154
+ //
155
+ size_t outputBufferSize =
156
+ ((length / BINARY_UNIT_SIZE)
157
+ + ((length % BINARY_UNIT_SIZE) ? 1 : 0))
158
+ * BASE64_UNIT_SIZE;
159
+ if (separateLines)
160
+ {
161
+ outputBufferSize +=
162
+ (outputBufferSize / OUTPUT_LINE_LENGTH) * CR_LF_SIZE;
163
+ }
164
+
165
+ //
166
+ // Include space for a terminating zero
167
+ //
168
+ outputBufferSize += 1;
169
+
170
+ //
171
+ // Allocate the output buffer
172
+ //
173
+ char *outputBuffer = (char *)malloc(outputBufferSize);
174
+ if (!outputBuffer)
175
+ {
176
+ return NULL;
177
+ }
178
+
179
+ size_t i = 0;
180
+ size_t j = 0;
181
+ const size_t lineLength = separateLines ? INPUT_LINE_LENGTH : length;
182
+ size_t lineEnd = lineLength;
183
+
184
+ while (TRUE)
185
+ {
186
+ if (lineEnd > length)
187
+ {
188
+ lineEnd = length;
189
+ }
190
+
191
+ for (; i + BINARY_UNIT_SIZE - 1 < lineEnd; i += BINARY_UNIT_SIZE)
192
+ {
193
+ //
194
+ // Inner loop: turn 48 bytes into 64 base64 characters
195
+ //
196
+ outputBuffer[j++] = base64EncodeLookup[(inputBuffer[i] & 0xFC) >> 2];
197
+ outputBuffer[j++] = base64EncodeLookup[((inputBuffer[i] & 0x03) << 4)
198
+ | ((inputBuffer[i + 1] & 0xF0) >> 4)];
199
+ outputBuffer[j++] = base64EncodeLookup[((inputBuffer[i + 1] & 0x0F) << 2)
200
+ | ((inputBuffer[i + 2] & 0xC0) >> 6)];
201
+ outputBuffer[j++] = base64EncodeLookup[inputBuffer[i + 2] & 0x3F];
202
+ }
203
+
204
+ if (lineEnd == length)
205
+ {
206
+ break;
207
+ }
208
+
209
+ //
210
+ // Add the newline
211
+ //
212
+ outputBuffer[j++] = '\r';
213
+ outputBuffer[j++] = '\n';
214
+ lineEnd += lineLength;
215
+ }
216
+
217
+ if (i + 1 < length)
218
+ {
219
+ //
220
+ // Handle the single '=' case
221
+ //
222
+ outputBuffer[j++] = base64EncodeLookup[(inputBuffer[i] & 0xFC) >> 2];
223
+ outputBuffer[j++] = base64EncodeLookup[((inputBuffer[i] & 0x03) << 4)
224
+ | ((inputBuffer[i + 1] & 0xF0) >> 4)];
225
+ outputBuffer[j++] = base64EncodeLookup[(inputBuffer[i + 1] & 0x0F) << 2];
226
+ outputBuffer[j++] = '=';
227
+ }
228
+ else if (i < length)
229
+ {
230
+ //
231
+ // Handle the double '=' case
232
+ //
233
+ outputBuffer[j++] = base64EncodeLookup[(inputBuffer[i] & 0xFC) >> 2];
234
+ outputBuffer[j++] = base64EncodeLookup[(inputBuffer[i] & 0x03) << 4];
235
+ outputBuffer[j++] = '=';
236
+ outputBuffer[j++] = '=';
237
+ }
238
+ outputBuffer[j] = 0;
239
+
240
+ //
241
+ // Set the output length and return the buffer
242
+ //
243
+ if (outputLength)
244
+ {
245
+ *outputLength = j;
246
+ }
247
+ return outputBuffer;
248
+ }