Merge branch '2019-10-28-azure-ci-support'
[oweals/u-boot.git] / common / image-sig.c
index 455f2b96294985af5fb7fb9011bb45e2bc8b33b3..639a1124504f5c0450e950912e98e8f5c1023fa0 100644 (file)
@@ -1,7 +1,6 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * Copyright (c) 2013, Google Inc.
- *
- * SPDX-License-Identifier:    GPL-2.0+
  */
 
 #ifdef USE_HOSTCC
@@ -32,51 +31,79 @@ void *image_get_host_blob(void)
 
 struct checksum_algo checksum_algos[] = {
        {
-               "sha1",
-               SHA1_SUM_LEN,
-               SHA1_DER_LEN,
-               sha1_der_prefix,
+               .name = "sha1",
+               .checksum_len = SHA1_SUM_LEN,
+               .der_len = SHA1_DER_LEN,
+               .der_prefix = sha1_der_prefix,
 #if IMAGE_ENABLE_SIGN
-               EVP_sha1,
+               .calculate_sign = EVP_sha1,
 #endif
-               hash_calculate,
+               .calculate = hash_calculate,
        },
        {
-               "sha256",
-               SHA256_SUM_LEN,
-               SHA256_DER_LEN,
-               sha256_der_prefix,
+               .name = "sha256",
+               .checksum_len = SHA256_SUM_LEN,
+               .der_len = SHA256_DER_LEN,
+               .der_prefix = sha256_der_prefix,
 #if IMAGE_ENABLE_SIGN
-               EVP_sha256,
+               .calculate_sign = EVP_sha256,
 #endif
-               hash_calculate,
+               .calculate = hash_calculate,
        }
 
 };
 
 struct crypto_algo crypto_algos[] = {
        {
-               "rsa2048",
-               RSA2048_BYTES,
-               rsa_sign,
-               rsa_add_verify_data,
-               rsa_verify,
+               .name = "rsa2048",
+               .key_len = RSA2048_BYTES,
+               .sign = rsa_sign,
+               .add_verify_data = rsa_add_verify_data,
+               .verify = rsa_verify,
        },
        {
-               "rsa4096",
-               RSA4096_BYTES,
-               rsa_sign,
-               rsa_add_verify_data,
-               rsa_verify,
+               .name = "rsa4096",
+               .key_len = RSA4096_BYTES,
+               .sign = rsa_sign,
+               .add_verify_data = rsa_add_verify_data,
+               .verify = rsa_verify,
        }
 
 };
 
+struct padding_algo padding_algos[] = {
+       {
+               .name = "pkcs-1.5",
+               .verify = padding_pkcs_15_verify,
+       },
+#ifdef CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT
+       {
+               .name = "pss",
+               .verify = padding_pss_verify,
+       }
+#endif /* CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT */
+};
+
 struct checksum_algo *image_get_checksum_algo(const char *full_name)
 {
        int i;
        const char *name;
 
+#if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC)
+       static bool done;
+
+       if (!done) {
+               done = true;
+               for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) {
+                       checksum_algos[i].name += gd->reloc_off;
+#if IMAGE_ENABLE_SIGN
+                       checksum_algos[i].calculate_sign += gd->reloc_off;
+#endif
+                       checksum_algos[i].calculate += gd->reloc_off;
+               }
+       }
+#endif
+
        for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) {
                name = checksum_algos[i].name;
                /* Make sure names match and next char is a comma */
@@ -93,6 +120,20 @@ struct crypto_algo *image_get_crypto_algo(const char *full_name)
        int i;
        const char *name;
 
+#if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC)
+       static bool done;
+
+       if (!done) {
+               done = true;
+               for (i = 0; i < ARRAY_SIZE(crypto_algos); i++) {
+                       crypto_algos[i].name += gd->reloc_off;
+                       crypto_algos[i].sign += gd->reloc_off;
+                       crypto_algos[i].add_verify_data += gd->reloc_off;
+                       crypto_algos[i].verify += gd->reloc_off;
+               }
+       }
+#endif
+
        /* Move name to after the comma */
        name = strchr(full_name, ',');
        if (!name)
@@ -107,6 +148,21 @@ struct crypto_algo *image_get_crypto_algo(const char *full_name)
        return NULL;
 }
 
