@@ -96,6 +96,12 @@ struct codec_ctx {
9696 cipher_ctx * write_ctx ;
9797};
9898
99+ /* activate and initialize sqlcipher. Most importantly, this will automatically
100+ intialize OpenSSL's EVP system if it hasn't already be externally. Note that
101+ this function may be called multiple times as new codecs are intiialized.
102+ Thus it performs some basic counting to ensure that only the last and final
103+ sqlcipher_deactivate() will free the EVP structures.
104+ */
99105void sqlcipher_activate () {
100106 sqlite3_mutex_enter (sqlite3MutexAlloc (SQLITE_MUTEX_STATIC_MASTER ));
101107
@@ -115,6 +121,9 @@ void sqlcipher_activate() {
115121 sqlite3_mutex_leave (sqlite3MutexAlloc (SQLITE_MUTEX_STATIC_MASTER ));
116122}
117123
124+ /* deactivate SQLCipher, most imporantly decremeting the activation count and
125+ freeing the EVP structures on the final deactivation to ensure that
126+ OpenSSL memory is cleaned up */
118127void sqlcipher_deactivate () {
119128 sqlite3_mutex_enter (sqlite3MutexAlloc (SQLITE_MUTEX_STATIC_MASTER ));
120129 /* If it is initialized externally, then the init counter should never be greater than zero.
@@ -132,6 +141,10 @@ void sqlcipher_deactivate() {
132141 sqlite3_mutex_leave (sqlite3MutexAlloc (SQLITE_MUTEX_STATIC_MASTER ));
133142}
134143
144+ /* constant time memset using volitile to avoid having the memset
145+ optimized out by the compiler.
146+ Note: As suggested by Joachim Schipper ([email protected] ) 147+ */
135148void * sqlcipher_memset (void * v , unsigned char value , int len ) {
136149 int i = 0 ;
137150 volatile unsigned char * a = v ;
@@ -179,7 +192,7 @@ int sqlcipher_random (void *buffer, int length) {
179192
180193/**
181194 * Free and wipe memory. Uses SQLites internal sqlite3_free so that memory
182- * can be countend and memory leak detection works in the tet suite.
195+ * can be countend and memory leak detection works in the test suite.
183196 * If ptr is not null memory will be freed.
184197 * If sz is greater than zero, the memory will be overwritten with zero before it is freed
185198 * If sz is > 0, and not compiled with OMIT_MEMLOCK, system will attempt to unlock the
@@ -223,7 +236,7 @@ void* sqlcipher_malloc(int sz) {
223236
224237
225238/**
226- * Initialize a a new cipher_ctx struct. This function will allocate memory
239+ * Initialize new cipher_ctx struct. This function will allocate memory
227240 * for the cipher context and for the key
228241 *
229242 * returns SQLITE_OK if initialization was successful
0 commit comments