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)  

open_luks.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 /*
26  * This header file is created by the build process and it checks if the version of cryptsetup >= 1.4.0
27  */
28 #include "luks_external_header.h"
29 
30 static int zuluExit( int st,struct crypt_device * cd )
31 {
32  crypt_free( cd ) ;
33  return st ;
34 }
35 
36 static int _open_luks_2( const char * device,const resolve_path_t * opt )
37 {
38  struct crypt_device * cd ;
39  uint32_t flags ;
40  int st ;
41 
42  /*
43  * open_struct_t is defined in includes.h
44  */
45  const open_struct_t * opts = opt->args ;
46 
47  if( zuluCryptPathIsNotValid( device ) ){
48 
49  return 3 ;
50  }
51  if( opts->luks_detached_header ){
52 #if LUKS_EXTERNAL_HEADER
53  if( crypt_init( &cd,opts->luks_detached_header ) != 0 ){
54 
55  return 1 ;
56  }
57 
58  if( crypt_load( cd,NULL,NULL ) != 0 ){
59 
60  return zuluExit( 2,cd ) ;
61  }
62 
63  if( crypt_set_data_device( cd,device ) != 0 ){
64 
65  return zuluExit( 1,cd ) ;
66  }
67 #else
68  return 1 ;
69 #endif
70  }else{
71  if( crypt_init( &cd,device ) != 0 ){
72 
73  return 2 ;
74  }
75 
76  if( crypt_load( cd,NULL,NULL ) != 0 ){
77 
78  return zuluExit( 2,cd ) ;
79  }
80  }
81 
82  if( opt->open_mode == O_RDONLY ){
83 
84  flags = CRYPT_ACTIVATE_READONLY ;
85  }else{
86  flags = CRYPT_ACTIVATE_ALLOW_DISCARDS ;
87  }
88 
89  st = crypt_activate_by_passphrase( cd,opts->mapper_name,CRYPT_ANY_SLOT,
90  opts->key,opts->key_len,flags ) ;
91 
92  if( st >= 0 ){
93 
94  return zuluExit( 0,cd ) ;
95 
96  }else if( st == -1 ){
97 
98  return zuluExit( 1,cd ) ;
99  }else{
100  return zuluExit( 2,cd ) ;
101  }
102 }
103 
104 int zuluCryptOpenLuks( const char * device,const char * mapper,
105  const char * mode,const char * key,size_t key_len )
106 {
107  open_struct_t opts ;
108 
109  memset( &opts,'\0',sizeof( open_struct_t ) ) ;
110 
111  opts.device = device ;
112  opts.mapper_name = mapper ;
113  opts.m_opts = mode ;
114  opts.key = key ;
115  opts.key_len = key_len;
116 
117  return zuluCryptOpenLuks_2( &opts ) ;
118 }
119 
121 {
122  return zuluCryptOpenLuks_2( opts ) ;
123 }
124 
126 {
127  /*
128  * zuluCryptResolveDevicePath_0() is defined in resolve_path.c
129  */
130  return zuluCryptResolveDevicePath_0( _open_luks_2,opts,2 ) ;
131 }
open_struct_t::mapper_name
const char * mapper_name
Definition: includes.h:71
open_struct_t
Definition: includes.h:68
resolve_path_t::args
const void * args
Definition: includes.h:119
zuluCryptOpenLuks_1
int zuluCryptOpenLuks_1(const open_struct_t *opts)
Definition: open_luks.c:120
_open_luks_2
static int _open_luks_2(const char *device, const resolve_path_t *opt)
Definition: open_luks.c:36
zuluCryptPathIsNotValid
int zuluCryptPathIsNotValid(const char *path)
Definition: is_path_valid.c:29
open_struct_t::device
const char * device
Definition: includes.h:69
zuluCryptResolveDevicePath_0
int zuluCryptResolveDevicePath_0(int(*)(const char *, const resolve_path_t *), const open_struct_t *, int)
Definition: resolve_paths.c:395
zuluCryptOpenLuks
int zuluCryptOpenLuks(const char *device, const char *mapper, const char *mode, const char *key, size_t key_len)
Definition: open_luks.c:104
open_struct_t::key
const char * key
Definition: includes.h:73
includes.h
resolve_path_t::open_mode
int open_mode
Definition: includes.h:118
open_struct_t::luks_detached_header
const char * luks_detached_header
Definition: includes.h:77
zuluExit
static int zuluExit(int st, struct crypt_device *cd)
Definition: open_luks.c:30
open_struct_t::key_len
size_t key_len
Definition: includes.h:79
zuluCryptOpenLuks_2
int zuluCryptOpenLuks_2(const open_struct_t *opts)
Definition: open_luks.c:125
resolve_path_t
Definition: includes.h:114
open_struct_t::m_opts
const char * m_opts
Definition: includes.h:76