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.
- charm/__init__.py +5 -0
- charm/adapters/__init__.py +0 -0
- charm/adapters/abenc_adapt_hybrid.py +90 -0
- charm/adapters/dabenc_adapt_hybrid.py +145 -0
- charm/adapters/ibenc_adapt_hybrid.py +72 -0
- charm/adapters/ibenc_adapt_identityhash.py +80 -0
- charm/adapters/kpabenc_adapt_hybrid.py +91 -0
- charm/adapters/pkenc_adapt_bchk05.py +121 -0
- charm/adapters/pkenc_adapt_chk04.py +91 -0
- charm/adapters/pkenc_adapt_hybrid.py +98 -0
- charm/adapters/pksig_adapt_naor01.py +89 -0
- charm/config.py +7 -0
- charm/core/__init__.py +0 -0
- charm/core/benchmark/benchmark_util.c +353 -0
- charm/core/benchmark/benchmark_util.h +61 -0
- charm/core/benchmark/benchmarkmodule.c +476 -0
- charm/core/benchmark/benchmarkmodule.h +162 -0
- charm/core/benchmark.cpython-313-darwin.so +0 -0
- charm/core/crypto/AES/AES.c +1464 -0
- charm/core/crypto/AES.cpython-313-darwin.so +0 -0
- charm/core/crypto/DES/DES.c +113 -0
- charm/core/crypto/DES.cpython-313-darwin.so +0 -0
- charm/core/crypto/DES3/DES3.c +26 -0
- charm/core/crypto/DES3.cpython-313-darwin.so +0 -0
- charm/core/crypto/__init__.py +0 -0
- charm/core/crypto/cryptobase/XOR.c +80 -0
- charm/core/crypto/cryptobase/_counter.c +496 -0
- charm/core/crypto/cryptobase/_counter.h +54 -0
- charm/core/crypto/cryptobase/block_template.c +900 -0
- charm/core/crypto/cryptobase/block_template.h +69 -0
- charm/core/crypto/cryptobase/cryptobasemodule.c +220 -0
- charm/core/crypto/cryptobase/libtom/tomcrypt.h +90 -0
- charm/core/crypto/cryptobase/libtom/tomcrypt_argchk.h +44 -0
- charm/core/crypto/cryptobase/libtom/tomcrypt_cfg.h +186 -0
- charm/core/crypto/cryptobase/libtom/tomcrypt_cipher.h +941 -0
- charm/core/crypto/cryptobase/libtom/tomcrypt_custom.h +556 -0
- charm/core/crypto/cryptobase/libtom/tomcrypt_des.c +1912 -0
- charm/core/crypto/cryptobase/libtom/tomcrypt_hash.h +407 -0
- charm/core/crypto/cryptobase/libtom/tomcrypt_mac.h +496 -0
- charm/core/crypto/cryptobase/libtom/tomcrypt_macros.h +435 -0
- charm/core/crypto/cryptobase/libtom/tomcrypt_math.h +534 -0
- charm/core/crypto/cryptobase/libtom/tomcrypt_misc.h +103 -0
- charm/core/crypto/cryptobase/libtom/tomcrypt_pk.h +653 -0
- charm/core/crypto/cryptobase/libtom/tomcrypt_pkcs.h +90 -0
- charm/core/crypto/cryptobase/libtom/tomcrypt_prng.h +199 -0
- charm/core/crypto/cryptobase/stream_template.c +271 -0
- charm/core/crypto/cryptobase/strxor.c +229 -0
- charm/core/crypto/cryptobase.cpython-313-darwin.so +0 -0
- charm/core/engine/__init__.py +5 -0
- charm/core/engine/protocol.py +293 -0
- charm/core/engine/util.py +174 -0
- charm/core/math/__init__.py +0 -0
- charm/core/math/elliptic_curve/ecmodule.c +1986 -0
- charm/core/math/elliptic_curve/ecmodule.h +230 -0
- charm/core/math/elliptic_curve.cpython-313-darwin.so +0 -0
- charm/core/math/elliptic_curve.pyi +63 -0
- charm/core/math/integer/integermodule.c +2539 -0
- charm/core/math/integer/integermodule.h +145 -0
- charm/core/math/integer.cpython-313-darwin.so +0 -0
- charm/core/math/integer.pyi +76 -0
- charm/core/math/pairing/miracl/miracl_config.h +37 -0
- charm/core/math/pairing/miracl/miracl_interface.h +118 -0
- charm/core/math/pairing/miracl/miracl_interface2.h +126 -0
- charm/core/math/pairing/miracl/pairingmodule2.c +2094 -0
- charm/core/math/pairing/miracl/pairingmodule2.h +307 -0
- charm/core/math/pairing/pairingmodule.c +2230 -0
- charm/core/math/pairing/pairingmodule.h +241 -0
- charm/core/math/pairing/relic/pairingmodule3.c +1853 -0
- charm/core/math/pairing/relic/pairingmodule3.h +233 -0
- charm/core/math/pairing/relic/relic_interface.c +1337 -0
- charm/core/math/pairing/relic/relic_interface.h +217 -0
- charm/core/math/pairing/relic/test_relic.c +171 -0
- charm/core/math/pairing.cpython-313-darwin.so +0 -0
- charm/core/math/pairing.pyi +69 -0
- charm/core/utilities/base64.c +248 -0
- charm/core/utilities/base64.h +15 -0
- charm/schemes/__init__.py +0 -0
- charm/schemes/abenc/__init__.py +0 -0
- charm/schemes/abenc/abenc_accountability_jyjxgd20.py +647 -0
- charm/schemes/abenc/abenc_bsw07.py +146 -0
- charm/schemes/abenc/abenc_ca_cpabe_ar17.py +684 -0
- charm/schemes/abenc/abenc_dacmacs_yj14.py +298 -0
- charm/schemes/abenc/abenc_lsw08.py +159 -0
- charm/schemes/abenc/abenc_maabe_rw15.py +236 -0
- charm/schemes/abenc/abenc_maabe_yj14.py +297 -0
- charm/schemes/abenc/abenc_tbpre_lww14.py +309 -0
- charm/schemes/abenc/abenc_unmcpabe_yahk14.py +223 -0
- charm/schemes/abenc/abenc_waters09.py +144 -0
- charm/schemes/abenc/abenc_yct14.py +208 -0
- charm/schemes/abenc/abenc_yllc15.py +178 -0
- charm/schemes/abenc/ac17.py +248 -0
- charm/schemes/abenc/bsw07.py +141 -0
- charm/schemes/abenc/cgw15.py +277 -0
- charm/schemes/abenc/dabe_aw11.py +204 -0
- charm/schemes/abenc/dfa_fe12.py +144 -0
- charm/schemes/abenc/pk_hve08.py +179 -0
- charm/schemes/abenc/waters11.py +143 -0
- charm/schemes/aggrsign_MuSig.py +150 -0
- charm/schemes/aggrsign_bls.py +267 -0
- charm/schemes/blindsig_ps16.py +654 -0
- charm/schemes/chamhash_adm05.py +113 -0
- charm/schemes/chamhash_rsa_hw09.py +100 -0
- charm/schemes/commit/__init__.py +0 -0
- charm/schemes/commit/commit_gs08.py +77 -0
- charm/schemes/commit/commit_pedersen92.py +53 -0
- charm/schemes/encap_bchk05.py +62 -0
- charm/schemes/grpsig/__init__.py +0 -0
- charm/schemes/grpsig/groupsig_bgls04.py +114 -0
- charm/schemes/grpsig/groupsig_bgls04_var.py +115 -0
- charm/schemes/hibenc/__init__.py +0 -0
- charm/schemes/hibenc/hibenc_bb04.py +105 -0
- charm/schemes/hibenc/hibenc_lew11.py +193 -0
- charm/schemes/ibenc/__init__.py +0 -0
- charm/schemes/ibenc/clpkc_rp03.py +119 -0
- charm/schemes/ibenc/ibenc_CW13_z.py +168 -0
- charm/schemes/ibenc/ibenc_bb03.py +94 -0
- charm/schemes/ibenc/ibenc_bf01.py +121 -0
- charm/schemes/ibenc/ibenc_ckrs09.py +120 -0
- charm/schemes/ibenc/ibenc_cllww12_z.py +172 -0
- charm/schemes/ibenc/ibenc_lsw08.py +120 -0
- charm/schemes/ibenc/ibenc_sw05.py +238 -0
- charm/schemes/ibenc/ibenc_waters05.py +144 -0
- charm/schemes/ibenc/ibenc_waters05_z.py +164 -0
- charm/schemes/ibenc/ibenc_waters09.py +107 -0
- charm/schemes/ibenc/ibenc_waters09_z.py +147 -0
- charm/schemes/joye_scheme.py +106 -0
- charm/schemes/lem_scheme.py +207 -0
- charm/schemes/pk_fre_ccv11.py +107 -0
- charm/schemes/pk_vrf.py +127 -0
- charm/schemes/pkenc/__init__.py +0 -0
- charm/schemes/pkenc/pkenc_cs98.py +108 -0
- charm/schemes/pkenc/pkenc_elgamal85.py +122 -0
- charm/schemes/pkenc/pkenc_gm82.py +98 -0
- charm/schemes/pkenc/pkenc_paillier99.py +118 -0
- charm/schemes/pkenc/pkenc_rabin.py +254 -0
- charm/schemes/pkenc/pkenc_rsa.py +186 -0
- charm/schemes/pksig/__init__.py +0 -0
- charm/schemes/pksig/pksig_CW13_z.py +135 -0
- charm/schemes/pksig/pksig_bls04.py +87 -0
- charm/schemes/pksig/pksig_boyen.py +156 -0
- charm/schemes/pksig/pksig_chch.py +97 -0
- charm/schemes/pksig/pksig_chp.py +70 -0
- charm/schemes/pksig/pksig_cl03.py +150 -0
- charm/schemes/pksig/pksig_cl04.py +87 -0
- charm/schemes/pksig/pksig_cllww12_z.py +142 -0
- charm/schemes/pksig/pksig_cyh.py +132 -0
- charm/schemes/pksig/pksig_dsa.py +76 -0
- charm/schemes/pksig/pksig_ecdsa.py +71 -0
- charm/schemes/pksig/pksig_hess.py +104 -0
- charm/schemes/pksig/pksig_hw.py +110 -0
- charm/schemes/pksig/pksig_lamport.py +63 -0
- charm/schemes/pksig/pksig_ps01.py +135 -0
- charm/schemes/pksig/pksig_ps02.py +124 -0
- charm/schemes/pksig/pksig_ps03.py +119 -0
- charm/schemes/pksig/pksig_rsa_hw09.py +206 -0
- charm/schemes/pksig/pksig_schnorr91.py +77 -0
- charm/schemes/pksig/pksig_waters.py +115 -0
- charm/schemes/pksig/pksig_waters05.py +121 -0
- charm/schemes/pksig/pksig_waters09.py +121 -0
- charm/schemes/pre_mg07.py +150 -0
- charm/schemes/prenc/pre_afgh06.py +126 -0
- charm/schemes/prenc/pre_bbs98.py +123 -0
- charm/schemes/prenc/pre_nal16.py +216 -0
- charm/schemes/protocol_a01.py +272 -0
- charm/schemes/protocol_ao00.py +215 -0
- charm/schemes/protocol_cns07.py +274 -0
- charm/schemes/protocol_schnorr91.py +125 -0
- charm/schemes/sigma1.py +64 -0
- charm/schemes/sigma2.py +129 -0
- charm/schemes/sigma3.py +126 -0
- charm/schemes/threshold/__init__.py +59 -0
- charm/schemes/threshold/dkls23_dkg.py +556 -0
- charm/schemes/threshold/dkls23_presign.py +1089 -0
- charm/schemes/threshold/dkls23_sign.py +761 -0
- charm/schemes/threshold/xrpl_wallet.py +967 -0
- charm/test/__init__.py +0 -0
- charm/test/adapters/__init__.py +0 -0
- charm/test/adapters/abenc_adapt_hybrid_test.py +29 -0
- charm/test/adapters/dabenc_adapt_hybrid_test.py +56 -0
- charm/test/adapters/ibenc_adapt_hybrid_test.py +36 -0
- charm/test/adapters/ibenc_adapt_identityhash_test.py +32 -0
- charm/test/adapters/kpabenc_adapt_hybrid_test.py +30 -0
- charm/test/benchmark/abenc_yllc15_bench.py +92 -0
- charm/test/benchmark/benchmark_test.py +148 -0
- charm/test/benchmark_threshold.py +260 -0
- charm/test/conftest.py +38 -0
- charm/test/fuzz/__init__.py +1 -0
- charm/test/fuzz/conftest.py +5 -0
- charm/test/fuzz/fuzz_policy_parser.py +76 -0
- charm/test/fuzz/fuzz_serialization.py +83 -0
- charm/test/schemes/__init__.py +0 -0
- charm/test/schemes/abenc/__init__.py +0 -0
- charm/test/schemes/abenc/abenc_bsw07_test.py +39 -0
- charm/test/schemes/abenc/abenc_dacmacs_yj14_test.py +16 -0
- charm/test/schemes/abenc/abenc_lsw08_test.py +33 -0
- charm/test/schemes/abenc/abenc_maabe_yj14_test.py +16 -0
- charm/test/schemes/abenc/abenc_tbpre_lww14_test.py +16 -0
- charm/test/schemes/abenc/abenc_waters09_test.py +38 -0
- charm/test/schemes/abenc/abenc_yllc15_test.py +74 -0
- charm/test/schemes/chamhash_adm05_test.py +31 -0
- charm/test/schemes/chamhash_rsa_hw09_test.py +29 -0
- charm/test/schemes/commit/__init__.py +0 -0
- charm/test/schemes/commit/commit_gs08_test.py +24 -0
- charm/test/schemes/commit/commit_pedersen92_test.py +26 -0
- charm/test/schemes/dabe_aw11_test.py +45 -0
- charm/test/schemes/encap_bchk05_test.py +21 -0
- charm/test/schemes/grpsig/__init__.py +0 -0
- charm/test/schemes/grpsig/groupsig_bgls04_test.py +35 -0
- charm/test/schemes/grpsig/groupsig_bgls04_var_test.py +39 -0
- charm/test/schemes/hibenc/__init__.py +0 -0
- charm/test/schemes/hibenc/hibenc_bb04_test.py +28 -0
- charm/test/schemes/ibenc/__init__.py +0 -0
- charm/test/schemes/ibenc/ibenc_bb03_test.py +26 -0
- charm/test/schemes/ibenc/ibenc_bf01_test.py +24 -0
- charm/test/schemes/ibenc/ibenc_ckrs09_test.py +25 -0
- charm/test/schemes/ibenc/ibenc_lsw08_test.py +31 -0
- charm/test/schemes/ibenc/ibenc_sw05_test.py +32 -0
- charm/test/schemes/ibenc/ibenc_waters05_test.py +31 -0
- charm/test/schemes/ibenc/ibenc_waters09_test.py +27 -0
- charm/test/schemes/pk_vrf_test.py +29 -0
- charm/test/schemes/pkenc/__init__.py +0 -0
- charm/test/schemes/pkenc_test.py +255 -0
- charm/test/schemes/pksig/__init__.py +0 -0
- charm/test/schemes/pksig_test.py +376 -0
- charm/test/schemes/rsa_alg_test.py +340 -0
- charm/test/schemes/threshold_test.py +1792 -0
- charm/test/serialize/__init__.py +0 -0
- charm/test/serialize/serialize_test.py +40 -0
- charm/test/toolbox/__init__.py +0 -0
- charm/test/toolbox/conversion_test.py +30 -0
- charm/test/toolbox/ecgroup_test.py +53 -0
- charm/test/toolbox/integer_arithmetic_test.py +441 -0
- charm/test/toolbox/paddingschemes_test.py +238 -0
- charm/test/toolbox/policy_parser_stress_test.py +969 -0
- charm/test/toolbox/secretshare_test.py +28 -0
- charm/test/toolbox/symcrypto_test.py +108 -0
- charm/test/toolbox/test_policy_expression.py +16 -0
- charm/test/vectors/__init__.py +1 -0
- charm/test/vectors/test_bls_vectors.py +289 -0
- charm/test/vectors/test_pedersen_vectors.py +315 -0
- charm/test/vectors/test_schnorr_vectors.py +368 -0
- charm/test/zkp_compiler/__init__.py +9 -0
- charm/test/zkp_compiler/benchmark_zkp.py +258 -0
- charm/test/zkp_compiler/test_and_proof.py +240 -0
- charm/test/zkp_compiler/test_batch_verify.py +248 -0
- charm/test/zkp_compiler/test_dleq_proof.py +264 -0
- charm/test/zkp_compiler/test_or_proof.py +231 -0
- charm/test/zkp_compiler/test_proof_serialization.py +121 -0
- charm/test/zkp_compiler/test_range_proof.py +241 -0
- charm/test/zkp_compiler/test_representation_proof.py +325 -0
- charm/test/zkp_compiler/test_schnorr_proof.py +221 -0
- charm/test/zkp_compiler/test_thread_safety.py +169 -0
- charm/test/zkp_compiler/test_zkp_parser.py +139 -0
- charm/toolbox/ABEnc.py +26 -0
- charm/toolbox/ABEncMultiAuth.py +66 -0
- charm/toolbox/ABEnumeric.py +800 -0
- charm/toolbox/Commit.py +24 -0
- charm/toolbox/DFA.py +89 -0
- charm/toolbox/FSA.py +1254 -0
- charm/toolbox/Hash.py +39 -0
- charm/toolbox/IBEnc.py +62 -0
- charm/toolbox/IBSig.py +64 -0
- charm/toolbox/PKEnc.py +66 -0
- charm/toolbox/PKSig.py +56 -0
- charm/toolbox/PREnc.py +32 -0
- charm/toolbox/ZKProof.py +289 -0
- charm/toolbox/__init__.py +0 -0
- charm/toolbox/bitstring.py +49 -0
- charm/toolbox/broadcast.py +220 -0
- charm/toolbox/conversion.py +100 -0
- charm/toolbox/eccurve.py +149 -0
- charm/toolbox/ecgroup.py +143 -0
- charm/toolbox/enum.py +60 -0
- charm/toolbox/hash_module.py +91 -0
- charm/toolbox/integergroup.py +323 -0
- charm/toolbox/iterate.py +22 -0
- charm/toolbox/matrixops.py +76 -0
- charm/toolbox/mpc_utils.py +296 -0
- charm/toolbox/msp.py +175 -0
- charm/toolbox/mta.py +985 -0
- charm/toolbox/node.py +120 -0
- charm/toolbox/ot/__init__.py +22 -0
- charm/toolbox/ot/base_ot.py +374 -0
- charm/toolbox/ot/dpf.py +642 -0
- charm/toolbox/ot/mpfss.py +228 -0
- charm/toolbox/ot/ot_extension.py +589 -0
- charm/toolbox/ot/silent_ot.py +378 -0
- charm/toolbox/paddingschemes.py +423 -0
- charm/toolbox/paddingschemes_test.py +238 -0
- charm/toolbox/pairingcurves.py +85 -0
- charm/toolbox/pairinggroup.py +186 -0
- charm/toolbox/policy_expression_spec.py +70 -0
- charm/toolbox/policytree.py +189 -0
- charm/toolbox/reCompiler.py +346 -0
- charm/toolbox/redundancyschemes.py +65 -0
- charm/toolbox/schemebase.py +188 -0
- charm/toolbox/secretshare.py +104 -0
- charm/toolbox/secretutil.py +174 -0
- charm/toolbox/securerandom.py +73 -0
- charm/toolbox/sigmaprotocol.py +46 -0
- charm/toolbox/specialprimes.py +45 -0
- charm/toolbox/symcrypto.py +279 -0
- charm/toolbox/threshold_sharing.py +553 -0
- charm/toolbox/xmlserialize.py +94 -0
- charm/toolbox/zknode.py +105 -0
- charm/zkp_compiler/__init__.py +89 -0
- charm/zkp_compiler/and_proof.py +460 -0
- charm/zkp_compiler/batch_verify.py +324 -0
- charm/zkp_compiler/dleq_proof.py +423 -0
- charm/zkp_compiler/or_proof.py +305 -0
- charm/zkp_compiler/range_proof.py +417 -0
- charm/zkp_compiler/representation_proof.py +466 -0
- charm/zkp_compiler/schnorr_proof.py +273 -0
- charm/zkp_compiler/thread_safe.py +150 -0
- charm/zkp_compiler/zk_demo.py +489 -0
- charm/zkp_compiler/zkp_factory.py +330 -0
- charm/zkp_compiler/zkp_generator.py +370 -0
- charm/zkp_compiler/zkparser.py +269 -0
- charm_crypto_framework-0.61.1.dist-info/METADATA +337 -0
- charm_crypto_framework-0.61.1.dist-info/RECORD +323 -0
- charm_crypto_framework-0.61.1.dist-info/WHEEL +5 -0
- charm_crypto_framework-0.61.1.dist-info/licenses/LICENSE.txt +165 -0
- charm_crypto_framework-0.61.1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
'''
|
|
2
|
+
**RSA Public Key Encryption Scheme (RSA)**
|
|
3
|
+
|
|
4
|
+
*Authors:* R. Rivest, A. Shamir, L. Adleman
|
|
5
|
+
|
|
6
|
+
| **Title:** "A Method for Obtaining Digital Signatures and Public-Key Cryptosystems"
|
|
7
|
+
| **Published in:** Communications of the ACM, 1978
|
|
8
|
+
| **Available from:**
|
|
9
|
+
| **Notes:**
|
|
10
|
+
|
|
11
|
+
.. rubric:: Scheme Properties
|
|
12
|
+
|
|
13
|
+
* **Type:** encryption (public key)
|
|
14
|
+
* **Setting:** Integer
|
|
15
|
+
* **Assumption:** RSA (Integer Factorization)
|
|
16
|
+
|
|
17
|
+
.. rubric:: Implementation
|
|
18
|
+
|
|
19
|
+
:Authors: J. Ayo Akinyele, Gary Belvin
|
|
20
|
+
:Date: 07/2011
|
|
21
|
+
'''
|
|
22
|
+
|
|
23
|
+
from charm.core.math.integer import integer,isPrime,gcd,random,randomPrime,toInt
|
|
24
|
+
from charm.toolbox.PKEnc import PKEnc
|
|
25
|
+
from charm.toolbox.PKSig import PKSig
|
|
26
|
+
from charm.toolbox.paddingschemes import OAEPEncryptionPadding,PSSPadding
|
|
27
|
+
from charm.toolbox.conversion import Conversion
|
|
28
|
+
from math import ceil
|
|
29
|
+
|
|
30
|
+
debug = False
|
|
31
|
+
class RSA():
|
|
32
|
+
def __init__(self):
|
|
33
|
+
pass
|
|
34
|
+
# generate p,q and n
|
|
35
|
+
def paramgen(self, secparam):
|
|
36
|
+
while True:
|
|
37
|
+
p, q = randomPrime(secparam), randomPrime(secparam)
|
|
38
|
+
if isPrime(p) and isPrime(q) and p != q:
|
|
39
|
+
N = p * q
|
|
40
|
+
phi_N = (p - 1) * (q - 1)
|
|
41
|
+
break
|
|
42
|
+
return (p, q, N, phi_N)
|
|
43
|
+
|
|
44
|
+
def keygen(self, secparam=1024, params=None):
|
|
45
|
+
if params:
|
|
46
|
+
(N, e, d, p, q) = self.convert(params)
|
|
47
|
+
phi_N = (p - 1) * (q - 1)
|
|
48
|
+
pk = { 'N':N, 'e':e }
|
|
49
|
+
sk = { 'phi_N':phi_N, 'd':d , 'N':N}
|
|
50
|
+
return (pk, sk)
|
|
51
|
+
|
|
52
|
+
(p, q, N, phi_N) = self.paramgen(secparam)
|
|
53
|
+
|
|
54
|
+
# Use deterministic algorithm to find coprime value instead of random search
|
|
55
|
+
# This fixes Python 3.12+ hanging issue where random values share common factors
|
|
56
|
+
# Try common RSA public exponents first, then search incrementally
|
|
57
|
+
common_exponents = [65537, 3, 5, 17, 257, 641, 6700417]
|
|
58
|
+
e_value = None
|
|
59
|
+
|
|
60
|
+
for candidate in common_exponents:
|
|
61
|
+
# Use isCoPrime() method which properly checks gcd == 1
|
|
62
|
+
if phi_N.isCoPrime(candidate):
|
|
63
|
+
e_value = candidate
|
|
64
|
+
break
|
|
65
|
+
|
|
66
|
+
# If common exponents don't work, search incrementally starting from a larger value
|
|
67
|
+
if e_value is None:
|
|
68
|
+
e_value = 65537
|
|
69
|
+
max_iterations = 10000000 # Large limit for deterministic search
|
|
70
|
+
|
|
71
|
+
for iterations in range(max_iterations):
|
|
72
|
+
# Use isCoPrime() method which properly checks gcd == 1
|
|
73
|
+
if phi_N.isCoPrime(e_value):
|
|
74
|
+
break
|
|
75
|
+
e_value += 2 # Only try odd numbers (even numbers can't be coprime with even phi_N)
|
|
76
|
+
|
|
77
|
+
# Check if we found a coprime value (either broke out of loop or on last iteration)
|
|
78
|
+
if not phi_N.isCoPrime(e_value):
|
|
79
|
+
raise RuntimeError(
|
|
80
|
+
f"Could not find coprime value after {max_iterations} iterations. "
|
|
81
|
+
f"phi_N={phi_N}, last e_value={e_value}, gcd(e_value, phi_N)={gcd(e_value, phi_N)}"
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
# Create modular integer with phi_N as modulus - this is required for modular inverse
|
|
85
|
+
# Similar to how Rabin does: integer(i) % pk['N']
|
|
86
|
+
e = integer(e_value, phi_N)
|
|
87
|
+
d = e ** -1 # Compute modular inverse
|
|
88
|
+
pk = { 'N':N, 'e':e_value } # Use the plain integer value for public key
|
|
89
|
+
sk = { 'phi_N':phi_N, 'd':d , 'N':N}
|
|
90
|
+
|
|
91
|
+
return (pk, sk)
|
|
92
|
+
|
|
93
|
+
def convert(self, N, e, d, p, q):
|
|
94
|
+
return (integer(N), integer(e), integer(d),
|
|
95
|
+
integer(p), integer(q))
|
|
96
|
+
|
|
97
|
+
class RSA_Enc(RSA,PKEnc):
|
|
98
|
+
"""
|
|
99
|
+
>>> rsa = RSA_Enc()
|
|
100
|
+
>>> (public_key, secret_key) = rsa.keygen(1024)
|
|
101
|
+
>>> msg = b'This is a test'
|
|
102
|
+
>>> cipher_text = rsa.encrypt(public_key, msg)
|
|
103
|
+
>>> decrypted_msg = rsa.decrypt(public_key, secret_key, cipher_text)
|
|
104
|
+
>>> decrypted_msg == msg
|
|
105
|
+
True
|
|
106
|
+
"""
|
|
107
|
+
def __init__(self, padding=OAEPEncryptionPadding(), params=None):
|
|
108
|
+
RSA.__init__(self)
|
|
109
|
+
PKEnc.__init__(self)
|
|
110
|
+
self.paddingscheme = padding
|
|
111
|
+
# m : Bytes
|
|
112
|
+
def encrypt(self, pk, m, salt=None):
|
|
113
|
+
octetlen = int(ceil(int(pk['N']).bit_length() / 8.0))
|
|
114
|
+
EM = self.paddingscheme.encode(m, octetlen, "", salt)
|
|
115
|
+
if debug: print("EM == >", EM)
|
|
116
|
+
i = Conversion.OS2IP(EM)
|
|
117
|
+
ip = integer(i) % pk['N'] #Convert to modular integer
|
|
118
|
+
return (ip ** pk['e']) % pk['N']
|
|
119
|
+
|
|
120
|
+
def decrypt(self, pk, sk, c):
|
|
121
|
+
octetlen = int(ceil(int(pk['N']).bit_length() / 8.0))
|
|
122
|
+
M = (c ** (sk['d'] % sk['phi_N'])) % pk['N']
|
|
123
|
+
os = Conversion.IP2OS(int(M), octetlen)
|
|
124
|
+
if debug: print("OS =>", os)
|
|
125
|
+
return self.paddingscheme.decode(os)
|
|
126
|
+
|
|
127
|
+
class RSA_Sig(RSA, PKSig):
|
|
128
|
+
"""
|
|
129
|
+
>>> msg = b'This is a test message.'
|
|
130
|
+
>>> rsa = RSA_Sig()
|
|
131
|
+
>>> (public_key, secret_key) = rsa.keygen(1024)
|
|
132
|
+
>>> signature = rsa.sign(secret_key, msg)
|
|
133
|
+
>>> rsa.verify(public_key, msg, signature)
|
|
134
|
+
True
|
|
135
|
+
"""
|
|
136
|
+
'''RSASSA-PSS'''
|
|
137
|
+
def __init__(self, padding=PSSPadding()):
|
|
138
|
+
RSA.__init__(self)
|
|
139
|
+
PKSig.__init__(self)
|
|
140
|
+
self.paddingscheme = padding
|
|
141
|
+
|
|
142
|
+
def sign(self,sk, M, salt=None):
|
|
143
|
+
#apply encoding
|
|
144
|
+
modbits = int(sk['N']).bit_length()
|
|
145
|
+
k = int(ceil(modbits / 8.0))
|
|
146
|
+
emLen = int(ceil((modbits -1) / 8.0))
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
em = self.paddingscheme.encode(M, modbits - 1, salt)
|
|
150
|
+
m = Conversion.OS2IP(em)
|
|
151
|
+
m = integer(m) % sk['N'] #ERRROR m is larger than N
|
|
152
|
+
s = (m ** sk['d']) % sk['N']
|
|
153
|
+
S = Conversion.IP2OS(s, k)
|
|
154
|
+
if debug:
|
|
155
|
+
print("Signing")
|
|
156
|
+
print("k =>", k)
|
|
157
|
+
print("emLen =>", emLen)
|
|
158
|
+
print("m =>", m)
|
|
159
|
+
print("em =>", em)
|
|
160
|
+
print("s =>", s)
|
|
161
|
+
print("S =>", S)
|
|
162
|
+
return S
|
|
163
|
+
|
|
164
|
+
def verify(self, pk, M, S):
|
|
165
|
+
modbits = int(pk['N']).bit_length()
|
|
166
|
+
k = int(ceil(modbits / 8.0))
|
|
167
|
+
emLen = int(ceil((modbits -1) / 8.0))
|
|
168
|
+
if len(S) != k:
|
|
169
|
+
if debug: print("Sig is %s octets long, not %" %(len(S), k))
|
|
170
|
+
return False
|
|
171
|
+
s = Conversion.OS2IP(S)
|
|
172
|
+
s = integer(s) % pk['N'] #Convert to modular integer
|
|
173
|
+
m = (s ** pk['e']) % pk['N']
|
|
174
|
+
EM = Conversion.IP2OS(m, emLen)
|
|
175
|
+
if debug:
|
|
176
|
+
print("Verifying")
|
|
177
|
+
print("k =>", k)
|
|
178
|
+
print("emLen =>", emLen)
|
|
179
|
+
print("s =>", s)
|
|
180
|
+
print("m =>", m)
|
|
181
|
+
print("em =>", EM)
|
|
182
|
+
print("S =>", S)
|
|
183
|
+
return self.paddingscheme.verify(M, EM, modbits-1)
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
|
|
File without changes
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
'''
|
|
2
|
+
**Chen-Wee Dual System Signature (CW13)**
|
|
3
|
+
|
|
4
|
+
*Authors:* J. Chen, H. Wee
|
|
5
|
+
|
|
6
|
+
| **Title:** "Dual System Groups and its Applications - Compact HIBE and More"
|
|
7
|
+
| **Published in:** Manuscript, 2013
|
|
8
|
+
| **Available from:** Manuscript
|
|
9
|
+
| **Notes:** Optimized implementation reducing exponential and multiplication operations.
|
|
10
|
+
|
|
11
|
+
.. rubric:: Scheme Properties
|
|
12
|
+
|
|
13
|
+
* **Type:** signature (identity-based)
|
|
14
|
+
* **Setting:** bilinear groups (asymmetric)
|
|
15
|
+
* **Assumption:** SXDH
|
|
16
|
+
|
|
17
|
+
.. rubric:: Implementation
|
|
18
|
+
|
|
19
|
+
:Authors: Fan Zhang (zfwise@gwu.edu), Hoeteck Wee
|
|
20
|
+
:Date: 5/2013
|
|
21
|
+
'''
|
|
22
|
+
from charm.toolbox.pairinggroup import PairingGroup,ZR,G1,G2,GT,pair
|
|
23
|
+
from charm.core.crypto.cryptobase import *
|
|
24
|
+
from charm.toolbox.PKSig import PKSig
|
|
25
|
+
from charm.toolbox.matrixops import *
|
|
26
|
+
|
|
27
|
+
debug = False
|
|
28
|
+
class Sign_CW13(PKSig):
|
|
29
|
+
def __init__(self, groupObj):
|
|
30
|
+
PKSig.__init__(self)
|
|
31
|
+
global group
|
|
32
|
+
group = groupObj
|
|
33
|
+
|
|
34
|
+
def keygen(self):
|
|
35
|
+
g2 = group.random(G1) #generator in G1
|
|
36
|
+
g1 = group.random(G2) #generator in G2
|
|
37
|
+
|
|
38
|
+
#generate B and B*
|
|
39
|
+
B = [[group.random(ZR), group.random(ZR)],[group.random(ZR), group.random(ZR)]]
|
|
40
|
+
|
|
41
|
+
Bt = MatrixTransGroups(B)
|
|
42
|
+
Bstar= [GaussEliminationinGroups([[Bt[0][0], Bt[0][1], group.init(ZR, 1)],
|
|
43
|
+
[Bt[1][0], Bt[1][1], group.init(ZR, 0)]]),
|
|
44
|
+
GaussEliminationinGroups([[Bt[0][0], Bt[0][1], group.init(ZR, 0)],
|
|
45
|
+
[Bt[1][0], Bt[1][1], group.init(ZR, 1)]])]
|
|
46
|
+
Bstar = MatrixTransGroups(Bstar)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
## checks Bt * Bstar = identity matrix
|
|
50
|
+
# for i in self.MatrixMulGroups(Bt, Bstar):
|
|
51
|
+
# print("[%s,%s]"%(i[0],i[1]))
|
|
52
|
+
|
|
53
|
+
#generate R
|
|
54
|
+
R = [[group.random(ZR), group.init(ZR, 0)],
|
|
55
|
+
[group.init(ZR, 0), group.init(ZR, 1)]]
|
|
56
|
+
|
|
57
|
+
#generate A1 and A2
|
|
58
|
+
A1 =[[group.random(ZR), group.random(ZR)],
|
|
59
|
+
[group.random(ZR), group.random(ZR)]]
|
|
60
|
+
A2 =[[group.random(ZR), group.random(ZR)],
|
|
61
|
+
[group.random(ZR), group.random(ZR)]]
|
|
62
|
+
k = [group.random(ZR),group.random(ZR)] #k is a 2 dimentional vector
|
|
63
|
+
|
|
64
|
+
BA1 = MatrixMulGroups(B,A1)
|
|
65
|
+
BA2 = MatrixMulGroups(B,A2)
|
|
66
|
+
BsR = MatrixMulGroups(Bstar,R)
|
|
67
|
+
BsA1R = MatrixMulGroups(MatrixMulGroups(Bstar, MatrixTransGroups(A1)),R)
|
|
68
|
+
BsA2R = MatrixMulGroups(MatrixMulGroups(Bstar, MatrixTransGroups(A2)),R)
|
|
69
|
+
b0 = [B[0][0],B[1][0]]
|
|
70
|
+
b1 = [BA1[0][0],BA1[1][0]]
|
|
71
|
+
b2 = [BA2[0][0],BA2[1][0]]
|
|
72
|
+
b0s = [BsR[0][0],BsR[1][0]]
|
|
73
|
+
b1s = [BsA1R[0][0],BsA1R[1][0]]
|
|
74
|
+
b2s = [BsA2R[0][0],BsA2R[1][0]]
|
|
75
|
+
|
|
76
|
+
#generate the mpk
|
|
77
|
+
g1b0 = [g1**b0[0], g1**b0[1]]
|
|
78
|
+
g1b1 = [g1**b1[0], g1**b1[1]]
|
|
79
|
+
g1b2 = [g1**b2[0], g1**b2[1]]
|
|
80
|
+
egg = (pair(g2, g1)) ** (k[0]*b0[0] + k[1]*b0[1])
|
|
81
|
+
|
|
82
|
+
pk = {'g1':g1, 'g2':g2, 'g1b0':g1b0, 'g1b1':g1b1, 'g1b2': g1b2, 'egg':egg}
|
|
83
|
+
|
|
84
|
+
#generate private parameters
|
|
85
|
+
sk = { 'k':k, 'b0s':b0s, 'b1s':b1s,'b2s':b2s}
|
|
86
|
+
|
|
87
|
+
if(debug):
|
|
88
|
+
print("Public parameters...")
|
|
89
|
+
group.debug(pk)
|
|
90
|
+
print("Secret parameters...")
|
|
91
|
+
group.debug(sk)
|
|
92
|
+
return (pk, sk)
|
|
93
|
+
|
|
94
|
+
def sign(self, pk, sk, m):
|
|
95
|
+
#_ID is an element in ZR, r is an random number in ZR
|
|
96
|
+
M = group.hash(m, ZR)
|
|
97
|
+
r = group.random(ZR)
|
|
98
|
+
|
|
99
|
+
sig = {'K0': [pk['g2']**(sk['b0s'][0]*r),
|
|
100
|
+
pk['g2']**(sk['b0s'][1]*r)],
|
|
101
|
+
'K1': [pk['g2']**(sk['k'][0] + (sk['b2s'][0]+M*sk['b1s'][0])*r),
|
|
102
|
+
pk['g2']**(sk['k'][1] + (sk['b2s'][1]+M*sk['b1s'][1])*r)]}
|
|
103
|
+
return sig
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def verify(self, pk, sig, m):
|
|
107
|
+
|
|
108
|
+
M = group.hash(m,ZR)
|
|
109
|
+
C0 = [pk['g1b0'][0], pk['g1b0'][1]]
|
|
110
|
+
C1 = [(pk['g1b2'][0]*(pk['g1b1'][0]**M)),
|
|
111
|
+
(pk['g1b2'][1]*(pk['g1b1'][1]**M))]
|
|
112
|
+
C2 = (pk['egg'])
|
|
113
|
+
|
|
114
|
+
mask = self.vpair(C0, sig['K1']) / self.vpair(C1, sig['K0'])
|
|
115
|
+
return (C2 == mask)
|
|
116
|
+
|
|
117
|
+
def vpair(self, g1v, g2v):
|
|
118
|
+
return pair(g2v[0],g1v[0]) * pair(g2v[1],g1v[1])
|
|
119
|
+
|
|
120
|
+
def main():
|
|
121
|
+
|
|
122
|
+
group = PairingGroup('MNT224', secparam=1024)
|
|
123
|
+
m = "plese sign this message!!!!"
|
|
124
|
+
pksig = Sign_CW13(group)
|
|
125
|
+
(pk, sk) = pksig.keygen()
|
|
126
|
+
|
|
127
|
+
signature = pksig.sign(pk, sk, m)
|
|
128
|
+
|
|
129
|
+
assert pksig.verify(pk, signature, m), "Invalid Verification!!!!"
|
|
130
|
+
if debug: print("Successful Individual Verification!")
|
|
131
|
+
|
|
132
|
+
if __name__ == '__main__':
|
|
133
|
+
debug = True
|
|
134
|
+
main()
|
|
135
|
+
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
'''
|
|
2
|
+
**Boneh-Lynn-Shacham Signature (BLS04)**
|
|
3
|
+
|
|
4
|
+
*Authors:* D. Boneh, B. Lynn, H. Shacham
|
|
5
|
+
|
|
6
|
+
| **Title:** "Short Signatures from the Weil Pairing"
|
|
7
|
+
| **Published in:** Journal of Cryptology, 2004
|
|
8
|
+
| **Available from:** https://crypto.stanford.edu/~dabo/pubs/papers/BLSmultisig.html
|
|
9
|
+
| **Notes:** This is the IBE (2-level HIBE) implementation of the HIBE scheme BB_2.
|
|
10
|
+
|
|
11
|
+
.. rubric:: Scheme Properties
|
|
12
|
+
|
|
13
|
+
* **Type:** signature (identity-based)
|
|
14
|
+
* **Setting:** bilinear groups (asymmetric)
|
|
15
|
+
* **Assumption:** CDH
|
|
16
|
+
|
|
17
|
+
.. rubric:: Implementation
|
|
18
|
+
|
|
19
|
+
:Authors: J. Ayo Akinyele
|
|
20
|
+
:Date: 1/2011
|
|
21
|
+
'''
|
|
22
|
+
from charm.toolbox.pairinggroup import PairingGroup, ZR, G1, G2, pair
|
|
23
|
+
from charm.core.engine.util import objectToBytes
|
|
24
|
+
from charm.toolbox.IBSig import *
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
debug = False
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class BLS01(IBSig):
|
|
31
|
+
"""
|
|
32
|
+
>>> from charm.toolbox.pairinggroup import PairingGroup
|
|
33
|
+
>>> group = PairingGroup('MNT224')
|
|
34
|
+
>>> messages = { 'a':"hello world!!!" , 'b':"test message" }
|
|
35
|
+
>>> ib = BLS01(group)
|
|
36
|
+
>>> (public_key, secret_key) = ib.keygen()
|
|
37
|
+
>>> signature = ib.sign(secret_key['x'], messages)
|
|
38
|
+
>>> ib.verify(public_key, signature, messages)
|
|
39
|
+
True
|
|
40
|
+
"""
|
|
41
|
+
def __init__(self, groupObj):
|
|
42
|
+
IBSig.__init__(self)
|
|
43
|
+
global group
|
|
44
|
+
group = groupObj
|
|
45
|
+
|
|
46
|
+
def dump(self, obj):
|
|
47
|
+
return objectToBytes(obj, group)
|
|
48
|
+
|
|
49
|
+
def keygen(self, secparam=None):
|
|
50
|
+
g, x = group.random(G2), group.random()
|
|
51
|
+
g_x = g ** x
|
|
52
|
+
pk = { 'g^x':g_x, 'g':g, 'identity':str(g_x), 'secparam':secparam }
|
|
53
|
+
sk = { 'x':x }
|
|
54
|
+
return (pk, sk)
|
|
55
|
+
|
|
56
|
+
def sign(self, x, message):
|
|
57
|
+
M = self.dump(message)
|
|
58
|
+
if debug: print("Message => '%s'" % M)
|
|
59
|
+
return group.hash(M, G1) ** x
|
|
60
|
+
|
|
61
|
+
def verify(self, pk, sig, message):
|
|
62
|
+
M = self.dump(message)
|
|
63
|
+
h = group.hash(M, G1)
|
|
64
|
+
if pair(sig, pk['g']) == pair(h, pk['g^x']):
|
|
65
|
+
return True
|
|
66
|
+
return False
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def main():
|
|
70
|
+
groupObj = PairingGroup('MNT224')
|
|
71
|
+
|
|
72
|
+
m = { 'a':"hello world!!!" , 'b':"test message" }
|
|
73
|
+
bls = BLS01(groupObj)
|
|
74
|
+
|
|
75
|
+
(pk, sk) = bls.keygen()
|
|
76
|
+
|
|
77
|
+
sig = bls.sign(sk['x'], m)
|
|
78
|
+
|
|
79
|
+
if debug: print("Message: '%s'" % m)
|
|
80
|
+
if debug: print("Signature: '%s'" % sig)
|
|
81
|
+
assert bls.verify(pk, sig, m), "Failure!!!"
|
|
82
|
+
if debug: print('SUCCESS!!!')
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
if __name__ == "__main__":
|
|
86
|
+
debug = True
|
|
87
|
+
main()
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
'''
|
|
2
|
+
**Boyen Mesh Signatures (Boyen07)**
|
|
3
|
+
|
|
4
|
+
*Authors:* X. Boyen
|
|
5
|
+
|
|
6
|
+
| **Title:** "Mesh Signatures: How to Leak a Secret with Unwitting and Unwilling Participants"
|
|
7
|
+
| **Published in:** EUROCRYPT, 2007
|
|
8
|
+
| **Available from:** http://eprint.iacr.org/2007/094.pdf
|
|
9
|
+
| **Notes:**
|
|
10
|
+
|
|
11
|
+
.. rubric:: Scheme Properties
|
|
12
|
+
|
|
13
|
+
* **Type:** signature (ring-based)
|
|
14
|
+
* **Setting:** bilinear groups (asymmetric)
|
|
15
|
+
* **Assumption:** q-SDH
|
|
16
|
+
|
|
17
|
+
.. rubric:: Implementation
|
|
18
|
+
|
|
19
|
+
:Authors: J. Ayo Akinyele
|
|
20
|
+
:Date: 11/2011
|
|
21
|
+
'''
|
|
22
|
+
from charm.toolbox.pairinggroup import PairingGroup,ZR,G1,G2,GT,pair
|
|
23
|
+
from charm.toolbox.PKSig import PKSig
|
|
24
|
+
|
|
25
|
+
debug = False
|
|
26
|
+
|
|
27
|
+
# need RingSig
|
|
28
|
+
class Boyen(PKSig):
|
|
29
|
+
"""
|
|
30
|
+
>>> from charm.toolbox.pairinggroup import PairingGroup
|
|
31
|
+
>>> group = PairingGroup('MNT224')
|
|
32
|
+
>>> boyen = Boyen(group)
|
|
33
|
+
>>> master_public_key = boyen.setup()
|
|
34
|
+
>>> num_signers = 3
|
|
35
|
+
>>> keys = [ boyen.keygen(master_public_key) for i in range(num_signers)]
|
|
36
|
+
>>> public_keys, secret_keys = {},{}
|
|
37
|
+
>>> for i in range(len(keys)):
|
|
38
|
+
... public_keys[ i+1 ] = keys[ i ][ 0 ]
|
|
39
|
+
... secret_keys[ i+1 ] = keys[ i ][ 1 ]
|
|
40
|
+
>>> signer = 3
|
|
41
|
+
>>> secret_key = secret_keys[signer]
|
|
42
|
+
>>> msg = 'please sign this new message!'
|
|
43
|
+
>>> signature = boyen.sign(signer, master_public_key, public_keys, secret_key, msg)
|
|
44
|
+
>>> boyen.verify(master_public_key, public_keys, msg, signature)
|
|
45
|
+
True
|
|
46
|
+
"""
|
|
47
|
+
def __init__(self, groupObj):
|
|
48
|
+
global group
|
|
49
|
+
group = groupObj
|
|
50
|
+
|
|
51
|
+
def setup(self):
|
|
52
|
+
global H
|
|
53
|
+
H = lambda a: group.hash(('1', str(a)), ZR)
|
|
54
|
+
g1, g2 = group.random(G1), group.random(G2)
|
|
55
|
+
a = [group.random(ZR) for i in range(3)]
|
|
56
|
+
A = []; At = [];
|
|
57
|
+
for i in range(3):
|
|
58
|
+
A.append(g1 ** a[i])
|
|
59
|
+
At.append(g2 ** a[i])
|
|
60
|
+
# public verification key "in the sky" for all users
|
|
61
|
+
return {'g1':g1, 'g2':g2, 'A':A[0], 'B':A[1], 'C':A[2],
|
|
62
|
+
'At':At[0], 'Bt':At[1], 'Ct':At[2]}
|
|
63
|
+
|
|
64
|
+
def keygen(self, mpk):
|
|
65
|
+
a, b, c = group.random(ZR), group.random(ZR), group.random(ZR)
|
|
66
|
+
A = mpk['g1'] ** a; B = mpk['g1'] ** b; C = mpk['g1'] ** c
|
|
67
|
+
At = mpk['g2'] ** a; Bt = mpk['g2'] ** b; Ct = mpk['g2'] ** c
|
|
68
|
+
sk = {'a':a, 'b':b, 'c':c}
|
|
69
|
+
pk = {'A':A, 'B':B, 'C':C, 'At':At, 'Bt':Bt, 'Ct':Ct}
|
|
70
|
+
return (pk, sk)
|
|
71
|
+
|
|
72
|
+
def getPKdict(self, mpk, pk, k):
|
|
73
|
+
A_pk, B_pk, C_pk = {}, {}, {}
|
|
74
|
+
A_pk[ 0 ] = mpk[ k[0] ]
|
|
75
|
+
B_pk[ 0 ] = mpk[ k[1] ]
|
|
76
|
+
C_pk[ 0 ] = mpk[ k[2] ]
|
|
77
|
+
for i in pk.keys():
|
|
78
|
+
A_pk[ i ] = pk[ i ][ k[0] ]
|
|
79
|
+
B_pk[ i ] = pk[ i ][ k[1] ]
|
|
80
|
+
C_pk[ i ] = pk[ i ][ k[2] ]
|
|
81
|
+
return A_pk, B_pk, C_pk
|
|
82
|
+
|
|
83
|
+
def sign(self, index, mpk, pk, sk, M):
|
|
84
|
+
if debug: print("pk =>", pk.keys())
|
|
85
|
+
(A_pk, B_pk, C_pk) = self.getPKdict(mpk, pk, ['A', 'B', 'C'])
|
|
86
|
+
m = H(M)
|
|
87
|
+
l = len(A_pk.keys())
|
|
88
|
+
assert index >= 0 and index < l, "invalid index"
|
|
89
|
+
if debug: print("l defined as =>", l)
|
|
90
|
+
s = {}
|
|
91
|
+
S = {}
|
|
92
|
+
for i in range(0, l):
|
|
93
|
+
if i != index:
|
|
94
|
+
s[i] = group.random(ZR)
|
|
95
|
+
S[i] = mpk['g1'] ** s[i]
|
|
96
|
+
t = [group.random(ZR) for i in range(l)]
|
|
97
|
+
# index=0
|
|
98
|
+
(A, B, C) = A_pk[ 0 ], B_pk[ 0 ], C_pk[ 0 ]
|
|
99
|
+
prod = (A * (B ** m) * (C ** t[0])) ** -s[0]
|
|
100
|
+
|
|
101
|
+
# 1 -> l
|
|
102
|
+
for i in range(1, l):
|
|
103
|
+
if i != index:
|
|
104
|
+
(A, B, C) = A_pk[i], B_pk[i], C_pk[i]
|
|
105
|
+
prod *= ((A * (B ** m) * (C ** t[i])) ** -s[i])
|
|
106
|
+
|
|
107
|
+
d = (sk['a'] + (sk['b'] * m) + (sk['c'] * t[index])) # s[l]
|
|
108
|
+
S[index] = (mpk['g1'] * prod) ** (1 / d) # S[l]
|
|
109
|
+
if debug: print("S[", index, "] :=", S[index])
|
|
110
|
+
sig = { 'S':S, 't':t }
|
|
111
|
+
return sig
|
|
112
|
+
|
|
113
|
+
def verify(self, mpk, pk, M, sig):
|
|
114
|
+
if debug: print("Verifying...")
|
|
115
|
+
At, Bt, Ct = self.getPKdict(mpk, pk, ['At', 'Bt', 'Ct'])
|
|
116
|
+
l = len(At.keys())
|
|
117
|
+
D = pair(mpk['g1'], mpk['g2'])
|
|
118
|
+
S, t = sig['S'], sig['t']
|
|
119
|
+
m = H(M)
|
|
120
|
+
dotProd0 = 1
|
|
121
|
+
for i in range(l):
|
|
122
|
+
dotProd0 *= pair(S[i], At[i] * (Bt[i] ** m) * (Ct[i] ** t[i]))
|
|
123
|
+
if dotProd0 == D:
|
|
124
|
+
return True
|
|
125
|
+
return False
|
|
126
|
+
|
|
127
|
+
def main():
|
|
128
|
+
groupObj = PairingGroup('MNT224')
|
|
129
|
+
boyen = Boyen(groupObj)
|
|
130
|
+
mpk = boyen.setup()
|
|
131
|
+
if debug: print("Pub parameters")
|
|
132
|
+
if debug: print(mpk, "\n\n")
|
|
133
|
+
|
|
134
|
+
num_signers = 3
|
|
135
|
+
L_keys = [ boyen.keygen(mpk) for i in range(num_signers)]
|
|
136
|
+
L_pk = {}; L_sk = {}
|
|
137
|
+
for i in range(len(L_keys)):
|
|
138
|
+
L_pk[ i+1 ] = L_keys[ i ][ 0 ] # pk
|
|
139
|
+
L_sk[ i+1 ] = L_keys[ i ][ 1 ]
|
|
140
|
+
|
|
141
|
+
if debug: print("Keygen...")
|
|
142
|
+
if debug: print("sec keys =>", L_sk.keys(),"\n", L_sk)
|
|
143
|
+
|
|
144
|
+
signer = 3
|
|
145
|
+
sk = L_sk[signer]
|
|
146
|
+
M = 'please sign this new message!'
|
|
147
|
+
sig = boyen.sign(signer, mpk, L_pk, sk, M)
|
|
148
|
+
if debug: print("\nSignature...")
|
|
149
|
+
if debug: print("sig =>", sig)
|
|
150
|
+
|
|
151
|
+
assert boyen.verify(mpk, L_pk, M, sig), "invalid signature!"
|
|
152
|
+
if debug: print("Verification successful!")
|
|
153
|
+
|
|
154
|
+
if __name__ == "__main__":
|
|
155
|
+
debug = True
|
|
156
|
+
main()
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
'''
|
|
2
|
+
**Cha-Cheon Identity-Based Signature (CHCH03)**
|
|
3
|
+
|
|
4
|
+
*Authors:* J. C. Cha, J. H. Cheon
|
|
5
|
+
|
|
6
|
+
| **Title:** "An Identity-Based Signature from Gap Diffie-Hellman Groups"
|
|
7
|
+
| **Published in:** PKC, 2003
|
|
8
|
+
| **Available from:** LNCS Vol. 2567, pages 18-30
|
|
9
|
+
| **Notes:**
|
|
10
|
+
|
|
11
|
+
.. rubric:: Scheme Properties
|
|
12
|
+
|
|
13
|
+
* **Type:** signature (identity-based)
|
|
14
|
+
* **Setting:** bilinear groups (asymmetric)
|
|
15
|
+
* **Assumption:** Gap-DH
|
|
16
|
+
|
|
17
|
+
.. rubric:: Implementation
|
|
18
|
+
|
|
19
|
+
:Authors: J. Ayo Akinyele
|
|
20
|
+
:Date: 11/2011
|
|
21
|
+
'''
|
|
22
|
+
from charm.toolbox.pairinggroup import PairingGroup,ZR,G1,G2,GT,pair
|
|
23
|
+
from charm.toolbox.PKSig import PKSig
|
|
24
|
+
|
|
25
|
+
debug = False
|
|
26
|
+
class CHCH(PKSig):
|
|
27
|
+
"""
|
|
28
|
+
>>> from charm.toolbox.pairinggroup import PairingGroup
|
|
29
|
+
>>> group = PairingGroup('SS512')
|
|
30
|
+
>>> chch = CHCH(group)
|
|
31
|
+
>>> (master_public_key, master_secret_key) = chch.setup()
|
|
32
|
+
>>> ID = "janedoe@email.com"
|
|
33
|
+
>>> (public_key, secret_key) = chch.keygen(master_secret_key, ID)
|
|
34
|
+
>>> msg = "this is a message!"
|
|
35
|
+
>>> signature = chch.sign(public_key, secret_key, msg)
|
|
36
|
+
>>> chch.verify(master_public_key, public_key, msg, signature)
|
|
37
|
+
True
|
|
38
|
+
"""
|
|
39
|
+
def __init__(self, groupObj):
|
|
40
|
+
global group,H1,H2
|
|
41
|
+
group = groupObj
|
|
42
|
+
H1 = lambda x: group.hash(x, G1)
|
|
43
|
+
H2 = lambda x,y: group.hash((x,y), ZR)
|
|
44
|
+
|
|
45
|
+
def setup(self):
|
|
46
|
+
g2, alpha = group.random(G2), group.random(ZR)
|
|
47
|
+
msk = alpha
|
|
48
|
+
P = g2 ** alpha
|
|
49
|
+
mpk = {'P':P, 'g2':g2}
|
|
50
|
+
return (mpk, msk)
|
|
51
|
+
|
|
52
|
+
def keygen(self, msk, ID):
|
|
53
|
+
alpha = msk
|
|
54
|
+
sk = H1(ID) ** alpha
|
|
55
|
+
pk = H1(ID)
|
|
56
|
+
return (pk, sk)
|
|
57
|
+
|
|
58
|
+
def sign(self, pk, sk, M):
|
|
59
|
+
if debug: print("sign...")
|
|
60
|
+
s = group.random(ZR)
|
|
61
|
+
S1 = pk ** s
|
|
62
|
+
a = H2(M, S1)
|
|
63
|
+
S2 = sk ** (s + a)
|
|
64
|
+
return {'S1':S1, 'S2':S2}
|
|
65
|
+
|
|
66
|
+
def verify(self, mpk, pk, M, sig):
|
|
67
|
+
if debug: print("verify...")
|
|
68
|
+
(S1, S2) = sig['S1'], sig['S2']
|
|
69
|
+
a = H2(M, S1)
|
|
70
|
+
if pair(S2, mpk['g2']) == pair(S1 * (pk ** a), mpk['P']):
|
|
71
|
+
return True
|
|
72
|
+
return False
|
|
73
|
+
|
|
74
|
+
def main():
|
|
75
|
+
groupObj = PairingGroup('SS512')
|
|
76
|
+
chch = CHCH(groupObj)
|
|
77
|
+
(mpk, msk) = chch.setup()
|
|
78
|
+
|
|
79
|
+
_id = "janedoe@email.com"
|
|
80
|
+
(pk, sk) = chch.keygen(msk, _id)
|
|
81
|
+
if debug:
|
|
82
|
+
print("Keygen...")
|
|
83
|
+
print("pk =>", pk)
|
|
84
|
+
print("sk =>", sk)
|
|
85
|
+
|
|
86
|
+
M = "this is a message!"
|
|
87
|
+
sig = chch.sign(pk, sk, M)
|
|
88
|
+
if debug:
|
|
89
|
+
print("Signature...")
|
|
90
|
+
print("sig =>", sig)
|
|
91
|
+
|
|
92
|
+
assert chch.verify(mpk, pk, M, sig), "invalid signature!"
|
|
93
|
+
if debug: print("Verification successful!")
|
|
94
|
+
|
|
95
|
+
if __name__ == "__main__":
|
|
96
|
+
debug = True
|
|
97
|
+
main()
|