1 /* 2 * Copyright (c) 2019-2023 Apple Inc. All rights reserved. 3 * 4 * @APPLE_OSREFERENCE_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. The rights granted to you under the License 10 * may not be used to create, or enable the creation or redistribution of, 11 * unlawful or unlicensed copies of an Apple operating system, or to 12 * circumvent, violate, or enable the circumvention or violation of, any 13 * terms of an Apple operating system software license agreement. 14 * 15 * Please obtain a copy of the License at 16 * http://www.opensource.apple.com/apsl/ and read it before using this file. 17 * 18 * The Original Code and all software distributed under the License are 19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 23 * Please see the License for the specific language governing rights and 24 * limitations under the License. 25 * 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 27 */ 28 #pragma once 29 30 #include <sys/cdefs.h> 31 #include <stdint.h> 32 #include <stdbool.h> 33 #include <libkern/crypto/sha2.h> 34 #include <mach/vm_types.h> 35 #include <pexpert/arm64/board_config.h> 36 #if CONFIG_SPTM 37 #include <arm64/sptm/sptm.h> 38 #else 39 #include <arm64/ppl/ppl_hib.h> 40 #endif /* CONFIG_SPTM */ 41 #include <IOKit/IOHibernatePrivate.h> 42 43 __BEGIN_DECLS 44 45 /** 46 * State representing where in the hibernation process a specific secure HMAC 47 * call is taking place. 48 */ 49 typedef enum { 50 SECURE_HMAC_HIB_NOT_STARTED = 0x1, 51 SECURE_HMAC_HIB_SETUP = 0x2, 52 SECURE_HMAC_HIB_WRITE_IMAGE = 0x4, 53 SECURE_HMAC_HIB_RESTORE = 0x8 54 } secure_hmac_hib_state_t; 55 56 void secure_hmac_init(void); 57 vm_address_t secure_hmac_get_reg_base(void); 58 vm_address_t secure_hmac_get_aes_reg_base(void); 59 vm_address_t secure_hmac_get_aes_offset(void); 60 61 void secure_hmac_hibernate_begin( 62 secure_hmac_hib_state_t state, 63 uint64_t *io_buffer_pages, 64 uint32_t num_io_buffer_pages); 65 void secure_hmac_hibernate_end(void); 66 67 void secure_hmac_reset(secure_hmac_hib_state_t state, bool wired_pages); 68 int secure_hmac_update_and_compress_page( 69 secure_hmac_hib_state_t state, 70 ppnum_t page_number, 71 const void **uncompressed, 72 void *compressed); 73 void secure_hmac_final(secure_hmac_hib_state_t state, uint8_t *output, size_t output_len); 74 void secure_hmac_fetch_hibseg_and_info( 75 /* out */ void *buffer, 76 /* in */ uint64_t buffer_len, 77 /* out */ IOHibernateHibSegInfo *info); 78 void secure_hmac_compute_rorgn_hmac(void); 79 void secure_hmac_fetch_rorgn_sha(uint8_t *output, size_t output_len); 80 void secure_hmac_fetch_rorgn_hmac(uint8_t *output, size_t output_len); 81 void secure_hmac_finalize_image( 82 const void *image_hash, 83 size_t image_hash_len, 84 uint8_t *hmac, 85 size_t hmac_len); 86 void secure_hmac_get_io_ranges(const hib_phys_range_t **io_ranges, size_t *num_io_ranges); 87 #if CONFIG_SPTM 88 bool hmac_is_io_buffer_page(uint64_t paddr); 89 #endif 90 91 __END_DECLS 92