File: add_key.c

package info (click to toggle)
zulucrypt 6.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 6,908 kB
  • sloc: ansic: 25,142; cpp: 23,281; makefile: 24; xml: 10; sh: 2
file content (252 lines) | stat: -rw-r--r-- 5,761 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
 *
 *  Copyright (c) 2011-2015
 *  name : Francis Banyikwa
 *  email: mhogomchungu@gmail.com
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "includes.h"
#include <libcryptsetup.h>
#include <fcntl.h>
#include <unistd.h>
#include "luks2_support.h"

typedef struct arguments{

	const char * key_0 ;
	size_t       key_0_len ;
	const char * key_1 ;
	size_t       key_1_len ;
	const char * options ;
	int ( *set_options )( int *,struct crypt_device *,const struct arguments * ) ;
}arguments ;

static int zuluExit( int st,struct crypt_device * cd )
{
	crypt_free( cd ) ;
	return st ;
}

static int _add_key( const char * device,const resolve_path_t * opts )
{
	struct crypt_device * cd ;

	const arguments * args = opts->args ;

	int key_slot ;

	if( zuluCryptVolumeIsNotLuks( device ) ){

		return 3 ;
	}
	if( crypt_init( &cd,device ) != 0 ){

		return 2 ;
	}
	if( crypt_load( cd,NULL,NULL ) != 0 ){

		return zuluExit( 2,cd ) ;
	}

	args->set_options( &key_slot,cd,args ) ;

	if( crypt_keyslot_add_by_passphrase( cd,
					     key_slot,
					     args->key_0,
					     args->key_0_len,
					     args->key_1,
					     args->key_1_len ) < 0 ){
		return zuluExit( 1,cd ) ;
	}else{
		return zuluExit( 0,cd ) ;
	}
}

int zuluCryptAddKey( const char * device,const char * existingkey,
		     size_t existingkey_size,const char * newkey,size_t newkey_size )
{
	return zuluCryptAddKey_0( device,
				  existingkey,
				  existingkey_size,
				  newkey,
				  newkey_size,
				  "-1.-1.argon2id.-1.-1.-1" ) ;
}

#ifdef CRYPT_LUKS2

static int _set_options( int * key_slot,struct crypt_device * cd,const arguments * args )
{
	stringList_t stl = StringListSplit( args->options,'.' ) ;

	size_t list_count = 0 ;

	char * const * list = NULL ;

	int r ;

	StringListStringArray_1( &list,&list_count,stl ) ;

	const char * pbkdf_type = NULL ;
	const char * max_memory = NULL ;
	const char * max_threads = NULL ;
	const char * time_cost = NULL ;
	const char * iterations = NULL ;
	const char * keySlot = NULL ;

	if( list_count >= 6 ){

		time_cost   = *( list + 0 ) ;
		iterations  = *( list + 1 ) ;
		pbkdf_type  = *( list + 2 ) ;
		max_memory  = *( list + 3 ) ;
		max_threads = *( list + 4 ) ;
		keySlot     = *( list + 5 ) ;
	}

	struct crypt_pbkdf_type pbkdf ;

	memset( &pbkdf,'\0',sizeof( pbkdf ) ) ;

#if SUPPORT_crypt_get_pbkdf_default
	/*
	 * added in cryptsetup 2.0.3
	 */
	memcpy( &pbkdf,crypt_get_pbkdf_default( CRYPT_LUKS2 ),sizeof( struct crypt_pbkdf_type ) ) ;
#else
	pbkdf.type             = CRYPT_KDF_ARGON2I ;
	pbkdf.max_memory_kb    = 1024 ;
	pbkdf.parallel_threads = 4 ;
	pbkdf.hash = "sha256" ;
#endif
	if( StringsAreNotEqual( pbkdf_type,"pbkdf2" ) ){

		if( max_memory && StringsAreNotEqual( max_memory,"-1" ) ){

			pbkdf.max_memory_kb = ( unsigned int ) StringConvertToInt( max_memory ) ;
		}

		if( max_threads && StringsAreNotEqual( max_threads,"-1" ) ){

			pbkdf.parallel_threads = ( unsigned int ) StringConvertToInt( max_threads ) ;
		}
	}else{
		pbkdf.max_memory_kb = 0 ;
		pbkdf.parallel_threads = 0 ;
	}

	if( time_cost && StringsAreNotEqual( time_cost,"-1" ) ){

		pbkdf.time_ms = (unsigned int) StringConvertToInt( time_cost ) ;
	}

	if( iterations && StringsAreNotEqual( iterations,"-1" ) ){

		pbkdf.time_ms = 0 ;
		pbkdf.iterations = (unsigned int) StringConvertToInt( iterations ) ;
		pbkdf.flags |= CRYPT_PBKDF_NO_BENCHMARK ;
	}

	if( pbkdf_type ){

		pbkdf.type = pbkdf_type ;
	}

	if( keySlot && StringsAreNotEqual( keySlot,"-1" ) ){

		*key_slot = (int)StringConvertToInt( keySlot ) ;
	}else{
		*key_slot = CRYPT_ANY_SLOT ;
	}

	r = crypt_set_pbkdf_type( cd,&pbkdf ) ;

	StringListDelete( &stl ) ;
	StringFree( list ) ;

	return r ;
}

#else

static int _set_options( int * key_slot,struct crypt_device * cd, const arguments * args )
{
	if( cd && args ){}

	stringList_t stl = StringListSplit( args->options,'.' ) ;

	size_t list_count = 0 ;

	char * const * list = NULL ;

	StringListStringArray_1( &list,&list_count,stl ) ;

	const char * keySlot ;

	if( list_count >= 6 ){

		keySlot = *( list + 5 ) ;

		if( keySlot && StringsAreNotEqual( keySlot,"-1" ) ){

			*key_slot = (int)StringConvertToInt( keySlot ) ;
		}else{
			*key_slot = CRYPT_ANY_SLOT ;
		}
	}else{
		*key_slot = CRYPT_ANY_SLOT ;
	}

	StringListDelete( &stl ) ;
	StringFree( list ) ;

	return 0 ;
}

#endif

int zuluCryptAddKey_0( const char * device,
		       const char * existingkey,
		       size_t existingkey_size,
		       const char * newkey,
		       size_t newkey_size,
		       const char * options )
{
	/*
	 * resolve_path_t is defined in includes.h
	 */
	resolve_path_t opts ;
	arguments args ;

	memset( &opts,'\0',sizeof( opts ) ) ;
	memset( &args,'\0',sizeof( args ) ) ;

	args.key_0        = existingkey ;
	args.key_0_len    = existingkey_size ;
	args.key_1        = newkey ;
	args.key_1_len    = newkey_size ;
	args.options      = options ;
	args.set_options  = _set_options ;

	opts.device       = device ;
	opts.args         = &args ;
	opts.open_mode    = O_RDWR ;
	opts.error_value  = 1 ;

	/*
	 * zuluCryptResolveDevicePath() is defined in resolve_path.c
	 */
	return zuluCryptResolveDevicePath( _add_key,&opts ) ;
}