├── SampleCode ├── debug_mock_key.bin └── SampleEnclave │ ├── Seal │ ├── Seal.lds │ ├── Seal.config.xml │ ├── Seal.edl │ ├── Seal_private.pem │ └── Seal.cpp │ ├── Enclave │ ├── Enclave.lds │ ├── Enclave.config.xml │ ├── Enclave.h │ ├── Enclave.cpp │ ├── TrustedLibrary │ │ ├── Libcxx.edl │ │ ├── Thread.edl │ │ ├── Libc.edl │ │ ├── Libc.cpp │ │ ├── Libcxx.cpp │ │ └── Thread.cpp │ ├── Enclave_private.pem │ ├── Enclave.edl │ └── Edger8rSyntax │ │ ├── Functions.cpp │ │ ├── Types.edl │ │ ├── Functions.edl │ │ ├── Arrays.cpp │ │ ├── Arrays.edl │ │ ├── Types.cpp │ │ ├── Pointers.cpp │ │ └── Pointers.edl │ ├── Include │ └── user_types.h │ └── App │ ├── TrustedLibrary │ ├── Libcxx.cpp │ ├── Libc.cpp │ └── Thread.cpp │ ├── App.h │ └── Edger8rSyntax │ ├── Functions.cpp │ ├── Types.cpp │ ├── Arrays.cpp │ └── Pointers.cpp ├── lib64 └── README ├── bin └── x64 │ └── README ├── Intel(R) SGX Protected Code Loader for Linux User Guide.pdf ├── Include └── sgx_pcl_guid.h ├── Makefile ├── Sources ├── unseal │ ├── pcl_unseal_internal.h │ ├── sim │ │ ├── pcl_deriv.cpp │ │ └── pcl_t_instructions.cpp │ ├── pcl_tSeal_util.cpp │ ├── pcl_tSeal_internal.cpp │ ├── pcl_tSeal.cpp │ └── pcl_sgx_get_key.cpp ├── pcl_internal.h ├── crypto │ ├── pcl_crypto_internal.h │ ├── pcl_modes_lcl.h │ ├── pcl_cmac.c │ ├── pcl_crypto.cpp │ └── pcl_md32_common.h ├── pcl_mem.cpp ├── Makefile └── pcl_entry.cpp ├── Tools └── Encryptip │ ├── Makefile │ └── encryptip.h ├── CONTRIBUTING.md ├── Common └── pcl_common.h └── License.txt /SampleCode/debug_mock_key.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel/linux-sgx-pcl/HEAD/SampleCode/debug_mock_key.bin -------------------------------------------------------------------------------- /lib64/README: -------------------------------------------------------------------------------- 1 | This folder holds the libraries which are generated by the build process. 2 | This folders structure follows the Intel(R) SGX PSW and SDK. -------------------------------------------------------------------------------- /bin/x64/README: -------------------------------------------------------------------------------- 1 | This folder holds the executable tool which is generated by the build process. 2 | This folders structure follows the Intel(R) SGX PSW and SDK. -------------------------------------------------------------------------------- /Intel(R) SGX Protected Code Loader for Linux User Guide.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel/linux-sgx-pcl/HEAD/Intel(R) SGX Protected Code Loader for Linux User Guide.pdf -------------------------------------------------------------------------------- /SampleCode/SampleEnclave/Seal/Seal.lds: -------------------------------------------------------------------------------- 1 | enclave.so 2 | { 3 | global: 4 | g_global_data_sim; 5 | g_global_data; 6 | enclave_entry; 7 | g_peak_heap_used; 8 | local: 9 | *; 10 | }; 11 | -------------------------------------------------------------------------------- /SampleCode/SampleEnclave/Enclave/Enclave.lds: -------------------------------------------------------------------------------- 1 | enclave.so 2 | { 3 | global: 4 | g_global_data_sim; 5 | g_global_data; 6 | enclave_entry; 7 | g_peak_heap_used; 8 | local: 9 | *; 10 | }; 11 | -------------------------------------------------------------------------------- /SampleCode/SampleEnclave/Seal/Seal.config.xml: -------------------------------------------------------------------------------- 1 | 2 | 0 3 | 0 4 | 0x40000 5 | 0x100000 6 | 10 7 | 1 8 | 9 | 0 10 | 0 11 | 0xFFFFFFFF 12 | 13 | -------------------------------------------------------------------------------- /SampleCode/SampleEnclave/Enclave/Enclave.config.xml: -------------------------------------------------------------------------------- 1 | 2 | 0 3 | 0 4 | 0x40000 5 | 0x100000 6 | 10 7 | 1 8 | 9 | 0 10 | 0 11 | 0xFFFFFFFF 12 | 13 | -------------------------------------------------------------------------------- /SampleCode/SampleEnclave/Include/user_types.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-2017 Intel Corporation. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in 12 | * the documentation and/or other materials provided with the 13 | * distribution. 14 | * * Neither the name of Intel Corporation nor the names of its 15 | * contributors may be used to endorse or promote products derived 16 | * from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | * 30 | */ 31 | 32 | 33 | /* User defined types */ 34 | 35 | 36 | #define LOOPS_PER_THREAD 500 37 | 38 | typedef void *buffer_t; 39 | typedef int array_t[10]; 40 | 41 | -------------------------------------------------------------------------------- /SampleCode/SampleEnclave/Enclave/Enclave.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-2017 Intel Corporation. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in 12 | * the documentation and/or other materials provided with the 13 | * distribution. 14 | * * Neither the name of Intel Corporation nor the names of its 15 | * contributors may be used to endorse or promote products derived 16 | * from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | * 30 | */ 31 | 32 | 33 | #ifndef _ENCLAVE_H_ 34 | #define _ENCLAVE_H_ 35 | 36 | #include 37 | #include 38 | 39 | #if defined(__cplusplus) 40 | extern "C" { 41 | #endif 42 | 43 | void printf(const char *fmt, ...); 44 | 45 | #if defined(__cplusplus) 46 | } 47 | #endif 48 | 49 | #endif /* !_ENCLAVE_H_ */ 50 | -------------------------------------------------------------------------------- /SampleCode/SampleEnclave/App/TrustedLibrary/Libcxx.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-2017 Intel Corporation. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in 12 | * the documentation and/or other materials provided with the 13 | * distribution. 14 | * * Neither the name of Intel Corporation nor the names of its 15 | * contributors may be used to endorse or promote products derived 16 | * from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | * 30 | */ 31 | 32 | #include 33 | 34 | #include "../App.h" 35 | #include "Enclave_u.h" 36 | 37 | /* ecall_libcxx_functions: 38 | * Invokes standard C++ functions. 39 | */ 40 | void ecall_libcxx_functions(void) 41 | { 42 | sgx_status_t ret = SGX_ERROR_UNEXPECTED; 43 | 44 | ret = ecall_exception(global_eid); 45 | if (ret != SGX_SUCCESS) 46 | abort(); 47 | 48 | ret = ecall_map(global_eid); 49 | if (ret != SGX_SUCCESS) 50 | abort(); 51 | } 52 | -------------------------------------------------------------------------------- /SampleCode/SampleEnclave/Enclave/Enclave.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-2017 Intel Corporation. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in 12 | * the documentation and/or other materials provided with the 13 | * distribution. 14 | * * Neither the name of Intel Corporation nor the names of its 15 | * contributors may be used to endorse or promote products derived 16 | * from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | * 30 | */ 31 | 32 | 33 | #include 34 | #include /* vsnprintf */ 35 | 36 | #include "Enclave.h" 37 | #include "Enclave_t.h" /* print_string */ 38 | 39 | /* 40 | * printf: 41 | * Invokes OCALL to display the enclave buffer to the terminal. 42 | */ 43 | void printf(const char *fmt, ...) 44 | { 45 | char buf[BUFSIZ] = {'\0'}; 46 | va_list ap; 47 | va_start(ap, fmt); 48 | vsnprintf(buf, BUFSIZ, fmt, ap); 49 | va_end(ap); 50 | ocall_print_string(buf); 51 | } 52 | -------------------------------------------------------------------------------- /SampleCode/SampleEnclave/Enclave/TrustedLibrary/Libcxx.edl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-2017 Intel Corporation. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in 12 | * the documentation and/or other materials provided with the 13 | * distribution. 14 | * * Neither the name of Intel Corporation nor the names of its 15 | * contributors may be used to endorse or promote products derived 16 | * from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | * 30 | */ 31 | 32 | 33 | /* Libcxx.edl - EDL sample for trusted C++ library. */ 34 | 35 | enclave { 36 | 37 | /* 38 | * A subset of the C++03 standard is supported. 39 | */ 40 | 41 | trusted { 42 | /* 43 | * Throw/catch exception inside the enclave. 44 | */ 45 | public void ecall_exception(void); 46 | 47 | /* 48 | * Utilize inside the enclave. 49 | */ 50 | public void ecall_map(void); 51 | }; 52 | }; 53 | -------------------------------------------------------------------------------- /SampleCode/SampleEnclave/App/TrustedLibrary/Libc.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-2017 Intel Corporation. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in 12 | * the documentation and/or other materials provided with the 13 | * distribution. 14 | * * Neither the name of Intel Corporation nor the names of its 15 | * contributors may be used to endorse or promote products derived 16 | * from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | * 30 | */ 31 | 32 | 33 | #include "../App.h" 34 | #include "Enclave_u.h" 35 | 36 | /* ecall_libc_functions: 37 | * Invokes standard C functions. 38 | */ 39 | void ecall_libc_functions(void) 40 | { 41 | sgx_status_t ret = SGX_ERROR_UNEXPECTED; 42 | 43 | ret = ecall_malloc_free(global_eid); 44 | if (ret != SGX_SUCCESS) 45 | abort(); 46 | 47 | int cpuid[4] = {0x1, 0x0, 0x0, 0x0}; 48 | ret = ecall_sgx_cpuid(global_eid, cpuid, 0x0); 49 | if (ret != SGX_SUCCESS) 50 | abort(); 51 | } 52 | -------------------------------------------------------------------------------- /SampleCode/SampleEnclave/Seal/Seal.edl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-2017 Intel Corporation. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in 12 | * the documentation and/or other materials provided with the 13 | * distribution. 14 | * * Neither the name of Intel Corporation nor the names of its 15 | * contributors may be used to endorse or promote products derived 16 | * from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | * 30 | */ 31 | 32 | /* Enclave.edl - Top EDL file. */ 33 | 34 | enclave { 35 | 36 | /* Import ECALL/OCALL from sub-directory EDLs. 37 | * [from]: specifies the location of EDL file. 38 | * [import]: specifies the functions to import, 39 | * [*]: implies to import all functions. 40 | */ 41 | 42 | 43 | trusted { 44 | public size_t ecall_get_sealed_blob_size (); 45 | public sgx_status_t ecall_generate_sealed_blob ( 46 | [out, count=sealed_blob_size]uint8_t* sealed_blob, 47 | size_t sealed_blob_size ); 48 | 49 | }; 50 | 51 | }; 52 | -------------------------------------------------------------------------------- /SampleCode/SampleEnclave/Enclave/TrustedLibrary/Thread.edl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-2017 Intel Corporation. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in 12 | * the documentation and/or other materials provided with the 13 | * distribution. 14 | * * Neither the name of Intel Corporation nor the names of its 15 | * contributors may be used to endorse or promote products derived 16 | * from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | * 30 | */ 31 | 32 | /* Thread.edl - EDL sample for trusted thread library. */ 33 | 34 | enclave { 35 | 36 | from "sgx_tstdc.edl" import sgx_thread_wait_untrusted_event_ocall, sgx_thread_set_untrusted_event_ocall, sgx_thread_setwait_untrusted_events_ocall, sgx_thread_set_multiple_untrusted_events_ocall; 37 | 38 | trusted { 39 | /* 40 | * Use SGX mutex. 41 | */ 42 | public size_t ecall_increase_counter(); 43 | 44 | /* 45 | * Use SGX condition variables. 46 | */ 47 | public void ecall_producer(); 48 | public void ecall_consumer(); 49 | 50 | }; 51 | }; 52 | -------------------------------------------------------------------------------- /SampleCode/SampleEnclave/Enclave/TrustedLibrary/Libc.edl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-2017 Intel Corporation. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in 12 | * the documentation and/or other materials provided with the 13 | * distribution. 14 | * * Neither the name of Intel Corporation nor the names of its 15 | * contributors may be used to endorse or promote products derived 16 | * from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | * 30 | */ 31 | 32 | 33 | /* Libc.edl - EDL sample for trusted C library. */ 34 | 35 | enclave { 36 | 37 | from "sgx_tstdc.edl" import sgx_oc_cpuidex; 38 | 39 | /* 40 | * A subset of the C99 standard is supported as well as SGX customized functions: 41 | * sgx_cpuid, etc. 42 | */ 43 | 44 | trusted { 45 | /* 46 | * Utilize malloc/free in enclave. 47 | */ 48 | public void ecall_malloc_free(void); 49 | 50 | /* 51 | * Utilize SGX version __cpuid() in enclave. 52 | */ 53 | public void ecall_sgx_cpuid([in, out] int cpuinfo[4], int leaf); 54 | }; 55 | }; 56 | -------------------------------------------------------------------------------- /SampleCode/SampleEnclave/Enclave/TrustedLibrary/Libc.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-2017 Intel Corporation. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in 12 | * the documentation and/or other materials provided with the 13 | * distribution. 14 | * * Neither the name of Intel Corporation nor the names of its 15 | * contributors may be used to endorse or promote products derived 16 | * from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | * 30 | */ 31 | 32 | 33 | #include 34 | #include "sgx_cpuid.h" 35 | 36 | #include "sgx_trts.h" 37 | #include "../Enclave.h" 38 | #include "Enclave_t.h" 39 | 40 | /* ecall_malloc_free: 41 | * Uses malloc/free to allocate/free trusted memory. 42 | */ 43 | void ecall_malloc_free(void) 44 | { 45 | void *ptr = malloc(100); 46 | assert(ptr != NULL); 47 | memset(ptr, 0x0, 100); 48 | free(ptr); 49 | } 50 | 51 | /* ecall_sgx_cpuid: 52 | * Uses sgx_cpuid to get CPU features and types. 53 | */ 54 | void ecall_sgx_cpuid(int cpuinfo[4], int leaf) 55 | { 56 | sgx_status_t ret = sgx_cpuid(cpuinfo, leaf); 57 | if (ret != SGX_SUCCESS) 58 | abort(); 59 | } 60 | -------------------------------------------------------------------------------- /SampleCode/SampleEnclave/Seal/Seal_private.pem: -------------------------------------------------------------------------------- 1 | -----BEGIN RSA PRIVATE KEY----- 2 | MIIG4gIBAAKCAYEAroOogvsj/fZDZY8XFdkl6dJmky0lRvnWMmpeH41Bla6U1qLZ 3 | AmZuyIF+mQC/cgojIsrBMzBxb1kKqzATF4+XwPwgKz7fmiddmHyYz2WDJfAjIveJ 4 | ZjdMjM4+EytGlkkJ52T8V8ds0/L2qKexJ+NBLxkeQLfV8n1mIk7zX7jguwbCG1Pr 5 | nEMdJ3Sew20vnje+RsngAzdPChoJpVsWi/K7cettX/tbnre1DL02GXc5qJoQYk7b 6 | 3zkmhz31TgFrd9VVtmUGyFXAysuSAb3EN+5VnHGr0xKkeg8utErea2FNtNIgua8H 7 | ONfm9Eiyaav1SVKzPHlyqLtcdxH3I8Wg7yqMsaprZ1n5A1v/levxnL8+It02KseD 8 | 5HqV4rf/cImSlCt3lpRg8U5E1pyFQ2IVEC/XTDMiI3c+AR+w2jSRB3Bwn9zJtFlW 9 | KHG3m1xGI4ck+Lci1JvWWLXQagQSPtZTsubxTQNx1gsgZhgv1JHVZMdbVlAbbRMC 10 | 1nSuJNl7KPAS/VfzAgEDAoIBgHRXxaynbVP5gkO0ug6Qw/E27wzIw4SmjsxG6Wpe 11 | K7kfDeRskKxESdsA/xCrKkwGwhcx1iIgS5+Qscd1Yg+1D9X9asd/P7waPmWoZd+Z 12 | AhlKwhdPsO7PiF3e1AzHhGQwsUTt/Y/aSI1MpHBvy2/s1h9mFCslOUxTmWw0oj/Q 13 | ldIEgWeNR72CE2+jFIJIyml6ftnb6qzPiga8Bm48ubKh0kvySOqnkmnPzgh+JBD6 14 | JnBmtZbfPT97bwTT+N6rnPqOOApvfHPf15kWI8yDbprG1l4OCUaIUH1AszxLd826 15 | 5IPM+8gINLRDP1MA6azECPjTyHXhtnSIBZCyWSVkc05vYmNXYUNiXWMajcxW9M02 16 | wKzFELO8NCEAkaTPxwo4SCyIjUxiK1LbQ9h8PSy4c1+gGP4LAMR8xqP4QKg6zdu9 17 | osUGG/xRe/uufgTBFkcjqBHtK5L5VI0jeNIUAgW/6iNbYXjBMJ0GfauLs+g1VsOm 18 | WfdgXzsb9DYdMa0OXXHypmV4GwKBwQDUwQj8RKJ6c8cT4vcWCoJvJF00+RFL+P3i 19 | Gx2DLERxRrDa8AVGfqaCjsR+3vLgG8V/py+z+dxZYSqeB80Qeo6PDITcRKoeAYh9 20 | xlT3LJOS+k1cJcEmlbbO2IjLkTmzSwa80fWexKu8/Xv6vv15gpqYl1ngYoqJM3pd 21 | vzmTIOi7MKSZ0WmEQavrZj8zK4endE3v0eAEeQ55j1GImbypSf7Idh7wOXtjZ7WD 22 | Dg6yWDrri+AP/L3gClMj8wsAxMV4ZR8CgcEA0fzDHkFa6raVOxWnObmRoDhAtE0a 23 | cjUj976NM5yyfdf2MrKy4/RhdTiPZ6b08/lBC/+xRfV3xKVGzacm6QjqjZrUpgHC 24 | 0LKiZaMtccCJjLtPwQd0jGQEnKfMFaPsnhOc5y8qVkCzVOSthY5qhz0XNotHHFmJ 25 | gffVgB0iqrMTvSL7IA2yqqpOqNRlhaYhNl8TiFP3gIeMtVa9rZy31JPgT2uJ+kfo 26 | gV7sdTPEjPWZd7OshGxWpT6QfVDj/T9T7L6tAoHBAI3WBf2DFvxNL2KXT2QHAZ9t 27 | k3imC4f7U+wSE6zILaDZyzygA4RUbwG0gv8/TJVn2P/Eynf76DuWHGlaiLWnCbSz 28 | Az2DHBQBBaku409zDQym3j1ugMRjzzSQWzJg0SIyBH3hTmnYcn3+Uqcp/lEBvGW6 29 | O+rsXFt3pukqJmIV8HzLGGaLm62BHUeZf3dyWm+i3p/hQAL7Xvu04QW70xuGqdr5 30 | afV7p5eaeQIJXyGQJ0eylV/90+qxjMKiB1XYg6WYvwKBwQCL/ddpgOdHJGN8uRom 31 | e7Zq0Csi3hGheMKlKbN3vcxT5U7MdyHtTZZOJbTvxKNNUNYH/8uD+PqDGNneb29G 32 | BfGzvI3EASyLIcGZF3OhKwZd0jUrWk2y7Vhob91jwp2+t73vdMbkKyI4mHOuXvGv 33 | fg95si9oO7EBT+Oqvhccd2J+F1IVXncccYnF4u5ZGWt5lLewN/pVr7MjjykeaHqN 34 | t+rfnQam2psA6fL4zS2zTmZPzR2tnY8Y1GBTi0Ko1OKd1HMCgcAb5cB/7/AQlhP9 35 | yQa04PLH9ygQkKKptZp7dy5WcWRx0K/hAHRoi2aw1wZqfm7VBNu2SLcs90kCCCxp 36 | 6C5sfJi6b8NpNbIPC+sc9wsFr7pGo9SFzQ78UlcWYK2Gu2FxlMjonhka5hvo4zvg 37 | WxlpXKEkaFt3gLd92m/dMqBrHfafH7VwOJY2zT3WIpjwuk0ZzmRg5p0pG/svVQEH 38 | NZmwRwlopysbR69B/n1nefJ84UO50fLh5s5Zr3gBRwbWNZyzhXk= 39 | -----END RSA PRIVATE KEY----- 40 | -------------------------------------------------------------------------------- /SampleCode/SampleEnclave/Enclave/Enclave_private.pem: -------------------------------------------------------------------------------- 1 | -----BEGIN RSA PRIVATE KEY----- 2 | MIIG4gIBAAKCAYEAroOogvsj/fZDZY8XFdkl6dJmky0lRvnWMmpeH41Bla6U1qLZ 3 | AmZuyIF+mQC/cgojIsrBMzBxb1kKqzATF4+XwPwgKz7fmiddmHyYz2WDJfAjIveJ 4 | ZjdMjM4+EytGlkkJ52T8V8ds0/L2qKexJ+NBLxkeQLfV8n1mIk7zX7jguwbCG1Pr 5 | nEMdJ3Sew20vnje+RsngAzdPChoJpVsWi/K7cettX/tbnre1DL02GXc5qJoQYk7b 6 | 3zkmhz31TgFrd9VVtmUGyFXAysuSAb3EN+5VnHGr0xKkeg8utErea2FNtNIgua8H 7 | ONfm9Eiyaav1SVKzPHlyqLtcdxH3I8Wg7yqMsaprZ1n5A1v/levxnL8+It02KseD 8 | 5HqV4rf/cImSlCt3lpRg8U5E1pyFQ2IVEC/XTDMiI3c+AR+w2jSRB3Bwn9zJtFlW 9 | KHG3m1xGI4ck+Lci1JvWWLXQagQSPtZTsubxTQNx1gsgZhgv1JHVZMdbVlAbbRMC 10 | 1nSuJNl7KPAS/VfzAgEDAoIBgHRXxaynbVP5gkO0ug6Qw/E27wzIw4SmjsxG6Wpe 11 | K7kfDeRskKxESdsA/xCrKkwGwhcx1iIgS5+Qscd1Yg+1D9X9asd/P7waPmWoZd+Z 12 | AhlKwhdPsO7PiF3e1AzHhGQwsUTt/Y/aSI1MpHBvy2/s1h9mFCslOUxTmWw0oj/Q 13 | ldIEgWeNR72CE2+jFIJIyml6ftnb6qzPiga8Bm48ubKh0kvySOqnkmnPzgh+JBD6 14 | JnBmtZbfPT97bwTT+N6rnPqOOApvfHPf15kWI8yDbprG1l4OCUaIUH1AszxLd826 15 | 5IPM+8gINLRDP1MA6azECPjTyHXhtnSIBZCyWSVkc05vYmNXYUNiXWMajcxW9M02 16 | wKzFELO8NCEAkaTPxwo4SCyIjUxiK1LbQ9h8PSy4c1+gGP4LAMR8xqP4QKg6zdu9 17 | osUGG/xRe/uufgTBFkcjqBHtK5L5VI0jeNIUAgW/6iNbYXjBMJ0GfauLs+g1VsOm 18 | WfdgXzsb9DYdMa0OXXHypmV4GwKBwQDUwQj8RKJ6c8cT4vcWCoJvJF00+RFL+P3i 19 | Gx2DLERxRrDa8AVGfqaCjsR+3vLgG8V/py+z+dxZYSqeB80Qeo6PDITcRKoeAYh9 20 | xlT3LJOS+k1cJcEmlbbO2IjLkTmzSwa80fWexKu8/Xv6vv15gpqYl1ngYoqJM3pd 21 | vzmTIOi7MKSZ0WmEQavrZj8zK4endE3v0eAEeQ55j1GImbypSf7Idh7wOXtjZ7WD 22 | Dg6yWDrri+AP/L3gClMj8wsAxMV4ZR8CgcEA0fzDHkFa6raVOxWnObmRoDhAtE0a 23 | cjUj976NM5yyfdf2MrKy4/RhdTiPZ6b08/lBC/+xRfV3xKVGzacm6QjqjZrUpgHC 24 | 0LKiZaMtccCJjLtPwQd0jGQEnKfMFaPsnhOc5y8qVkCzVOSthY5qhz0XNotHHFmJ 25 | gffVgB0iqrMTvSL7IA2yqqpOqNRlhaYhNl8TiFP3gIeMtVa9rZy31JPgT2uJ+kfo 26 | gV7sdTPEjPWZd7OshGxWpT6QfVDj/T9T7L6tAoHBAI3WBf2DFvxNL2KXT2QHAZ9t 27 | k3imC4f7U+wSE6zILaDZyzygA4RUbwG0gv8/TJVn2P/Eynf76DuWHGlaiLWnCbSz 28 | Az2DHBQBBaku409zDQym3j1ugMRjzzSQWzJg0SIyBH3hTmnYcn3+Uqcp/lEBvGW6 29 | O+rsXFt3pukqJmIV8HzLGGaLm62BHUeZf3dyWm+i3p/hQAL7Xvu04QW70xuGqdr5 30 | afV7p5eaeQIJXyGQJ0eylV/90+qxjMKiB1XYg6WYvwKBwQCL/ddpgOdHJGN8uRom 31 | e7Zq0Csi3hGheMKlKbN3vcxT5U7MdyHtTZZOJbTvxKNNUNYH/8uD+PqDGNneb29G 32 | BfGzvI3EASyLIcGZF3OhKwZd0jUrWk2y7Vhob91jwp2+t73vdMbkKyI4mHOuXvGv 33 | fg95si9oO7EBT+Oqvhccd2J+F1IVXncccYnF4u5ZGWt5lLewN/pVr7MjjykeaHqN 34 | t+rfnQam2psA6fL4zS2zTmZPzR2tnY8Y1GBTi0Ko1OKd1HMCgcAb5cB/7/AQlhP9 35 | yQa04PLH9ygQkKKptZp7dy5WcWRx0K/hAHRoi2aw1wZqfm7VBNu2SLcs90kCCCxp 36 | 6C5sfJi6b8NpNbIPC+sc9wsFr7pGo9SFzQ78UlcWYK2Gu2FxlMjonhka5hvo4zvg 37 | WxlpXKEkaFt3gLd92m/dMqBrHfafH7VwOJY2zT3WIpjwuk0ZzmRg5p0pG/svVQEH 38 | NZmwRwlopysbR69B/n1nefJ84UO50fLh5s5Zr3gBRwbWNZyzhXk= 39 | -----END RSA PRIVATE KEY----- 40 | -------------------------------------------------------------------------------- /Include/sgx_pcl_guid.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-2017 Intel Corporation. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in 12 | * the documentation and/or other materials provided with the 13 | * distribution. 14 | * * Neither the name of Intel Corporation nor the names of its 15 | * contributors may be used to endorse or promote products derived 16 | * from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | * 30 | */ 31 | 32 | #ifndef SGX_PCL_GUID_H 33 | #define SGX_PCL_GUID_H 34 | /* 35 | * GUID enables coupling of PCL lib (e.g. libsgx_pcl.a) and sealed blob 36 | * Before the PCL unseals the sealed blob, the PCL must verify the AAD 37 | * portion of the sealed blob equals the GUID embedded in enclave binary. 38 | */ 39 | // SGX_PCL_GUID_SIZE must equal 16 Bytes 40 | #ifndef SGX_PCL_GUID_SIZE 41 | 42 | #define SGX_PCL_GUID_SIZE (16) 43 | 44 | #else // SGX_PCL_GUID_SIZE is defined: // #ifndef SGX_PCL_GUID_SIZE 45 | 46 | #if (16 != SGX_PCL_GUID_SIZE) 47 | #error SGX_PCL_GUID_SIZE != 16 48 | #endif // #if (16 != SGX_PCL_GUID_SIZE) 49 | 50 | #endif // #ifndef SGX_PCL_GUID_SIZE 51 | 52 | /* g_pcl_guid is used by: 53 | * 1. Sealing enclave (decryption-key provisioning enclave) 54 | * as AAD for the sealed key blob. 55 | * 2. Encryption tool which embeds the GUID into the PCL enclave binary. 56 | */ 57 | uint8_t g_pcl_guid[SGX_PCL_GUID_SIZE] = 58 | {0x95, 0x48, 0x6e, 0x8f, 0x8f, 0x4a, 0x41, 0x4f, 0xb1, 0x27, 0x46, 0x21, 0xa8, 0x59, 0xa8, 0xac}; 59 | 60 | #endif // #ifndef SGX_PCL_GUID_H 61 | -------------------------------------------------------------------------------- /SampleCode/SampleEnclave/Enclave/Enclave.edl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-2017 Intel Corporation. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in 12 | * the documentation and/or other materials provided with the 13 | * distribution. 14 | * * Neither the name of Intel Corporation nor the names of its 15 | * contributors may be used to endorse or promote products derived 16 | * from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | * 30 | */ 31 | 32 | /* Enclave.edl - Top EDL file. */ 33 | 34 | enclave { 35 | 36 | include "user_types.h" /* buffer_t */ 37 | 38 | /* Import ECALL/OCALL from sub-directory EDLs. 39 | * [from]: specifies the location of EDL file. 40 | * [import]: specifies the functions to import, 41 | * [*]: implies to import all functions. 42 | */ 43 | 44 | from "Edger8rSyntax/Types.edl" import *; 45 | from "Edger8rSyntax/Pointers.edl" import *; 46 | from "Edger8rSyntax/Arrays.edl" import *; 47 | from "Edger8rSyntax/Functions.edl" import *; 48 | 49 | from "TrustedLibrary/Libc.edl" import *; 50 | from "TrustedLibrary/Libcxx.edl" import ecall_exception, ecall_map; 51 | from "TrustedLibrary/Thread.edl" import *; 52 | 53 | /* 54 | * ocall_print_string - invokes OCALL to display string buffer inside the enclave. 55 | * [in]: copy the string buffer to App outside. 56 | * [string]: specifies 'str' is a NULL terminated buffer. 57 | */ 58 | untrusted { 59 | void ocall_print_string([in, string] const char *str); 60 | }; 61 | 62 | }; 63 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (C) 2011-2017 Intel Corporation. All rights reserved. 3 | # 4 | # Redistribution and use in source and binary forms, with or without 5 | # modification, are permitted provided that the following conditions 6 | # are met: 7 | # 8 | # * Redistributions of source code must retain the above copyright 9 | # notice, this list of conditions and the following disclaimer. 10 | # * Redistributions in binary form must reproduce the above copyright 11 | # notice, this list of conditions and the following disclaimer in 12 | # the documentation and/or other materials provided with the 13 | # distribution. 14 | # * Neither the name of Intel Corporation nor the names of its 15 | # contributors may be used to endorse or promote products derived 16 | # from this software without specific prior written permission. 17 | # 18 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | # 30 | # 31 | 32 | # main PCL makefile to build lib and tool 33 | # SGX_MODE=HW/SIM (default=HW) 34 | # DEBUG=0/1 (default=0) 35 | # SGX_PRERELEASE=0/1 (default=0) 36 | 37 | ifndef $(VERBOSE) 38 | VERBOSE:=@ 39 | endif 40 | 41 | # root dir and sdk source dir 42 | PCL_DIR := $(CURDIR) 43 | 44 | ifeq ($(SGX_SDK_SRCS),) 45 | $(error SGX_SDK_SRCS environment variable must be set to home directory of SGX SDK source files!!!) 46 | endif 47 | 48 | export PCL_DIR 49 | export SGX_SDK_SRCS 50 | 51 | MODULES := Sources Tools/Encryptip 52 | 53 | .PHONY: all clean $(MODULES) 54 | 55 | all: $(MODULES) 56 | $(VERBOSE) echo "all: done" 57 | $(VERBOSE) echo 58 | 59 | clean: $(MODULES) 60 | $(VERBOSE) echo "clean: done" 61 | $(VERBOSE) echo 62 | 63 | $(MODULES): 64 | $(VERBOSE)$(MAKE) -C $@ $(MAKECMDGOALS) 65 | $(VERBOSE) echo "$@: done" 66 | 67 | .PHONY: help 68 | help: 69 | @echo "build Intel(R) SGX PCL library (both HW and SIM versions) and encryption tool" 70 | @echo " all: build Intel(R) SGX PCL library without debug symbols" 71 | @echo " all DEBUG=1: build Intel(R) SGX PCL library with debug symbols" 72 | @echo " clean: clean everything" 73 | @echo " help: display this help message" 74 | -------------------------------------------------------------------------------- /SampleCode/SampleEnclave/App/App.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-2017 Intel Corporation. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in 12 | * the documentation and/or other materials provided with the 13 | * distribution. 14 | * * Neither the name of Intel Corporation nor the names of its 15 | * contributors may be used to endorse or promote products derived 16 | * from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | * 30 | */ 31 | 32 | 33 | #ifndef _APP_H_ 34 | #define _APP_H_ 35 | 36 | #include 37 | #include 38 | #include 39 | #include 40 | 41 | #include "sgx_error.h" /* sgx_status_t */ 42 | #include "sgx_eid.h" /* sgx_enclave_id_t */ 43 | 44 | #ifndef TRUE 45 | # define TRUE 1 46 | #endif 47 | 48 | #ifndef FALSE 49 | # define FALSE 0 50 | #endif 51 | 52 | #define TOKEN_FILENAME "enclave.token" 53 | #define ENCLAVE_FILENAME "enclave.signed.so" 54 | #define SEAL_TOKEN_FILENAME "seal.token" 55 | #define SEAL_FILENAME "Seal.signed.so" 56 | #define SEALED_KEY_FILE_NAME "sealed_key.bin" 57 | 58 | extern sgx_enclave_id_t global_eid; /* global enclave id */ 59 | 60 | #if defined(__cplusplus) 61 | extern "C" { 62 | #endif 63 | 64 | void edger8r_array_attributes(void); 65 | void edger8r_type_attributes(void); 66 | void edger8r_pointer_attributes(void); 67 | void edger8r_function_attributes(void); 68 | 69 | void ecall_libc_functions(void); 70 | void ecall_libcxx_functions(void); 71 | void ecall_thread_functions(void); 72 | 73 | #if defined(__cplusplus) 74 | } 75 | #endif 76 | 77 | #endif /* !_APP_H_ */ 78 | -------------------------------------------------------------------------------- /SampleCode/SampleEnclave/App/Edger8rSyntax/Functions.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-2017 Intel Corporation. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in 12 | * the documentation and/or other materials provided with the 13 | * distribution. 14 | * * Neither the name of Intel Corporation nor the names of its 15 | * contributors may be used to endorse or promote products derived 16 | * from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | * 30 | */ 31 | 32 | 33 | #include "../App.h" 34 | #include "Enclave_u.h" 35 | 36 | /* No need to implement memccpy here! */ 37 | 38 | /* edger8r_function_attributes: 39 | * Invokes ECALL declared with calling convention attributes. 40 | * Invokes ECALL declared with [public]. 41 | */ 42 | void edger8r_function_attributes(void) 43 | { 44 | sgx_status_t ret = SGX_ERROR_UNEXPECTED; 45 | 46 | ret = ecall_function_calling_convs(global_eid); 47 | if (ret != SGX_SUCCESS) 48 | abort(); 49 | 50 | ret = ecall_function_public(global_eid); 51 | if (ret != SGX_SUCCESS) 52 | abort(); 53 | 54 | /* user shall not invoke private function here */ 55 | int runned = 0; 56 | ret = ecall_function_private(global_eid, &runned); 57 | if (ret != SGX_ERROR_ECALL_NOT_ALLOWED || runned != 0) 58 | abort(); 59 | } 60 | 61 | /* ocall_function_allow: 62 | * The OCALL invokes the [allow]ed ECALL 'edger8r_private'. 63 | */ 64 | void ocall_function_allow(void) 65 | { 66 | int runned = 0; 67 | sgx_status_t ret = SGX_ERROR_UNEXPECTED; 68 | 69 | ret = ecall_function_private(global_eid, &runned); 70 | if (ret != SGX_SUCCESS || runned != 1) 71 | abort(); 72 | } 73 | -------------------------------------------------------------------------------- /SampleCode/SampleEnclave/Enclave/Edger8rSyntax/Functions.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-2017 Intel Corporation. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in 12 | * the documentation and/or other materials provided with the 13 | * distribution. 14 | * * Neither the name of Intel Corporation nor the names of its 15 | * contributors may be used to endorse or promote products derived 16 | * from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | * 30 | */ 31 | 32 | 33 | /* Test Calling Conventions */ 34 | 35 | #include 36 | #include 37 | 38 | #include "../Enclave.h" 39 | #include "Enclave_t.h" 40 | 41 | /* ecall_function_calling_convs: 42 | * memccpy is defined in system C library. 43 | */ 44 | void ecall_function_calling_convs(void) 45 | { 46 | sgx_status_t ret = SGX_ERROR_UNEXPECTED; 47 | 48 | char s1[] = "1234567890"; 49 | char s2[] = "0987654321"; 50 | 51 | char buf[BUFSIZ] = {'\0'}; 52 | memcpy(buf, s1, strlen(s1)); 53 | 54 | ret = memccpy(NULL, s1, s2, '\0', strlen(s1)); 55 | 56 | if (ret != SGX_SUCCESS) 57 | abort(); 58 | assert(memcmp(s1, s2, strlen(s1)) == 0); 59 | 60 | return; 61 | } 62 | 63 | /* ecall_function_public: 64 | * The public ECALL that invokes the OCALL 'ocall_function_allow'. 65 | */ 66 | void ecall_function_public(void) 67 | { 68 | sgx_status_t ret = SGX_ERROR_UNEXPECTED; 69 | 70 | ret = ocall_function_allow(); 71 | if (ret != SGX_SUCCESS) 72 | abort(); 73 | 74 | return; 75 | } 76 | 77 | /* ecall_function_private: 78 | * The private ECALL that only can be invoked in the OCALL 'ocall_function_allow'. 79 | */ 80 | int ecall_function_private(void) 81 | { 82 | return 1; 83 | } 84 | 85 | -------------------------------------------------------------------------------- /SampleCode/SampleEnclave/Enclave/TrustedLibrary/Libcxx.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-2017 Intel Corporation. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in 12 | * the documentation and/or other materials provided with the 13 | * distribution. 14 | * * Neither the name of Intel Corporation nor the names of its 15 | * contributors may be used to endorse or promote products derived 16 | * from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | * 30 | */ 31 | 32 | 33 | #include 34 | #include 35 | 36 | #include "../Enclave.h" 37 | #include "Enclave_t.h" 38 | 39 | /* 40 | * ecall_exception: 41 | * throw/catch C++ exception inside the enclave. 42 | */ 43 | 44 | void ecall_exception(void) 45 | { 46 | std::string foo = "foo"; 47 | try { 48 | throw std::runtime_error(foo); 49 | } 50 | catch (std::runtime_error const& e) { 51 | assert( foo == e.what() ); 52 | std::runtime_error clone(""); 53 | clone = e; 54 | assert(foo == clone.what() ); 55 | } 56 | catch (...) { 57 | assert( false ); 58 | } 59 | } 60 | 61 | #include 62 | #include 63 | 64 | using namespace std; 65 | 66 | /* 67 | * ecall_map: 68 | * Utilize STL in the enclave. 69 | */ 70 | void ecall_map(void) 71 | { 72 | typedef map > map_t; 73 | typedef map_t::value_type map_value; 74 | map_t m; 75 | 76 | m.insert(map_value('a', 1)); 77 | m.insert(map_value('b', 2)); 78 | m.insert(map_value('c', 3)); 79 | m.insert(map_value('d', 4)); 80 | 81 | assert(m['a'] == 1); 82 | assert(m['b'] == 2); 83 | assert(m['c'] == 3); 84 | assert(m['d'] == 4); 85 | 86 | assert(m.find('e') == m.end()); 87 | 88 | return; 89 | } 90 | -------------------------------------------------------------------------------- /SampleCode/SampleEnclave/App/Edger8rSyntax/Types.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-2017 Intel Corporation. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in 12 | * the documentation and/or other materials provided with the 13 | * distribution. 14 | * * Neither the name of Intel Corporation nor the names of its 15 | * contributors may be used to endorse or promote products derived 16 | * from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | * 30 | */ 31 | 32 | 33 | #include "../App.h" 34 | #include "Enclave_u.h" 35 | 36 | /* edger8r_type_attributes: 37 | * Invokes ECALLs declared with basic types. 38 | */ 39 | void edger8r_type_attributes(void) 40 | { 41 | sgx_status_t ret = SGX_ERROR_UNEXPECTED; 42 | 43 | ret = ecall_type_char(global_eid, (char)0x12); 44 | if (ret != SGX_SUCCESS) 45 | abort(); 46 | 47 | ret = ecall_type_int(global_eid, (int)1234); 48 | if (ret != SGX_SUCCESS) 49 | abort(); 50 | 51 | ret = ecall_type_float(global_eid, (float)1234.0); 52 | if (ret != SGX_SUCCESS) 53 | abort(); 54 | 55 | ret = ecall_type_double(global_eid, (double)1234.5678); 56 | if (ret != SGX_SUCCESS) 57 | abort(); 58 | 59 | ret = ecall_type_size_t(global_eid, (size_t)12345678); 60 | if (ret != SGX_SUCCESS) 61 | abort(); 62 | 63 | ret = ecall_type_wchar_t(global_eid, (wchar_t)0x1234); 64 | if (ret != SGX_SUCCESS) 65 | abort(); 66 | 67 | struct struct_foo_t g = {1234, 5678}; 68 | ret = ecall_type_struct(global_eid, g); 69 | if (ret != SGX_SUCCESS) 70 | abort(); 71 | 72 | union union_foo_t val = {0}; 73 | ret = ecall_type_enum_union(global_eid, ENUM_FOO_0, &val); 74 | if (ret != SGX_SUCCESS) 75 | abort(); 76 | assert(val.union_foo_0 == 2); 77 | } 78 | -------------------------------------------------------------------------------- /SampleCode/SampleEnclave/App/Edger8rSyntax/Arrays.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-2017 Intel Corporation. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in 12 | * the documentation and/or other materials provided with the 13 | * distribution. 14 | * * Neither the name of Intel Corporation nor the names of its 15 | * contributors may be used to endorse or promote products derived 16 | * from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | * 30 | */ 31 | 32 | 33 | #include "../App.h" 34 | #include "Enclave_u.h" 35 | 36 | /* edger8r_array_attributes: 37 | * Invokes ECALLs declared with array attributes. 38 | */ 39 | void edger8r_array_attributes(void) 40 | { 41 | sgx_status_t ret = SGX_ERROR_UNEXPECTED; 42 | 43 | /* user_check */ 44 | int arr1[4] = {0, 1, 2, 3}; 45 | ret = ecall_array_user_check(global_eid, arr1); 46 | if (ret != SGX_SUCCESS) 47 | abort(); 48 | 49 | /* make sure arr1 is changed */ 50 | for (int i = 0; i < 4; i++) 51 | assert(arr1[i] == (3 - i)); 52 | 53 | /* in */ 54 | int arr2[4] = {0, 1, 2, 3}; 55 | ret = ecall_array_in(global_eid, arr2); 56 | if (ret != SGX_SUCCESS) 57 | abort(); 58 | 59 | /* arr2 is not changed */ 60 | for (int i = 0; i < 4; i++) 61 | assert(arr2[i] == i); 62 | 63 | /* out */ 64 | int arr3[4] = {0, 1, 2, 3}; 65 | ret = ecall_array_out(global_eid, arr3); 66 | if (ret != SGX_SUCCESS) 67 | abort(); 68 | 69 | /* arr3 is changed */ 70 | for (int i = 0; i < 4; i++) 71 | assert(arr3[i] == (3 - i)); 72 | 73 | /* in, out */ 74 | int arr4[4] = {0, 1, 2, 3}; 75 | ret = ecall_array_in_out(global_eid, arr4); 76 | if (ret != SGX_SUCCESS) 77 | abort(); 78 | 79 | /* arr4 is changed */ 80 | for (int i = 0; i < 4; i++) 81 | assert(arr4[i] == (3 - i)); 82 | 83 | /* isary */ 84 | array_t arr5 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; 85 | ret = ecall_array_isary(global_eid, arr5); 86 | if (ret != SGX_SUCCESS) 87 | abort(); 88 | 89 | /* arr5 is changed */ 90 | for (int i = 0; i < 10; i++) 91 | assert(arr5[i] == (9 - i)); 92 | } 93 | -------------------------------------------------------------------------------- /SampleCode/SampleEnclave/Enclave/Edger8rSyntax/Types.edl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-2017 Intel Corporation. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in 12 | * the documentation and/or other materials provided with the 13 | * distribution. 14 | * * Neither the name of Intel Corporation nor the names of its 15 | * contributors may be used to endorse or promote products derived 16 | * from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | * 30 | */ 31 | 32 | /* Types.edl - Samples for basic types. */ 33 | 34 | enclave { 35 | 36 | /* 37 | * Following types can be supported in Edger8r: 38 | * char, short, int, float, double, void, 39 | * int8_t, int16_t, int32_t, int64_t, 40 | * size_t, wchar_t, 41 | * uint8_t, uint16_t, uint32_t, uint64_t, 42 | * unsigned, struct, enum, union. 43 | */ 44 | 45 | /* 46 | * We will demo few types in ECALL functions, data 47 | * types in OCALL functions can be handled either. 48 | */ 49 | 50 | /* structure definition */ 51 | struct struct_foo_t { 52 | /* Basic types can be used in structure. */ 53 | uint32_t struct_foo_0; 54 | uint64_t struct_foo_1; 55 | }; 56 | 57 | /* enum definition */ 58 | enum enum_foo_t { 59 | ENUM_FOO_0 = 0, 60 | ENUM_FOO_1 = 1 61 | }; 62 | 63 | /* union definition */ 64 | union union_foo_t { 65 | uint32_t union_foo_0; 66 | uint32_t union_foo_1; 67 | uint64_t union_foo_3; 68 | }; 69 | 70 | trusted { 71 | 72 | public void ecall_type_char(char val); 73 | public void ecall_type_int(int val); 74 | 75 | public void ecall_type_float(float val); 76 | public void ecall_type_double(double val); 77 | 78 | public void ecall_type_size_t(size_t val); 79 | public void ecall_type_wchar_t(wchar_t val); 80 | 81 | public void ecall_type_struct(struct struct_foo_t val); 82 | public void ecall_type_enum_union(enum enum_foo_t val1, [user_check] union union_foo_t *val2); 83 | 84 | /* for using user defined types, please refer to Pointers.edl, Arrays.edl. */ 85 | }; 86 | 87 | }; 88 | -------------------------------------------------------------------------------- /SampleCode/SampleEnclave/App/TrustedLibrary/Thread.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-2017 Intel Corporation. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in 12 | * the documentation and/or other materials provided with the 13 | * distribution. 14 | * * Neither the name of Intel Corporation nor the names of its 15 | * contributors may be used to endorse or promote products derived 16 | * from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | * 30 | */ 31 | 32 | 33 | #include 34 | #include 35 | using namespace std; 36 | 37 | #include "../App.h" 38 | #include "Enclave_u.h" 39 | 40 | static size_t counter = 0; 41 | 42 | void increase_counter(void) 43 | { 44 | size_t cnr = 0; 45 | sgx_status_t ret = SGX_ERROR_UNEXPECTED; 46 | ret = ecall_increase_counter(global_eid, &cnr); 47 | if (cnr != 0) counter = cnr; 48 | if (ret != SGX_SUCCESS) 49 | abort(); 50 | } 51 | 52 | void data_producer(void) 53 | { 54 | sgx_status_t ret = SGX_ERROR_UNEXPECTED; 55 | ret = ecall_producer(global_eid); 56 | if (ret != SGX_SUCCESS) 57 | abort(); 58 | } 59 | 60 | void data_consumer(void) 61 | { 62 | sgx_status_t ret = SGX_ERROR_UNEXPECTED; 63 | ret = ecall_consumer(global_eid); 64 | if (ret != SGX_SUCCESS) 65 | abort(); 66 | } 67 | 68 | /* ecall_thread_functions: 69 | * Invokes thread functions including mutex, condition variable, etc. 70 | */ 71 | void ecall_thread_functions(void) 72 | { 73 | thread adder1(increase_counter); 74 | thread adder2(increase_counter); 75 | thread adder3(increase_counter); 76 | thread adder4(increase_counter); 77 | 78 | adder1.join(); 79 | adder2.join(); 80 | adder3.join(); 81 | adder4.join(); 82 | 83 | assert(counter == 4*LOOPS_PER_THREAD); 84 | 85 | printf("Info: executing thread synchronization, please wait... \n"); 86 | /* condition variable */ 87 | thread consumer1(data_consumer); 88 | thread producer0(data_producer); 89 | thread consumer2(data_consumer); 90 | thread consumer3(data_consumer); 91 | thread consumer4(data_consumer); 92 | 93 | consumer1.join(); 94 | consumer2.join(); 95 | consumer3.join(); 96 | consumer4.join(); 97 | producer0.join(); 98 | } 99 | -------------------------------------------------------------------------------- /SampleCode/SampleEnclave/Enclave/Edger8rSyntax/Functions.edl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-2017 Intel Corporation. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in 12 | * the documentation and/or other materials provided with the 13 | * distribution. 14 | * * Neither the name of Intel Corporation nor the names of its 15 | * contributors may be used to endorse or promote products derived 16 | * from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | * 30 | */ 31 | 32 | 33 | /* Functions.edl - Samples for function attributes. */ 34 | 35 | enclave { 36 | 37 | /* 38 | * Following keywords/attributes are supported for untrusted functions: 39 | * cdecl, stdcall, fastcall, dllimport (only for Windows). 40 | * [public] is only supported for the trusted functions. 41 | * Trusted function will be treated as [private] w/o the [public]. 42 | */ 43 | 44 | trusted { 45 | 46 | public void ecall_function_calling_convs(void); 47 | 48 | /* 49 | * [public]: 50 | * public ECALL can be called directly in App. 51 | */ 52 | 53 | public void ecall_function_public(void); 54 | 55 | /* 56 | * [private]: 57 | * private ECALL cannot be called directly in App. 58 | */ 59 | 60 | int ecall_function_private(void); 61 | 62 | }; 63 | 64 | untrusted { 65 | 66 | /* 67 | * [cdecl]: 68 | * tells edger8r the calling convention of the OCALLs is 'cdecl'. 69 | * [dllimport]: 70 | * indicats the OCALL is provided in DLLs. 71 | * 72 | * Note: memccpy() is provided by MS system DLL, we don't need to implement it in App side. 73 | */ 74 | 75 | [cdecl, dllimport] void *memccpy([in, out, size=len] void *dest, [in, size=len] const void *src, int val, size_t len); 76 | 77 | /* 78 | * [allow]: 79 | * OCALL 'ocall_function_allow' can invoke ECALL 'ecall_function_private' in App side. 80 | * 81 | * Note: No ECALL can be called in OCALL w/o [allow]. 82 | */ 83 | 84 | void ocall_function_allow(void) allow(ecall_function_private); 85 | 86 | }; 87 | 88 | }; 89 | -------------------------------------------------------------------------------- /Sources/unseal/pcl_unseal_internal.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-2017 Intel Corporation. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in 12 | * the documentation and/or other materials provided with the 13 | * distribution. 14 | * * Neither the name of Intel Corporation nor the names of its 15 | * contributors may be used to endorse or promote products derived 16 | * from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | * 30 | */ 31 | 32 | /* Includes common to PCL unsealing files*/ 33 | 34 | #ifndef SGX_PCL_UNSEAL_INTERNAL_H 35 | #define SGX_PCL_UNSEAL_INTERNAL_H 36 | 37 | #define BIT_ERROR(x) (1 << (x)) 38 | 39 | 40 | // Key request defines: 41 | #define KEY_ALIGN_SIZE (16) 42 | #define KEY_REQUEST_SIZE (512) 43 | #define KEY_REQUEST_ALIGN_SIZE (512) 44 | #define PCL_EGETKEY_BUFFER_SIZE (2 * KEY_REQUEST_ALIGN_SIZE + KEY_ALIGN_SIZE - 1) 45 | PCL_COMPILE_TIME_ASSERT(KEY_REQUEST_ALIGN_SIZE >= KEY_ALIGN_SIZE); 46 | 47 | extern "C" 48 | { 49 | 50 | extern uint8_t ip1_buf[]; 51 | 52 | int do_egetkey(const sgx_key_request_t *key_request, sgx_key_128bit_t *key); 53 | 54 | sgx_status_t pcl_unseal_data_helper(const sgx_sealed_data_t *p_sealed_data, uint8_t *p_additional_MACtext, 55 | uint32_t additional_MACtext_length, uint8_t *p_decrypted_text, uint32_t decrypted_text_length); 56 | sgx_status_t pcl_sgx_get_key(const sgx_key_request_t *key_request, sgx_key_128bit_t *key); 57 | // Typedefs: 58 | typedef enum _egetkey_status_t 59 | { 60 | EGETKEY_SUCCESS = 0, 61 | EGETKEY_INVALID_ATTRIBUTE = BIT_ERROR(1), 62 | EGETKEY_INVALID_CPUSVN = BIT_ERROR(5), 63 | EGETKEY_INVALID_ISVSVN = BIT_ERROR(6), 64 | EGETKEY_INVALID_KEYNAME = BIT_ERROR(8), 65 | } egetkey_status_t; 66 | 67 | uint32_t pcl_calc_sealed_data_size(const uint32_t aad_mac_txt_size, const uint32_t txt_encrypt_size); 68 | uint32_t pcl_get_aad_mac_txt_len(const sgx_sealed_data_t* p_sealed_data); 69 | uint32_t pcl_get_encrypt_txt_len(const sgx_sealed_data_t* p_sealed_data); 70 | 71 | #ifdef SE_SIM 72 | 73 | egetkey_status_t pcl_egetkey(sgx_key_request_t* kr, sgx_key_128bit_t okey); 74 | void pcl_derive_key(const derivation_data_t* dd, sgx_key_128bit_t okey); 75 | 76 | #endif // #ifdef SE_SIM 77 | 78 | }; // extern "C" 79 | 80 | #endif // #ifndef SGX_PCL_UNSEAL_INTERNAL_H 81 | 82 | -------------------------------------------------------------------------------- /Tools/Encryptip/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (C) 2011-2017 Intel Corporation. All rights reserved. 3 | # 4 | # Redistribution and use in source and binary forms, with or without 5 | # modification, are permitted provided that the following conditions 6 | # are met: 7 | # 8 | # * Redistributions of source code must retain the above copyright 9 | # notice, this list of conditions and the following disclaimer. 10 | # * Redistributions in binary form must reproduce the above copyright 11 | # notice, this list of conditions and the following disclaimer in 12 | # the documentation and/or other materials provided with the 13 | # distribution. 14 | # * Neither the name of Intel Corporation nor the names of its 15 | # contributors may be used to endorse or promote products derived 16 | # from this software without specific prior written permission. 17 | # 18 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | # 30 | # 31 | 32 | ifndef $(VERBOSE) 33 | VERBOSE := @ 34 | endif 35 | 36 | ######## Verify environment variables ######## 37 | 38 | ifeq ($(PCL_DIR),) 39 | $(error PCL_DIR environment variable must be set!!!) 40 | endif 41 | 42 | ifeq ($(OPENSSL_ROOT),) 43 | $(error OPENSSL_ROOT environment variable must be set to root directory of OpenSSL 1.1.0g !!!) 44 | endif 45 | 46 | ######## SGX SDK Settings ######## 47 | 48 | # location of SDK include, bin, lib folders 49 | # SGX_SDK defined - build with SDK install (default: /opt/intel/sgxsdk) 50 | ifeq ($(SGX_SDK),) 51 | $(error SGX_SDK environment variable must be set to SGX SDK root directory (e.g. /opt/intel/sgxsdk) !!!) 52 | endif 53 | 54 | SGX_LIBRARY_PATH := $(SGX_SDK)/lib64 55 | SERVICE_LIBRARY_NAME := sgx_tservice 56 | 57 | ######## App Settings ######## 58 | 59 | APP_INCLUDE_PATH := -I$(OPENSSL_ROOT)/include -I$(PCL_DIR)/Include -I$(PCL_DIR)/Tools/Encryptip -I$(PCL_DIR)/Common -I$(SGX_SDK)/include 60 | APP_C_FLAGS := $(APP_INCLUDE_PATH) -fPIC -Wno-attributes -g -mrdrnd 61 | 62 | SSL_CRYPTO_LIB_NAME := crypto 63 | 64 | APP_LINK_FLAGS := -Wl,-Bstatic -L$(OPENSSL_ROOT) -l$(SSL_CRYPTO_LIB_NAME) -Wl,-Bdynamic -L$(SGX_LIBRARY_PATH) -l$(SERVICE_LIBRARY_NAME) -ldl -lpthread 65 | 66 | ######## PCL Tool ########### 67 | 68 | PCL_ENCRYPTION_CPP_FILES := encryptip.cpp 69 | PCL_ENCRYPTION_OBJECTS := $(PCL_ENCRYPTION_CPP_FILES:.cpp=.o) 70 | PCL_ENCRYPTION_TOOL := $(PCL_DIR)/bin/x64/sgx_encrypt 71 | 72 | .PHONY: all 73 | 74 | all: $(PCL_ENCRYPTION_TOOL) 75 | 76 | $(PCL_ENCRYPTION_OBJECTS): $(PCL_ENCRYPTION_CPP_FILES) 77 | $(VERBOSE)$(CXX) -c $^ -o $@ $(APP_C_FLAGS) 78 | @echo "CXX => $@" 79 | 80 | $(PCL_ENCRYPTION_TOOL): $(PCL_ENCRYPTION_OBJECTS) 81 | $(VERBOSE)$(CXX) $^ -o $@ $(APP_LINK_FLAGS) 82 | @echo "LINK => $@" 83 | 84 | 85 | .PHONY: clean 86 | 87 | clean: 88 | $(VERBOSE)rm -f $(PCL_ENCRYPTION_TOOL) $(PCL_ENCRYPTION_OBJECTS) 89 | 90 | -------------------------------------------------------------------------------- /Sources/unseal/sim/pcl_deriv.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-2017 Intel Corporation. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in 12 | * the documentation and/or other materials provided with the 13 | * distribution. 14 | * * Neither the name of Intel Corporation nor the names of its 15 | * contributors may be used to endorse or promote products derived 16 | * from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | * 30 | */ 31 | 32 | /* Content from sdk/simulation/tinst/deriv.cpp */ 33 | 34 | #include "t_instructions.h" /* for `g_global_data_sim' */ 35 | #include "deriv.h" 36 | #include 37 | #include 38 | #include 39 | 40 | extern global_data_sim_t g_global_data_sim; 41 | 42 | int pcl_cmac(const sgx_cmac_128bit_key_t *p_key, const uint8_t *p_src, 43 | uint32_t src_len, sgx_cmac_128bit_tag_t *p_mac); 44 | 45 | // The built-in seal key in simulation mode 46 | static const uint8_t PCL_BASE_SEAL_KEY[] = { 47 | 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 48 | 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 49 | }; 50 | 51 | static const uint8_t* pcl_get_base_key(uint16_t key_name) 52 | { 53 | // Shouldn't use switch else requires relocation 54 | /* PCL UNUSED START * 55 | switch (key_name) { 56 | case SGX_KEYSELECT_SEAL: 57 | return BASE_SEAL_KEY; 58 | case SGX_KEYSELECT_REPORT: 59 | return BASE_REPORT_KEY; 60 | case SGX_KEYSELECT_EINITTOKEN: 61 | return BASE_EINITTOKEN_KEY; 62 | case SGX_KEYSELECT_PROVISION: 63 | return BASE_PROVISION_KEY; 64 | case SGX_KEYSELECT_PROVISION_SEAL: 65 | return BASE_PROV_SEAL_KEY; 66 | } 67 | /* PCL UNUSED END */ 68 | if (key_name == SGX_KEYSELECT_SEAL) return PCL_BASE_SEAL_KEY; 69 | 70 | // Should not come here - error should have been reported 71 | // when the key name is not supported in the caller. 72 | return (uint8_t*)0; 73 | } 74 | 75 | // Compute the CMAC of derivation data with corresponding base key 76 | // and save it to `okey'. 77 | void pcl_derive_key(const derivation_data_t* dd, sgx_key_128bit_t okey) 78 | { 79 | const uint8_t* base_key = pcl_get_base_key(dd->key_name); 80 | pcl_cmac((const sgx_cmac_128bit_key_t*)base_key,dd->ddbuf, dd->size, (sgx_cmac_128bit_tag_t*)okey); 81 | } 82 | 83 | -------------------------------------------------------------------------------- /SampleCode/SampleEnclave/Enclave/Edger8rSyntax/Arrays.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-2017 Intel Corporation. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in 12 | * the documentation and/or other materials provided with the 13 | * distribution. 14 | * * Neither the name of Intel Corporation nor the names of its 15 | * contributors may be used to endorse or promote products derived 16 | * from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | * 30 | */ 31 | 32 | /* Test Array Attributes */ 33 | 34 | #include "sgx_trts.h" 35 | #include "../Enclave.h" 36 | #include "Enclave_t.h" 37 | 38 | /* ecall_array_user_check: 39 | * [user_check] parameter does not perfrom copy operations. 40 | */ 41 | void ecall_array_user_check(int arr[4]) 42 | { 43 | if (sgx_is_outside_enclave(arr, 4 * sizeof(int)) != 1) 44 | abort(); 45 | 46 | for (int i = 0; i < 4; i++) { 47 | assert(arr[i] == i); 48 | arr[i] = 3 - i; 49 | } 50 | } 51 | 52 | /* ecall_array_in: 53 | * arr[] is copied to trusted domain, but modified 54 | * results will not be reflected to the untrusted side. 55 | */ 56 | void ecall_array_in(int arr[4]) 57 | { 58 | for (int i = 0; i < 4; i++) { 59 | assert(arr[i] == i); 60 | arr[i] = (3 - i); 61 | } 62 | } 63 | 64 | /* ecall_array_out: 65 | * arr[] is allocated inside the enclave, and it will be copied 66 | * to the untrusted side 67 | */ 68 | void ecall_array_out(int arr[4]) 69 | { 70 | for (int i = 0; i < 4; i++) { 71 | /* arr is not copied from App */ 72 | assert(arr[i] == 0); 73 | arr[i] = (3 - i); 74 | } 75 | } 76 | 77 | /* ecall_array_in_out: 78 | * arr[] will be allocated inside the enclave, content of arr[] will be copied either. 79 | * After ECALL returns, the results will be copied to the outside. 80 | */ 81 | void ecall_array_in_out(int arr[4]) 82 | { 83 | for (int i = 0; i < 4; i++) { 84 | assert(arr[i] == i); 85 | arr[i] = (3 - i); 86 | } 87 | } 88 | 89 | /* ecall_array_isary: 90 | * [isary] tells Edger8r that user defined 'array_t' is an array type. 91 | */ 92 | void ecall_array_isary(array_t arr) 93 | { 94 | if (sgx_is_outside_enclave(arr, sizeof(array_t)) != 1) 95 | abort(); 96 | 97 | int n = sizeof(array_t)/sizeof(arr[0]); 98 | for (int i = 0; i < n; i++) { 99 | assert(arr[i] == i); 100 | arr[i] = (n - 1 - i); 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | ## Issues 4 | 5 | GitHub Issues tracks Intel(R) SGX PCL design and development issues, bugs, and feature requests. 6 | For usage, installation, or other requests for help, please use the [01.org](https://01.org/intel-software-guard-extensions/forum/forum) forum instead. 7 | 8 | When reporting a bug, please provide the following information, where applicable: 9 | 10 | * What are the steps to reproduce the bug? 11 | * Can you reproduce the bug using the latest [master](https://github.com/01org/linux-sgx-pcl/tree/master)? 12 | * What CPU, platform, operating system/distribution, and SGX driver are you running? The more specific, the better. 13 | * For crashes, please provide the backtrace (use gdb). 14 | 15 | 16 | ### Contribution Guide 17 | 18 | We accept contributions as pull requests on GitHub. More detailed guidelines will be added later. Please follow these simple rules for now: 19 | 20 | * A PR should have a clear purpose, and do one thing only, and nothing more. This will enable us review your PR more quickly. 21 | * Each commit in PR should be a small, atomic change representing one step in development. 22 | * Please squash intermediate steps within PR for bugfixes, style cleanups, reversions, etc., so they would not appear in merged PR history. 23 | * Please explain anything non-obvious from the code in comments, commit messages, or the PR description, as appropriate. 24 | 25 | ### License 26 | 27 | Intel(R) SGX PCL is licensed under the terms in [LICENSE](https://github.com/01org/linux-sgx-pcl/blob/master/License.txt). By contributing to the project, you agree to the license and copyright terms therein and release your contribution under these terms. 28 | 29 | ### Sign your work 30 | 31 | Please use the sign-off line at the end of the patch. Your signature certifies that you wrote the patch or otherwise have the right to pass it on as an open-source patch. The rules are pretty simple: if you can certify 32 | the below (from [developercertificate.org](http://developercertificate.org/)): 33 | 34 | ``` 35 | Developer Certificate of Origin 36 | Version 1.1 37 | 38 | Copyright (C) 2004, 2006 The Linux Foundation and its contributors. 39 | 660 York Street, Suite 102, 40 | San Francisco, CA 94110 USA 41 | 42 | Everyone is permitted to copy and distribute verbatim copies of this 43 | license document, but changing it is not allowed. 44 | 45 | Developer's Certificate of Origin 1.1 46 | 47 | By making a contribution to this project, I certify that: 48 | 49 | (a) The contribution was created in whole or in part by me and I 50 | have the right to submit it under the open source license 51 | indicated in the file; or 52 | 53 | (b) The contribution is based upon previous work that, to the best 54 | of my knowledge, is covered under an appropriate open source 55 | license and I have the right under that license to submit that 56 | work with modifications, whether created in whole or in part 57 | by me, under the same open source license (unless I am 58 | permitted to submit under a different license), as indicated 59 | in the file; or 60 | 61 | (c) The contribution was provided directly to me by some other 62 | person who certified (a), (b) or (c) and I have not modified 63 | it. 64 | 65 | (d) I understand and agree that this project and the contribution 66 | are public and that a record of the contribution (including all 67 | personal information I submit with it, including my sign-off) is 68 | maintained indefinitely and may be redistributed consistent with 69 | this project or the open source license(s) involved. 70 | ``` 71 | 72 | Then you just add a line to every git commit message: 73 | 74 | Signed-off-by: Joe Smith 75 | 76 | Use your real name (sorry, no pseudonyms or anonymous contributions.) 77 | 78 | If you set your `user.name` and `user.email` git configs, you can sign your 79 | commit automatically with `git commit -s`. 80 | -------------------------------------------------------------------------------- /Sources/pcl_internal.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-2017 Intel Corporation. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in 12 | * the documentation and/or other materials provided with the 13 | * distribution. 14 | * * Neither the name of Intel Corporation nor the names of its 15 | * contributors may be used to endorse or promote products derived 16 | * from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | * 30 | */ 31 | 32 | #ifndef SGX_PCL_INTERNAL_H 33 | #define SGX_PCL_INTERNAL_H 34 | 35 | #define ALIGN_X2Y_AUX(x,y) \ 36 | (x % y ? x + y - (x % y) : x) 37 | #define ALIGN_X2Y(x,y) ALIGN_X2Y_AUX((x),(y)) 38 | #define CASTU8P(x) ((uint8_t*)(x)) 39 | #define CAST_VOLATILE_U8P(x) ((volatile uint8_t*)(x)) 40 | #define CASTU64(x) ((uint64_t)(x)) 41 | #define ROUND_TO(x,y) ALIGN_X2Y(x,y) 42 | #define MAX(x,y) ((x)>(y)?(x):(y)) 43 | #define assert(x) 44 | 45 | 46 | 47 | #ifdef __cplusplus 48 | extern "C" { 49 | #endif // #ifdef __cplusplus 50 | 51 | void pcl_memcpy(OUT void* dst, IN void* src, size_t size); 52 | void pcl_memset(OUT void* dst, uint8_t val, size_t size); 53 | void pcl_volatile_memset(OUT volatile void* dst, uint8_t val, size_t size); 54 | // code taken from consttime_memequal 55 | uint32_t pcl_consttime_memequal(IN const void *b1, IN const void *b2, size_t len); 56 | 57 | int pcl_decrypt( 58 | IN unsigned char *ciphertext, 59 | size_t ciphertext_len, 60 | IN unsigned char *aad, 61 | size_t aad_len, 62 | IN unsigned char *tag, 63 | IN unsigned char *key, 64 | IN unsigned char *iv, 65 | OUT unsigned char *plaintext); 66 | 67 | sgx_status_t pcl_sha256(IN uint8_t* buf, size_t buflen, OUT uint8_t* hash); 68 | sgx_status_t pcl_unseal_data( 69 | IN const sgx_sealed_data_t *p_sealed_data, 70 | IN uint8_t *p_additional_MACtext, 71 | INOUT uint32_t *p_additional_MACtext_length, 72 | OUT uint8_t *p_decrypted_text, 73 | INOUT uint32_t *p_decrypted_text_length); 74 | sgx_status_t pcl_gcm_decrypt( 75 | IN uint8_t* plaintext, 76 | OUT uint8_t* ciphertext, 77 | size_t textlen, 78 | IN uint8_t* aad, 79 | size_t aad_len, 80 | IN uint8_t* key, 81 | IN uint8_t* iv, 82 | IN uint8_t* tag); 83 | 84 | uint32_t pcl_bswap32(uint32_t val); 85 | uint64_t pcl_bswap64(uint64_t val); 86 | 87 | sgx_status_t pcl_increment_iv(INOUT uint8_t* iv); 88 | 89 | void abort(void) __attribute__((__noreturn__)); 90 | 91 | int pcl_is_outside_enclave(const void *addr, size_t size); 92 | int pcl_is_within_enclave(const void *addr, size_t size); 93 | #ifdef __cplusplus 94 | }; // extern "C" 95 | #endif // #ifdef __cplusplus 96 | 97 | #endif // #ifndef SGX_PCL_INTERNAL_H 98 | 99 | -------------------------------------------------------------------------------- /SampleCode/SampleEnclave/Enclave/TrustedLibrary/Thread.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-2017 Intel Corporation. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in 12 | * the documentation and/or other materials provided with the 13 | * distribution. 14 | * * Neither the name of Intel Corporation nor the names of its 15 | * contributors may be used to endorse or promote products derived 16 | * from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | * 30 | */ 31 | 32 | 33 | #include "../Enclave.h" 34 | #include "Enclave_t.h" 35 | 36 | #include "sgx_thread.h" 37 | 38 | static size_t global_counter = 0; 39 | static sgx_thread_mutex_t global_mutex = SGX_THREAD_MUTEX_INITIALIZER; 40 | 41 | #define BUFFER_SIZE 50 42 | 43 | typedef struct { 44 | int buf[BUFFER_SIZE]; 45 | int occupied; 46 | int nextin; 47 | int nextout; 48 | sgx_thread_mutex_t mutex; 49 | sgx_thread_cond_t more; 50 | sgx_thread_cond_t less; 51 | } cond_buffer_t; 52 | 53 | static cond_buffer_t buffer = {{0, 0, 0, 0, 0, 0}, 0, 0, 0, 54 | SGX_THREAD_MUTEX_INITIALIZER, SGX_THREAD_COND_INITIALIZER, SGX_THREAD_COND_INITIALIZER}; 55 | 56 | /* 57 | * ecall_increase_counter: 58 | * Utilize thread APIs inside the enclave. 59 | */ 60 | size_t ecall_increase_counter(void) 61 | { 62 | size_t ret = 0; 63 | for (int i = 0; i < LOOPS_PER_THREAD; i++) { 64 | sgx_thread_mutex_lock(&global_mutex); 65 | /* mutually exclusive adding */ 66 | size_t tmp = global_counter; 67 | global_counter = ++tmp; 68 | if (4*LOOPS_PER_THREAD == global_counter) 69 | ret = global_counter; 70 | sgx_thread_mutex_unlock(&global_mutex); 71 | } 72 | return ret; 73 | } 74 | 75 | void ecall_producer(void) 76 | { 77 | for (int i = 0; i < 4*LOOPS_PER_THREAD; i++) { 78 | cond_buffer_t *b = &buffer; 79 | sgx_thread_mutex_lock(&b->mutex); 80 | while (b->occupied >= BUFFER_SIZE) 81 | sgx_thread_cond_wait(&b->less, &b->mutex); 82 | b->buf[b->nextin] = b->nextin; 83 | b->nextin++; 84 | b->nextin %= BUFFER_SIZE; 85 | b->occupied++; 86 | sgx_thread_cond_signal(&b->more); 87 | sgx_thread_mutex_unlock(&b->mutex); 88 | } 89 | } 90 | 91 | void ecall_consumer(void) 92 | { 93 | for (int i = 0; i < LOOPS_PER_THREAD; i++) { 94 | cond_buffer_t *b = &buffer; 95 | sgx_thread_mutex_lock(&b->mutex); 96 | while(b->occupied <= 0) 97 | sgx_thread_cond_wait(&b->more, &b->mutex); 98 | b->buf[b->nextout++] = 0; 99 | b->nextout %= BUFFER_SIZE; 100 | b->occupied--; 101 | sgx_thread_cond_signal(&b->less); 102 | sgx_thread_mutex_unlock(&b->mutex); 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /SampleCode/SampleEnclave/Enclave/Edger8rSyntax/Arrays.edl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-2017 Intel Corporation. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in 12 | * the documentation and/or other materials provided with the 13 | * distribution. 14 | * * Neither the name of Intel Corporation nor the names of its 15 | * contributors may be used to endorse or promote products derived 16 | * from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | * 30 | */ 31 | 32 | 33 | /* Arrays.edl - Samples for array attributes. */ 34 | 35 | enclave { 36 | 37 | /* 38 | * Only for fixed-size array (size is explicitly specified). 39 | */ 40 | 41 | trusted { 42 | 43 | /* 44 | * []: can be used to declare an array. 45 | * [user_check]: 46 | * pointer of the array won't be valified, and the buffer pointed by 'arr' 47 | * is not copied into the enclave either. But enclave can modify the memory outside. 48 | */ 49 | 50 | public void ecall_array_user_check([user_check] int arr[4]); 51 | 52 | /* 53 | * [in]: 54 | * buffer for the array will be allocated inside the enclave, 55 | * content of the array will be copied into the new allocated memory inside. 56 | * Any changes performed inside the enclave will not affect the array outside. 57 | */ 58 | 59 | public void ecall_array_in([in] int arr[4]); 60 | 61 | /* 62 | * [out]: 63 | * buffer for the array will be allocated inside the enclave, 64 | * but the content of the array won't be copied. After ECALL returns, 65 | * the buffer inside the enclave will copied into outside array. 66 | */ 67 | 68 | public void ecall_array_out([out] int arr[4]); 69 | 70 | /* 71 | * [in, out]: 72 | * buffer for the array will be allocated inside the enclave, 73 | * the content of the array will be copied either. After ECALL returns, 74 | * the buffer inside the enclave will by copied into outside array again. 75 | */ 76 | 77 | public void ecall_array_in_out([in, out] int arr[4]); 78 | 79 | /* 80 | * [isary]: 81 | * tells Edger8r the user defined 'array_t' is an array type, 'arr' will be 82 | * treated as a pointer, no memory copied either due to [user_check]. 83 | * For OCALLs, 'arr' shall point to the memory outside the enclave. 84 | */ 85 | 86 | public void ecall_array_isary([user_check, isary] array_t arr); 87 | 88 | }; 89 | 90 | untrusted { 91 | 92 | /* 93 | * [user_check|in|out|in,out|isary] can also be used in OCALLs, refer to the "User Guide" for details. 94 | */ 95 | 96 | }; 97 | 98 | }; 99 | -------------------------------------------------------------------------------- /Sources/crypto/pcl_crypto_internal.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-2017 Intel Corporation. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in 12 | * the documentation and/or other materials provided with the 13 | * distribution. 14 | * * Neither the name of Intel Corporation nor the names of its 15 | * contributors may be used to endorse or promote products derived 16 | * from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | * 30 | */ 31 | 32 | #ifndef SGX_PCL_CRYPTO_INTERNAL_H 33 | #define SGX_PCL_CRYPTO_INTERNAL_H 34 | 35 | extern "C" 36 | { 37 | 38 | int pcl_SHA256_Update(SHA256_CTX *c, void *data_, size_t len); 39 | int pcl_SHA256_Final(unsigned char *md, SHA256_CTX *c); 40 | int pcl_SHA256_Init(SHA256_CTX *c); 41 | 42 | typedef uint64_t u64; 43 | typedef uint32_t u32; 44 | typedef uint8_t u8; 45 | typedef struct { 46 | u64 hi, lo; 47 | } u128; 48 | 49 | int pcl_vpaes_set_encrypt_key(const unsigned char *userKey, const int bits, AES_KEY *key); 50 | 51 | struct gcm128_context { 52 | /* Following 6 names follow names in GCM specification */ 53 | union { 54 | u64 u[2]; 55 | u32 d[4]; 56 | u8 c[16]; 57 | size_t t[16 / sizeof(size_t)]; 58 | } Yi, EKi, EK0, len, Xi, H; 59 | /* 60 | * Relative position of Xi, H and pre-computed Htable is used in some 61 | * assembler modules, i.e. don't change the order! 62 | */ 63 | u128 Htable[16]; 64 | void (*gmult) (u64 Xi[2], const u128 Htable[16]); 65 | void (*ghash) (u64 Xi[2], const u128 Htable[16], const u8 *inp, 66 | size_t len); 67 | unsigned int mres, ares; 68 | block128_f block; 69 | void *key; 70 | }; 71 | 72 | void pcl_CRYPTO_gcm128_init(GCM128_CONTEXT *ctx, void *key, block128_f block); 73 | void pcl_CRYPTO_gcm128_setiv( 74 | GCM128_CONTEXT *ctx, 75 | const unsigned char *iv, 76 | size_t len); 77 | int pcl_CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx, 78 | const unsigned char *in, unsigned char *out, 79 | size_t len); 80 | int pcl_CRYPTO_gcm128_aad( 81 | GCM128_CONTEXT *ctx, 82 | const unsigned char *aad, 83 | size_t len); 84 | int pcl_CRYPTO_gcm128_finish( 85 | GCM128_CONTEXT *ctx, 86 | const unsigned char *tag, 87 | size_t len); 88 | void pcl_vpaes_encrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key); 89 | 90 | #ifdef SE_SIM 91 | 92 | void make_kn( 93 | unsigned char *k1, 94 | const unsigned char *l, 95 | int bl); 96 | void pcl_vpaes_cbc_encrypt( 97 | uint8_t* in, 98 | uint8_t* out, 99 | size_t len, 100 | AES_KEY* wide_key_p, 101 | uint8_t* iv, 102 | bool encrypt); 103 | 104 | #endif // #ifdef SE_SIM 105 | 106 | }; // extern "C" 107 | 108 | #endif // #ifndef SGX_PCL_CRYPTO_INTERNAL_H 109 | 110 | -------------------------------------------------------------------------------- /Common/pcl_common.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-2017 Intel Corporation. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in 12 | * the documentation and/or other materials provided with the 13 | * distribution. 14 | * * Neither the name of Intel Corporation nor the names of its 15 | * contributors may be used to endorse or promote products derived 16 | * from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | * 30 | */ 31 | 32 | #ifndef PCL_COMMON_H 33 | #define PCL_COMMON_H 34 | 35 | /* 36 | * This file includes definition used by PCL library and encryption tool 37 | */ 38 | 39 | #define IN 40 | #define OUT 41 | #define INOUT 42 | 43 | // Define both ASSERT_CONCAT and ASSERT_CONCAT_ so that __COUNTER__ receives a value 44 | #define ASSERT_CONCAT_(a, b) a##b 45 | #ifndef ASSERT_CONCAT 46 | #define ASSERT_CONCAT(a, b) ASSERT_CONCAT_(a, b) 47 | #endif // #ifndef ASSERT_CONCAT 48 | #define PCL_COMPILE_TIME_ASSERT(exp) \ 49 | enum { ASSERT_CONCAT(static_assert_, __COUNTER__) = 1/(!!(exp)) } 50 | 51 | // PCL uses AES with 16 Bytes block size 52 | #define PCL_AES_BLOCK_LEN (16) 53 | #define PCL_COUNTER_SIZE (16) 54 | #define PCL_AES_BLOCK_LEN_BITS (128) 55 | 56 | #define PCLTBL_SECTION_NAME ".pcltbl" 57 | 58 | typedef struct iv_t_ 59 | { 60 | uint8_t val[SGX_AESGCM_IV_SIZE]; 61 | uint8_t reserved[4]; 62 | }iv_t; 63 | 64 | typedef struct rva_size_tag_iv_t_ 65 | { 66 | size_t rva; 67 | size_t size; 68 | sgx_cmac_128bit_tag_t tag; 69 | iv_t iv; 70 | }rva_size_tag_iv_t; 71 | 72 | // Hardcoded maximal size of sealed bolb. ISV can modify if requried 73 | #define PCL_SEALED_BLOB_SIZE (0x250) 74 | #define SGX_PCL_GUID_SIZE (16) 75 | // Hardcoded maximal number of encrypted sections. ISV can modify if requried 76 | #define PCL_MAX_NUM_ENCRYPTED_SECTIONS (0x80) 77 | 78 | typedef enum pcl_status_e_ 79 | { 80 | PCL_PLAIN = 0xABABABAB, 81 | PCL_CIPHER = 0xBCBCBCBC, 82 | PCL_RUNNING = 0xDEDEDEDE, 83 | PCL_DONE = 0xFAFAFAFA, 84 | }pcl_status_e; 85 | 86 | typedef struct pcl_table_t_ 87 | { 88 | pcl_status_e pcl_state; // Current state of PCL 89 | uint32_t reserved1[3]; // Must be 0 90 | uint8_t pcl_guid[SGX_PCL_GUID_SIZE]; // GUID must match GUID in Sealed blob 91 | size_t sealed_blob_size; // Size of selaed blob 92 | uint32_t reserved2[2]; // Must be 0 93 | uint8_t sealed_blob[PCL_SEALED_BLOB_SIZE]; // For security, sealed blob is copied into enclave 94 | uint8_t decryption_key_hash[SGX_SHA256_HASH_SIZE]; // SHA256 digest of decryption key 95 | uint32_t num_rvas; // Number of RVAs 96 | uint32_t reserved3[3]; // Must be 0 97 | rva_size_tag_iv_t rvas_sizes_tags_ivs[PCL_MAX_NUM_ENCRYPTED_SECTIONS]; // Array of rva_size_tag_iv_t 98 | }pcl_table_t; 99 | 100 | #endif // #ifndef PCL_COMMON_H 101 | 102 | -------------------------------------------------------------------------------- /Sources/unseal/pcl_tSeal_util.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-2017 Intel Corporation. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in 12 | * the documentation and/or other materials provided with the 13 | * distribution. 14 | * * Neither the name of Intel Corporation nor the names of its 15 | * contributors may be used to endorse or promote products derived 16 | * from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | * 30 | */ 31 | 32 | /* Content from sdk/tseal/tSeal_util.cpp */ 33 | 34 | #include 35 | #include 36 | #include 37 | #ifdef SE_SIM 38 | #include 39 | #endif // #ifdef SE_SIM 40 | #include 41 | #include 42 | #include 43 | 44 | 45 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 46 | // pcl_calc_sealed_data_size() 47 | // computes sealedDataSize = payload size + sizeof(sgx_sealed_data_t) 48 | // Parameters: 49 | // [IN] aad_mac_txt_size - optional additional data size 50 | // [IN] txt_encrypt_size - the actual encrypted data (payload) 51 | // Return Value: 52 | // sealedDataSize 53 | uint32_t pcl_calc_sealed_data_size(const uint32_t aad_mac_txt_size, const uint32_t txt_encrypt_size) 54 | { 55 | // code copied from tseal_util.cpp from the tseal library 56 | if (aad_mac_txt_size > UINT32_MAX - txt_encrypt_size) 57 | return UINT32_MAX; 58 | uint32_t payload_size = aad_mac_txt_size + txt_encrypt_size; //Calculate the payload size 59 | 60 | if (payload_size > UINT32_MAX - sizeof(sgx_sealed_data_t)) 61 | return UINT32_MAX; 62 | return (uint32_t)(sizeof(sgx_sealed_data_t) + payload_size); 63 | } 64 | 65 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 66 | // pcl_get_aad_mac_txt_len() 67 | // computes optional additional data size 68 | // Parameters: 69 | // [IN] p_sealed_data - pointer to the sealed blob 70 | // Return Value: 71 | // additional data size 72 | uint32_t pcl_get_aad_mac_txt_len(const sgx_sealed_data_t* p_sealed_data) 73 | { 74 | if (p_sealed_data == NULL) 75 | return UINT32_MAX; 76 | 77 | uint32_t data_size = p_sealed_data->aes_data.payload_size - p_sealed_data->plain_text_offset; 78 | if (data_size > p_sealed_data->aes_data.payload_size) 79 | return UINT32_MAX; 80 | return data_size; 81 | } 82 | 83 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 84 | // pcl_get_encrypt_txt_len() 85 | // computes encrypted data size 86 | // Parameters: 87 | // [IN] p_sealed_data - pointer to the sealed blob 88 | // Return Value: 89 | // encrypted data size 90 | uint32_t pcl_get_encrypt_txt_len(const sgx_sealed_data_t* p_sealed_data) 91 | { 92 | return ((p_sealed_data == NULL) ? UINT32_MAX : p_sealed_data->plain_text_offset); 93 | } 94 | 95 | -------------------------------------------------------------------------------- /License.txt: -------------------------------------------------------------------------------- 1 | BSD 3-clause "New" or "Revised" License 2 | 3 | Copyright (C) 2011-2017 Intel Corporation. All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without 6 | modification, are permitted provided that the following conditions 7 | are met: 8 | 9 | * Redistributions of source code must retain the above copyright 10 | notice, this list of conditions and the following disclaimer. 11 | * Redistributions in binary form must reproduce the above copyright 12 | notice, this list of conditions and the following disclaimer in 13 | the documentation and/or other materials provided with the 14 | distribution. 15 | * Neither the name of Intel Corporation nor the names of its 16 | contributors may be used to endorse or promote products derived 17 | from this software without specific prior written permission. 18 | 19 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | ================================================================= 32 | 33 | This project use OpenSSL crypto tests. 34 | 35 | /* ==================================================================== 36 | * Copyright (c) 1998-2017 The OpenSSL Project. All rights reserved. 37 | * 38 | * Redistribution and use in source and binary forms, with or without 39 | * modification, are permitted provided that the following conditions 40 | * are met: 41 | * 42 | * 1. Redistributions of source code must retain the above copyright 43 | * notice, this list of conditions and the following disclaimer. 44 | * 45 | * 2. Redistributions in binary form must reproduce the above copyright 46 | * notice, this list of conditions and the following disclaimer in 47 | * the documentation and/or other materials provided with the 48 | * distribution. 49 | * 50 | * 3. All advertising materials mentioning features or use of this 51 | * software must display the following acknowledgment: 52 | * "This product includes software developed by the OpenSSL Project 53 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 54 | * 55 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 56 | * endorse or promote products derived from this software without 57 | * prior written permission. For written permission, please contact 58 | * openssl-core@openssl.org. 59 | * 60 | * 5. Products derived from this software may not be called "OpenSSL" 61 | * nor may "OpenSSL" appear in their names without prior written 62 | * permission of the OpenSSL Project. 63 | * 64 | * 6. Redistributions of any form whatsoever must retain the following 65 | * acknowledgment: 66 | * "This product includes software developed by the OpenSSL Project 67 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 68 | * 69 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 70 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 71 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 72 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 73 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 74 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 75 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 76 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 77 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 78 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 79 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 80 | * OF THE POSSIBILITY OF SUCH DAMAGE. 81 | * ==================================================================== 82 | * 83 | * This product includes cryptographic software written by Eric Young 84 | * (eay@cryptsoft.com). This product includes software written by Tim 85 | * Hudson (tjh@cryptsoft.com). 86 | * 87 | */ -------------------------------------------------------------------------------- /Sources/crypto/pcl_modes_lcl.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-2017 Intel Corporation. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in 12 | * the documentation and/or other materials provided with the 13 | * distribution. 14 | * * Neither the name of Intel Corporation nor the names of its 15 | * contributors may be used to endorse or promote products derived 16 | * from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | * 30 | */ 31 | 32 | /* ==================================================================== 33 | * Copyright (c) 1998-2017 The OpenSSL Project. All rights reserved. 34 | * 35 | * Redistribution and use in source and binary forms, with or without 36 | * modification, are permitted provided that the following conditions 37 | * are met: 38 | * 39 | * 1. Redistributions of source code must retain the above copyright 40 | * notice, this list of conditions and the following disclaimer. 41 | * 42 | * 2. Redistributions in binary form must reproduce the above copyright 43 | * notice, this list of conditions and the following disclaimer in 44 | * the documentation and/or other materials provided with the 45 | * distribution. 46 | * 47 | * 3. All advertising materials mentioning features or use of this 48 | * software must display the following acknowledgment: 49 | * "This product includes software developed by the OpenSSL Project 50 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 51 | * 52 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 53 | * endorse or promote products derived from this software without 54 | * prior written permission. For written permission, please contact 55 | * openssl-core@openssl.org. 56 | * 57 | * 5. Products derived from this software may not be called "OpenSSL" 58 | * nor may "OpenSSL" appear in their names without prior written 59 | * permission of the OpenSSL Project. 60 | * 61 | * 6. Redistributions of any form whatsoever must retain the following 62 | * acknowledgment: 63 | * "This product includes software developed by the OpenSSL Project 64 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 65 | * 66 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 67 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 68 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 69 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 70 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 71 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 72 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 73 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 74 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 75 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 76 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 77 | * OF THE POSSIBILITY OF SUCH DAMAGE. 78 | * ==================================================================== 79 | * 80 | * This product includes cryptographic software written by Eric Young 81 | * (eay@cryptsoft.com). This product includes software written by Tim 82 | * Hudson (tjh@cryptsoft.com). 83 | * 84 | */ 85 | 86 | /* Content from openssl-1.1.0e/crypto/modes/modes_lcl.h */ 87 | 88 | #ifndef SGX_PCL_MODES_LCL_H 89 | #define SGX_PCL_MODES_LCL_H 90 | 91 | # define BSWAP8(x) ({ u64 ret_=(x); \ 92 | asm ("bswapq %0" \ 93 | : "+r"(ret_)); ret_; }) 94 | 95 | #endif // #ifndef SGX_PCL_MODES_LCL_H 96 | 97 | -------------------------------------------------------------------------------- /SampleCode/SampleEnclave/App/Edger8rSyntax/Pointers.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-2017 Intel Corporation. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in 12 | * the documentation and/or other materials provided with the 13 | * distribution. 14 | * * Neither the name of Intel Corporation nor the names of its 15 | * contributors may be used to endorse or promote products derived 16 | * from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | * 30 | */ 31 | 32 | 33 | #include "../App.h" 34 | #include "Enclave_u.h" 35 | 36 | /* edger8r_pointer_attributes: 37 | * Invokes the ECALLs declared with pointer attributes. 38 | */ 39 | void edger8r_pointer_attributes(void) 40 | { 41 | int val = 0; 42 | sgx_status_t ret = SGX_ERROR_UNEXPECTED; 43 | 44 | char c[128] = {0}; 45 | size_t len = 0; 46 | memset(c, 0xe, 128); 47 | ret = ecall_pointer_user_check(global_eid, &len, &c, 128); 48 | if (ret != SGX_SUCCESS) 49 | abort(); 50 | assert(strcmp(c, "SGX_SUCCESS") == 0); 51 | 52 | 53 | val = 0; 54 | ret = ecall_pointer_in(global_eid, &val); 55 | if (ret != SGX_SUCCESS) 56 | abort(); 57 | assert(val == 0); 58 | 59 | val = 0; 60 | ret = ecall_pointer_out(global_eid, &val); 61 | if (ret != SGX_SUCCESS) 62 | abort(); 63 | assert(val == 1234); 64 | 65 | val = 0; 66 | ret = ecall_pointer_in_out(global_eid, &val); 67 | if (ret != SGX_SUCCESS) 68 | abort(); 69 | assert(val == 1234); 70 | 71 | ret = ocall_pointer_attr(global_eid); 72 | if (ret != SGX_SUCCESS) 73 | abort(); 74 | 75 | char str1[] = "1234567890"; 76 | ret = ecall_pointer_string(global_eid, str1); 77 | if (ret != SGX_SUCCESS) 78 | abort(); 79 | assert(memcmp(str1, "0987654321", strlen(str1)) == 0); 80 | 81 | const char str2[] = "1234567890"; 82 | ret = ecall_pointer_string_const(global_eid, str2); 83 | if (ret != SGX_SUCCESS) 84 | abort(); 85 | assert(memcmp(str2, "1234567890", strlen(str2)) == 0); 86 | 87 | char str3[] = "1234567890"; 88 | ret = ecall_pointer_size(global_eid, (void*)str3, strlen(str3)); 89 | if (ret != SGX_SUCCESS) 90 | abort(); 91 | assert(memcmp(str3, "0987654321", strlen(str3)) == 0); 92 | 93 | char str4[] = "1234567890"; 94 | ret = ecall_pointer_isptr_readonly(global_eid, (buffer_t)str4, strlen(str4)); 95 | if (ret != SGX_SUCCESS) 96 | abort(); 97 | assert(memcmp(str4, "1234567890", strlen(str4)) == 0); 98 | 99 | int arr[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; 100 | ret = ecall_pointer_count(global_eid, arr, 10); 101 | if (ret != SGX_SUCCESS) 102 | abort(); 103 | 104 | for (int i = 0; i < 10; i++) 105 | assert(arr[i] == (9 - i)); 106 | 107 | memset(arr, 0x0, sizeof(arr)); 108 | ret = ecall_pointer_sizefunc(global_eid, (char *)arr); 109 | if (ret != SGX_SUCCESS) 110 | abort(); 111 | 112 | for (int i = 0; i < 10; i++) 113 | assert(arr[i] == i); 114 | 115 | return; 116 | } 117 | 118 | /* ocall_pointer_user_check: 119 | * The OCALL declared with [user_check]. 120 | */ 121 | void ocall_pointer_user_check(int* val) 122 | { 123 | (void)val; 124 | assert(val != NULL); 125 | } 126 | 127 | /* ocall_pointer_in: 128 | * The OCALL declared with [in]. 129 | */ 130 | void ocall_pointer_in(int* val) 131 | { 132 | *val = 1234; 133 | } 134 | 135 | /* ocall_pointer_out: 136 | * The OCALL declared with [out]. 137 | */ 138 | void ocall_pointer_out(int* val) 139 | { 140 | *val = 1234; 141 | } 142 | 143 | /* ocall_pointer_in_out: 144 | * The OCALL declared with [in, out]. 145 | */ 146 | void ocall_pointer_in_out(int* val) 147 | { 148 | *val = 1234; 149 | } 150 | -------------------------------------------------------------------------------- /SampleCode/SampleEnclave/Enclave/Edger8rSyntax/Types.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-2017 Intel Corporation. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in 12 | * the documentation and/or other materials provided with the 13 | * distribution. 14 | * * Neither the name of Intel Corporation nor the names of its 15 | * contributors may be used to endorse or promote products derived 16 | * from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | * 30 | */ 31 | 32 | 33 | /* Test Basic Types */ 34 | 35 | #include "sgx_trts.h" 36 | #include "../Enclave.h" 37 | #include "Enclave_t.h" 38 | #include 39 | #include 40 | 41 | /* used to eliminate `unused variable' warning */ 42 | #define UNUSED(val) (void)(val) 43 | 44 | #define ULP 2 45 | 46 | /* used to compare double variables in order to avoid compile warnings */ 47 | bool almost_equal(double x, double y) 48 | { 49 | /* the machine epsilon has to be scaled to the magnitude of the larger value 50 | and multiplied by the desired precision in ULPs (units in the last place) */ 51 | return std::abs(x-y) <= std::numeric_limits::epsilon() * std::abs(x+y) * ULP; 52 | } 53 | 54 | /* used to compare double variables in order to avoid compile warnings */ 55 | bool almost_equal(float x, float y) 56 | { 57 | /* the machine epsilon has to be scaled to the magnitude of the larger value 58 | and multiplied by the desired precision in ULPs (units in the last place) */ 59 | return std::abs(x-y) <= std::numeric_limits::epsilon() * std::abs(x+y) * ULP; 60 | } 61 | 62 | /* ecall_type_char: 63 | * [char] value passed by App. 64 | */ 65 | void ecall_type_char(char val) 66 | { 67 | assert(val == 0x12); 68 | #ifndef DEBUG 69 | UNUSED(val); 70 | #endif 71 | } 72 | 73 | /* ecall_type_int: 74 | * [int] value passed by App. 75 | */ 76 | void ecall_type_int(int val) 77 | { 78 | assert(val == 1234); 79 | #ifndef DEBUG 80 | UNUSED(val); 81 | #endif 82 | } 83 | 84 | /* ecall_type_float: 85 | * [float] value passed by App. 86 | */ 87 | void ecall_type_float(float val) 88 | { 89 | assert(almost_equal(val, (float)1234.0)); 90 | #ifndef DEBUG 91 | UNUSED(val); 92 | #endif 93 | } 94 | 95 | /* ecall_type_double: 96 | * [double] value passed by App. 97 | */ 98 | void ecall_type_double(double val) 99 | { 100 | assert(almost_equal(val, (double)1234.5678)); 101 | #ifndef DEBUG 102 | UNUSED(val); 103 | #endif 104 | } 105 | 106 | /* ecall_type_size_t: 107 | * [size_t] value passed by App. 108 | */ 109 | void ecall_type_size_t(size_t val) 110 | { 111 | assert(val == (size_t)12345678); 112 | #ifndef DEBUG 113 | UNUSED(val); 114 | #endif 115 | } 116 | 117 | /* ecall_type_wchar_t: 118 | * [wchar_t] value passed by App. 119 | */ 120 | void ecall_type_wchar_t(wchar_t val) 121 | { 122 | assert(val == (wchar_t)0x1234); 123 | #ifndef DEBUG 124 | UNUSED(val); 125 | #endif 126 | } 127 | 128 | /* ecall_type_struct: 129 | * struct_foo_t is defined in EDL and can be used in ECALL. 130 | */ 131 | void ecall_type_struct(struct struct_foo_t val) 132 | { 133 | assert(val.struct_foo_0 == 1234); 134 | assert(val.struct_foo_1 == 5678); 135 | #ifndef DEBUG 136 | UNUSED(val); 137 | #endif 138 | } 139 | 140 | /* 141 | * ecall_type_enum_union: 142 | * enum_foo_t/union_foo_t is defined in EDL 143 | * and can be used in ECALL. 144 | */ 145 | void ecall_type_enum_union(enum enum_foo_t val1, union union_foo_t *val2) 146 | { 147 | if (sgx_is_outside_enclave(val2, sizeof(union union_foo_t)) != 1) 148 | abort(); 149 | val2->union_foo_0 = 1; 150 | val2->union_foo_1 = 2; /* overwrite union_foo_0 */ 151 | assert(val1 == ENUM_FOO_0); 152 | #ifndef DEBUG 153 | UNUSED(val1); 154 | #endif 155 | } 156 | -------------------------------------------------------------------------------- /Sources/unseal/pcl_tSeal_internal.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-2017 Intel Corporation. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in 12 | * the documentation and/or other materials provided with the 13 | * distribution. 14 | * * Neither the name of Intel Corporation nor the names of its 15 | * contributors may be used to endorse or promote products derived 16 | * from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | * 30 | */ 31 | 32 | /* Content from sdk/tseal/tSeal_internal.cpp */ 33 | 34 | #include 35 | #include 36 | #include 37 | #ifdef SE_SIM 38 | #include 39 | #endif // #ifdef SE_SIM 40 | #include 41 | #include 42 | #include 43 | 44 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 45 | // pcl_unseal_data_helper() 46 | // helper function for pcl_unseal_data, does the actual decryption and MAC compare 47 | // Parameters: 48 | // [IN] p_sealed_data - pointer to sealed key blob 49 | // [OUT] p_additional_MACtext - additional text to MAC 50 | // [IN] additional_MACtext_length - length of additional text 51 | // [OUT] p_decrypted_text - buffer to fill with decrypted data 52 | // [IN] decrypted_text_length - length of p_decrypted_text buffer 53 | // Return Value: 54 | // SGX_SUCCESS or error codes 55 | sgx_status_t pcl_unseal_data_helper(const sgx_sealed_data_t *p_sealed_data, uint8_t *p_additional_MACtext, 56 | uint32_t additional_MACtext_length, uint8_t *p_decrypted_text, uint32_t decrypted_text_length) 57 | { 58 | // code based on tSeal_internal.cpp in tseal library 59 | 60 | sgx_status_t err = SGX_ERROR_UNEXPECTED; 61 | sgx_key_128bit_t seal_key; 62 | pcl_memset(&seal_key, 0, sizeof(sgx_key_128bit_t)); 63 | uint8_t payload_iv[SGX_SEAL_IV_SIZE]; 64 | pcl_memset(&payload_iv, 0, SGX_SEAL_IV_SIZE); 65 | const uint8_t* p_aad = NULL; 66 | uint32_t res = 0; 67 | sgx_aes_gcm_128bit_tag_t mac; 68 | //uint8_t mac[16]; 69 | 70 | if (decrypted_text_length > 0) 71 | pcl_memset(p_decrypted_text, 0, decrypted_text_length); 72 | 73 | if (additional_MACtext_length > 0) 74 | { 75 | // Verify GUID in sealed blob matches GUID in PCL table: 76 | p_aad = const_cast(&(p_sealed_data->aes_data.payload[decrypted_text_length])); 77 | if (pcl_consttime_memequal(p_aad, p_additional_MACtext, additional_MACtext_length) == 0) 78 | { 79 | return SGX_ERROR_PCL_GUID_MISMATCH; 80 | } 81 | pcl_memset(p_additional_MACtext, 0, additional_MACtext_length); 82 | } 83 | 84 | // Get the seal key 85 | err = pcl_sgx_get_key(&p_sealed_data->key_request, &seal_key); 86 | if (err != SGX_SUCCESS) 87 | { 88 | // Provide only error codes that the calling code could act on 89 | if ((err != SGX_ERROR_INVALID_CPUSVN) && (err != SGX_ERROR_INVALID_ISVSVN)) 90 | err = SGX_ERROR_MAC_MISMATCH; 91 | goto out; 92 | } 93 | err = pcl_gcm_decrypt( 94 | (uint8_t*)p_decrypted_text, 95 | (uint8_t*)p_sealed_data->aes_data.payload, 96 | decrypted_text_length, 97 | (uint8_t*)p_aad, 98 | additional_MACtext_length, 99 | (uint8_t*)(&seal_key), 100 | (uint8_t*)(&payload_iv[0]), 101 | (uint8_t*)(p_sealed_data->aes_data.payload_tag)); 102 | 103 | if (SGX_SUCCESS != err) 104 | { 105 | // Scrub p_decrypted_text 106 | pcl_volatile_memset((volatile void*)p_decrypted_text, 0, decrypted_text_length); 107 | goto out; 108 | } 109 | 110 | err = SGX_SUCCESS; 111 | out: 112 | // Scrub seal_key 113 | pcl_volatile_memset((volatile void*)(&seal_key), 0, sizeof(sgx_key_128bit_t)); 114 | return err; 115 | } 116 | 117 | -------------------------------------------------------------------------------- /Sources/crypto/pcl_cmac.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-2017 Intel Corporation. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in 12 | * the documentation and/or other materials provided with the 13 | * distribution. 14 | * * Neither the name of Intel Corporation nor the names of its 15 | * contributors may be used to endorse or promote products derived 16 | * from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | * 30 | */ 31 | 32 | /* ==================================================================== 33 | * Copyright (c) 1998-2017 The OpenSSL Project. All rights reserved. 34 | * 35 | * Redistribution and use in source and binary forms, with or without 36 | * modification, are permitted provided that the following conditions 37 | * are met: 38 | * 39 | * 1. Redistributions of source code must retain the above copyright 40 | * notice, this list of conditions and the following disclaimer. 41 | * 42 | * 2. Redistributions in binary form must reproduce the above copyright 43 | * notice, this list of conditions and the following disclaimer in 44 | * the documentation and/or other materials provided with the 45 | * distribution. 46 | * 47 | * 3. All advertising materials mentioning features or use of this 48 | * software must display the following acknowledgment: 49 | * "This product includes software developed by the OpenSSL Project 50 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 51 | * 52 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 53 | * endorse or promote products derived from this software without 54 | * prior written permission. For written permission, please contact 55 | * openssl-core@openssl.org. 56 | * 57 | * 5. Products derived from this software may not be called "OpenSSL" 58 | * nor may "OpenSSL" appear in their names without prior written 59 | * permission of the OpenSSL Project. 60 | * 61 | * 6. Redistributions of any form whatsoever must retain the following 62 | * acknowledgment: 63 | * "This product includes software developed by the OpenSSL Project 64 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 65 | * 66 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 67 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 68 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 69 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 70 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 71 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 72 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 73 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 74 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 75 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 76 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 77 | * OF THE POSSIBILITY OF SUCH DAMAGE. 78 | * ==================================================================== 79 | * 80 | * This product includes cryptographic software written by Eric Young 81 | * (eay@cryptsoft.com). This product includes software written by Tim 82 | * Hudson (tjh@cryptsoft.com). 83 | * 84 | */ 85 | 86 | /* Content from openssl-1.1.0e/crypto/cmac/cmac.c */ 87 | 88 | #include 89 | #include 90 | #include 91 | #include 92 | #include 93 | #include 94 | #include 95 | 96 | #ifdef SE_SIM 97 | 98 | void make_kn( 99 | unsigned char *k1, 100 | const unsigned char *l, 101 | int bl) 102 | { 103 | int i; 104 | unsigned char c = l[0], carry = c >> 7, cnext; 105 | 106 | /* Shift block to left, including carry */ 107 | for (i = 0; i < bl - 1; i++, c = cnext) 108 | k1[i] = (c << 1) | ((cnext = l[i + 1]) >> 7); 109 | 110 | /* If MSB set fixup with R */ 111 | k1[i] = (c << 1) ^ ((0 - carry) & (bl == 16 ? 0x87 : 0x1b)); 112 | } 113 | 114 | #endif // #ifdef SE_SIM 115 | 116 | -------------------------------------------------------------------------------- /SampleCode/SampleEnclave/Seal/Seal.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-2017 Intel Corporation. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in 12 | * the documentation and/or other materials provided with the 13 | * distribution. 14 | * * Neither the name of Intel Corporation nor the names of its 15 | * contributors may be used to endorse or promote products derived 16 | * from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | * 30 | */ 31 | 32 | #include 33 | #include "Seal_t.h" 34 | #include 35 | #include 36 | #include "pcl_common.h" 37 | 38 | /** 39 | * @func provision_key_mock assigns the decryption key 40 | * @param OUT uint8_t* key_ptr: pointer to a key buffer allocated by caller. 41 | * @param uint32_t key_len: key buffer size. 42 | * @return sgx_status_t 43 | * SGX_ERROR_INVALID_PARAMETER if key size is not SGX_AESGCM_KEY_SIZE or if key_ptr is NULL 44 | * SGX_SUCCESS if success 45 | * Notice: Function returns a hardcoded key. Never use in release code!!! 46 | * ISV must replace with secured key provisioning scheme, e.g. using remote attestation & TLS. 47 | */ 48 | sgx_status_t provision_key_mock ( OUT uint8_t* key_ptr, uint32_t key_len ) 49 | { 50 | if ( (NULL == key_ptr) || (SGX_AESGCM_KEY_SIZE != key_len)) 51 | { 52 | return SGX_ERROR_INVALID_PARAMETER; 53 | } 54 | const uint8_t key[SGX_AESGCM_KEY_SIZE] = 55 | { 0x22, 0x22, 0x33, 0x33, 0x44, 0x44, 0x55, 0x55, 0x66, 0x66, 0x77, 0x77, 0x88, 0x88, 0x99, 0x99 }; 56 | memcpy (key_ptr, key, key_len); 57 | return SGX_SUCCESS; 58 | } 59 | 60 | /* 61 | * @func provision_key provisions the key to the ISVs platform 62 | * @param OUT uint8_t* key_ptr is the resulting decryption key 63 | * @param uint32_t key_len is key size in bytes 64 | * @return sgx_status_t, SGX_SUCCESS if function passes 65 | * @warning ISV must replace content of this function with ISVs scheme to provision 66 | * the decryption key to the platform 67 | */ 68 | sgx_status_t provision_key( uint8_t* key_ptr, uint32_t key_len ) 69 | { 70 | /* 71 | * ISV must replace call to provision_key_mock with an alternative ISV's secured key provisioning scheme, e.g. using remote attestation & TLS. 72 | * For more details, see 'Intel(R) SGX PCL Linux User Guide.pdf', chapter 'Integration with PCL', sub chapter 'Sealing Enclave'. 73 | */ 74 | return provision_key_mock(key_ptr, key_len); 75 | } 76 | 77 | extern "C" 78 | { 79 | 80 | /* 81 | * @func ecall_get_sealed_blob_size returns the PCL sealed blob size 82 | * @return size_t, size of PCL sealed blob size in bytes 83 | */ 84 | size_t ecall_get_sealed_blob_size() 85 | { 86 | return (size_t)sgx_calc_sealed_data_size ( SGX_PCL_GUID_SIZE, SGX_AESGCM_KEY_SIZE ); 87 | } 88 | 89 | /* 90 | * @func ecall_generate_sealed_blob generates the sealed blob 91 | * @param OUT uint8_t* sealed_blob is the resulting sealed blob 92 | * @param uint32_t sealed_blob_size is sealed blob size in bytes 93 | * @return sgx_status_t 94 | * SGX_ERROR_INVALID_PARAMETER if sealed_blob is NULL or if sealed_blob_size does not match PCL sealed blob size 95 | * The respective error in case provision_key or sgx_seal_data fail 96 | * SGX_SUCCESS if function passes 97 | */ 98 | sgx_status_t ecall_generate_sealed_blob(OUT uint8_t* sealed_blob, size_t sealed_blob_size) 99 | { 100 | if ((NULL == sealed_blob) || (ecall_get_sealed_blob_size() != sealed_blob_size)) 101 | { 102 | return SGX_ERROR_INVALID_PARAMETER; 103 | } 104 | 105 | sgx_status_t retstatus = SGX_ERROR_UNEXPECTED; 106 | uint8_t key[SGX_AESGCM_KEY_SIZE] = { 0 }; 107 | 108 | retstatus = provision_key(key, SGX_AESGCM_KEY_SIZE); 109 | if (retstatus != SGX_SUCCESS ) 110 | { 111 | return retstatus; 112 | } 113 | 114 | retstatus = sgx_seal_data ( 115 | SGX_PCL_GUID_SIZE, // AAD size 116 | g_pcl_guid, // AAD 117 | SGX_AESGCM_KEY_SIZE, // Key len 118 | key, // Key 119 | sealed_blob_size, // Resulting blob size 120 | (sgx_sealed_data_t*)sealed_blob ); // Resulting blob 121 | 122 | memset(key, 0,SGX_AESGCM_KEY_SIZE); 123 | return retstatus; 124 | } 125 | 126 | }; // extern "C" 127 | 128 | -------------------------------------------------------------------------------- /Sources/unseal/pcl_tSeal.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-2017 Intel Corporation. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in 12 | * the documentation and/or other materials provided with the 13 | * distribution. 14 | * * Neither the name of Intel Corporation nor the names of its 15 | * contributors may be used to endorse or promote products derived 16 | * from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | * 30 | */ 31 | 32 | /* Content from sdk/tseal/tSeal.cpp */ 33 | 34 | #include 35 | #include 36 | #include 37 | #ifdef SE_SIM 38 | #include 39 | #endif // #ifdef SE_SIM 40 | #include 41 | #include 42 | #include 43 | 44 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 45 | // pcl_unseal_data() 46 | // PCL version of sgx_unseal_data (needed since the SGX SDK is unavailable at this point) 47 | // Parameters: 48 | // [IN] p_sealed_data - pointer to sealed key blob 49 | // [OUT] p_additional_MACtext - additional text to MAC 50 | // [IN] additional_MACtext_length - length of additional text 51 | // [OUT] p_decrypted_text - buffer to fill with decrypted data 52 | // [IN] decrypted_text_length - length of p_decrypted_text buffer 53 | // Return Value: 54 | // SGX_SUCCESS or error codes 55 | sgx_status_t pcl_unseal_data(const sgx_sealed_data_t *p_sealed_data, uint8_t *p_additional_MACtext, 56 | uint32_t *p_additional_MACtext_length, uint8_t *p_decrypted_text, uint32_t *p_decrypted_text_length) 57 | { 58 | sgx_status_t err = SGX_ERROR_UNEXPECTED; 59 | // Ensure the the sgx_sealed_data_t members are all inside enclave before using them. 60 | if ((p_sealed_data == NULL) || (!pcl_is_within_enclave(p_sealed_data,sizeof(sgx_sealed_data_t)))) 61 | { 62 | return SGX_ERROR_INVALID_PARAMETER; 63 | } 64 | 65 | uint32_t encrypt_text_length = pcl_get_encrypt_txt_len(p_sealed_data); 66 | if (encrypt_text_length == UINT32_MAX) 67 | { 68 | return SGX_ERROR_MAC_MISMATCH; // Return error indicating the blob is corrupted 69 | } 70 | uint32_t aad_text_length = pcl_get_aad_mac_txt_len(p_sealed_data); 71 | if (aad_text_length == UINT32_MAX) 72 | { 73 | return SGX_ERROR_MAC_MISMATCH; // Return error indicating the blob is corrupted 74 | } 75 | uint32_t sealedDataSize = pcl_calc_sealed_data_size(aad_text_length, encrypt_text_length); 76 | if (sealedDataSize == UINT32_MAX) 77 | { 78 | return SGX_ERROR_MAC_MISMATCH; // Return error indicating the blob is corrupted 79 | } 80 | 81 | // 82 | // Check parameters 83 | // 84 | // Ensure sealed data blob is within an enclave during the sealing process 85 | if (!pcl_is_within_enclave(p_sealed_data,sealedDataSize)) 86 | { 87 | return SGX_ERROR_INVALID_PARAMETER; 88 | } 89 | if ((aad_text_length > 0) && ((p_additional_MACtext == NULL) || (p_additional_MACtext_length == NULL))) 90 | { 91 | return SGX_ERROR_INVALID_PARAMETER; 92 | } 93 | if ((encrypt_text_length < 1) || (p_decrypted_text == NULL) || (p_decrypted_text_length == NULL)) 94 | { 95 | return SGX_ERROR_INVALID_PARAMETER; 96 | } 97 | if (!pcl_is_within_enclave(p_decrypted_text,encrypt_text_length)) 98 | { 99 | return SGX_ERROR_INVALID_PARAMETER; 100 | } 101 | if (!pcl_is_within_enclave(p_decrypted_text_length,sizeof(p_decrypted_text_length))) 102 | { 103 | return SGX_ERROR_INVALID_PARAMETER; 104 | } 105 | // Ensure aad data does not cross enclave boundary 106 | if ((aad_text_length > 0) && 107 | (!(pcl_is_within_enclave(p_additional_MACtext,aad_text_length) || pcl_is_outside_enclave(p_additional_MACtext, aad_text_length)))) 108 | { 109 | return SGX_ERROR_INVALID_PARAMETER; 110 | } 111 | if ((*p_decrypted_text_length) < encrypt_text_length) 112 | { 113 | return SGX_ERROR_INVALID_PARAMETER; 114 | } 115 | uint32_t additional_MACtext_length = (NULL != p_additional_MACtext_length) ? *p_additional_MACtext_length : 0; 116 | if (additional_MACtext_length != aad_text_length) { 117 | return SGX_ERROR_INVALID_PARAMETER; 118 | } 119 | 120 | err = pcl_unseal_data_helper(p_sealed_data, p_additional_MACtext, aad_text_length, 121 | p_decrypted_text, encrypt_text_length); 122 | if (err == SGX_SUCCESS) 123 | { 124 | *p_decrypted_text_length = encrypt_text_length; 125 | if (p_additional_MACtext_length != NULL) 126 | *p_additional_MACtext_length = aad_text_length; 127 | } 128 | else 129 | { 130 | // Scrub p_decrypted_text 131 | pcl_volatile_memset((volatile void*)p_decrypted_text, 0, encrypt_text_length); 132 | } 133 | return err; 134 | } 135 | 136 | -------------------------------------------------------------------------------- /Tools/Encryptip/encryptip.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-2017 Intel Corporation. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in 12 | * the documentation and/or other materials provided with the 13 | * distribution. 14 | * * Neither the name of Intel Corporation nor the names of its 15 | * contributors may be used to endorse or promote products derived 16 | * from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | * 30 | */ 31 | 32 | #ifndef ENCRYPTIP_H 33 | #define ENCRYPTIP_H 34 | 35 | // PCL table must be alligned to PCL_TABLE_ALLIGNMENT bytes 36 | #define PCL_TABLE_ALLIGNMENT (16) 37 | 38 | #define PCL_TEXT_SECTION_NAME ".nipx" 39 | #define PCL_DATA_SECTION_NAME ".nipd" 40 | #define PCL_RODATA_SECTION_NAME ".niprod" 41 | 42 | #define NUM_NON_IP_OR_DEBUG_SEC_NAMES (23) 43 | #define NUM_NON_IP_SEC_NAMES (12) 44 | 45 | /* 46 | * When running CPUID with leaf 0, bit 30 of the output ECX 47 | * indicates if platform supports RDRAND 48 | */ 49 | #define SUPPORT_RDRAND (1<<30) 50 | 51 | // GCM can encrypt up to (2^32 - 2) blocks per single IV 52 | #define PCL_GCM_MAX_NUM_BLOCKS \ 53 | ((size_t)((uint32_t)(-2))) 54 | 55 | #define PCL_GCM_NUM_BLOCKS(size) \ 56 | ((size + PCL_AES_BLOCK_LEN - 1)/ PCL_AES_BLOCK_LEN) 57 | 58 | #define EVP_SUCCESS (1) 59 | #define RDRAND_SUCCESS (1) 60 | #define ENCIP_ERROR(r) (ENCIP_SUCCESS != (r)) 61 | 62 | typedef enum encip_ret_e_ 63 | { 64 | ENCIP_SUCCESS = 0x0, 65 | ENCIP_ERROR_FAIL = 0x80, 66 | ENCIP_ERROR_INVALID_PARAM = 0x82, 67 | ENCIP_ERROR_KEY_FILE_SIZE = 0x84, 68 | ENCIP_ERROR_ENCLAVE_SIZE = 0x86, 69 | ENCIP_ERROR_MEM_ALLOC = 0x88, 70 | ENCIP_ERROR_PARSE_ARGS = 0x8a, 71 | ENCIP_ERROR_PARSE_INVALID_PARAM = 0x8c, 72 | ENCIP_ERROR_READF_INVALID_PARAM = 0x8e, 73 | ENCIP_ERROR_READF_OPEN = 0x90, 74 | ENCIP_ERROR_READF_ALLOC = 0x92, 75 | ENCIP_ERROR_READF_READ = 0x94, 76 | ENCIP_ERROR_WRITEF_INVALID_PARAM = 0x96, 77 | ENCIP_ERROR_WRITEF_OPEN = 0x98, 78 | ENCIP_ERROR_WRITEF_WRITE = 0x9a, 79 | ENCIP_ERROR_SEALED_BUF_SIZE = 0x9c, 80 | ENCIP_ERROR_GETTBL_INVALID_PARAM = 0x9e, 81 | ENCIP_ERROR_TBL_NOT_FOUND = 0xa0, 82 | ENCIP_ERROR_TBL_NOT_ALIGNED = 0xa2, 83 | ENCIP_ERROR_ALREADY_ENCRYPTED = 0xa4, 84 | ENCIP_ERROR_IMPROPER_STATE = 0xa6, 85 | ENCIP_ERROR_RANDIV_INVALID_PARAM = 0xa8, 86 | ENCIP_ERROR_RDRAND_NOT_SUPPORTED = 0xa9, 87 | ENCIP_ERROR_RDRAND_FAILED = 0xaa, 88 | ENCIP_ERROR_GCM_ENCRYPT_INVALID_PARAM = 0xac, 89 | ENCIP_ERROR_ENCRYPT_ALLOC = 0xae, 90 | ENCIP_ERROR_ENCRYPT_INIT_EX = 0xb0, 91 | ENCIP_ERROR_ENCRYPT_IV_LEN = 0xb2, 92 | ENCIP_ERROR_ENCRYPT_INIT_KEY = 0xb4, 93 | ENCIP_ERROR_ENCRYPT_AAD = 0xb6, 94 | ENCIP_ERROR_ENCRYPT_UPDATE = 0xb8, 95 | ENCIP_ERROR_ENCRYPT_FINAL = 0xba, 96 | ENCIP_ERROR_ENCRYPT_TAG = 0xbc, 97 | ENCIP_ERROR_INCIV_INVALID_PARAM = 0xbe, 98 | ENCIP_ERROR_IV_OVERFLOW = 0xc0, 99 | ENCIP_ERROR_SHA_INVALID_PARAM = 0xc2, 100 | ENCIP_ERROR_SHA_ALLOC = 0xc4, 101 | ENCIP_ERROR_SHA_INIT = 0xc6, 102 | ENCIP_ERROR_SHA_UPDATE = 0xc8, 103 | ENCIP_ERROR_SHA_FINAL = 0xca, 104 | ENCIP_ERROR_ENCSECS_INVALID_PARAM = 0xcc, 105 | ENCIP_ERROR_ENCSECS_COUNTER_OVERFLOW = 0xce, 106 | ENCIP_ERROR_ENCSECS_RVAS_OVERFLOW = 0xd0, 107 | ENCIP_ERROR_UPDATEF_INVALID_PAR = 0xd2, 108 | ENCIP_ERROR_ENCRYPTIP_INVALID_PARAM = 0xd4, 109 | ENCIP_ERROR_PARSE_ELF_INVALID_PARAM = 0xd6, 110 | ENCIP_ERROR_PARSE_ELF_INVALID_IMAGE = 0xd8, 111 | ENCIP_ERROR_SEGMENT_NOT_READABLE = 0xda, 112 | }encip_ret_e; 113 | 114 | typedef struct pcl_data_t_ 115 | { 116 | 117 | Elf64_Shdr* elf_sec; 118 | Elf64_Half shstrndx; 119 | char* sections_names; 120 | Elf64_Phdr* phdr; 121 | uint16_t nsections; 122 | uint16_t nsegments; 123 | }pcl_data_t; 124 | 125 | encip_ret_e parse_args( 126 | int argc, 127 | IN char* argv[], 128 | OUT char** ifname, 129 | OUT char** ofname, 130 | OUT char** kfname, 131 | OUT bool* debug); 132 | static encip_ret_e read_file(IN const char* const name,OUT uint8_t** data,OUT size_t* size_out); 133 | static encip_ret_e write_file(const char* const name, uint8_t* data, size_t size); 134 | encip_ret_e gcm_encrypt( 135 | unsigned char *plaintext, 136 | size_t plaintext_len, 137 | unsigned char *aad, 138 | size_t aad_len, 139 | unsigned char *key, 140 | unsigned char *iv, 141 | unsigned char *ciphertext, 142 | unsigned char *tag); 143 | encip_ret_e sha256(const void* const buf, size_t buflen, uint8_t* hash); 144 | encip_ret_e encrypt_ip(uint8_t* elf_buf, size_t elf_size, uint8_t* key, bool debug); 145 | encip_ret_e write_tbl_strs(pcl_table_t* tbl); 146 | 147 | #endif // #ifndef ENCRYPTIP_H 148 | 149 | -------------------------------------------------------------------------------- /SampleCode/SampleEnclave/Enclave/Edger8rSyntax/Pointers.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-2017 Intel Corporation. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in 12 | * the documentation and/or other materials provided with the 13 | * distribution. 14 | * * Neither the name of Intel Corporation nor the names of its 15 | * contributors may be used to endorse or promote products derived 16 | * from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | * 30 | */ 31 | 32 | 33 | /* Test Pointer Auttributes */ 34 | 35 | #include 36 | #include 37 | 38 | #include "sgx_trts.h" 39 | #include "../Enclave.h" 40 | #include "Enclave_t.h" 41 | 42 | /* checksum_internal: 43 | * get simple checksum of input buffer and length 44 | */ 45 | int32_t checksum_internal(char *buf, size_t count) 46 | { 47 | register int32_t sum = 0; 48 | int16_t *ptr = (int16_t *)buf; 49 | 50 | /* Main summing loop */ 51 | while(count > 1) { 52 | sum = sum + *ptr++; 53 | count = count - 2; 54 | } 55 | 56 | /* Add left-over byte, if any */ 57 | if (count > 0) 58 | sum = sum + *((char *)ptr); 59 | 60 | return ~sum; 61 | } 62 | 63 | /* ecall_pointer_user_check, ecall_pointer_in, ecall_pointer_out, ecall_pointer_in_out: 64 | * The root ECALLs to test [in], [out], [user_check] attributes. 65 | */ 66 | size_t ecall_pointer_user_check(void *val, size_t sz) 67 | { 68 | /* check if the buffer is allocated outside */ 69 | if (sgx_is_outside_enclave(val, sz) != 1) 70 | abort(); 71 | 72 | char tmp[100] = {0}; 73 | size_t len = sz>100?100:sz; 74 | 75 | /* copy the memory into the enclave to make sure 'val' 76 | * is not being changed in checksum_internal() */ 77 | memcpy(tmp, val, len); 78 | 79 | int32_t sum = checksum_internal((char *)tmp, len); 80 | printf("Checksum(0x%p, %zu) = 0x%x\n", 81 | val, len, (unsigned int)sum); 82 | 83 | /* modify outside memory directly */ 84 | memcpy(val, "SGX_SUCCESS", len>12?12:len); 85 | 86 | return len; 87 | } 88 | 89 | /* ecall_pointer_in: 90 | * the buffer of val is copied to the enclave. 91 | */ 92 | 93 | void ecall_pointer_in(int *val) 94 | { 95 | if (sgx_is_within_enclave(val, sizeof(int)) != 1) 96 | abort(); 97 | *val = 1234; 98 | } 99 | 100 | /* ecall_pointer_out: 101 | * the buffer of val is copied to the untrusted side. 102 | */ 103 | void ecall_pointer_out(int *val) 104 | { 105 | if (sgx_is_within_enclave(val, sizeof(int)) != 1) 106 | abort(); 107 | assert(*val == 0); 108 | *val = 1234; 109 | } 110 | 111 | /* ecall_pointer_in_out: 112 | * the buffer of val is double-copied. 113 | */ 114 | void ecall_pointer_in_out(int *val) 115 | { 116 | if (sgx_is_within_enclave(val, sizeof(int)) != 1) 117 | abort(); 118 | *val = 1234; 119 | } 120 | 121 | /* ocall_pointer_attr: 122 | * The root ECALL that test OCALL [in], [out], [user_check]. 123 | */ 124 | void ocall_pointer_attr(void) 125 | { 126 | sgx_status_t ret = SGX_ERROR_UNEXPECTED; 127 | 128 | int val = 0; 129 | ret = ocall_pointer_user_check(&val); 130 | if (ret != SGX_SUCCESS) 131 | abort(); 132 | 133 | val = 0; 134 | ret = ocall_pointer_in(&val); 135 | if (ret != SGX_SUCCESS) 136 | abort(); 137 | assert(val == 0); 138 | 139 | val = 0; 140 | ret = ocall_pointer_out(&val); 141 | if (ret != SGX_SUCCESS) 142 | abort(); 143 | assert(val == 1234); 144 | 145 | val = 0; 146 | ret = ocall_pointer_in_out(&val); 147 | if (ret != SGX_SUCCESS) 148 | abort(); 149 | assert(val == 1234); 150 | 151 | return; 152 | } 153 | 154 | /* ecall_pointer_string: 155 | * [string] defines a string. 156 | */ 157 | void ecall_pointer_string(char *str) 158 | { 159 | strncpy(str, "0987654321", strlen(str)); 160 | } 161 | 162 | /* ecall_pointer_string_const: 163 | * const [string] defines a string that cannot be modified. 164 | */ 165 | void ecall_pointer_string_const(const char *str) 166 | { 167 | char* temp = new char[strlen(str)]; 168 | strncpy(temp, str, strlen(str)); 169 | delete []temp; 170 | } 171 | 172 | /* ecall_pointer_size: 173 | * 'len' needs to be specified to tell Edger8r the length of 'str'. 174 | */ 175 | void ecall_pointer_size(void *ptr, size_t len) 176 | { 177 | strncpy((char*)ptr, "0987654321", len); 178 | } 179 | 180 | /* ecall_pointer_count: 181 | * 'cnt' needs to be specified to tell Edger8r the number of elements in 'arr'. 182 | */ 183 | void ecall_pointer_count(int *arr, int cnt) 184 | { 185 | for (int i = (cnt - 1); i >= 0; i--) 186 | arr[i] = (cnt - 1 - i); 187 | } 188 | 189 | /* ecall_pointer_isptr_readonly: 190 | * 'buf' is user defined type, shall be tagged with [isptr]. 191 | * if it's not writable, [readonly] shall be specified. 192 | */ 193 | void ecall_pointer_isptr_readonly(buffer_t buf, size_t len) 194 | { 195 | strncpy((char*)buf, "0987654321", len); 196 | } 197 | 198 | /* get_buffer_len: 199 | * get the length of input buffer 'buf'. 200 | */ 201 | size_t get_buffer_len(const char* buf) 202 | { 203 | (void)buf; 204 | return 10*sizeof(int); 205 | } 206 | 207 | /* ecall_pointer_sizefunc: 208 | * call get_buffer_len to determin the length of 'buf'. 209 | */ 210 | void ecall_pointer_sizefunc(char *buf) 211 | { 212 | int *tmp = (int*)buf; 213 | for (int i = 0; i < 10; i++) { 214 | assert(tmp[i] == 0); 215 | tmp[i] = i; 216 | } 217 | } 218 | -------------------------------------------------------------------------------- /Sources/pcl_mem.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-2017 Intel Corporation. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in 12 | * the documentation and/or other materials provided with the 13 | * distribution. 14 | * * Neither the name of Intel Corporation nor the names of its 15 | * contributors may be used to endorse or promote products derived 16 | * from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | * 30 | */ 31 | 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | 40 | /* 41 | * @func pcl_memcpy implements memcpy that can run before runtime initiazliation 42 | * @param OUT void* dst, output destination buffer 43 | * @param IN void* src, input source buffer 44 | * @param size_t size, buffer size in bytes 45 | */ 46 | void pcl_memcpy(OUT void* dst, IN void* src, size_t size) 47 | { 48 | if(NULL == dst || NULL == src)abort(); 49 | for(size_t i=0;i> 8)); 116 | } 117 | 118 | /* 119 | * g_pcl_imagebase is set by PCL at runtime to ELF base address. 120 | * It is used by functions pcl_is_outside_enclave and pcl_is_within_enclave 121 | */ 122 | extern uintptr_t g_pcl_imagebase; 123 | 124 | /* 125 | * @func pcl_is_outside_enclave checks if buffer is completely outside the enclave 126 | * @par IN const void* addr - the start address of the buffer 127 | * @par size_t size - buffer size in bytes 128 | * @return int 129 | * 1 - the buffer is strictly outside the enclave 130 | * 0 - the whole buffer or part of the buffer is not outside the enclave, 131 | * or the buffer is wrap around 132 | */ 133 | int pcl_is_outside_enclave(IN const void *addr, size_t size) 134 | { 135 | size_t start = (size_t)addr; 136 | size_t end = 0; 137 | size_t enclave_start = (size_t)g_pcl_imagebase; 138 | size_t enclave_end = enclave_start + g_global_data.enclave_size - 1; 139 | // g_global_data.enclave_end = enclave_base + enclave_size - 1; 140 | // so the enclave range is [enclave_start, enclave_end] inclusively 141 | 142 | if(size > 0) 143 | { 144 | end = start + size - 1; 145 | } 146 | else 147 | { 148 | end = start; 149 | } 150 | 151 | if( (start <= end) && ((end < enclave_start) || (start > enclave_end)) ) 152 | { 153 | return 1; 154 | } 155 | return 0; 156 | } 157 | 158 | /* 159 | * @func pcl_is_outside_enclave checks if buffer is completely inside the enclave 160 | * @par IN const void* addr - the start address of the buffer 161 | * @par size_t size - buffer size in bytes 162 | * @return int 163 | * 1 - the buffer is strictly within the enclave 164 | * 0 - the whole buffer or part of the buffer is not within the enclave, 165 | * or the buffer is wrap around 166 | */ 167 | int pcl_is_within_enclave(const void *addr, size_t size) 168 | { 169 | size_t start = (size_t)addr; 170 | size_t end = 0; 171 | size_t enclave_start = (size_t)g_pcl_imagebase; 172 | size_t enclave_end = enclave_start + g_global_data.enclave_size - 1; 173 | // g_global_data.enclave_end = enclave_base + enclave_size - 1; 174 | // so the enclave range is [enclave_start, enclave_end] inclusively 175 | 176 | if(size > 0) 177 | { 178 | end = start + size - 1; 179 | } 180 | else 181 | { 182 | end = start; 183 | } 184 | if( (start <= end) && (start >= enclave_start) && (end <= enclave_end) ) 185 | { 186 | return 1; 187 | } 188 | return 0; 189 | } 190 | 191 | -------------------------------------------------------------------------------- /Sources/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (C) 2011-2017 Intel Corporation. All rights reserved. 3 | # 4 | # Redistribution and use in source and binary forms, with or without 5 | # modification, are permitted provided that the following conditions 6 | # are met: 7 | # 8 | # * Redistributions of source code must retain the above copyright 9 | # notice, this list of conditions and the following disclaimer. 10 | # * Redistributions in binary form must reproduce the above copyright 11 | # notice, this list of conditions and the following disclaimer in 12 | # the documentation and/or other materials provided with the 13 | # distribution. 14 | # * Neither the name of Intel Corporation nor the names of its 15 | # contributors may be used to endorse or promote products derived 16 | # from this software without specific prior written permission. 17 | # 18 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | # 30 | # 31 | 32 | # PCL library makefile 33 | 34 | ifndef $(VERBOSE) 35 | VERBOSE := @ 36 | endif 37 | 38 | ######## Verify environment: ######## 39 | 40 | ifeq ($(PCL_DIR),) 41 | $(error PCL_DIR environment variable must be set!!!) 42 | endif 43 | 44 | ifeq ($(SGX_SDK_SRCS),) 45 | $(error SGX_SDK_SRCS environment variable must be set to home directory of SGX SDK source files!!!) 46 | endif 47 | 48 | ######## SGX SDK Settings ######## 49 | 50 | # location of SDK include, bin, lib folders 51 | # SGX_SDK defined - build with SDK install (default: /opt/intel/sgxsdk) 52 | ifeq ($(SGX_SDK),) 53 | $(error SGX_SDK environment variable must be set to SGX SDK root directory (e.g. /opt/intel/sgxsdk) !!!) 54 | endif 55 | 56 | # build configuration default settings HW, x64, debug 57 | # SGX_MODE ?= HW default required for clean 58 | SGX_MODE ?= HW 59 | SGX_ARCH ?= x64 60 | DEBUG ?= 0 61 | 62 | # 32 bit build currently not supported on linux 63 | ifeq ($(shell getconf LONG_BIT), 32) 64 | $(error x86 build is not supported, only x64!!) 65 | else ifeq ($(findstring -m32, $(CXXFLAGS)), -m32) 66 | $(error x86 build is not supported, only x64!!) 67 | endif 68 | 69 | ifeq ($(SGX_ARCH), x86) 70 | $(error x86 build is not supported, only x64!!) 71 | else 72 | SGX_COMMON_CFLAGS := -m64 73 | endif 74 | 75 | ifeq ($(DEBUG), 1) 76 | SGX_COMMON_CFLAGS += -O0 -g 77 | else 78 | SGX_COMMON_CFLAGS += -O2 79 | endif 80 | 81 | ######## Enclave Settings ######## 82 | 83 | # output dir for simulation build objects 84 | SIM_DIR := simulation/ 85 | 86 | # library names 87 | ifneq ($(SGX_MODE), HW) 88 | SIM := sim 89 | OBJ_DIR := $(SIM_DIR) 90 | endif 91 | 92 | PCL_SIM_LIB_NAME := sgx_pclsim 93 | PCL_LIB_NAME := sgx_pcl$(SIM) 94 | PCL_SIM_LIB := $(PCL_DIR)/lib64/lib$(PCL_SIM_LIB_NAME).a 95 | PCL_LIB := $(PCL_DIR)/lib64/lib$(PCL_LIB_NAME).a 96 | 97 | # source files 98 | PCL_ASM_FILES := \ 99 | crypto/pcl_vpaes-x86_64.s \ 100 | crypto/pcl_ghash-x86_64.s 101 | 102 | PCL_CPP_FILES := \ 103 | pcl_entry.cpp \ 104 | pcl_mem.cpp \ 105 | crypto/pcl_crypto.cpp \ 106 | unseal/pcl_tSeal.cpp \ 107 | unseal/pcl_sgx_get_key.cpp \ 108 | unseal/pcl_tSeal_util.cpp \ 109 | unseal/pcl_tSeal_internal.cpp 110 | 111 | PCL_C_FILES := \ 112 | crypto/pcl_sha256.c \ 113 | crypto/pcl_gcm128.c 114 | 115 | # simulation mode only 116 | PCL_SIM_C_FILES := crypto/pcl_cmac.c 117 | PCL_SIM_CPP_FILES := unseal/sim/pcl_deriv.cpp unseal/sim/pcl_t_instructions.cpp 118 | 119 | ifneq ($(SGX_MODE), HW) 120 | PCL_C_FILES += $(PCL_SIM_C_FILES) 121 | PCL_CPP_FILES += $(PCL_SIM_CPP_FILES) 122 | endif 123 | 124 | # object files 125 | PCL_CPP_OBJECTS := $(PCL_CPP_FILES:%.cpp=$(OBJ_DIR)%.o) 126 | PCL_C_OBJECTS := $(PCL_C_FILES:%.c=$(OBJ_DIR)%.o) 127 | PCL_ASM_OBJECTS := $(PCL_ASM_FILES:%.s=$(OBJ_DIR)%.o) 128 | 129 | # simulation objects required for clean 130 | PCL_SIM_CPP_OBJECTS := $(PCL_SIM_CPP_FILES:%.cpp=$(OBJ_DIR)%.o) 131 | PCL_SIM_C_OBJECTS := $(PCL_SIM_C_FILES:%.c=$(OBJ_DIR)%.o) 132 | 133 | # build flags 134 | PCL_INCLUDE_PATH := -I$(PCL_DIR)/Include \ 135 | -I$(PCL_DIR)/Common \ 136 | -I$(PCL_DIR)/Sources/crypto \ 137 | -I$(PCL_DIR)/Sources/unseal \ 138 | -I$(SGX_SDK)/include \ 139 | -I$(SGX_SDK)/include/tlibc \ 140 | -I/usr/include/x86_64-linux-gnu \ 141 | -I/usr/include \ 142 | -I./ 143 | 144 | PCL_LIB_C_FLAGS := $(SGX_COMMON_CFLAGS) -nostdinc -fvisibility=hidden -fpie -fstack-protector $(PCL_INCLUDE_PATH) -I$(SGX_SDK_SRCS)/common/inc/internal 145 | ifneq ($(SGX_MODE), HW) 146 | PCL_LIB_C_FLAGS += -DSE_SIM=1 -I$(SGX_SDK_SRCS)/sdk/simulation/tinst -I$(SGX_SDK_SRCS)/common/inc/internal 147 | endif 148 | 149 | PCL_LIB_CPP_FLAGS := $(PCL_LIB_C_FLAGS) -std=c++03 -nostdinc++ 150 | 151 | # targets 152 | .PHONY: all 153 | 154 | all: 155 | $(VERBOSE)$(MAKE) SGX_MODE=HW build 156 | $(VERBOSE)$(MAKE) SGX_MODE=SIM build 157 | 158 | build: $(PCL_LIB) 159 | 160 | ######## PCL Lib ################ 161 | 162 | OBJCOPY := objcopy 163 | NIPX := .nipx 164 | NIPD := .nipd 165 | NIPRODT := .niprod 166 | 167 | $(OBJ_DIR)%.o: %.s 168 | $(VERBOSE)mkdir -p $(@D) 169 | $(VERBOSE)$(CXX) $(PCL_LIB_C_FLAGS) -c $< -o $@ 170 | $(VERBOSE)$(OBJCOPY) $@ --rename-section .text=$(NIPX) 171 | $(VERBOSE)$(OBJCOPY) $@ --rename-section .data=$(NIPD) 172 | $(VERBOSE)$(OBJCOPY) $@ --rename-section .rodata=$(NIPRODT) 173 | @echo "CXX <= $<" 174 | 175 | $(OBJ_DIR)%.o: %.c 176 | $(VERBOSE)mkdir -p $(@D) 177 | $(VERBOSE)$(CXX) $(PCL_LIB_C_FLAGS) -c $< -o $@ 178 | $(VERBOSE)$(OBJCOPY) $@ --rename-section .text=$(NIPX) 179 | $(VERBOSE)$(OBJCOPY) $@ --rename-section .data=$(NIPD) 180 | $(VERBOSE)$(OBJCOPY) $@ --rename-section .rodata=$(NIPRODT) 181 | @echo "CXX <= $<" 182 | 183 | $(OBJ_DIR)%.o: %.cpp 184 | $(VERBOSE)mkdir -p $(@D) 185 | $(VERBOSE)$(CXX) $(PCL_LIB_CPP_FLAGS) -c $< -o $@ 186 | $(VERBOSE)$(OBJCOPY) $@ --rename-section .text=$(NIPX) 187 | $(VERBOSE)$(OBJCOPY) $@ --rename-section .data=$(NIPD) 188 | $(VERBOSE)$(OBJCOPY) $@ --rename-section .rodata=$(NIPRODT) 189 | @echo "CXX <= $<" 190 | 191 | $(PCL_LIB): $(PCL_CPP_OBJECTS) $(PCL_ASM_OBJECTS) $(PCL_C_OBJECTS) 192 | @echo "library build with DEBUG =" $(DEBUG) 193 | $(VERBOSE)ar -rc $@ $^ 194 | @echo "AR <= $@" 195 | 196 | .PHONY: clean 197 | 198 | clean: 199 | $(VERBOSE)rm -f $(PCL_LIB) $(PCL_SIM_LIB) $(PCL_CPP_OBJECTS) $(PCL_ASM_OBJECTS) $(PCL_C_OBJECTS) $(PCL_SIM_CPP_OBJECTS) $(PCL_SIM_C_OBJECTS) 200 | $(VERBOSE)rm -rf $(SIM_DIR) 201 | -------------------------------------------------------------------------------- /Sources/pcl_entry.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-2017 Intel Corporation. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in 12 | * the documentation and/or other materials provided with the 13 | * distribution. 14 | * * Neither the name of Intel Corporation nor the names of its 15 | * contributors may be used to endorse or promote products derived 16 | * from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | * 30 | */ 31 | 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | 38 | /* 39 | * g_tbl holds the PCL table. Its content is set by enclave encryption tool at build time 40 | * It is located in its own section (PCLTBL_SECTION_NAME) so that 41 | * enclave encryption tool can find it. 42 | */ 43 | pcl_table_t g_tbl __attribute__((section(PCLTBL_SECTION_NAME))) = {PCL_PLAIN}; 44 | 45 | /* 46 | * g_pcl_imagebase is set at runtime to ELF base address. 47 | * It is used by functions pcl_is_outside_enclave and pcl_is_within_enclave 48 | */ 49 | uintptr_t g_pcl_imagebase = 0; 50 | 51 | /* 52 | * @func pcl_entry is the PCL entry point. It is called from init_enclave in 53 | * trusted runtime entry point. It extracts the decryption key from the sealed blob 54 | * and use it to decrypt the encrypted portions of the enclave binary. 55 | * @param INOUT void* elf_base, base address of enclave 56 | * @param IN void* sealed_blob, the sealed blob 57 | * @return sgx_status_t 58 | * SGX_ERROR_UNEXPECTED if 59 | * 1. Table inconsistencies: 60 | * a. PCL state in PCL table is not PCL_CIPHER 61 | * b. PCL_SEALED_BLOB_MAX_SIZE < tbl->sealed_blob_size 62 | * 2. pcl_unseal_data returns incorrect values for either guid size or key size 63 | * Respective error returned from pcl_unseal_data, pcl_sha256, pcl_gcm_decrypt or pcl_increment_iv 64 | * SGX_SUCCESS if successfull 65 | */ 66 | sgx_status_t pcl_entry(void* elf_base, void* sealed_blob) 67 | { 68 | sgx_status_t ret = SGX_SUCCESS; 69 | pcl_table_t* tbl = &g_tbl; 70 | unsigned char* iv = NULL; 71 | int memcmpret = 0; 72 | sgx_sha256_hash_t hash = {0}; 73 | sgx_aes_gcm_128bit_key_t key = {0}; 74 | 75 | // Verify PCL state: 76 | if(PCL_CIPHER != tbl->pcl_state) 77 | { 78 | return SGX_ERROR_UNEXPECTED; 79 | } 80 | tbl->pcl_state = PCL_RUNNING; 81 | 82 | // ELF base address used by pcl_is_outside_enclave and pcl_is_within_enclave 83 | g_pcl_imagebase = (uintptr_t)elf_base; 84 | 85 | // Get key from sealed blob: 86 | uint32_t guid_size = SGX_PCL_GUID_SIZE; 87 | uint32_t key_size = SGX_AESGCM_KEY_SIZE; 88 | 89 | // Verify the buffer size: 90 | if(PCL_SEALED_BLOB_SIZE != tbl->sealed_blob_size) 91 | { 92 | return SGX_ERROR_UNEXPECTED; 93 | } 94 | if(!(pcl_is_outside_enclave(sealed_blob, PCL_SEALED_BLOB_SIZE))) 95 | { 96 | return SGX_ERROR_UNEXPECTED; 97 | } 98 | // LFENCE for spectre 1 attack 99 | __builtin_ia32_lfence(); 100 | // Copy sealed blob into enclave binary. 101 | pcl_memcpy(tbl->sealed_blob, sealed_blob, PCL_SEALED_BLOB_SIZE); 102 | 103 | // Unseal the sealed blob: 104 | ret = pcl_unseal_data( 105 | (const sgx_sealed_data_t*)tbl->sealed_blob, // Sealed data 106 | tbl->pcl_guid, // AAD buffer 107 | &guid_size, // pointer to AAD buffer length 108 | key, // Resulting key 109 | &key_size); // Size of resulting key 110 | if(SGX_SUCCESS != ret) 111 | { 112 | goto Label_erase_key; 113 | } 114 | if((sizeof(tbl->pcl_guid) != guid_size) || 115 | (sizeof(key) != key_size )) 116 | { 117 | ret = SGX_ERROR_UNEXPECTED; 118 | goto Label_erase_key; 119 | } 120 | 121 | // Verify key hash matches: 122 | ret = pcl_sha256(key, SGX_AESGCM_KEY_SIZE, hash); 123 | if(SGX_SUCCESS != ret) 124 | { 125 | goto Label_erase_key; 126 | } 127 | memcmpret = pcl_consttime_memequal(hash, tbl->decryption_key_hash, sizeof(sgx_sha256_hash_t)); 128 | pcl_volatile_memset((volatile void*)hash, 0, sizeof(sgx_sha256_hash_t)); // Scrub hash 129 | if(1 != memcmpret) 130 | { 131 | ret = SGX_ERROR_PCL_SHA_MISMATCH; 132 | goto Label_erase_key; 133 | } 134 | 135 | for(uint32_t i = 0;i< tbl->num_rvas;i++) 136 | { 137 | size_t size = tbl->rvas_sizes_tags_ivs[i].size; 138 | unsigned char* ciphertext = (unsigned char *)((uint64_t)elf_base + tbl->rvas_sizes_tags_ivs[i].rva); 139 | unsigned char* plaintext = ciphertext; // decrypt in place 140 | unsigned char* tag = (unsigned char *)&(tbl->rvas_sizes_tags_ivs[i].tag); 141 | unsigned char* iv = (unsigned char *)&(tbl->rvas_sizes_tags_ivs[i].iv.val); 142 | // Verify ciphertext is inside the enclave: 143 | if(!(pcl_is_within_enclave(ciphertext, size))) 144 | { 145 | ret = SGX_ERROR_UNEXPECTED; 146 | goto Label_erase_key; 147 | } 148 | ret = pcl_gcm_decrypt(plaintext, ciphertext, size, NULL, 0, key, iv, tag); 149 | if(SGX_SUCCESS != ret) 150 | { 151 | goto Label_erase_key; 152 | } 153 | } 154 | 155 | // Return success: 156 | ret = SGX_SUCCESS; 157 | 158 | Label_erase_key: 159 | // Erase key: 160 | pcl_volatile_memset((volatile void*)key, 0, sizeof(sgx_aes_gcm_128bit_key_t)); 161 | tbl->pcl_state = PCL_DONE; 162 | return ret; 163 | } 164 | 165 | /* 166 | * @func pcl_bswap32 swaps a 32bit value endianity 167 | * @param uint32_t val, input value 168 | * @return uint32_t, val in opposite endianity 169 | */ 170 | uint32_t pcl_bswap32(uint32_t val) 171 | { 172 | uint32_t ret=(val); 173 | asm ("bswap %0":"+r"(ret)); 174 | return ret; 175 | } 176 | 177 | -------------------------------------------------------------------------------- /Sources/unseal/pcl_sgx_get_key.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-2017 Intel Corporation. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in 12 | * the documentation and/or other materials provided with the 13 | * distribution. 14 | * * Neither the name of Intel Corporation nor the names of its 15 | * contributors may be used to endorse or promote products derived 16 | * from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | * 30 | */ 31 | 32 | /* Content from sdk/selib/sgx_get_key.cpp */ 33 | 34 | #include 35 | #include 36 | #include 37 | #ifdef SE_SIM 38 | #include 39 | #endif // #ifdef SE_SIM 40 | #include 41 | #include 42 | #include 43 | 44 | PCL_COMPILE_TIME_ASSERT(KEY_ALIGN_SIZE == SGX_AESGCM_KEY_SIZE) ; 45 | PCL_COMPILE_TIME_ASSERT(sizeof(sgx_key_128bit_t) == SGX_AESGCM_KEY_SIZE) ; 46 | PCL_COMPILE_TIME_ASSERT(KEY_REQUEST_ALIGN_SIZE == KEY_REQUEST_SIZE); 47 | PCL_COMPILE_TIME_ASSERT(sizeof(sgx_key_request_t) == KEY_REQUEST_SIZE); 48 | uint8_t ip1_buf[PCL_EGETKEY_BUFFER_SIZE]; 49 | 50 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 51 | // pcl_sgx_get_key() 52 | // Intel(R) SGX PCL version of sgx_get_key. 53 | // It is requried because the Intel(R) SGX SDK is unavailable at 54 | // this point 55 | // Parameters: 56 | // [IN] key_request - pointer to key_request structure 57 | // [OUT] key - resulting key 58 | // Return Value: 59 | // SGX_SUCCESS or error codes 60 | sgx_status_t pcl_sgx_get_key(IN const sgx_key_request_t *key_request, OUT sgx_key_128bit_t *key) 61 | { 62 | sgx_status_t err = SGX_ERROR_UNEXPECTED; 63 | void *buffer = NULL; 64 | size_t size = 0, buf_ptr =0; 65 | sgx_key_request_t *tmp_key_request = NULL; 66 | sgx_key_128bit_t *tmp_key = NULL; 67 | egetkey_status_t egetkey_status = EGETKEY_SUCCESS; 68 | int i = 0; 69 | 70 | // check parameters 71 | // 72 | // key_request must be within the enclave 73 | if(!key_request || !pcl_is_within_enclave(key_request, sizeof(*key_request))) 74 | { 75 | err = SGX_ERROR_INVALID_PARAMETER; 76 | goto CLEANUP; 77 | } 78 | 79 | if (key_request->reserved1 != 0) 80 | { 81 | err = SGX_ERROR_INVALID_PARAMETER; 82 | goto CLEANUP; 83 | } 84 | 85 | for (i=0; ireserved2[i] != 0) 88 | { 89 | err = SGX_ERROR_INVALID_PARAMETER; 90 | goto CLEANUP; 91 | } 92 | } 93 | 94 | // key must be within the enclave 95 | if(!key || !pcl_is_within_enclave(key, sizeof(*key))) 96 | { 97 | err = SGX_ERROR_INVALID_PARAMETER; 98 | goto CLEANUP; 99 | } 100 | // check key_request->key_policy reserved bits 101 | if(key_request->key_policy & ~(SGX_KEYPOLICY_MRENCLAVE | SGX_KEYPOLICY_MRSIGNER)) 102 | { 103 | err = SGX_ERROR_INVALID_PARAMETER; 104 | goto CLEANUP; 105 | } 106 | 107 | // allocate memory 108 | // 109 | // To minimize the effort of memory management, the two elements allocation 110 | // are combined in a single malloc. The calculation for the required size has 111 | // an assumption, that 112 | // the elements should be allocated in descending order of the alignment size. 113 | // 114 | // If the alignment requirements are changed, the allocation order needs to 115 | // change accordingly. 116 | // 117 | // Current allocation order is: 118 | // key_request -> key 119 | // 120 | // key_request: 512-byte aligned, 512-byte length 121 | // key: 16-byte aligned, 16-byte length 122 | size = ROUND_TO(sizeof(*key_request), KEY_REQUEST_ALIGN_SIZE) + ROUND_TO(sizeof(*key), KEY_ALIGN_SIZE); 123 | size += MAX(KEY_REQUEST_ALIGN_SIZE, KEY_ALIGN_SIZE) - 1; 124 | 125 | // There is no heap during Intel(R) SGX PCL execution. 126 | // Must use global memory 127 | buffer = &ip1_buf[0]; 128 | pcl_memset(buffer, 0, size); 129 | buf_ptr = reinterpret_cast(buffer); 130 | 131 | buf_ptr = ROUND_TO(buf_ptr, KEY_REQUEST_ALIGN_SIZE); 132 | tmp_key_request = reinterpret_cast(buf_ptr); 133 | buf_ptr += sizeof(*tmp_key_request); 134 | 135 | buf_ptr = ROUND_TO(buf_ptr, KEY_ALIGN_SIZE); 136 | tmp_key = reinterpret_cast(buf_ptr); 137 | 138 | // Copy data from user buffer to the aligned memory 139 | pcl_memcpy(tmp_key_request, (void*)key_request, sizeof(*tmp_key_request)); 140 | 141 | // Do EGETKEY 142 | #ifndef SE_SIM 143 | egetkey_status = (egetkey_status_t) do_egetkey(tmp_key_request, tmp_key); 144 | #else 145 | egetkey_status = (egetkey_status_t)pcl_egetkey(tmp_key_request,(uint8_t *)tmp_key); 146 | #endif 147 | switch(egetkey_status) 148 | { 149 | case EGETKEY_SUCCESS: 150 | err = SGX_SUCCESS; 151 | break; 152 | case EGETKEY_INVALID_ATTRIBUTE: 153 | err = SGX_ERROR_INVALID_ATTRIBUTE; 154 | break; 155 | case EGETKEY_INVALID_CPUSVN: 156 | err = SGX_ERROR_INVALID_CPUSVN; 157 | break; 158 | case EGETKEY_INVALID_ISVSVN: 159 | err = SGX_ERROR_INVALID_ISVSVN; 160 | break; 161 | case EGETKEY_INVALID_KEYNAME: 162 | err = SGX_ERROR_INVALID_KEYNAME; 163 | break; 164 | default: 165 | err = SGX_ERROR_UNEXPECTED; 166 | break; 167 | } 168 | 169 | 170 | CLEANUP: 171 | /* 172 | * - If success, copy resulting key into key buffer. 173 | * - In case of failure, leave the key buffer untouched becuase 174 | * Intel(R) SGX PCL does not call sgx_read_rand. 175 | */ 176 | if((SGX_SUCCESS == err) && (NULL != key)) 177 | { 178 | // Copy data to the user buffer 179 | pcl_memcpy(key, tmp_key, sizeof(*key)); 180 | } 181 | 182 | // cleanup 183 | if(buffer) 184 | { 185 | pcl_volatile_memset((volatile void*)buffer, 0, size); 186 | } 187 | 188 | return err; 189 | } 190 | -------------------------------------------------------------------------------- /Sources/crypto/pcl_crypto.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-2017 Intel Corporation. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in 12 | * the documentation and/or other materials provided with the 13 | * distribution. 14 | * * Neither the name of Intel Corporation nor the names of its 15 | * contributors may be used to endorse or promote products derived 16 | * from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | * 30 | */ 31 | 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | 41 | /* 42 | * @func pcl_gcm_decrypt applies AES-GCM-128 43 | * @param OUT uint8_t* plaintext, input plain text buffer 44 | * @param IN uint8_t* ciphertext, output cipher text buffer 45 | * @param size_t textlen, size of buffer in bytes 46 | * @param IN uint8_t* aad, aditional authenticated data 47 | * @param size_t aad_len, size of aditional authenticated data 48 | * @param IN uint8_t* key, 16 bytes decryption key 49 | * @param IN uint8_t* iv, 12 bytes IV 50 | * @param IN uint8_t* tag, 16 bytes TAG result 51 | * @return sgx_status_t 52 | * SGX_ERROR_INVALID_PARAMETER if any pointer is NULL except for aad 53 | * SGX_ERROR_UNEXPECTED if any of the following functions fail: 54 | * pcl_vpaes_set_encrypt_key, pcl_CRYPTO_gcm128_aad or pcl_CRYPTO_gcm128_decrypt 55 | * SGX_ERROR_PCL_MAC_MISMATCH if MAC mismatch when calling pcl_CRYPTO_gcm128_finish 56 | * SGX_SUCCESS if successfull 57 | */ 58 | sgx_status_t pcl_gcm_decrypt( 59 | OUT uint8_t* plaintext, 60 | IN uint8_t* ciphertext, 61 | size_t textlen, 62 | IN uint8_t* aad, 63 | size_t aad_len, 64 | IN uint8_t* key, 65 | IN uint8_t* iv, 66 | IN uint8_t* tag) 67 | { 68 | sgx_status_t ret_status = SGX_ERROR_INVALID_PARAMETER; 69 | 70 | if( NULL == plaintext || 71 | NULL == ciphertext || 72 | NULL == key || 73 | NULL == iv || 74 | NULL == tag) 75 | { 76 | return SGX_ERROR_INVALID_PARAMETER; 77 | } 78 | 79 | AES_KEY wide_key = { 0 }; 80 | GCM128_CONTEXT gcm_ctx; 81 | 82 | int ret = pcl_vpaes_set_encrypt_key(key, PCL_AES_BLOCK_LEN_BITS, &wide_key); 83 | if(0 != ret) 84 | { 85 | ret_status = SGX_ERROR_UNEXPECTED; 86 | goto Label_zero_wide_key; 87 | } 88 | 89 | pcl_CRYPTO_gcm128_init(&gcm_ctx, &wide_key, (block128_f)pcl_vpaes_encrypt); 90 | 91 | pcl_CRYPTO_gcm128_setiv(&gcm_ctx, iv, SGX_AESGCM_IV_SIZE); 92 | 93 | if(NULL != aad) 94 | { 95 | ret = pcl_CRYPTO_gcm128_aad(&gcm_ctx, aad, aad_len); 96 | if(0 != ret) 97 | { 98 | ret_status = SGX_ERROR_UNEXPECTED; 99 | goto Label_zero_buffers; 100 | } 101 | } 102 | 103 | ret = pcl_CRYPTO_gcm128_decrypt( 104 | &gcm_ctx, 105 | ciphertext, 106 | plaintext, 107 | textlen); 108 | if(0 != ret) 109 | { 110 | ret_status = SGX_ERROR_UNEXPECTED; 111 | goto Label_zero_buffers; 112 | } 113 | 114 | ret = pcl_CRYPTO_gcm128_finish(&gcm_ctx, tag, SGX_CMAC_MAC_SIZE); 115 | if(0 != ret) 116 | { 117 | ret_status = SGX_ERROR_PCL_MAC_MISMATCH; 118 | goto Label_zero_buffers; 119 | } 120 | 121 | ret_status = SGX_SUCCESS; 122 | 123 | // Scrab secrets from stack: 124 | Label_zero_buffers: 125 | pcl_volatile_memset((volatile void*)(&gcm_ctx), 0, sizeof(gcm_ctx)); 126 | Label_zero_wide_key: 127 | pcl_volatile_memset((volatile void*)(&wide_key), 0, sizeof(wide_key)); 128 | 129 | return ret_status; 130 | } 131 | 132 | /* 133 | * @func pcl_sha256 calculates the payload SHA256 134 | * @param IN uint8_t* buf, payload buffer 135 | * @param size_t buflen, buffer size in bytes 136 | * @param OUT uint8_t* hash, SHA256 result 137 | */ 138 | sgx_status_t pcl_sha256(IN uint8_t* buf, size_t buflen, OUT uint8_t* hash) 139 | { 140 | if(NULL == buf || NULL == hash) 141 | { 142 | return SGX_ERROR_INVALID_PARAMETER; 143 | } 144 | 145 | SHA256_CTX sha256; 146 | 147 | pcl_SHA256_Init(&sha256); 148 | 149 | pcl_SHA256_Update(&sha256, buf, buflen); 150 | 151 | pcl_SHA256_Final(hash, &sha256); 152 | 153 | pcl_volatile_memset((volatile void*)(&sha256), 0, sizeof(SHA256_CTX)); 154 | 155 | return SGX_SUCCESS; 156 | } 157 | 158 | #ifdef SE_SIM 159 | 160 | /* 161 | * @func pcl_cmac calcualtes CMAC-128 on payload 162 | * @param IN const sgx_cmac_128bit_key_t *p_key, CMAC key 163 | * @param IN const uint8_t *p_src, input buffer 164 | * @param uint32_t src_len, buffer size in bytes 165 | * @param OUT sgx_cmac_128bit_tag_t *p_mac, 16 bytes resulting MAC 166 | * @return int, -1 if p_key, p_src or p_mac are NULL, 0 if success 167 | */ 168 | int pcl_cmac( 169 | const sgx_cmac_128bit_key_t *p_key, 170 | const uint8_t *p_src, 171 | uint32_t src_len, 172 | sgx_cmac_128bit_tag_t *p_mac) 173 | { 174 | if(NULL == p_key || NULL == p_src || NULL == p_mac) 175 | { 176 | return -1; 177 | } 178 | unsigned char iv[PCL_COUNTER_SIZE] = { 0 }; 179 | unsigned char aux[PCL_AES_BLOCK_LEN] = { 0 }; 180 | unsigned char k1[PCL_AES_BLOCK_LEN] = { 0 }; 181 | 182 | AES_KEY wide_key; 183 | pcl_vpaes_set_encrypt_key((const unsigned char *)p_key, PCL_AES_BLOCK_LEN_BITS, &wide_key); 184 | 185 | // Apply AES-CBC encrypt on input = 0^16 and IV = 0^16: 186 | pcl_vpaes_cbc_encrypt(iv, aux, PCL_AES_BLOCK_LEN, &wide_key, iv, 1); 187 | 188 | // Use result to generate K1: 189 | make_kn(k1, aux, PCL_AES_BLOCK_LEN); 190 | 191 | // Digest message except for last block: 192 | pcl_memset(iv, 0, PCL_COUNTER_SIZE); 193 | while(src_len > PCL_AES_BLOCK_LEN) 194 | { 195 | pcl_vpaes_cbc_encrypt((uint8_t *)p_src, aux, PCL_AES_BLOCK_LEN, &wide_key, iv, 1); 196 | src_len -= PCL_AES_BLOCK_LEN; 197 | p_src += PCL_AES_BLOCK_LEN; 198 | } 199 | 200 | 201 | // XOR K1 with last block of message: 202 | for (int i = 0; i < PCL_AES_BLOCK_LEN; i++)aux[i] = p_src[i] ^ k1[i]; 203 | 204 | // Apply AES-CBC encrypt on result and IV 205 | pcl_vpaes_cbc_encrypt(aux, (uint8_t*)p_mac, PCL_AES_BLOCK_LEN, &wide_key, iv, 1); 206 | return 0; 207 | } 208 | 209 | #endif // #ifdef SE_SIM 210 | 211 | -------------------------------------------------------------------------------- /SampleCode/SampleEnclave/Enclave/Edger8rSyntax/Pointers.edl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-2017 Intel Corporation. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in 12 | * the documentation and/or other materials provided with the 13 | * distribution. 14 | * * Neither the name of Intel Corporation nor the names of its 15 | * contributors may be used to endorse or promote products derived 16 | * from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | * 30 | */ 31 | 32 | 33 | /* Pointers.edl - Samples for pointer attributes. */ 34 | 35 | enclave { 36 | 37 | /* 38 | * Following keywords/attributes are supported for pointers in Edger8r: 39 | * in, out, user_check, 40 | * string, wstring, 41 | * const, size, count, sizefunc, isptr, readonly 42 | */ 43 | 44 | trusted { 45 | 46 | /* 47 | * [user_check]: 48 | * the pointer won't be validated, and the buffer pointed by 49 | * 'val' is not copied into the enclave either. But Enclave 50 | * can modify the memory pointed by 'val'. 51 | */ 52 | 53 | public size_t ecall_pointer_user_check([user_check] void *val, size_t sz); 54 | 55 | /* 56 | * [in]: 57 | * buffer with the same size will be allocated inside the enclave, 58 | * content pointed by 'val' will be copied into the new allocated 59 | * memory inside. Any changes performed inside the enclave will not 60 | * affect the buffer outside. 61 | */ 62 | 63 | public void ecall_pointer_in([in] int *val); 64 | 65 | /* 66 | * [out]: 67 | * buffer with the same size will be allocated inside the enclave, 68 | * but the content pointed by 'val' won't be copied. But after return, 69 | * the buffer inside the enclave will copied into outside 'val'. 70 | */ 71 | 72 | public void ecall_pointer_out([out] int *val); 73 | 74 | /* 75 | * [in, out]: 76 | * buffer with the same size will be allocated inside the enclave, 77 | * the content pointed by 'val' will be copied either. After return, 78 | * the buffer inside the enclave will by copied into outside 'val' again. 79 | */ 80 | 81 | public void ecall_pointer_in_out([in, out] int *val); 82 | 83 | /* 84 | * [string]: 85 | * the attribute tells Edger8r 'str' is NULL terminated string, so strlen 86 | * will be used to count the length of buffer pointed by 'str'. 87 | */ 88 | 89 | public void ecall_pointer_string([in, out, string] char *str); 90 | 91 | /* 92 | * [const]: 93 | * the attribute tells Edger8r the buffer pointed by 'str' cannot be modified, 94 | * so users cannot decorate 'str' with [out] attribute anymore. 95 | */ 96 | 97 | public void ecall_pointer_string_const([in, string] const char *str); 98 | 99 | /* 100 | * [size]: 101 | * the attribute tells Edger8r the length of buffer in byte pointed by 'ptr' 102 | * (shall be copied or not). 103 | * Note: Users shall not specify [size] on [string] parameters. 104 | */ 105 | 106 | public void ecall_pointer_size([in, out, size=len] void *ptr, size_t len); 107 | 108 | /* 109 | * [count]: 110 | * the attribute tells Edger8r the number of integers to be copied from 'arr'. 111 | */ 112 | 113 | public void ecall_pointer_count([in, out, count=cnt] int *arr, int cnt); 114 | 115 | /* 116 | * [isptr]: 117 | * tells Edger8r the user defined type is a pointer; 118 | * [readonly]: 119 | * forbids the buffer allocated inside the enclave to be copied back to App 120 | * (cannot use with [out]). 121 | */ 122 | 123 | public void ecall_pointer_isptr_readonly([in, isptr, readonly, size=len] buffer_t buf, size_t len); 124 | 125 | /* 126 | * [sizefunc]: 127 | * call a function to decide the size/length of the parameter; 128 | * Note: 129 | * User need to define and implement `get_buf_len' as: 130 | * size_t get_buf_len(const char* buf); 131 | */ 132 | 133 | public void ecall_pointer_sizefunc([sizefunc = get_buffer_len, in, out] char *buf); 134 | 135 | }; 136 | 137 | /* 138 | * Users can define multiple trusted/untrusted blocks, 139 | * edger8r will merged them into one trusted/untrusted block. 140 | */ 141 | trusted { 142 | /* 143 | * Test pointer attributes in OCALLs 144 | */ 145 | 146 | public void ocall_pointer_attr(void); 147 | 148 | }; 149 | 150 | untrusted { 151 | 152 | /* 153 | * [user_check]: 154 | * the pointer won't be valified, and the buffer pointed by 'val' is not 155 | * copied to outside buffer either. Besides 'App' cannot modify the memory 156 | * pointer by 'val'. 157 | */ 158 | 159 | void ocall_pointer_user_check([user_check] int *val); 160 | 161 | /* 162 | * [in]: 163 | * buffer with the same size will be allocated in 'App' side, the content 164 | * pointed by 'val' will be copied into the new allocated memory outside. 165 | * Any changes performed by 'App' will not affect the buffer pointed by 'val'. 166 | */ 167 | 168 | void ocall_pointer_in([in] int *val); 169 | 170 | /* 171 | * [out]: 172 | * buffer with the same size will be allocated in 'App' side, the content 173 | * pointed by 'val' won't be copied. But after return, the buffer outside 174 | * will be copied into the enclave. 175 | */ 176 | 177 | void ocall_pointer_out([out] int *val); 178 | 179 | /* 180 | * [in, out]: 181 | * buffer with the same size will be allocated in 'App' side, the content 182 | * pointed by 'val' will be copied either. After return, the buffer outside 183 | * will copied into the enclave. 184 | */ 185 | 186 | void ocall_pointer_in_out([in, out] int *val); 187 | 188 | }; 189 | 190 | }; 191 | -------------------------------------------------------------------------------- /Sources/crypto/pcl_md32_common.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-2017 Intel Corporation. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in 12 | * the documentation and/or other materials provided with the 13 | * distribution. 14 | * * Neither the name of Intel Corporation nor the names of its 15 | * contributors may be used to endorse or promote products derived 16 | * from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | * 30 | */ 31 | 32 | /* ==================================================================== 33 | * Copyright (c) 1998-2017 The OpenSSL Project. All rights reserved. 34 | * 35 | * Redistribution and use in source and binary forms, with or without 36 | * modification, are permitted provided that the following conditions 37 | * are met: 38 | * 39 | * 1. Redistributions of source code must retain the above copyright 40 | * notice, this list of conditions and the following disclaimer. 41 | * 42 | * 2. Redistributions in binary form must reproduce the above copyright 43 | * notice, this list of conditions and the following disclaimer in 44 | * the documentation and/or other materials provided with the 45 | * distribution. 46 | * 47 | * 3. All advertising materials mentioning features or use of this 48 | * software must display the following acknowledgment: 49 | * "This product includes software developed by the OpenSSL Project 50 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 51 | * 52 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 53 | * endorse or promote products derived from this software without 54 | * prior written permission. For written permission, please contact 55 | * openssl-core@openssl.org. 56 | * 57 | * 5. Products derived from this software may not be called "OpenSSL" 58 | * nor may "OpenSSL" appear in their names without prior written 59 | * permission of the OpenSSL Project. 60 | * 61 | * 6. Redistributions of any form whatsoever must retain the following 62 | * acknowledgment: 63 | * "This product includes software developed by the OpenSSL Project 64 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 65 | * 66 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 67 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 68 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 69 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 70 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 71 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 72 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 73 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 74 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 75 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 76 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 77 | * OF THE POSSIBILITY OF SUCH DAMAGE. 78 | * ==================================================================== 79 | * 80 | * This product includes cryptographic software written by Eric Young 81 | * (eay@cryptsoft.com). This product includes software written by Tim 82 | * Hudson (tjh@cryptsoft.com). 83 | * 84 | */ 85 | 86 | /* Content from openssl-1.1.0e/crypto/include/internal/md32_common.h */ 87 | 88 | #ifndef SGX_PCL_MD32_COMMON_H 89 | #define SGX_PCL_MD32_COMMON_H 90 | 91 | # define ROTATE(a,n) ({ register unsigned int ret; \ 92 | asm ( \ 93 | "roll %1,%0" \ 94 | : "=r"(ret) \ 95 | : "I"(n), "0"((unsigned int)(a)) \ 96 | : "cc"); \ 97 | ret; \ 98 | }) 99 | 100 | # define HOST_c2l(c,l) ({ unsigned int r=*((const unsigned int *)(c)); \ 101 | asm ("bswapl %0":"=r"(r):"0"(r)); \ 102 | (c)+=4; (l)=r; }) 103 | # define HOST_l2c(l,c) ({ unsigned int r=(l); \ 104 | asm ("bswapl %0":"=r"(r):"0"(r)); \ 105 | *((unsigned int *)(c))=r; (c)+=4; r; }) 106 | 107 | int pcl_SHA256_Update(SHA256_CTX *c, void *data_, size_t len) 108 | { 109 | unsigned char *data = (unsigned char *)data_; 110 | unsigned char *p; 111 | SHA_LONG l; 112 | size_t n; 113 | 114 | if (len == 0) 115 | return 1; 116 | 117 | l = (c->Nl + (((SHA_LONG) len) << 3)) & 0xffffffffUL; 118 | /* 119 | * 95-05-24 eay Fixed a bug with the overflow handling, thanks to Wei Dai 120 | * for pointing it out. 121 | */ 122 | if (l < c->Nl) /* overflow */ 123 | c->Nh++; 124 | c->Nh += (SHA_LONG) (len >> 29); /* might cause compiler warning on 125 | * 16-bit */ 126 | c->Nl = l; 127 | /* PCL UNUSED START * 128 | n = c->num; 129 | if (n != 0) { 130 | p = (unsigned char *)c->data; 131 | 132 | if (len >= SHA_CBLOCK || len + n >= SHA_CBLOCK) { 133 | pcl_memcpy(p + n, data, SHA_CBLOCK - n); 134 | pcl_sha256_block_data_order(c, p, 1); 135 | n = SHA_CBLOCK - n; 136 | data += n; 137 | len -= n; 138 | c->num = 0; 139 | /* 140 | * We use memset rather than OPENSSL_cleanse() here deliberately. 141 | * Using OPENSSL_cleanse() here could be a performance issue. It 142 | * will get properly cleansed on finalisation so this isn't a 143 | * security problem. 144 | * / 145 | pcl_memset(p, 0, SHA_CBLOCK); /* keep it zeroed * / 146 | } else { 147 | pcl_memcpy(p + n, data, len); 148 | c->num += (unsigned int)len; 149 | return 1; 150 | } 151 | } 152 | /* PCL UNUSED END */ 153 | n = len / SHA_CBLOCK; 154 | if (n > 0) { 155 | pcl_sha256_block_data_order(c, data, n); 156 | n *= SHA_CBLOCK; 157 | data += n; 158 | len -= n; 159 | } 160 | 161 | if (len != 0) { 162 | p = (unsigned char *)c->data; 163 | c->num = (unsigned int)len; 164 | pcl_memcpy(p, data, len); 165 | } 166 | return 1; 167 | } 168 | 169 | int pcl_SHA256_Final(unsigned char *md, SHA256_CTX *c) 170 | { 171 | unsigned char *p = (unsigned char *)c->data; 172 | size_t n = c->num; 173 | 174 | p[n] = 0x80; /* there is always room for one */ 175 | n++; 176 | 177 | if (n > (SHA_CBLOCK - 8)) { 178 | pcl_memset(p + n, 0, SHA_CBLOCK - n); 179 | n = 0; 180 | pcl_sha256_block_data_order(c, p, 1); 181 | } 182 | pcl_memset(p + n, 0, SHA_CBLOCK - 8 - n); 183 | 184 | p += SHA_CBLOCK - 8; 185 | /* PCL UNUSED START * 186 | #if defined(DATA_ORDER_IS_BIG_ENDIAN) 187 | /* PCL UNUSED END */ 188 | (void)HOST_l2c(c->Nh, p); 189 | (void)HOST_l2c(c->Nl, p); 190 | /* PCL UNUSED START * 191 | #elif defined(DATA_ORDER_IS_LITTLE_ENDIAN) 192 | (void)HOST_l2c(c->Nl, p); 193 | (void)HOST_l2c(c->Nh, p); 194 | #endif 195 | /* PCL UNUSED END */ 196 | p -= SHA_CBLOCK; 197 | pcl_sha256_block_data_order(c, p, 1); 198 | c->num = 0; 199 | pcl_memset(p, 0, SHA_CBLOCK); 200 | /* PCL UNUSED START * 201 | #ifndef HASH_MAKE_STRING 202 | # error "HASH_MAKE_STRING must be defined!" 203 | #else 204 | /* PCL UNUSED END */ 205 | HASH_MAKE_STRING(c, md); 206 | /* PCL UNUSED START * 207 | #endif 208 | /* PCL UNUSED END */ 209 | 210 | return 1; 211 | } 212 | 213 | #define MD32_REG_T int 214 | 215 | #endif // #ifndef SGX_PCL_MD32_COMMON_H 216 | 217 | -------------------------------------------------------------------------------- /Sources/unseal/sim/pcl_t_instructions.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-2017 Intel Corporation. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in 12 | * the documentation and/or other materials provided with the 13 | * distribution. 14 | * * Neither the name of Intel Corporation nor the names of its 15 | * contributors may be used to endorse or promote products derived 16 | * from this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | * 30 | */ 31 | 32 | /* Content from sdk/simulation/tinst/t_instructions.cpp */ 33 | 34 | #include "t_instructions.h" /* for `g_global_data_sim' */ 35 | #include "deriv.h" 36 | #include 37 | #include "pcl_common.h" 38 | #include "pcl_internal.h" 39 | #include "pcl_unseal_internal.h" 40 | 41 | //////////////////////////////////////////////////////////////////////// 42 | // Simulation for EGETKEY 43 | //////////////////////////////////////////////////////////////////////// 44 | 45 | // The hard-coded OwnerEpoch. 46 | static const se_owner_epoch_t SIMU_OWNER_EPOCH_MSR = { 47 | 0x54, 0x48, 0x49, 0x53, 0x49, 0x53, 0x4f, 0x57, 48 | 0x4e, 0x45, 0x52, 0x45, 0x50, 0x4f, 0x43, 0x48, 49 | }; 50 | 51 | static const sgx_cpu_svn_t PCL_DEFAULT_CPUSVN = { 52 | { 53 | 0x48, 0x20, 0xf3, 0x37, 0x6a, 0xe6, 0xb2, 0xf2, 54 | 0x03, 0x4d, 0x3b, 0x7a, 0x4b, 0x48, 0xa7, 0x78 55 | } 56 | }; 57 | 58 | static const sgx_cpu_svn_t PCL_UPGRADED_CPUSVN = { 59 | { 60 | 0x53, 0x39, 0xae, 0x8c, 0x93, 0xae, 0x8f, 0x3c, 61 | 0xe4, 0x32, 0xdb, 0x92, 0x4d, 0x0f, 0x07, 0x33 62 | } 63 | }; 64 | 65 | static const sgx_cpu_svn_t PCL_DOWNGRADED_CPUSVN = { 66 | { 67 | 0x64, 0xea, 0x4f, 0x3f, 0xa0, 0x03, 0x0c, 0x36, 68 | 0x38, 0x3c, 0x32, 0x2d, 0x4f, 0x3a, 0x8d, 0x4f 69 | } 70 | }; 71 | 72 | egetkey_status_t pcl_check_cpu_svn(sgx_key_request_t* kr) 73 | { 74 | if(!pcl_consttime_memequal(&kr->cpu_svn, (void*)&PCL_UPGRADED_CPUSVN, sizeof(PCL_UPGRADED_CPUSVN)) && 75 | !pcl_consttime_memequal(&kr->cpu_svn, (void*)&PCL_DEFAULT_CPUSVN, sizeof(PCL_DEFAULT_CPUSVN)) && 76 | !pcl_consttime_memequal(&kr->cpu_svn, (void*)&PCL_DOWNGRADED_CPUSVN, sizeof(PCL_DOWNGRADED_CPUSVN))){ 77 | return EGETKEY_INVALID_CPUSVN; 78 | } 79 | if ( (pcl_consttime_memequal(&g_global_data_sim.cpusvn_sim, (void*)&PCL_DEFAULT_CPUSVN, sizeof(PCL_DEFAULT_CPUSVN)) && 80 | pcl_consttime_memequal(&kr->cpu_svn, (void*)&PCL_UPGRADED_CPUSVN, sizeof(PCL_UPGRADED_CPUSVN))) || 81 | (pcl_consttime_memequal(&g_global_data_sim.cpusvn_sim, (void*)&PCL_DOWNGRADED_CPUSVN, sizeof(PCL_DOWNGRADED_CPUSVN)) && 82 | !pcl_consttime_memequal(&kr->cpu_svn, (void*)&PCL_DOWNGRADED_CPUSVN, sizeof(PCL_DOWNGRADED_CPUSVN)))){ 83 | return EGETKEY_INVALID_CPUSVN; 84 | } 85 | return EGETKEY_SUCCESS; 86 | } 87 | 88 | 89 | #define check_attr_flag(secs, flag) do { \ 90 | if ((secs->attributes.flags & flag) == 0) { \ 91 | return EGETKEY_INVALID_ATTRIBUTE; \ 92 | } \ 93 | } while(0) 94 | 95 | 96 | // The hardware EGETKEY instruction will set ZF on failure. 97 | // 98 | // In simulation mode, we can not guarentee that the ZF is always set 99 | // between _EGETKEY ending its life and tRTS testing ZF. Since there 100 | // are additional assembly code in between. 101 | // 102 | // In simulation mode, we check return code instead of ZF. 103 | // c.f. do_egetkey() in trts/linux/trts_pic.S 104 | # define GP() return ((egetkey_status_t)-1) 105 | #define GP_ON(cond) do { if (unlikely(cond)) GP(); } while (0) 106 | 107 | egetkey_status_t pcl_check_isv_svn(sgx_key_request_t* kr, secs_t* secs) 108 | { 109 | if (kr->isv_svn > secs->isv_svn) { 110 | return EGETKEY_INVALID_ISVSVN; 111 | } 112 | return EGETKEY_SUCCESS; 113 | } 114 | 115 | egetkey_status_t pcl_egetkey(sgx_key_request_t* kr, sgx_key_128bit_t okey) 116 | { 117 | // check alignment of KEYREQUEST 118 | GP_ON(((size_t)kr & (KEY_REQUEST_ALIGN_SIZE - 1)) != 0); 119 | 120 | // check to see if KEYREQEUST is inside the current enclave 121 | ////GP_ON(!sgx_is_within_enclave(kr, sizeof(sgx_key_request_t))); 122 | 123 | // check alignment of OUTPUTDATA 124 | GP_ON(((size_t)okey & (KEY_ALIGN_SIZE - 1)) != 0); 125 | 126 | // check to see if OUTPUTDATA is inside the current enclave 127 | ////GP_ON(!sgx_is_within_enclave(okey, sizeof(sgx_key_128bit_t))); 128 | 129 | // check reserved bits are not set 130 | GP_ON((kr->key_policy & ~(SGX_KEYPOLICY_MRENCLAVE | SGX_KEYPOLICY_MRSIGNER)) != 0); 131 | 132 | // check to see if reserved space in KEYREQUEST are valid 133 | const uint8_t* u8ptr = (uint8_t *)(&(kr->reserved1)); 134 | for (unsigned i = 0; i < sizeof(kr->reserved1); ++i) 135 | GP_ON(u8ptr[i] != (uint8_t)0); 136 | 137 | u8ptr = (uint8_t *)(&(kr->reserved2)); 138 | for (unsigned i = 0; i < sizeof(kr->reserved2); ++i) 139 | GP_ON(u8ptr[i] != (uint8_t)0); 140 | 141 | secs_t* cur_secs = g_global_data_sim.secs_ptr; 142 | sgx_attributes_t tmp_attr; 143 | derivation_data_t dd; 144 | 145 | pcl_memset(&dd, 0, sizeof(dd)); 146 | dd.key_name = kr->key_name; 147 | 148 | // Determine which enclave attributes that must be included in the key. 149 | // Attributes that must always be included INIT & DEBUG. 150 | pcl_memset(&tmp_attr, 0, sizeof(tmp_attr)); 151 | tmp_attr.flags = kr->attribute_mask.flags | SGX_FLAGS_INITTED | SGX_FLAGS_DEBUG; 152 | tmp_attr.flags &= cur_secs->attributes.flags; 153 | tmp_attr.xfrm = kr->attribute_mask.xfrm & cur_secs->attributes.xfrm; 154 | // HW supports CPUSVN to be set as 0. 155 | // To be consistent with HW behaviour, we replace the cpusvn as DEFAULT_CPUSVN if the input cpusvn is 0. 156 | if(pcl_consttime_memequal(&kr->cpu_svn, &dd.ddpk.cpu_svn, sizeof(sgx_cpu_svn_t))) 157 | { 158 | pcl_memcpy(&kr->cpu_svn, (void*)&DEFAULT_CPUSVN, sizeof(sgx_cpu_svn_t)); 159 | } 160 | 161 | // Must not use swich else relocations 162 | /* PCL UNUSED START * 163 | switch (kr->key_name) { 164 | case SGX_KEYSELECT_SEAL: 165 | /* PCL UNUSED END */ 166 | egetkey_status_t esa = pcl_check_isv_svn(kr, cur_secs); 167 | if(EGETKEY_SUCCESS != esa)return esa; 168 | esa = pcl_check_cpu_svn(kr); 169 | if(EGETKEY_SUCCESS != esa)return esa; 170 | // assemble derivation data 171 | dd.size = sizeof(dd_seal_key_t); 172 | if (kr->key_policy & SGX_KEYPOLICY_MRENCLAVE) { 173 | pcl_memcpy(&dd.ddsk.mrenclave, &cur_secs->mr_enclave, sizeof(sgx_measurement_t)); 174 | } 175 | 176 | if (kr->key_policy & SGX_KEYPOLICY_MRSIGNER) { 177 | pcl_memcpy(&dd.ddsk.mrsigner, (void*)&cur_secs->mr_signer, sizeof(sgx_measurement_t)); 178 | } 179 | 180 | pcl_memcpy(&dd.ddsk.tmp_attr, &tmp_attr, sizeof(sgx_attributes_t)); 181 | pcl_memcpy(&dd.ddsk.attribute_mask, &kr->attribute_mask, sizeof(sgx_attributes_t)); 182 | pcl_memcpy(dd.ddsk.csr_owner_epoch, (void*)SIMU_OWNER_EPOCH_MSR, sizeof(se_owner_epoch_t)); 183 | pcl_memcpy(&dd.ddsk.cpu_svn,&kr->cpu_svn,sizeof(sgx_cpu_svn_t)); 184 | dd.ddsk.isv_svn = kr->isv_svn; 185 | dd.ddsk.isv_prod_id = cur_secs->isv_prod_id; 186 | pcl_memcpy(&dd.ddsk.key_id, &kr->key_id, sizeof(sgx_key_id_t)); 187 | 188 | 189 | /* PCL UNUSED START * 190 | default: 191 | return EGETKEY_INVALID_KEYNAME; 192 | } 193 | /* PCL UNUSED END */ 194 | pcl_derive_key(&dd, okey); 195 | return EGETKEY_SUCCESS; 196 | } 197 | --------------------------------------------------------------------------------