| 1 | /* Copyright (c) (2010-2012,2014-2022) Apple Inc. All rights reserved. |
| 2 | * |
| 3 | * corecrypto is licensed under Apple Inc.’s Internal Use License Agreement (which |
| 4 | * is contained in the License.txt file distributed with corecrypto) and only to |
| 5 | * people who accept that license. IMPORTANT: Any license rights granted to you by |
| 6 | * Apple Inc. (if any) are limited to internal use within your organization only on |
| 7 | * devices and computers you own or control, for the sole purpose of verifying the |
| 8 | * security characteristics and correct functioning of the Apple Software. You may |
| 9 | * not, directly or indirectly, redistribute the Apple Software or any portions thereof. |
| 10 | */ |
| 11 | |
| 12 | #ifndef _CORECRYPTO_CC_PRIV_H_ |
| 13 | #define _CORECRYPTO_CC_PRIV_H_ |
| 14 | |
| 15 | #include <corecrypto/cc.h> |
| 16 | |
| 17 | CC_PTRCHECK_CAPABLE_HEADER() |
| 18 | |
| 19 | #if !CC_EXCLAVEKIT |
| 20 | // Fork handlers for the stateful components of corecrypto. |
| 21 | void cc_atfork_prepare(void); |
| 22 | void cc_atfork_parent(void); |
| 23 | void cc_atfork_child(void); |
| 24 | #endif |
| 25 | |
| 26 | #ifndef __has_builtin |
| 27 | #define __has_builtin(x) 0 |
| 28 | #endif |
| 29 | |
| 30 | #ifndef __DECONST |
| 31 | #define __DECONST(type, var) ((type)(uintptr_t)(const void *)(var)) |
| 32 | #endif |
| 33 | |
| 34 | /* defines the following macros : |
| 35 | |
| 36 | CC_ARRAY_LEN: returns the number of elements in an array |
| 37 | |
| 38 | CC_ROR : Rotate Right 32 bits. Rotate count can be a variable. |
| 39 | CC_ROL : Rotate Left 32 bits. Rotate count can be a variable. |
| 40 | CC_RORc : Rotate Right 32 bits. Rotate count must be a constant. |
| 41 | CC_ROLc : Rotate Left 32 bits. Rotate count must be a constant. |
| 42 | |
| 43 | CC_ROR64 : Rotate Right 64 bits. Rotate count can be a variable. |
| 44 | CC_ROL64 : Rotate Left 64 bits. Rotate count can be a variable. |
| 45 | CC_ROR64c : Rotate Right 64 bits. Rotate count must be a constant. |
| 46 | CC_ROL64c : Rotate Left 64 bits. Rotate count must be a constant. |
| 47 | |
| 48 | CC_BSWAP : byte swap a 32 bits variable. |
| 49 | |
| 50 | CC_H2BE32 : convert a 32 bits value between host and big endian order. |
| 51 | CC_H2LE32 : convert a 32 bits value between host and little endian order. |
| 52 | |
| 53 | CC_BSWAP64 : byte swap a 64 bits variable |
| 54 | |
| 55 | CC_H2BE64 : convert a 64 bits value between host and big endian order |
| 56 | CC_H2LE64 : convert a 64 bits value between host and little endian order |
| 57 | |
| 58 | */ |
| 59 | |
| 60 | // RTKitOSPlatform should replace CC_MEMCPY with memcpy |
| 61 | #define CC_MEMCPY(D,S,L) cc_memcpy((D),(S),(L)) |
| 62 | #define CC_MEMMOVE(D,S,L) cc_memmove((D),(S),(L)) |
| 63 | #define CC_MEMSET(D,V,L) cc_memset((D),(V),(L)) |
| 64 | |
| 65 | #if CC_EFI |
| 66 | void *cc_memcpy(void *dst, const void *src, size_t len); |
| 67 | #define cc_memcpy_nochk(dst, src, len) cc_memcpy((dst), (src), (len)) |
| 68 | #elif __has_builtin(__builtin___memcpy_chk) && !defined(_MSC_VER) && !CC_SGX && !CC_ARM_ARCH_6M |
| 69 | #define cc_memcpy(dst, src, len) __builtin___memcpy_chk((dst), (src), (len), __builtin_object_size((dst), 1)) |
| 70 | #define cc_memcpy_nochk(dst, src, len) __builtin___memcpy_chk((dst), (src), (len), __builtin_object_size((dst), 0)) |
| 71 | #else |
| 72 | #define cc_memcpy(dst, src, len) memcpy((dst), (src), (len)) |
| 73 | #define cc_memcpy_nochk(dst, src, len) memcpy((dst), (src), (len)) |
| 74 | #endif |
| 75 | |
| 76 | #if CC_EFI |
| 77 | void *cc_memmove(void *dst, const void *src, size_t len); |
| 78 | #elif __has_builtin(__builtin___memmove_chk) && !defined(_MSC_VER) && !CC_SGX && !CC_ARM_ARCH_6M |
| 79 | #define cc_memmove(dst, src, len) __builtin___memmove_chk((dst), (src), (len), __builtin_object_size((dst), 1)) |
| 80 | #else |
| 81 | #define cc_memmove(dst, src, len) memmove((dst), (src), (len)) |
| 82 | #endif |
| 83 | |
| 84 | #if CC_EFI |
| 85 | void *cc_memset(void *dst, int val, size_t num); |
| 86 | #elif __has_builtin(__builtin___memset_chk) && !defined(_MSC_VER) && !CC_SGX && !CC_ARM_ARCH_6M |
| 87 | #define cc_memset(dst, val, len) __builtin___memset_chk((dst), (val), (len), __builtin_object_size((dst), 1)) |
| 88 | #else |
| 89 | #define cc_memset(dst, val, len) memset((dst), (val), (len)) |
| 90 | #endif |
| 91 | |
| 92 | #define CC_ARRAY_LEN(x) (sizeof((x))/sizeof((x)[0])) |
| 93 | |
| 94 | // MARK: - Loads and Store |
| 95 | |
| 96 | // 64 bit load & store big endian |
| 97 | #if defined(__x86_64__) && !defined(_MSC_VER) |
| 98 | CC_INLINE void cc_store64_be(uint64_t x, uint8_t cc_sized_by(8) * y) |
| 99 | { |
| 100 | __asm__("bswapq %1 \n\t" |
| 101 | "movq %1, %0 \n\t" |
| 102 | "bswapq %1 \n\t" |
| 103 | : "=m" (*(y)) |
| 104 | : "r" (x)); |
| 105 | } |
| 106 | CC_INLINE uint64_t cc_load64_be(const uint8_t cc_sized_by(8) * y) |
| 107 | { |
| 108 | uint64_t x; |
| 109 | __asm__("movq %1, %0 \n\t" |
| 110 | "bswapq %0 \n\t" |
| 111 | : "=r" (x) |
| 112 | : "m" (*(y))); |
| 113 | return x; |
| 114 | } |
| 115 | #else |
| 116 | CC_INLINE void cc_store64_be(uint64_t x, uint8_t cc_sized_by(8) * y) |
| 117 | { |
| 118 | y[0] = (uint8_t)(x >> 56); |
| 119 | y[1] = (uint8_t)(x >> 48); |
| 120 | y[2] = (uint8_t)(x >> 40); |
| 121 | y[3] = (uint8_t)(x >> 32); |
| 122 | y[4] = (uint8_t)(x >> 24); |
| 123 | y[5] = (uint8_t)(x >> 16); |
| 124 | y[6] = (uint8_t)(x >> 8); |
| 125 | y[7] = (uint8_t)(x); |
| 126 | } |
| 127 | CC_INLINE uint64_t cc_load64_be(const uint8_t cc_sized_by(8) * y) |
| 128 | { |
| 129 | return (((uint64_t)(y[0])) << 56) | (((uint64_t)(y[1])) << 48) | (((uint64_t)(y[2])) << 40) | (((uint64_t)(y[3])) << 32) | |
| 130 | (((uint64_t)(y[4])) << 24) | (((uint64_t)(y[5])) << 16) | (((uint64_t)(y[6])) << 8) | ((uint64_t)(y[7])); |
| 131 | } |
| 132 | #endif |
| 133 | |
| 134 | // 32 bit load & store big endian |
| 135 | #if (defined(__i386__) || defined(__x86_64__)) && !defined(_MSC_VER) |
| 136 | CC_INLINE void cc_store32_be(uint32_t x, uint8_t cc_sized_by(4) * y) |
| 137 | { |
| 138 | __asm__("bswapl %1 \n\t" |
| 139 | "movl %1, %0 \n\t" |
| 140 | "bswapl %1 \n\t" |
| 141 | : "=m" (*(y)) |
| 142 | : "r" (x)); |
| 143 | } |
| 144 | CC_INLINE uint32_t cc_load32_be(const uint8_t cc_sized_by(4) * y) |
| 145 | { |
| 146 | uint32_t x; |
| 147 | __asm__("movl %1, %0 \n\t" |
| 148 | "bswapl %0 \n\t" |
| 149 | : "=r" (x) |
| 150 | : "m" (*(y))); |
| 151 | return x; |
| 152 | } |
| 153 | #else |
| 154 | CC_INLINE void cc_store32_be(uint32_t x, uint8_t cc_sized_by(4) * y) |
| 155 | { |
| 156 | y[0] = (uint8_t)(x >> 24); |
| 157 | y[1] = (uint8_t)(x >> 16); |
| 158 | y[2] = (uint8_t)(x >> 8); |
| 159 | y[3] = (uint8_t)(x); |
| 160 | } |
| 161 | CC_INLINE uint32_t cc_load32_be(const uint8_t cc_sized_by(4) * y) |
| 162 | { |
| 163 | return (((uint32_t)(y[0])) << 24) | (((uint32_t)(y[1])) << 16) | (((uint32_t)(y[2])) << 8) | ((uint32_t)(y[3])); |
| 164 | } |
| 165 | #endif |
| 166 | |
| 167 | CC_INLINE void cc_store16_be(uint16_t x, uint8_t cc_sized_by(2) * y) |
| 168 | { |
| 169 | y[0] = (uint8_t)(x >> 8); |
| 170 | y[1] = (uint8_t)(x); |
| 171 | } |
| 172 | CC_INLINE uint16_t cc_load16_be(const uint8_t cc_sized_by(2) * y) |
| 173 | { |
| 174 | return (uint16_t) (((uint16_t)(y[0])) << 8) | ((uint16_t)(y[1])); |
| 175 | } |
| 176 | |
| 177 | // 64 bit load & store little endian |
| 178 | CC_INLINE void cc_store64_le(uint64_t x, uint8_t cc_sized_by(8) * y) |
| 179 | { |
| 180 | y[7] = (uint8_t)(x >> 56); |
| 181 | y[6] = (uint8_t)(x >> 48); |
| 182 | y[5] = (uint8_t)(x >> 40); |
| 183 | y[4] = (uint8_t)(x >> 32); |
| 184 | y[3] = (uint8_t)(x >> 24); |
| 185 | y[2] = (uint8_t)(x >> 16); |
| 186 | y[1] = (uint8_t)(x >> 8); |
| 187 | y[0] = (uint8_t)(x); |
| 188 | } |
| 189 | CC_INLINE uint64_t cc_load64_le(const uint8_t cc_sized_by(8) * y) |
| 190 | { |
| 191 | return (((uint64_t)(y[7])) << 56) | (((uint64_t)(y[6])) << 48) | (((uint64_t)(y[5])) << 40) | (((uint64_t)(y[4])) << 32) | |
| 192 | (((uint64_t)(y[3])) << 24) | (((uint64_t)(y[2])) << 16) | (((uint64_t)(y[1])) << 8) | ((uint64_t)(y[0])); |
| 193 | } |
| 194 | |
| 195 | // 32 bit load & store little endian |
| 196 | CC_INLINE void cc_store32_le(uint32_t x, uint8_t cc_sized_by(4) * y) |
| 197 | { |
| 198 | y[3] = (uint8_t)(x >> 24); |
| 199 | y[2] = (uint8_t)(x >> 16); |
| 200 | y[1] = (uint8_t)(x >> 8); |
| 201 | y[0] = (uint8_t)(x); |
| 202 | } |
| 203 | CC_INLINE uint32_t cc_load32_le(const uint8_t cc_sized_by(4) * y) |
| 204 | { |
| 205 | return (((uint32_t)(y[3])) << 24) | (((uint32_t)(y[2])) << 16) | (((uint32_t)(y[1])) << 8) | ((uint32_t)(y[0])); |
| 206 | } |
| 207 | |
| 208 | #if (CCN_UNIT_SIZE == 8) |
| 209 | #define cc_load_le cc_load64_le |
| 210 | #define cc_store_le cc_store64_le |
| 211 | #else |
| 212 | #define cc_load_le cc_load32_le |
| 213 | #define cc_store_le cc_store32_le |
| 214 | #endif |
| 215 | |
| 216 | // MARK: - Byte Swaps |
| 217 | |
| 218 | #if __has_builtin(__builtin_bswap32) |
| 219 | #define CC_BSWAP32(x) __builtin_bswap32(x) |
| 220 | #else |
| 221 | CC_INLINE uint32_t CC_BSWAP32(uint32_t x) |
| 222 | { |
| 223 | return |
| 224 | ((x & 0xff000000) >> 24) | |
| 225 | ((x & 0x00ff0000) >> 8) | |
| 226 | ((x & 0x0000ff00) << 8) | |
| 227 | ((x & 0x000000ff) << 24); |
| 228 | } |
| 229 | #endif |
| 230 | |
| 231 | #if __has_builtin(__builtin_bswap64) |
| 232 | #define CC_BSWAP64(x) __builtin_bswap64(x) |
| 233 | #else |
| 234 | CC_INLINE uint64_t CC_BSWAP64(uint64_t x) |
| 235 | { |
| 236 | return |
| 237 | ((x & 0xff00000000000000ULL) >> 56) | |
| 238 | ((x & 0x00ff000000000000ULL) >> 40) | |
| 239 | ((x & 0x0000ff0000000000ULL) >> 24) | |
| 240 | ((x & 0x000000ff00000000ULL) >> 8) | |
| 241 | ((x & 0x00000000ff000000ULL) << 8) | |
| 242 | ((x & 0x0000000000ff0000ULL) << 24) | |
| 243 | ((x & 0x000000000000ff00ULL) << 40) | |
| 244 | ((x & 0x00000000000000ffULL) << 56); |
| 245 | } |
| 246 | #endif |
| 247 | |
| 248 | #ifdef __LITTLE_ENDIAN__ |
| 249 | #define CC_H2BE32(x) CC_BSWAP32(x) |
| 250 | #define CC_H2LE32(x) (x) |
| 251 | #define CC_H2BE64(x) CC_BSWAP64(x) |
| 252 | #define CC_H2LE64(x) (x) |
| 253 | #else |
| 254 | #define CC_H2BE32(x) (x) |
| 255 | #define CC_H2LE32(x) CC_BSWAP32(x) |
| 256 | #define CC_H2BE64(x) (x) |
| 257 | #define CC_H2LE64(x) CC_BSWAP64(x) |
| 258 | #endif |
| 259 | |
| 260 | #define cc_ceiling(a,b) (((a)+((b)-1))/(b)) |
| 261 | #define CC_BITLEN_TO_BYTELEN(x) cc_ceiling((x), 8) |
| 262 | |
| 263 | #define CC_PROVIDES_ABORT (!(CC_BASEBAND || CC_EFI || CC_RTKITROM || CC_USE_SEPROM)) |
| 264 | |
| 265 | /*! |
| 266 | @function cc_abort |
| 267 | @abstract Abort execution unconditionally |
| 268 | */ |
| 269 | CC_NORETURN |
| 270 | void cc_abort(const char *msg); |
| 271 | |
| 272 | /*! |
| 273 | @function cc_try_abort |
| 274 | @abstract Abort execution iff the platform provides a function like @p abort() or @p panic() |
| 275 | |
| 276 | @discussion If the platform does not provide a means to abort execution, this function does nothing; therefore, callers should return an error code after calling this function. |
| 277 | */ |
| 278 | void cc_try_abort(const char *msg); |
| 279 | |
| 280 | #if __has_builtin(__builtin_expect) |
| 281 | #define CC_LIKELY(cond) __builtin_expect(!!(cond), 1) |
| 282 | #define CC_UNLIKELY(cond) __builtin_expect(!!(cond), 0) |
| 283 | #else |
| 284 | #define CC_LIKELY(cond) cond |
| 285 | #define CC_UNLIKELY(cond) cond |
| 286 | #endif |
| 287 | |
| 288 | #define cc_abort_if(cond, msg) \ |
| 289 | do { \ |
| 290 | if (CC_UNLIKELY(cond)) { \ |
| 291 | cc_abort(msg); \ |
| 292 | } \ |
| 293 | } while (0) |
| 294 | |
| 295 | void cc_try_abort_if(bool condition, const char *msg); |
| 296 | |
| 297 | /* |
| 298 | Unfortunately, since we export this symbol, this declaration needs |
| 299 | to be in a public header to satisfy TAPI. |
| 300 | |
| 301 | See fipspost_trace_priv.h for more details. |
| 302 | */ |
| 303 | extern const void *fipspost_trace_vtable; |
| 304 | |
| 305 | |
| 306 | // MARK: -- Deprecated macros |
| 307 | /* |
| 308 | Use `cc_store32_be`, `cc_store32_le`, `cc_store64_be`, `cc_store64_le`, and |
| 309 | `cc_load32_be`, `cc_load32_le`, `cc_load64_be`, `cc_load64_le` instead. |
| 310 | |
| 311 | CC_STORE32_BE : store 32 bit value in big endian in unaligned buffer. |
| 312 | CC_STORE32_LE : store 32 bit value in little endian in unaligned buffer. |
| 313 | CC_STORE64_BE : store 64 bit value in big endian in unaligned buffer. |
| 314 | CC_STORE64_LE : store 64 bit value in little endian in unaligned buffer. |
| 315 | CC_LOAD32_BE : load 32 bit value in big endian from unaligned buffer. |
| 316 | CC_LOAD32_LE : load 32 bit value in little endian from unaligned buffer. |
| 317 | CC_LOAD64_BE : load 64 bit value in big endian from unaligned buffer. |
| 318 | CC_LOAD64_LE : load 64 bit value in little endian from unaligned buffer. |
| 319 | CC_READ_LE32 : read a 32 bits little endian value |
| 320 | CC_WRITE_LE32 : write a 32 bits little endian value |
| 321 | CC_WRITE_LE64 : write a 64 bits little endian value |
| 322 | */ |
| 323 | |
| 324 | #define CC_STORE32_BE(x, y) cc_store32_be((uint32_t)(x), (uint8_t *)(y)) |
| 325 | #define CC_STORE32_LE(x, y) cc_store32_le((uint32_t)(x), (uint8_t *)(y)) |
| 326 | #define CC_STORE64_BE(x, y) cc_store64_be((uint64_t)(x), (uint8_t *)(y)) |
| 327 | #define CC_STORE64_LE(x, y) cc_store64_le((uint64_t)(x), (uint8_t *)(y)) |
| 328 | |
| 329 | #define CC_LOAD32_BE(x, y) ((x) = cc_load32_be((uint8_t *)(y))) |
| 330 | #define CC_LOAD32_LE(x, y) ((x) = cc_load32_le((uint8_t *)(y))) |
| 331 | #define CC_LOAD64_BE(x, y) ((x) = cc_load64_be((uint8_t *)(y))) |
| 332 | #define CC_LOAD64_LE(x, y) ((x) = cc_load64_le((uint8_t *)(y))) |
| 333 | |
| 334 | #define CC_READ_LE32(ptr) cc_load32_le((uint8_t *)(ptr)) |
| 335 | |
| 336 | #define CC_WRITE_LE32(ptr, x) cc_store32_le((uint32_t)(x), (uint8_t *)(ptr)) |
| 337 | #define CC_WRITE_LE64(ptr, x) cc_store64_le((uint64_t)(x), (uint8_t *)(ptr)) |
| 338 | |
| 339 | #endif /* _CORECRYPTO_CC_PRIV_H_ */ |
| 340 | |