"Fossies" - the Fresh Open Source Software Archive

Member "cryptsetup-2.1.0/lib/luks2/luks2_json_format.c" (7 Feb 2019, 10102 Bytes) of package /linux/misc/cryptsetup-2.1.0.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "luks2_json_format.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.0.6_vs_2.1.0.

    1 /*
    2  * LUKS - Linux Unified Key Setup v2, LUKS2 header format code
    3  *
    4  * Copyright (C) 2015-2019 Red Hat, Inc. All rights reserved.
    5  * Copyright (C) 2015-2019 Milan Broz
    6  *
    7  * This program is free software; you can redistribute it and/or
    8  * modify it under the terms of the GNU General Public License
    9  * as published by the Free Software Foundation; either version 2
   10  * of the License, or (at your option) any later version.
   11  *
   12  * This program is distributed in the hope that it will be useful,
   13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   15  * GNU General Public License for more details.
   16  *
   17  * You should have received a copy of the GNU General Public License
   18  * along with this program; if not, write to the Free Software
   19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
   20  */
   21 
   22 #include "luks2_internal.h"
   23 #include <uuid/uuid.h>
   24 #include <assert.h>
   25 
   26 struct area {
   27     uint64_t offset;
   28     uint64_t length;
   29 };
   30 
   31 static size_t get_area_size(size_t keylength)
   32 {
   33     //FIXME: calculate this properly, for now it is AF_split_sectors
   34     return size_round_up(keylength * 4000, 4096);
   35 }
   36 
   37 static size_t get_min_offset(struct luks2_hdr *hdr)
   38 {
   39     return 2 * hdr->hdr_size;
   40 }
   41 
   42 static size_t get_max_offset(struct crypt_device *cd)
   43 {
   44     return crypt_get_data_offset(cd) * SECTOR_SIZE;
   45 }
   46 
   47 int LUKS2_find_area_gap(struct crypt_device *cd, struct luks2_hdr *hdr,
   48             size_t keylength, uint64_t *area_offset, uint64_t *area_length)
   49 {
   50     struct area areas[LUKS2_KEYSLOTS_MAX], sorted_areas[LUKS2_KEYSLOTS_MAX] = {};
   51     int i, j, k, area_i;
   52     size_t offset, length;
   53 
   54     /* fill area offset + length table */
   55     for (i = 0; i < LUKS2_KEYSLOTS_MAX; i++) {
   56         if (!LUKS2_keyslot_area(hdr, i, &areas[i].offset, &areas[i].length))
   57             continue;
   58         areas[i].length = 0;
   59         areas[i].offset = 0;
   60     }
   61 
   62     /* sort table */
   63     k = 0; /* index in sorted table */
   64     for (i = 0; i < LUKS2_KEYSLOTS_MAX; i++) {
   65         offset = get_max_offset(cd) ?: UINT64_MAX;
   66         area_i = -1;
   67         /* search for the smallest offset in table */
   68         for (j = 0; j < LUKS2_KEYSLOTS_MAX; j++)
   69             if (areas[j].offset && areas[j].offset <= offset) {
   70                 area_i = j;
   71                 offset = areas[j].offset;
   72             }
   73 
   74         if (area_i >= 0) {
   75             sorted_areas[k].length = areas[area_i].length;
   76             sorted_areas[k].offset = areas[area_i].offset;
   77             areas[area_i].length = 0;
   78             areas[area_i].offset = 0;
   79             k++;
   80         }
   81     }
   82 
   83     /* search for the gap we can use */
   84     offset = get_min_offset(hdr);
   85     length = get_area_size(keylength);
   86     for (i = 0; i < LUKS2_KEYSLOTS_MAX; i++) {
   87         /* skip empty */
   88         if (sorted_areas[i].offset == 0 || sorted_areas[i].length == 0)
   89             continue;
   90 
   91         /* enough space before the used area */
   92         if ((offset < sorted_areas[i].offset) && ((offset + length) <= sorted_areas[i].offset))
   93             break;
   94 
   95         /* both offset and length are already aligned to 4096 bytes */
   96         offset = sorted_areas[i].offset + sorted_areas[i].length;
   97     }
   98 
   99     if (get_max_offset(cd) && (offset + length) > get_max_offset(cd)) {
  100         log_err(cd, _("No space for new keyslot."));
  101         return -EINVAL;
  102     }
  103 
  104     log_dbg(cd, "Found area %zu -> %zu", offset, length + offset);
  105 /*
  106     log_dbg("Area offset min: %zu, max %zu, slots max %u",
  107            get_min_offset(hdr), get_max_offset(cd), LUKS2_KEYSLOTS_MAX);
  108     for (i = 0; i < LUKS2_KEYSLOTS_MAX; i++)
  109         log_dbg("SLOT[%02i]: %-8" PRIu64 " -> %-8" PRIu64, i,
  110             sorted_areas[i].offset,
  111             sorted_areas[i].length + sorted_areas[i].offset);
  112 */
  113     *area_offset = offset;
  114     *area_length = length;
  115     return 0;
  116 }
  117 
  118 int LUKS2_check_metadata_area_size(uint64_t metadata_size)
  119 {
  120     /* see LUKS2_HDR2_OFFSETS */
  121     return (metadata_size != 0x004000 &&
  122         metadata_size != 0x008000 && metadata_size != 0x010000 &&
  123         metadata_size != 0x020000 && metadata_size != 0x040000 &&
  124         metadata_size != 0x080000 && metadata_size != 0x100000 &&
  125         metadata_size != 0x200000 && metadata_size != 0x400000);
  126 }
  127 
  128 int LUKS2_check_keyslots_area_size(uint64_t keyslots_size)
  129 {
  130     return (MISALIGNED_4K(keyslots_size) ||
  131         keyslots_size > LUKS2_MAX_KEYSLOTS_SIZE);
  132 }
  133 
  134 int LUKS2_generate_hdr(
  135     struct crypt_device *cd,
  136     struct luks2_hdr *hdr,
  137     const struct volume_key *vk,
  138     const char *cipherName,
  139     const char *cipherMode,
  140     const char *integrity,
  141     const char *uuid,
  142     unsigned int sector_size,  /* in bytes */
  143     uint64_t data_offset,      /* in bytes */
  144     uint64_t align_offset,     /* in bytes */
  145     uint64_t required_alignment,
  146     uint64_t metadata_size,
  147     uint64_t keyslots_size)
  148 {
  149     struct json_object *jobj_segment, *jobj_integrity, *jobj_keyslots, *jobj_segments, *jobj_config;
  150     char cipher[128];
  151     uuid_t partitionUuid;
  152     int digest;
  153 
  154     if (!metadata_size)
  155         metadata_size = LUKS2_HDR_16K_LEN;
  156     hdr->hdr_size = metadata_size;
  157 
  158     if (data_offset && data_offset < get_min_offset(hdr)) {
  159         log_err(cd, _("Requested data offset is too small."));
  160         return -EINVAL;
  161     }
  162 
  163     /* Increase keyslot size according to data offset */
  164     if (!keyslots_size && data_offset)
  165         keyslots_size = data_offset - get_min_offset(hdr);
  166 
  167     /* keyslots size has to be 4 KiB aligned */
  168     keyslots_size -= (keyslots_size % 4096);
  169 
  170     if (keyslots_size > LUKS2_MAX_KEYSLOTS_SIZE)
  171         keyslots_size = LUKS2_MAX_KEYSLOTS_SIZE;
  172 
  173     if (!keyslots_size) {
  174         assert(LUKS2_DEFAULT_HDR_SIZE > 2 * LUKS2_HDR_OFFSET_MAX);
  175         keyslots_size = LUKS2_DEFAULT_HDR_SIZE - get_min_offset(hdr);
  176     }
  177 
  178     /* Decrease keyslots_size if we have smaller data_offset */
  179     if (data_offset && (keyslots_size + get_min_offset(hdr)) > data_offset) {
  180         keyslots_size = data_offset - get_min_offset(hdr);
  181         log_dbg(cd, "Decreasing keyslot area size to %" PRIu64
  182             " bytes due to the requested data offset %"
  183             PRIu64 " bytes.", keyslots_size, data_offset);
  184     }
  185 
  186     /* Data offset has priority */
  187     if (!data_offset && required_alignment) {
  188         data_offset = size_round_up(get_min_offset(hdr) + keyslots_size,
  189                         (size_t)required_alignment);
  190         data_offset += align_offset;
  191     }
  192 
  193     log_dbg(cd, "Formatting LUKS2 with JSON metadata area %" PRIu64
  194         " bytes and keyslots area %" PRIu64 " bytes.",
  195         metadata_size - LUKS2_HDR_BIN_LEN, keyslots_size);
  196 
  197     if (keyslots_size < (LUKS2_HDR_OFFSET_MAX - 2*LUKS2_HDR_16K_LEN))
  198         log_std(cd, _("WARNING: keyslots area (%" PRIu64 " bytes) is very small,"
  199             " available LUKS2 keyslot count is very limited.\n"),
  200             keyslots_size);
  201 
  202     hdr->seqid = 1;
  203     hdr->version = 2;
  204     memset(hdr->label, 0, LUKS2_LABEL_L);
  205     strcpy(hdr->checksum_alg, "sha256");
  206     crypt_random_get(cd, (char*)hdr->salt1, LUKS2_SALT_L, CRYPT_RND_SALT);
  207     crypt_random_get(cd, (char*)hdr->salt2, LUKS2_SALT_L, CRYPT_RND_SALT);
  208 
  209     if (uuid && uuid_parse(uuid, partitionUuid) == -1) {
  210         log_err(cd, _("Wrong LUKS UUID format provided."));
  211         return -EINVAL;
  212     }
  213     if (!uuid)
  214         uuid_generate(partitionUuid);
  215 
  216     uuid_unparse(partitionUuid, hdr->uuid);
  217 
  218     if (*cipherMode != '\0')
  219         snprintf(cipher, sizeof(cipher), "%s-%s", cipherName, cipherMode);
  220     else
  221         snprintf(cipher, sizeof(cipher), "%s", cipherName);
  222 
  223     hdr->jobj = json_object_new_object();
  224 
  225     jobj_keyslots = json_object_new_object();
  226     json_object_object_add(hdr->jobj, "keyslots", jobj_keyslots);
  227     json_object_object_add(hdr->jobj, "tokens", json_object_new_object());
  228     jobj_segments = json_object_new_object();
  229     json_object_object_add(hdr->jobj, "segments", jobj_segments);
  230     json_object_object_add(hdr->jobj, "digests", json_object_new_object());
  231     jobj_config = json_object_new_object();
  232     json_object_object_add(hdr->jobj, "config", jobj_config);
  233 
  234     digest = LUKS2_digest_create(cd, "pbkdf2", hdr, vk);
  235     if (digest < 0) {
  236         json_object_put(hdr->jobj);
  237         hdr->jobj = NULL;
  238         return -EINVAL;
  239     }
  240 
  241     if (LUKS2_digest_segment_assign(cd, hdr, CRYPT_DEFAULT_SEGMENT, digest, 1, 0) < 0) {
  242         json_object_put(hdr->jobj);
  243         hdr->jobj = NULL;
  244         return -EINVAL;
  245     }
  246 
  247     jobj_segment = json_object_new_object();
  248     json_object_object_add(jobj_segment, "type", json_object_new_string("crypt"));
  249     json_object_object_add(jobj_segment, "offset", json_object_new_uint64(data_offset));
  250     json_object_object_add(jobj_segment, "iv_tweak", json_object_new_string("0"));
  251     json_object_object_add(jobj_segment, "size", json_object_new_string("dynamic"));
  252     json_object_object_add(jobj_segment, "encryption", json_object_new_string(cipher));
  253     json_object_object_add(jobj_segment, "sector_size", json_object_new_int(sector_size));
  254 
  255     if (integrity) {
  256         jobj_integrity = json_object_new_object();
  257         json_object_object_add(jobj_integrity, "type", json_object_new_string(integrity));
  258         json_object_object_add(jobj_integrity, "journal_encryption", json_object_new_string("none"));
  259         json_object_object_add(jobj_integrity, "journal_integrity", json_object_new_string("none"));
  260         json_object_object_add(jobj_segment, "integrity", jobj_integrity);
  261     }
  262 
  263     json_object_object_add_by_uint(jobj_segments, CRYPT_DEFAULT_SEGMENT, jobj_segment);
  264 
  265     json_object_object_add(jobj_config, "json_size", json_object_new_uint64(metadata_size - LUKS2_HDR_BIN_LEN));
  266     json_object_object_add(jobj_config, "keyslots_size", json_object_new_uint64(keyslots_size));
  267 
  268     JSON_DBG(cd, hdr->jobj, "Header JSON:");
  269     return 0;
  270 }
  271 
  272 int LUKS2_wipe_header_areas(struct crypt_device *cd,
  273     struct luks2_hdr *hdr)
  274 {
  275     int r;
  276     uint64_t offset, length;
  277     size_t wipe_block;
  278 
  279     /* Wipe complete header, keyslots and padding areas with zeroes. */
  280     offset = 0;
  281     length = LUKS2_get_data_offset(hdr) * SECTOR_SIZE;
  282     wipe_block = 1024 * 1024;
  283 
  284     if (LUKS2_hdr_validate(cd, hdr->jobj, hdr->hdr_size - LUKS2_HDR_BIN_LEN))
  285         return -EINVAL;
  286 
  287     /* On detached header wipe at least the first 4k */
  288     if (length == 0) {
  289         length = 4096;
  290         wipe_block = 4096;
  291     }
  292 
  293     log_dbg(cd, "Wiping LUKS areas (0x%06" PRIx64 " - 0x%06" PRIx64") with zeroes.",
  294         offset, length + offset);
  295 
  296     r = crypt_wipe_device(cd, crypt_metadata_device(cd), CRYPT_WIPE_ZERO,
  297                   offset, length, wipe_block, NULL, NULL);
  298     if (r < 0)
  299         return r;
  300 
  301     /* Wipe keyslot area */
  302     wipe_block = 1024 * 1024;
  303     offset = get_min_offset(hdr);
  304     length = LUKS2_keyslots_size(hdr->jobj);
  305 
  306     log_dbg(cd, "Wiping keyslots area (0x%06" PRIx64 " - 0x%06" PRIx64") with random data.",
  307         offset, length + offset);
  308 
  309     return crypt_wipe_device(cd, crypt_metadata_device(cd), CRYPT_WIPE_RANDOM,
  310                  offset, length, wipe_block, NULL, NULL);
  311 }