2 * cryptsetup volume key implementation
4 * Copyright (C) 2004-2006, Clemens Fruhwirth <clemens@endorphin.org>
5 * Copyright (C) 2010-2011, 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 * version 2 as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 struct volume_key *crypt_alloc_volume_key(unsigned keylength, const char *key)
28 struct volume_key *vk = malloc(sizeof(*vk) + keylength);
33 vk->keylength = keylength;
35 memcpy(&vk->key, key, keylength);
40 void crypt_free_volume_key(struct volume_key *vk)
43 memset(vk->key, 0, vk->keylength);
49 struct volume_key *crypt_generate_volume_key(struct crypt_device *cd, unsigned keylength)
52 struct volume_key *vk;
54 vk = crypt_alloc_volume_key(keylength, NULL);
58 r = crypt_random_get(cd, vk->key, keylength, CRYPT_RND_KEY);
60 crypt_free_volume_key(vk);