15acaca2fSWilliam A. Kennington III /* 25acaca2fSWilliam A. Kennington III * Copyright 2021 Google LLC 35acaca2fSWilliam A. Kennington III * 45acaca2fSWilliam A. Kennington III * Licensed under the Apache License, Version 2.0 (the "License"); 55acaca2fSWilliam A. Kennington III * you may not use this file except in compliance with the License. 65acaca2fSWilliam A. Kennington III * You may obtain a copy of the License at 75acaca2fSWilliam A. Kennington III * 85acaca2fSWilliam A. Kennington III * http://www.apache.org/licenses/LICENSE-2.0 95acaca2fSWilliam A. Kennington III * 105acaca2fSWilliam A. Kennington III * Unless required by applicable law or agreed to in writing, software 115acaca2fSWilliam A. Kennington III * distributed under the License is distributed on an "AS IS" BASIS, 125acaca2fSWilliam A. Kennington III * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 135acaca2fSWilliam A. Kennington III * See the License for the specific language governing permissions and 145acaca2fSWilliam A. Kennington III * limitations under the License. 155acaca2fSWilliam A. Kennington III */ 165acaca2fSWilliam A. Kennington III #include <assert.h> 175acaca2fSWilliam A. Kennington III #include <libcr51sign/libcr51sign.h> 185acaca2fSWilliam A. Kennington III #include <stdio.h> 195acaca2fSWilliam A. Kennington III #include <stdlib.h> 205acaca2fSWilliam A. Kennington III #include <string.h> 215acaca2fSWilliam A. Kennington III 225acaca2fSWilliam A. Kennington III #ifdef __cplusplus 235acaca2fSWilliam A. Kennington III extern "C" 245acaca2fSWilliam A. Kennington III { 255acaca2fSWilliam A. Kennington III #endif 265acaca2fSWilliam A. Kennington III 275acaca2fSWilliam A. Kennington III #ifndef USER_PRINT 28deb5501aSWilliam A. Kennington III #define CPRINTS(ctx, ...) fprintf(stderr, __VA_ARGS__) 295acaca2fSWilliam A. Kennington III #endif 305acaca2fSWilliam A. Kennington III 315acaca2fSWilliam A. Kennington III #define MEMBER_SIZE(type, field) sizeof(((type*)0)->field) 325acaca2fSWilliam A. Kennington III 335acaca2fSWilliam A. Kennington III // True of x is a power of two 345acaca2fSWilliam A. Kennington III #define POWER_OF_TWO(x) ((x) && !((x) & ((x)-1))) 355acaca2fSWilliam A. Kennington III 365acaca2fSWilliam A. Kennington III // Maximum version supported. Major revisions are not backwards compatible. 375acaca2fSWilliam A. Kennington III #define MAX_MAJOR_VERSION 1 385acaca2fSWilliam A. Kennington III 395acaca2fSWilliam A. Kennington III // Descriptor alignment on the external EEPROM. 405acaca2fSWilliam A. Kennington III #define DESCRIPTOR_ALIGNMENT (64 * 1024) 415acaca2fSWilliam A. Kennington III 425acaca2fSWilliam A. Kennington III // SPS EEPROM sector size is 4KiB, since this is the smallest erasable size. 435acaca2fSWilliam A. Kennington III #define IMAGE_REGION_ALIGNMENT 4096 445acaca2fSWilliam A. Kennington III 455acaca2fSWilliam A. Kennington III #define MAX_READ_SIZE 1024 465acaca2fSWilliam A. Kennington III 475acaca2fSWilliam A. Kennington III #ifndef ARRAY_SIZE 485acaca2fSWilliam A. Kennington III #define ARRAY_SIZE(t) (sizeof(t) / sizeof(t[0])) 495acaca2fSWilliam A. Kennington III #endif 505acaca2fSWilliam A. Kennington III 515acaca2fSWilliam A. Kennington III // Values of SIGNATURE_OFFSET shuold be same for all sig types (2048,3072,4096) 525acaca2fSWilliam A. Kennington III #define SIGNATURE_OFFSET offsetof(struct signature_rsa3072_pkcs15, modulus) 535acaca2fSWilliam A. Kennington III 545acaca2fSWilliam A. Kennington III #ifndef BUILD_ASSERT 555acaca2fSWilliam A. Kennington III #define BUILD_ASSERT(cond) ((void)sizeof(char[1 - 2 * !(cond)])) 565acaca2fSWilliam A. Kennington III #endif 575acaca2fSWilliam A. Kennington III 585acaca2fSWilliam A. Kennington III typedef enum libcr51sign_validation_failure_reason failure_reason; 595acaca2fSWilliam A. Kennington III 605acaca2fSWilliam A. Kennington III // Returns the bytes size of keys used in the given signature_scheme. 615acaca2fSWilliam A. Kennington III // Return error if signature_scheme is invalid. 625acaca2fSWilliam A. Kennington III // 635acaca2fSWilliam A. Kennington III static failure_reason get_key_size(enum signature_scheme signature_scheme, 645acaca2fSWilliam A. Kennington III uint16_t* key_size) 655acaca2fSWilliam A. Kennington III { 665acaca2fSWilliam A. Kennington III switch (signature_scheme) 675acaca2fSWilliam A. Kennington III { 685acaca2fSWilliam A. Kennington III case SIGNATURE_RSA2048_PKCS15: 695acaca2fSWilliam A. Kennington III *key_size = 256; 705acaca2fSWilliam A. Kennington III return LIBCR51SIGN_SUCCESS; 715acaca2fSWilliam A. Kennington III case SIGNATURE_RSA3072_PKCS15: 725acaca2fSWilliam A. Kennington III *key_size = 384; 735acaca2fSWilliam A. Kennington III return LIBCR51SIGN_SUCCESS; 745acaca2fSWilliam A. Kennington III case SIGNATURE_RSA4096_PKCS15: 755acaca2fSWilliam A. Kennington III case SIGNATURE_RSA4096_PKCS15_SHA512: 765acaca2fSWilliam A. Kennington III *key_size = 512; 775acaca2fSWilliam A. Kennington III return LIBCR51SIGN_SUCCESS; 785acaca2fSWilliam A. Kennington III default: 795acaca2fSWilliam A. Kennington III return LIBCR51SIGN_ERROR_INVALID_SIG_SCHEME; 805acaca2fSWilliam A. Kennington III } 815acaca2fSWilliam A. Kennington III } 825acaca2fSWilliam A. Kennington III 835acaca2fSWilliam A. Kennington III // Returns the hash_type for a given signature scheme 845acaca2fSWilliam A. Kennington III // Returns error if scheme is invalid. 855acaca2fSWilliam A. Kennington III failure_reason get_hash_type_from_signature(enum signature_scheme scheme, 865acaca2fSWilliam A. Kennington III enum hash_type* type) 875acaca2fSWilliam A. Kennington III { 885acaca2fSWilliam A. Kennington III switch (scheme) 895acaca2fSWilliam A. Kennington III { 905acaca2fSWilliam A. Kennington III case SIGNATURE_RSA2048_PKCS15: 915acaca2fSWilliam A. Kennington III case SIGNATURE_RSA3072_PKCS15: 925acaca2fSWilliam A. Kennington III case SIGNATURE_RSA4096_PKCS15: 935acaca2fSWilliam A. Kennington III *type = HASH_SHA2_256; 945acaca2fSWilliam A. Kennington III return LIBCR51SIGN_SUCCESS; 955acaca2fSWilliam A. Kennington III case SIGNATURE_RSA4096_PKCS15_SHA512: 965acaca2fSWilliam A. Kennington III *type = HASH_SHA2_512; 975acaca2fSWilliam A. Kennington III return LIBCR51SIGN_SUCCESS; 985acaca2fSWilliam A. Kennington III default: 995acaca2fSWilliam A. Kennington III return LIBCR51SIGN_ERROR_INVALID_SIG_SCHEME; 1005acaca2fSWilliam A. Kennington III } 1015acaca2fSWilliam A. Kennington III } 1025acaca2fSWilliam A. Kennington III 1035acaca2fSWilliam A. Kennington III // Check if the given hash_type is supported. 1045acaca2fSWilliam A. Kennington III // Returns error if hash_type is not supported. 1055acaca2fSWilliam A. Kennington III static failure_reason is_hash_type_supported(enum hash_type type) 1065acaca2fSWilliam A. Kennington III { 1075acaca2fSWilliam A. Kennington III switch (type) 1085acaca2fSWilliam A. Kennington III { 1095acaca2fSWilliam A. Kennington III case HASH_SHA2_256: 1105acaca2fSWilliam A. Kennington III case HASH_SHA2_512: 1115acaca2fSWilliam A. Kennington III return LIBCR51SIGN_SUCCESS; 1125acaca2fSWilliam A. Kennington III default: 1135acaca2fSWilliam A. Kennington III return LIBCR51SIGN_ERROR_INVALID_HASH_TYPE; 1145acaca2fSWilliam A. Kennington III } 1155acaca2fSWilliam A. Kennington III } 1165acaca2fSWilliam A. Kennington III 1175acaca2fSWilliam A. Kennington III // Determines digest size for a given hash_type. 1185acaca2fSWilliam A. Kennington III // Returns error if hash_type is not supported. 1195acaca2fSWilliam A. Kennington III static failure_reason get_hash_digest_size(enum hash_type type, 1205acaca2fSWilliam A. Kennington III uint32_t* size) 1215acaca2fSWilliam A. Kennington III { 1225acaca2fSWilliam A. Kennington III switch (type) 1235acaca2fSWilliam A. Kennington III { 1245acaca2fSWilliam A. Kennington III case HASH_SHA2_256: 1255acaca2fSWilliam A. Kennington III *size = LIBCR51SIGN_SHA256_DIGEST_SIZE; 1265acaca2fSWilliam A. Kennington III return LIBCR51SIGN_SUCCESS; 1275acaca2fSWilliam A. Kennington III case HASH_SHA2_512: 1285acaca2fSWilliam A. Kennington III *size = LIBCR51SIGN_SHA512_DIGEST_SIZE; 1295acaca2fSWilliam A. Kennington III return LIBCR51SIGN_SUCCESS; 1305acaca2fSWilliam A. Kennington III default: 1315acaca2fSWilliam A. Kennington III return LIBCR51SIGN_ERROR_INVALID_HASH_TYPE; 1325acaca2fSWilliam A. Kennington III } 1335acaca2fSWilliam A. Kennington III } 1345acaca2fSWilliam A. Kennington III 1355acaca2fSWilliam A. Kennington III // Determines hash struct size for a given hash_type. 1365acaca2fSWilliam A. Kennington III // Returns error if hash_type is not supported. 1375acaca2fSWilliam A. Kennington III static failure_reason get_hash_struct_size(enum hash_type type, 1385acaca2fSWilliam A. Kennington III uint32_t* size) 1395acaca2fSWilliam A. Kennington III { 1405acaca2fSWilliam A. Kennington III switch (type) 1415acaca2fSWilliam A. Kennington III { 1425acaca2fSWilliam A. Kennington III case HASH_SHA2_256: 1435acaca2fSWilliam A. Kennington III *size = sizeof(struct hash_sha256); 1445acaca2fSWilliam A. Kennington III return LIBCR51SIGN_SUCCESS; 1455acaca2fSWilliam A. Kennington III case HASH_SHA2_512: 1465acaca2fSWilliam A. Kennington III *size = sizeof(struct hash_sha256); 1475acaca2fSWilliam A. Kennington III return LIBCR51SIGN_SUCCESS; 1485acaca2fSWilliam A. Kennington III default: 1495acaca2fSWilliam A. Kennington III return LIBCR51SIGN_ERROR_INVALID_HASH_TYPE; 1505acaca2fSWilliam A. Kennington III } 1515acaca2fSWilliam A. Kennington III } 1525acaca2fSWilliam A. Kennington III 1535acaca2fSWilliam A. Kennington III // Checks that: 1545acaca2fSWilliam A. Kennington III // - The signing key is trusted 1555acaca2fSWilliam A. Kennington III // - The target version is not denylisted 1565acaca2fSWilliam A. Kennington III // If validating a staged update, also checks that: 1575acaca2fSWilliam A. Kennington III // - The target image family matches the current image family 1585acaca2fSWilliam A. Kennington III // - The image type transition is legal (i.e. dev -> *|| prod -> prod) or 1595acaca2fSWilliam A. Kennington III // alternatively that the hardware ID is allowlisted 1605acaca2fSWilliam A. Kennington III // Assuming the caller has performed following: 1615acaca2fSWilliam A. Kennington III // board_get_base_key_index(); 1625acaca2fSWilliam A. Kennington III // board_get_key_array 1635acaca2fSWilliam A. Kennington III // Possible return codes: 1645acaca2fSWilliam A. Kennington III // LIBCR51SIGN_SUCCESS = 0, 1655acaca2fSWilliam A. Kennington III // LIBCR51SIGN_ERROR_RUNTIME_FAILURE = 1, 1665acaca2fSWilliam A. Kennington III // LIBCR51SIGN_ERROR_INVALID_DESCRIPTOR = 3, 1675acaca2fSWilliam A. Kennington III // LIBCR51SIGN_ERROR_INVALID_IMAGE_FAMILY = 4, 1685acaca2fSWilliam A. Kennington III // LIBCR51SIGN_ERROR_IMAGE_TYPE_DISALLOWED = 5, 1695acaca2fSWilliam A. Kennington III 1705acaca2fSWilliam A. Kennington III static failure_reason 1715acaca2fSWilliam A. Kennington III validate_transition(const struct libcr51sign_ctx* ctx, 1725acaca2fSWilliam A. Kennington III const struct libcr51sign_intf* intf, 1735acaca2fSWilliam A. Kennington III uint32_t signature_struct_offset) 1745acaca2fSWilliam A. Kennington III { 1755acaca2fSWilliam A. Kennington III BUILD_ASSERT((offsetof(struct signature_rsa2048_pkcs15, modulus) == 1765acaca2fSWilliam A. Kennington III SIGNATURE_OFFSET && 1775acaca2fSWilliam A. Kennington III offsetof(struct signature_rsa3072_pkcs15, modulus) == 1785acaca2fSWilliam A. Kennington III SIGNATURE_OFFSET && 1795acaca2fSWilliam A. Kennington III offsetof(struct signature_rsa4096_pkcs15, modulus) == 1805acaca2fSWilliam A. Kennington III SIGNATURE_OFFSET)); 1815acaca2fSWilliam A. Kennington III 1825acaca2fSWilliam A. Kennington III // Read up to the modulus. 1835acaca2fSWilliam A. Kennington III enum 1845acaca2fSWilliam A. Kennington III { 1855acaca2fSWilliam A. Kennington III read_len = SIGNATURE_OFFSET 1865acaca2fSWilliam A. Kennington III }; 1875acaca2fSWilliam A. Kennington III uint8_t buffer[read_len]; 1885acaca2fSWilliam A. Kennington III int rv; 1895acaca2fSWilliam A. Kennington III rv = intf->read(ctx, signature_struct_offset, read_len, buffer); 1905acaca2fSWilliam A. Kennington III if (rv != LIBCR51SIGN_SUCCESS) 1915acaca2fSWilliam A. Kennington III { 1925acaca2fSWilliam A. Kennington III CPRINTS(ctx, 1935acaca2fSWilliam A. Kennington III "validate_transition: failed to read signature struct"); 1945acaca2fSWilliam A. Kennington III return LIBCR51SIGN_ERROR_RUNTIME_FAILURE; 1955acaca2fSWilliam A. Kennington III } 196*12e6ad0dSWilliam A. Kennington III if (*(uint32_t*)buffer != SIGNATURE_MAGIC) 1975acaca2fSWilliam A. Kennington III { 1985acaca2fSWilliam A. Kennington III CPRINTS(ctx, "validate_transition: bad signature magic"); 1995acaca2fSWilliam A. Kennington III return LIBCR51SIGN_ERROR_INVALID_DESCRIPTOR; 2005acaca2fSWilliam A. Kennington III } 2015acaca2fSWilliam A. Kennington III 2025acaca2fSWilliam A. Kennington III if (ctx->descriptor.image_family != ctx->current_image_family && 2035acaca2fSWilliam A. Kennington III ctx->descriptor.image_family != IMAGE_FAMILY_ALL && 2045acaca2fSWilliam A. Kennington III ctx->current_image_family != IMAGE_FAMILY_ALL) 2055acaca2fSWilliam A. Kennington III { 2065acaca2fSWilliam A. Kennington III CPRINTS(ctx, "validate_transition: invalid image family"); 2075acaca2fSWilliam A. Kennington III return LIBCR51SIGN_ERROR_INVALID_IMAGE_FAMILY; 2085acaca2fSWilliam A. Kennington III } 2095acaca2fSWilliam A. Kennington III 210d2bcdd5cSWilly Tu if (intf->is_production_mode == NULL) 2115acaca2fSWilliam A. Kennington III { 2125acaca2fSWilliam A. Kennington III CPRINTS(ctx, "validate_transition: missing is_production_mode"); 2135acaca2fSWilliam A. Kennington III return LIBCR51SIGN_ERROR_INVALID_INTERFACE; 2145acaca2fSWilliam A. Kennington III } 2155acaca2fSWilliam A. Kennington III if (intf->is_production_mode() && 2165acaca2fSWilliam A. Kennington III (ctx->descriptor.image_type == IMAGE_DEV)) 2175acaca2fSWilliam A. Kennington III { 2185acaca2fSWilliam A. Kennington III CPRINTS(ctx, "validate_transition: checking exemption allowlist"); 2195acaca2fSWilliam A. Kennington III 220d2bcdd5cSWilly Tu // If function is NULL or if the function call return false, return 221d2bcdd5cSWilly Tu // error 222d2bcdd5cSWilly Tu if (intf->prod_to_dev_downgrade_allowed == NULL || 223d2bcdd5cSWilly Tu !intf->prod_to_dev_downgrade_allowed()) 2245acaca2fSWilliam A. Kennington III { 2255acaca2fSWilliam A. Kennington III CPRINTS(ctx, "validate_transition: illegal image type"); 2265acaca2fSWilliam A. Kennington III return LIBCR51SIGN_ERROR_DEV_DOWNGRADE_DISALLOWED; 2275acaca2fSWilliam A. Kennington III } 2285acaca2fSWilliam A. Kennington III } 2295acaca2fSWilliam A. Kennington III return LIBCR51SIGN_SUCCESS; 2305acaca2fSWilliam A. Kennington III } 2315acaca2fSWilliam A. Kennington III 2325acaca2fSWilliam A. Kennington III // If caller had provided read_and_hash_update call that, otherwise call 2335acaca2fSWilliam A. Kennington III // read and then update. 2345acaca2fSWilliam A. Kennington III 2355acaca2fSWilliam A. Kennington III static failure_reason 2365acaca2fSWilliam A. Kennington III read_and_hash_update(const struct libcr51sign_ctx* ctx, 2375acaca2fSWilliam A. Kennington III const struct libcr51sign_intf* intf, 2385acaca2fSWilliam A. Kennington III uint32_t offset, uint32_t size) 2395acaca2fSWilliam A. Kennington III { 2405acaca2fSWilliam A. Kennington III uint8_t read_buffer[MAX_READ_SIZE]; 2415acaca2fSWilliam A. Kennington III int rv; 2425acaca2fSWilliam A. Kennington III int read_size; 2435acaca2fSWilliam A. Kennington III 2445acaca2fSWilliam A. Kennington III if (intf->read_and_hash_update) 2455acaca2fSWilliam A. Kennington III { 2465acaca2fSWilliam A. Kennington III rv = intf->read_and_hash_update((void*)ctx, offset, size); 2475acaca2fSWilliam A. Kennington III } 2485acaca2fSWilliam A. Kennington III else 2495acaca2fSWilliam A. Kennington III { 2505acaca2fSWilliam A. Kennington III if (!intf->hash_update) 2515acaca2fSWilliam A. Kennington III { 2525acaca2fSWilliam A. Kennington III CPRINTS(ctx, "read_and_hash_update: missing hash_update"); 2535acaca2fSWilliam A. Kennington III return LIBCR51SIGN_ERROR_INVALID_INTERFACE; 2545acaca2fSWilliam A. Kennington III } 2555acaca2fSWilliam A. Kennington III do 2565acaca2fSWilliam A. Kennington III { 2575acaca2fSWilliam A. Kennington III read_size = size < MAX_READ_SIZE ? size : MAX_READ_SIZE; 2585acaca2fSWilliam A. Kennington III rv = intf->read((void*)ctx, offset, read_size, read_buffer); 2595acaca2fSWilliam A. Kennington III if (rv != LIBCR51SIGN_SUCCESS) 2605acaca2fSWilliam A. Kennington III { 2615acaca2fSWilliam A. Kennington III return LIBCR51SIGN_ERROR_RUNTIME_FAILURE; 2625acaca2fSWilliam A. Kennington III } 2635acaca2fSWilliam A. Kennington III rv = intf->hash_update((void*)ctx, read_buffer, read_size); 2645acaca2fSWilliam A. Kennington III if (rv != LIBCR51SIGN_SUCCESS) 2655acaca2fSWilliam A. Kennington III { 2665acaca2fSWilliam A. Kennington III return LIBCR51SIGN_ERROR_RUNTIME_FAILURE; 2675acaca2fSWilliam A. Kennington III } 2685acaca2fSWilliam A. Kennington III offset += read_size; 2695acaca2fSWilliam A. Kennington III size -= read_size; 2705acaca2fSWilliam A. Kennington III } while (size > 0); 2715acaca2fSWilliam A. Kennington III } 2725acaca2fSWilliam A. Kennington III return rv; 2735acaca2fSWilliam A. Kennington III } 2745acaca2fSWilliam A. Kennington III 2755acaca2fSWilliam A. Kennington III // Validates the image_region array, namely that: 2765acaca2fSWilliam A. Kennington III // - The regions are aligned, contiguous & exhaustive 2775acaca2fSWilliam A. Kennington III // - That the image descriptor resides in a static region 2785acaca2fSWilliam A. Kennington III // 2795acaca2fSWilliam A. Kennington III // If the array is consistent, proceeds to hash the static regions and 2805acaca2fSWilliam A. Kennington III // validates the hash. d_offset is the absolute image descriptor offset 2815acaca2fSWilliam A. Kennington III 2825acaca2fSWilliam A. Kennington III static failure_reason validate_payload_regions( 2835acaca2fSWilliam A. Kennington III const struct libcr51sign_ctx* ctx, struct libcr51sign_intf* intf, 2845acaca2fSWilliam A. Kennington III uint32_t d_offset, struct libcr51sign_validated_regions* image_regions) 2855acaca2fSWilliam A. Kennington III { 2865acaca2fSWilliam A. Kennington III // Allocate buffer to accomodate largest supported hash-type(SHA512) 2875acaca2fSWilliam A. Kennington III uint8_t magic_and_digest[MEMBER_SIZE(struct hash_sha512, hash_magic) + 2885acaca2fSWilliam A. Kennington III LIBCR51SIGN_SHA512_DIGEST_SIZE]; 2895acaca2fSWilliam A. Kennington III uint8_t dcrypto_digest[LIBCR51SIGN_SHA512_DIGEST_SIZE]; 2905acaca2fSWilliam A. Kennington III uint32_t byte_count, region_count, image_size, hash_offset, digest_size; 291deb5501aSWilliam A. Kennington III uint32_t i; 2925acaca2fSWilliam A. Kennington III uint8_t d_region_num = 0; 293deb5501aSWilliam A. Kennington III int rv; 2945acaca2fSWilliam A. Kennington III struct image_region const* region; 2955acaca2fSWilliam A. Kennington III 2965acaca2fSWilliam A. Kennington III if (image_regions == NULL) 2975acaca2fSWilliam A. Kennington III { 2985acaca2fSWilliam A. Kennington III CPRINTS(ctx, "Missing image region input"); 2995acaca2fSWilliam A. Kennington III return LIBCR51SIGN_ERROR_INVALID_REGION_INPUT; 3005acaca2fSWilliam A. Kennington III } 3015acaca2fSWilliam A. Kennington III 3025acaca2fSWilliam A. Kennington III BUILD_ASSERT((MEMBER_SIZE(struct hash_sha256, hash_magic) == 3035acaca2fSWilliam A. Kennington III MEMBER_SIZE(struct hash_sha512, hash_magic))); 3045acaca2fSWilliam A. Kennington III image_size = ctx->descriptor.image_size; 3055acaca2fSWilliam A. Kennington III region_count = ctx->descriptor.region_count; 3065acaca2fSWilliam A. Kennington III hash_offset = d_offset + sizeof(struct image_descriptor) + 3075acaca2fSWilliam A. Kennington III region_count * sizeof(struct image_region); 3085acaca2fSWilliam A. Kennington III // Read the image_region array. 3095acaca2fSWilliam A. Kennington III 3105acaca2fSWilliam A. Kennington III if (region_count > ARRAY_SIZE(image_regions->image_regions)) 3115acaca2fSWilliam A. Kennington III { 3125acaca2fSWilliam A. Kennington III CPRINTS(ctx, "validate_payload_regions: " 3135acaca2fSWilliam A. Kennington III "ctx->descriptor.region_count is greater " 3145acaca2fSWilliam A. Kennington III "than LIBCR51SIGN_MAX_REGION_COUNT"); 3155acaca2fSWilliam A. Kennington III return LIBCR51SIGN_ERROR_INVALID_REGION_SIZE; 3165acaca2fSWilliam A. Kennington III } 3175acaca2fSWilliam A. Kennington III 318deb5501aSWilliam A. Kennington III rv = intf->read(ctx, d_offset + sizeof(struct image_descriptor), 3195acaca2fSWilliam A. Kennington III region_count * sizeof(struct image_region), 3205acaca2fSWilliam A. Kennington III (uint8_t*)&image_regions->image_regions); 3215acaca2fSWilliam A. Kennington III 3225acaca2fSWilliam A. Kennington III image_regions->region_count = region_count; 3235acaca2fSWilliam A. Kennington III 3245acaca2fSWilliam A. Kennington III if (rv != LIBCR51SIGN_SUCCESS) 3255acaca2fSWilliam A. Kennington III { 3265acaca2fSWilliam A. Kennington III CPRINTS(ctx, 3275acaca2fSWilliam A. Kennington III "validate_payload_regions: failed to read region array"); 3285acaca2fSWilliam A. Kennington III return LIBCR51SIGN_ERROR_RUNTIME_FAILURE; 3295acaca2fSWilliam A. Kennington III } 3305acaca2fSWilliam A. Kennington III 3315acaca2fSWilliam A. Kennington III // Validate that the regions are contiguous & exhaustive. 3325acaca2fSWilliam A. Kennington III for (i = 0, byte_count = 0; i < region_count; i++) 3335acaca2fSWilliam A. Kennington III { 3345acaca2fSWilliam A. Kennington III region = image_regions->image_regions + i; 3355acaca2fSWilliam A. Kennington III 3365acaca2fSWilliam A. Kennington III CPRINTS(ctx, 3375acaca2fSWilliam A. Kennington III "validate_payload_regions: region #%d \"%s\" (%x - %x)", i, 338deb5501aSWilliam A. Kennington III (const char*)region->region_name, region->region_offset, 3395acaca2fSWilliam A. Kennington III region->region_offset + region->region_size); 3405acaca2fSWilliam A. Kennington III if ((region->region_offset % IMAGE_REGION_ALIGNMENT) != 0 || 3415acaca2fSWilliam A. Kennington III (region->region_size % IMAGE_REGION_ALIGNMENT) != 0) 3425acaca2fSWilliam A. Kennington III { 3435acaca2fSWilliam A. Kennington III CPRINTS( 3445acaca2fSWilliam A. Kennington III ctx, 3455acaca2fSWilliam A. Kennington III "validate_payload_regions: regions must be sector aligned"); 3465acaca2fSWilliam A. Kennington III return LIBCR51SIGN_ERROR_INVALID_DESCRIPTOR; 3475acaca2fSWilliam A. Kennington III } 3485acaca2fSWilliam A. Kennington III if (region->region_offset != byte_count || 3495acaca2fSWilliam A. Kennington III region->region_size > image_size - byte_count) 3505acaca2fSWilliam A. Kennington III { 3515acaca2fSWilliam A. Kennington III CPRINTS(ctx, "validate_payload_regions: invalid region array"); 3525acaca2fSWilliam A. Kennington III return LIBCR51SIGN_ERROR_INVALID_DESCRIPTOR; 3535acaca2fSWilliam A. Kennington III } 3545acaca2fSWilliam A. Kennington III byte_count += region->region_size; 3555acaca2fSWilliam A. Kennington III // The image descriptor must be part of a static region. 3565acaca2fSWilliam A. Kennington III if (d_offset >= region->region_offset && d_offset < byte_count) 3575acaca2fSWilliam A. Kennington III { 3585acaca2fSWilliam A. Kennington III d_region_num = i; 3595acaca2fSWilliam A. Kennington III CPRINTS( 3605acaca2fSWilliam A. Kennington III ctx, 3615acaca2fSWilliam A. Kennington III "validate_payload_regions: image descriptor in region %d", 3625acaca2fSWilliam A. Kennington III i); 3635acaca2fSWilliam A. Kennington III // The descriptor can't span regions. 3645acaca2fSWilliam A. Kennington III if (ctx->descriptor.descriptor_area_size > byte_count || 3655acaca2fSWilliam A. Kennington III !(region->region_attributes & IMAGE_REGION_STATIC)) 3665acaca2fSWilliam A. Kennington III { 3675acaca2fSWilliam A. Kennington III CPRINTS( 3685acaca2fSWilliam A. Kennington III ctx, 3695acaca2fSWilliam A. Kennington III "validate_payload_regions: descriptor must reside in " 3705acaca2fSWilliam A. Kennington III "static region"); 3715acaca2fSWilliam A. Kennington III return LIBCR51SIGN_ERROR_INVALID_DESCRIPTOR; 3725acaca2fSWilliam A. Kennington III } 3735acaca2fSWilliam A. Kennington III } 3745acaca2fSWilliam A. Kennington III } 3755acaca2fSWilliam A. Kennington III if (byte_count != image_size) 3765acaca2fSWilliam A. Kennington III { 3775acaca2fSWilliam A. Kennington III CPRINTS(ctx, "validate_payload_regions: invalid image size"); 3785acaca2fSWilliam A. Kennington III return LIBCR51SIGN_ERROR_INVALID_DESCRIPTOR; 3795acaca2fSWilliam A. Kennington III } 3805acaca2fSWilliam A. Kennington III 3815acaca2fSWilliam A. Kennington III rv = get_hash_digest_size(ctx->descriptor.hash_type, &digest_size); 3825acaca2fSWilliam A. Kennington III if (rv != LIBCR51SIGN_SUCCESS) 3835acaca2fSWilliam A. Kennington III { 3845acaca2fSWilliam A. Kennington III return rv; 3855acaca2fSWilliam A. Kennington III } 3865acaca2fSWilliam A. Kennington III 3875acaca2fSWilliam A. Kennington III rv = intf->read(ctx, hash_offset, 3885acaca2fSWilliam A. Kennington III MEMBER_SIZE(struct hash_sha256, hash_magic) + 3895acaca2fSWilliam A. Kennington III digest_size, 3905acaca2fSWilliam A. Kennington III magic_and_digest); 3915acaca2fSWilliam A. Kennington III if (rv != LIBCR51SIGN_SUCCESS) 3925acaca2fSWilliam A. Kennington III { 3935acaca2fSWilliam A. Kennington III CPRINTS(ctx, 3945acaca2fSWilliam A. Kennington III "validate_payload_regions: failed to read hash from flash"); 3955acaca2fSWilliam A. Kennington III return LIBCR51SIGN_ERROR_RUNTIME_FAILURE; 3965acaca2fSWilliam A. Kennington III } 3975acaca2fSWilliam A. Kennington III if (*(uint32_t*)magic_and_digest != HASH_MAGIC) 3985acaca2fSWilliam A. Kennington III { 3995acaca2fSWilliam A. Kennington III CPRINTS(ctx, "validate_payload_regions: bad hash magic"); 4005acaca2fSWilliam A. Kennington III return LIBCR51SIGN_ERROR_INVALID_DESCRIPTOR; 4015acaca2fSWilliam A. Kennington III } 4025acaca2fSWilliam A. Kennington III rv = intf->hash_init(ctx, ctx->descriptor.hash_type); 4035acaca2fSWilliam A. Kennington III if (rv != LIBCR51SIGN_SUCCESS) 4045acaca2fSWilliam A. Kennington III { 4055acaca2fSWilliam A. Kennington III CPRINTS(ctx, "validate_payload_regions: hash_init failed"); 4065acaca2fSWilliam A. Kennington III return LIBCR51SIGN_ERROR_RUNTIME_FAILURE; 4075acaca2fSWilliam A. Kennington III } 4085acaca2fSWilliam A. Kennington III for (i = 0; i < region_count; i++) 4095acaca2fSWilliam A. Kennington III { 4105acaca2fSWilliam A. Kennington III uint32_t hash_start, hash_size; 4115acaca2fSWilliam A. Kennington III region = image_regions->image_regions + i; 4125acaca2fSWilliam A. Kennington III 4135acaca2fSWilliam A. Kennington III if (!(region->region_attributes & IMAGE_REGION_STATIC)) 4145acaca2fSWilliam A. Kennington III { 4155acaca2fSWilliam A. Kennington III continue; 4165acaca2fSWilliam A. Kennington III } 4175acaca2fSWilliam A. Kennington III hash_start = region->region_offset; 4185acaca2fSWilliam A. Kennington III hash_size = region->region_size; 4195acaca2fSWilliam A. Kennington III 4205acaca2fSWilliam A. Kennington III // Skip the descriptor. 4215acaca2fSWilliam A. Kennington III do 4225acaca2fSWilliam A. Kennington III { 4235acaca2fSWilliam A. Kennington III if (i == d_region_num) 4245acaca2fSWilliam A. Kennington III { 4255acaca2fSWilliam A. Kennington III hash_size = d_offset - hash_start; 4265acaca2fSWilliam A. Kennington III } 4275acaca2fSWilliam A. Kennington III 4285acaca2fSWilliam A. Kennington III if (!hash_size) 4295acaca2fSWilliam A. Kennington III { 4305acaca2fSWilliam A. Kennington III hash_start += ctx->descriptor.descriptor_area_size; 4315acaca2fSWilliam A. Kennington III hash_size = (region->region_offset + region->region_size - 4325acaca2fSWilliam A. Kennington III hash_start); 4335acaca2fSWilliam A. Kennington III } 4345acaca2fSWilliam A. Kennington III CPRINTS("validate_payload_regions: hashing %s (%x - %x)", 435deb5501aSWilliam A. Kennington III (const char*)region->region_name, hash_start, 4365acaca2fSWilliam A. Kennington III hash_start + hash_size); 4375acaca2fSWilliam A. Kennington III // Read the image_region array. 4385acaca2fSWilliam A. Kennington III rv = read_and_hash_update(ctx, intf, hash_start, hash_size); 4395acaca2fSWilliam A. Kennington III if (rv != LIBCR51SIGN_SUCCESS) 4405acaca2fSWilliam A. Kennington III { 4415acaca2fSWilliam A. Kennington III return rv; 4425acaca2fSWilliam A. Kennington III } 4435acaca2fSWilliam A. Kennington III hash_start += hash_size; 4445acaca2fSWilliam A. Kennington III } while (hash_start != region->region_offset + region->region_size); 4455acaca2fSWilliam A. Kennington III } 4465acaca2fSWilliam A. Kennington III rv = intf->hash_final((void*)ctx, (uint8_t*)dcrypto_digest); 4475acaca2fSWilliam A. Kennington III 4485acaca2fSWilliam A. Kennington III if (rv != LIBCR51SIGN_SUCCESS) 4495acaca2fSWilliam A. Kennington III { 4505acaca2fSWilliam A. Kennington III return LIBCR51SIGN_ERROR_RUNTIME_FAILURE; 4515acaca2fSWilliam A. Kennington III } 4525acaca2fSWilliam A. Kennington III 4535acaca2fSWilliam A. Kennington III if (memcmp(magic_and_digest + 4545acaca2fSWilliam A. Kennington III MEMBER_SIZE(struct hash_sha256, hash_magic), 4555acaca2fSWilliam A. Kennington III dcrypto_digest, digest_size)) 4565acaca2fSWilliam A. Kennington III { 4575acaca2fSWilliam A. Kennington III CPRINTS(ctx, "validate_payload_regions: invalid hash"); 4585acaca2fSWilliam A. Kennington III return LIBCR51SIGN_ERROR_INVALID_HASH; 4595acaca2fSWilliam A. Kennington III } 4605acaca2fSWilliam A. Kennington III // Image is valid. 4615acaca2fSWilliam A. Kennington III return LIBCR51SIGN_SUCCESS; 4625acaca2fSWilliam A. Kennington III } 4635acaca2fSWilliam A. Kennington III 4645acaca2fSWilliam A. Kennington III // Create empty image_regions to pass to validate_payload_regions 4655acaca2fSWilliam A. Kennington III // Support validate_payload_regions_helper to remove image_regions as a 4665acaca2fSWilliam A. Kennington III // required input. 4675acaca2fSWilliam A. Kennington III 4685acaca2fSWilliam A. Kennington III static failure_reason 4695acaca2fSWilliam A. Kennington III allocate_and_validate_payload_regions(const struct libcr51sign_ctx* ctx, 4705acaca2fSWilliam A. Kennington III struct libcr51sign_intf* intf, 4715acaca2fSWilliam A. Kennington III uint32_t d_offset) 4725acaca2fSWilliam A. Kennington III { 4735acaca2fSWilliam A. Kennington III struct libcr51sign_validated_regions image_regions; 4745acaca2fSWilliam A. Kennington III return validate_payload_regions(ctx, intf, d_offset, &image_regions); 4755acaca2fSWilliam A. Kennington III } 4765acaca2fSWilliam A. Kennington III 4775acaca2fSWilliam A. Kennington III // Wrapper around validate_payload_regions to allow nullptr for 4785acaca2fSWilliam A. Kennington III // image_regions. Calls allocate_and_validate_payload_regions when 4795acaca2fSWilliam A. Kennington III // image_regions is nullptr to create placer holder image_regions. 4805acaca2fSWilliam A. Kennington III 4815acaca2fSWilliam A. Kennington III static failure_reason validate_payload_regions_helper( 4825acaca2fSWilliam A. Kennington III const struct libcr51sign_ctx* ctx, struct libcr51sign_intf* intf, 4835acaca2fSWilliam A. Kennington III uint32_t d_offset, struct libcr51sign_validated_regions* image_regions) 4845acaca2fSWilliam A. Kennington III { 4855acaca2fSWilliam A. Kennington III if (image_regions) 4865acaca2fSWilliam A. Kennington III { 4875acaca2fSWilliam A. Kennington III return validate_payload_regions(ctx, intf, d_offset, image_regions); 4885acaca2fSWilliam A. Kennington III } 4895acaca2fSWilliam A. Kennington III 4905acaca2fSWilliam A. Kennington III return allocate_and_validate_payload_regions(ctx, intf, d_offset); 4915acaca2fSWilliam A. Kennington III } 4925acaca2fSWilliam A. Kennington III 4935acaca2fSWilliam A. Kennington III // Check if the given signature_scheme is supported. 4945acaca2fSWilliam A. Kennington III // Returns nonzero on error, zero on success 4955acaca2fSWilliam A. Kennington III 4965acaca2fSWilliam A. Kennington III static failure_reason 4975acaca2fSWilliam A. Kennington III is_signature_scheme_supported(enum signature_scheme scheme) 4985acaca2fSWilliam A. Kennington III { 4995acaca2fSWilliam A. Kennington III switch (scheme) 5005acaca2fSWilliam A. Kennington III { 5015acaca2fSWilliam A. Kennington III case SIGNATURE_RSA2048_PKCS15: 5025acaca2fSWilliam A. Kennington III case SIGNATURE_RSA3072_PKCS15: 5035acaca2fSWilliam A. Kennington III case SIGNATURE_RSA4096_PKCS15: 5045acaca2fSWilliam A. Kennington III case SIGNATURE_RSA4096_PKCS15_SHA512: 5055acaca2fSWilliam A. Kennington III return LIBCR51SIGN_SUCCESS; 5065acaca2fSWilliam A. Kennington III default: 5075acaca2fSWilliam A. Kennington III return LIBCR51SIGN_ERROR_INVALID_SIG_SCHEME; 5085acaca2fSWilliam A. Kennington III } 5095acaca2fSWilliam A. Kennington III } 5105acaca2fSWilliam A. Kennington III 5115acaca2fSWilliam A. Kennington III // Returns size of signature struct size in |size| 5125acaca2fSWilliam A. Kennington III // Returns nonzero on error, zero on success 5135acaca2fSWilliam A. Kennington III 5145acaca2fSWilliam A. Kennington III static failure_reason 5155acaca2fSWilliam A. Kennington III get_signature_struct_size(enum signature_scheme scheme, uint32_t* size) 5165acaca2fSWilliam A. Kennington III { 5175acaca2fSWilliam A. Kennington III switch (scheme) 5185acaca2fSWilliam A. Kennington III { 5195acaca2fSWilliam A. Kennington III case SIGNATURE_RSA2048_PKCS15: 5205acaca2fSWilliam A. Kennington III *size = sizeof(struct signature_rsa2048_pkcs15); 5215acaca2fSWilliam A. Kennington III return LIBCR51SIGN_SUCCESS; 5225acaca2fSWilliam A. Kennington III case SIGNATURE_RSA3072_PKCS15: 5235acaca2fSWilliam A. Kennington III *size = sizeof(struct signature_rsa3072_pkcs15); 5245acaca2fSWilliam A. Kennington III return LIBCR51SIGN_SUCCESS; 5255acaca2fSWilliam A. Kennington III case SIGNATURE_RSA4096_PKCS15: 5265acaca2fSWilliam A. Kennington III case SIGNATURE_RSA4096_PKCS15_SHA512: 5275acaca2fSWilliam A. Kennington III *size = sizeof(struct signature_rsa4096_pkcs15); 5285acaca2fSWilliam A. Kennington III return LIBCR51SIGN_SUCCESS; 5295acaca2fSWilliam A. Kennington III default: 5305acaca2fSWilliam A. Kennington III return LIBCR51SIGN_ERROR_INVALID_SIG_SCHEME; 5315acaca2fSWilliam A. Kennington III } 5325acaca2fSWilliam A. Kennington III } 5335acaca2fSWilliam A. Kennington III 5345acaca2fSWilliam A. Kennington III static failure_reason 5355acaca2fSWilliam A. Kennington III get_signature_field_offset(enum signature_scheme scheme, 5365acaca2fSWilliam A. Kennington III uint32_t* offset) 5375acaca2fSWilliam A. Kennington III { 5385acaca2fSWilliam A. Kennington III switch (scheme) 5395acaca2fSWilliam A. Kennington III { 5405acaca2fSWilliam A. Kennington III case SIGNATURE_RSA2048_PKCS15: 5415acaca2fSWilliam A. Kennington III *offset = offsetof(struct signature_rsa2048_pkcs15, signature); 5425acaca2fSWilliam A. Kennington III return LIBCR51SIGN_SUCCESS; 5435acaca2fSWilliam A. Kennington III case SIGNATURE_RSA3072_PKCS15: 5445acaca2fSWilliam A. Kennington III *offset = offsetof(struct signature_rsa3072_pkcs15, signature); 5455acaca2fSWilliam A. Kennington III return LIBCR51SIGN_SUCCESS; 5465acaca2fSWilliam A. Kennington III case SIGNATURE_RSA4096_PKCS15: 5475acaca2fSWilliam A. Kennington III case SIGNATURE_RSA4096_PKCS15_SHA512: 5485acaca2fSWilliam A. Kennington III *offset = offsetof(struct signature_rsa4096_pkcs15, signature); 5495acaca2fSWilliam A. Kennington III return LIBCR51SIGN_SUCCESS; 5505acaca2fSWilliam A. Kennington III default: 5515acaca2fSWilliam A. Kennington III return LIBCR51SIGN_ERROR_INVALID_SIG_SCHEME; 5525acaca2fSWilliam A. Kennington III } 5535acaca2fSWilliam A. Kennington III } 5545acaca2fSWilliam A. Kennington III 5555acaca2fSWilliam A. Kennington III // Validates the signature (of type scheme) read from "device" at 5565acaca2fSWilliam A. Kennington III //"raw_signature_offset" with "public_key" over a SHA256/SHA512 digest of 5575acaca2fSWilliam A. Kennington III // EEPROM area "data_offset:data_size". 5585acaca2fSWilliam A. Kennington III 5595acaca2fSWilliam A. Kennington III static failure_reason validate_signature( 5605acaca2fSWilliam A. Kennington III const struct libcr51sign_ctx* ctx, const struct libcr51sign_intf* intf, 5615acaca2fSWilliam A. Kennington III uint32_t data_offset, uint32_t data_size, enum signature_scheme scheme, 5625acaca2fSWilliam A. Kennington III uint32_t raw_signature_offset) 5635acaca2fSWilliam A. Kennington III { 5645acaca2fSWilliam A. Kennington III uint8_t signature[LIBCR51SIGN_MAX_SIGNATURE_SIZE]; 5655acaca2fSWilliam A. Kennington III uint16_t key_size; 5665acaca2fSWilliam A. Kennington III uint32_t digest_size; 5675acaca2fSWilliam A. Kennington III uint8_t dcrypto_digest[LIBCR51SIGN_SHA512_DIGEST_SIZE]; 5685acaca2fSWilliam A. Kennington III int rv; 5695acaca2fSWilliam A. Kennington III enum hash_type hash_type; 5705acaca2fSWilliam A. Kennington III 5715acaca2fSWilliam A. Kennington III if (!intf->hash_init) 5725acaca2fSWilliam A. Kennington III { 5735acaca2fSWilliam A. Kennington III CPRINTS(ctx, "validate_signature: missing hash_init"); 5745acaca2fSWilliam A. Kennington III return LIBCR51SIGN_ERROR_INVALID_INTERFACE; 5755acaca2fSWilliam A. Kennington III } 5765acaca2fSWilliam A. Kennington III rv = get_hash_type_from_signature(scheme, &hash_type); 5775acaca2fSWilliam A. Kennington III if (rv != LIBCR51SIGN_SUCCESS) 5785acaca2fSWilliam A. Kennington III { 5795acaca2fSWilliam A. Kennington III CPRINTS( 5805acaca2fSWilliam A. Kennington III ctx, 5815acaca2fSWilliam A. Kennington III "validate_payload_regions: hash_type from signature failed"); 5825acaca2fSWilliam A. Kennington III return rv; 5835acaca2fSWilliam A. Kennington III } 5845acaca2fSWilliam A. Kennington III rv = intf->hash_init(ctx, hash_type); 5855acaca2fSWilliam A. Kennington III if (rv != LIBCR51SIGN_SUCCESS) 5865acaca2fSWilliam A. Kennington III { 5875acaca2fSWilliam A. Kennington III CPRINTS(ctx, "validate_payload_regions: hash_init failed"); 5885acaca2fSWilliam A. Kennington III return LIBCR51SIGN_ERROR_RUNTIME_FAILURE; 5895acaca2fSWilliam A. Kennington III } 5905acaca2fSWilliam A. Kennington III rv = read_and_hash_update(ctx, intf, data_offset, data_size); 5915acaca2fSWilliam A. Kennington III if (rv != LIBCR51SIGN_SUCCESS) 5925acaca2fSWilliam A. Kennington III { 5935acaca2fSWilliam A. Kennington III CPRINTS(ctx, "validate_signature: hash_update failed"); 5945acaca2fSWilliam A. Kennington III return rv; 5955acaca2fSWilliam A. Kennington III } 5965acaca2fSWilliam A. Kennington III if (!intf->hash_final) 5975acaca2fSWilliam A. Kennington III { 5985acaca2fSWilliam A. Kennington III CPRINTS(ctx, "validate_signature: missing hash_final"); 5995acaca2fSWilliam A. Kennington III return LIBCR51SIGN_ERROR_INVALID_INTERFACE; 6005acaca2fSWilliam A. Kennington III } 6015acaca2fSWilliam A. Kennington III rv = intf->hash_final((void*)ctx, dcrypto_digest); 6025acaca2fSWilliam A. Kennington III if (rv != LIBCR51SIGN_SUCCESS) 6035acaca2fSWilliam A. Kennington III { 6045acaca2fSWilliam A. Kennington III CPRINTS(ctx, "validate_signature: hash_final failed (status = %d)", 6055acaca2fSWilliam A. Kennington III rv); 6065acaca2fSWilliam A. Kennington III return LIBCR51SIGN_ERROR_RUNTIME_FAILURE; 6075acaca2fSWilliam A. Kennington III } 6085acaca2fSWilliam A. Kennington III rv = get_key_size(scheme, &key_size); 6095acaca2fSWilliam A. Kennington III if (rv != LIBCR51SIGN_SUCCESS) 6105acaca2fSWilliam A. Kennington III { 6115acaca2fSWilliam A. Kennington III return rv; 6125acaca2fSWilliam A. Kennington III } 6135acaca2fSWilliam A. Kennington III 6145acaca2fSWilliam A. Kennington III rv = intf->read(ctx, raw_signature_offset, key_size, signature); 6155acaca2fSWilliam A. Kennington III if (rv != LIBCR51SIGN_SUCCESS) 6165acaca2fSWilliam A. Kennington III { 6175acaca2fSWilliam A. Kennington III CPRINTS( 6185acaca2fSWilliam A. Kennington III ctx, 6195acaca2fSWilliam A. Kennington III "validate_signature: failed to read signature (status = %d)", 6205acaca2fSWilliam A. Kennington III rv); 6215acaca2fSWilliam A. Kennington III return LIBCR51SIGN_ERROR_RUNTIME_FAILURE; 6225acaca2fSWilliam A. Kennington III } 6235acaca2fSWilliam A. Kennington III if (!intf->verify_signature) 6245acaca2fSWilliam A. Kennington III { 6255acaca2fSWilliam A. Kennington III CPRINTS(ctx, "validate_signature: missing verify_signature"); 6265acaca2fSWilliam A. Kennington III return LIBCR51SIGN_ERROR_INVALID_INTERFACE; 6275acaca2fSWilliam A. Kennington III } 6285acaca2fSWilliam A. Kennington III rv = get_hash_digest_size(hash_type, &digest_size); 6295acaca2fSWilliam A. Kennington III if (rv != LIBCR51SIGN_SUCCESS) 6305acaca2fSWilliam A. Kennington III { 6315acaca2fSWilliam A. Kennington III return rv; 6325acaca2fSWilliam A. Kennington III } 6335acaca2fSWilliam A. Kennington III rv = intf->verify_signature(ctx, scheme, signature, key_size, 6345acaca2fSWilliam A. Kennington III dcrypto_digest, digest_size); 6355acaca2fSWilliam A. Kennington III if (rv != LIBCR51SIGN_SUCCESS) 6365acaca2fSWilliam A. Kennington III { 6375acaca2fSWilliam A. Kennington III CPRINTS(ctx, 6385acaca2fSWilliam A. Kennington III "validate_signature: verification failed (status = %d)", 6395acaca2fSWilliam A. Kennington III rv); 6405acaca2fSWilliam A. Kennington III return LIBCR51SIGN_ERROR_INVALID_SIGNATURE; 6415acaca2fSWilliam A. Kennington III } 6425acaca2fSWilliam A. Kennington III CPRINTS(ctx, "validate_signature: verification succeeded"); 6435acaca2fSWilliam A. Kennington III return LIBCR51SIGN_SUCCESS; 6445acaca2fSWilliam A. Kennington III } 6455acaca2fSWilliam A. Kennington III 6465acaca2fSWilliam A. Kennington III // Sanity checks the image descriptor & validates its signature. 6475acaca2fSWilliam A. Kennington III // This function does not validate the image_region array or image hash. 6485acaca2fSWilliam A. Kennington III // 6495acaca2fSWilliam A. Kennington III //@param[in] ctx context which describes the image and holds opaque private 6505acaca2fSWilliam A. Kennington III // data for the user of the library 6515acaca2fSWilliam A. Kennington III //@param[in] intf function pointers which interface to the current system 6525acaca2fSWilliam A. Kennington III // and environment 6535acaca2fSWilliam A. Kennington III //@param offset Absolute image descriptor flash offset. 6545acaca2fSWilliam A. Kennington III //@param relative_offset Image descriptor offset relative to image start. 6555acaca2fSWilliam A. Kennington III //@param image_size Image size in bytes. 6565acaca2fSWilliam A. Kennington III //@param descriptor Output pointer to an image_descriptor struct 6575acaca2fSWilliam A. Kennington III 6585acaca2fSWilliam A. Kennington III static failure_reason validate_descriptor( 6595acaca2fSWilliam A. Kennington III const struct libcr51sign_ctx* ctx, const struct libcr51sign_intf* intf, 6605acaca2fSWilliam A. Kennington III uint32_t offset, uint32_t relative_offset, uint32_t image_size) 6615acaca2fSWilliam A. Kennington III { 6625acaca2fSWilliam A. Kennington III uint32_t max_descriptor_size, signed_size, signature_scheme, 6635acaca2fSWilliam A. Kennington III signature_offset; 6645acaca2fSWilliam A. Kennington III uint32_t signature_struct_offset, signature_struct_size, 6655acaca2fSWilliam A. Kennington III hash_struct_size; 6665acaca2fSWilliam A. Kennington III int rv; 6675acaca2fSWilliam A. Kennington III 6685acaca2fSWilliam A. Kennington III max_descriptor_size = image_size - relative_offset; 6695acaca2fSWilliam A. Kennington III if (image_size < relative_offset || 6705acaca2fSWilliam A. Kennington III max_descriptor_size < sizeof(struct image_descriptor)) 6715acaca2fSWilliam A. Kennington III { 6725acaca2fSWilliam A. Kennington III CPRINTS(ctx, "validate_descriptor: invalid arguments"); 6735acaca2fSWilliam A. Kennington III return LIBCR51SIGN_ERROR_INVALID_DESCRIPTOR; 6745acaca2fSWilliam A. Kennington III } 6755acaca2fSWilliam A. Kennington III 6765acaca2fSWilliam A. Kennington III rv = intf->read(ctx, offset, sizeof(ctx->descriptor), 6775acaca2fSWilliam A. Kennington III (uint8_t*)&ctx->descriptor); 6785acaca2fSWilliam A. Kennington III if (rv != LIBCR51SIGN_SUCCESS) 6795acaca2fSWilliam A. Kennington III { 6805acaca2fSWilliam A. Kennington III CPRINTS(ctx, "validate_descriptor: failed to read descriptor"); 6815acaca2fSWilliam A. Kennington III return LIBCR51SIGN_ERROR_RUNTIME_FAILURE; 6825acaca2fSWilliam A. Kennington III } 6835acaca2fSWilliam A. Kennington III if (ctx->descriptor.descriptor_magic != DESCRIPTOR_MAGIC || 6845acaca2fSWilliam A. Kennington III ctx->descriptor.descriptor_offset != relative_offset || 6855acaca2fSWilliam A. Kennington III ctx->descriptor.region_count == 0 || 6865acaca2fSWilliam A. Kennington III ctx->descriptor.descriptor_area_size > max_descriptor_size || 6875acaca2fSWilliam A. Kennington III ctx->descriptor.image_size != image_size) 6885acaca2fSWilliam A. Kennington III { 6895acaca2fSWilliam A. Kennington III CPRINTS(ctx, "validate_descriptor: invalid descriptor"); 6905acaca2fSWilliam A. Kennington III return LIBCR51SIGN_ERROR_INVALID_DESCRIPTOR; 6915acaca2fSWilliam A. Kennington III } 6925acaca2fSWilliam A. Kennington III if (ctx->descriptor.image_type != IMAGE_DEV && 6935acaca2fSWilliam A. Kennington III ctx->descriptor.image_type != IMAGE_PROD && 6945acaca2fSWilliam A. Kennington III ctx->descriptor.image_type != IMAGE_BREAKOUT && 6955acaca2fSWilliam A. Kennington III ctx->descriptor.image_type != IMAGE_TEST && 6965acaca2fSWilliam A. Kennington III ctx->descriptor.image_type != IMAGE_UNSIGNED_INTEGRITY) 6975acaca2fSWilliam A. Kennington III { 6985acaca2fSWilliam A. Kennington III CPRINTS(ctx, "validate_descriptor: bad image type"); 6995acaca2fSWilliam A. Kennington III return LIBCR51SIGN_ERROR_INVALID_DESCRIPTOR; 7005acaca2fSWilliam A. Kennington III } 7015acaca2fSWilliam A. Kennington III // Although the image_descriptor struct supports unauthenticated 7025acaca2fSWilliam A. Kennington III // images, Haven will not allow it. 7035acaca2fSWilliam A. Kennington III // Haven only supports SHA256 + RSA2048/RSA3072_PKCS15 currently. 7045acaca2fSWilliam A. Kennington III 7055acaca2fSWilliam A. Kennington III signature_scheme = ctx->descriptor.signature_scheme; 7065acaca2fSWilliam A. Kennington III 7075acaca2fSWilliam A. Kennington III rv = is_signature_scheme_supported(signature_scheme); 7085acaca2fSWilliam A. Kennington III if (rv != LIBCR51SIGN_SUCCESS) 7095acaca2fSWilliam A. Kennington III { 7105acaca2fSWilliam A. Kennington III return rv; 7115acaca2fSWilliam A. Kennington III } 7125acaca2fSWilliam A. Kennington III rv = is_hash_type_supported(ctx->descriptor.hash_type); 7135acaca2fSWilliam A. Kennington III if (rv != LIBCR51SIGN_SUCCESS) 7145acaca2fSWilliam A. Kennington III { 7155acaca2fSWilliam A. Kennington III CPRINTS(ctx, "validate_payload_regions: invalid hash type"); 7165acaca2fSWilliam A. Kennington III return rv; 7175acaca2fSWilliam A. Kennington III } 7185acaca2fSWilliam A. Kennington III if (ctx->descriptor.descriptor_major > MAX_MAJOR_VERSION || 7195acaca2fSWilliam A. Kennington III ctx->descriptor.region_count > LIBCR51SIGN_MAX_REGION_COUNT) 7205acaca2fSWilliam A. Kennington III { 7215acaca2fSWilliam A. Kennington III CPRINTS(ctx, "validate_descriptor: unsupported descriptor"); 7225acaca2fSWilliam A. Kennington III return LIBCR51SIGN_ERROR_UNSUPPORTED_DESCRIPTOR; 7235acaca2fSWilliam A. Kennington III } 7245acaca2fSWilliam A. Kennington III rv = 7255acaca2fSWilliam A. Kennington III get_signature_struct_size(signature_scheme, &signature_struct_size); 7265acaca2fSWilliam A. Kennington III if (rv != LIBCR51SIGN_SUCCESS) 7275acaca2fSWilliam A. Kennington III { 7285acaca2fSWilliam A. Kennington III return rv; 7295acaca2fSWilliam A. Kennington III } 7305acaca2fSWilliam A. Kennington III 7315acaca2fSWilliam A. Kennington III // Compute the size of the signed portion of the image descriptor. 7325acaca2fSWilliam A. Kennington III signed_size = 7335acaca2fSWilliam A. Kennington III sizeof(struct image_descriptor) + 7345acaca2fSWilliam A. Kennington III ctx->descriptor.region_count * sizeof(struct image_region); 7355acaca2fSWilliam A. Kennington III rv = get_hash_struct_size(ctx->descriptor.hash_type, &hash_struct_size); 7365acaca2fSWilliam A. Kennington III if (rv != LIBCR51SIGN_SUCCESS) 7375acaca2fSWilliam A. Kennington III { 7385acaca2fSWilliam A. Kennington III return rv; 7395acaca2fSWilliam A. Kennington III } 7405acaca2fSWilliam A. Kennington III signed_size += hash_struct_size; 7415acaca2fSWilliam A. Kennington III if (ctx->descriptor.denylist_size) 7425acaca2fSWilliam A. Kennington III { 7435acaca2fSWilliam A. Kennington III signed_size += sizeof(struct denylist); 7445acaca2fSWilliam A. Kennington III signed_size += 7455acaca2fSWilliam A. Kennington III ctx->descriptor.denylist_size * sizeof(struct denylist_record); 7465acaca2fSWilliam A. Kennington III } 7475acaca2fSWilliam A. Kennington III if (ctx->descriptor.blob_size) 7485acaca2fSWilliam A. Kennington III { 7495acaca2fSWilliam A. Kennington III signed_size += sizeof(struct blob); 7505acaca2fSWilliam A. Kennington III // Previous additions are guaranteed not to overflow. 7515acaca2fSWilliam A. Kennington III if (ctx->descriptor.blob_size > 7525acaca2fSWilliam A. Kennington III ctx->descriptor.descriptor_area_size - signed_size) 7535acaca2fSWilliam A. Kennington III { 7545acaca2fSWilliam A. Kennington III CPRINTS(ctx, "validate_descriptor: invalid blob size (0x%x)", 7555acaca2fSWilliam A. Kennington III ctx->descriptor.blob_size); 7565acaca2fSWilliam A. Kennington III return LIBCR51SIGN_ERROR_INVALID_DESCRIPTOR; 7575acaca2fSWilliam A. Kennington III } 7585acaca2fSWilliam A. Kennington III signed_size += ctx->descriptor.blob_size; 7595acaca2fSWilliam A. Kennington III } 7605acaca2fSWilliam A. Kennington III if (signature_struct_size > 7615acaca2fSWilliam A. Kennington III ctx->descriptor.descriptor_area_size - signed_size) 7625acaca2fSWilliam A. Kennington III { 7635acaca2fSWilliam A. Kennington III CPRINTS(ctx, 7645acaca2fSWilliam A. Kennington III "validate_descriptor: invalid descriptor area size " 7655acaca2fSWilliam A. Kennington III "(expected = 0x%x, actual = 0x%x)", 7665acaca2fSWilliam A. Kennington III ctx->descriptor.descriptor_area_size, 7675acaca2fSWilliam A. Kennington III signed_size + signature_struct_size); 7685acaca2fSWilliam A. Kennington III return LIBCR51SIGN_ERROR_INVALID_DESCRIPTOR; 7695acaca2fSWilliam A. Kennington III } 7705acaca2fSWilliam A. Kennington III signature_struct_offset = signed_size; 7715acaca2fSWilliam A. Kennington III // Omit the actual signature. 7725acaca2fSWilliam A. Kennington III rv = get_signature_field_offset(signature_scheme, &signature_offset); 7735acaca2fSWilliam A. Kennington III if (rv != LIBCR51SIGN_SUCCESS) 7745acaca2fSWilliam A. Kennington III { 7755acaca2fSWilliam A. Kennington III return rv; 7765acaca2fSWilliam A. Kennington III } 7775acaca2fSWilliam A. Kennington III signed_size += signature_offset; 7785acaca2fSWilliam A. Kennington III 7795acaca2fSWilliam A. Kennington III // Lookup key & validate transition. 7805acaca2fSWilliam A. Kennington III rv = validate_transition(ctx, intf, offset + signature_struct_offset); 7815acaca2fSWilliam A. Kennington III 7825acaca2fSWilliam A. Kennington III if (rv != LIBCR51SIGN_SUCCESS) 7835acaca2fSWilliam A. Kennington III { 7845acaca2fSWilliam A. Kennington III return rv; 7855acaca2fSWilliam A. Kennington III } 7865acaca2fSWilliam A. Kennington III return validate_signature(ctx, intf, offset, signed_size, 7875acaca2fSWilliam A. Kennington III signature_scheme, offset + signed_size); 7885acaca2fSWilliam A. Kennington III } 7895acaca2fSWilliam A. Kennington III 7905acaca2fSWilliam A. Kennington III // Scans the external EEPROM for a magic value at "alignment" boundaries. 7915acaca2fSWilliam A. Kennington III // 7925acaca2fSWilliam A. Kennington III //@param device Handle to the external EEPROM. 7935acaca2fSWilliam A. Kennington III //@param magic 8-byte pattern to search for. 7945acaca2fSWilliam A. Kennington III //@param start_offset Offset to begin searching at. 7955acaca2fSWilliam A. Kennington III //@param limit Exclusive address (e.g. EEPROM size). 7965acaca2fSWilliam A. Kennington III //@param alignment Alignment boundaries (POW2) to search on. 7975acaca2fSWilliam A. Kennington III //@param header_offset Location to place the new header offset. 7985acaca2fSWilliam A. Kennington III //@return LIBCR51SIGN_SUCCESS (or non-zero on error). 7995acaca2fSWilliam A. Kennington III 8005acaca2fSWilliam A. Kennington III int scan_for_magic_8(const struct libcr51sign_ctx* ctx, 8015acaca2fSWilliam A. Kennington III const struct libcr51sign_intf* intf, uint64_t magic, 8025acaca2fSWilliam A. Kennington III uint32_t start_offset, uint32_t limit, 8035acaca2fSWilliam A. Kennington III uint32_t alignment, uint32_t* header_offset) 8045acaca2fSWilliam A. Kennington III { 8055acaca2fSWilliam A. Kennington III uint64_t read_data; 8065acaca2fSWilliam A. Kennington III uint32_t offset; 8075acaca2fSWilliam A. Kennington III int rv; 8085acaca2fSWilliam A. Kennington III 8095acaca2fSWilliam A. Kennington III if (limit <= start_offset || limit > ctx->end_offset || 8105acaca2fSWilliam A. Kennington III limit < sizeof(magic) || !POWER_OF_TWO(alignment)) 8115acaca2fSWilliam A. Kennington III { 8125acaca2fSWilliam A. Kennington III return LIBCR51SIGN_ERROR_INVALID_ARGUMENT; 8135acaca2fSWilliam A. Kennington III } 8145acaca2fSWilliam A. Kennington III 8155acaca2fSWilliam A. Kennington III if (!intf->read) 8165acaca2fSWilliam A. Kennington III { 8175acaca2fSWilliam A. Kennington III CPRINTS(ctx, "scan_for_magic_8: missing intf->read"); 8185acaca2fSWilliam A. Kennington III return LIBCR51SIGN_ERROR_INVALID_INTERFACE; 8195acaca2fSWilliam A. Kennington III } 8205acaca2fSWilliam A. Kennington III // Align start_offset to the next valid boundary. 8215acaca2fSWilliam A. Kennington III start_offset = ((start_offset - 1) & ~(alignment - 1)) + alignment; 8225acaca2fSWilliam A. Kennington III for (offset = start_offset; offset < limit - sizeof(magic); 8235acaca2fSWilliam A. Kennington III offset += alignment) 8245acaca2fSWilliam A. Kennington III { 8255acaca2fSWilliam A. Kennington III rv = intf->read((void*)ctx, offset, sizeof(read_data), 8265acaca2fSWilliam A. Kennington III (uint8_t*)&read_data); 8275acaca2fSWilliam A. Kennington III if (rv != LIBCR51SIGN_SUCCESS) 8285acaca2fSWilliam A. Kennington III { 8295acaca2fSWilliam A. Kennington III return rv; 8305acaca2fSWilliam A. Kennington III } 8315acaca2fSWilliam A. Kennington III if (read_data == magic) 8325acaca2fSWilliam A. Kennington III { 8335acaca2fSWilliam A. Kennington III if (header_offset) 8345acaca2fSWilliam A. Kennington III { 8355acaca2fSWilliam A. Kennington III *header_offset = offset; 8365acaca2fSWilliam A. Kennington III } 8375acaca2fSWilliam A. Kennington III return LIBCR51SIGN_SUCCESS; 8385acaca2fSWilliam A. Kennington III } 8395acaca2fSWilliam A. Kennington III } 8405acaca2fSWilliam A. Kennington III // Failed to locate magic. 8415acaca2fSWilliam A. Kennington III return LIBCR51SIGN_ERROR_FAILED_TO_LOCATE_MAGIC; 8425acaca2fSWilliam A. Kennington III } 8435acaca2fSWilliam A. Kennington III 8445acaca2fSWilliam A. Kennington III // Check whether the signature on the image is valid. 8455acaca2fSWilliam A. Kennington III // Validates the authenticity of an EEPROM image. Scans for & validates the 8465acaca2fSWilliam A. Kennington III // signature on the image descriptor. If the descriptor validates, hashes 8475acaca2fSWilliam A. Kennington III // the rest of the image to verify its integrity. 8485acaca2fSWilliam A. Kennington III // 8495acaca2fSWilliam A. Kennington III // @param[in] ctx - context which describes the image and holds opaque 8505acaca2fSWilliam A. Kennington III // private 8515acaca2fSWilliam A. Kennington III // data for the user of the library 8525acaca2fSWilliam A. Kennington III // @param[in] intf - function pointers which interface to the current system 8535acaca2fSWilliam A. Kennington III // and environment 8545acaca2fSWilliam A. Kennington III // @param[out] image_regions - image_region pointer to an array for the 8555acaca2fSWilliam A. Kennington III // output 8565acaca2fSWilliam A. Kennington III // 8575acaca2fSWilliam A. Kennington III // @return nonzero on error, zero on success 8585acaca2fSWilliam A. Kennington III 8595acaca2fSWilliam A. Kennington III failure_reason libcr51sign_validate( 8605acaca2fSWilliam A. Kennington III const struct libcr51sign_ctx* ctx, struct libcr51sign_intf* intf, 8615acaca2fSWilliam A. Kennington III struct libcr51sign_validated_regions* image_regions) 8625acaca2fSWilliam A. Kennington III { 8635acaca2fSWilliam A. Kennington III uint32_t image_limit = 0; 8645acaca2fSWilliam A. Kennington III int rv, rv_first_desc = LIBCR51SIGN_SUCCESS; 8655acaca2fSWilliam A. Kennington III uint32_t descriptor_offset; 8665acaca2fSWilliam A. Kennington III 8675acaca2fSWilliam A. Kennington III if (!ctx) 8685acaca2fSWilliam A. Kennington III { 8695acaca2fSWilliam A. Kennington III CPRINTS(ctx, "MIssing context"); 8705acaca2fSWilliam A. Kennington III return LIBCR51SIGN_ERROR_INVALID_CONTEXT; 8715acaca2fSWilliam A. Kennington III } 8725acaca2fSWilliam A. Kennington III else if (!intf) 8735acaca2fSWilliam A. Kennington III { 8745acaca2fSWilliam A. Kennington III CPRINTS(ctx, "Missing interface"); 8755acaca2fSWilliam A. Kennington III return LIBCR51SIGN_ERROR_INVALID_INTERFACE; 8765acaca2fSWilliam A. Kennington III } 8775acaca2fSWilliam A. Kennington III 8785acaca2fSWilliam A. Kennington III rv = scan_for_magic_8(ctx, intf, DESCRIPTOR_MAGIC, ctx->start_offset, 8795acaca2fSWilliam A. Kennington III ctx->end_offset, DESCRIPTOR_ALIGNMENT, 8805acaca2fSWilliam A. Kennington III &descriptor_offset); 8815acaca2fSWilliam A. Kennington III while (rv == LIBCR51SIGN_SUCCESS) 8825acaca2fSWilliam A. Kennington III { 8835acaca2fSWilliam A. Kennington III CPRINTS(ctx, "validate: potential image descriptor found @%x ", 8845acaca2fSWilliam A. Kennington III descriptor_offset); 8855acaca2fSWilliam A. Kennington III // Validation is split into 2 functions to minimize 8865acaca2fSWilliam A. Kennington III // stack usage. 8875acaca2fSWilliam A. Kennington III 8885acaca2fSWilliam A. Kennington III rv = validate_descriptor(ctx, intf, descriptor_offset, 8895acaca2fSWilliam A. Kennington III descriptor_offset - ctx->start_offset, 8905acaca2fSWilliam A. Kennington III ctx->end_offset - ctx->start_offset); 8915acaca2fSWilliam A. Kennington III if (rv != LIBCR51SIGN_SUCCESS) 8925acaca2fSWilliam A. Kennington III { 8935acaca2fSWilliam A. Kennington III CPRINTS(ctx, "validate: validate_descriptor() failed ec%d ", 8945acaca2fSWilliam A. Kennington III rv); 8955acaca2fSWilliam A. Kennington III } 8965acaca2fSWilliam A. Kennington III 8975acaca2fSWilliam A. Kennington III if (rv == LIBCR51SIGN_SUCCESS) 8985acaca2fSWilliam A. Kennington III { 8995acaca2fSWilliam A. Kennington III rv = validate_payload_regions_helper( 9005acaca2fSWilliam A. Kennington III ctx, intf, descriptor_offset, image_regions); 9015acaca2fSWilliam A. Kennington III if (rv == LIBCR51SIGN_SUCCESS) 9025acaca2fSWilliam A. Kennington III { 9035acaca2fSWilliam A. Kennington III CPRINTS(ctx, "validate: success!"); 9045acaca2fSWilliam A. Kennington III return rv; 9055acaca2fSWilliam A. Kennington III } 9065acaca2fSWilliam A. Kennington III CPRINTS(ctx, 9075acaca2fSWilliam A. Kennington III "validate: validate_payload_regions() failed ec%d ", 9085acaca2fSWilliam A. Kennington III rv); 9095acaca2fSWilliam A. Kennington III } 9105acaca2fSWilliam A. Kennington III // Store the first desc fail reason if any 9115acaca2fSWilliam A. Kennington III if (rv != LIBCR51SIGN_SUCCESS && 9125acaca2fSWilliam A. Kennington III rv_first_desc == LIBCR51SIGN_SUCCESS) 9135acaca2fSWilliam A. Kennington III rv_first_desc = rv; 9145acaca2fSWilliam A. Kennington III 9155acaca2fSWilliam A. Kennington III // scan_for_magic_8() will round up to the next aligned boundary. 9165acaca2fSWilliam A. Kennington III descriptor_offset++; 9175acaca2fSWilliam A. Kennington III image_limit = ctx->end_offset - ctx->start_offset; 9185acaca2fSWilliam A. Kennington III rv = scan_for_magic_8(ctx, intf, DESCRIPTOR_MAGIC, 9195acaca2fSWilliam A. Kennington III descriptor_offset, image_limit, 9205acaca2fSWilliam A. Kennington III DESCRIPTOR_ALIGNMENT, &descriptor_offset); 9215acaca2fSWilliam A. Kennington III } 9225acaca2fSWilliam A. Kennington III CPRINTS(ctx, "validate: failed to validate image ec%d ", rv); 9235acaca2fSWilliam A. Kennington III // If desc validation failed for some reason then return that reason 9245acaca2fSWilliam A. Kennington III if (rv_first_desc != LIBCR51SIGN_SUCCESS) 9255acaca2fSWilliam A. Kennington III return rv_first_desc; 9265acaca2fSWilliam A. Kennington III else 9275acaca2fSWilliam A. Kennington III return rv; 9285acaca2fSWilliam A. Kennington III } 9295acaca2fSWilliam A. Kennington III 9305acaca2fSWilliam A. Kennington III // @func to returns the libcr51sign error code as a string 9315acaca2fSWilliam A. Kennington III // @param[in] ec - Error code 9325acaca2fSWilliam A. Kennington III // @return error code in string format 9335acaca2fSWilliam A. Kennington III 9345acaca2fSWilliam A. Kennington III const char* libcr51sign_errorcode_to_string(failure_reason ec) 9355acaca2fSWilliam A. Kennington III { 9365acaca2fSWilliam A. Kennington III switch (ec) 9375acaca2fSWilliam A. Kennington III { 9385acaca2fSWilliam A. Kennington III case LIBCR51SIGN_SUCCESS: 9395acaca2fSWilliam A. Kennington III return "Success"; 9405acaca2fSWilliam A. Kennington III case LIBCR51SIGN_ERROR_RUNTIME_FAILURE: 9415acaca2fSWilliam A. Kennington III return "Runtime Error Failure"; 9425acaca2fSWilliam A. Kennington III case LIBCR51SIGN_ERROR_UNSUPPORTED_DESCRIPTOR: 9435acaca2fSWilliam A. Kennington III return "Unsupported descriptor"; 9445acaca2fSWilliam A. Kennington III case LIBCR51SIGN_ERROR_INVALID_DESCRIPTOR: 9455acaca2fSWilliam A. Kennington III return "Invalid descriptor"; 9465acaca2fSWilliam A. Kennington III case LIBCR51SIGN_ERROR_INVALID_IMAGE_FAMILY: 9475acaca2fSWilliam A. Kennington III return "Invalid image family"; 9485acaca2fSWilliam A. Kennington III case LIBCR51SIGN_ERROR_IMAGE_TYPE_DISALLOWED: 9495acaca2fSWilliam A. Kennington III return "Image type disallowed"; 9505acaca2fSWilliam A. Kennington III case LIBCR51SIGN_ERROR_DEV_DOWNGRADE_DISALLOWED: 9515acaca2fSWilliam A. Kennington III return "Dev downgrade disallowed"; 9525acaca2fSWilliam A. Kennington III case LIBCR51SIGN_ERROR_UNTRUSTED_KEY: 9535acaca2fSWilliam A. Kennington III return "Untrusted key"; 9545acaca2fSWilliam A. Kennington III case LIBCR51SIGN_ERROR_INVALID_SIGNATURE: 9555acaca2fSWilliam A. Kennington III return "Invalid signature"; 9565acaca2fSWilliam A. Kennington III case LIBCR51SIGN_ERROR_INVALID_HASH: 9575acaca2fSWilliam A. Kennington III return "Invalid hash"; 9585acaca2fSWilliam A. Kennington III case LIBCR51SIGN_ERROR_INVALID_HASH_TYPE: 9595acaca2fSWilliam A. Kennington III return "Invalid hash type"; 9605acaca2fSWilliam A. Kennington III case LIBCR51SIGN_ERROR_INVALID_ARGUMENT: 9615acaca2fSWilliam A. Kennington III return "Invalid Argument"; 9625acaca2fSWilliam A. Kennington III case LIBCR51SIGN_ERROR_FAILED_TO_LOCATE_MAGIC: 9635acaca2fSWilliam A. Kennington III return "Failed to locate descriptor"; 9645acaca2fSWilliam A. Kennington III case LIBCR51SIGN_ERROR_INVALID_CONTEXT: 9655acaca2fSWilliam A. Kennington III return "Invalid context"; 9665acaca2fSWilliam A. Kennington III case LIBCR51SIGN_ERROR_INVALID_INTERFACE: 9675acaca2fSWilliam A. Kennington III return "Invalid interface"; 9685acaca2fSWilliam A. Kennington III case LIBCR51SIGN_ERROR_INVALID_SIG_SCHEME: 9695acaca2fSWilliam A. Kennington III return "Invalid signature scheme"; 9705acaca2fSWilliam A. Kennington III case LIBCR51SIGN_ERROR_INVALID_REGION_INPUT: 9715acaca2fSWilliam A. Kennington III return "Invalid image region input"; 9725acaca2fSWilliam A. Kennington III case LIBCR51SIGN_ERROR_INVALID_REGION_SIZE: 9735acaca2fSWilliam A. Kennington III return "Invalid image region size"; 9745acaca2fSWilliam A. Kennington III default: 9755acaca2fSWilliam A. Kennington III return "Unknown error"; 9765acaca2fSWilliam A. Kennington III } 9775acaca2fSWilliam A. Kennington III } 9785acaca2fSWilliam A. Kennington III 9795acaca2fSWilliam A. Kennington III #ifdef __cplusplus 9805acaca2fSWilliam A. Kennington III } // extern "C" 9815acaca2fSWilliam A. Kennington III #endif 982