1/* 2 * Copyright (c) 2010 Apple Inc. All Rights Reserved. 3 * 4 * @APPLE_LICENSE_HEADER_START@ 5 * 6 * This file contains Original Code and/or Modifications of Original Code 7 * as defined in and that are subject to the Apple Public Source License 8 * Version 2.0 (the 'License'). You may not use this file except in 9 * compliance with the License. Please obtain a copy of the License at 10 * http://www.opensource.apple.com/apsl/ and read it before using this 11 * file. 12 * 13 * The Original Code and all software distributed under the License are 14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 * Please see the License for the specific language governing rights and 19 * limitations under the License. 20 * 21 * @APPLE_LICENSE_HEADER_END@ 22 */ 23 24#ifndef _CC_DH_H_ 25#define _CC_DH_H_ 26 27#include <Availability.h> 28 29#include <stddef.h> 30 31#ifdef __cplusplus 32extern "C" { 33#endif 34 35typedef struct CCDHRef_s *CCDHRef; 36 37typedef struct CCDHParameters_s *CCDHParameters; 38 39extern const CCDHParameters kCCDHRFC2409Group2 40 __OSX_AVAILABLE_STARTING(__MAC_10_8, __IPHONE_5_0); 41extern const CCDHParameters kCCDHRFC3526Group5 42 __OSX_AVAILABLE_STARTING(__MAC_10_8, __IPHONE_5_0); 43 44/*! 45 @function CCDHCreate 46 @abstract Creates a Diffie-Hellman context. 47 48 @param dhParameter The Diffie-Hellman Group to use (provides p and g). 49 The only appropriate values are kCCDHGenerator2 or 50 kCCDHGenerator5, defined above. 51 52 @result If unable to allocate memory this returns NULL. 53*/ 54 55CCDHRef 56CCDHCreate(CCDHParameters dhParameter) 57__OSX_AVAILABLE_STARTING(__MAC_10_8, __IPHONE_5_0); 58 59/*! 60 @function CCDHRelease 61 @abstract Releases a Diffie-Hellman context. 62 63 @param ref The Diffie-Hellman context to clear and deallocate. 64 65*/ 66 67void 68CCDHRelease(CCDHRef ref) 69__OSX_AVAILABLE_STARTING(__MAC_10_8, __IPHONE_5_0); 70 71/*! 72 @function CCDHGenerateKey 73 @abstract Generate the public key for use in a Diffie-Hellman handshake. 74 This value is returned as a byte string. 75 76 @param ref The Diffie-Hellman context. 77 @result returns -1 on failure. 78 79*/ 80 81 82int 83CCDHGenerateKey(CCDHRef ref, void *output, size_t *outputLength) 84__OSX_AVAILABLE_STARTING(__MAC_10_8, __IPHONE_5_0); 85 86/*! 87 @function CCDHComputeKey 88 @abstract Compute the shared Diffie-Hellman key using the peer's public 89 key. 90 91 @param sharedKey Shared key computed from the peer public key, p, g, 92 and the private key. 93 @param peerPubKey Public key received from the peer. 94 @param peerPubKeyLen Length of peer public key. 95 @param ref The Diffie-Hellman context to clear and deallocate. 96 97 @param returns the length of the shared key. 98 99*/ 100 101int 102CCDHComputeKey(unsigned char *sharedKey, size_t *sharedKeyLen, const void *peerPubKey, size_t peerPubKeyLen, CCDHRef ref) 103__OSX_AVAILABLE_STARTING(__MAC_10_8, __IPHONE_5_0); 104 105 106CCDHParameters 107CCDHParametersCreateFromData(const void *p, size_t pLen, const void *g, size_t gLen, size_t l) 108__OSX_AVAILABLE_STARTING(__MAC_10_8, __IPHONE_5_0); 109 110CCDHParameters 111CCDHParametersCreateFromPKCS3(const void *data, size_t len) 112__OSX_AVAILABLE_STARTING(__MAC_10_8, __IPHONE_5_0); 113 114size_t 115CCDHParametersPKCS3EncodeLength(CCDHParameters parms) 116__OSX_AVAILABLE_STARTING(__MAC_10_8, __IPHONE_5_0); 117 118size_t 119CCDHParametersPKCS3Encode(CCDHParameters parms, void *data, size_t dataAvailable) 120__OSX_AVAILABLE_STARTING(__MAC_10_8, __IPHONE_5_0); 121 122void 123CCDHParametersRelease(CCDHParameters parameters) 124__OSX_AVAILABLE_STARTING(__MAC_10_8, __IPHONE_5_0); 125 126#ifdef __cplusplus 127} 128#endif 129 130#endif /* _CC_DH_H_ */ 131