1 /* 2 * This file is part of the openHiTLS project. 3 * 4 * openHiTLS is licensed under the Mulan PSL v2. 5 * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 * You may obtain a copy of Mulan PSL v2 at: 7 * 8 * http://license.coscl.org.cn/MulanPSL2 9 * 10 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 11 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 12 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 13 * See the Mulan PSL v2 for more details. 14 */ 15 #ifndef PAILLIER_LOCAL_H 16 #define PAILLIER_LOCAL_H 17 18 #include "hitls_build.h" 19 #ifdef HITLS_CRYPTO_PAILLIER 20 21 #include "crypt_paillier.h" 22 #include "crypt_bn.h" 23 #include "crypt_local_types.h" 24 #include "crypt_types.h" 25 #include "sal_atomic.h" 26 27 #ifdef __cplusplus 28 extern "C" { 29 #endif /* __cpluscplus */ 30 31 typedef struct { 32 BN_BigNum *n; // modulo Value - converted.Not in char 33 BN_BigNum *g; // modulo Value -converted.Not in char 34 BN_BigNum *n2; // square of n 35 } CRYPT_PAILLIER_PubKey; 36 37 typedef struct { 38 BN_BigNum *n; // pub key n needed for decryption 39 BN_BigNum *lambda; // modulo Value - converted.Not in char 40 BN_BigNum *mu; // modulo Value -converted.Not in char 41 BN_BigNum *n2; // pub key n2 needed for decryption 42 } CRYPT_PAILLIER_PrvKey; 43 44 struct PAILLIER_Para { 45 BN_BigNum *p; // prime factor p 46 BN_BigNum *q; // prime factor q 47 uint32_t bits; // length in bits of modulus 48 }; 49 50 struct PAILLIER_Ctx { 51 CRYPT_PAILLIER_PubKey *pubKey; 52 CRYPT_PAILLIER_PrvKey *prvKey; 53 CRYPT_PAILLIER_Para *para; 54 BSL_SAL_RefCount references; 55 void *libCtx; 56 }; 57 58 CRYPT_PAILLIER_PrvKey *Paillier_NewPrvKey(uint32_t bits); 59 CRYPT_PAILLIER_PubKey *Paillier_NewPubKey(uint32_t bits); 60 void PAILLIER_FreePrvKey(CRYPT_PAILLIER_PrvKey *prvKey); 61 void PAILLIER_FreePubKey(CRYPT_PAILLIER_PubKey *pubKey); 62 CRYPT_PAILLIER_Para *CRYPT_Paillier_DupPara(const CRYPT_PAILLIER_Para *para); 63 64 #define PAILLIER_FREE_PRV_KEY(prvKey_) \ 65 do { \ 66 PAILLIER_FreePrvKey((prvKey_)); \ 67 (prvKey_) = NULL; \ 68 } while (0) 69 70 #define PAILLIER_FREE_PUB_KEY(pubKey_) \ 71 do { \ 72 PAILLIER_FreePubKey((pubKey_)); \ 73 (pubKey_) = NULL; \ 74 } while (0) 75 76 #define PAILLIER_FREE_PARA(para_) \ 77 do { \ 78 CRYPT_PAILLIER_FreePara((para_)); \ 79 (para_) = NULL; \ 80 } while (0) 81 82 #ifdef __cplusplus 83 } 84 #endif 85 86 #endif // HITLS_CRYPTO_PAILLIER 87 #endif // PAILLIER_LOCAL_H