HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/cryptenroll/cryptenroll.c

56,899 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: bbbccfddeea8945adf1d58bc2c6019e76df3b539

Lines 1001–1332 of 1,332

                        return log_oom();
                c->tpm2_n_hash_pcr_values = arg_tpm2_n_hash_pcr_values;
        }
 
        return 0;
}
 
int enroll_now(
                const EnrollContext *c,
                struct crypt_device *cd,
                const struct iovec *volume_key,
                char **ret_recovery_key) {
 
        int slot, slot_to_wipe = -1, r;
 
        assert(c);
        assert(cd);
        assert(iovec_is_set(volume_key));
 
        switch (c->enroll_type) {
 
        case ENROLL_PASSWORD:
                return enroll_password(c, cd, volume_key);
 
        case ENROLL_RECOVERY:
                return enroll_recovery(c, cd, volume_key, ret_recovery_key);
 
        case ENROLL_PKCS11:
                return enroll_pkcs11(c, cd, volume_key);
 
        case ENROLL_FIDO2:
                return enroll_fido2(c, cd, volume_key);
 
        case ENROLL_TPM2:
                slot = enroll_tpm2(c, cd, volume_key, &slot_to_wipe);
                if (slot < 0)
                        return slot;
 
                if (slot_to_wipe >= 0) {
                        assert(slot != slot_to_wipe);
 
                        /* Updating the PIN on an existing enrollment: wipe just that one slot. This is an
                         * internal one-off wipe, unrelated to the user's wipe selection, so use a throwaway
                         * context referencing a single explicit slot. */
                        _cleanup_(enroll_context_done) EnrollContext wipe_ctx = ENROLL_CONTEXT_NULL;
                        wipe_ctx.wipe_slots = newdup(int, &slot_to_wipe, 1);
                        if (!wipe_ctx.wipe_slots)
                                return log_oom();
 
                        wipe_ctx.n_wipe_slots = 1;
 
                        r = wipe_slots(&wipe_ctx, cd, /* ret_wiped_slots= */ NULL, /* ret_n_wiped_slots= */ NULL);
                        if (r < 0)
                                return r;
                }
 
                return slot;
 
        default:
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Operation not implemented yet.");
        }
}
 
static void argon2id_parameters_init_autotune(Argon2IdParameters *ret_params) {
        assert(ret_params);
 
        unsigned lanes = ARGON2ID_PARAMETERS_DEFAULT.lanes;
        (void) cpus_online(&lanes);
        *ret_params = (Argon2IdParameters) {
                .lanes = lanes,
        };
}
 
static int argon2id_benchmark_once(
                const struct iovec *password,
                const struct iovec *salt,
                uint64_t memcost_bytes,
                uint32_t iterations,
                uint32_t lanes,
                usec_t *ret_elapsed) {
 
        assert(ret_elapsed);
 
        _cleanup_(iovec_done_erase) struct iovec result = {};
        Argon2IdParameters bp = {
                .memcost_bytes = memcost_bytes,
                .iterations = iterations,
                .lanes = lanes,
        };
 
        usec_t start = now(CLOCK_MONOTONIC);
        int r = kdf_argon2id_derive(password, salt, &bp, /* derive_size= */ 64, &result);
        *ret_elapsed = now(CLOCK_MONOTONIC) - start;
 
        return r;
}
 
