zuluCrypt  5.7.1
About: zuluCrypt is a simple but feature rich solution for hard drives encryption. It can manage PLAIN dm-crypt, LUKS, TrueCrypt and VeraCrypt encrypted volumes.
  Fossies Dox: zuluCrypt-5.7.1.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

add_key.c
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2011-2015
4  * name : Francis Banyikwa
5  * email: mhogomchungu@gmail.com
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
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.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "includes.h"
21 #include <libcryptsetup.h>
22 #include <fcntl.h>
23 #include <unistd.h>
24 
25 typedef struct{
26 
27  const char * key_0 ;
28  size_t key_0_len ;
29  const char * key_1 ;
30  size_t key_1_len ;
31 
32 }arguments ;
33 
34 static int zuluExit( int st,struct crypt_device * cd )
35 {
36  crypt_free( cd ) ;
37  return st ;
38 }
39 
40 static int _add_key( const char * device,const resolve_path_t * opts )
41 {
42  struct crypt_device * cd ;
43 
44  const arguments * args = opts->args ;
45 
46  if( zuluCryptVolumeIsNotLuks( device ) ){
47 
48  return 3 ;
49  }
50  if( crypt_init( &cd,device ) != 0 ){
51 
52  return 2 ;
53  }
54  if( crypt_load( cd,NULL,NULL ) != 0 ){
55 
56  return zuluExit( 2,cd ) ;
57  }
58  if( crypt_keyslot_add_by_passphrase( cd,CRYPT_ANY_SLOT,args->key_0,
59 
60  args->key_0_len,args->key_1,args->key_1_len ) < 0 ){
61  return zuluExit( 1,cd ) ;
62  }else{
63  return zuluExit( 0,cd ) ;
64  }
65 }
66 
67 int zuluCryptAddKey( const char * device,const char * existingkey,
68  size_t existingkey_size,const char * newkey,size_t newkey_size )
69 {
70  /*
71  * resolve_path_t is defined in includes.h
72  */
73  resolve_path_t opts ;
74  arguments args ;
75 
76  memset( &opts,'\0',sizeof( opts ) ) ;
77  memset( &args,'\0',sizeof( args ) ) ;
78 
79  args.key_0 = existingkey ;
80  args.key_0_len = existingkey_size ;
81  args.key_1 = newkey ;
82  args.key_1_len = newkey_size ;
83 
84  opts.device = device ;
85  opts.args = &args ;
86  opts.open_mode = O_RDWR ;
87  opts.error_value = 1 ;
88 
89  /*
90  * zuluCryptResolveDevicePath() is defined in resolve_path.c
91  */
92  return zuluCryptResolveDevicePath( _add_key,&opts ) ;
93 }
zuluCryptResolveDevicePath
int zuluCryptResolveDevicePath(int(*)(const char *, const resolve_path_t *), const resolve_path_t *)
Definition: resolve_paths.c:389
arguments::key_1_len
size_t key_1_len
Definition: add_key.c:30
arguments
Definition: add_key.c:25
arguments::key_1
const char * key_1
Definition: add_key.c:29
resolve_path_t::args
const void * args
Definition: includes.h:119
zuluCryptAddKey
int zuluCryptAddKey(const char *device, const char *existingkey, size_t existingkey_size, const char *newkey, size_t newkey_size)
Definition: add_key.c:67
arguments
struct arguments arguments
args
Definition: open_plain.c:25
resolve_path_t::error_value
int error_value
Definition: includes.h:116
zuluCryptVolumeIsNotLuks
int zuluCryptVolumeIsNotLuks(const char *dev)
Definition: is_luks.c:63
resolve_path_t::device
const char * device
Definition: includes.h:115
arguments::key_0_len
size_t key_0_len
Definition: add_key.c:28
arguments::key_0
const char * key_0
Definition: add_key.c:27
includes.h
resolve_path_t::open_mode
int open_mode
Definition: includes.h:118
_add_key
static int _add_key(const char *device, const resolve_path_t *opts)
Definition: add_key.c:40
resolve_path_t
Definition: includes.h:114
zuluExit
static int zuluExit(int st, struct crypt_device *cd)
Definition: add_key.c:34