2 * Copyright (c) 2007-2008,2010,2012-2013 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
25 * SecDH.c - Implement the crypto required for a Diffie-Hellman key exchange.
29 #include <libDER/DER_Keys.h>
30 #include <corecrypto/ccdh.h>
31 #include <libDER/DER_Keys.h>
32 #include <libDER/DER_Encode.h>
33 #include <libDER/asn1Types.h>
34 #include <libkern/OSByteOrder.h>
35 #include "utilities/debugging.h"
36 #include <Security/SecInternal.h>
37 #include <Security/SecRandom.h>
39 #include <Security/SecBase.h>
40 #include <Security/SecBasePriv.h>
46 /* SecDHContext memory layout
54 static inline ccdh_gp_t
SecDH_gp(SecDHContext dh
)
59 static inline ccdh_full_ctx_t
SecDH_priv(SecDHContext dh
)
61 ccdh_gp_t gp
= SecDH_gp(dh
);
62 cc_size s
= ccn_sizeof_n(ccdh_gp_n(gp
));
63 ccdh_full_ctx_t priv
= (ccdh_full_ctx_t
)((char *) dh
+ ccdh_gp_size(s
));
68 size_t SecDHGetMaxKeyLength(SecDHContext dh
) {
70 ccdh_gp_t gp
= SecDH_gp(dh
);
71 cc_size s
= ccn_sizeof_n(ccdh_gp_n(gp
));
77 static inline size_t SecDH_context_size(size_t p_len
)
79 cc_size n
= ccn_nof_size(p_len
);
80 cc_size real_p_len
= ccn_sizeof_n(n
);
81 size_t context_size
= ccdh_gp_size(real_p_len
)+ccdh_full_ctx_size(real_p_len
);
85 /* Shared static functions. */
88 der2OSStatus(DERReturn derReturn
)
92 case DR_Success
: return errSecSuccess
;
93 case DR_EndOfSequence
: return errSecDecode
;
94 case DR_UnexpectedTag
: return errSecDecode
;
95 case DR_DecodeError
: return errSecDecode
;
96 case DR_Unimplemented
: return errSecUnimplemented
;
97 case DR_IncompleteSeq
: return errSecDecode
;
98 case DR_ParamErr
: return errSecParam
;
99 case DR_BufOverflow
: return errSecBufferTooSmall
;
100 default: return errSecInternal
;
104 static int dhRngCallback(struct ccrng_state
*rng
, unsigned long outlen
, void *out
)
106 return SecRandomCopyBytes(kSecRandomDefault
, outlen
, out
);
109 static struct ccrng_state dhrng
= {
110 .generate
= dhRngCallback
113 OSStatus
SecDHCreate(uint32_t g
, const uint8_t *p
, size_t p_len
,
114 uint32_t l
, const uint8_t *recip
, size_t recip_len
, SecDHContext
*pdh
)
116 cc_size n
= ccn_nof_size(p_len
);
117 size_t context_size
= SecDH_context_size(p_len
);
118 void *context
= malloc(context_size
);
119 cc_clear(context_size
, context
);
127 if(ccn_read_uint(n
, CCDH_GP_PRIME(gp
), p_len
, p
))
130 if(ccn_read_uint(n
+1, CCDH_GP_RECIP(gp
), recip_len
, recip
))
132 cczp_init_with_recip(CCDH_GP_ZP(gp
), CCDH_GP_RECIP(gp
));
133 } else if (cczp_init(CCDH_GP_ZP(gp
))) {
136 ccn_seti(n
, CCDH_GP_G(gp
), g
);
138 *pdh
= (SecDHContext
) context
;
140 return errSecSuccess
;
143 SecDHDestroy(context
);
145 return errSecInternal
;
149 /* this used to be in libgDH */
151 * Support for encoding and decoding DH parameter blocks.
152 * Apple form encodes the reciprocal of the prime p.
154 /* PKCS3, Openssl compatible */
159 DERItem recip
; /* Only used in Apple Custom blocks. */
162 static const DERItemSpec DER_DHParamsItemSpecs
[] =
164 { DER_OFFSET(DER_DHParams
, p
),
166 DER_DEC_NO_OPTS
| DER_ENC_SIGNED_INT
},
167 { DER_OFFSET(DER_DHParams
, g
),
169 DER_DEC_NO_OPTS
| DER_ENC_SIGNED_INT
},
170 { DER_OFFSET(DER_DHParams
, l
),
172 DER_DEC_OPTIONAL
| DER_ENC_SIGNED_INT
},
173 /* Not part of PKCS3 per-se, but we add it on just for kicks. Since
174 it's optional we will automatically decode any apple specific
175 params, but we won't add this section unless the caller asks
177 { DER_OFFSET(DER_DHParams
, recip
),
178 ASN1_PRIVATE
| ASN1_PRIMITIVE
| 0,
179 DER_DEC_OPTIONAL
| DER_ENC_SIGNED_INT
},
181 static const DERSize DER_NumDHParamsItemSpecs
=
182 sizeof(DER_DHParamsItemSpecs
) / sizeof(DERItemSpec
);
185 OSStatus
SecDHCreateFromParameters(const uint8_t *params
,
186 size_t params_len
, SecDHContext
*pdh
)
188 // We support DomainParameters as specified in PKCS#3
189 // (http://www.emc.com/emc-plus/rsa-labs/standards-initiatives/pkcs-3-diffie-hellman-key-agreement-standar.htm)
190 // DHParameter ::= SEQUENCE {
191 // prime INTEGER, -- p
192 // base INTEGER, -- g
193 // privateValueLength INTEGER OPTIONAL }
196 DERItem paramItem
= {(DERByte
*)params
, params_len
};
197 DER_DHParams decodedParams
;
200 drtn
= DERParseSequence(¶mItem
,
201 DER_NumDHParamsItemSpecs
, DER_DHParamsItemSpecs
,
202 &decodedParams
, sizeof(decodedParams
));
206 if (decodedParams
.l
.length
> 0) {
207 drtn
= DERParseInteger(&decodedParams
.l
, &l
);
211 cc_size n
= ccn_nof_size(decodedParams
.p
.length
);
212 cc_size p_len
= ccn_sizeof_n(n
);
213 size_t context_size
= ccdh_gp_size(p_len
)+ccdh_full_ctx_size(p_len
);
214 void *context
= malloc(context_size
);
216 return errSecAllocate
;
218 bzero(context
, context_size
);
220 ccdh_gp_t gp
= context
;
225 if(ccn_read_uint(n
, CCDH_GP_PRIME(gp
), decodedParams
.p
.length
, decodedParams
.p
.data
))
227 if(decodedParams
.recip
.length
) {
228 if(ccn_read_uint(n
+1, CCDH_GP_RECIP(gp
), decodedParams
.recip
.length
, decodedParams
.recip
.data
))
230 cczp_init_with_recip(CCDH_GP_ZP(gp
), CCDH_GP_RECIP(gp
));
231 } else if (cczp_init(CCDH_GP_ZP(gp
))) {
235 if(ccn_read_uint(n
, CCDH_GP_G(gp
), decodedParams
.g
.length
, decodedParams
.g
.data
))
238 *pdh
= (SecDHContext
) context
;
239 return errSecSuccess
;
242 SecDHDestroy(context
);
244 return errSecInvalidKey
;
247 OSStatus
SecDHCreateFromAlgorithmId(const uint8_t *alg
, size_t alg_len
,
249 DERAlgorithmId algorithmId
;
252 algId
.data
= (uint8_t *)alg
;
253 algId
.length
= alg_len
;
255 DERReturn drtn
= DERParseSequence(&algId
,
256 DERNumAlgorithmIdItemSpecs
, DERAlgorithmIdItemSpecs
,
257 &algorithmId
, sizeof(algorithmId
));
258 if (drtn
!= DR_Success
)
259 return der2OSStatus(drtn
);
261 return SecDHCreateFromParameters(algorithmId
.params
.data
,
262 algorithmId
.params
.length
, pdh
);
265 OSStatus
SecDHGenerateKeypair(SecDHContext dh
, uint8_t *pub_key
,
269 ccdh_gp_t gp
= SecDH_gp(dh
);
270 ccdh_full_ctx_t priv
= SecDH_priv(dh
);
272 if((result
= ccdh_generate_key(gp
, &dhrng
, priv
)))
275 /* output y as a big endian byte buffer */
276 size_t ylen
= ccn_write_uint_size(ccdh_gp_n(gp
), ccdh_ctx_y(priv
));
277 if(*pub_key_len
< ylen
)
278 return errSecBufferTooSmall
;
279 ccn_write_uint(ccdh_gp_n(gp
),ccdh_ctx_y(priv
), ylen
, pub_key
);
282 return errSecSuccess
;
285 OSStatus
SecDHComputeKey(SecDHContext dh
,
286 const uint8_t *pub_key
, size_t pub_key_len
,
287 uint8_t *computed_key
, size_t *computed_key_len
)
289 ccdh_gp_t gp
= SecDH_gp(dh
);
290 ccdh_full_ctx_t priv
= SecDH_priv(dh
);
291 ccdh_pub_ctx_decl_gp(gp
, pub
);
292 cc_size n
= ccdh_gp_n(gp
);
295 if(ccdh_import_pub(gp
, pub_key_len
, pub_key
, pub
))
296 return errSecInvalidKey
;
298 //ccdh_compute_shared_secret() cannot be used directly, because it doesn't allow truncated output. Buffering is needed.
299 if(ccdh_compute_key(priv
, pub
, r
))
300 return errSecInvalidKey
;
302 ccn_write_uint(n
, r
, *computed_key_len
, computed_key
);
303 size_t out_size
= ccn_write_uint_size(n
, r
);
304 if(out_size
< *computed_key_len
)
305 *computed_key_len
=out_size
;
307 return errSecSuccess
;
310 void SecDHDestroy(SecDHContext dh
) {
311 /* Zero out key material. */
312 ccdh_gp_t gp
= SecDH_gp(dh
);
313 cc_size p_len
= ccn_sizeof_n(ccdh_gp_n(gp
));
314 size_t context_size
= SecDH_context_size(p_len
);
316 cc_clear(context_size
, dh
);
320 /* Max encoded size for standard (PKCS3) parameters */
321 #define DH_ENCODED_PARAM_SIZE(primeSizeInBytes) \
322 DER_MAX_ENCODED_SIZE( \
323 DER_MAX_ENCODED_SIZE(primeSizeInBytes) + /* g */ \
324 DER_MAX_ENCODED_SIZE(primeSizeInBytes) + /* p */ \
325 DER_MAX_ENCODED_SIZE(4)) /* l */
328 OSStatus
SecDHEncodeParams(CFDataRef g
, CFDataRef p
,
329 CFDataRef l
, CFDataRef recip
,
333 DER_DHParams derParams
=
336 .length
= CFDataGetLength(p
),
337 .data
= (DERByte
*)CFDataGetBytePtr(p
),
340 .length
= CFDataGetLength(g
),
341 .data
= (DERByte
*)CFDataGetBytePtr(g
),
344 .length
= l
?CFDataGetLength(l
):0,
345 .data
= (DERByte
*)(l
?CFDataGetBytePtr(l
):NULL
),
348 .length
= recip
?CFDataGetLength(recip
):0,
349 .data
= (DERByte
*)(recip
?CFDataGetBytePtr(recip
):NULL
),
353 DERSize ioLen
= DERLengthOfEncodedSequence(ASN1_CONSTR_SEQUENCE
,
355 DER_NumDHParamsItemSpecs
, DER_DHParamsItemSpecs
);
357 DERByte
*der
= malloc(ioLen
);
358 // FIXME: What if this fails - we should probably not have a malloc here ?
360 ortn
= (int)DEREncodeSequence(ASN1_CONSTR_SEQUENCE
,
362 DER_NumDHParamsItemSpecs
, DER_DHParamsItemSpecs
,
367 *params
=CFDataCreate(kCFAllocatorDefault
, der
, ioLen
);
369 // FIXME: we should just allocate the CFDataRef
376 OSStatus
SecDHDecodeParams(CFDataRef
*g
, CFDataRef
*p
,
377 CFDataRef
*l
, CFDataRef
*r
,
381 DERItem paramItem
= {(DERByte
*)CFDataGetBytePtr(params
), CFDataGetLength(params
)};
382 DER_DHParams decodedParams
;
384 drtn
= DERParseSequence(¶mItem
,
385 DER_NumDHParamsItemSpecs
, DER_DHParamsItemSpecs
,
386 &decodedParams
, sizeof(decodedParams
));
390 if(g
) *g
=CFDataCreate(kCFAllocatorDefault
, decodedParams
.g
.data
, decodedParams
.g
.length
);
391 if(p
) *p
=CFDataCreate(kCFAllocatorDefault
, decodedParams
.p
.data
, decodedParams
.p
.length
);
392 if(l
) *l
=CFDataCreate(kCFAllocatorDefault
, decodedParams
.l
.data
, decodedParams
.l
.length
);
393 if(r
) *r
=CFDataCreate(kCFAllocatorDefault
, decodedParams
.recip
.data
, decodedParams
.recip
.length
);
395 return errSecSuccess
;