Skip to content

Commit e1d83a5

Browse files
committed
check the return value of RAND_bytes()
use OPENSSL_cleanse()
1 parent 67c050a commit e1d83a5

11 files changed

Lines changed: 46 additions & 33 deletions

File tree

ChangeLog

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2019-01-01 Jim Zubov <jz@vesvault.com>
2+
* * Usage of RAND_bytes() and OPENSSL_cleanup()
3+
credits: https://reddit.com/u/skeeto

configure

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#! /bin/sh
22
# Guess values for system-dependent variables and create Makefiles.
3-
# Generated by GNU Autoconf 2.69 for libVES-c 0.9b.
3+
# Generated by GNU Autoconf 2.69 for VES 0.901b.
44
#
55
# Report bugs to <bugs@vesvault.com>.
66
#
@@ -578,10 +578,10 @@ MFLAGS=
578578
MAKEFLAGS=
579579

580580
# Identity of this package.
581-
PACKAGE_NAME='libVES-c'
582-
PACKAGE_TARNAME='libves-c'
583-
PACKAGE_VERSION='0.9b'
584-
PACKAGE_STRING='libVES-c 0.9b'
581+
PACKAGE_NAME='VES'
582+
PACKAGE_TARNAME='ves'
583+
PACKAGE_VERSION='0.901b'
584+
PACKAGE_STRING='VES 0.901b'
585585
PACKAGE_BUGREPORT='bugs@vesvault.com'
586586
PACKAGE_URL=''
587587

