2 * cryptsetup volume key implementation
4 * Copyright (C) 2004-2006, Clemens Fruhwirth <clemens@endorphin.org>
5 * Copyright (C) 2010-2012, Red Hat, Inc. All rights reserved.
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.
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.
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.
27 struct volume_key *crypt_alloc_volume_key(unsigned keylength, const char *key)
29 struct volume_key *vk = malloc(sizeof(*vk) + keylength);
34 vk->keylength = keylength;
36 memcpy(&vk->key, key, keylength);
38 memset(&vk->key, 0, keylength);
43 void crypt_free_volume_key(struct volume_key *vk)
46 memset(vk->key, 0, vk->keylength);
52 struct volume_key *crypt_generate_volume_key(struct crypt_device *cd, unsigned keylength)
55 struct volume_key *vk;
57 vk = crypt_alloc_volume_key(keylength, NULL);
61 r = crypt_random_get(cd, vk->key, keylength, CRYPT_RND_KEY);
63 crypt_free_volume_key(vk);