2 * Copyright (c) 2011-2012,2014 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 #include "Security_regressions.h"
27 #include <Security/SecOTRMath.h>
28 #include <Security/SecOTRDHKey.h>
29 #include <utilities/SecCFWrappers.h>
31 int otr_otrdh(int argc
, char *const * argv
)
35 SecOTRFullDHKeyRef aliceFull
= SecOTRFullDHKCreate(kCFAllocatorDefault
);
36 SecOTRPublicDHKeyRef alicePublic
= SecOTRPublicDHKCreateFromFullKey(kCFAllocatorDefault
, aliceFull
);
38 SecOTRFullDHKeyRef bobFull
= SecOTRFullDHKCreate(kCFAllocatorDefault
);
39 SecOTRPublicDHKeyRef bobPublic
= SecOTRPublicDHKCreateFromFullKey(kCFAllocatorDefault
, bobFull
);
41 SecOTRPublicDHKeyRef aliceCompactDeserialized
= NULL
;
42 SecOTRPublicDHKeyRef aliceDeserialized
= NULL
;
44 CFMutableDataRef aliceCompactSerialized
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
45 SecFDHKAppendCompactPublicSerialization(aliceFull
, aliceCompactSerialized
);
46 size_t aliceCompactLength
= CFDataGetLength(aliceCompactSerialized
);
47 const uint8_t *aliceCompactSerializationStart
= CFDataGetMutableBytePtr(aliceCompactSerialized
);
49 aliceCompactDeserialized
= SecOTRPublicDHKCreateFromCompactSerialization(kCFAllocatorDefault
, &aliceCompactSerializationStart
, &aliceCompactLength
);
50 ok(CFEqualSafe(aliceCompactDeserialized
, alicePublic
), "Compact serialized compare to created");
53 CFMutableDataRef aliceSerialized
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
54 SecFDHKAppendPublicSerialization(aliceFull
, aliceSerialized
);
55 size_t aliceLength
= CFDataGetLength(aliceSerialized
);
56 const uint8_t *aliceSerializationStart
= CFDataGetMutableBytePtr(aliceSerialized
);
58 aliceDeserialized
= SecOTRPublicDHKCreateFromSerialization(kCFAllocatorDefault
, &aliceSerializationStart
, &aliceLength
);
59 ok(CFEqualSafe(aliceDeserialized
, alicePublic
), "Serialized compare to created");
61 ok(CFEqualSafe(aliceCompactDeserialized
, aliceDeserialized
), "Serialized compared to compact serailized");
63 uint8_t aliceMessageKeys
[2][kOTRMessageKeyBytes
];
64 uint8_t aliceMacKeys
[2][kOTRMessageMacKeyBytes
];
66 SecOTRDHKGenerateOTRKeys(aliceFull
, bobPublic
,
67 aliceMessageKeys
[0], aliceMacKeys
[0],
68 aliceMessageKeys
[1], aliceMacKeys
[1]);
70 uint8_t bobMessageKeys
[2][kOTRMessageKeyBytes
];
71 uint8_t bobMacKeys
[2][kOTRMessageMacKeyBytes
];
73 SecOTRDHKGenerateOTRKeys(bobFull
, alicePublic
,
74 bobMessageKeys
[0], bobMacKeys
[0],
75 bobMessageKeys
[1], bobMacKeys
[1]);
78 ok(0 == memcmp(aliceMessageKeys
[0], bobMessageKeys
[1], sizeof(aliceMessageKeys
[0])), "Mac Keys don't match!!");
79 ok(0 == memcmp(aliceMessageKeys
[1], bobMessageKeys
[0], sizeof(aliceMessageKeys
[1])), "Mac Keys don't match!!");
80 ok(0 == memcmp(aliceMacKeys
[0], bobMacKeys
[1], sizeof(aliceMacKeys
[0])), "Mac Keys don't match!!");
81 ok(0 == memcmp(aliceMacKeys
[1], bobMacKeys
[0], sizeof(aliceMacKeys
[1])), "Mac Keys don't match!!");
82 CFReleaseNull(aliceCompactSerialized
);
83 CFReleaseNull(aliceSerialized
);
84 CFReleaseNull(aliceFull
);
85 CFReleaseNull(alicePublic
);
86 CFReleaseNull(bobFull
);
87 CFReleaseNull(bobPublic
);