Debian Codegraph @

src/crypto/crypto.html

raw symbols SCIP
1 <!doctype html public "-//w3c//dtd html 4.0 transitional//en">
2 <html>
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
5 <meta name="GENERATOR" content="Mozilla/4.76 [en] (X11; U; FreeBSD 4.3-RELEASE i386) [Netscape]">
6 </head>
7 <body>
8
9 <center>
10 <h1>
11 &nbsp;Security Interface for Berkeley DB</h1></center>
12
13 <center><i>Susan LoVerso</i>
14 <br><i>Rev 1.6</i>
15 <br><i>2002 Feb 26</i></center>
16
17 <p>We provide an interface allowing secure access to Berkeley DB.&nbsp;&nbsp;
18 Our goal is to allow users to have encrypted secure databases.&nbsp; In
19 this document, the term <i>ciphering</i> means the act of encryption or
20 decryption.&nbsp; They are equal but opposite actions and the same issues
21 apply to both just in the opposite direction.
22 <h3>
23 Requirements</h3>
24 The overriding requirement is to provide a simple mechanism to allow users
25 to have a secure database.&nbsp; A secure database means that all of the
26 pages of a database will be encrypted, and all of the log files will be
27 encrypted.
28 <p>Falling out from this work will be a simple mechanism to allow users
29 to request that we checksum their data for additional error detection (without
30 encryption/decryption).
31 <p>We expect that data in process memory or stored in shared memory, potentially
32 backed by disk, is not encrypted or secure.
33 <h2>
34 <a NAME="DB Modifications"></a>DB Method Interface Modifications</h2>
35 With a logging environment, all database changes are recorded in the log
36 files.&nbsp; Therefore, users requiring secure databases in such environments
37 also require secure log files.
38 <p>A prior thought had been to allow different passwords on the environment
39 and the databases within.&nbsp; However, such a scheme, then requires that
40 the password be logged in order for recovery to be able to restore the
41 database.&nbsp; Therefore, any application having the password for the
42 log could get the password for any databases by reading the log.&nbsp;
43 So having a different password on a database does not gain any additional
44 security and it makes certain things harder and more complex.&nbsp; Some
45 of those more complex things include the need to handle database and env
46 passwords differently since they'd need to be stored and accessed from
47 different places.&nbsp; Also resolving the issue of how <i>db_checkpoint</i>
48 or <i>db_sync</i>, which flush database pages to disk, would find the passwords
49 of various databases without any dbps was unsolved.&nbsp; The feature didn't
50 gain anything and caused significant pain.&nbsp; Therefore the decision
51 is that there will be a single password protecting an environment and all
52 the logs and some databases within that environment.&nbsp; We do allow
53 users to have a secure environment and clear databases.&nbsp; Users that
54 want secure databases within a secure environment must set a flag.
55 <p>Users wishing to enable encryption on a database in a secure environment
56 or enable just checksumming on their database pages will use new flags
57 to <a href="../docs/api_c/db_set_flags.html">DB->set_flags()</a>.&nbsp;
58 Providing ciphering over an entire environment is accomplished by adding
59 a single environment method: <a href="../docs/api_c/env_set_encrypt.html">DBENV->set_encrypt()</a>.&nbsp;
60 Providing encryption for a database (not part of an environment) is accomplished
61 by adding a new database method: <a href="../docs/api_c/db_set_encrypt.html">DB->set_encrypt()</a>.
62 <p>Both of the <i>set_encrypt</i> methods must be called before their respective
63 <i>open</i> calls.&nbsp; The environment method must be before the environment
64 open because we must know about security before there is any possibility
65 of writing any log records out.&nbsp; The database method must be before
66 the database open in order to read the root page.&nbsp; The planned interfaces
67 for these methods are:
68 <pre>DBENV->set_encrypt(DBENV *dbenv,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* DB_ENV structure */
69 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char *passwd&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Password */
70 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; u_int32_t flags);&nbsp;&nbsp;&nbsp;&nbsp; /* Flags */</pre>
71
72 <pre>DB->set_encrypt(DB *dbp,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* DB structure */
73 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char *passwd&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Password */
74 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; u_int32_t flags);&nbsp;&nbsp;&nbsp;&nbsp; /* Flags */</pre>
75 The flags accepted by these functions are:
76 <pre>#define DB_ENCRYPT_AES&nbsp; 0x00000001&nbsp; /* Use the AES encryption algorithm */</pre>
77 Passwords are NULL-terminated strings.&nbsp; NULL or zero length strings
78 are illegal.&nbsp; These flags enable the checksumming and encryption using
79 the particular algorithms we have chosen for this implementation.&nbsp;
80 The flags are named such that there is a logical naming pattern if additional
81 checksum or encryption algorithms are used. If a user gives a flag of zero,
82 it will behave in a manner similar to DB_UNKNOWN. It will be illegal if
83 they are creating the environment or database, as an algorithm must be
84 specified. If they are joining an existing environment or opening an existing
85 database, they will use whatever algorithm is in force at the time.&nbsp;
86 Using DB_ENCRYPT_AES automatically implies SHA1 checksumming.
87 <p>These functions will perform several initialization steps.&nbsp; We
88 will allocate crypto_handle for our env handle and set up our function
89 pointers.&nbsp; We will allocate space and copy the password into our env
90 handle password area.&nbsp; Similar to <i>DB->set_cachesize</i>, calling
91 <i>DB->set_encrypt</i>
92 will actually reflect back into the local environment created by DB.
93 <p>Lastly, we will add a new flag, DB_OVERWRITE, to the <a href="../docs/api_c/env_remove.html">DBENV->remove</a>
94 method.&nbsp; The purpose of this flag is to force all of the memory used
95 by the shared regions to be overwritten before removal.&nbsp; We will use
96 <i>rm_overwrite</i>,
97 a function that overwrites and syncs a file 3 times with varying bit patterns
98 to really remove a file.&nbsp; Additionally, this flag will force a sync
99 of the overwritten regions to disk, if the regions are backed by the file
100 system.&nbsp; That way there is no residual information left in the clear
101 in memory or freed disk blocks.&nbsp; Although we expect that this flag
102 will be used by customers using security, primarily, its action is not
103 dependent on passwords or a secure setup, and so can be used by anyone.
104 <h4>
105 Initialization of the Environment</h4>
106 The setup of the security subsystem will be similar to replication initialization
107 since it is a sort of subsystem, but it does not have its own region.&nbsp;
108 When the environment handle is created via <i>db_env_create</i>, we initialize
109 our <i>set_encrypt</i> method to be the RPC or local version.&nbsp; Therefore
110 the <i>DB_ENV</i> structure needs a new pointer:
111 <pre>&nbsp;&nbsp;&nbsp; void&nbsp;&nbsp;&nbsp; *crypto_handle;&nbsp;&nbsp; /* Security handle */</pre>
112 The crypto handle will really point to a new <i>__db_cipher</i> structure
113 that will contain a set of functions and a pointer to the in-memory information
114 needed by the specific encryption algorithm.&nbsp; It will look like:
115 <pre>typedef struct __db_cipher {
116 &nbsp;&nbsp;&nbsp; int&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (*init)__P((...));&nbsp;&nbsp;&nbsp; /* Alg-specific initialization function */
117 &nbsp;&nbsp;&nbsp; int&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (*encrypt)__P((...)); /* Alg-specific encryption algorithm */
118 &nbsp;&nbsp;&nbsp; int&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (*decrypt)__P((...)); /* Alg-specific decryption function */
119 &nbsp;&nbsp;&nbsp; void&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; *data;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Pointer to alg-specific information (AES_CIPHER) */
120 &nbsp;&nbsp;&nbsp; u_int32_t flags;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Cipher flags */
121 } DB_CIPHER;</pre>
122
123 <pre>#define DB_MAC_KEY&nbsp;&nbsp;&nbsp; 20&nbsp;&nbsp;&nbsp; /* Size of the MAC key */
124 typedef struct __aes_cipher {
125 &nbsp;&nbsp;&nbsp; keyInstance&nbsp;&nbsp;&nbsp; encrypt_ki;&nbsp;&nbsp; /* Encrypt keyInstance temp. */
126 &nbsp;&nbsp;&nbsp; keyInstance&nbsp;&nbsp;&nbsp; decrypt_ki;&nbsp;&nbsp; /* Decrypt keyInstance temp. */
127 &nbsp;&nbsp;&nbsp; u_int8_t&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mac_key[DB_MAC_KEY]; /* MAC key */
128 &nbsp;&nbsp;&nbsp; u_int32_t&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; flags;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* AES-specific flags */
129 } AES_CIPHER;</pre>
130 It should be noted that none of these structures have their own mutex.&nbsp;
131 We hold the environment region locked while we are creating this, but once
132 this is set up, it is read-only forever.
133 <p>During <a href="../docs/api_c/env_set_encrypt.html">dbenv->set_encrypt</a>,
134 we set the encryption, decryption and checksumming methods to the appropriate
135 functions based on the flags.&nbsp; This function will allocate us a crypto
136 handle that we store in the <i>DB_ENV</i> structure just like all the
137 other subsystems.&nbsp; For now, only AES ciphering functions and SHA1
138 checksumming functions are supported.&nbsp; Also we will copy the password
139 into the <i>DB_ENV</i> structure.&nbsp; We ultimately need to keep the
140 password in the environment's shared memory region or compare this one
141 against the one that is there, if we are joining an existing environment,
142 but we do not have it yet because open has not yet been called.&nbsp; We
143 will allocate a structure that will be used in initialization and set up
144 the function pointers to point to the algorithm-specific functions.
145 <p>In the&nbsp; <i>__env_open</i> path, in <i>__db_e_attach</i>, if we
146 are creating the region and the <i>dbenv->passwd</i> field is set, we need
147 to use the length of the password in the initial computation of the environment's
148 size.&nbsp; This guarantees sufficient space for storing the password in
149 shared memory.&nbsp; Then we will call a new function to initialize the
150 security region, <i>__crypto_region_init</i> in <i>__env_open</i>.&nbsp;
151 If we are the creator, we will allocate space in the shared region to store
152 the password and copy the password into that space.&nbsp; Or, if we are
153 not the creator we will compare the password stored in the dbenv with the
154 one in shared memory.&nbsp;&nbsp; Additionally, we will compare the ciphering
155 algorithm to the one stored in the shared region.We'll smash the dbenv
156 password and free it.&nbsp; If they do not match, we return an error.&nbsp;
157 If we are the creator we store the offset into the REGENV structure.&nbsp;
158 Then <i>__crypto_region_init&nbsp;</i> will call the initialization function
159 set up earlier based on the ciphering algorithm specified.&nbsp; For now
160 we will call <i>__aes_init</i>.&nbsp; Additionally this function will allocate
161 and set up the per-process state vector for this encryption's IVs.&nbsp;
162 See <a href="#Generating the Initialization Vector">Generating the Initialization
163 Vector</a> for a detailed description of the IV and state vector.
164 <p>In the AES-specific initialization function, <i>__aes_init</i>,&nbsp;
165 we will initialize it by calling
166 <i>__aes_derivekeys</i> in order to fill
167 in the keyInstance and mac_key fields in that structure.&nbsp; The REGENV
168 structure will have one additional item
169 <pre>&nbsp;&nbsp; roff_t&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; passwd_off;&nbsp;&nbsp; /* Offset of passwd */</pre>
170
171 <h4>
172 Initializing a Database</h4>
173 During <a href="../docs/api_c/db_set_encrypt.html">db->set_encrypt</a>,
174 we set the encryption, decryption and checksumming methods to the appropriate
175 functions based on the flags.&nbsp; Basically, we test that we are not
176 in an existing environment and we haven't called open.&nbsp; Then we just
177 call through the environment handle to set the password.
178 <p>Also, we will need to add a flag in the database meta-data page that
179 indicates that the database is encrypted and what its algorithm is.&nbsp;
180 This will be used when the meta-page is read after reopening a file. We
181 need this information on the meta-page in order to detect a user opening
182 a secure database without a password.&nbsp; I propose using the first unused1
183 byte (renaming it too) in the meta page for this purpose.
184 <p>All pages will not be encrypted for the first 64 bytes of data.&nbsp;
185 Database meta-pages will be encrypted on the first 512 bytes only.&nbsp;
186 All meta-page types will have an IV and checksum added within the first
187 512 bytes as well as a crypto magic number.&nbsp; This will expand the
188 size of the meta-page from 256 bytes to 512 bytes. The page in/out routines,
189 <i>__db_pgin</i> and <i>__db_pgout</i> know the page type of the page and
190 will apply the 512 bytes ciphering to meta pages.&nbsp; In <i>__db_pgout</i>,
191 if we have a crypto handle in our (private) environment, we will apply
192 ciphering to either the entire page, or the first 512 bytes if it is a
193 meta-page.&nbsp; In <i>__db_pgin</i>, we will decrypt if the page we have
194 a crypto handle.
195 <p>When multiple processes share a database, all must use the same password
196 as the database creator. Using an existing database requires several conditions
197 to be true.&nbsp; First, if the creator of the database did not create
198 with security, then opening later with security is an error.&nbsp; Second,
199 if the creator did create it with security, then opening later without
200 security is an error.&nbsp; Third, we need to be able to test and check
201 that when another process opens a secure database that the password they
202 provided is the same as the one in use by the creator.
203 <p>When reading the meta-page, in <i>__db_file_setup</i>, we do not go
204 through the paging functions, but directly read via <i>__os_read</i>.&nbsp;
205 It is at this point that we will determine if the user is configured correctly.&nbsp;
206 If the meta-page we read has an IV and checksum, they better have a crypto
207 handle.&nbsp; If they have a crypto handle, then the meta-page must have
208 an IV and checksum.&nbsp; If both of those are true, we test the password.&nbsp;
209 We compare the unencrypted magic number to the newly-decrypted crypto magic
210 number and if they are not the same, then we report that the user gave
211 us a bad password.
212 <p>On a mostly unrelated topic, even when we go to very large pagesizes,
213 the meta information will still be within a disk sector.&nbsp; So, after
214 talking it over with Keith and Margo, we determined that unencrypted meta-pages
215 still will not need a checksum.
216 <h3>
217 Encryption and Checksum Routines</h3>
218 These routines are provided to us by Adam Stubblefield at Rice University
219 ([email protected]).&nbsp; The functional interfaces are:
220 <pre>__aes_derivekeys(DB_ENV *dbenv,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* dbenv */
221 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; u_int8_t *passwd,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Password */
222 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; size_t passwd_len,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Length of passwd */
223 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; u_int8_t *mac_key,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* 20 byte array to store MAC key */
224 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; keyInstance *encrypt_key,&nbsp;&nbsp; /* Encryption key of passwd */
225 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; keyInstance *decrypt_key);&nbsp; /* Decryption key of passwd */</pre>
226 This is the only function requiring the textual user password.&nbsp; From
227 the password, this function generates a key used in the checksum function,
228 <i>__db_chksum</i>.&nbsp;
229 It also fills in <i>keyInstance</i> structures which are then used in the
230 encryption and decryption routines.&nbsp; The keyInstance structures must
231 already be allocated.&nbsp; These will be stored in the AES_CIPHER structure.
232 <pre>&nbsp;__db_chksum(u_int8_t *data,&nbsp;&nbsp;&nbsp; /* Data to checksum */
233 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; size_t data_len,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Length of data */
234 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; u_int8_t *mac_key,&nbsp;&nbsp;&nbsp; /* 20 byte array from __db_derive_keys */
235 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; u_int8_t *checksum);&nbsp; /* 20 byte array to store checksum */</pre>
236 This function generates a checksum on the data given.&nbsp; This function
237 will do double-duty for users that simply want error detection on their
238 pages.&nbsp; When users are using encryption, the <i>mac_key </i>will contain
239 the 20-byte key set up in <i>__aes_derivekeys</i>.&nbsp; If they just want
240 checksumming, then <i>mac_key</i> will be NULL.&nbsp; According to Adam,
241 we can safely use the first N-bytes of the checksum.&nbsp; So for seeding
242 the generator for initialization vectors, we'll hash the time and then
243 send in the first 4 bytes for the seed.&nbsp; I believe we can probably
244 do the same thing for checksumming log records.&nbsp; We can only use 4
245 bytes for the checksum in the non-secure case.&nbsp; So when we want to
246 verify the log checksum we can compute the mac but just compare the first
247 4 bytes to the one we read.&nbsp; All locations where we generate or check
248 log record checksums that currently call <i>__ham_func4</i> will now call
249 <i>__db_chksum</i>.&nbsp;
250 I believe there are 5 such locations,
251 <i>__log_put, __log_putr, __log_newfile,
252 __log_rep_put
253 </i>and<i> __txn_force_abort.</i>
254 <pre>__aes_encrypt(DB_ENV *dbenv,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* dbenv */
255 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; keyInstance *key,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Password key instance from __db_derive_keys */
256 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; u_int8_t *iv,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Initialization vector */
257 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; u_int8_t *data,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Data to encrypt */
258 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; size_t data_len);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Length of data to encrypt - 16 byte multiple */</pre>
259 This is the function to encrypt data.&nbsp; It will be called to encrypt
260 pages and log records.&nbsp; The <i>key</i> instance is initialized in
261 <i>__aes_derivekeys</i>.&nbsp;
262 The initialization vector, <i>iv</i>, is the 16 byte random value set up
263 by the Mersenne Twister pseudo-random generator.&nbsp; Lastly, we pass
264 in a pointer to the <i>data</i> to encrypt and its length in <i>data_len</i>.&nbsp;
265 The <i>data_len</i> must be a multiple of 16 bytes. The encryption is done
266 in-place so that when the encryption code returns our encrypted data is
267 in the same location as the original data.
268 <pre>__aes_decrypt(DB_ENV *dbenv,&nbsp;&nbsp;&nbsp; /* dbenv */
269 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; keyInstance *key,&nbsp; /* Password key instance from __db_derive_keys */
270 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; u_int8_t *iv,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Initialization vector */
271 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; u_int8_t *data,&nbsp;&nbsp;&nbsp; /* Data to decrypt */
272 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; size_t data_len);&nbsp; /* Length of data to decrypt - 16 byte multiple */</pre>
273 This is the function to decrypt the data.&nbsp; It is exactly the same
274 as the encryption function except for the action it performs.&nbsp; All
275 of the args and issues are the same.&nbsp; It also decrypts in place.
276 <h3>
277 <a NAME="Generating the Initialization Vector"></a>Generating the Initialization
278 Vector</h3>
279 Internally, we need to provide a unique initialization vector (IV) of 16
280 bytes every time we encrypt any data with the same password.&nbsp; For
281 the IV we are planning on using mt19937, the Mersenne Twister, a random
282 number generator that has a period of 2**19937-1. This package can be found
283 at <a href="http://www.math.keio.ac.jp/~matumoto/emt.html">http://www.math.keio.ac.jp/~matumoto/emt.html</a>.&nbsp;
284 Tests show that although it repeats a single integer every once in a while,
285 that after several million iterations, it doesn't repeat any 4 integers
286 that we'd be stuffing into our 16-byte IV.&nbsp; We plan on seeding this
287 generator with the time (tv_sec) hashed through SHA1 when we create the
288 environment.&nbsp; This package uses a global state vector that contains
289 624 unsigned long integers.&nbsp; We do not allow a 16-byte IV of zero.&nbsp;
290 It is simpler just to reject any 4-byte value of 0 and if we get one, just
291 call the generator again and get a different number.&nbsp; We need to detect
292 holes in files and if we read an IV of zero that is a simple indication
293 that we need to check for an entire page of zero.&nbsp; The IVs are stored
294 on the page after encryption and are not encrypted themselves so it is
295 not possible for an entire encrypted page to be read as all zeroes, unless
296 it was a hole in a file.&nbsp; See <a href="#Holes in Files">Holes in Files</a>
297 for more details.
298 <p>We will not be holding any locks when we need to generate our IV but
299 we need to protect access to the state vector and the index.&nbsp; Calls
300 to the MT code will come while encrypting some data in <i>__aes_encrypt.</i>&nbsp;&nbsp;
301 The MT code will assume that all necessary locks are held in the caller.&nbsp;
302 We will have per-process state vectors that are set up when a process begins.&nbsp;
303 That way we minimize the contention and only multi-threaded processes need
304 acquire locks for the IV.&nbsp; We will have the state vector in the environment
305 handle in heap memory, as well as the index and there will be a mutex protecting
306 it for threaded access.&nbsp; This will be added to the <i>DB_ENV</i>
307 structure:
308 <pre>&nbsp;&nbsp;&nbsp; DB_MUTEX&nbsp;&nbsp;&nbsp; *mt_mutexp;&nbsp;&nbsp; /* Mersenne Twister mutex */
309 &nbsp;&nbsp;&nbsp; int&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; *mti;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* MT index */
310 &nbsp;&nbsp;&nbsp; u_long&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; *mt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* MT state vector */</pre>
311 This portion of the environment will be initialized at the end of _<i>_dbenv_open</i>,
312 right after we initialize the other mutex for the <i>dblist</i>. When we
313 allocate the space, we will generate our initial state vector. If we are
314 multi-threaded we'll allocate and initialize our mutex also.
315 <p>We need to make changes to the MT code to make it work in our namespace
316 and&nbsp; to take&nbsp; a pointer to the location of the state vector and
317 the index.&nbsp;&nbsp; There will be a wrapper function <i>__db_generate_iv</i>
318 that DB will call and it will call the appropriate MT function.&nbsp; I
319 am also going to change the default seed to use a hashed time instead of
320 a hard coded value.&nbsp; I have looked at other implementations of the
321 MT code available on the web site.&nbsp; The C++ version does a hash on
322 the current time.&nbsp; I will modify our MT code to seed with the hashed
323 time as well.&nbsp; That way the code to seed is contained within the MT
324 code and we can just write the wrapper to get an IV.&nbsp; We will not
325 be changing the core computational code of MT.
326 <h2>
327 DB Internal Issues</h2>
328
329 <h4>
330 When do we Cipher?</h4>
331 All of the page ciphering is done in the <i>__db_pgin/__db_pgout</i> functions.&nbsp;
332 We will encrypt after the method-specific function on page-out and decrypt
333 before the method-specfic function on page-in.&nbsp; We do not hold any
334 locks when entering these functions.&nbsp; We determine that we need to
335 cipher based on the existence of the encryption flag in the dbp.
336 <p>For ciphering log records, the encryption will be done as the first
337 thing (or a new wrapper) in <i>__log_put.&nbsp; </i>See <a href="#Log Record Encryption">Log
338 Record Encryption</a> for those details.
339 <br>&nbsp;
340 <h4>
341 Page Changes</h4>
342 The checksum and IV values will be stored prior to the first index of the
343 page.&nbsp; We have a new P_INP macro that replaces use of inp[X] in the
344 code. &nbsp;This macro takes a dbp as an argument and determines where
345 our first index is based on whether we have DB_AM_CHKSUM and DB_AM_ENCRYPT
346 set.&nbsp; If neither is set, then our first index is where it always was.
347 &nbsp;If just checksumming is set, then we reserve a 4-byte checksum.&nbsp;
348 If encryption is set, then we reserve 36 bytes for our checksum/IV as well
349 as some space to get proper alignment to encrypt on a 16-byte boundary.
350 <p>Since several paging macros use inp[X] in them, those macros must now
351 take a dbp.&nbsp; There are a lot of changes to make all the necessary
352 paging macros take a dbp, although these changes are trivial in nature.
353 <p>Also, there is a new function <i>__db_chk_meta</i> to perform checksumming
354 and decryption checking on meta pages specifically.&nbsp; This function
355 is where we check that the database algorithm matches what the user gave
356 (or if they set DB_CIPHER_ANY then we set it), and other encryption related
357 testing for bad combinations of what is in the file versus what is in the
358 user structures.
359 <h4>
360 Verification</h4>
361 The verification code will also need to be updated to deal with secure
362 pages.&nbsp; Basically when the verification code reads in the meta page
363 it will call <i>__db_chk_meta</i> to perform any checksumming and decryption.
364 <h4>
365 <a NAME="Holes in Files"></a>Holes in Files</h4>
366 Holes in files will be dealt with rather simply.&nbsp; We need to be able
367 to distinguish reading a hole in a file from an encrypted page that happened
368 to encrypt to all zero's.&nbsp; If we read a hole in a file, we do not
369 want to send that empty page through the decryption routine.&nbsp; This
370 can be determined simply without incurring the performance penalty of comparing
371 every byte on a page on every read until we get a non-zero byte.
372 <br>The __db_pgin function is only given an invalid page P_INVALID in this
373 case.&nbsp;&nbsp;So, if the page type, which is always unencrypted, is
374 P_INVALID, then we do not perform any checksum verification or decryption.
375 <h4>
376 Errors and Recovery</h4>
377 Dealing with a checksum error is tricky.&nbsp; Ultimately, if a checksum
378 error occurs it is extremely likely that the user must do catastrophic
379 recovery.&nbsp; There is no other failure return other than&nbsp; DB_RUNRECOVERY
380 for indicating that the user should run catastrophic recovery.&nbsp; We
381 do not want to add a new error return for applications to check because
382 a lot of applications already look for and deal with DB_RUNRECOVERY as
383 an error condition and we want to fit ourselves into that application model.&nbsp;
384 We already indicate to the user that when they get that error, then they
385 need to run recovery.&nbsp; If recovery fails, then they need to run catastrophic
386 recovery.&nbsp; We need to get ourselves to the point where users will
387 run catastrophic recovery.
388 <p>If we get a checksum error, then we need to log a message stating a
389 checksum error occurred on page N.&nbsp; In <i>__db_pgin</i>, we can check
390 if logging is on in the environment.&nbsp; If so, we want to log the message.
391 <p>When the application gets the DB_RUNRECOVERY error, they'll have to
392 shut down their application and run recovery.&nbsp; When the recovery encounters
393 the record indicating checksum failure, then normal recovery will fail
394 and the user will have to perform catastrophic recovery.&nbsp; When catastrophic
395 recovery encounters that record, it will simply ignore it.
396 <h4>
397 <a NAME="Log Record Encryption"></a>Log Record Encryption</h4>
398 Log records will be ciphered.&nbsp; It might make sense to wrap <i>__log_put</i>
399 to encrypt the DBT we send down.&nbsp; The <i>__log_put </i>function is
400 where the checksum is computed before acquiring the region lock.&nbsp;
401 But also this function is where we call <i>__rep_send_message</i> to send
402 the DBT to the replication clients.&nbsp; Therefore, we need the DBT to
403 be encrypted prior to there.&nbsp; We also need it encrypted before checksumming.
404 I think <i>__log_put </i>will become <i>__log_put_internal</i>, and the
405 new <i>__log_put</i> will encrypt if needed and then call <i>__log_put_internal
406 </i>(the
407 function formerly known as <i>__log_put</i>).&nbsp; Log records are kept
408 in a shared memory region buffer prior to going out to disk.&nbsp; Records
409 in the buffer will be encrypted.&nbsp; No locks are held at the time we
410 will need to encrypt.
411 <p>On reading the log, via log cursors, the log code stores log records
412 in the log buffer.&nbsp; Records in that buffer will be encrypted, so decryption
413 will occur no matter whether we are returning records from the buffer or
414 if we are returning log records directly from the disk. Current checksum
415 checking is done in
416 <i>__logc_get_int.</i>&nbsp; Decryption will be done
417 after the checksum is checked.
418 <p>There are currently two nasty issues with encrypted log records.&nbsp;
419 The first is that <i>__txn_force_abort</i> overwrites a commit record in
420 the log buffer with an abort record.&nbsp; Well, our log buffer will be
421 encrypted.&nbsp; Therefore, <i>__txn_force_abort</i> is going to need to
422 do encryption of its new record.&nbsp; This can be accomplished by sending
423 in the dbenv handle to the function.&nbsp; It is available to us in <i>__log_flush_commit</i>
424 and we can just pass it in.&nbsp; I don't like putting log encryption in
425 the txn code, but the layering violation is already there.
426 <p>The second issue is that the encryption code requires data that is a
427 multiple of 16 bytes and log record lengths are variable.&nbsp; We will
428 need to pad log records to meet the requirement.&nbsp; Since the callers
429 of <i>__log_put</i> set up the given DBT it is a logical place to pad if
430 necessary. We will modify the gen_rec.awk script to have all of the generated
431 logging functions pad for us if we have a crypto handle. This padding will
432 also expand the size of log files. Anyone calling <i>log_put</i> and using
433 security from the application will have to pad on their own or it will
434 return an error.
435 <p>When ciphering the log file, we will need a different header than the
436 current one.&nbsp; The current header only has space for a 4 byte checksum.&nbsp;
437 Our secure header will need space for the 16 byte IV and 20 byte checksum.&nbsp;
438 This will blow up our log files when running securely since every single
439 log record header will now consume 32 additional bytes.&nbsp; I believe
440 that the log header does not need to be encrypted.&nbsp; It contains an
441 offset, a length and our IV and checksum.&nbsp; Our IV and checksum are
442 never encrypted.&nbsp; I don't believe there to be any risk in having the
443 offset and length in the clear.
444 <p>I would prefer not to have two types of log headers that are incompatible
445 with each other.&nbsp; It is not acceptable to increase the log headers
446 of all users from 12 bytes to 44 bytes.&nbsp; Such a change would also
447 make log files incompatible with earlier releases.&nbsp; Worse even, is
448 that the <i>cksum</i> field of the header is in between the offset and
449 len.&nbsp; It would be really convenient if we could have just made a bigger
450 cksum portion without affecting the location of the other fields.&nbsp;
451 Oh well.&nbsp; Most customers will not be using encryption and we won't
452 make them pay the price of the expanded header.&nbsp; Keith indicates that
453 the log file format is changing with the next release so I will move the
454 cksum field so it can at least be overlaid.
455 <p>One method around this would be to have a single internal header that
456 contains all the information both mechanisms need, but when we write out
457 the header we choose which pieces to write.&nbsp; By appending the security
458 information to the end of the existing structure, and adding a size field,
459 we can modify a few places to use the size field to write out only the
460 current first 12 bytes, or the entire security header needed.
461 <h4>
462 Replication</h4>
463 Replication clients are going to need to start all of their individual
464 environment handles with the same password.&nbsp; The log records are going
465 to be sent to the clients decrypted and the clients will have to encrypt
466 them on their way to the client log files.&nbsp; We cannot send encrypted
467 log records to clients. &nbsp;The reason is that the checksum and IV&nbsp;are
468 stored in the log header and the master only sends the log record itself
469 to the client. &nbsp;Therefore, the client has no way to decrypt a log
470 record from the master. &nbsp;Therefore, anyone wanting to use truly secure
471 replication is going to have to have a secure transport mechanism.&nbsp;
472 By not encrypting records, clients can theoretically have different passwords
473 and DB won't care.
474 <p>On the master side we must copy the DBT sent in.&nbsp; We encrypt the
475 original and send to clients the clear record.&nbsp; On the client side,
476 support for encryption is added into <i>__log_rep_put</i>.
477 <h4>
478 Sharing the Environment</h4>
479 When multiple processes join the environment, all must use the same password
480 as the creator.
481 <p>Joining an existing environment requires several conditions to be true.&nbsp;
482 First, if the creator of the environment did not create with security,
483 then joining later with security is an error.&nbsp; Second, if the creator
484 did create it with security, then joining later without security is an
485 error.&nbsp; Third, we need to be able to test and check that when another
486 process joins a secure environment that the password they provided is the
487 same as the one in use by the creator.
488 <p>The first two scenarios should be fairly trivial to determine, if we
489 aren't creating the environment, we can compare what is there with what
490 we have.&nbsp; In the third case, the <i>__crypto_region_init</i> function
491 will see that the environment region has a valid passwd_off and we'll then
492 compare that password to the one we have in our dbenv handle.&nbsp; In
493 any case we'll smash the dbenv handle's passwd and free that memory before
494 returning whether we have a password match or not.
495 <p>We need to store the passwords themselves in the region because multiple
496 calls to the <i>__aes_derivekeys </i>function with the same password yields
497 different keyInstance contents.&nbsp; Therefore we don't have any way to
498 check passwords other than retaining and comparing the actual passwords.
499 <h4>
500 Other APIs</h4>
501 All of the other APIs will need interface enhancements to support the new
502 security methods.&nbsp; The Java and C++ interfaces will likely be done
503 by Michael Cahill and Sue will implement the Tcl and RPC changes.&nbsp;
504 Tcl will need the changes for testing purposes but the interface should
505 be public, not test-only.&nbsp; RPC should fully support security.&nbsp;
506 The biggest risk that I can see is that the client will send the password
507 to the server in the clear.&nbsp; Anyone sniffing the wires or running
508 tcpdump or other packet grabbing code could grab that.&nbsp; Someone really
509 interested in using security over RPC probably ought to add authentication
510 and other measures to the RPC server as well.
511 <h4>
512 <a NAME="Utilities"></a>Utilities</h4>
513 All should take a -P flag to specify a password for the environment or
514 password.&nbsp; Those that take an env and a database might need something
515 more to distinguish between env passwds and db passwds. Here is what we
516 do for each utility:
517 <ul>
518 <li>
519 berkeley_db_svc - Needs -P after each -h specified.</li>
520
521 <li>
522 db_archive - Needs -P if the env is encrypted.</li>
523
524 <li>
525 db_checkpoint - Needs -P if the env is encrypted.</li>
526
527 <li>
528 db_deadlock - No changes</li>
529
530 <li>
531 db_dump - Needs -P if the env or database is encrypted.</li>
532
533 <li>
534 db_load - Needs -P if the env or database is encrypted.</li>
535
536 <li>
537 db_printlog - Needs -P if the env is encrypted.</li>
538
539 <li>
540 db_recover - Needs -P if the env is encrypted.</li>
541
542 <li>
543 db_stat - Needs -P if the env or database is encrypted.</li>
544
545 <li>
546 db_upgrade - Needs -P if the env or database is encrypted.</li>
547
548 <li>
549 db_verify - Needs -P if the env or database is encrypted.</li>
550 </ul>
551
552 <h2>
553 Testing</h2>
554 All testing should be able to be accomplished via Tcl.&nbsp; The following
555 tests (and probably others I haven't thought of yet) should be performed:
556 <ul>
557 <li>
558 Basic functionality - basically a test001 but encrypted without an env</li>
559
560 <li>
561 Basic functionality, w/ env - like the previous test but with an env.</li>
562
563 <li>
564 Basic functionality, multiple processes - like first test, but make sure
565 others can correctly join.</li>
566
567 <li>
568 Basic functionality, mult. processes - like above test, but initialize/close
569 environment/database first so that the next test processes are all joiners
570 of an existing env, but creator no longer exists and the shared region
571 must be opened.</li>
572
573 <li>
574 Recovery test - Run recovery over an encrypted environment.</li>
575
576 <li>
577 Subdb test - Run with subdbs that are encrypted.</li>
578
579 <li>
580 Utility test - Verify the new options to all the utilities.</li>
581
582 <li>
583 Error handling - Test the basic setup errors for both env's and databases
584 with multiple processes.&nbsp; They are:</li>
585
586 <ol>
587 <li>
588 Attempt to set a NULL or zero-length passwd.</li>
589
590 <li>
591 Create Env w/ security and attempt to create database w/ its own password.</li>
592
593 <li>
594 Env/DB creates with security.&nbsp; Proc2 joins without - should get an
595 error.</li>
596
597 <li>
598 Env/DB creates without security.&nbsp; Proc2 joins with - should get an
599 error.</li>
600
601 <li>
602 Env/DB creates with security.&nbsp; Proc2 joins with different password
603 - should get an error.</li>
604
605 <li>
606 Env/DB creates with security.&nbsp; Closes.&nbsp; Proc2 reopens with different
607 password - should get an error.</li>
608
609 <li>
610 Env/DB creates with security.&nbsp; Closes.&nbsp; Tcl overwrites a page
611 of the database with garbage.&nbsp; Proc2 reopens with the correct password.&nbsp;
612 Code should detect checksum error.</li>
613
614 <li>
615 Env/DB creates with security.&nbsp; Open a 2nd identical DB with a different
616 password.&nbsp; Put the exact same data into both databases.&nbsp; Close.&nbsp;
617 Overwrite the identical page of DB1 with the one from DB2.&nbsp; Reopen
618 the database with correct DB1 password.&nbsp; Code should detect an encryption
619 error on that page.</li>
620 </ol>
621 </ul>
622
623 <h2>
624 Risks</h2>
625 There are several holes in this design.&nbsp; It is important to document
626 them clearly.
627 <p>The first is that all of the pages are stored in memory and possibly
628 the file system in the clear.&nbsp; The password is stored in the shared
629 data regions in the clear.&nbsp; Therefore if an attacker can read the
630 process memory, they can do whatever they want.&nbsp; If the attacker can
631 read system memory or swap they can access the data as well.&nbsp; Since
632 everything in the shared data regions (with the exception of the buffered
633 log) will be in the clear, it is important to realize that file backed
634 regions will be written in the clear, including the portion of the regions
635 containing passwords.&nbsp; We recommend to users that they use system
636 memory instead of file backed shared memory.
637 </body>
638 </html>