]>
git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_cryptkit/lib/feePublicKey.h
1 /* Copyright (c) 1998,2011-2012,2014 Apple Inc. All Rights Reserved.
3 * NOTICE: USE OF THE MATERIALS ACCOMPANYING THIS NOTICE IS SUBJECT
4 * TO THE TERMS OF THE SIGNED "FAST ELLIPTIC ENCRYPTION (FEE) REFERENCE
5 * SOURCE CODE EVALUATION AGREEMENT" BETWEEN APPLE, INC. AND THE
6 * ORIGINAL LICENSEE THAT OBTAINED THESE MATERIALS FROM APPLE,
7 * INC. ANY USE OF THESE MATERIALS NOT PERMITTED BY SUCH AGREEMENT WILL
8 * EXPOSE YOU TO LIABILITY.
9 ***************************************************************************
18 * Added ECDSA signature routines.
23 #ifndef _CK_FEEPUBLICKEY_H_
24 #define _CK_FEEPUBLICKEY_H_
28 #include <sys/types.h> /* size_t */
30 #if !defined(__MACH__)
33 #include <security_cryptkit/feeTypes.h>
41 * Obatin a newly allocated feePubKey.
43 feePubKey
feePubKeyAlloc(void);
45 void feePubKeyFree(feePubKey pubKey
);
48 * Init feePubKey from private "password" data. Incoming password data will
49 * be processed with digests before use if hashPrivData is true, otherwise
50 * it'll be used as is. In the 'as is' case, the privData must be at least
51 * as large as the key being created.
53 * Currently two versions - one in which the size of the key is specified as
54 * a feeDepth; one for key size in bits and optional primeType and curveType.
56 feeReturn
feePubKeyInitFromPrivDataDepth(feePubKey pubKey
,
57 const unsigned char *privData
,
62 feeReturn
feePubKeyInitFromPrivDataKeyBits(feePubKey pubKey
,
63 const unsigned char *privData
,
65 unsigned keyBits
, /* key size in bits */
66 feePrimeType primeType
, /* FPT_Fefault means "best one" */
67 feeCurveType curveType
, /* FCT_Default means "best one" */
71 * Init feePubKey from private "password" and from data curve parameters
72 * matching existing oldKey. Incoming password data will
73 * be processed with digests before use if hashPrivData is true, otherwise
74 * it'll be used as is. In the 'as is' case, the privData must be at least
75 * as large as the key being created.
78 feeReturn
feePubKeyInitFromKey(feePubKey pubKey
,
79 const unsigned char *privData
,
85 *** Exportable key blob support.
87 *** Currently there are three different ways of representing a feePubKey in
88 *** an exportable format.
90 *** Raw blob: basic native blob format.
91 *** DER blob: DER-encoded. Currently not available in ANSI C version of
92 *** CryptKit library without additional porting; the OS X version of
93 *** Apple implements this functionality via SNACC-generated C++ classes.
94 *** KeyString: NULL-terminated ASCII C string, suitable for application such as
95 *** distributing one's public key via email. Only public keys (not
96 *** private) can be exported and imported via KeyStrings.
100 * Obtain portable public and private key blobs from a key.
102 feeReturn
feePubKeyCreatePubBlob(feePubKey pubKey
,
103 unsigned char **keyBlob
, // mallocd and RETURNED
104 unsigned *keyBlobLen
); // RETURNED
106 feeReturn
feePubKeyCreatePrivBlob(feePubKey pubKey
,
107 unsigned char **keyBlob
, // mallocd and RETURNED
108 unsigned *keyBlobLen
); // RETURNED
111 * Init an empty feePubKey from a blob, public and private key versions.
113 feeReturn
feePubKeyInitFromPubBlob(feePubKey pubKey
,
114 unsigned char *keyBlob
,
115 unsigned keyBlobLen
);
116 feeReturn
feePubKeyInitFromPrivBlob(feePubKey pubKey
,
117 unsigned char *keyBlob
,
118 unsigned keyBlobLen
);
121 * Create a public key in the form of a null-terminated C string.
123 feeReturn
feePubKeyCreateKeyString(feePubKey pubKey
,
124 char **pubKeyString
, /* fmalloc'd & RETURNED */
125 unsigned *pubKeyStringLen
); /* RETURNED */
128 * Init feePubKey from a public key string.
130 feeReturn
feePubKeyInitFromKeyString(feePubKey pubKey
,
134 #if CRYPTKIT_DER_ENABLE
137 * DER format support.
138 * Obtain portable public and private DER-encoded key blobs from a key.
140 feeReturn
feePubKeyCreateDERPubBlob(feePubKey pubKey
,
141 unsigned char **keyBlob
, // mallocd and RETURNED
142 unsigned *keyBlobLen
); // RETURNED
144 feeReturn
feePubKeyCreateDERPrivBlob(feePubKey pubKey
,
145 unsigned char **keyBlob
, // mallocd and RETURNED
146 unsigned *keyBlobLen
); // RETURNED
149 * Init an empty feePubKey from a DER-encoded blob, public and private key versions.
151 feeReturn
feePubKeyInitFromDERPubBlob(feePubKey pubKey
,
152 unsigned char *keyBlob
,
154 feeReturn
feePubKeyInitFromDERPrivBlob(feePubKey pubKey
,
155 unsigned char *keyBlob
,
159 * X509 (public) and PKCS8 (private) key formatting.
161 feeReturn
feePubKeyCreateX509Blob(
162 feePubKey pubKey
, // public key
163 unsigned char **keyBlob
, // mallocd and RETURNED
164 unsigned *keyBlobLen
); // RETURNED
166 feeReturn
feePubKeyCreatePKCS8Blob(
167 feePubKey pubKey
, // private key
168 unsigned char **keyBlob
, // mallocd and RETURNED
169 unsigned *keyBlobLen
); // RETURNED
171 feeReturn
feePubKeyInitFromX509Blob(
172 feePubKey pubKey
, // public key
173 unsigned char *keyBlob
,
176 feeReturn
feePubKeyInitFromPKCS8Blob(
177 feePubKey pubKey
, // private key
178 unsigned char *keyBlob
,
182 * The native OpenSSL ECDSA key format contains both the private and public
183 * components in one blob. This throws a bit of a monkey wrench into the API
184 * here, as we only have one encoder - which requires a private key - and one
185 * decoder, which can result in the decoding of either a public or a private
188 feeReturn
feePubKeyCreateOpenSSLBlob(
189 feePubKey pubKey
, // private key
190 unsigned char **keyBlob
, // mallocd and RETURNED
191 unsigned *keyBlobLen
); // RETURNED
193 feeReturn
feePubKeyInitFromOpenSSLBlob(
194 feePubKey pubKey
, // private or public key
196 unsigned char *keyBlob
,
199 #endif /* CRYPTKIT_DER_ENABLE */
202 * ANSI X9.62/Certicom key support.
203 * Public key is 04 || x || y
204 * Private key is privData per Certicom SEC1 C.4.
206 feeReturn
feeCreateECDSAPubBlob(feePubKey pubKey
,
207 unsigned char **keyBlob
,
208 unsigned *keyBlobLen
);
210 feeReturn
feeCreateECDSAPrivBlob(feePubKey pubKey
,
211 unsigned char **keyBlob
,
212 unsigned *keyBlobLen
);
214 /* Caller determines depth from other sources (e.g. AlgId.Params) */
215 feeReturn
feePubKeyInitFromECDSAPubBlob(feePubKey pubKey
,
216 const unsigned char *keyBlob
,
220 feeReturn
feePubKeyInitFromECDSAPrivBlob(feePubKey pubKey
,
221 const unsigned char *keyBlob
,
227 *** Miscellaneous feePubKey functions.
231 * Given private-capable privKey, initialize pubKey to be its corresponding
234 feeReturn
feePubKeyInitPubKeyFromPriv(feePubKey privKey
,
238 * Returns non-zero if two keys are equivalent.
240 int feePubKeyIsEqual(feePubKey key1
,
244 * Returns non-zero if key is private-capable (i.e., capable of signing
247 int feePubKeyIsPrivate(feePubKey key
);
249 #if CRYPTKIT_KEY_EXCHANGE
252 * Generate a pad, for use with symmetric encryption, derived from two keys.
253 * 'myKey' must be created with private data (via feePubKeyInitFromPrivData()
254 * or feePubKeyInitFromKey().
256 feeReturn
feePubKeyCreatePad(feePubKey myKey
,
258 unsigned char **padData
, /* fmalloc'd & RETURNED */
259 unsigned *padDataLen
); /* RETURNED padData length in bytes */
261 #endif /* CRYPTKIT_KEY_EXCHANGE */
263 #if CRYPTKIT_HIGH_LEVEL_SIG
266 * The following two routines are implemented using primitives in the
267 * feeHash and feeDigitalSignature objects.
269 * Generate digital signature, ElGamal style.
271 feeReturn
feePubKeyCreateSignature(feePubKey pubKey
,
272 const unsigned char *data
,
274 unsigned char **signature
, /* fmalloc'd and RETURNED */
275 unsigned *signatureLen
); /* RETURNED */
278 * Verify digital signature, ElGamal style.
280 feeReturn
feePubKeyVerifySignature(feePubKey pubKey
,
281 const unsigned char *data
,
283 const unsigned char *signature
,
284 unsigned signatureLen
);
286 #if CRYPTKIT_ECDSA_ENABLE
289 * The following two routines are implemented using primitives in the
290 * feeHash and feeECDSA objects.
292 * Generate digital signature, ECDSA style.
294 feeReturn
feePubKeyCreateECDSASignature(feePubKey pubKey
,
295 const unsigned char *data
,
297 unsigned char **signature
, /* fmalloc'd and RETURNED */
298 unsigned *signatureLen
); /* RETURNED */
301 * Verify digital signature, ECDSA style.
303 feeReturn
feePubKeyVerifyECDSASignature(feePubKey pubKey
,
304 const unsigned char *data
,
306 const unsigned char *signature
,
307 unsigned signatureLen
);
309 #endif /* CRYPTKIT_ECDSA_ENABLE */
311 #endif /* CRYPTKIT_HIGH_LEVEL_SIG */
314 * Diffie-Hellman. Public key is specified either as a feePubKey or
315 * a ANSI X9.62 format public key string (0x04 | x | y). In either case
316 * the caller must ensure that the two keys are on the same curve.
317 * Output data is falloc'd here; caller must free. Output data is
318 * exactly the size of the curve's modulus in bytes.
320 feeReturn
feePubKeyECDH(
322 /* one of the following two is non-NULL */
324 const unsigned char *pubKeyStr
,
325 unsigned pubKeyStrLen
,
326 /* output fallocd and RETURNED here */
327 unsigned char **output
,
328 unsigned *outputLen
);
333 const char *feePubKeyAlgorithmName(void);
335 unsigned feePubKeyBitsize(feePubKey pubKey
);
341 #endif /*_CK_FEEPUBLICKEY_H_*/