Skip to content

Commit 29b00cb

Browse files
Initial FIPS integration
1 parent ea0d002 commit 29b00cb

File tree

8 files changed

+49
-4
lines changed

8 files changed

+49
-4
lines changed

Makefile.msc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,13 @@ LIBRESOBJS =
664664
# All of the source code files.
665665
#
666666
SRC = \
667+
$(TOP)\src\crypto.c \
668+
$(TOP)\src\crypto_cc.c \
669+
$(TOP)\src\crypto_impl.c \
670+
$(TOP)\src\crypto_libtomcrypt.c \
671+
$(TOP)\src\crypto_openssl.c \
672+
$(TOP)\src\crypto.h \
673+
$(TOP)\src\sqlcipher.h \
667674
$(TOP)\src\alter.c \
668675
$(TOP)\src\analyze.c \
669676
$(TOP)\src\attach.c \

src/crypto.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@ int sqlcipher_codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLef
8989

9090
CODEC_TRACE(("sqlcipher_codec_pragma: entered db=%p iDb=%d pParse=%p zLeft=%s zRight=%s ctx=%p\n", db, iDb, pParse, zLeft, zRight, ctx));
9191

92+
if( sqlite3StrICmp(zLeft, "cipher_fips_status")== 0 && !zRight ){
93+
if(ctx) {
94+
char *fips_mode_status = sqlite3_mprintf("%d", sqlcipher_codec_fips_status(ctx));
95+
codec_vdbe_return_static_string(pParse, "cipher_fips_status", fips_mode_status);
96+
sqlite3_free(fips_mode_status);
97+
}
98+
} else
9299
if( sqlite3StrICmp(zLeft, "cipher_store_pass")==0 && zRight ) {
93100
sqlcipher_codec_set_store_pass(ctx, sqlite3GetBoolean(zRight, 1));
94101
} else

src/crypto.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,12 @@
4444
#define FILE_HEADER_SZ 16
4545

4646
#ifndef CIPHER_VERSION
47+
#ifdef SQLCIPHER_FIPS
48+
#define CIPHER_VERSION "3.2.0 FIPS"
49+
#else
4750
#define CIPHER_VERSION "3.2.0"
4851
#endif
52+
#endif
4953

5054
#ifndef CIPHER
5155
#define CIPHER "aes-256-cbc"
@@ -219,9 +223,10 @@ int sqlcipher_codec_ctx_migrate(codec_ctx *ctx);
219223
int sqlcipher_codec_add_random(codec_ctx *ctx, const char *data, int random_sz);
220224
int sqlcipher_cipher_profile(sqlite3 *db, const char *destination);
221225
static void sqlcipher_profile_callback(void *file, const char *sql, sqlite3_uint64 run_time);
222-
int sqlcipher_codec_get_store_pass(codec_ctx *ctx);
223-
void sqlcipher_codec_get_pass(codec_ctx *ctx, void **zKey, int *nKey);
224-
void sqlcipher_codec_set_store_pass(codec_ctx *ctx, int value);
226+
static int sqlcipher_codec_get_store_pass(codec_ctx *ctx);
227+
static void sqlcipher_codec_get_pass(codec_ctx *ctx, void **zKey, int *nKey);
228+
static void sqlcipher_codec_set_store_pass(codec_ctx *ctx, int value);
229+
int sqlcipher_codec_fips_status(codec_ctx *ctx);
225230

226231
#endif
227232
#endif

src/crypto_cc.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ static int sqlcipher_cc_ctx_free(void **ctx) {
120120
return SQLITE_OK;
121121
}
122122

