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 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
|
/*
*
* Copyright (c) 2012-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 "../bin/includes.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <libcryptsetup.h>
#define SIZE 512
static void _write( int x,const void * y,size_t z )
{
if( write( x,y,z ) ){}
}
static void _read( int x,void * y,size_t z )
{
if( read( x,y,z ) ){}
}
/*
* routines for encrypting and decrypting stand alone files.
*
* Basic idea of the operation:
*
* basic concept:
*
* to create an encrypted file:
*
* create a "shell" file, attach a mapper to it and then copy the content of plain text file
* to the shell file through the mapper to create a cypher text file.
*
* to create a decrypted file:
*
* create a container file, attach a mapper to the container file and copy contents ofthe file to be encrypted to the container file through
* the mapper .
*
* The container file has a simple format.
* 1. The file will be a multiple of 512,if the plain text file has a file size that is not a multiple of 512,then
* encrypted file will be padded to the next file size of 512 mupltiple.
*
* 2. The encrypted file will have an addition size of 512 bytes added to the beginning of the encrypted file.This is the header
* of the encrypted file.
*
* 100 bytes of data are read from "/dev/urandom" and added at offset 100 and offset 200.These two 100 bytes data chunks will bo
* compared to check if the decryption key is the same as encrypting key.The offsett should math if thhe keys are the same.
*
* The first 100 bytes will contain the size of the load and the format should be a format atoll() can correctly undestand. ie
* the size must be a null terminated string of digits representing the size of the load.
*
*
* If for example, a 100 bytes file is to be encrypted,the encrypted file will have a file size of 1024 bytes. First, the 100 bytes
* will be padded to 512 and then 512 bytes will be added to store the size of the plain text file,useful when decrypting the file.
*
*/
static string_t crypt_mapper( const char * path,const char * key,u_int64_t key_len )
{
string_t p ;
char * mpath = realpath( path,NULL ) ;
if( mpath == NULL ){
return StringVoid ;
}
/*
* ZULUCRYPTshortMapperPath is set in ../constants.h
* zuluCryptCreateMapperName() is defined at ../lib/create_mapper_name.c
*/
p = zuluCryptCreateMapperName( mpath,strrchr( mpath,'/' ) + 1,0,ZULUCRYPTshortMapperPath ) ;
if( zuluCryptOpenPlain( mpath,StringContent( p ),"rw",key,key_len ) != 0 ){
StringDelete( &p ) ;
}else{
StringMultiplePrepend( p,"/",crypt_get_dir(),NULL ) ;
}
StringFree( mpath ) ;
return p ;
}
static int zuluExit( int st,int f_in,int f_out,string_t p )
{
if( f_out != -1 ){
close( f_out ) ;
}
close( f_in ) ;
zuluCryptCloseMapper( StringContent( p ) ) ;
StringDelete( &p ) ;
return st ;
}
/*
* function responsible for creating a decrypted file
*/
int zuluCryptDecryptFile( const char * source,const char * dest,const char * key,u_int64_t key_len )
{
struct stat st ;
char buffer[ SIZE ] ;
u_int64_t size ;
u_int64_t len ;
u_int64_t i ;
int f_in ;
int f_out = -1 ;
int test ;
/*
* attach a mapper to the file containing encrypted data
*/
string_t p = crypt_mapper( source,key,key_len ) ;
if( p == StringVoid ){
return 1 ;
}
f_in = open( StringContent( p ),O_RDONLY ) ;
/*
* 100 bytes from offset 100 and 100 bytes from offset 200 are supposed to be te same if
* the right key is used.
*/
_read( f_in,buffer,SIZE ) ;
if( memcmp( buffer + 100,buffer + 200,100 ) != 0 ){
return zuluExit( 2,f_in,f_out,p ) ;
}
/*
* get the size of encrypted data
*/
size = (size_t)atoll( buffer ) ;
/*
* Make sure the size of the encrypted file is with in expected range.
* It being out of range means either a wrong encrypted key was used or the encrypted file is corrupted.
*
* Because the padding can be up to 511 bytes and the header takes 512 bytes, the encrypted file will be
* larger and the different will be >= 512 and < 1024.
*/
stat( source,&st ) ;
test = (int)st.st_size - (int)size ;
if( test < SIZE || test >= ( SIZE * 2 ) ){
return zuluExit( 2,f_in,f_out,p ) ;
}
f_out = open( dest,O_WRONLY | O_CREAT,S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ) ;
if( size <= SIZE ){
_read( f_in,buffer,size ) ;
_write( f_out,buffer,size ) ;
}else{
len = size / SIZE ;
for( i = 0 ; i < len ; i++ ){
_read( f_in,buffer,SIZE ) ;
_write( f_out,buffer,SIZE ) ;
}
len = size - ( i * SIZE ) ;
_read( f_in,buffer,len ) ;
_write( f_out,buffer,len ) ;
}
return zuluExit( 0,f_in,f_out,p ) ;
}
/*
* function responsible for creating an encrypted file
*/
int zuluCryptEncryptFile( const char * source,const char * dest,const char * key,u_int64_t key_len )
{
string_t p ;
string_t q ;
char buffer[ SIZE ] ;
char r = '\0' ;
int f_in ;
int f_out ;
u_int64_t size ;
u_int64_t size_1 ;
const char * mapper ;
struct stat st ;
stat( source,&st ) ;
size = (size_t)st.st_size ;
/*
* make sure the encrypted file is a multiple of 512, important because data will be read/written in chunks of 512 bytes.
*/
while( size % SIZE != 0 ){
size++ ;
}
/*
* add 512 bytes to encrypted file, the exta space will be used to store the content size of the data to be encrypted.
*/
size += SIZE ;
memset( buffer,0,SIZE ) ;
f_out = open( dest,O_WRONLY | O_CREAT,S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ) ;
size_1 = 0 ;
/*
* create a file to be used to store encrypted data.
*/
while( 1 ){
_write( f_out,buffer,SIZE );
size_1 += SIZE ;
if( size_1 == size ){
break ;
}
}
close( f_out ) ;
/*
* attach a mapper to the file that will contain encrypted data
*/
p = crypt_mapper( dest,key,key_len ) ;
if( p == StringVoid ){
remove( dest ) ;
return 1 ;
}
mapper = StringContent( p ) ;
f_out = open( mapper,O_WRONLY ) ;
q = StringIntToString( (u_int64_t)st.st_size ) ;
/*
* write the size of plain text file to the encrypted file. This information is important when decrypting the data
* because it tells us how much padding was applied if any.
*
*/
_write( f_out,StringContent( q ),StringLength( q ) ) ;
_write( f_out,&r,1 ) ;
/*
* write the same 100 byte random data in two locations to be used to check the decrypting key during decryption. *
*/
f_in = open( "/dev/urandom",O_RDONLY ) ;
_read( f_in,buffer,100 ) ;
close( f_in ) ;
lseek( f_out,100,SEEK_SET ) ;
_write( f_out,buffer,100 ) ;
_write( f_out,buffer,100 ) ;
/*
* set the beginning of the payload,The cypher text will start at byte 512.
*/
lseek( f_out,SIZE,SEEK_SET ) ;
f_in = open( source,O_RDONLY ) ;
/*
* Copy over plain text data to the "shell" file through the mapper, creating an encrypted file.
*/
while( read( f_in,buffer,SIZE ) > 0 ){
_write( f_out,buffer,SIZE ) ;
}
close( f_in ) ;
close( f_out ) ;
zuluCryptCloseMapper( mapper ) ;
StringMultipleDelete( &q,&p,NULL ) ;
return 0 ;
}
|