Eet data cipher/decipher example

In this example, we exemplify the usage of eet_write_cipher() and eet_read_cipher().

For it to work, make sure to have your Eet installation with a ciphering backend enabled.

We start by defining the information to record in an Eet file (buffer), the key to cipher that (key) and a dummy wrong key to try to access that information, later (key_bad).

const char *buffer = "Here is a string of data to save !";
const char *key = "This is a crypto key";
const char *key_bad = "This is another crypto key";

After opening our file, we simply use the first cited function to write our string ciphered:

if (!ef)
{
fprintf(
stderr, "ERROR: could not access file (%s).\n", file);
goto error;
}
if (!eet_write_cipher(ef, "keys/tests", buffer, strlen(buffer) + 1, 0, key))
{
fprintf(
stderr, "ERROR: could not access file (%s).\n", file);
goto error;
}
eet_close(ef);

Then, after closing it on purpose, we open it again, to retrieve the encrypted information back, in a readable format:

if (!ef)
{
fprintf(
stderr, "ERROR: could not access file (%s).\n", file);
goto error;
}
test = eet_read_cipher(ef, "keys/tests", &size, key);
if (!test)
{
fprintf(
stderr, "ERROR: could decript contents on file %s, with key %s.\n",
file, key);
goto error;
}
if (size != (int)strlen(buffer) + 1)
{
fprintf(
stderr, "ERROR: something is wrong with the decripted data\n");
goto error;
}
if (memcmp(test, buffer, strlen(buffer) + 1) != 0)
{
fprintf(
stderr, "ERROR: something is wrong with the decripted data\n");
goto error;
}
eet_close(ef);
/* Decrypt an eet file, now using our BAD key!! */
if (!ef)
{
fprintf(
stderr, "ERROR: could not access file (%s).\n", file);
goto error;
}
test = eet_read_cipher(ef, "keys/tests", &size, key_bad);
if (size == (int)strlen(buffer) + 1)
if (memcmp(test, buffer, strlen(buffer) + 1) == 0)
{
fprintf(
stderr, "ERROR: something is wrong with the contents of %s, as"
" we accessed it with a different key and it decripted our"
" information right.\n", file);
goto error;
}
eet_close(ef);

Note that we do it twice, being the last time with the wrong key. In this last case, if the information is read back and matches the original buffer, something wrong is going on (we made it to fail on purpose). The former access is OK, and must work.

What we do in sequence is just to delete the file. The complete code of the example follows.

1 //Compile with:
2 // gcc -o eet-data-cipher_decipher eet-data-cipher_decipher.c `pkg-config --cflags --libs eet eina`
3 
4 #include <Eina.h>
5 #include <Eet.h>
6 #include <stdio.h>
7 #include <limits.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <unistd.h>
11 #include <string.h>
12 
13 int
14 main(void)
15 {
16  const char *buffer = "Here is a string of data to save !";
17  const char *key = "This is a crypto key";
18  const char *key_bad = "This is another crypto key";
19 
20  char *file = strdup("/tmp/eet_cipher_example_XXXXXX");
21  Eet_File *ef;
22  char *test;
23  int size;
24  int tmpfd;
25 
26  eet_init();
27 
28  if (-1 == (tmpfd = mkstemp(file)) || !!close(tmpfd))
29  {
30  fprintf(
31  stderr, "ERROR: could not create temporary file (%s) : %s\n",
32  file, strerror(errno));
33  goto panic;
34  }
35 
36  /* Crypt an eet file. */
37  ef = eet_open(file, EET_FILE_MODE_WRITE);
38  if (!ef)
39  {
40  fprintf(
41  stderr, "ERROR: could not access file (%s).\n", file);
42  goto error;
43  }
44 
45  if (!eet_write_cipher(ef, "keys/tests", buffer, strlen(buffer) + 1, 0, key))
46  {
47  fprintf(
48  stderr, "ERROR: could not access file (%s).\n", file);
49  goto error;
50  }
51 
52  eet_close(ef);
53 
54  /* Decrypt an eet file. */
55  ef = eet_open(file, EET_FILE_MODE_READ);
56  if (!ef)
57  {
58  fprintf(
59  stderr, "ERROR: could not access file (%s).\n", file);
60  goto error;
61  }
62 
63  test = eet_read_cipher(ef, "keys/tests", &size, key);
64  if (!test)
65  {
66  fprintf(
67  stderr, "ERROR: could decript contents on file %s, with key %s.\n",
68  file, key);
69  goto error;
70  }
71 
72  if (size != (int)strlen(buffer) + 1)
73  {
74  fprintf(
75  stderr, "ERROR: something is wrong with the decripted data\n");
76  goto error;
77  }
78 
79  if (memcmp(test, buffer, strlen(buffer) + 1) != 0)
80  {
81  fprintf(
82  stderr, "ERROR: something is wrong with the decripted data\n");
83  goto error;
84  }
85 
86  eet_close(ef);
87 
88  /* Decrypt an eet file, now using our BAD key!! */
89  ef = eet_open(file, EET_FILE_MODE_READ);
90  if (!ef)
91  {
92  fprintf(
93  stderr, "ERROR: could not access file (%s).\n", file);
94  goto error;
95  }
96 
97  test = eet_read_cipher(ef, "keys/tests", &size, key_bad);
98 
99  if (size == (int)strlen(buffer) + 1)
100  if (memcmp(test, buffer, strlen(buffer) + 1) == 0)
101  {
102  fprintf(
103  stderr, "ERROR: something is wrong with the contents of %s, as"
104  " we accessed it with a different key and it decripted our"
105  " information right.\n", file);
106  goto error;
107  }
108 
109  eet_close(ef);
110 
111 error:
112  if (unlink(file) != 0)
113  {
114  fprintf(
115  stderr, "ERROR: could not unlink file (%s).\n", file);
116  }
117 
118 panic:
119  eet_shutdown();
120 }
121 
EAPI Eet_File * eet_open(const char *file, Eet_File_Mode mode)
Opens an eet file on disk, and returns a handle to it.
Definition: eet_lib.c:1499
struct _Eet_File Eet_File
Opaque handle that defines an Eet file (or memory).
Definition: Eet.h:527
EAPI void * eet_read_cipher(Eet_File *ef, const char *name, int *size_ret, const char *cipher_key)
Reads a specified entry from an eet file and return data using a cipher.
Definition: eet_lib.c:1905
File is read-only.
Definition: Eet.h:479
EAPI int eet_shutdown(void)
Shuts down the EET library.
Definition: eet_lib.c:594
EAPI int eet_write_cipher(Eet_File *ef, const char *name, const void *data, int size, int compress, const char *cipher_key)
Writes a specified entry to an eet file handle using a cipher.
Definition: eet_lib.c:2342
The file that provides the eet functions.
File is write-only.
Definition: Eet.h:480
Eina Utility library.
EAPI Eet_Error eet_close(Eet_File *ef)
Closes an eet file handle and flush pending writes.
Definition: eet_lib.c:1899
EAPI int eet_init(void)
Initializes the EET library.
Definition: eet_lib.c:540