Skip to content

Commit c44622a

Browse files
authored
Merge pull request gost-engine#454 from arx11/missing_buffer_cleanses
Added missing buffer cleanses for some sensitive buffers
2 parents fea06f9 + 295b988 commit c44622a

4 files changed

Lines changed: 18 additions & 6 deletions

File tree

gost_crypt.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,7 @@ static int magma_cipher_init_ctr_acpkm_omac(EVP_CIPHER_CTX *ctx, const unsigned
529529
if (key) {
530530
struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
531531
unsigned char cipher_key[32];
532+
int ret;
532533
c->omac_ctx = EVP_MD_CTX_new();
533534

534535
if (c->omac_ctx == NULL) {
@@ -543,7 +544,9 @@ static int magma_cipher_init_ctr_acpkm_omac(EVP_CIPHER_CTX *ctx, const unsigned
543544
return 0;
544545
}
545546

546-
return magma_cipher_init(ctx, cipher_key, iv, enc);
547+
ret = magma_cipher_init(ctx, cipher_key, iv, enc);
548+
OPENSSL_cleanse(cipher_key, sizeof(cipher_key));
549+
return ret;
547550
}
548551

549552
return magma_cipher_init(ctx, key, iv, enc);
@@ -1324,6 +1327,7 @@ static int magma_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
13241327
memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), adjusted_iv, 8);
13251328

13261329
magma_key(c, newkey);
1330+
OPENSSL_cleanse(newkey, sizeof(newkey));
13271331
return 1;
13281332
}
13291333
}

gost_grasshopper_cipher.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ static void acpkm_next(gost_grasshopper_cipher_ctx * c)
175175
&c->buffer);
176176
}
177177
gost_grasshopper_cipher_key(c, newkey);
178+
OPENSSL_cleanse(newkey, sizeof(newkey));
178179
}
179180

180181
/* Set 256 bit key into context */
@@ -351,6 +352,7 @@ gost_grasshopper_cipher_init_ctracpkm_omac(EVP_CIPHER_CTX
351352

352353
if (key) {
353354
unsigned char cipher_key[32];
355+
int ret;
354356
c->omac_ctx = EVP_MD_CTX_new();
355357

356358
if (c->omac_ctx == NULL) {
@@ -365,7 +367,9 @@ gost_grasshopper_cipher_init_ctracpkm_omac(EVP_CIPHER_CTX
365367
return 0;
366368
}
367369

368-
return gost_grasshopper_cipher_init(ctx, cipher_key, iv, enc);
370+
ret = gost_grasshopper_cipher_init(ctx, cipher_key, iv, enc);
371+
OPENSSL_cleanse(cipher_key, sizeof(cipher_key));
372+
return ret;
369373
}
370374

371375
return gost_grasshopper_cipher_init(ctx, key, iv, enc);
@@ -1158,6 +1162,7 @@ static int gost_grasshopper_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, v
11581162
memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), adjusted_iv, 16);
11591163

11601164
gost_grasshopper_cipher_key(c, newkey);
1165+
OPENSSL_cleanse(newkey, sizeof(newkey));
11611166
return 1;
11621167
}
11631168
}

gost_keyexpimp.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ int gost_tlstree(int cipher_nid, const unsigned char *in, unsigned char *out,
269269
uint64_t seed1, seed2, seed3;
270270
uint64_t seq;
271271
unsigned char ko1[32], ko2[32];
272+
int ret;
272273

273274
switch (cipher_nid) {
274275
case NID_magma_cbc:
@@ -293,15 +294,16 @@ int gost_tlstree(int cipher_nid, const unsigned char *in, unsigned char *out,
293294
seed2 = seq & c2;
294295
seed3 = seq & c3;
295296

296-
if (gost_kdftree2012_256(ko1, 32, in, 32, (const unsigned char *)"level1", 6,
297+
ret = !(gost_kdftree2012_256(ko1, 32, in, 32, (const unsigned char *)"level1", 6,
297298
(const unsigned char *)&seed1, 8, 1) <= 0
298299
|| gost_kdftree2012_256(ko2, 32, ko1, 32, (const unsigned char *)"level2", 6,
299300
(const unsigned char *)&seed2, 8, 1) <= 0
300301
|| gost_kdftree2012_256(out, 32, ko2, 32, (const unsigned char *)"level3", 6,
301-
(const unsigned char *)&seed3, 8, 1) <= 0)
302-
return 0;
302+
(const unsigned char *)&seed3, 8, 1) <= 0);
303303

304-
return 1;
304+
OPENSSL_cleanse(ko1, sizeof(ko1));
305+
OPENSSL_cleanse(ko2, sizeof(ko2));
306+
return ret;
305307
}
306308

307309
#define GOST_WRAP_FLAGS EVP_CIPH_CTRL_INIT | EVP_CIPH_WRAP_MODE | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER | EVP_CIPH_FLAG_DEFAULT_ASN1

gost_omac.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ int omac_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
249249
|| (cipher = EVP_CIPHER_fetch(NULL, c->cipher_name, NULL)))
250250
ret = omac_key(c, cipher, diversed_key, 32);
251251
EVP_CIPHER_free(cipher);
252+
OPENSSL_cleanse(diversed_key, sizeof(diversed_key));
252253
}
253254
return ret;
254255
}

0 commit comments

Comments
 (0)