]>
git.saurik.com Git - apple/security.git/blob - sec/Security/vmdh.c
5 * Created by Michael Brouwer on 11/7/06.
6 * Copyright (c) 2006-2007 Apple Inc. All Rights Reserved.
12 The functions provided in vmdh.h implement the crypto exchange required
13 for a Diffie-Hellman voicemail exchange.
18 #include <CommonCrypto/CommonCryptor.h>
19 #include <security_utilities/debugging.h>
21 #include <Security/SecInternal.h>
22 #include <Security/SecDH.h>
24 vmdh_t
vmdh_create(uint32_t g
, const uint8_t *p
, size_t p_len
,
25 const uint8_t *recip
, size_t recip_len
) {
27 if (SecDHCreate(g
, p
, p_len
, 0/*l*/, recip
, recip_len
, &dh
))
32 bool vmdh_generate_key(vmdh_t vmdh
, uint8_t *pub_key
, size_t *pub_key_len
) {
33 return !SecDHGenerateKeypair((SecDHContext
)vmdh
, pub_key
, pub_key_len
);
36 bool vmdh_encrypt_password(vmdh_t vmdh
,
37 const uint8_t *pub_key
, size_t pub_key_len
,
38 const uint8_t *pw
, size_t pw_len
, uint8_t *encpw
, size_t *encpw_len
) {
39 uint8_t aes_key
[kCCKeySizeAES128
];
40 size_t aes_key_len
= kCCKeySizeAES128
;
42 if (SecDHComputeKey((SecDHContext
)vmdh
, pub_key
, pub_key_len
,
43 aes_key
, &aes_key_len
)) {
47 /* Use the first 16 bytes in aes_key as an AES key. */
48 if (CCCrypt(kCCEncrypt
, kCCAlgorithmAES128
,
49 kCCOptionPKCS7Padding
, aes_key
, kCCKeySizeAES128
, NULL
,
50 pw
, pw_len
, encpw
, *encpw_len
, encpw_len
)) {
54 /* Zero out key material. */
55 bzero(aes_key
, kCCKeySizeAES128
);
60 void vmdh_destroy(vmdh_t vmdh
) {
61 return SecDHDestroy((SecDHContext
)vmdh
);