123+
static int sqlcipher_cc_fips_status(void *ctx) {
124+
return 0;
125+
}
126+
123127
int sqlcipher_cc_setup(sqlcipher_provider *p) {
124128
p->random = sqlcipher_cc_random;
125129
p->get_provider_name = sqlcipher_cc_get_provider_name;
@@ -137,6 +141,7 @@ int sqlcipher_cc_setup(sqlcipher_provider *p) {
137141
p->ctx_init = sqlcipher_cc_ctx_init;
138142
p->ctx_free = sqlcipher_cc_ctx_free;
139143
p->add_random = sqlcipher_cc_add_random;
144+
p->fips_status = sqlcipher_cc_fips_status;
140145
return SQLITE_OK;
141146
}
142147

src/crypto_impl.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,6 +1227,9 @@ static void sqlcipher_profile_callback(void *file, const char *sql, sqlite3_uint
12271227
if( f ) fprintf(f, "Elapsed time:%.3f ms - %s\n", elapsed, sql);
12281228
}
12291229

1230+
int sqlcipher_codec_fips_status(codec_ctx *ctx) {
1231+
return ctx->read_ctx->provider->fips_status(ctx->read_ctx);
1232+
}
12301233

12311234
#endif
12321235
/* END SQLCIPHER */

src/crypto_libtomcrypt.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,10 @@ static int sqlcipher_ltc_ctx_free(void **ctx) {
227227
return SQLITE_OK;
228228
}
229229

230+
static int sqlcipher_ltc_fips_status(void *ctx) {
231+
return 0;
232+
}
233+
230234
int sqlcipher_ltc_setup(sqlcipher_provider *p) {
231235
p->activate = sqlcipher_ltc_activate;
232236
p->deactivate = sqlcipher_ltc_deactivate;
@@ -246,6 +250,7 @@ int sqlcipher_ltc_setup(sqlcipher_provider *p) {
246250
p->ctx_init = sqlcipher_ltc_ctx_init;
247251
p->ctx_free = sqlcipher_ltc_ctx_free;
248252
p->add_random = sqlcipher_ltc_add_random;
253+
p->fips_status = sqlcipher_ltc_fips_status;
249254
return SQLITE_OK;
250255
}
251256

src/crypto_openssl.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ typedef struct {
4242
EVP_CIPHER *evp_cipher;
4343
} openssl_ctx;
4444

45-
45+
static int openssl_fips_status = 0;
4646
static unsigned int openssl_external_init = 0;
4747
static unsigned int openssl_init_count = 0;
4848
static sqlite3_mutex* openssl_rand_mutex = NULL;
@@ -78,6 +78,13 @@ static int sqlcipher_openssl_activate(void *ctx) {
7878
}
7979

8080
if(openssl_init_count == 0 && openssl_external_init == 0) {
81+
#ifdef SQLCIPHER_FIPS
82+
openssl_fips_status = FIPS_mode_set(1);
83+
if(!openssl_fips_status){
84+
ERR_load_crypto_strings();
85+
ERR_print_errors_fp(stdout);
86+
}
87+
#endif
8188
/* if the library was not externally initialized, then should be now */
8289
OpenSSL_add_all_algorithms();
8390
}
@@ -224,6 +231,10 @@ static int sqlcipher_openssl_ctx_free(void **ctx) {
224231
return SQLITE_OK;
225232
}
226233

234+
static int sqlcipher_openssl_fips_status(void *ctx) {
235+
return openssl_fips_status;
236+
}
237+
227238
int sqlcipher_openssl_setup(sqlcipher_provider *p) {
228239
p->activate = sqlcipher_openssl_activate;
229240
p->deactivate = sqlcipher_openssl_deactivate;
@@ -243,6 +254,7 @@ int sqlcipher_openssl_setup(sqlcipher_provider *p) {
243254
p->ctx_init = sqlcipher_openssl_ctx_init;
244255
p->ctx_free = sqlcipher_openssl_ctx_free;
245256
p->add_random = sqlcipher_openssl_add_random;
257+
p->fips_status = sqlcipher_openssl_fips_status;
246258
return SQLITE_OK;
247259
}
248260

src/sqlcipher.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ typedef struct {
5555
int (*ctx_cmp)(void *c1, void *c2);
5656
int (*ctx_init)(void **ctx);
5757
int (*ctx_free)(void **ctx);
58+
int (*fips_status)(void *ctx);
5859
} sqlcipher_provider;
5960

6061
/* utility functions */

0 commit comments

Comments
 (0)