I got a task to make a small image, encrypt it with the simpliest method and write a decrypt driver.
Making an image:
Code:
dd if=/dev/zero of=myFS bs=1024 count=60
mkfs.ext2 myFS
Encrypting with XOR:
Code:
int main()
{
std::fstream streamIn;
std::fstream streamOut;
const char key = 0b00001111;
streamIn.open("myFS", std::ios_base::binary | std::ios_base::in);
streamOut.open("newFS", std::ios_base::binary | std::ios_base::trunc | std::ios_base::out);
char b;
while (streamIn.read(&b, 1))
{
b ^= key;
streamOut.write(&b, 1);
}
streamIn.close();
streamOut.close();
return 0;
}
Then writing driver. It is based on device-mapper target and the main working function has this prototype:
Code:
static int sddm_target_map(struct dm_target *ti, struct bio *bio);
You see, I have a bio structure, and I think, that I should decrypt every byte it contains. What I have now:
Code:
struct bio_vec vec;
struct bvec_iter it;
unsigned int len;
char *addr;
unsigned int i;
bio_for_each_segment(vec, bio, it)
{
len = vec.bv_len;
addr = (char*)(page_address(vec.bv_page) + vec.bv_offset);
for (i = 0; i != len; ++i)
{
*(addr+i) ^= 0b00001111;
}
}
Of course, it doesn't work. Attempts to mount it finished with error:
Code:
mount: wrong fs type, bad option, bad superblock on /dev/mapper/mydevice
Also saw another algorithm:
Code:
bio_for_each_segment(vec, bio, it)
{
len = vec.bv_len;
addr = kmap_atomic(vec.bv_page);
pointer = (char *)(addr + vec.bv_offset);
for (i = 0; i != len; ++i)
{
*(pointer+i) ^= 0b00001111;
}
kunmap_atomic(addr);
}
But it goes with the same error.
How can I implement my plan? Thanks in advance!