1336252Sian/*-
2336252Sian * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3336252Sian *
4336252Sian * Copyright (c) 2018 Ian Lepore <ian@FreeBSD.org>
5336252Sian *
6336252Sian * Redistribution and use in source and binary forms, with or without
7336252Sian * modification, are permitted provided that the following conditions
8336252Sian * are met:
9336252Sian * 1. Redistributions of source code must retain the above copyright
10336252Sian *    notice, this list of conditions and the following disclaimer.
11336252Sian * 2. Redistributions in binary form must reproduce the above copyright
12336252Sian *    notice, this list of conditions and the following disclaimer in the
13336252Sian *    documentation and/or other materials provided with the distribution.
14336252Sian *
15336252Sian * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16336252Sian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17336252Sian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18336252Sian * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19336252Sian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20336252Sian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21336252Sian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22336252Sian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23336252Sian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24336252Sian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25336252Sian * SUCH DAMAGE.
26336252Sian *
27336252Sian * $FreeBSD: stable/11/stand/libsa/geli/geli_metadata.c 344399 2019-02-20 23:55:35Z kevans $
28336252Sian */
29336252Sian
30336252Sian#include <stand.h>
31336252Sian#include <bootstrap.h>
32336252Sian#include <sys/param.h>
33336252Sian#include <sys/linker.h>
34336252Sian#include "geliboot.h"
35336252Sian
36336252Sian/*
37336252Sian * Export a keybuf as metadata attached to a kernel module.  This is separate
38336252Sian * from the lower-level key management functions to avoid creating a linker
39336252Sian * dependency on the libsa metadata routines when the geli code is linked into
40336252Sian * early-stage bootloaders such as gptboot.  Only loader(8) variants call this.
41336252Sian */
42336252Sianvoid
43336252Siangeli_export_key_metadata(struct preloaded_file *kfp)
44336252Sian{
45336252Sian    struct keybuf *keybuf;
46336252Sian
47336252Sian    keybuf = malloc(GELI_KEYBUF_SIZE);
48336252Sian    geli_export_key_buffer(keybuf);
49336252Sian    file_addmetadata(kfp, MODINFOMD_KEYBUF, GELI_KEYBUF_SIZE, keybuf);
50336252Sian    explicit_bzero(keybuf, GELI_KEYBUF_SIZE);
51336252Sian    free(keybuf);
52336252Sian}
53