1//
2//  OTRMath.h
3//  libsecurity_libSecOTR
4//
5//  Created by Mitch Adler on 1/28/11.
6//  Copyright 2011 Apple Inc. All rights reserved.
7//
8
9#ifndef _SECOTRMATH_H_
10#define _SECOTRMATH_H_
11
12#include <CoreFoundation/CFBase.h>
13
14#include <corecrypto/ccn.h>
15#include <corecrypto/ccaes.h>
16#include <corecrypto/ccmode.h>
17
18#define kOTRAuthKeyBytes 16
19#define kOTRAuthMACKeyBytes 32
20
21#define kOTRMessageKeyBytes 16
22#define kOTRMessageMacKeyBytes 20
23
24#define kExponentiationBits 1536
25#define kExponentiationUnits ccn_nof(kExponentiationBits)
26#define kExponentiationBytes ((kExponentiationBits+7)/8)
27
28#define kSHA256HMAC160Bits  160
29#define kSHA256HMAC160Bytes (kSHA256HMAC160Bits/8)
30
31// Result and exponent are expected to be kExponentiationUnits big.
32void OTRExponentiate(cc_unit* res, const cc_unit* base, const cc_unit* exponent);
33void OTRGroupExponentiate(cc_unit* result, const cc_unit* exponent);
34
35OSStatus GetRandomBytesInLSBs(size_t bytesOfRandomness, size_t n, cc_unit* place);
36OSStatus FillWithRandomBytes(size_t n, cc_unit* place);
37
38typedef enum {
39    kSSID = 0x00,
40    kCs = 0x01,
41    kM1 = 0x02,
42    kM2 = 0x03,
43    kM1Prime = 0x04,
44    kM2Prime = 0x05
45} KeyType;
46
47
48void DeriveOTR256BitsFromS(KeyType whichKey, size_t sSize, const cc_unit* s, size_t keySize, uint8_t* key);
49void DeriveOTR128BitPairFromS(KeyType whichHalf, size_t sSize, const cc_unit* s,
50                              size_t firstKeySize, uint8_t* firstKey,
51                              size_t secondKeySize, uint8_t* secondKey);
52void DeriveOTR64BitsFromS(KeyType whichKey, size_t sSize, const cc_unit* s,
53                          size_t firstKeySize, uint8_t* firstKey);
54
55
56void AES_CTR_HighHalf_Transform(size_t keySize, const uint8_t* key,
57                                uint64_t highHalf,
58                                size_t howMuch, const uint8_t* from,
59                                uint8_t* to);
60
61void AES_CTR_IV0_Transform(size_t keySize, const uint8_t* key,
62                           size_t howMuch, const uint8_t* from,
63                           uint8_t* to);
64
65#endif
66