+struct padding_algo *image_get_padding_algo(const char *name)
+{
+       int i;
+
+       if (!name)
+               return NULL;
+
+       for (i = 0; i < ARRAY_SIZE(padding_algos); i++) {
+               if (!strcmp(padding_algos[i].name, name))
+                       return &padding_algos[i];
+       }
+
+       return NULL;
+}
+
 /**
  * fit_region_make_list() - Make a list of image regions
  *
@@ -156,11 +212,22 @@ static int fit_image_setup_verify(struct image_sign_info *info,
                char **err_msgp)
 {
        char *algo_name;
+       const char *padding_name;
+
+       if (fdt_totalsize(fit) > CONFIG_FIT_SIGNATURE_MAX_SIZE) {
+               *err_msgp = "Total size too large";
+               return 1;
+       }
 
        if (fit_image_hash_get_algo(fit, noffset, &algo_name)) {
                *err_msgp = "Can't get hash algo property";
                return -1;
        }
+
+       padding_name = fdt_getprop(fit, noffset, "padding", NULL);
+       if (!padding_name)
+               padding_name = RSA_DEFAULT_PADDING_NAME;
+
        memset(info, '\0', sizeof(*info));
        info->keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
        info->fit = (void *)fit;
@@ -168,11 +235,12 @@ static int fit_image_setup_verify(struct image_sign_info *info,
        info->name = algo_name;
        info->checksum = image_get_checksum_algo(algo_name);
        info->crypto = image_get_crypto_algo(algo_name);
+       info->padding = image_get_padding_algo(padding_name);
        info->fdt_blob = gd_fdt_blob();
        info->required_keynode = required_keynode;
        printf("%s:%s", algo_name, info->keyname);
 
-       if (!info->checksum || !info->crypto) {
+       if (!info->checksum || !info->crypto || !info->padding) {
                *err_msgp = "Unknown signature algorithm";
                return -1;
        }
@@ -330,6 +398,11 @@ int fit_config_check_sig(const void *fit, int noffset, int required_keynode,
                return -1;
        }
 
+       if (prop && prop_len > 0 && prop[prop_len - 1] != '\0') {
+               *err_msgp = "hashed-nodes property must be null-terminated";
+               return -1;
+       }
+
        /* Add a sanity check here since we are using the stack */
        if (count > IMAGE_MAX_HASHED_NODES) {
                *err_msgp = "Number of hashed nodes exceeds maximum";
@@ -347,7 +420,7 @@ int fit_config_check_sig(const void *fit, int noffset, int required_keynode,
 
        /*
         * Each node can generate one region for each sub-node. Allow for
-        * 7 sub-nodes (hash@1, signature@1, etc.) and some extra.
+        * 7 sub-nodes (hash-1, signature-1, etc.) and some extra.
         */
        max_regions = 20 + count * 7;
        struct fdt_region fdt_regions[max_regions];
@@ -373,8 +446,11 @@ int fit_config_check_sig(const void *fit, int noffset, int required_keynode,
        /* Add the strings */
        strings = fdt_getprop(fit, noffset, "hashed-strings", NULL);
        if (strings) {
-               fdt_regions[count].offset = fdt_off_dt_strings(fit) +
-                               fdt32_to_cpu(strings[0]);
+               /*
+                * The strings region offset must be a static 0x0.
+                * This is set in tool/image-host.c
+                */
+               fdt_regions[count].offset = fdt_off_dt_strings(fit);
                fdt_regions[count].size = fdt32_to_cpu(strings[1]);
                count++;
        }