1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 
4 /* PKCS Header Info */
5 
6 /* ===> PKCS #1 -- RSA Cryptography <=== */
7 #ifdef LTC_PKCS_1
8 
9 enum ltc_pkcs_1_v1_5_blocks
10 {
11   LTC_PKCS_1_EMSA   = 1,        /* Block type 1 (PKCS #1 v1.5 signature padding) */
12   LTC_PKCS_1_EME    = 2         /* Block type 2 (PKCS #1 v1.5 encryption padding) */
13 };
14 
15 enum ltc_pkcs_1_paddings
16 {
17   LTC_PKCS_1_V1_5     = 1,        /* PKCS #1 v1.5 padding (\sa ltc_pkcs_1_v1_5_blocks) */
18   LTC_PKCS_1_OAEP     = 2,        /* PKCS #1 v2.0 encryption padding */
19   LTC_PKCS_1_PSS      = 3,        /* PKCS #1 v2.1 signature padding */
20   LTC_PKCS_1_V1_5_NA1 = 4         /* PKCS #1 v1.5 padding - No ASN.1 (\sa ltc_pkcs_1_v1_5_blocks) */
21 };
22 
23 int pkcs_1_mgf1(      int            hash_idx,
24                 const unsigned char *seed, unsigned long seedlen,
25                       unsigned char *mask, unsigned long masklen);
26 
27 int pkcs_1_i2osp(void *n, unsigned long modulus_len, unsigned char *out);
28 int pkcs_1_os2ip(void *n, unsigned char *in, unsigned long inlen);
29 
30 /* *** v1.5 padding */
31 int pkcs_1_v1_5_encode(const unsigned char *msg,
32                              unsigned long  msglen,
33                              int            block_type,
34                              unsigned long  modulus_bitlen,
35                                 prng_state *prng,
36                                        int  prng_idx,
37                              unsigned char *out,
38                              unsigned long *outlen);
39 
40 int pkcs_1_v1_5_decode(const unsigned char *msg,
41                              unsigned long  msglen,
42                                        int  block_type,
43                              unsigned long  modulus_bitlen,
44                              unsigned char *out,
45                              unsigned long *outlen,
46                                        int *is_valid);
47 
48 /* *** v2.1 padding */
49 int pkcs_1_oaep_encode(const unsigned char *msg,    unsigned long msglen,
50                        const unsigned char *lparam, unsigned long lparamlen,
51                              unsigned long modulus_bitlen, prng_state *prng,
52                              int           prng_idx,
53                              int           mgf_hash, int lparam_hash,
54                              unsigned char *out,    unsigned long *outlen);
55 
56 int pkcs_1_oaep_decode(const unsigned char *msg,    unsigned long msglen,
57                        const unsigned char *lparam, unsigned long lparamlen,
58                              unsigned long modulus_bitlen,
59                              int           mgf_hash, int          lparam_hash,
60                              unsigned char *out,    unsigned long *outlen,
61                              int           *res);
62 
63 int pkcs_1_pss_encode(const unsigned char *msghash, unsigned long msghashlen,
64                             unsigned long saltlen,  prng_state   *prng,
65                             int           prng_idx, int           hash_idx,
66                             unsigned long modulus_bitlen,
67                             unsigned char *out,     unsigned long *outlen);
68 
69 int pkcs_1_pss_decode(const unsigned char *msghash, unsigned long msghashlen,
70                       const unsigned char *sig,     unsigned long siglen,
71                             unsigned long saltlen,  int           hash_idx,
72                             unsigned long modulus_bitlen, int    *res);
73 
74 #endif /* LTC_PKCS_1 */
75 
76 /* ===> PKCS #5 -- Password Based Cryptography <=== */
77 #ifdef LTC_PKCS_5
78 
79 /* Algorithm #1 (PBKDF1) */
80 int pkcs_5_alg1(const unsigned char *password, unsigned long password_len,
81                 const unsigned char *salt,
82                 int iteration_count,  int hash_idx,
83                 unsigned char *out,   unsigned long *outlen);
84 
85 /* Algorithm #1 (PBKDF1) - OpenSSL-compatible variant for arbitrarily-long keys.
86    Compatible with EVP_BytesToKey() */
87 int pkcs_5_alg1_openssl(const unsigned char *password,
88                         unsigned long password_len,
89                         const unsigned char *salt,
90                         int iteration_count,  int hash_idx,
91                         unsigned char *out,   unsigned long *outlen);
92 
93 /* Algorithm #2 (PBKDF2) */
94 int pkcs_5_alg2(const unsigned char *password, unsigned long password_len,
95                 const unsigned char *salt,     unsigned long salt_len,
96                 int iteration_count,           int hash_idx,
97                 unsigned char *out,            unsigned long *outlen);
98 
99 int pkcs_5_test (void);
100 #endif  /* LTC_PKCS_5 */
101 
102