xref: /xnu-10063.121.3/EXTERNAL_HEADERS/corecrypto/ccrsa.h (revision 2c2f96dc2b9a4408a43d3150ae9c105355ca3daa)
1*2c2f96dcSApple OSS Distributions /* Copyright (c) (2010-2012,2014-2022) Apple Inc. All rights reserved.
2*2c2f96dcSApple OSS Distributions  *
3*2c2f96dcSApple OSS Distributions  * corecrypto is licensed under Apple Inc.’s Internal Use License Agreement (which
4*2c2f96dcSApple OSS Distributions  * is contained in the License.txt file distributed with corecrypto) and only to
5*2c2f96dcSApple OSS Distributions  * people who accept that license. IMPORTANT:  Any license rights granted to you by
6*2c2f96dcSApple OSS Distributions  * Apple Inc. (if any) are limited to internal use within your organization only on
7*2c2f96dcSApple OSS Distributions  * devices and computers you own or control, for the sole purpose of verifying the
8*2c2f96dcSApple OSS Distributions  * security characteristics and correct functioning of the Apple Software.  You may
9*2c2f96dcSApple OSS Distributions  * not, directly or indirectly, redistribute the Apple Software or any portions thereof.
10*2c2f96dcSApple OSS Distributions  */
11*2c2f96dcSApple OSS Distributions 
12*2c2f96dcSApple OSS Distributions #ifndef _CORECRYPTO_CCRSA_H_
13*2c2f96dcSApple OSS Distributions #define _CORECRYPTO_CCRSA_H_
14*2c2f96dcSApple OSS Distributions 
15*2c2f96dcSApple OSS Distributions #include <corecrypto/cc.h>
16*2c2f96dcSApple OSS Distributions #include <corecrypto/ccdigest.h>
17*2c2f96dcSApple OSS Distributions #include <corecrypto/ccrng.h>
18*2c2f96dcSApple OSS Distributions #include <corecrypto/cczp.h>
19*2c2f96dcSApple OSS Distributions #include <corecrypto/cc_fault_canary.h>
20*2c2f96dcSApple OSS Distributions 
21*2c2f96dcSApple OSS Distributions CC_PTRCHECK_CAPABLE_HEADER()
22*2c2f96dcSApple OSS Distributions 
23*2c2f96dcSApple OSS Distributions // Apple does not generate keys of greater than 4096 bits
24*2c2f96dcSApple OSS Distributions // This limit is relaxed to accommodate potential third-party consumers
25*2c2f96dcSApple OSS Distributions #define CCRSA_KEYGEN_MAX_NBITS 8192
26*2c2f96dcSApple OSS Distributions 
27*2c2f96dcSApple OSS Distributions struct ccrsa_full_ctx {
28*2c2f96dcSApple OSS Distributions     __CCZP_ELEMENTS_DEFINITIONS(pb_)
29*2c2f96dcSApple OSS Distributions } CC_ALIGNED(CCN_UNIT_SIZE);
30*2c2f96dcSApple OSS Distributions 
31*2c2f96dcSApple OSS Distributions struct ccrsa_pub_ctx {
32*2c2f96dcSApple OSS Distributions     __CCZP_ELEMENTS_DEFINITIONS(pb_)
33*2c2f96dcSApple OSS Distributions } CC_ALIGNED(CCN_UNIT_SIZE);
34*2c2f96dcSApple OSS Distributions 
35*2c2f96dcSApple OSS Distributions typedef struct ccrsa_full_ctx* ccrsa_full_ctx_t;
36*2c2f96dcSApple OSS Distributions typedef struct ccrsa_pub_ctx* ccrsa_pub_ctx_t;
37*2c2f96dcSApple OSS Distributions 
38*2c2f96dcSApple OSS Distributions /*
39*2c2f96dcSApple OSS Distributions  struct ccrsa_pub_ctx {
40*2c2f96dcSApple OSS Distributions      cczp zm;
41*2c2f96dcSApple OSS Distributions      cc_unit m[n];
42*2c2f96dcSApple OSS Distributions      cc_unit m0inv;
43*2c2f96dcSApple OSS Distributions      cc_unit mr2[n];
44*2c2f96dcSApple OSS Distributions      cc_unit e[n];
45*2c2f96dcSApple OSS Distributions  }
46*2c2f96dcSApple OSS Distributions 
47*2c2f96dcSApple OSS Distributions  struct ccrsa_priv_ctx {
48*2c2f96dcSApple OSS Distributions      cc_unit d[n];        // e^(-1) mod lcm(p-1, q-1)
49*2c2f96dcSApple OSS Distributions 
50*2c2f96dcSApple OSS Distributions      cczp zp;
51*2c2f96dcSApple OSS Distributions      cc_unit p[n/2+1];
52*2c2f96dcSApple OSS Distributions      cc_unit p0inv;
53*2c2f96dcSApple OSS Distributions      cc_unit pr2[n/2+1];
54*2c2f96dcSApple OSS Distributions 
55*2c2f96dcSApple OSS Distributions      cczp zq;
56*2c2f96dcSApple OSS Distributions      cc_unit q[n/2+1];
57*2c2f96dcSApple OSS Distributions      cc_unit q0inv;
58*2c2f96dcSApple OSS Distributions      cc_unit qr2[n/2+1];
59*2c2f96dcSApple OSS Distributions 
60*2c2f96dcSApple OSS Distributions      cc_unit dp[n/2+1];   // d mod (p-1)
61*2c2f96dcSApple OSS Distributions      cc_unit dq[n/2+1];   // d mod (q-1)
62*2c2f96dcSApple OSS Distributions      cc_unit qinv[n/2+1]; // q^(-1) mod p
63*2c2f96dcSApple OSS Distributions  }
64*2c2f96dcSApple OSS Distributions 
65*2c2f96dcSApple OSS Distributions  struct ccrsa_full_ctx {
66*2c2f96dcSApple OSS Distributions      struct ccrsa_pub_ctx;
67*2c2f96dcSApple OSS Distributions      struct ccrsa_priv_ctx;
68*2c2f96dcSApple OSS Distributions  }
69*2c2f96dcSApple OSS Distributions  */
70*2c2f96dcSApple OSS Distributions 
71*2c2f96dcSApple OSS Distributions // Compute the internal structure size in bytes based on key (i.e. modulus) byte size.
72*2c2f96dcSApple OSS Distributions #define ccrsa_pub_ctx_size(_nbytes_)   (sizeof(struct cczp) + CCN_UNIT_SIZE + 3 * ccn_sizeof_size(_nbytes_))
73*2c2f96dcSApple OSS Distributions #define ccrsa_priv_ctx_size(_nbytes_)  (ccn_sizeof_size(_nbytes_) + (sizeof(struct cczp) + CCN_UNIT_SIZE) * 2 + 7 * ccn_sizeof_n(ccn_nof_size(_nbytes_) / 2 + 1))
74*2c2f96dcSApple OSS Distributions #define ccrsa_full_ctx_size(_nbytes_)  (ccrsa_pub_ctx_size(_nbytes_) + ccrsa_priv_ctx_size(_nbytes_))
75*2c2f96dcSApple OSS Distributions 
76*2c2f96dcSApple OSS Distributions #define ccrsa_pub_ctx_ws(_n_) ccn_nof_size(ccrsa_pub_ctx_size(ccn_sizeof_n(_n_)))
77*2c2f96dcSApple OSS Distributions #define ccrsa_full_ctx_ws(_n_) ccn_nof_size(ccrsa_full_ctx_size(ccn_sizeof_n(_n_)))
78*2c2f96dcSApple OSS Distributions 
79*2c2f96dcSApple OSS Distributions // Declare structure based on key byte size.
80*2c2f96dcSApple OSS Distributions #define ccrsa_full_ctx_decl(_nbytes_, _name_)   cc_ctx_decl(struct ccrsa_full_ctx, ccrsa_full_ctx_size(_nbytes_), _name_)
81*2c2f96dcSApple OSS Distributions #define ccrsa_full_ctx_clear(_nbytes_, _name_)  cc_clear(ccrsa_full_ctx_size(_nbytes_), _name_)
82*2c2f96dcSApple OSS Distributions #define ccrsa_pub_ctx_decl(_nbytes_, _name_)    cc_ctx_decl(struct ccrsa_pub_ctx, ccrsa_pub_ctx_size(_nbytes_), _name_)
83*2c2f96dcSApple OSS Distributions #define ccrsa_pub_ctx_clear(_nbytes_, _name_)   cc_clear(ccrsa_pub_ctx_size(_nbytes_), _name_)
84*2c2f96dcSApple OSS Distributions 
85*2c2f96dcSApple OSS Distributions // Declare structure based on key bit size.
86*2c2f96dcSApple OSS Distributions #define ccrsa_full_ctx_decl_nbits(_nbits_, _name_)   cc_ctx_decl(struct ccrsa_full_ctx, ccrsa_full_ctx_size(ccn_sizeof(_nbits_)), _name_)
87*2c2f96dcSApple OSS Distributions #define ccrsa_full_ctx_clear_nbits(_nbits_, _name_)  cc_clear(ccrsa_full_ctx_size(ccn_sizeof(_nbits_)), _name_)
88*2c2f96dcSApple OSS Distributions #define ccrsa_pub_ctx_decl_nbits(_nbits_, _name_)    cc_ctx_decl(struct ccrsa_pub_ctx, ccrsa_pub_ctx_size(ccn_sizeof(_nbits_)), _name_)
89*2c2f96dcSApple OSS Distributions #define ccrsa_pub_ctx_clear_nbits(_nbits_, _name_)   cc_clear(ccrsa_pub_ctx_size(ccn_sizeof(_nbits_)), _name_)
90*2c2f96dcSApple OSS Distributions 
91*2c2f96dcSApple OSS Distributions // Declare structure based number of cc_units. Not a typical use case. Size depends on processor.
92*2c2f96dcSApple OSS Distributions #define ccrsa_full_ctx_decl_n(_nunits_, _name_)   cc_ctx_decl(struct ccrsa_full_ctx, ccrsa_full_ctx_size(ccn_sizeof_n(_nunits_)), _name_)
93*2c2f96dcSApple OSS Distributions #define ccrsa_full_ctx_clear_n(_nunits_, _name_)  cc_clear(ccrsa_full_ctx_size(ccn_sizeof_n(_nunits_)), _name_)
94*2c2f96dcSApple OSS Distributions #define ccrsa_pub_ctx_decl_n(_nunits_, _name_)    cc_ctx_decl(struct ccrsa_pub_ctx, ccrsa_pub_ctx_size(ccn_sizeof_n(_nunits_)), _name_)
95*2c2f96dcSApple OSS Distributions #define ccrsa_pub_ctx_clear_n(_nunits_, _name_)   cc_clear(ccrsa_pub_ctx_size(ccn_sizeof_n(_nunits_)), _name_)
96*2c2f96dcSApple OSS Distributions 
97*2c2f96dcSApple OSS Distributions // Accessors to ccrsa full and public key fields. */
98*2c2f96dcSApple OSS Distributions // The offsets are computed using pb_ccn. If any object other than ccrsa_full_ctx_t
99*2c2f96dcSApple OSS Distributions // or ccrsa_pub_ctx_t is passed to the macros, compiler error is generated.
100*2c2f96dcSApple OSS Distributions 
101*2c2f96dcSApple OSS Distributions 
102*2c2f96dcSApple OSS Distributions 
103*2c2f96dcSApple OSS Distributions #define ccrsa_ctx_zm(_ctx_)        ((struct cczp* cc_single)(_ctx_))
104*2c2f96dcSApple OSS Distributions #define ccrsa_ctx_n(_ctx_)         (ccrsa_ctx_zm(_ctx_)->n)
105*2c2f96dcSApple OSS Distributions 
106*2c2f96dcSApple OSS Distributions #define ccrsa_ctx_m(_ctx_)         ((cc_unit *)cc_unsafe_forge_bidi_indexable((_ctx_)->pb_ccn, ccn_sizeof_n(ccrsa_ctx_n(_ctx_))))
107*2c2f96dcSApple OSS Distributions 
108*2c2f96dcSApple OSS Distributions // Forge a bixi indexable at (_ctx_->pb_cnn + _offset_), of size in bytes _nbytes_.
109*2c2f96dcSApple OSS Distributions #define ccrsa_unsafe_forge_bidi_indexable(_ctx_, _nbytes_, _offset_) ((cc_unit *)cc_unsafe_forge_bidi_indexable((cc_unit *)cc_unsafe_forge_bidi_indexable((_ctx_)->pb_ccn, _nbytes_ + ccn_sizeof_n(_offset_)) + _offset_, _nbytes_))
110*2c2f96dcSApple OSS Distributions 
111*2c2f96dcSApple OSS Distributions #define ccrsa_ctx_e(_ctx_)         ccrsa_unsafe_forge_bidi_indexable(_ctx_, ccn_sizeof_n(ccrsa_ctx_n(_ctx_)), 2 * ccrsa_ctx_n(_ctx_) + 1)
112*2c2f96dcSApple OSS Distributions #define ccrsa_ctx_d(_ctx_)         ccrsa_unsafe_forge_bidi_indexable(_ctx_, ccn_sizeof_n(ccrsa_ctx_n(_ctx_)), 3 * ccrsa_ctx_n(_ctx_) + 1)
113*2c2f96dcSApple OSS Distributions 
114*2c2f96dcSApple OSS Distributions 
115*2c2f96dcSApple OSS Distributions 
116*2c2f96dcSApple OSS Distributions // accessors to ccrsa private key fields
117*2c2f96dcSApple OSS Distributions #define ccrsa_ctx_private_zq(FK)   ((cczp_t)(ccrsa_ctx_private_zp(FK)->ccn + 2 * ccrsa_ctx_private_zp(FK)->n + 1))
118*2c2f96dcSApple OSS Distributions #define ccrsa_ctx_private_dp(FK)   (ccrsa_ctx_private_zq(FK)->ccn + 2 * ccrsa_ctx_private_zp(FK)->n + 1)
119*2c2f96dcSApple OSS Distributions #define ccrsa_ctx_private_dq(FK)   (ccrsa_ctx_private_dp(FK) + ccrsa_ctx_private_zp(FK)->n)
120*2c2f96dcSApple OSS Distributions #define ccrsa_ctx_private_qinv(FK) (ccrsa_ctx_private_dq(FK) + ccrsa_ctx_private_zp(FK)->n)
121*2c2f96dcSApple OSS Distributions 
122*2c2f96dcSApple OSS Distributions cczp_t ccrsa_ctx_private_zp(ccrsa_full_ctx_t fk);
123*2c2f96dcSApple OSS Distributions 
124*2c2f96dcSApple OSS Distributions /*!
125*2c2f96dcSApple OSS Distributions  @function   ccrsa_ctx_public
126*2c2f96dcSApple OSS Distributions  @abstract   gets the public key from full key
127*2c2f96dcSApple OSS Distributions  @param      fk      RSA full key
128*2c2f96dcSApple OSS Distributions  @result     Returns RSA public ker
129*2c2f96dcSApple OSS Distributions  */
130*2c2f96dcSApple OSS Distributions 
131*2c2f96dcSApple OSS Distributions ccrsa_pub_ctx_t ccrsa_ctx_public(ccrsa_full_ctx_t fk);
132*2c2f96dcSApple OSS Distributions 
133*2c2f96dcSApple OSS Distributions /*!
134*2c2f96dcSApple OSS Distributions @function   ccrsa_pubkeylength
135*2c2f96dcSApple OSS Distributions @abstract   Compute the actual bit length of the RSA key (bit length of the modulus)
136*2c2f96dcSApple OSS Distributions @param      pubk  An initialized RSA public key
137*2c2f96dcSApple OSS Distributions @result     bit length of the RSA key
138*2c2f96dcSApple OSS Distributions */
139*2c2f96dcSApple OSS Distributions CC_NONNULL_ALL
140*2c2f96dcSApple OSS Distributions size_t ccrsa_pubkeylength(ccrsa_pub_ctx_t pubk);
141*2c2f96dcSApple OSS Distributions 
142*2c2f96dcSApple OSS Distributions /* PKCS1 pad_markers */
143*2c2f96dcSApple OSS Distributions #define CCRSA_PKCS1_PAD_SIGN     1
144*2c2f96dcSApple OSS Distributions #define CCRSA_PKCS1_PAD_ENCRYPT  2
145*2c2f96dcSApple OSS Distributions 
146*2c2f96dcSApple OSS Distributions /*!
147*2c2f96dcSApple OSS Distributions @function   ccrsa_init_pub
148*2c2f96dcSApple OSS Distributions @abstract   Initialize an RSA public key structure based on modulus and exponent. Values are copied into the structure.
149*2c2f96dcSApple OSS Distributions @param      pubk   allocated public key structure (see requirements below)
150*2c2f96dcSApple OSS Distributions @param      modulus  cc_unit array of the modulus
151*2c2f96dcSApple OSS Distributions @param      exponent  cc_unit array of the exponent
152*2c2f96dcSApple OSS Distributions @result     CCERR_OK if no error
153*2c2f96dcSApple OSS Distributions 
154*2c2f96dcSApple OSS Distributions @discussion ccrsa_ctx_n(pubk) must have been initialized based on the modulus size, typically using ccn_nof_size(mod_nbytes).
155*2c2f96dcSApple OSS Distributions  The public key structure pubk is typically allocated with ccrsa_pub_ctx_decl(mod_nbytes, pubk);
156*2c2f96dcSApple OSS Distributions */
157*2c2f96dcSApple OSS Distributions CC_NONNULL_ALL
158*2c2f96dcSApple OSS Distributions int ccrsa_init_pub(ccrsa_pub_ctx_t pubk, const cc_unit *modulus,
159*2c2f96dcSApple OSS Distributions                     const cc_unit *exponent);
160*2c2f96dcSApple OSS Distributions 
161*2c2f96dcSApple OSS Distributions /*! @function ccrsa_make_priv
162*2c2f96dcSApple OSS Distributions   @abstract   Initializes an RSA public and private key given the public
163*2c2f96dcSApple OSS Distributions               exponent e and prime factors p and q.
164*2c2f96dcSApple OSS Distributions 
165*2c2f96dcSApple OSS Distributions   @param      full_ctx   Initialized context with ccrsa_ctx_n(full_ctx) set to 2*ccn_nof_size(p_nbytes)
166*2c2f96dcSApple OSS Distributions   @param      e_nbytes   Number of bytes of public exponent e.
167*2c2f96dcSApple OSS Distributions   @param      e_bytes    Public exponent e in Big Endian.
168*2c2f96dcSApple OSS Distributions   @param      p_nbytes   Number of bytes of prime factor p.
169*2c2f96dcSApple OSS Distributions   @param      p_bytes    Prime factor p in Big Endian.
170*2c2f96dcSApple OSS Distributions   @param      q_nbytes   Number of bytes of prime factor q.
171*2c2f96dcSApple OSS Distributions   @param      q_bytes    Prime factor q in Big Endian.
172*2c2f96dcSApple OSS Distributions 
173*2c2f96dcSApple OSS Distributions   @return     0          iff successful.
174*2c2f96dcSApple OSS Distributions 
175*2c2f96dcSApple OSS Distributions   @discussion  ccrsa_ctx_n(full_ctx) must already be set to 2*ccn_nof_size(p_mbytes), with the expectation that p_nbytes>q_nbytes.
176*2c2f96dcSApple OSS Distributions   e is the public exponent, and e_nbytes<= 2*p_nbytes.
177*2c2f96dcSApple OSS Distributions   The output is a fully formed RSA context with N=pq, d=e^{-1} mod lambda(N), and appropriate inverses of different associated values precomputed
178*2c2f96dcSApple OSS Distributions   to speed computation.
179*2c2f96dcSApple OSS Distributions */
180*2c2f96dcSApple OSS Distributions int ccrsa_make_priv(ccrsa_full_ctx_t full_ctx,
181*2c2f96dcSApple OSS Distributions                     size_t e_nbytes, const uint8_t *cc_counted_by(e_nbytes) e_bytes,
182*2c2f96dcSApple OSS Distributions                     size_t p_nbytes, const uint8_t *cc_counted_by(p_nbytes) p_bytes,
183*2c2f96dcSApple OSS Distributions                     size_t q_nbytes, const uint8_t *cc_counted_by(q_nbytes) q_bytes);
184*2c2f96dcSApple OSS Distributions 
185*2c2f96dcSApple OSS Distributions /*! @function ccrsa_recover_priv
186*2c2f96dcSApple OSS Distributions   @abstract   Initializes an RSA public and private key given the modulus m,
187*2c2f96dcSApple OSS Distributions               the public exponent e and the private exponent d.
188*2c2f96dcSApple OSS Distributions 
189*2c2f96dcSApple OSS Distributions   @discussion Follows the algorithm described by
190*2c2f96dcSApple OSS Distributions               NIST SP 800-56B, Appendix C, "Prime Factory Recovery".
191*2c2f96dcSApple OSS Distributions 
192*2c2f96dcSApple OSS Distributions   @param      full_ctx   Initialized context with ccrsa_ctx_n(full_ctx) set to ccn_nof_size(m_nbytes)
193*2c2f96dcSApple OSS Distributions   @param      m_nbytes   Number of bytes of modulus m.
194*2c2f96dcSApple OSS Distributions   @param      m_bytes    Modulus m in Big Endian.
195*2c2f96dcSApple OSS Distributions   @param      e_nbytes   Number of bytes of public exponent e.
196*2c2f96dcSApple OSS Distributions   @param      e_bytes    Public exponent e in Big Endian.
197*2c2f96dcSApple OSS Distributions   @param      d_nbytes   Number of bytes of private exponent d.
198*2c2f96dcSApple OSS Distributions   @param      d_bytes    Private exponent d in Big Endian.
199*2c2f96dcSApple OSS Distributions   @param      rng        RNG instance.
200*2c2f96dcSApple OSS Distributions 
201*2c2f96dcSApple OSS Distributions   @return     0          iff successful.
202*2c2f96dcSApple OSS Distributions */
203*2c2f96dcSApple OSS Distributions int ccrsa_recover_priv(ccrsa_full_ctx_t full_ctx,
204*2c2f96dcSApple OSS Distributions                        size_t m_nbytes, const uint8_t *cc_counted_by(m_nbytes) m_bytes,
205*2c2f96dcSApple OSS Distributions                        size_t e_nbytes, const uint8_t *cc_counted_by(e_nbytes) e_bytes,
206*2c2f96dcSApple OSS Distributions                        size_t d_nbytes, const uint8_t *cc_counted_by(d_nbytes) d_bytes,
207*2c2f96dcSApple OSS Distributions                        struct ccrng_state *rng);
208*2c2f96dcSApple OSS Distributions 
209*2c2f96dcSApple OSS Distributions /*!
210*2c2f96dcSApple OSS Distributions @function   ccrsa_make_pub
211*2c2f96dcSApple OSS Distributions @abstract   Initialize public key based on modulus and public exponent  as big endian byte arrays;
212*2c2f96dcSApple OSS Distributions 
213*2c2f96dcSApple OSS Distributions @param      pubk   allocated public key structure (see requirements below)
214*2c2f96dcSApple OSS Distributions @param      exp_nbytes Number of bytes in big endian exponent.
215*2c2f96dcSApple OSS Distributions @param      exp     Pointer to big endian exponent e (may have leading 0's).
216*2c2f96dcSApple OSS Distributions @param      mod_nbytes  Number of bytes in big endian modulus.
217*2c2f96dcSApple OSS Distributions @param      mod     Pointer to big endian to rsa modulus  N.
218*2c2f96dcSApple OSS Distributions @result     0    iff successful.
219*2c2f96dcSApple OSS Distributions 
220*2c2f96dcSApple OSS Distributions @discussion ccrsa_ctx_n(pubk) must have been initialized based on the modulus size, typically using ccn_nof_size(mod_nbytes).
221*2c2f96dcSApple OSS Distributions     The public key structure pubk is typically allocated with ccrsa_pub_ctx_decl(mod_nbytes, pubk);
222*2c2f96dcSApple OSS Distributions */
223*2c2f96dcSApple OSS Distributions 
224*2c2f96dcSApple OSS Distributions CC_NONNULL((1, 3, 5))
225*2c2f96dcSApple OSS Distributions int ccrsa_make_pub(ccrsa_pub_ctx_t pubk,
226*2c2f96dcSApple OSS Distributions                    size_t exp_nbytes, const uint8_t *cc_counted_by(exp_nbytes) exp,
227*2c2f96dcSApple OSS Distributions                    size_t mod_nbytes, const uint8_t *cc_counted_by(mod_nbytes) mod);
228*2c2f96dcSApple OSS Distributions 
229*2c2f96dcSApple OSS Distributions /*!
230*2c2f96dcSApple OSS Distributions @function   ccrsa_pub_crypt
231*2c2f96dcSApple OSS Distributions @abstract   Perform an RSA public key operation: (in)^e mod m
232*2c2f96dcSApple OSS Distributions @param      key   initialized public key defining e and m
233*2c2f96dcSApple OSS Distributions @param      out   result of the operation, at least ccrsa_key_n(key) cc_units must have been allocated
234*2c2f96dcSApple OSS Distributions @param      in     base of the exponentiation, of size ccrsa_key_n(key)
235*2c2f96dcSApple OSS Distributions @result     CCERR_OK if no error
236*2c2f96dcSApple OSS Distributions 
237*2c2f96dcSApple OSS Distributions @discussion Input to this function must not be secrets as the execution flow may expose their values
238*2c2f96dcSApple OSS Distributions         Clients can use ccn_read_uint() to convert bytes to cc_units to use for this API.
239*2c2f96dcSApple OSS Distributions */
240*2c2f96dcSApple OSS Distributions CC_NONNULL((1, 2, 3))
241*2c2f96dcSApple OSS Distributions int ccrsa_pub_crypt(ccrsa_pub_ctx_t key, cc_unit *cc_unsafe_indexable out, const cc_unit *cc_unsafe_indexable in);
242*2c2f96dcSApple OSS Distributions 
243*2c2f96dcSApple OSS Distributions /*!
244*2c2f96dcSApple OSS Distributions @function   ccrsa_generate_key
245*2c2f96dcSApple OSS Distributions @abstract   Generate a nbit RSA key pair.
246*2c2f96dcSApple OSS Distributions 
247*2c2f96dcSApple OSS Distributions @param      nbits      Bit size requested for the key
248*2c2f96dcSApple OSS Distributions @param      fk         Allocated context where the generated key will be stored
249*2c2f96dcSApple OSS Distributions @param      e_nbytes   Byte size of the input public exponent
250*2c2f96dcSApple OSS Distributions @param      e_bytes    Input public exponent in big endian. Recommend value is {0x01, 0x00, 0x01}
251*2c2f96dcSApple OSS Distributions @param      rng        Random Number generator used.
252*2c2f96dcSApple OSS Distributions @result     CCERR_OK if no error
253*2c2f96dcSApple OSS Distributions 
254*2c2f96dcSApple OSS Distributions @discussion
255*2c2f96dcSApple OSS Distributions     fk should be allocated using ccrsa_full_ctx_decl_nbits(nbits, fk).
256*2c2f96dcSApple OSS Distributions     The unsigned big endian byte array exponent e of length e_size is used as the exponent. It's an error to call this function with an exponent larger than nbits
257*2c2f96dcSApple OSS Distributions */
258*2c2f96dcSApple OSS Distributions CC_NONNULL_ALL
259*2c2f96dcSApple OSS Distributions int ccrsa_generate_key(size_t nbits, ccrsa_full_ctx_t fk,
260*2c2f96dcSApple OSS Distributions                        size_t e_nbytes, const void *cc_sized_by(e_nbytes) e_bytes, struct ccrng_state *rng) CC_WARN_RESULT;
261*2c2f96dcSApple OSS Distributions 
262*2c2f96dcSApple OSS Distributions /*!
263*2c2f96dcSApple OSS Distributions @function   ccrsa_generate_key_deterministic
264*2c2f96dcSApple OSS Distributions @abstract   Generate a deterministic nbit RSA key pair.
265*2c2f96dcSApple OSS Distributions 
266*2c2f96dcSApple OSS Distributions @param      nbits          Bit size requested for the key.
267*2c2f96dcSApple OSS Distributions @param      fk             Allocated context where the generated key will be stored.
268*2c2f96dcSApple OSS Distributions @param      e_nbytes       Byte length of public exponent.
269*2c2f96dcSApple OSS Distributions @param      e              Public exponent in big endian. Recommend value is {0x01, 0x00, 0x01}.
270*2c2f96dcSApple OSS Distributions @param      entropy_nbytes Byte length of entropy.
271*2c2f96dcSApple OSS Distributions @param      entropy        Entropy, initial DRBG seed.
272*2c2f96dcSApple OSS Distributions @param      nonce_nbytes   Byte length of nonce.
273*2c2f96dcSApple OSS Distributions @param      nonce          Unique value combined with the entropy/seed.
274*2c2f96dcSApple OSS Distributions @param      flags          Bitmap for options.
275*2c2f96dcSApple OSS Distributions                            (Only CCRSA_GENKEY_DETERMINISTIC_LEGACY currently supported.)
276*2c2f96dcSApple OSS Distributions @param      rng            Random Number generator for primality testing.
277*2c2f96dcSApple OSS Distributions @result     CCERR_OK if no error
278*2c2f96dcSApple OSS Distributions 
279*2c2f96dcSApple OSS Distributions @discussion
280*2c2f96dcSApple OSS Distributions     fk should be allocated using ccrsa_full_ctx_decl_nbits(nbits, fk).
281*2c2f96dcSApple OSS Distributions     The unsigned big endian byte array exponent e of length e_size is used as the exponent. It's an error to call this function with an exponent larger than nbits
282*2c2f96dcSApple OSS Distributions */
283*2c2f96dcSApple OSS Distributions CC_NONNULL((2, 4, 6, 10))
284*2c2f96dcSApple OSS Distributions int ccrsa_generate_key_deterministic(size_t nbits, ccrsa_full_ctx_t fk,
285*2c2f96dcSApple OSS Distributions                                      size_t e_nbytes, const uint8_t *cc_sized_by(e_nbytes) e,
286*2c2f96dcSApple OSS Distributions                                      size_t entropy_nbytes, const uint8_t *cc_sized_by(entropy_nbytes) entropy,
287*2c2f96dcSApple OSS Distributions                                      size_t nonce_nbytes, const uint8_t *cc_sized_by(nonce_nbytes) nonce,
288*2c2f96dcSApple OSS Distributions                                      uint32_t flags,
289*2c2f96dcSApple OSS Distributions                                      struct ccrng_state *rng);
290*2c2f96dcSApple OSS Distributions 
291*2c2f96dcSApple OSS Distributions #define CCRSA_GENKEY_DETERMINISTIC_LEGACY 0b1
292*2c2f96dcSApple OSS Distributions 
293*2c2f96dcSApple OSS Distributions /*!
294*2c2f96dcSApple OSS Distributions @function   ccrsa_generate_fips186_key
295*2c2f96dcSApple OSS Distributions @abstract   Generate a nbit RSA key pair in conformance with FIPS186-4 standard.
296*2c2f96dcSApple OSS Distributions 
297*2c2f96dcSApple OSS Distributions @param      nbits      Bit size requested for the key
298*2c2f96dcSApple OSS Distributions @param      fk         Allocated context where the generated key will be stored
299*2c2f96dcSApple OSS Distributions @param      e_nbytes   Byte size of the input public exponent
300*2c2f96dcSApple OSS Distributions @param      e_bytes    Input public exponent in big endian. Recommend value is {0x01, 0x00, 0x01}
301*2c2f96dcSApple OSS Distributions @param      rng        Random Number generator used for p and q
302*2c2f96dcSApple OSS Distributions @param      rng_mr     Random Number generator only used for the primality check
303*2c2f96dcSApple OSS Distributions @result     CCERR_OK if no error
304*2c2f96dcSApple OSS Distributions 
305*2c2f96dcSApple OSS Distributions @discussion
306*2c2f96dcSApple OSS Distributions    fk should be allocated using ccrsa_full_ctx_decl_nbits(nbits, fk).
307*2c2f96dcSApple OSS Distributions    rng and rng_mr shoud be set to the same value. The distinction is only relevant for testing
308*2c2f96dcSApple OSS Distributions */
309*2c2f96dcSApple OSS Distributions CC_NONNULL_ALL int
310*2c2f96dcSApple OSS Distributions ccrsa_generate_fips186_key(size_t nbits, ccrsa_full_ctx_t fk,
311*2c2f96dcSApple OSS Distributions                            size_t e_nbytes, const void *cc_sized_by(e_nbytes) e_bytes,
312*2c2f96dcSApple OSS Distributions                            struct ccrng_state *rng, struct ccrng_state *rng_mr) CC_WARN_RESULT;
313*2c2f96dcSApple OSS Distributions 
314*2c2f96dcSApple OSS Distributions /*
315*2c2f96dcSApple OSS Distributions  Signing and Verification algorithms
316*2c2f96dcSApple OSS Distributions */
317*2c2f96dcSApple OSS Distributions 
318*2c2f96dcSApple OSS Distributions /*!
319*2c2f96dcSApple OSS Distributions @function ccrsa_sign_pss
320*2c2f96dcSApple OSS Distributions 
321*2c2f96dcSApple OSS Distributions @brief  ccrsa_sign_pss() generates RSASSA-PSS signature in PKCS1-V2 format given an input digest
322*2c2f96dcSApple OSS Distributions 
323*2c2f96dcSApple OSS Distributions @param  key               The RSA key
324*2c2f96dcSApple OSS Distributions @param  hashAlgorithm    The hash algorithm used to generate mHash from the original message. It is also used inside the PSS encoding function.
325*2c2f96dcSApple OSS Distributions @param  MgfHashAlgorithm The hash algorithm for thr mask generation function
326*2c2f96dcSApple OSS Distributions @param  rng              Random number geberator to generate salt in PSS encoding
327*2c2f96dcSApple OSS Distributions @param  salt_nbytes     Intended length of the salt
328*2c2f96dcSApple OSS Distributions @param  digest_nbytes   Length of message hash . Must be equal to hashAlgorithm->output_size
329*2c2f96dcSApple OSS Distributions @param  digest           The input that needs to be signed. This is the hash of message M with length of hLen
330*2c2f96dcSApple OSS Distributions @param  sig_nbytes       The length of generated signature in bytes, which equals the size of the RSA modulus.
331*2c2f96dcSApple OSS Distributions @param  sig               The signature output
332*2c2f96dcSApple OSS Distributions @return 0:ok, non-zero:error
333*2c2f96dcSApple OSS Distributions 
334*2c2f96dcSApple OSS Distributions @discussion
335*2c2f96dcSApple OSS Distributions   note that in RSASSA-PSS, salt length is part of the signature as specified in ASN1
336*2c2f96dcSApple OSS Distributions   RSASSA-PSS-params ::= SEQUENCE {
337*2c2f96dcSApple OSS Distributions   hashAlgorithm      [0] HashAlgorithm      DEFAULT sha1,
338*2c2f96dcSApple OSS Distributions   maskGenAlgorithm   [1] MaskGenAlgorithm   DEFAULT mgf1SHA1,
339*2c2f96dcSApple OSS Distributions   saltLength         [2] INTEGER            DEFAULT 20,
340*2c2f96dcSApple OSS Distributions   trailerField       [3] TrailerField       DEFAULT trailerFieldBC
341*2c2f96dcSApple OSS Distributions 
342*2c2f96dcSApple OSS Distributions   • If nlen = 1024 bits (i.e., 128 bytes), and the output length of the approved hash function output block is 512 bits (i.e., 64 bytes), then the length (in bytes) of the salt (sLen) shall satisfy 0 ≤ sLen ≤ hLen – 2,
343*2c2f96dcSApple OSS Distributions   • Otherwise, the length (in bytes) of the salt (sLen) shall satisfy 0 ≤ sLen ≤ hLen, where hLen is the length of the hash function output block (in bytes).
344*2c2f96dcSApple OSS Distributions  */
345*2c2f96dcSApple OSS Distributions CC_NONNULL((1, 2, 3, 5, 7, 8, 9))
346*2c2f96dcSApple OSS Distributions int ccrsa_sign_pss(ccrsa_full_ctx_t key,
347*2c2f96dcSApple OSS Distributions                    const struct ccdigest_info* hashAlgorithm, const struct ccdigest_info* MgfHashAlgorithm,
348*2c2f96dcSApple OSS Distributions                    size_t salt_nbytes, struct ccrng_state *rng,
349*2c2f96dcSApple OSS Distributions                    size_t digest_nbytes, const uint8_t *cc_counted_by(digest_nbytes) digest,
350*2c2f96dcSApple OSS Distributions                    size_t *sig_nbytes, uint8_t *cc_unsafe_indexable sig);
351*2c2f96dcSApple OSS Distributions 
352*2c2f96dcSApple OSS Distributions /*!
353*2c2f96dcSApple OSS Distributions @function ccrsa_sign_pss_msg
354*2c2f96dcSApple OSS Distributions 
355*2c2f96dcSApple OSS Distributions @brief  ccrsa_sign_pss_msg() generates a RSASSA-PSS signature in PKCS1-V2 format given an input message
356*2c2f96dcSApple OSS Distributions 
357*2c2f96dcSApple OSS Distributions @param  key               The RSA key
358*2c2f96dcSApple OSS Distributions @param  hashAlgorithm     The hash algorithm used to generate mHash from the input message. It is also used inside the PSS encoding function.
359*2c2f96dcSApple OSS Distributions @param  MgfHashAlgorithm  The hash algorithm for thr mask generation function
360*2c2f96dcSApple OSS Distributions @param  rng               Random number generator to generate salt in PSS encoding
361*2c2f96dcSApple OSS Distributions @param  salt_nbytes       Intended length of the salt
362*2c2f96dcSApple OSS Distributions @param  msg_nbytes        Length of message.
363*2c2f96dcSApple OSS Distributions @param  msg               The input that needs to be signed. This will be hashed using `hashAlgorithm`
364*2c2f96dcSApple OSS Distributions @param  sig_nbytes        The length of generated signature in bytes, which equals the size of the RSA modulus.
365*2c2f96dcSApple OSS Distributions @param  sig               The signature output
366*2c2f96dcSApple OSS Distributions @return 0:ok, non-zero:error
367*2c2f96dcSApple OSS Distributions 
368*2c2f96dcSApple OSS Distributions @discussion
369*2c2f96dcSApple OSS Distributions   note that in RSASSA-PSS, salt length is part of the signature as specified in ASN1
370*2c2f96dcSApple OSS Distributions   RSASSA-PSS-params ::= SEQUENCE {
371*2c2f96dcSApple OSS Distributions   hashAlgorithm      [0] HashAlgorithm      DEFAULT sha1,
372*2c2f96dcSApple OSS Distributions   maskGenAlgorithm   [1] MaskGenAlgorithm   DEFAULT mgf1SHA1,
373*2c2f96dcSApple OSS Distributions   saltLength         [2] INTEGER            DEFAULT 20,
374*2c2f96dcSApple OSS Distributions   trailerField       [3] TrailerField       DEFAULT trailerFieldBC
375*2c2f96dcSApple OSS Distributions 
376*2c2f96dcSApple OSS Distributions   • If nlen = 1024 bits (i.e., 128 bytes), and the output length of the approved hash function output block is 512 bits (i.e., 64 bytes), then the length (in bytes) of the salt (sLen) shall satisfy 0 ≤ sLen ≤ hLen – 2,
377*2c2f96dcSApple OSS Distributions   • Otherwise, the length (in bytes) of the salt (sLen) shall satisfy 0 ≤ sLen ≤ hLen, where hLen is the length of the hash function output block (in bytes).
378*2c2f96dcSApple OSS Distributions  */
379*2c2f96dcSApple OSS Distributions CC_NONNULL((1, 2, 3, 5, 7, 8, 9))
380*2c2f96dcSApple OSS Distributions int ccrsa_sign_pss_msg(ccrsa_full_ctx_t key,
381*2c2f96dcSApple OSS Distributions                    const struct ccdigest_info* hashAlgorithm, const struct ccdigest_info* MgfHashAlgorithm,
382*2c2f96dcSApple OSS Distributions                    size_t salt_nbytes, struct ccrng_state *rng,
383*2c2f96dcSApple OSS Distributions                    size_t msg_nbytes, const uint8_t *cc_counted_by(msg_nbytes) msg,
384*2c2f96dcSApple OSS Distributions                    size_t *sig_nbytes, uint8_t *cc_unsafe_indexable sig);
385*2c2f96dcSApple OSS Distributions 
386*2c2f96dcSApple OSS Distributions /*!
387*2c2f96dcSApple OSS Distributions @function   ccrsa_verify_pss_digest
388*2c2f96dcSApple OSS Distributions 
389*2c2f96dcSApple OSS Distributions @brief ccrsa_verify_pss_digest() verifies RSASSA-PSS signature in PKCS1-V2 format, given the digest
390*2c2f96dcSApple OSS Distributions 
391*2c2f96dcSApple OSS Distributions @param   key               The RSA public key
392*2c2f96dcSApple OSS Distributions @param   di                The hash algorithm used to generate the hash of the message.
393*2c2f96dcSApple OSS Distributions @param   mgfdi             The hash algorithm for the mask generation function
394*2c2f96dcSApple OSS Distributions @param   digest_nbytes     Length of digest. Must be equal to di->output_size
395*2c2f96dcSApple OSS Distributions @param   digest            The signed message hash
396*2c2f96dcSApple OSS Distributions @param   sig_nbytes        The length of generated signature in bytes, which equals the size of the RSA modulus.
397*2c2f96dcSApple OSS Distributions @param   sig               The signature to verify
398*2c2f96dcSApple OSS Distributions @param   salt_nbytes       Length of the salt as used during signature generation.
399*2c2f96dcSApple OSS Distributions @param   fault_canary_out  OPTIONAL cc_fault_canary_t (see discussion)
400*2c2f96dcSApple OSS Distributions 
401*2c2f96dcSApple OSS Distributions @result   CCERR_VALID_SIGNATURE on signature success.
402*2c2f96dcSApple OSS Distributions          CCERR_INVALID_SIGNATURE on signature failure.
403*2c2f96dcSApple OSS Distributions          other on some other signature verification issue.
404*2c2f96dcSApple OSS Distributions 
405*2c2f96dcSApple OSS Distributions @discussion If the fault_canary_out argument is not NULL, the value `CCRSA_PSS_FAULT_CANARY` will be placed into fault_canary_out
406*2c2f96dcSApple OSS Distributions  if the salted input hash is equal to the decoded hash (which strongly implies the signature is valid). Callers can then securely compare this output buffer against CCRSA_PSS_FAULT_CANARY, using CC_FAULT_CANARY_EQUAL, as an additional check of signature validity: if the two canary values are equal, the signature is valid otherwise it is not. If the signature is valid and the canary values are NOT equal this may indicate a potentially injected computational fault.
407*2c2f96dcSApple OSS Distributions */
408*2c2f96dcSApple OSS Distributions 
409*2c2f96dcSApple OSS Distributions CC_NONNULL((1, 2, 3, 5, 7))
410*2c2f96dcSApple OSS Distributions int ccrsa_verify_pss_digest(ccrsa_pub_ctx_t key,
411*2c2f96dcSApple OSS Distributions                             const struct ccdigest_info* di,
412*2c2f96dcSApple OSS Distributions                             const struct ccdigest_info* mgfdi,
413*2c2f96dcSApple OSS Distributions                             size_t digest_nbytes, const uint8_t *cc_counted_by(digest_nbytes) digest,
414*2c2f96dcSApple OSS Distributions                             size_t sig_nbytes, const uint8_t *cc_unsafe_indexable sig,
415*2c2f96dcSApple OSS Distributions                             size_t salt_nbytes, cc_fault_canary_t fault_canary_out);
416*2c2f96dcSApple OSS Distributions 
417*2c2f96dcSApple OSS Distributions /*!
418*2c2f96dcSApple OSS Distributions @function   ccrsa_verify_pss_msg
419*2c2f96dcSApple OSS Distributions 
420*2c2f96dcSApple OSS Distributions @brief ccrsa_verify_pss_msg() verifies RSASSA-PSS signature in PKCS1-V2 format, given the message
421*2c2f96dcSApple OSS Distributions 
422*2c2f96dcSApple OSS Distributions @param   key               The RSA public key
423*2c2f96dcSApple OSS Distributions @param   di                The hash algorithm used to generate the hash of the message.
424*2c2f96dcSApple OSS Distributions @param   mgfdi             The hash algorithm for the mask generation function
425*2c2f96dcSApple OSS Distributions @param   msg_nbytes        Length of message
426*2c2f96dcSApple OSS Distributions @param   msg               The signed message
427*2c2f96dcSApple OSS Distributions @param   sig_nbytes        The length of generated signature in bytes, which equals the size of the RSA modulus.
428*2c2f96dcSApple OSS Distributions @param   sig               The signature to verify
429*2c2f96dcSApple OSS Distributions @param   salt_nbytes       Length of the salt as used during signature generation.
430*2c2f96dcSApple OSS Distributions @param   fault_canary_out  OPTIONAL cc_fault_canary_t (see discussion)
431*2c2f96dcSApple OSS Distributions 
432*2c2f96dcSApple OSS Distributions @result  CCERR_VALID_SIGNATURE on signature success.
433*2c2f96dcSApple OSS Distributions         CCERR_INVALID_SIGNATURE on signature failure.
434*2c2f96dcSApple OSS Distributions         other on some other signature verification issue.
435*2c2f96dcSApple OSS Distributions 
436*2c2f96dcSApple OSS Distributions @discussion If the fault_canary_out argument is not NULL, the value `CCRSA_PSS_FAULT_CANARY` will be placed into fault_canary_out
437*2c2f96dcSApple OSS Distributions if the salted input hash is equal to the decoded hash (which strongly implies the signature is valid). Callers can then securely compare this output buffer against CCRSA_PSS_FAULT_CANARY, using CC_FAULT_CANARY_EQUAL, as an additional check of signature validity: if the two canary values are equal, the signature is valid otherwise it is not. If the signature is valid and the canary values are NOT equal this may indicate a potentially injected computational fault.
438*2c2f96dcSApple OSS Distributions */
439*2c2f96dcSApple OSS Distributions 
440*2c2f96dcSApple OSS Distributions CC_NONNULL((1, 2, 3, 5, 7))
441*2c2f96dcSApple OSS Distributions int ccrsa_verify_pss_msg(ccrsa_pub_ctx_t key,
442*2c2f96dcSApple OSS Distributions                          const struct ccdigest_info* di,
443*2c2f96dcSApple OSS Distributions                          const struct ccdigest_info* mgfdi,
444*2c2f96dcSApple OSS Distributions                          size_t msg_nbytes, const uint8_t *cc_counted_by(msg_nbytes) msg,
445*2c2f96dcSApple OSS Distributions                          size_t sig_nbytes, const uint8_t *cc_counted_by(sig_nbytes) sig,
446*2c2f96dcSApple OSS Distributions                          size_t salt_nbytes, cc_fault_canary_t fault_canary_out);
447*2c2f96dcSApple OSS Distributions 
448*2c2f96dcSApple OSS Distributions 
449*2c2f96dcSApple OSS Distributions /*!
450*2c2f96dcSApple OSS Distributions  @function   ccrsa_sign_pkcs1v15
451*2c2f96dcSApple OSS Distributions  @abstract   RSA signature with PKCS#1 v1.5 format per PKCS#1 v2.2
452*2c2f96dcSApple OSS Distributions 
453*2c2f96dcSApple OSS Distributions  @param      key        Full key
454*2c2f96dcSApple OSS Distributions  @param      oid        OID describing the type of digest passed in
455*2c2f96dcSApple OSS Distributions  @param      digest_len Byte length of the digest
456*2c2f96dcSApple OSS Distributions  @param      digest     Byte array of digest_len bytes containing the digest
457*2c2f96dcSApple OSS Distributions  @param      sig_len    Pointer to the number of bytes allocated for sig.
458*2c2f96dcSApple OSS Distributions                         Output the exact size of the signature.
459*2c2f96dcSApple OSS Distributions  @param      sig        Pointer to the allocated buffer of size *sig_len
460*2c2f96dcSApple OSS Distributions                         for the output signature
461*2c2f96dcSApple OSS Distributions 
462*2c2f96dcSApple OSS Distributions  @result     CCERR_OK iff successful.
463*2c2f96dcSApple OSS Distributions 
464*2c2f96dcSApple OSS Distributions   @discussion Null OID is a special case, required to support RFC 4346 where the padding
465*2c2f96dcSApple OSS Distributions  is based on SHA1+MD5. In general it is not recommended to use a NULL OID,
466*2c2f96dcSApple OSS Distributions  except when strictly required for interoperability
467*2c2f96dcSApple OSS Distributions 
468*2c2f96dcSApple OSS Distributions  */
469*2c2f96dcSApple OSS Distributions CC_NONNULL((1, 4, 5, 6))
470*2c2f96dcSApple OSS Distributions int ccrsa_sign_pkcs1v15(ccrsa_full_ctx_t key, const uint8_t *oid,
471*2c2f96dcSApple OSS Distributions                         size_t digest_len, const uint8_t *cc_counted_by(digest_len) digest,
472*2c2f96dcSApple OSS Distributions                         size_t *sig_len, uint8_t *cc_unsafe_indexable sig);
473*2c2f96dcSApple OSS Distributions 
474*2c2f96dcSApple OSS Distributions /*!
475*2c2f96dcSApple OSS Distributions  @function   ccrsa_sign_pkcs1v15_msg
476*2c2f96dcSApple OSS Distributions  @abstract   RSA signature with PKCS#1 v1.5 format per PKCS#1 v2.2
477*2c2f96dcSApple OSS Distributions 
478*2c2f96dcSApple OSS Distributions  @param      key       Full key
479*2c2f96dcSApple OSS Distributions  @param      di        Digest context
480*2c2f96dcSApple OSS Distributions  @param      msg_len   Byte length of the message to sign
481*2c2f96dcSApple OSS Distributions  @param      msg       Byte array of msg_len bytes containing the message. Will be hashed with di.
482*2c2f96dcSApple OSS Distributions  @param      sig_len   Pointer to the number of bytes allocated for sig.
483*2c2f96dcSApple OSS Distributions                        Output the exact size of the signature.
484*2c2f96dcSApple OSS Distributions  @param      sig       Pointer to the allocated buffer of size *sig_len
485*2c2f96dcSApple OSS Distributions                        for the output signature
486*2c2f96dcSApple OSS Distributions 
487*2c2f96dcSApple OSS Distributions  @result     CCERR_OK iff successful.
488*2c2f96dcSApple OSS Distributions 
489*2c2f96dcSApple OSS Distributions  @discussion Null OID is not supported by this API.
490*2c2f96dcSApple OSS Distributions 
491*2c2f96dcSApple OSS Distributions  */
492*2c2f96dcSApple OSS Distributions CC_NONNULL((1, 2, 4, 5, 6))
493*2c2f96dcSApple OSS Distributions int ccrsa_sign_pkcs1v15_msg(ccrsa_full_ctx_t key, const struct ccdigest_info* di,
494*2c2f96dcSApple OSS Distributions                             size_t msg_len, const uint8_t *cc_counted_by(msg_len) msg,
495*2c2f96dcSApple OSS Distributions                             size_t *sig_len, uint8_t *cc_unsafe_indexable sig);
496*2c2f96dcSApple OSS Distributions 
497*2c2f96dcSApple OSS Distributions 
498*2c2f96dcSApple OSS Distributions /*!
499*2c2f96dcSApple OSS Distributions   @function   ccrsa_verify_pkcs1v15
500*2c2f96dcSApple OSS Distributions   @abstract   RSA signature with PKCS#1 v1.5 format per PKCS#1 v2.2
501*2c2f96dcSApple OSS Distributions 
502*2c2f96dcSApple OSS Distributions   @param      key        Public key
503*2c2f96dcSApple OSS Distributions   @param      oid        OID describing the type of digest passed in
504*2c2f96dcSApple OSS Distributions   @param      digest_len Byte length of the digest
505*2c2f96dcSApple OSS Distributions   @param      digest     Byte array of digest_len bytes containing the digest
506*2c2f96dcSApple OSS Distributions   @param      sig_len    Number of bytes of the signature sig.
507*2c2f96dcSApple OSS Distributions   @param      sig        Pointer to the signature buffer of sig_len
508*2c2f96dcSApple OSS Distributions   @param      valid      Output boolean, true if the signature is valid.
509*2c2f96dcSApple OSS Distributions 
510*2c2f96dcSApple OSS Distributions   @result     A return value of 0 and valid = True indicates a valid signature.
511*2c2f96dcSApple OSS Distributions               A non-zero return value or valid = False indicates an invalid signature.
512*2c2f96dcSApple OSS Distributions 
513*2c2f96dcSApple OSS Distributions   @discussion Null OID is a special case, required to support RFC 4346
514*2c2f96dcSApple OSS Distributions   where the padding is based on SHA1+MD5. In general it is not
515*2c2f96dcSApple OSS Distributions   recommended to use a NULL OID, except when strictly required for
516*2c2f96dcSApple OSS Distributions   interoperability.
517*2c2f96dcSApple OSS Distributions */
518*2c2f96dcSApple OSS Distributions CC_NONNULL((1, 4, 6, 7))
519*2c2f96dcSApple OSS Distributions int ccrsa_verify_pkcs1v15(ccrsa_pub_ctx_t key, const uint8_t *oid,
520*2c2f96dcSApple OSS Distributions                           size_t digest_len, const uint8_t *cc_counted_by(digest_len) digest,
521*2c2f96dcSApple OSS Distributions                           size_t sig_len, const uint8_t *cc_counted_by(sig_len) sig,
522*2c2f96dcSApple OSS Distributions                           bool *valid);
523*2c2f96dcSApple OSS Distributions 
524*2c2f96dcSApple OSS Distributions /*!
525*2c2f96dcSApple OSS Distributions   @function   ccrsa_verify_pkcs1v15_digest
526*2c2f96dcSApple OSS Distributions   @abstract   RSA signature with PKCS#1 v1.5 format per PKCS#1 v2.2, given a digest
527*2c2f96dcSApple OSS Distributions 
528*2c2f96dcSApple OSS Distributions   @param      key                 Public key
529*2c2f96dcSApple OSS Distributions   @param      oid                 OID describing the type of digest passed in
530*2c2f96dcSApple OSS Distributions   @param      digest_len          Byte length of the digest
531*2c2f96dcSApple OSS Distributions   @param      digest              Byte array of digest_len bytes containing the digest
532*2c2f96dcSApple OSS Distributions   @param      sig_len             Number of bytes of the signature sig.
533*2c2f96dcSApple OSS Distributions   @param      sig                 Pointer to the signature buffer of sig_len
534*2c2f96dcSApple OSS Distributions   @param      fault_canary_out    OPTIONAL cc_fault_canary_t
535*2c2f96dcSApple OSS Distributions 
536*2c2f96dcSApple OSS Distributions   @result      CCERR_VALID_SIGNATURE if a valid signature.
537*2c2f96dcSApple OSS Distributions               CCERR_INVALID_SIGNATURE if an invalid signature.
538*2c2f96dcSApple OSS Distributions               Other if the verification procedure failed.
539*2c2f96dcSApple OSS Distributions 
540*2c2f96dcSApple OSS Distributions  @discussion If the fault_canary_out argument is not NULL, the value `CCRSA_PKCS1_FAULT_CANARY` will be placed into fault_canary_out
541*2c2f96dcSApple OSS Distributions  if the input hash is equal to the decoded hash (which strongly implies the signature is valid). Callers can then securely compare this output buffer against CCRSA_PKCS1_FAULT_CANARY, using CC_FAULT_CANARY_EQUAL, as an additional check of signature validity: if the two canary values are equal, the signature is valid otherwise it is not. If the signature is valid and the canary values are NOT equal this may indicate a potentially injected computational fault.
542*2c2f96dcSApple OSS Distributions */
543*2c2f96dcSApple OSS Distributions CC_NONNULL((1, 4, 6))
544*2c2f96dcSApple OSS Distributions int ccrsa_verify_pkcs1v15_digest(ccrsa_pub_ctx_t key, const uint8_t *oid,
545*2c2f96dcSApple OSS Distributions                           size_t digest_len, const uint8_t *cc_counted_by(digest_len) digest,
546*2c2f96dcSApple OSS Distributions                           size_t sig_len, const uint8_t *cc_counted_by(sig_len) sig,
547*2c2f96dcSApple OSS Distributions                           cc_fault_canary_t fault_canary_out);
548*2c2f96dcSApple OSS Distributions 
549*2c2f96dcSApple OSS Distributions /*!
550*2c2f96dcSApple OSS Distributions   @function   ccrsa_verify_pkcs1v15_msg
551*2c2f96dcSApple OSS Distributions   @abstract   RSA signature with PKCS#1 v1.5 format per PKCS#1 v2.2
552*2c2f96dcSApple OSS Distributions 
553*2c2f96dcSApple OSS Distributions   @param      key                Public key
554*2c2f96dcSApple OSS Distributions   @param      di                 Hash function
555*2c2f96dcSApple OSS Distributions   @param      msg_len            Byte length of the digest
556*2c2f96dcSApple OSS Distributions   @param      msg                Byte array of digest_len bytes containing the digest
557*2c2f96dcSApple OSS Distributions   @param      sig_len            Number of bytes of the signature sig.
558*2c2f96dcSApple OSS Distributions   @param      sig                Pointer to the signature buffer of sig_len
559*2c2f96dcSApple OSS Distributions   @param      fault_canary_out   OPTIONAL cc_fault_canary_t
560*2c2f96dcSApple OSS Distributions 
561*2c2f96dcSApple OSS Distributions   @result     CCERR_VALID_SIGNATURE if a valid signature.
562*2c2f96dcSApple OSS Distributions              CCERR_INVALID_SIGNATURE if an invalid signature.
563*2c2f96dcSApple OSS Distributions              Other if the verification procedure failed.
564*2c2f96dcSApple OSS Distributions 
565*2c2f96dcSApple OSS Distributions   @discussion Null OID is not supported by this API.
566*2c2f96dcSApple OSS Distributions              If the fault_canary_out argument is not NULL, the value `CCRSA_PKCS1_FAULT_CANARY` will
567*2c2f96dcSApple OSS Distributions              be placed into fault_canary_out if the input hash is equal to the decoded hash (which strongly
568*2c2f96dcSApple OSS Distributions              implies the signature is valid). Callers can then securely compare this output buffer against CCRSA_PKCS1_FAULT_CANARY, using CC_FAULT_CANARY_EQUAL, as an additional check of signature validity: if the two canary values are equal, the signature is valid otherwise it is not. If the signature is valid and the canary values are NOT equal this may indicate a potentially injected computational fault.
569*2c2f96dcSApple OSS Distributions */
570*2c2f96dcSApple OSS Distributions CC_NONNULL((1, 2, 4, 6))
571*2c2f96dcSApple OSS Distributions int ccrsa_verify_pkcs1v15_msg(ccrsa_pub_ctx_t key, const struct ccdigest_info* di,
572*2c2f96dcSApple OSS Distributions                           size_t msg_len, const uint8_t *cc_counted_by(msg_len) msg,
573*2c2f96dcSApple OSS Distributions                           size_t sig_len, const uint8_t *cc_counted_by(sig_len) sig,
574*2c2f96dcSApple OSS Distributions                           cc_fault_canary_t fault_canary_out);
575*2c2f96dcSApple OSS Distributions 
576*2c2f96dcSApple OSS Distributions /*!
577*2c2f96dcSApple OSS Distributions  @function   ccder_encode_rsa_pub_size
578*2c2f96dcSApple OSS Distributions  @abstract   Calculate size of public key export format data package.
579*2c2f96dcSApple OSS Distributions 
580*2c2f96dcSApple OSS Distributions  @param      key        Public key
581*2c2f96dcSApple OSS Distributions 
582*2c2f96dcSApple OSS Distributions  @result     Returns size required for encoding.
583*2c2f96dcSApple OSS Distributions  */
584*2c2f96dcSApple OSS Distributions 
585*2c2f96dcSApple OSS Distributions CC_NONNULL((1))
586*2c2f96dcSApple OSS Distributions size_t ccder_encode_rsa_pub_size(const ccrsa_pub_ctx_t key);
587*2c2f96dcSApple OSS Distributions 
588*2c2f96dcSApple OSS Distributions /*!
589*2c2f96dcSApple OSS Distributions  @function   ccrsa_export_priv_pkcs1
590*2c2f96dcSApple OSS Distributions  @abstract   Export a public key.
591*2c2f96dcSApple OSS Distributions 
592*2c2f96dcSApple OSS Distributions  @param      key        Public key
593*2c2f96dcSApple OSS Distributions  @param      der        Beginning of output DER buffer
594*2c2f96dcSApple OSS Distributions  @param      der_end    End of output DER buffer
595*2c2f96dcSApple OSS Distributions  */
596*2c2f96dcSApple OSS Distributions 
597*2c2f96dcSApple OSS Distributions CC_NONNULL((1, 2, 3))
598*2c2f96dcSApple OSS Distributions uint8_t *ccder_encode_rsa_pub(const ccrsa_pub_ctx_t key, uint8_t *cc_ended_by(der_end) der, uint8_t *der_end);
599*2c2f96dcSApple OSS Distributions 
600*2c2f96dcSApple OSS Distributions 
601*2c2f96dcSApple OSS Distributions /*!
602*2c2f96dcSApple OSS Distributions  @function   ccder_encode_rsa_priv_size
603*2c2f96dcSApple OSS Distributions  @abstract   Calculate size of full key exported in PKCS#1 format.
604*2c2f96dcSApple OSS Distributions 
605*2c2f96dcSApple OSS Distributions  @param      key        Full key
606*2c2f96dcSApple OSS Distributions 
607*2c2f96dcSApple OSS Distributions  @result     Returns size required for encoding.
608*2c2f96dcSApple OSS Distributions  */
609*2c2f96dcSApple OSS Distributions 
610*2c2f96dcSApple OSS Distributions CC_NONNULL((1))
611*2c2f96dcSApple OSS Distributions size_t ccder_encode_rsa_priv_size(const ccrsa_full_ctx_t key);
612*2c2f96dcSApple OSS Distributions 
613*2c2f96dcSApple OSS Distributions /*!
614*2c2f96dcSApple OSS Distributions  @function   ccder_encode_rsa_priv
615*2c2f96dcSApple OSS Distributions  @abstract   Export a full key in PKCS#1 format.
616*2c2f96dcSApple OSS Distributions 
617*2c2f96dcSApple OSS Distributions  @param      key        Full key
618*2c2f96dcSApple OSS Distributions  @param      der        Beginning of output DER buffer
619*2c2f96dcSApple OSS Distributions  @param      der_end    End of output DER buffer
620*2c2f96dcSApple OSS Distributions  */
621*2c2f96dcSApple OSS Distributions 
622*2c2f96dcSApple OSS Distributions CC_NONNULL((1, 2, 3))
623*2c2f96dcSApple OSS Distributions uint8_t *ccder_encode_rsa_priv(const ccrsa_full_ctx_t key, const uint8_t *cc_ended_by(der_end) der, uint8_t *der_end);
624*2c2f96dcSApple OSS Distributions 
625*2c2f96dcSApple OSS Distributions /*!
626*2c2f96dcSApple OSS Distributions  @function   ccder_decode_rsa_pub_n
627*2c2f96dcSApple OSS Distributions  @abstract   Calculate "n" for a public key imported from a data package.
628*2c2f96dcSApple OSS Distributions         PKCS #1 format
629*2c2f96dcSApple OSS Distributions 
630*2c2f96dcSApple OSS Distributions  @param      der        Beginning of input DER buffer
631*2c2f96dcSApple OSS Distributions  @param      der_end    End of input DER buffer
632*2c2f96dcSApple OSS Distributions 
633*2c2f96dcSApple OSS Distributions  @result the "n" of the RSA key that would result from the import.  This can be used
634*2c2f96dcSApple OSS Distributions  to declare the key itself.
635*2c2f96dcSApple OSS Distributions  */
636*2c2f96dcSApple OSS Distributions 
637*2c2f96dcSApple OSS Distributions CC_NONNULL((1, 2))
638*2c2f96dcSApple OSS Distributions cc_size ccder_decode_rsa_pub_n(const uint8_t *cc_ended_by(der_end) der, const uint8_t *der_end);
639*2c2f96dcSApple OSS Distributions 
640*2c2f96dcSApple OSS Distributions /*!
641*2c2f96dcSApple OSS Distributions  @function   ccder_decode_rsa_pub
642*2c2f96dcSApple OSS Distributions  @abstract   Import a public RSA key from a package in public key format.
643*2c2f96dcSApple OSS Distributions         PKCS #1 format
644*2c2f96dcSApple OSS Distributions 
645*2c2f96dcSApple OSS Distributions  @param      key          Public key (n must be set)
646*2c2f96dcSApple OSS Distributions  @param      der        Beginning of input DER buffer
647*2c2f96dcSApple OSS Distributions  @param      der_end    End of input DER buffer
648*2c2f96dcSApple OSS Distributions 
649*2c2f96dcSApple OSS Distributions  @result     Key is initialized using the data in the public key message.
650*2c2f96dcSApple OSS Distributions  */
651*2c2f96dcSApple OSS Distributions 
652*2c2f96dcSApple OSS Distributions CC_NONNULL((1, 2, 3))
653*2c2f96dcSApple OSS Distributions const uint8_t *ccder_decode_rsa_pub(const ccrsa_pub_ctx_t key, const uint8_t *cc_ended_by(der_end) der, const uint8_t *der_end);
654*2c2f96dcSApple OSS Distributions 
655*2c2f96dcSApple OSS Distributions /*!
656*2c2f96dcSApple OSS Distributions  @function   ccder_decode_rsa_pub_x509_n
657*2c2f96dcSApple OSS Distributions  @abstract   Calculate "n" for a public key imported from a data package in x509 format
658*2c2f96dcSApple OSS Distributions 
659*2c2f96dcSApple OSS Distributions  @param      der        Beginning of input DER buffer
660*2c2f96dcSApple OSS Distributions  @param      der_end    End of input DER buffer
661*2c2f96dcSApple OSS Distributions 
662*2c2f96dcSApple OSS Distributions  @result the "n" of the RSA key that would result from the import.  This can be used
663*2c2f96dcSApple OSS Distributions  to declare the key itself.
664*2c2f96dcSApple OSS Distributions  */
665*2c2f96dcSApple OSS Distributions 
666*2c2f96dcSApple OSS Distributions CC_NONNULL((1, 2))
667*2c2f96dcSApple OSS Distributions cc_size ccder_decode_rsa_pub_x509_n(const uint8_t *cc_ended_by(der_end) der, const uint8_t *der_end);
668*2c2f96dcSApple OSS Distributions 
669*2c2f96dcSApple OSS Distributions /*!
670*2c2f96dcSApple OSS Distributions  @function   ccder_decode_rsa_pub_x509
671*2c2f96dcSApple OSS Distributions  @abstract   Import a public RSA key from a package in x509 format.
672*2c2f96dcSApple OSS Distributions 
673*2c2f96dcSApple OSS Distributions  @param      key          Public key (n must be set)
674*2c2f96dcSApple OSS Distributions  @param      der        Beginning of input DER buffer
675*2c2f96dcSApple OSS Distributions  @param      der_end    End of input DER buffer
676*2c2f96dcSApple OSS Distributions 
677*2c2f96dcSApple OSS Distributions  @result     Key is initialized using the data in the public key message.
678*2c2f96dcSApple OSS Distributions  */
679*2c2f96dcSApple OSS Distributions 
680*2c2f96dcSApple OSS Distributions CC_NONNULL((1, 2, 3))
681*2c2f96dcSApple OSS Distributions const uint8_t *ccder_decode_rsa_pub_x509(const ccrsa_pub_ctx_t key, const uint8_t *cc_ended_by(der_end) der, const uint8_t *der_end);
682*2c2f96dcSApple OSS Distributions 
683*2c2f96dcSApple OSS Distributions 
684*2c2f96dcSApple OSS Distributions /*!
685*2c2f96dcSApple OSS Distributions  @function   ccder_decode_rsa_priv_n
686*2c2f96dcSApple OSS Distributions  @abstract   Calculate "n" for a private key imported from a data package.
687*2c2f96dcSApple OSS Distributions 
688*2c2f96dcSApple OSS Distributions  @param      der        Beginning of input DER buffer
689*2c2f96dcSApple OSS Distributions  @param      der_end    End of input DER buffer
690*2c2f96dcSApple OSS Distributions 
691*2c2f96dcSApple OSS Distributions  @result the "n" of the RSA key that would result from the import.  This can be used
692*2c2f96dcSApple OSS Distributions  to declare the key itself.
693*2c2f96dcSApple OSS Distributions  */
694*2c2f96dcSApple OSS Distributions 
695*2c2f96dcSApple OSS Distributions CC_NONNULL((1, 2))
696*2c2f96dcSApple OSS Distributions cc_size ccder_decode_rsa_priv_n(const uint8_t *cc_ended_by(der_end) der, const uint8_t *der_end);
697*2c2f96dcSApple OSS Distributions 
698*2c2f96dcSApple OSS Distributions /*!
699*2c2f96dcSApple OSS Distributions  @function   ccder_decode_rsa_priv
700*2c2f96dcSApple OSS Distributions  @abstract   Import a private RSA key from a package in PKCS#1 format.
701*2c2f96dcSApple OSS Distributions 
702*2c2f96dcSApple OSS Distributions  @param      key          Full key (n must be set)
703*2c2f96dcSApple OSS Distributions  @param      der        Beginning of input DER buffer
704*2c2f96dcSApple OSS Distributions  @param      der_end    End of input DER buffer
705*2c2f96dcSApple OSS Distributions 
706*2c2f96dcSApple OSS Distributions  @result     Key is initialized using the data in the public key message.
707*2c2f96dcSApple OSS Distributions  */
708*2c2f96dcSApple OSS Distributions 
709*2c2f96dcSApple OSS Distributions CC_NONNULL((1, 2, 3))
710*2c2f96dcSApple OSS Distributions const uint8_t *ccder_decode_rsa_priv(const ccrsa_full_ctx_t key, const uint8_t *cc_ended_by(der_end) der, const uint8_t *der_end);
711*2c2f96dcSApple OSS Distributions 
712*2c2f96dcSApple OSS Distributions /*!
713*2c2f96dcSApple OSS Distributions  @function   ccrsa_export_pub_size
714*2c2f96dcSApple OSS Distributions  @abstract   Calculate size of public key exported data package.
715*2c2f96dcSApple OSS Distributions 
716*2c2f96dcSApple OSS Distributions  @param      key        Public key
717*2c2f96dcSApple OSS Distributions 
718*2c2f96dcSApple OSS Distributions  @result     Returns size required for encoding.
719*2c2f96dcSApple OSS Distributions  */
720*2c2f96dcSApple OSS Distributions 
721*2c2f96dcSApple OSS Distributions CC_NONNULL((1))
722*2c2f96dcSApple OSS Distributions size_t ccrsa_export_pub_size(const ccrsa_pub_ctx_t key);
723*2c2f96dcSApple OSS Distributions 
724*2c2f96dcSApple OSS Distributions /*!
725*2c2f96dcSApple OSS Distributions  @function   ccrsa_export_pub
726*2c2f96dcSApple OSS Distributions  @abstract   Export a public key in public key format.
727*2c2f96dcSApple OSS Distributions 
728*2c2f96dcSApple OSS Distributions  @param      key        Public key
729*2c2f96dcSApple OSS Distributions  @param      out_len    Allocated size
730*2c2f96dcSApple OSS Distributions  @param      out        Output buffer
731*2c2f96dcSApple OSS Distributions  */
732*2c2f96dcSApple OSS Distributions 
733*2c2f96dcSApple OSS Distributions CC_NONNULL((1, 3))
734*2c2f96dcSApple OSS Distributions int ccrsa_export_pub(const ccrsa_pub_ctx_t key, size_t out_len, uint8_t *cc_counted_by(out_len) out);
735*2c2f96dcSApple OSS Distributions /*!
736*2c2f96dcSApple OSS Distributions  @function   ccrsa_import_pub_n
737*2c2f96dcSApple OSS Distributions  @abstract   Calculate "n" for a public key imported from a data package.
738*2c2f96dcSApple OSS Distributions 
739*2c2f96dcSApple OSS Distributions  @param      inlen        Length of public key package data
740*2c2f96dcSApple OSS Distributions  @param      der          pointer to public key package data
741*2c2f96dcSApple OSS Distributions 
742*2c2f96dcSApple OSS Distributions  @result the "n" of the RSA key that would result from the import.  This can be used
743*2c2f96dcSApple OSS Distributions  to declare the key itself.
744*2c2f96dcSApple OSS Distributions  */
745*2c2f96dcSApple OSS Distributions 
746*2c2f96dcSApple OSS Distributions CC_NONNULL((2))
747*2c2f96dcSApple OSS Distributions cc_size ccrsa_import_pub_n(size_t inlen, const uint8_t *cc_sized_by(inlen) der);
748*2c2f96dcSApple OSS Distributions 
749*2c2f96dcSApple OSS Distributions /*!
750*2c2f96dcSApple OSS Distributions  @function   ccrsa_import_pub
751*2c2f96dcSApple OSS Distributions  @abstract   Import a public RSA key from a package in public key format.
752*2c2f96dcSApple OSS Distributions 
753*2c2f96dcSApple OSS Distributions  @param      key          Public key (n must be set)
754*2c2f96dcSApple OSS Distributions  @param      inlen        Length of public key package data
755*2c2f96dcSApple OSS Distributions  @param      der           pointer to public key package data
756*2c2f96dcSApple OSS Distributions 
757*2c2f96dcSApple OSS Distributions  @result     Key is initialized using the data in the public key message.
758*2c2f96dcSApple OSS Distributions  */
759*2c2f96dcSApple OSS Distributions 
760*2c2f96dcSApple OSS Distributions CC_NONNULL((1, 3))
761*2c2f96dcSApple OSS Distributions int ccrsa_import_pub(ccrsa_pub_ctx_t key, size_t inlen, const uint8_t *cc_sized_by(inlen) der);
762*2c2f96dcSApple OSS Distributions 
763*2c2f96dcSApple OSS Distributions /*!
764*2c2f96dcSApple OSS Distributions  @function   ccrsa_export_priv_size
765*2c2f96dcSApple OSS Distributions  @abstract   Calculate size of full key exported in PKCS#1 format.
766*2c2f96dcSApple OSS Distributions 
767*2c2f96dcSApple OSS Distributions  @param      key        Full key
768*2c2f96dcSApple OSS Distributions 
769*2c2f96dcSApple OSS Distributions  @result     Returns size required for encoding.
770*2c2f96dcSApple OSS Distributions  */
771*2c2f96dcSApple OSS Distributions 
772*2c2f96dcSApple OSS Distributions CC_NONNULL((1))
773*2c2f96dcSApple OSS Distributions size_t ccrsa_export_priv_size(const ccrsa_full_ctx_t key);
774*2c2f96dcSApple OSS Distributions 
775*2c2f96dcSApple OSS Distributions /*!
776*2c2f96dcSApple OSS Distributions  @function   ccrsa_export_priv
777*2c2f96dcSApple OSS Distributions  @abstract   Export a full key in PKCS#1 format.
778*2c2f96dcSApple OSS Distributions 
779*2c2f96dcSApple OSS Distributions  @param      key        Full key
780*2c2f96dcSApple OSS Distributions  @param      out_len    Allocated size
781*2c2f96dcSApple OSS Distributions  @param      out        Output buffer
782*2c2f96dcSApple OSS Distributions  */
783*2c2f96dcSApple OSS Distributions 
784*2c2f96dcSApple OSS Distributions CC_NONNULL((1, 3))
785*2c2f96dcSApple OSS Distributions int ccrsa_export_priv(const ccrsa_full_ctx_t key, size_t out_len, uint8_t *cc_sized_by(out_len) out);
786*2c2f96dcSApple OSS Distributions 
787*2c2f96dcSApple OSS Distributions /*!
788*2c2f96dcSApple OSS Distributions  @function   ccrsa_import_priv_n
789*2c2f96dcSApple OSS Distributions  @abstract   Calculate size of full key exported in PKCS#1 format.
790*2c2f96dcSApple OSS Distributions 
791*2c2f96dcSApple OSS Distributions  @param      inlen        Length of PKCS#1 package data
792*2c2f96dcSApple OSS Distributions  @param      der           pointer to PKCS#1 package data
793*2c2f96dcSApple OSS Distributions 
794*2c2f96dcSApple OSS Distributions  @result the "n" of the RSA key that would result from the import.  This can be used
795*2c2f96dcSApple OSS Distributions  to declare the key itself.
796*2c2f96dcSApple OSS Distributions  */
797*2c2f96dcSApple OSS Distributions 
798*2c2f96dcSApple OSS Distributions CC_NONNULL((2))
799*2c2f96dcSApple OSS Distributions cc_size ccrsa_import_priv_n(size_t inlen, const uint8_t *cc_sized_by(inlen) der);
800*2c2f96dcSApple OSS Distributions 
801*2c2f96dcSApple OSS Distributions /*!
802*2c2f96dcSApple OSS Distributions  @function   ccrsa_import_priv
803*2c2f96dcSApple OSS Distributions  @abstract   Import a full RSA key from a package in PKCS#1 format.
804*2c2f96dcSApple OSS Distributions 
805*2c2f96dcSApple OSS Distributions  @param      key          Full key (n must be set)
806*2c2f96dcSApple OSS Distributions  @param      inlen        Length of PKCS#1 package data
807*2c2f96dcSApple OSS Distributions  @param      der           pointer to PKCS#1 package data
808*2c2f96dcSApple OSS Distributions 
809*2c2f96dcSApple OSS Distributions  @result     Key is initialized using the data in the PKCS#1 message.
810*2c2f96dcSApple OSS Distributions  */
811*2c2f96dcSApple OSS Distributions 
812*2c2f96dcSApple OSS Distributions CC_NONNULL_ALL
813*2c2f96dcSApple OSS Distributions int ccrsa_import_priv(ccrsa_full_ctx_t key, size_t inlen, const uint8_t *cc_sized_by(inlen) der);
814*2c2f96dcSApple OSS Distributions 
815*2c2f96dcSApple OSS Distributions /*!
816*2c2f96dcSApple OSS Distributions @function   ccrsa_get_pubkey_components
817*2c2f96dcSApple OSS Distributions @abstract   Copy each component of the public key to the given buffers
818*2c2f96dcSApple OSS Distributions 
819*2c2f96dcSApple OSS Distributions @param      pubkey                       Public key
820*2c2f96dcSApple OSS Distributions @param      modulus                     Buffer to the output buffer for the modulus
821*2c2f96dcSApple OSS Distributions @param      modulusLength        Pointer to the byte size allocated for the modulus, updated with actual output size
822*2c2f96dcSApple OSS Distributions @param      exponent                  Buffer to the output buffer for the exponent
823*2c2f96dcSApple OSS Distributions @param      exponentLength     Pointer to the byte size allocated for the exponent, updated with actual output size
824*2c2f96dcSApple OSS Distributions 
825*2c2f96dcSApple OSS Distributions @return     0 is success, not 0 in case of error
826*2c2f96dcSApple OSS Distributions 
827*2c2f96dcSApple OSS Distributions @discussion if either allocated buffer length is insufficient, the function returns an error
828*2c2f96dcSApple OSS Distributions */
829*2c2f96dcSApple OSS Distributions CC_NONNULL((1, 2))
830*2c2f96dcSApple OSS Distributions int ccrsa_get_pubkey_components(const ccrsa_pub_ctx_t pubkey, uint8_t *cc_unsafe_indexable modulus, size_t *modulusLength, uint8_t *cc_unsafe_indexable exponent, size_t *exponentLength);
831*2c2f96dcSApple OSS Distributions 
832*2c2f96dcSApple OSS Distributions /*!
833*2c2f96dcSApple OSS Distributions @function   ccrsa_get_fullkey_components
834*2c2f96dcSApple OSS Distributions @abstract   Copy each component of the public key to the given buffers
835*2c2f96dcSApple OSS Distributions 
836*2c2f96dcSApple OSS Distributions @param      key                              Full key
837*2c2f96dcSApple OSS Distributions @param      modulus                     Output buffer for the modulus
838*2c2f96dcSApple OSS Distributions @param      modulusLength        Pointer to the byte size allocated for the modulus, updated with actual output size
839*2c2f96dcSApple OSS Distributions @param      d                                   Output buffer for the private exponent d.
840*2c2f96dcSApple OSS Distributions @param      dLength                     Pointer to the byte size allocated for the private exponent d, updated with actual output size
841*2c2f96dcSApple OSS Distributions @param      p                                  Output buffer for the first prime factor of the modulus
842*2c2f96dcSApple OSS Distributions @param      pLength                     Pointer to the byte size allocated for the prime factor, updated with actual output size
843*2c2f96dcSApple OSS Distributions @param      q                                  Output buffer for the second prime factor of the modulus
844*2c2f96dcSApple OSS Distributions @param      qLength                     Pointer to the byte size allocated for the prime factor, updated with actual output size
845*2c2f96dcSApple OSS Distributions 
846*2c2f96dcSApple OSS Distributions @return     0 is success, not 0 in case of error
847*2c2f96dcSApple OSS Distributions 
848*2c2f96dcSApple OSS Distributions @discussion if either allocated buffer length is insufficient, the function returns an error
849*2c2f96dcSApple OSS Distributions */
850*2c2f96dcSApple OSS Distributions CC_NONNULL((1, 2))
851*2c2f96dcSApple OSS Distributions int ccrsa_get_fullkey_components(const ccrsa_full_ctx_t key, uint8_t *cc_unsafe_indexable modulus, size_t *modulusLength,
852*2c2f96dcSApple OSS Distributions                                  uint8_t *cc_unsafe_indexable d, size_t *dLength,
853*2c2f96dcSApple OSS Distributions                                  uint8_t *cc_unsafe_indexable p, size_t *pLength,
854*2c2f96dcSApple OSS Distributions                                  uint8_t *cc_unsafe_indexable q, size_t *qLength);
855*2c2f96dcSApple OSS Distributions 
856*2c2f96dcSApple OSS Distributions 
857*2c2f96dcSApple OSS Distributions /*!
858*2c2f96dcSApple OSS Distributions  @function   ccrsa_dump_public_key
859*2c2f96dcSApple OSS Distributions  @abstract   Print a rsa public key in the console (printf)
860*2c2f96dcSApple OSS Distributions 
861*2c2f96dcSApple OSS Distributions  @param      key          Public key
862*2c2f96dcSApple OSS Distributions  */
863*2c2f96dcSApple OSS Distributions void ccrsa_dump_public_key(ccrsa_pub_ctx_t key);
864*2c2f96dcSApple OSS Distributions 
865*2c2f96dcSApple OSS Distributions /*!
866*2c2f96dcSApple OSS Distributions  @function   ccrsa_dump_full_key
867*2c2f96dcSApple OSS Distributions  @abstract   Print a rsa private key in the console (printf)
868*2c2f96dcSApple OSS Distributions 
869*2c2f96dcSApple OSS Distributions  @param      key          Public key
870*2c2f96dcSApple OSS Distributions  */
871*2c2f96dcSApple OSS Distributions void ccrsa_dump_full_key(ccrsa_full_ctx_t key);
872*2c2f96dcSApple OSS Distributions 
873*2c2f96dcSApple OSS Distributions #endif /* _CORECRYPTO_CCRSA_H_ */
874