@@ -1261,7 +1261,7 @@ if test "$ac_init_help" = "long"; then
12611261
# Omit some internal or obsolete options to make the list less imposing.
12621262
# This message is too long to be a string in the A/UX 3.1 sh.
12631263
cat <<_ACEOF
1264-
\`configure' configures libVES-c 0.9b to adapt to many kinds of systems.
1264+
\`configure' configures VES 0.901b to adapt to many kinds of systems.
12651265
12661266
Usage: $0 [OPTION]... [VAR=VALUE]...
12671267
@@ -1309,7 +1309,7 @@ Fine tuning of the installation directories:
13091309
--infodir=DIR info documentation [DATAROOTDIR/info]
13101310
--localedir=DIR locale-dependent data [DATAROOTDIR/locale]
13111311
--mandir=DIR man documentation [DATAROOTDIR/man]
1312-
--docdir=DIR documentation root [DATAROOTDIR/doc/libves-c]
1312+
--docdir=DIR documentation root [DATAROOTDIR/doc/ves]
13131313
--htmldir=DIR html documentation [DOCDIR]
13141314
--dvidir=DIR dvi documentation [DOCDIR]
13151315
--pdfdir=DIR pdf documentation [DOCDIR]
@@ -1327,7 +1327,7 @@ fi
13271327

13281328
if test -n "$ac_init_help"; then
13291329
case $ac_init_help in
1330-
short | recursive ) echo "Configuration of libVES-c 0.9b:";;
1330+
short | recursive ) echo "Configuration of VES 0.901b:";;
13311331
esac
13321332
cat <<\_ACEOF
13331333
@@ -1414,7 +1414,7 @@ fi
14141414
test -n "$ac_init_help" && exit $ac_status
14151415
if $ac_init_version; then
14161416
cat <<\_ACEOF
1417-
libVES-c configure 0.9b
1417+
VES configure 0.901b
14181418
generated by GNU Autoconf 2.69
14191419
14201420
Copyright (C) 2012 Free Software Foundation, Inc.
@@ -1837,7 +1837,7 @@ cat >config.log <<_ACEOF
18371837
This file contains any messages produced by compilers while
18381838
running configure, to aid debugging if configure makes a mistake.
18391839
1840-
It was created by libVES-c $as_me 0.9b, which was
1840+
It was created by VES $as_me 0.901b, which was
18411841
generated by GNU Autoconf 2.69. Invocation command line was
18421842
18431843
$ $0 $@
@@ -2655,8 +2655,8 @@ fi
26552655
26562656
26572657
# Define the identity of the package.
2658-
PACKAGE='libves-c'
2659-
VERSION='0.9b'
2658+
PACKAGE='ves'
2659+
VERSION='0.901b'
26602660
26612661
26622662
cat >>confdefs.h <<_ACEOF
@@ -5006,7 +5006,7 @@ cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1
50065006
# report actual input values of CONFIG_FILES etc. instead of their
50075007
# values after options handling.
50085008
ac_log="
5009-
This file was extended by libVES-c $as_me 0.9b, which was
5009+
This file was extended by VES $as_me 0.901b, which was
50105010
generated by GNU Autoconf 2.69. Invocation command line was
50115011
50125012
CONFIG_FILES = $CONFIG_FILES
@@ -5072,7 +5072,7 @@ _ACEOF
50725072
cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
50735073
ac_cs_config="`$as_echo "$ac_configure_args" | sed 's/^ //; s/[\\""\`\$]/\\\\&/g'`"
50745074
ac_cs_version="\\
5075-
libVES-c config.status 0.9b
5075+
VES config.status 0.901b
50765076
configured by $0, generated by GNU Autoconf 2.69,
50775077
with options \\"\$ac_cs_config\\"
50785078

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44

55
AC_PREREQ([2.69])
6-
AC_INIT([libVES-c], [0.9b], [bugs@vesvault.com])
6+
AC_INIT([VES], [0.901b], [bugs@vesvault.com])
77
AC_CONFIG_SRCDIR([src/ves-util.c])
88
AC_CONFIG_HEADERS([src/config.h])
99

lib/libVES.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
* libVES.h libVES: Main header
2929
*
3030
***************************************************************************/
31-
#define LIBVES_VERSION_NUMBER 0x00090000L
32-
#define LIBVES_VERSION_CODE "0.9b"
31+
#define LIBVES_VERSION_NUMBER 0x00090001L
32+
#define LIBVES_VERSION_CODE "0.901b"
3333
#define LIBVES_VERSION_STR "libVES.c " LIBVES_VERSION_CODE " (c) 2018 VESvault Corp"
3434
#define LIBVES_VERSION_SHORT "libVES/" LIBVES_VERSION_CODE
3535

lib/libVES/CiAlgo_AES.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include <openssl/bio.h>
3838
#include <openssl/pem.h>
3939
#include <openssl/engine.h>
40+
#include <openssl/crypto.h>
4041
#include "Cipher.h"
4142
#include "CiAlgo_AES.h"
4243
#include "Util.h"
@@ -50,7 +51,7 @@
5051
memcpy(ci->alg.key, key, sizeof(ci->alg.key)); \
5152
memcpy(ci->alg.seed, key + (keylen <= sizeof(ci->alg.key) + sizeof(ci->alg.seed) ? keylen - sizeof(ci->alg.seed) : sizeof(ci->alg.key)), sizeof(ci->alg.seed)); \
5253
} else { \
53-
RAND_bytes(ci->alg.key, sizeof(ci->alg.key) + sizeof(ci->alg.seed)); \
54+
if (RAND_bytes(ci->alg.key, sizeof(ci->alg.key) + sizeof(ci->alg.seed)) <= 0) libVES_throwEVP(ves, LIBVES_E_CRYPTO, "RAND_bytes", NULL); \
5455
}
5556

5657
#define libVES_CiAlgo_LEN_1K 1024
@@ -218,7 +219,7 @@ int libVES_CiAlgo_e_AES256GCMp(libVES_Cipher *ci, int final, const char *plainte
218219
if (ci->gcm.offs) {
219220
l = libVES_CiAlgo_e_AES256GCM(ci, 0, ci->gcm.pbuf, ci->gcm.offs, ctext);
220221
if (l < 0) return -1;
221-
memset(ci->gcm.pbuf, 0, ci->gcm.offs);
222+
OPENSSL_cleanse(ci->gcm.pbuf, ci->gcm.offs);
222223
ctext += l;
223224
}
224225
l = libVES_CiAlgo_e_AES256GCM(ci, 0, plaintext, ptlen, ctext);
@@ -327,7 +328,7 @@ int libVES_CiAlgo_e_AES256GCM1K(libVES_Cipher *ci, int final, const char *plaint
327328
while (ptext < ptail || ff) {
328329
ff = 0;
329330
if (!ci->gcm.offs) {
330-
RAND_bytes((unsigned char *) ctext, sizeof(ci->gcm.gbuf));
331+
if (RAND_bytes((unsigned char *) ctext, sizeof(ci->gcm.gbuf)) <= 0) libVES_throwEVP(ci->ves, LIBVES_E_CRYPTO, "RAND_bytes", -1);
331332
if (!libVES_CiAlgo_setiv_AES256GCM1K(ci, ctext)) return -1;
332333
ctext += sizeof(ci->gcm.gbuf);
333334
ci->gcm.offs = sizeof(ci->gcm.gbuf);
@@ -352,7 +353,7 @@ int libVES_CiAlgo_e_AES256GCM1K(libVES_Cipher *ci, int final, const char *plaint
352353

353354
void libVES_CiAlgo_r_AES256GCMp(libVES_Cipher *ci) {
354355
if (ci->gcm.pbuf) {
355-
memset(ci->gcm.pbuf, 0, ci->gcm.offs);
356+
OPENSSL_cleanse(ci->gcm.pbuf, ci->gcm.offs);
356357
free(ci->gcm.pbuf);
357358
ci->gcm.pbuf = NULL;
358359
}
@@ -460,7 +461,7 @@ int libVES_CiAlgo_l_AES256CFB(libVES_Cipher *ci) {
460461
}
461462

462463
void libVES_CiAlgo_f_AES256(libVES_Cipher *ci) {
463-
memset(ci->gcm.key, 0, sizeof(ci->gcm.key));
464+
OPENSSL_cleanse(ci->gcm.key, sizeof(ci->gcm.key));
464465
}
465466

466467

lib/libVES/KeyAlgo_EVP.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include <openssl/bio.h>
3838
#include <openssl/pem.h>
3939
#include <openssl/engine.h>
40+
#include <openssl/crypto.h>
4041
#include "../jVar.h"
4142
#include "../libVES.h"
4243
#include "VaultKey.h"
@@ -197,7 +198,7 @@ int libVES_KeyAlgo_RSA_decrypt(libVES_VaultKey *vkey, const char *ciphertext, si
197198
res = 0;
198199
} else res = dlen;
199200
}
200-
if (keybuf) memset(keybuf, 0, len);
201+
if (keybuf) OPENSSL_cleanse(keybuf, len);
201202
free(keybuf);
202203
EVP_PKEY_CTX_free(ctx);
203204
return res;
@@ -212,7 +213,7 @@ int libVES_KeyAlgo_RSA_encrypt(libVES_VaultKey *vkey, const char *plaintext, siz
212213
if (*ptlen + libVES_KeyAlgo_RSA_LENpad > len && *ptlen > *keylen) {
213214
*ptlen = 0;
214215
if (!ciphertext) return len;
215-
RAND_bytes((unsigned char *) key, *keylen);
216+
if (RAND_bytes((unsigned char *) key, *keylen) <= 0) libVES_throwEVP(vkey->ves, LIBVES_E_CRYPTO, "RAND_bytes", -1);
216217
s = key;
217218
sl = *keylen;
218219
} else {
@@ -284,7 +285,7 @@ int libVES_KeyAlgo_ECDH_derive(EVP_PKEY *pub, EVP_PKEY *priv, char *buf, size_t
284285
}
285286
if (mdctx) EVP_MD_CTX_destroy(mdctx);
286287
}
287-
memset(dh, 0, sizeof(dh));
288+
OPENSSL_cleanse(dh, sizeof(dh));
288289
EVP_PKEY_CTX_free(ctx);
289290
return res;
290291
}

lib/libVES/VaultItem.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <stdlib.h>
3434
#include <string.h>
3535
#include <stdio.h>
36+
#include <openssl/crypto.h>
3637
#include "../jVar.h"
3738
#include "../libVES.h"
3839
#include "VaultItem.h"
@@ -369,7 +370,7 @@ void libVES_VaultItem_free(libVES_VaultItem *vitem) {
369370
if (!vitem) return;
370371
jVar_free(vitem->meta);
371372
if (vitem->value) {
372-
memset(vitem->value, 0, vitem->len);
373+
OPENSSL_cleanse(vitem->value, vitem->len);
373374
free(vitem->value);
374375
vitem->len = 0;
375376
}

lib/libVES/VaultKey.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <openssl/bio.h>
3737
#include <openssl/pem.h>
3838
#include <openssl/engine.h>
39+
#include <openssl/crypto.h>
3940
#include "../jVar.h"
4041
#include "../libVES.h"
4142
#include "VaultKey.h"
@@ -382,7 +383,7 @@ int libVES_VaultKey_decrypt(libVES_VaultKey *vkey, const char *ciphertext, char
382383
else pl = -1;
383384
}
384385
free(ctext);
385-
memset(cikey, 0, sizeof(cikey));
386+
OPENSSL_cleanse(cikey, sizeof(cikey));
386387
return pl;
387388
}
388389

@@ -410,7 +411,7 @@ char *libVES_VaultKey_encrypt(libVES_VaultKey *vkey, const char *plaintext, size
410411
if (l >= 0) cl += l;
411412
else cl = -1;
412413
}
413-
memset(cikey, 0, sizeof(cikey));
414+
OPENSSL_cleanse(cikey, sizeof(cikey));
414415
if (cl >= 0) return libVES_b64encode(ctext, cl, ctbuf);
415416
free(ctbuf);
416417
return NULL;
@@ -564,7 +565,7 @@ libVES_veskey *libVES_veskey_new(size_t keylen, const char *veskey) {
564565
libVES_veskey *vk = malloc(offsetof(libVES_veskey, veskey) + keylen);
565566
if (veskey) memcpy(vk->veskey, veskey, keylen);
566567
else {
567-
RAND_bytes((unsigned char *) vk->veskey, keylen);
568+
if (RAND_bytes((unsigned char *) vk->veskey, keylen) <= 0) return NULL;
568569
char *p;
569570
for (p = vk->veskey; p < vk->veskey + keylen; p++) {
570571
unsigned char c = *p;
@@ -577,6 +578,6 @@ libVES_veskey *libVES_veskey_new(size_t keylen, const char *veskey) {
577578

578579
void libVES_veskey_free(libVES_veskey *veskey) {
579580
if (!veskey) return;
580-
memset(veskey, 0, veskey->keylen + sizeof(veskey->keylen));
581+
OPENSSL_cleanse(veskey, veskey->keylen + sizeof(veskey->keylen));
581582
free(veskey);
582583
}

lib/libVES/VaultKey.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,16 @@ extern const struct libVES_ListCtl libVES_VaultKey_ListCtlU;
210210

211211
/***************************************************************************
212212
* VESkey, a binary with length
213+
* Generate a random one if veskey == NULL, see libVES_veskey_generate()
213214
***************************************************************************/
214215
libVES_veskey *libVES_veskey_new(size_t keylen, const char *veskey);
215216

216217
/***************************************************************************
217218
* Generate a random ascii VESkey
219+
* The character frequency is biased to improve human readability,
220+
* the entropy is ~ 203 bit for keylen == 32 (vs 256 bit for a random binary)
221+
* Character frequency graph:
222+
* https://i.imgur.com/o2oTDLz.png (credits: https://reddit.com/u/skeeto)
218223
***************************************************************************/
219224
#define libVES_veskey_generate(keylen) libVES_veskey_new(keylen, NULL)
220225

src/ves-util.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
#include <libVES/Ref.h>
5050
#include <libVES/Cipher.h>
5151
#include <libVES/User.h>
52+
#include <openssl/crypto.h>
5253
#include "ves-util.h"
5354
#include "ves-util/put.h"
5455
#include "ves-util/get.h"
@@ -515,7 +516,7 @@ int main(int argc, char **argv) {
515516
char *passwd = get_noecho(msg, &gl, NULL);
516517
if (!passwd) break;
517518
pvkey = libVES_primary(ctx.ves, pri, passwd);
518-
memset(passwd, 0, strlen(passwd));
519+
OPENSSL_cleanse(passwd, strlen(passwd));
519520
free(passwd);
520521
if (pvkey) break;
521522
if (--retry <= 0 || !libVES_checkError(ctx.ves, LIBVES_E_DENIED)) break;
@@ -530,7 +531,7 @@ int main(int argc, char **argv) {
530531
char *vk = get_noecho(msg, &gl, NULL);
531532
if (!vk) break;
532533
libVES_veskey *veskey = libVES_veskey_new(strlen(vk), vk);
533-
memset(vk, 0, gl);
534+
OPENSSL_cleanse(vk, gl);
534535
free(vk);
535536
unlk = libVES_VaultKey_unlock(pvkey, veskey);
536537
libVES_veskey_free(veskey);

0 commit comments

Comments
 (0)