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_plain.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 * rng ;
28  const char * algo ;
29  const char * cipher ;
30  const char * keySize ;
31  const char * hash ;
32  const char * offset ;
33 }args ;
34 
35 static int zuluExit( int st,struct crypt_device * cd )
36 {
37  crypt_free( cd ) ;
38  return st ;
39 }
40 
41 static const char * _remove_letters( char * e )
42 {
43  char * m = e ;
44 
45  while( *e ){
46 
47  if( !( *e >= '0' && *e <= '9' ) ){
48 
49  *e = '\0' ;
50  break ;
51  }
52 
53  e++ ;
54  }
55 
56  return m ;
57 }
58 
59 static u_int64_t _offset( const char * offset )
60 {
61  char * e ;
62 
63  u_int64_t r = 0 ;
64 
65  size_t s = StringSize( offset ) ;
66 
67  if( s == 0 ){
68 
69  return 0 ;
70  }else{
71  e = StringCopy_2( offset ) ;
72 
73  if( StringEndsWithAtLeastOne( e,"m","MB","mb","Mb","M",NULL ) ){
74 
76  r = 2 * 1024 * r ;
77 
78  }else if( StringEndsWithAtLeastOne( e,"g","GB","gb","Gb","G",NULL ) ){
79 
81  r = 2 * 1024 * 1024 * r ;
82 
83  }else if( StringEndsWithAtLeastOne( e,"t","TB","tb","Tb","T",NULL ) ){
84 
86  r = 1.0 * 2 * 1024 * 1024 * 1024 * r ;
87 
88  }else if( StringEndsWithAtLeastOne( e,"k","KB","kb","Kb","K",NULL ) ){
89 
91  r = 2 * r ;
92 
93  }else if( StringEndsWithAtLeastOne( e,"b","B",NULL ) ){
94 
96 
97  if( r < 512 ){
98  r = 0 ;
99  }else{
100  r = r / 512 ;
101  }
102  }else {
103  r = StringConvertToInt( offset ) ;
104  }
105 
106  StringFree( e ) ;
107  }
108 
109  return r ;
110 }
111 
112 static int _open_plain( const char * device,const resolve_path_t * opts )
113 {
114  uint32_t flags ;
115 
116  struct crypt_device * cd ;
117  struct crypt_params_plain params ;
118 
119  size_t size ;
120  /*
121  * open_struct_t is defined in includes.h
122  */
123  const open_struct_t * opt = opts->args ;
124 
125  const args * e = opt->variables ;
126 
127  memset( &params,'\0',sizeof( struct crypt_params_plain ) ) ;
128 
129  params.hash = e->hash ;
130 
131  if( zuluCryptPathIsNotValid( device ) ){
132 
133  return 3 ;
134  }
135  if( crypt_init( &cd,device ) != 0 ){
136 
137  return 2 ;
138  }
139 
140  params.offset = _offset( e->offset ) ;
141 
142  if( opts->open_mode == O_RDONLY ){
143 
144  flags = CRYPT_ACTIVATE_READONLY ;
145  }else{
146  flags = CRYPT_ACTIVATE_ALLOW_DISCARDS ;
147  }
148 
149  size = ( size_t ) StringConvertToInt( e->keySize ) / 8 ;
150 
151  if( crypt_format( cd,CRYPT_PLAIN,e->algo,e->cipher,NULL,NULL,size,&params ) != 0 ){
152 
153  return zuluExit( 2,cd ) ;
154  }
155  if( crypt_activate_by_passphrase( cd,opt->mapper_name,CRYPT_ANY_SLOT,
156  opt->key,opt->key_len,flags ) < 0 ){
157 
158  return zuluExit( 2,cd ) ;
159  }else{
160  return zuluExit( 0,cd ) ;
161  }
162 }
163 
164 int zuluCryptOpenPlain_2( const char * device,const char * mapper,
165  const char * mode,const char * key,size_t key_len,
166  const char * options )
167 {
168  open_struct_t opt ;
169 
170  stringList_t stl ;
171 
172  int r ;
173 
174  char * const * e = NULL ;
175  size_t n = 0 ;
176 
177  args s = { "/dev/urandom","aes","cbc-essiv:sha256","256","ripemd160","0" } ;
178 
179  memset( &opt,'\0',sizeof( open_struct_t ) ) ;
180 
181  opt.device = device ;
182  opt.mapper_name = mapper ;
183  opt.key = key ;
184  opt.key_len = key_len ;
185  opt.m_opts = mode ;
186  opt.variables = &s ;
187 
188  stl = StringListSplit( options,'.' ) ;
189 
190  StringListStringArray_1( &e,&n,stl ) ;
191 
192  if( n == 6 ){
193 
194  s.rng = *( e + 0 ) ;
195  s.algo = *( e + 1 ) ;
196  s.cipher = *( e + 2 ) ;
197  s.keySize = *( e + 3 ) ;
198  s.hash = *( e + 4 ) ;
199  s.offset = *( e + 5 ) ;
200 
201  }else if( n == 5 ){
202 
203  s.algo = *( e + 0 ) ;
204  s.cipher = *( e + 1 ) ;
205  s.keySize = *( e + 2 ) ;
206  s.hash = *( e + 3 ) ;
207  s.offset = *( e + 4 ) ;
208  }
209 
210  /*
211  * zuluCryptResolveDevicePath_0() is defined in resolve_path.c
212  */
214 
215  StringListDelete( &stl ) ;
216 
217  StringFree( e ) ;
218 
219  return r ;
220 }
221 
222 int zuluCryptOpenPlain( const char * device,const char * mapper,
223  const char * mode,const char * key,size_t key_len )
224 {
225  return zuluCryptOpenPlain_2( device,
226  mapper,
227  mode,
228  key,
229  key_len,
230  "/dev/urandom.aes.cbc-essiv:sha256.256.ripemd160.0" ) ;
231 }
232 
234 {
235  return zuluCryptOpenPlain_2( opt->device,
236  opt->mapper_name,
237  opt->m_opts,
238  opt->key,
239  opt->key_len,
240  opt->plain_dm_properties ) ;
241 }
open_struct_t::mapper_name
const char * mapper_name
Definition: includes.h:71
zuluCryptOpenPlain_1
int zuluCryptOpenPlain_1(const open_struct_t *opt)
Definition: open_plain.c:233
open_struct_t
Definition: includes.h:68
args::hash
const char * hash
Definition: open_plain.c:31
_open_plain
static int _open_plain(const char *device, const resolve_path_t *opts)
Definition: open_plain.c:112
open_struct_t::plain_dm_properties
const char * plain_dm_properties
Definition: includes.h:70
args::algo
const char * algo
Definition: open_plain.c:28
args::offset
const char * offset
Definition: open_plain.c:32
StringEndsWithAtLeastOne
int StringEndsWithAtLeastOne(const char *e,...)
Definition: String.c:1104
resolve_path_t::args
const void * args
Definition: includes.h:119
StringListDelete
void StringListDelete(stringList_t *stl)
Definition: StringList.c:1184
zuluExit
static int zuluExit(int st, struct crypt_device *cd)
Definition: open_plain.c:35
StringCopy_2
char * StringCopy_2(const char *str)
Definition: String.c:725
zuluCryptPathIsNotValid
int zuluCryptPathIsNotValid(const char *path)
Definition: is_path_valid.c:29
args::keySize
const char * keySize
Definition: open_plain.c:30
args
Definition: open_plain.c:25
StringSize
static __inline__ size_t StringSize(const char *str)
Definition: String.h:846
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
zuluCryptOpenPlain_2
int zuluCryptOpenPlain_2(const char *device, const char *mapper, const char *mode, const char *key, size_t key_len, const char *options)
Definition: open_plain.c:164
zuluCryptOpenPlain
int zuluCryptOpenPlain(const char *device, const char *mapper, const char *mode, const char *key, size_t key_len)
Definition: open_plain.c:222
_offset
static u_int64_t _offset(const char *offset)
Definition: open_plain.c:59
args::rng
const char * rng
Definition: open_plain.c:27
open_struct_t::key
const char * key
Definition: includes.h:73
StringListType
Definition: StringList.c:33
e
static QString e
Definition: about.cpp:31
args::cipher
const char * cipher
Definition: open_plain.c:29
StringListSplit
stringList_t StringListSplit(const char *cstring, char splitter)
Definition: StringList.c:428
includes.h
StringConvertToInt
u_int64_t StringConvertToInt(const char *s)
Definition: String.c:1416
resolve_path_t::open_mode
int open_mode
Definition: includes.h:118
_remove_letters
static const char * _remove_letters(char *e)
Definition: open_plain.c:41
open_struct_t::key_len
size_t key_len
Definition: includes.h:79
open_struct_t::variables
void * variables
Definition: includes.h:94
StringListStringArray_1
void StringListStringArray_1(char *const **buffer, size_t *size, stringList_t stl)
Definition: StringList.c:943
resolve_path_t
Definition: includes.h:114
open_struct_t::m_opts
const char * m_opts
Definition: includes.h:76
StringFree
static __inline__ void StringFree(const void *str)
Definition: String.h:823