• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #ifndef ELGAMAL_LOCAL_H
17 #define ELGAMAL_LOCAL_H
18 
19 #include "hitls_build.h"
20 #ifdef HITLS_CRYPTO_ELGAMAL
21 
22 #include "crypt_elgamal.h"
23 #include "crypt_bn.h"
24 #include "crypt_local_types.h"
25 #include "crypt_types.h"
26 #include "sal_atomic.h"
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif /* __cpluscplus */
31 
32 typedef struct {
33     BN_BigNum *p; // prime factor p
34     BN_BigNum *g; // primitive root of p
35     BN_BigNum *y; // y = g^x (mod p)
36     BN_BigNum *q; // prime factor q
37 } CRYPT_ELGAMAL_PubKey;
38 
39 typedef struct {
40     BN_BigNum *p; // prime factor p
41     BN_BigNum *g; // primitive root of g
42     BN_BigNum *x; // pub key x needed for decryption
43 } CRYPT_ELGAMAL_PrvKey;
44 
45 struct ELGAMAL_Para {
46     BN_BigNum *q; // prime factor q
47     uint32_t k_bits; // security parameter k
48     uint32_t bits; // length in bits of modulus
49 };
50 
51 struct ELGAMAL_Ctx {
52     CRYPT_ELGAMAL_PubKey *pubKey;
53     CRYPT_ELGAMAL_PrvKey *prvKey;
54     CRYPT_ELGAMAL_Para *para;
55     BSL_SAL_RefCount references;
56     void *libCtx;
57 };
58 
59 CRYPT_ELGAMAL_PrvKey *ElGamal_NewPrvKey(uint32_t bits);
60 CRYPT_ELGAMAL_PubKey *ElGamal_NewPubKey(uint32_t bits);
61 void ELGAMAL_FreePrvKey(CRYPT_ELGAMAL_PrvKey *prvKey);
62 void ELGAMAL_FreePubKey(CRYPT_ELGAMAL_PubKey *pubKey);
63 CRYPT_ELGAMAL_Para *CRYPT_ElGamal_DupPara(const CRYPT_ELGAMAL_Para *para);
64 
65 #define ELGAMAL_FREE_PRV_KEY(prvKey_)  \
66     do {                               \
67         ELGAMAL_FreePrvKey((prvKey_)); \
68         (prvKey_) = NULL;              \
69     } while (0)
70 
71 #define ELGAMAL_FREE_PUB_KEY(pubKey_)  \
72     do {                               \
73         ELGAMAL_FreePubKey((pubKey_)); \
74         (pubKey_) = NULL;              \
75     } while (0)
76 
77 #define ELGAMAL_FREE_PARA(para_)         \
78     do {                                 \
79         CRYPT_ELGAMAL_FreePara((para_)); \
80         (para_) = NULL;                  \
81     } while (0)
82 #ifdef __cplusplus
83 }
84 #endif
85 #endif // HITLS_CRYPTO_ELGAMAL
86 #endif // ELGAMAL_LOCAL_H