Skip to content

Commit 891e0b9

Browse files
committed
swift package add dep/corecrypto
Signed-off-by: Joseph Mattello <mail@joemattiello.com>
1 parent 9c3a57c commit 891e0b9

79 files changed

Lines changed: 16265 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//
2+
// CoreCryptoMacros.swift
3+
// AltSign
4+
//
5+
// Created by Riley Testut on 8/20/20.
6+
// Copyright © 2020 Riley Testut. All rights reserved.
7+
//
8+
9+
@_exported import CCoreCrypto
10+
11+
/// Reimplementations of various CoreCrypto macros that are unavailable in Swift.
12+
13+
public func ccdigest_ctx_size(_ stateSize: Int, _ blockSize: Int) -> Int
14+
{
15+
(stateSize + MemoryLayout<UInt64>.size + blockSize + MemoryLayout<UInt32>.size)
16+
}
17+
18+
public func ccdigest_di_size(_ digestInfo: UnsafePointer<ccdigest_info>) -> Int
19+
{
20+
ccdigest_ctx_size(digestInfo.pointee.state_size, digestInfo.pointee.block_size)
21+
}
22+
23+
public func ccsrp_gpbuf_size(_ group: ccdh_const_gp_t) -> Int
24+
{
25+
ccdh_ccn_size(group) * 4
26+
}
27+
28+
public func ccsrp_dibuf_size(_ digestInfo: UnsafePointer<ccdigest_info>) -> Int
29+
{
30+
digestInfo.pointee.output_size * 4
31+
}
32+
33+
public func ccsrp_sizeof_srp(_ digestInfo: UnsafePointer<ccdigest_info>, _ group: ccdh_const_gp_t) -> Int
34+
{
35+
MemoryLayout<ccsrp_ctx>.size + ccsrp_gpbuf_size(group) + ccsrp_dibuf_size(digestInfo)
36+
}
37+
38+
public func cchmac_ctx_size(_ stateSize: Int, _ blockSize: Int) -> Int
39+
{
40+
ccdigest_ctx_size(stateSize, blockSize) + stateSize
41+
}
42+
43+
public func cchmac_di_size(_ digestInfo: UnsafePointer<ccdigest_info>) -> Int
44+
{
45+
if #available(iOS 14, macOS 11, *)
46+
{
47+
// CoreCrypto headers aren't accurate for iOS 14 or macOS 11 yet,
48+
// so return bigger buffer size to ensure against invalid memory access.
49+
return cchmac_ctx_size(digestInfo.pointee.state_size, digestInfo.pointee.block_size) * 2
50+
}
51+
else
52+
{
53+
return cchmac_ctx_size(digestInfo.pointee.state_size, digestInfo.pointee.block_size)
54+
}
55+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// ccsrp.c
3+
// AltSign
4+
//
5+
// Created by Riley Testut on 6/25/20.
6+
// Copyright © 2020 Riley Testut. All rights reserved.
7+
//
8+
9+
#include "ccsrp.h"
10+
11+
#import <Foundation/Foundation.h>
12+
13+
cc_unit *srp_ccn(void *srp)
14+
{
15+
// Memory layout of ccsrp_ctx changed between iOS 13/macOS 10.15 and iOS 14/macOS 11.
16+
// Dynamically cast to correct memory layout to ensure we access valid memory.
17+
18+
#if TARGET_OS_IPHONE
19+
if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion){ 14, 0, 0 }])
20+
#else
21+
if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion){ 10, 16, 0 }])
22+
#endif
23+
{
24+
return SRP_CCN((ccsrp_ctx_t)(srp));
25+
}
26+
else
27+
{
28+
return SRP_CCN((ccsrp_ctx_t_legacy)(srp));
29+
}
30+
}
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
/*
2+
* Copyright (c) 2010,2011,2012,2014,2015,2016,2017,2018 Apple Inc. All rights reserved.
3+
*
4+
* corecrypto Internal Use License Agreement
5+
*
6+
* IMPORTANT: This Apple corecrypto software is supplied to you by Apple Inc. ("Apple")
7+
* in consideration of your agreement to the following terms, and your download or use
8+
* of this Apple software constitutes acceptance of these terms. If you do not agree
9+
* with these terms, please do not download or use this Apple software.
10+
*
11+
* 1. As used in this Agreement, the term "Apple Software" collectively means and
12+
* includes all of the Apple corecrypto materials provided by Apple here, including
13+
* but not limited to the Apple corecrypto software, frameworks, libraries, documentation
14+
* and other Apple-created materials. In consideration of your agreement to abide by the
15+
* following terms, conditioned upon your compliance with these terms and subject to
16+
* these terms, Apple grants you, for a period of ninety (90) days from the date you
17+
* download the Apple Software, a limited, non-exclusive, non-sublicensable license
18+
* under Apple’s copyrights in the Apple Software to make a reasonable number of copies
19+
* of, compile, and run the Apple Software internally within your organization only on
20+
* devices and computers you own or control, for the sole purpose of verifying the
21+
* security characteristics and correct functioning of the Apple Software; provided
22+
* that you must retain this notice and the following text and disclaimers in all
23+
* copies of the Apple Software that you make. You may not, directly or indirectly,
24+
* redistribute the Apple Software or any portions thereof. The Apple Software is only
25+
* licensed and intended for use as expressly stated above and may not be used for other
26+
* purposes or in other contexts without Apple's prior written permission. Except as
27+
* expressly stated in this notice, no other rights or licenses, express or implied, are
28+
* granted by Apple herein.
29+
*
30+
* 2. The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
31+
* WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES
32+
* OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING
33+
* THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS,
34+
* SYSTEMS, OR SERVICES. APPLE DOES NOT WARRANT THAT THE APPLE SOFTWARE WILL MEET YOUR
35+
* REQUIREMENTS, THAT THE OPERATION OF THE APPLE SOFTWARE WILL BE UNINTERRUPTED OR
36+
* ERROR-FREE, THAT DEFECTS IN THE APPLE SOFTWARE WILL BE CORRECTED, OR THAT THE APPLE
37+
* SOFTWARE WILL BE COMPATIBLE WITH FUTURE APPLE PRODUCTS, SOFTWARE OR SERVICES. NO ORAL
38+
* OR WRITTEN INFORMATION OR ADVICE GIVEN BY APPLE OR AN APPLE AUTHORIZED REPRESENTATIVE
39+
* WILL CREATE A WARRANTY.
40+
*
41+
* 3. IN NO EVENT SHALL APPLE BE LIABLE FOR ANY DIRECT, SPECIAL, INDIRECT, INCIDENTAL
42+
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
43+
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ARISING
44+
* IN ANY WAY OUT OF THE USE, REPRODUCTION, COMPILATION OR OPERATION OF THE APPLE
45+
* SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING
46+
* NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
47+
* POSSIBILITY OF SUCH DAMAGE.
48+
*
49+
* 4. This Agreement is effective until terminated. Your rights under this Agreement will
50+
* terminate automatically without notice from Apple if you fail to comply with any term(s)
51+
* of this Agreement. Upon termination, you agree to cease all use of the Apple Software
52+
* and destroy all copies, full or partial, of the Apple Software. This Agreement will be
53+
* governed and construed in accordance with the laws of the State of California, without
54+
* regard to its choice of law rules.
55+
*
56+
* You may report security issues about Apple products to product-security@apple.com,
57+
* as described here:  https://www.apple.com/support/security/. Non-security bugs and
58+
* enhancement requests can be made via https://bugreport.apple.com as described
59+
* here: https://developer.apple.com/bug-reporting/
60+
*
61+
* EA1350
62+
* 10/5/15
63+
*/
64+
65+
#ifndef _CORECRYPTO_CC_H_
66+
#define _CORECRYPTO_CC_H_
67+
68+
#include <corecrypto/cc_config.h>
69+
#include <string.h>
70+
#include <stdint.h>
71+
72+
/* Provide a general purpose macro concat method. */
73+
#define cc_concat_(a, b) a##b
74+
#define cc_concat(a, b) cc_concat_(a, b)
75+
76+
/* Manage asserts here because a few functions in header public files do use asserts */
77+
#define cc_assert(x) assert(x)
78+
#if CC_KERNEL
79+
#include <kern/assert.h>
80+
#elif CC_USE_S3
81+
#define assert(args) // No assert in S3
82+
#else
83+
#include <assert.h>
84+
#endif
85+
86+
/* Provide a static assert that can be used to create compile-type failures. */
87+
#define cc_static_assert(e,m) \
88+
;enum { cc_concat(static_assert_, __COUNTER__) = 1/(int)(!!(e)) }
89+
90+
/* Declare a struct element with a guarenteed alignment of _alignment_.
91+
The resulting struct can be used to create arrays that are aligned by
92+
a certain amount. */
93+
#define cc_aligned_struct(_alignment_) \
94+
typedef struct { \
95+
uint8_t b[_alignment_]; \
96+
} CC_ALIGNED(_alignment_)
97+
98+
/* number of array elements used in a cc_ctx_decl */
99+
#define cc_ctx_n(_type_, _size_) ((_size_ + sizeof(_type_) - 1) / sizeof(_type_))
100+
101+
/* sizeof of a context declared with cc_ctx_decl */
102+
#define cc_ctx_sizeof(_type_, _size_) sizeof(_type_[cc_ctx_n(_type_, _size_)])
103+
104+
/*
105+
1. _alloca cannot be removed becasue this header file is compiled with both MSVC++ and with clang.
106+
2. The _MSC_VER version of cc_ctx_decl() is not compatible with the way *_decl macros as used in CommonCrypto, AppleKeyStore and SecurityFrameworks. To observe the incompatibilities and errors, use below definition. Corecrypto itself, accepts both deinitions
107+
#define cc_ctx_decl(_type_, _size_, _name_) _type_ _name_ ## _array[cc_ctx_n(_type_, (_size_))]; _type_ *_name_ = _name_ ## _array
108+
3. Never use sizeof() operator for the variables declared with cc_ctx_decl(), because it is not be compatible with the _MSC_VER version of cc_ctx_decl().
109+
*/
110+
#if defined(_MSC_VER)
111+
#define cc_ctx_decl(_type_, _size_, _name_) _type_ * _name_ = (_type_ *) _alloca(sizeof(_type_) * cc_ctx_n(_type_, _size_) )
112+
#else
113+
#define cc_ctx_decl(_type_, _size_, _name_) _type_ _name_ [cc_ctx_n(_type_, _size_)]
114+
#endif
115+
116+
/* bzero is deprecated. memset is the way to go */
117+
/* FWIW, L4, HEXAGON and ARMCC even with gnu compatibility mode don't have bzero */
118+
#define cc_zero(_size_,_data_) memset((_data_),0 ,(_size_))
119+
120+
/*!
121+
@brief cc_clear(len, dst) zeroizes array dst and it will not be optimized out.
122+
@discussion It is used to clear sensitive data, particularly when the are defined in the stack
123+
@param len number of bytes to be cleared in dst
124+
@param dst input array
125+
*/
126+
CC_NONNULL2
127+
void cc_clear(size_t len, void *dst);
128+
129+
#define cc_copy(_size_, _dst_, _src_) memcpy(_dst_, _src_, _size_)
130+
131+
CC_INLINE CC_NONNULL2 CC_NONNULL3 CC_NONNULL4
132+
void cc_xor(size_t size, void *r, const void *s, const void *t) {
133+
uint8_t *_r=(uint8_t *)r;
134+
const uint8_t *_s=(const uint8_t *)s;
135+
const uint8_t *_t=(const uint8_t *)t;
136+
while (size--) {
137+
_r[size] = _s[size] ^ _t[size];
138+
}
139+
}
140+
141+
/*!
142+
@brief cc_cmp_safe(num, pt1, pt2) compares two array ptr1 and ptr2 of num bytes.
143+
@discussion The execution time/cycles is independent of the data and therefore guarantees no leak about the data. However, the execution time depends on num.
144+
@param num number of bytes in each array
145+
@param ptr1 input array
146+
@param ptr2 input array
147+
@return returns 0 if the num bytes starting at ptr1 are identical to the num bytes starting at ptr2 and 1 if they are different or if num is 0 (empty arrays).
148+
*/
149+
CC_NONNULL2 CC_NONNULL3
150+
int cc_cmp_safe (size_t num, const void * ptr1, const void * ptr2);
151+
152+
/* Exchange S and T of any type. NOTE: Both and S and T are evaluated
153+
mutliple times and MUST NOT be expressions. */
154+
#define CC_SWAP(S,T) do { \
155+
__typeof__(S) _cc_swap_tmp = S; S = T; T = _cc_swap_tmp; \
156+
} while(0)
157+
158+
/* Return the maximum value between S and T. */
159+
#define CC_MAX(S, T) ({__typeof__(S) _cc_max_s = S; __typeof__(T) _cc_max_t = T; _cc_max_s > _cc_max_t ? _cc_max_s : _cc_max_t;})
160+
161+
/* Return the minimum value between S and T. */
162+
#define CC_MIN(S, T) ({__typeof__(S) _cc_min_s = S; __typeof__(T) _cc_min_t = T; _cc_min_s <= _cc_min_t ? _cc_min_s : _cc_min_t;})
163+
164+
#endif /* _CORECRYPTO_CC_H_ */

0 commit comments

Comments
 (0)