1/*
2 * Copyright (c) 2011-2014 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
25#ifndef _SECOTRSESSIONPRIV_H_
26#define _SECOTRSESSIONPRIV_H_
27
28#include <CoreFoundation/CFBase.h>
29#include <CoreFoundation/CFRuntime.h>
30
31#include <Security/SecOTR.h>
32#include <corecrypto/ccn.h>
33#include <corecrypto/ccmode.h>
34#include <corecrypto/ccsha1.h>
35
36#include <CommonCrypto/CommonDigest.h>
37
38#include <dispatch/dispatch.h>
39
40#include <Security/SecOTRMath.h>
41#include <Security/SecOTRDHKey.h>
42
43__BEGIN_DECLS
44
45typedef enum {
46    kIdle,
47    kAwaitingDHKey,
48    kAwaitingRevealSignature,
49    kAwaitingSignature,
50    kDone
51} SecOTRAuthState;
52
53struct _SecOTRCacheElement {
54    uint8_t _fullKeyHash[CCSHA1_OUTPUT_SIZE];
55    uint8_t _publicKeyHash[CCSHA1_OUTPUT_SIZE];
56
57    uint8_t _sendMacKey[kOTRMessageMacKeyBytes];
58    uint8_t _sendEncryptionKey[kOTRMessageKeyBytes];
59
60    uint8_t _receiveMacKey[kOTRMessageMacKeyBytes];
61    uint8_t _receiveEncryptionKey[kOTRMessageKeyBytes];
62
63    uint64_t _counter;
64    uint64_t _theirCounter;
65
66};
67typedef struct _SecOTRCacheElement SecOTRCacheElement;
68
69#define kOTRKeyCacheSize 4
70
71struct _SecOTRSession {
72    CFRuntimeBase _base;
73
74    SecOTRAuthState _state;
75
76    SecOTRFullIdentityRef    _me;
77    SecOTRPublicIdentityRef  _them;
78
79    uint8_t _r[kOTRAuthKeyBytes];
80
81    CFDataRef _receivedDHMessage;
82    CFDataRef _receivedDHKeyMessage;
83
84    uint32_t _keyID;
85    SecOTRFullDHKeyRef _myKey;
86    SecOTRFullDHKeyRef _myNextKey;
87
88    uint32_t _theirKeyID;
89    SecOTRPublicDHKeyRef _theirPreviousKey;
90    SecOTRPublicDHKeyRef _theirKey;
91
92    CFMutableDataRef _macKeysToExpose;
93
94    dispatch_queue_t _queue;
95
96    SecOTRCacheElement _keyCache[kOTRKeyCacheSize];
97
98    bool _textOutput;
99    bool _compactAppleMessages;
100};
101
102CFDataRef SecOTRCopyIncomingBytes(CFDataRef incomingMessage);
103void SecOTRPrepareOutgoingBytes(CFMutableDataRef destinationMessage, CFMutableDataRef protectedMessage);
104
105__END_DECLS
106
107#endif
108