static void argon2id_parameters_benchmark(Argon2IdParameters *p, usec_t target_time) {
        int r;
 
        assert(p);
        assert(target_time > 0);
 
        bool mem_fixed = p->memcost_bytes > 0;
        bool iter_fixed = p->iterations > 0;
 
        if (mem_fixed && iter_fixed)
                return;
 
        struct iovec password = IOVEC_MAKE_STRING("benchmark");
        struct iovec salt = IOVEC_MAKE_STRING("benchmark-salt");
 
        uint64_t target_ms = MAX(target_time / USEC_PER_MSEC, 1U);
 
        uint32_t iterations = iter_fixed ? p->iterations : 2;
        uint64_t memcost_bytes = mem_fixed ? p->memcost_bytes : ARGON2ID_BENCHMARK_MIN_MEMORY;
 
        uint64_t max_mem_bytes;
        if (mem_fixed)
                max_mem_bytes = memcost_bytes;
        else {
                max_mem_bytes = physical_memory_scale(1, 2);
                if (max_mem_bytes == 0 || max_mem_bytes == UINT64_MAX)
                        max_mem_bytes = ARGON2ID_PARAMETERS_DEFAULT.memcost_bytes;
                if (memcost_bytes > max_mem_bytes)
                        memcost_bytes = max_mem_bytes;
        }
 
        target_ms = MIN(target_ms, UINT64_MAX / MAX(1u, MAX(max_mem_bytes, (uint64_t) UINT32_MAX)));
 
        usec_t actual_elapsed = 0;
 
        for (;;) {
                usec_t elapsed;
                r = argon2id_benchmark_once(&password, &salt, memcost_bytes, iterations, p->lanes, &elapsed);
                log_debug("Benchmarking Argon2id with %"PRIu64" memory, %u iterations, %u lanes…",
                          memcost_bytes, iterations, p->lanes);
                if (r < 0) {
                        log_debug_errno(r, "Argon2id benchmark failed, using default parameters: %m");
                        *p = ARGON2ID_PARAMETERS_DEFAULT;
                        return;
                }
 
                actual_elapsed = elapsed;
 
                if (elapsed >= ARGON2ID_BENCHMARK_MIN_MS * USEC_PER_MSEC)
                        break;
 
                if (!mem_fixed && memcost_bytes < max_mem_bytes) {
                        uint64_t new_mem = MIN(memcost_bytes * 2, max_mem_bytes);
                        if (new_mem > memcost_bytes)
                                memcost_bytes = new_mem;
                        else
                                memcost_bytes = max_mem_bytes;
                } else if (!iter_fixed) {
                        uint32_t new_iter = MIN(2u * iterations, UINT32_MAX / 2u);
                        if (new_iter > iterations)
                                iterations = new_iter;
                        else
                                break;
                } else
                        break;
        }
 
        p->memcost_bytes = memcost_bytes;
 
        for (unsigned attempt = 0; attempt < ARGON2ID_BENCHMARK_MAX_ATTEMPTS; attempt++) {
                usec_t elapsed;
                r = argon2id_benchmark_once(&password, &salt, memcost_bytes, iterations, p->lanes, &elapsed);
                if (r < 0) {
                        log_debug_errno(r, "Argon2id fine-tuning failed, keeping coarse parameters: %m");
                        break;
                }
 
                actual_elapsed = elapsed;
 
                uint64_t ms = MAX(elapsed / USEC_PER_MSEC, 1U);
 
                uint64_t lower = target_ms * ARGON2ID_BENCHMARK_PERCENT_MIN / 100;
                uint64_t upper = target_ms * ARGON2ID_BENCHMARK_PERCENT_MAX / 100;
                if (ms >= lower && ms <= upper)
                        break;
 
                uint64_t new_mem = memcost_bytes;
                uint32_t new_iter = iterations;
 
                if (ms < target_ms) {
                        if (!mem_fixed) {
                                new_mem = MIN(memcost_bytes * target_ms / ms, max_mem_bytes);
                                if (new_mem >= max_mem_bytes && !iter_fixed)
                                        new_iter = (uint32_t) MIN(
                                                (uint64_t) iterations * target_ms / ms,
                                                UINT32_MAX);
                        } else if (!iter_fixed)
                                new_iter = (uint32_t) MIN(iterations * target_ms / ms, UINT32_MAX);
                } else {
                        if (!iter_fixed) {
                                new_iter = MAX((uint64_t) iterations * target_ms / ms, 2ULL);
                                if (new_iter <= 2 && !mem_fixed)
                                        new_mem = MAX(
                                                memcost_bytes * target_ms / ms,
                                                ARGON2ID_BENCHMARK_MIN_MEMORY);
                        } else if (!mem_fixed)
                                new_mem = MAX(
                                                memcost_bytes * target_ms / ms,
                                                ARGON2ID_BENCHMARK_MIN_MEMORY);
                }
 
                if (new_iter == iterations && new_mem == memcost_bytes)
                        break;
 
                iterations = new_iter;
                memcost_bytes = new_mem;
        }
 
        p->memcost_bytes = memcost_bytes;
        p->iterations = iterations;
 
        log_notice("Argon2id benchmark: %u iterations, %"PRIu64" MiB, %u lanes, ~%"PRIu64"ms.",
                   p->iterations, p->memcost_bytes / 1024 / 1024, p->lanes,
                   actual_elapsed > 0 ? actual_elapsed / USEC_PER_MSEC : target_ms);
}
 
