encryptip.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (C) 2011-2018 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. #ifndef ENCRYPTIP_H
  32. #define ENCRYPTIP_H
  33. // PCL table must be alligned to PCL_TABLE_ALLIGNMENT bytes
  34. #define PCL_TABLE_ALLIGNMENT (16)
  35. #define PCL_TEXT_SECTION_NAME ".nipx"
  36. #define PCL_DATA_SECTION_NAME ".nipd"
  37. #define PCL_RODATA_SECTION_NAME ".niprod"
  38. #define NUM_NON_IP_OR_DEBUG_SEC_NAMES (23)
  39. #define NUM_NON_IP_SEC_NAMES (12)
  40. /*
  41. * When running CPUID with leaf 0, bit 30 of the output ECX
  42. * indicates if platform supports RDRAND
  43. */
  44. #define SUPPORT_RDRAND (1<<30)
  45. // GCM can encrypt up to (2^32 - 2) blocks per single IV
  46. #define PCL_GCM_MAX_NUM_BLOCKS \
  47. ((size_t)((uint32_t)(-2)))
  48. #define PCL_GCM_NUM_BLOCKS(size) \
  49. ((size + PCL_AES_BLOCK_LEN - 1)/ PCL_AES_BLOCK_LEN)
  50. #define EVP_SUCCESS (1)
  51. #define RDRAND_SUCCESS (1)
  52. #define ENCIP_ERROR(r) (ENCIP_SUCCESS != (r))
  53. typedef enum encip_ret_e_
  54. {
  55. ENCIP_SUCCESS = 0x0,
  56. ENCIP_ERROR_FAIL = 0x80,
  57. ENCIP_ERROR_INVALID_PARAM = 0x82,
  58. ENCIP_ERROR_KEY_FILE_SIZE = 0x84,
  59. ENCIP_ERROR_ENCLAVE_SIZE = 0x86,
  60. ENCIP_ERROR_MEM_ALLOC = 0x88,
  61. ENCIP_ERROR_PARSE_ARGS = 0x8a,
  62. ENCIP_ERROR_PARSE_INVALID_PARAM = 0x8c,
  63. ENCIP_ERROR_READF_INVALID_PARAM = 0x8e,
  64. ENCIP_ERROR_READF_OPEN = 0x90,
  65. ENCIP_ERROR_READF_ALLOC = 0x92,
  66. ENCIP_ERROR_READF_READ = 0x94,
  67. ENCIP_ERROR_WRITEF_INVALID_PARAM = 0x96,
  68. ENCIP_ERROR_WRITEF_OPEN = 0x98,
  69. ENCIP_ERROR_WRITEF_WRITE = 0x9a,
  70. ENCIP_ERROR_SEALED_BUF_SIZE = 0x9c,
  71. ENCIP_ERROR_GETTBL_INVALID_PARAM = 0x9e,
  72. ENCIP_ERROR_TBL_NOT_FOUND = 0xa0,
  73. ENCIP_ERROR_TBL_NOT_ALIGNED = 0xa2,
  74. ENCIP_ERROR_ALREADY_ENCRYPTED = 0xa4,
  75. ENCIP_ERROR_IMPROPER_STATE = 0xa6,
  76. ENCIP_ERROR_RANDIV_INVALID_PARAM = 0xa8,
  77. ENCIP_ERROR_RDRAND_NOT_SUPPORTED = 0xa9,
  78. ENCIP_ERROR_RDRAND_FAILED = 0xaa,
  79. ENCIP_ERROR_GCM_ENCRYPT_INVALID_PARAM = 0xac,
  80. ENCIP_ERROR_ENCRYPT_ALLOC = 0xae,
  81. ENCIP_ERROR_ENCRYPT_INIT_EX = 0xb0,
  82. ENCIP_ERROR_ENCRYPT_IV_LEN = 0xb2,
  83. ENCIP_ERROR_ENCRYPT_INIT_KEY = 0xb4,
  84. ENCIP_ERROR_ENCRYPT_AAD = 0xb6,
  85. ENCIP_ERROR_ENCRYPT_UPDATE = 0xb8,
  86. ENCIP_ERROR_ENCRYPT_FINAL = 0xba,
  87. ENCIP_ERROR_ENCRYPT_TAG = 0xbc,
  88. ENCIP_ERROR_INCIV_INVALID_PARAM = 0xbe,
  89. ENCIP_ERROR_IV_OVERFLOW = 0xc0,
  90. ENCIP_ERROR_SHA_INVALID_PARAM = 0xc2,
  91. ENCIP_ERROR_SHA_ALLOC = 0xc4,
  92. ENCIP_ERROR_SHA_INIT = 0xc6,
  93. ENCIP_ERROR_SHA_UPDATE = 0xc8,
  94. ENCIP_ERROR_SHA_FINAL = 0xca,
  95. ENCIP_ERROR_ENCSECS_INVALID_PARAM = 0xcc,
  96. ENCIP_ERROR_ENCSECS_COUNTER_OVERFLOW = 0xce,
  97. ENCIP_ERROR_ENCSECS_RVAS_OVERFLOW = 0xd0,
  98. ENCIP_ERROR_UPDATEF_INVALID_PAR = 0xd2,
  99. ENCIP_ERROR_ENCRYPTIP_INVALID_PARAM = 0xd4,
  100. ENCIP_ERROR_PARSE_ELF_INVALID_PARAM = 0xd6,
  101. ENCIP_ERROR_PARSE_ELF_INVALID_IMAGE = 0xd8,
  102. ENCIP_ERROR_SEGMENT_NOT_READABLE = 0xda,
  103. }encip_ret_e;
  104. typedef struct pcl_data_t_
  105. {
  106. Elf64_Shdr* elf_sec;
  107. Elf64_Half shstrndx;
  108. char* sections_names;
  109. Elf64_Phdr* phdr;
  110. uint16_t nsections;
  111. uint16_t nsegments;
  112. }pcl_data_t;
  113. encip_ret_e parse_args(
  114. int argc,
  115. IN char* argv[],
  116. OUT char** ifname,
  117. OUT char** ofname,
  118. OUT char** kfname,
  119. OUT bool* debug);
  120. static encip_ret_e read_file(IN const char* const name,OUT uint8_t** data,OUT size_t* size_out);
  121. static encip_ret_e write_file(const char* const name, uint8_t* data, size_t size);
  122. encip_ret_e gcm_encrypt(
  123. unsigned char *plaintext,
  124. size_t plaintext_len,
  125. unsigned char *aad,
  126. size_t aad_len,
  127. unsigned char *key,
  128. unsigned char *iv,
  129. unsigned char *ciphertext,
  130. unsigned char *tag);
  131. encip_ret_e sha256(const void* const buf, size_t buflen, uint8_t* hash);
  132. encip_ret_e encrypt_ip(uint8_t* elf_buf, size_t elf_size, uint8_t* key, bool debug);
  133. encip_ret_e write_tbl_strs(pcl_table_t* tbl);
  134. #endif // #ifndef ENCRYPTIP_H