5 * Created on 11/16/2010
7 * Copyright (c) 2010,2011,2012,2014,2015 Apple Inc. All rights reserved.
11 #ifndef _CORECRYPTO_CCRSA_H_
12 #define _CORECRYPTO_CCRSA_H_
14 #include <corecrypto/cc.h>
15 #include <corecrypto/ccdigest.h>
16 #include <corecrypto/ccrng.h>
17 #include <corecrypto/cczp.h>
20 // Apple does not generate keys of greater than 4096 bits
21 // This limit is relaxed to accommodate potential third-party consumers
22 #define CCRSA_KEYGEN_MAX_NBITS 8192
24 // Program error: buffer too small or encrypted message is too small
25 #define CCRSA_INVALID_INPUT -1
26 // Invalid crypto configuration: Hash length versus RSA key size
27 #define CCRSA_INVALID_CONFIG -2
28 // The data is invalid (we won't say more for security
29 #define CCRSA_DECRYPTION_ERROR -3
31 #define CCRSA_ENCODING_ERROR -4
32 #define CCRSA_DECODING_ERROR -5
33 #define CCRSA_SIGNATURE_GEN_ERROR -6
35 struct ccrsa_full_ctx
{
36 __CCZP_ELEMENTS_DEFINITIONS(pb_
)
37 } CC_ALIGNED(CCN_UNIT_SIZE
);
39 struct ccrsa_pub_ctx
{
40 __CCZP_ELEMENTS_DEFINITIONS(pb_
)
41 } CC_ALIGNED(CCN_UNIT_SIZE
);
43 struct ccrsa_priv_ctx
{
44 __CCZP_ELEMENTS_DEFINITIONS(pv_
)
45 } CC_ALIGNED(CCN_UNIT_SIZE
);
48 #if CORECRYPTO_USE_TRANSPARENT_UNION
51 struct ccrsa_pub_ctx
* pub
;
52 struct ccrsa_full_ctx
*full
;
53 } ccrsa_full_ctx_t
__attribute__((transparent_union
));
54 typedef struct ccrsa_full_ctx ccrsa_full_ctx
;
55 typedef struct ccrsa_priv_ctx ccrsa_priv_ctx
;
60 } ccrsa_priv_ctx_t
__attribute__((transparent_union
));
63 typedef ccrsa_full_ctx_t ccrsa_pub_ctx_t
;
64 typedef struct ccrsa_pub_ctx ccrsa_pub_ctx
;
67 typedef struct ccrsa_full_ctx
* ccrsa_full_ctx_t
;
68 typedef struct ccrsa_pub_ctx
* ccrsa_pub_ctx_t
;
69 typedef struct ccrsa_priv_ctx
* ccrsa_priv_ctx_t
;
75 public key cczp d=e^-1 mod phi(m) priv key cczp priv key cczq dp, dq, qinv
78 +-------+------+-------+------++------++-------+------+---------++-------+------+---------++-------+-------+---------+
79 | zm_hd | m[n] |mr[n+1]| e[n] || d[n] || zp_hd |p[n/2]|pr[n/2+1]|| zq_hd |q[n/2]|qr[n/2+1]||dp[n/2]|dq[n/2]|qinv[n/2]|
80 +-------+------+-------+------++------++-------+------+---------++-------+------+---------++-------+-------+---------+
83 /* Return the size of an ccec_full_ctx where each ccn is _size_ bytes. Get _size_ through ccn_sizeof(nbits) */
85 /* Return the size of an ccec_full_ctx where each ccn is _size_ bytes. */
87 #define ccrsa_pub_ctx_size(_size_) (sizeof(struct cczp) + CCN_UNIT_SIZE + 3 * (_size_))
88 #define ccrsa_priv_ctx_size(_size_) ((sizeof(struct cczp) + CCN_UNIT_SIZE) * 2 + 7 * ccn_sizeof(ccn_bitsof_size(_size_)/2 + 1))
89 #define ccrsa_full_ctx_size(_size_) (ccrsa_pub_ctx_size(_size_) + _size_ + ccrsa_priv_ctx_size(_size_))
91 /* Declare a fully scheduled rsa key. Size is the size in bytes each ccn in
92 the key. For example to declare (on the stack or in a struct) a 1021 bit
93 rsa public key named foo use ccrsa_pub_ctx_decl(ccn_sizeof(1021), foo).
95 #define ccrsa_full_ctx_decl(_size_, _name_) cc_ctx_decl(struct ccrsa_full_ctx, ccrsa_full_ctx_size(_size_), _name_)
96 #define ccrsa_full_ctx_clear(_size_, _name_) cc_clear(ccrsa_full_ctx_size(_size_), _name_)
97 #define ccrsa_pub_ctx_decl(_size_, _name_) cc_ctx_decl(struct ccrsa_pub_ctx, ccrsa_pub_ctx_size(_size_), _name_)
98 #define ccrsa_pub_ctx_clear(_size_, _name_) cc_clear(ccrsa_pub_ctx_size(_size_), _name_)
100 // accessors to ccrsa full and public key fields. */
101 // The offsets are computed using pb_ccn. If any object other than ccrsa_full_ctx_t
102 // or ccrsa_pub_ctx_t is passed to the macros, compiler error is generated.
106 #if CORECRYPTO_USE_TRANSPARENT_UNION
107 //#define ccrsa_ctx_zm(_ctx_) (((ccrsa_pub_ctx_t)(_ctx_)).zp)
109 CC_CONST CC_INLINE cczp_t
ccrsa_ctx_zm(ccrsa_full_ctx_t _ctx_
) { return ((cczp_t
)(struct cczp
*)((_ctx_
).full
)); }
110 CC_CONST CC_INLINE cc_unit
*ccrsa_ctx_m(ccrsa_full_ctx_t _ctx_
){ return ((_ctx_
).full
->pb_ccn
);}
111 #define ccrsa_ctx_n(_ctx_) (ccrsa_ctx_zm(_ctx_).zp->n)
113 #define ccrsa_ctx_zm(_ctx_) ((cczp_t)(_ctx_))
114 #define ccrsa_ctx_n(_ctx_) (ccrsa_ctx_zm(_ctx_)->n)
115 #define ccrsa_ctx_m(_ctx_) ((_ctx_)->pb_ccn)
118 #define ccrsa_ctx_e(_ctx_) (ccrsa_ctx_m(_ctx_) + 2 * ccrsa_ctx_n(_ctx_) + 1)
119 #define ccrsa_ctx_d(_ctx_) (ccrsa_ctx_m(_ctx_) + 3 * ccrsa_ctx_n(_ctx_) + 1)
121 // accessors to ccrsa private key fields
122 // The offsets are computed using pv_ccn. If any object other than ccrsa_priv_ctx_t
123 // is passed to the macros, compiler error is generated.
124 #if CORECRYPTO_USE_TRANSPARENT_UNION
126 /* rvalue accessors to ccec_key fields. */
128 ccrsa_priv_ctx_t
ccrsa_get_private_ctx_ptr(ccrsa_full_ctx_t fk
) {
129 cc_unit
*p
= (cc_unit
*)fk
.full
;
130 cc_size p_size
= ccrsa_ctx_n(fk
);
131 p
+= ccn_nof_size(ccrsa_pub_ctx_size(ccn_sizeof_n(p_size
))) + p_size
;
132 ccrsa_priv_ctx
*priv
= (ccrsa_priv_ctx
*)p
;
133 return (ccrsa_priv_ctx_t
)priv
;
137 ccrsa_pub_ctx_t
ccrsa_ctx_public(ccrsa_full_ctx_t fk
) {
138 return (ccrsa_pub_ctx_t
) fk
.full
;
141 #define ccrsa_ctx_private_zp(FK) ((ccrsa_get_private_ctx_ptr(FK)).zp)
142 #define ccrsa_ctx_private_zq(FK) ((cczp_t)((ccrsa_get_private_ctx_ptr(FK)).zp.zp->ccn + 2 * ccrsa_ctx_private_zp(FK).zp->n + 1))
143 #define ccrsa_ctx_private_dp(FK) ((ccrsa_get_private_ctx_ptr(FK)).zp.zp->ccn + 4 * ccrsa_ctx_private_zp(FK).zp->n + 2 + ccn_nof_size(sizeof(struct cczp)))
144 #define ccrsa_ctx_private_dq(FK) ((ccrsa_get_private_ctx_ptr(FK)).zp.zp->ccn + 5 * ccrsa_ctx_private_zp(FK).zp->n + 2 + ccn_nof_size(sizeof(struct cczp)))
145 #define ccrsa_ctx_private_qinv(FK) ((ccrsa_get_private_ctx_ptr(FK)).zp.zp->ccn + 6 * ccrsa_ctx_private_zp(FK).zp->n + 2 + ccn_nof_size(sizeof(struct cczp)))
148 #define ccrsa_ctx_private_zp(FK) ((cczp_t)ccrsa_get_private_ctx_ptr(FK))
149 #define ccrsa_ctx_private_zq(FK) ((cczp_t)((ccrsa_get_private_ctx_ptr(FK))->pv_ccn + 2 * ccrsa_ctx_private_zp(FK)->n + 1))
150 #define ccrsa_ctx_private_dp(FK) ((ccrsa_get_private_ctx_ptr(FK))->pv_ccn + 4 * ccrsa_ctx_private_zp(FK)->n + 2 + ccn_nof_size(sizeof(struct cczp)))
151 #define ccrsa_ctx_private_dq(FK) ((ccrsa_get_private_ctx_ptr(FK))->pv_ccn + 5 * ccrsa_ctx_private_zp(FK)->n + 2 + ccn_nof_size(sizeof(struct cczp)))
152 #define ccrsa_ctx_private_qinv(FK) ((ccrsa_get_private_ctx_ptr(FK))->pv_ccn + 6 * ccrsa_ctx_private_zp(FK)->n + 2 + ccn_nof_size(sizeof(struct cczp)))
155 ccrsa_priv_ctx_t
ccrsa_get_private_ctx_ptr(ccrsa_full_ctx_t fk
) {
156 ccrsa_priv_ctx_t priv
= (ccrsa_priv_ctx_t
)(ccrsa_ctx_d(fk
)+ccrsa_ctx_n(fk
));
161 @function ccrsa_ctx_public
162 @abstract gets the public key from full key
163 @param fk RSA full key
164 @result Returns RSA public ker
167 ccrsa_pub_ctx_t
ccrsa_ctx_public(ccrsa_full_ctx_t fk
) {
168 return (ccrsa_pub_ctx_t
) fk
;
173 /* Return exact key bit size */
175 ccrsa_pubkeylength(ccrsa_pub_ctx_t pubk
) {
176 return cczp_bitlen(ccrsa_ctx_zm(pubk
));
179 /* PKCS1 pad_markers */
180 #define CCRSA_PKCS1_PAD_SIGN 1
181 #define CCRSA_PKCS1_PAD_ENCRYPT 2
183 /* Initialize key based on modulus and e as cc_unit. key->zp.n must already be set. */
184 CC_NONNULL_TU((1)) CC_NONNULL((2, 3))
185 int ccrsa_init_pub(ccrsa_pub_ctx_t key
, const cc_unit
*modulus
,
188 /* Initialize key based on modulus and e as big endian byte array
189 key->zp.n must already be set. */
190 CC_NONNULL_TU((1)) CC_NONNULL((3 ,5))
191 int ccrsa_make_pub(ccrsa_pub_ctx_t pubk
,
192 size_t exp_nbytes
, const uint8_t *exp
,
193 size_t mod_nbytes
, const uint8_t *mod
);
195 /* Do a public key crypto operation (typically verify or encrypt) on in and put
196 the result in out. Both in and out should be cc_unit aligned and
197 ccrsa_key_n(key) units long. Clients should use ccn_read_uint() to
198 convert bytes to a cc_unit to use for this API.*/
199 CC_NONNULL_TU((1)) CC_NONNULL((2, 3))
200 int ccrsa_pub_crypt(ccrsa_pub_ctx_t key
, cc_unit
*out
, const cc_unit
*in
);
202 /* Generate an nbit rsa key pair in key, which should be allocated using
203 ccrsa_full_ctx_decl(ccn_sizeof(1024), rsa_ctx). The unsigned big endian
204 byte array exponent e of length e_size is used as the exponent. It's an
205 error to call this function with an exponent larger than nbits. rng
206 must be a pointer to an initialized struct ccrng_state. */
207 CC_NONNULL_TU((2)) CC_NONNULL((4, 5))
208 int ccrsa_generate_key(size_t nbits
, ccrsa_full_ctx_t rsa_ctx
,
209 size_t e_size
, const void *e
, struct ccrng_state
*rng
) CC_WARN_RESULT
;
211 /* Generate RSA key in conformance with FIPS186-4 standard */
212 CC_NONNULL_TU((2)) CC_NONNULL((4, 5, 6))
214 ccrsa_generate_fips186_key(size_t nbits
, ccrsa_full_ctx_t fk
,
215 size_t e_size
, const void *eBytes
,
216 struct ccrng_state
*rng1
, struct ccrng_state
*rng2
) CC_WARN_RESULT
;
218 /* Construct RSA key from fix input in conformance with FIPS186-4 standard */
219 CC_NONNULL_TU((16)) CC_NONNULL((3, 5, 7, 9, 11, 13, 15))
221 ccrsa_make_fips186_key(size_t nbits
,
222 const cc_size e_n
, const cc_unit
*e
,
223 const cc_size xp1Len
, const cc_unit
*xp1
, const cc_size xp2Len
, const cc_unit
*xp2
,
224 const cc_size xpLen
, const cc_unit
*xp
,
225 const cc_size xq1Len
, const cc_unit
*xq1
, const cc_size xq2Len
, const cc_unit
*xq2
,
226 const cc_size xqLen
, const cc_unit
*xq
,
228 cc_size
*np
, cc_unit
*r_p
,
229 cc_size
*nq
, cc_unit
*r_q
,
230 cc_size
*nm
, cc_unit
*r_m
,
231 cc_size
*nd
, cc_unit
*r_d
);
234 * @brief ccrsa_sign_pss() generates RSASSA-PSS signature in PKCS1-V2 format
236 * note that in RSASSA-PSS, salt length is part of the signature as specified in ASN1
237 * RSASSA-PSS-params ::= SEQUENCE {
238 * hashAlgorithm [0] HashAlgorithm DEFAULT sha1,
239 * maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1,
240 * saltLength [2] INTEGER DEFAULT 20,
241 * trailerField [3] TrailerField DEFAULT trailerFieldBC
244 * FIPS 186-4 for RSASSA-PSS:
245 * .... Both signature schemes are approved for use, but additional constraints are imposed beyond those specified in PKCS #1 v2.1.....
247 * • 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,
248 * • 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).
251 * • CAVS test vectors are not very useful in the case of RSA-PSS, because they only validate the exponentiation part of the signature. See: http://csrc.nist.gov/groups/STM/cavp/documents/components/RSA2SP1VS.pdf
253 * @param key The RSA key
254 * @param hashAlgorithm The hash algorithm used to generate mHash from the original message. It is also used inside the PSS encoding function. This is also the hash function to be used in the mask generation function (MGF)
255 * @param MgfHashAlgorithm The hash algorithm for thr mask generation function
256 * @param rng Random number geberator to generate salt in PSS encoding
257 * @param saltSize Intended length of the salt
258 * @param hSize Length of message hash . Must be equal to hashAlgorithm->output_size
259 * @param mHash The input that needs to be signed. This is the hash of message M with length of hLen
261 * @param sig The signature output
262 * @param sigSize The length of generated signature in bytes, which equals the size of the RSA modulus.
263 * @return 0:ok, non-zero:error
265 CC_NONNULL((2,3,5,7,8,9))
266 int ccrsa_sign_pss(ccrsa_full_ctx_t key
,
267 const struct ccdigest_info
* hashAlgorithm
, const struct ccdigest_info
* MgfHashAlgorithm
,
268 size_t saltSize
, struct ccrng_state
*rng
,
269 size_t hSize
, const uint8_t *mHash
,
270 size_t *sigSize
, uint8_t *sig
);
272 CC_NONNULL((2,3,5,7,9))
273 int ccrsa_verify_pss(ccrsa_pub_ctx_t key
,
274 const struct ccdigest_info
* di
, const struct ccdigest_info
* MgfDi
,
275 size_t digestSize
, const uint8_t *digest
,
276 size_t sigSize
, const uint8_t *sig
,
277 size_t saltSize
, bool *valid
);
280 @function ccrsa_sign_pkcs1v15
281 @abstract RSA signature with PKCS#1 v1.5 format per PKCS#1 v2.2
284 @param oid OID describing the type of digest passed in
285 @param digest_len Byte length of the digest
286 @param digest Byte array of digest_len bytes containing the digest
287 @param sig_len Pointer to the number of byte allocate for sig.
288 Output the exact size of the signature.
289 @param sig Pointer to the allocated buffer of size *sig_len
290 for the output signature
292 @result 0 iff successful.
294 @discussion Null OID is a special case, required to support RFC 4346 where the padding
295 is based on SHA1+MD5. In general it is not recommended to use a NULL OID,
296 except when strictly required for interoperability
299 CC_NONNULL_TU((1)) CC_NONNULL((4, 5, 6))
300 int ccrsa_sign_pkcs1v15(ccrsa_full_ctx_t key
, const uint8_t *oid
,
301 size_t digest_len
, const uint8_t *digest
,
302 size_t *sig_len
, uint8_t *sig
);
306 @function ccrsa_sign_pkcs1v15
307 @abstract RSA signature with PKCS#1 v1.5 format per PKCS#1 v2.2
309 @param key Public key
310 @param oid OID describing the type of digest passed in
311 @param digest_len Byte length of the digest
312 @param digest Byte array of digest_len bytes containing the digest
313 @param sig_len Number of byte of the signature sig.
314 @param sig Pointer to the signature buffer of sig_len
315 @param valid Output boolean, true if the signature is valid.
317 @result 0 iff successful.
319 @discussion Null OID is a special case, required to support RFC 4346 where the padding
320 is based on SHA1+MD5. In general it is not recommended to use a NULL OID,
321 except when strictly required for interoperability
323 CC_NONNULL_TU((1)) CC_NONNULL((4, 6, 7))
324 int ccrsa_verify_pkcs1v15(ccrsa_pub_ctx_t key
, const uint8_t *oid
,
325 size_t digest_len
, const uint8_t *digest
,
326 size_t sig_len
, const uint8_t *sig
,
330 @function ccder_encode_rsa_pub_size
331 @abstract Calculate size of public key export format data package.
333 @param key Public key
335 @result Returns size required for encoding.
339 size_t ccder_encode_rsa_pub_size(const ccrsa_pub_ctx_t key
);
342 @function ccrsa_export_priv_pkcs1
343 @abstract Export a public key.
345 @param key Public key
346 @param der Beginning of output DER buffer
347 @param der_end End of output DER buffer
350 CC_NONNULL_TU((1)) CC_NONNULL((2)) CC_NONNULL((3))
351 uint8_t *ccder_encode_rsa_pub(const ccrsa_pub_ctx_t key
, uint8_t *der
, uint8_t *der_end
);
355 @function ccder_encode_rsa_priv_size
356 @abstract Calculate size of full key exported in PKCS#1 format.
360 @result Returns size required for encoding.
364 size_t ccder_encode_rsa_priv_size(const ccrsa_full_ctx_t key
);
367 @function ccder_encode_rsa_priv
368 @abstract Export a full key in PKCS#1 format.
371 @param der Beginning of output DER buffer
372 @param der_end End of output DER buffer
375 CC_NONNULL_TU((1)) CC_NONNULL((2)) CC_NONNULL((3))
376 uint8_t *ccder_encode_rsa_priv(const ccrsa_full_ctx_t key
, const uint8_t *der
, uint8_t *der_end
);
379 @function ccder_decode_rsa_pub_n
380 @abstract Calculate "n" for a public key imported from a data package.
383 @param der Beginning of input DER buffer
384 @param der_end End of input DER buffer
386 @result the "n" of the RSA key that would result from the import. This can be used
387 to declare the key itself.
390 CC_NONNULL((1)) CC_NONNULL((2))
391 cc_size
ccder_decode_rsa_pub_n(const uint8_t *der
, const uint8_t *der_end
);
394 @function ccder_decode_rsa_pub
395 @abstract Import a public RSA key from a package in public key format.
398 @param key Public key (n must be set)
399 @param der Beginning of input DER buffer
400 @param der_end End of input DER buffer
402 @result Key is initialized using the data in the public key message.
405 CC_NONNULL_TU((1)) CC_NONNULL((2)) CC_NONNULL((3))
406 const uint8_t *ccder_decode_rsa_pub(const ccrsa_pub_ctx_t key
, const uint8_t *der
, const uint8_t *der_end
);
409 @function ccder_decode_rsa_pub_x509_n
410 @abstract Calculate "n" for a public key imported from a data package in x509 format
412 @param der Beginning of input DER buffer
413 @param der_end End of input DER buffer
415 @result the "n" of the RSA key that would result from the import. This can be used
416 to declare the key itself.
419 CC_NONNULL((1)) CC_NONNULL((2))
420 cc_size
ccder_decode_rsa_pub_x509_n(const uint8_t *der
, const uint8_t *der_end
);
423 @function ccder_decode_rsa_pub_x509
424 @abstract Import a public RSA key from a package in x509 format.
426 @param key Public key (n must be set)
427 @param der Beginning of input DER buffer
428 @param der_end End of input DER buffer
430 @result Key is initialized using the data in the public key message.
433 CC_NONNULL_TU((1)) CC_NONNULL((2)) CC_NONNULL((3))
434 const uint8_t *ccder_decode_rsa_pub_x509(const ccrsa_pub_ctx_t key
, const uint8_t *der
, const uint8_t *der_end
);
438 @function ccder_decode_rsa_priv_n
439 @abstract Calculate "n" for a private key imported from a data package.
441 @param der Beginning of input DER buffer
442 @param der_end End of input DER buffer
444 @result the "n" of the RSA key that would result from the import. This can be used
445 to declare the key itself.
448 CC_NONNULL((1)) CC_NONNULL((2))
449 cc_size
ccder_decode_rsa_priv_n(const uint8_t *der
, const uint8_t *der_end
);
452 @function ccder_decode_rsa_priv
453 @abstract Import a private RSA key from a package in PKCS#1 format.
455 @param key Full key (n must be set)
456 @param der Beginning of input DER buffer
457 @param der_end End of input DER buffer
459 @result Key is initialized using the data in the public key message.
462 CC_NONNULL_TU((1)) CC_NONNULL((2)) CC_NONNULL((3))
463 const uint8_t *ccder_decode_rsa_priv(const ccrsa_full_ctx_t key
, const uint8_t *der
, const uint8_t *der_end
);
466 @function ccrsa_export_pub_size
467 @abstract Calculate size of public key exported data package.
469 @param key Public key
471 @result Returns size required for encoding.
474 CC_CONST CC_INLINE
CC_NONNULL_TU((1))
475 size_t ccrsa_export_pub_size(const ccrsa_pub_ctx_t key
) {
476 return ccder_encode_rsa_pub_size(key
);
480 @function ccrsa_export_pub
481 @abstract Export a public key in public key format.
483 @param key Public key
484 @param out_len Allocated size
485 @param out Output buffer
488 CC_NONNULL_TU((1)) CC_NONNULL((3))
489 int ccrsa_export_pub(const ccrsa_pub_ctx_t key
, size_t out_len
, uint8_t *out
);
491 @function ccrsa_import_pub_n
492 @abstract Calculate "n" for a public key imported from a data package.
494 @param inlen Length of public key package data
495 @param der pointer to public key package data
497 @result the "n" of the RSA key that would result from the import. This can be used
498 to declare the key itself.
501 CC_CONST CC_INLINE
CC_NONNULL((2))
502 cc_size
ccrsa_import_pub_n(size_t inlen
, const uint8_t *der
) {
503 cc_size size
= ccder_decode_rsa_pub_x509_n(der
, der
+ inlen
);
505 size
= ccder_decode_rsa_pub_n(der
, der
+ inlen
);
511 @function ccrsa_import_pub
512 @abstract Import a public RSA key from a package in public key format.
514 @param key Public key (n must be set)
515 @param inlen Length of public key package data
516 @param der pointer to public key package data
518 @result Key is initialized using the data in the public key message.
521 CC_NONNULL_TU((1)) CC_NONNULL((3))
522 int ccrsa_import_pub(ccrsa_pub_ctx_t key
, size_t inlen
, const uint8_t *der
);
525 @function ccrsa_export_priv_size
526 @abstract Calculate size of full key exported in PKCS#1 format.
530 @result Returns size required for encoding.
533 CC_CONST CC_INLINE
CC_NONNULL_TU((1))
534 size_t ccrsa_export_priv_size(const ccrsa_full_ctx_t key
) {
535 return ccder_encode_rsa_priv_size(key
);
539 @function ccrsa_export_priv
540 @abstract Export a full key in PKCS#1 format.
543 @param out_len Allocated size
544 @param out Output buffer
547 CC_CONST CC_INLINE
CC_NONNULL_TU((1)) CC_NONNULL((3))
548 int ccrsa_export_priv(const ccrsa_full_ctx_t key
, size_t out_len
, uint8_t *out
) {
549 return (ccder_encode_rsa_priv(key
, out
, out
+out_len
) != out
);
553 @function ccrsa_import_priv_n
554 @abstract Calculate size of full key exported in PKCS#1 format.
556 @param inlen Length of PKCS#1 package data
557 @param der pointer to PKCS#1 package data
559 @result the "n" of the RSA key that would result from the import. This can be used
560 to declare the key itself.
563 CC_CONST CC_INLINE
CC_NONNULL((2))
564 cc_size
ccrsa_import_priv_n(size_t inlen
, const uint8_t *der
) {
565 return ccder_decode_rsa_priv_n(der
, der
+ inlen
);
569 @function ccrsa_import_priv
570 @abstract Import a full RSA key from a package in PKCS#1 format.
572 @param key Full key (n must be set)
573 @param inlen Length of PKCS#1 package data
574 @param der pointer to PKCS#1 package data
576 @result Key is initialized using the data in the PKCS#1 message.
579 CC_CONST CC_INLINE
CC_NONNULL_TU((1)) CC_NONNULL((3))
580 int ccrsa_import_priv(ccrsa_full_ctx_t key
, size_t inlen
, const uint8_t *der
) {
581 return (ccder_decode_rsa_priv(key
, der
, der
+inlen
) == NULL
);
585 CC_NONNULL_TU((1)) CC_NONNULL2
586 int ccrsa_get_pubkey_components(const ccrsa_pub_ctx_t pubkey
, uint8_t *modulus
, size_t *modulusLength
, uint8_t *exponent
, size_t *exponentLength
);
588 CC_NONNULL_TU((1)) CC_NONNULL2
589 int ccrsa_get_fullkey_components(const ccrsa_full_ctx_t key
, uint8_t *modulus
, size_t *modulusLength
, uint8_t *exponent
, size_t *exponentLength
,
590 uint8_t *p
, size_t *pLength
, uint8_t *q
, size_t *qLength
);
594 @function ccrsa_dump_public_key
595 @abstract Print a rsa public key in the console (printf)
597 @param key Public key
599 void ccrsa_dump_public_key(ccrsa_pub_ctx_t key
);
602 @function ccrsa_dump_full_key
603 @abstract Print a rsa private key in the console (printf)
605 @param key Public key
607 void ccrsa_dump_full_key(ccrsa_full_ctx_t key
);
609 #endif /* _CORECRYPTO_CCRSA_H_ */