static int run(int argc, char *argv[]) {
        _cleanup_(crypt_freep) struct crypt_device *cd = NULL;
        _cleanup_(iovec_done_erase) struct iovec vk = {};
        _cleanup_(enroll_context_done) EnrollContext c = ENROLL_CONTEXT_NULL;
        int slot, r;
 
        LIBCRYPTO_NOTE(suggested);
        LIBCRYPTSETUP_NOTE(required);
        LIBFIDO2_NOTE(suggested);
        LIBP11KIT_NOTE(suggested);
        LIBQRENCODE_NOTE(suggested);
        PASSWORD_NOTE(suggested);
        TPM2_NOTE(suggested);
 
        log_setup();
 
        /* A delicious drop of snake oil */
        (void) safe_mlockall(MCL_CURRENT|MCL_FUTURE|MCL_ONFAULT);
 
        /* If invoked as a Varlink service, hand off to the Varlink server and don't process the command
         * line any further. */
        r = sd_varlink_invocation(SD_VARLINK_ALLOW_ACCEPT);
        if (r < 0)
                return log_error_errno(r, "Failed to check if invoked in Varlink mode: %m");
        if (r > 0)
                return cryptenroll_varlink_server();
 
        argon2id_parameters_init_autotune(&arg_tpm2_argon2id_params);
 
        r = parse_argv(argc, argv);
        if (r <= 0)
                return r;
 
        r = dlopen_cryptsetup(LOG_ERR);
        if (r < 0)
                return r;
 
        r = enroll_context_from_args(&c);
        if (r < 0)
                return r;
 
        _cleanup_(sd_varlink_flush_close_unrefp) sd_varlink *mute_console_link = NULL;
 
        /* Ensure the interactive chrome (drawn by cryptenroll_run_interactive() in --firstboot mode) is
         * always torn down on exit; chrome_hide() is a no-op if no chrome was shown. */
        DEFER_VOID_CALL(chrome_hide);
 
        if (arg_firstboot) {
                assert(c.enroll_type < 0);
 
                r = cryptenroll_run_interactive(
                                &c,
                                arg_prompt_suppress_mask,
                                arg_chrome,
                                arg_mute_console ? &mute_console_link : NULL);
                if (r <= 0)
                        return r;
 
                assert(c.enroll_type >= 0);
 
        } else if (c.enroll_type < 0) {
                /* If we are called without anything to enroll, we just need the LUKS device, not the volume key. */
                r = prepare_luks(&c, &cd, /* ret_volume_key= */ NULL);
                if (r < 0)
                        return r;
 
                /* List enrolled slots if we are called without anything to enroll or wipe */
                if (!wipe_requested())
                        return list_enrolled(cd);
 
                /* Only slot wiping selected */
                return wipe_slots(&c, cd, /* ret_wiped_slots= */ NULL, /* ret_n_wiped_slots= */ NULL);
        }
 
        r = prepare_luks(&c, &cd, &vk);
        if (r < 0)
                goto finish;
 
        /* Benchmark Argon2id parameters before TPM2 enrollment with PIN */
        if (c.enroll_type == ENROLL_TPM2 && c.tpm2_pin == TPM2_WITH_PIN_YES)
                argon2id_parameters_benchmark(
                                &c.tpm2_argon2id_params,
                                c.tpm2_argon2id_iter_time > 0
                                        ? c.tpm2_argon2id_iter_time
                                        : (usec_t) ARGON2ID_BENCHMARK_DEFAULT_TARGET_MS * USEC_PER_MSEC);
 
        slot = enroll_now(&c, cd, &vk, /* ret_recovery_key= */ NULL);
        if (slot < 0) {
                r = slot;
                goto finish;
        }
 
        /* After we completed enrolling, remove user selected slots (keeping the one we just added) */
        c.wipe_except_slot = slot;
        r = wipe_slots(&c, cd, /* ret_wiped_slots= */ NULL, /* ret_n_wiped_slots= */ NULL);
        if (r < 0)
                goto finish;
 
        r = 0;
 
finish:
        if (arg_firstboot)
                (void) any_key_to_proceed();
 
        return r;
}
 
DEFINE_MAIN_FUNCTION(run);