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