Latest update

This commit is contained in:
2019-04-26 08:41:22 +09:00
parent 704c3822e0
commit a7bf77581f
91 changed files with 4677 additions and 389 deletions
+154 -8
View File
@@ -18,28 +18,170 @@ static int test_bio_memleak(void)
int ok = 0;
BIO *bio;
BUF_MEM bufmem;
const char *str = "BIO test\n";
static const char str[] = "BIO test\n";
char buf[100];
bio = BIO_new(BIO_s_mem());
if (bio == NULL)
if (!TEST_ptr(bio))
goto finish;
bufmem.length = strlen(str) + 1;
bufmem.length = sizeof(str);
bufmem.data = (char *) str;
bufmem.max = bufmem.length;
BIO_set_mem_buf(bio, &bufmem, BIO_NOCLOSE);
BIO_set_flags(bio, BIO_FLAGS_MEM_RDONLY);
if (BIO_read(bio, buf, sizeof(buf)) <= 0)
goto finish;
ok = strcmp(buf, str) == 0;
if (!TEST_int_eq(BIO_read(bio, buf, sizeof(buf)), sizeof(str)))
goto finish;
if (!TEST_mem_eq(buf, sizeof(str), str, sizeof(str)))
goto finish;
ok = 1;
finish:
BIO_free(bio);
return ok;
}
static int test_bio_get_mem(void)
{
int ok = 0;
BIO *bio = NULL;
BUF_MEM *bufmem = NULL;
bio = BIO_new(BIO_s_mem());
if (!TEST_ptr(bio))
goto finish;
if (!TEST_int_eq(BIO_puts(bio, "Hello World\n"), 12))
goto finish;
BIO_get_mem_ptr(bio, &bufmem);
if (!TEST_ptr(bufmem))
goto finish;
if (!TEST_int_gt(BIO_set_close(bio, BIO_NOCLOSE), 0))
goto finish;
BIO_free(bio);
bio = NULL;
if (!TEST_mem_eq(bufmem->data, bufmem->length, "Hello World\n", 12))
goto finish;
ok = 1;
finish:
BIO_free(bio);
BUF_MEM_free(bufmem);
return ok;
}
static int test_bio_new_mem_buf(void)
{
int ok = 0;
BIO *bio;
BUF_MEM *bufmem;
char data[16];
bio = BIO_new_mem_buf("Hello World\n", 12);
if (!TEST_ptr(bio))
goto finish;
if (!TEST_int_eq(BIO_read(bio, data, 5), 5))
goto finish;
if (!TEST_mem_eq(data, 5, "Hello", 5))
goto finish;
if (!TEST_int_gt(BIO_get_mem_ptr(bio, &bufmem), 0))
goto finish;
if (!TEST_int_lt(BIO_write(bio, "test", 4), 0))
goto finish;
if (!TEST_int_eq(BIO_read(bio, data, 16), 7))
goto finish;
if (!TEST_mem_eq(data, 7, " World\n", 7))
goto finish;
if (!TEST_int_gt(BIO_reset(bio), 0))
goto finish;
if (!TEST_int_eq(BIO_read(bio, data, 16), 12))
goto finish;
if (!TEST_mem_eq(data, 12, "Hello World\n", 12))
goto finish;
ok = 1;
finish:
BIO_free(bio);
return ok;
}
static int test_bio_rdonly_mem_buf(void)
{
int ok = 0;
BIO *bio, *bio2 = NULL;
BUF_MEM *bufmem;
char data[16];
bio = BIO_new_mem_buf("Hello World\n", 12);
if (!TEST_ptr(bio))
goto finish;
if (!TEST_int_eq(BIO_read(bio, data, 5), 5))
goto finish;
if (!TEST_mem_eq(data, 5, "Hello", 5))
goto finish;
if (!TEST_int_gt(BIO_get_mem_ptr(bio, &bufmem), 0))
goto finish;
(void)BIO_set_close(bio, BIO_NOCLOSE);
bio2 = BIO_new(BIO_s_mem());
if (!TEST_ptr(bio2))
goto finish;
BIO_set_mem_buf(bio2, bufmem, BIO_CLOSE);
BIO_set_flags(bio2, BIO_FLAGS_MEM_RDONLY);
if (!TEST_int_eq(BIO_read(bio2, data, 16), 7))
goto finish;
if (!TEST_mem_eq(data, 7, " World\n", 7))
goto finish;
if (!TEST_int_gt(BIO_reset(bio2), 0))
goto finish;
if (!TEST_int_eq(BIO_read(bio2, data, 16), 7))
goto finish;
if (!TEST_mem_eq(data, 7, " World\n", 7))
goto finish;
ok = 1;
finish:
BIO_free(bio);
BIO_free(bio2);
return ok;
}
static int test_bio_rdwr_rdonly(void)
{
int ok = 0;
BIO *bio = NULL;
char data[16];
bio = BIO_new(BIO_s_mem());
if (!TEST_ptr(bio))
goto finish;
if (!TEST_int_eq(BIO_puts(bio, "Hello World\n"), 12))
goto finish;
BIO_set_flags(bio, BIO_FLAGS_MEM_RDONLY);
if (!TEST_int_eq(BIO_read(bio, data, 16), 12))
goto finish;
if (!TEST_mem_eq(data, 12, "Hello World\n", 12))
goto finish;
if (!TEST_int_gt(BIO_reset(bio), 0))
goto finish;
BIO_clear_flags(bio, BIO_FLAGS_MEM_RDONLY);
if (!TEST_int_eq(BIO_puts(bio, "Hi!\n"), 4))
goto finish;
if (!TEST_int_eq(BIO_read(bio, data, 16), 16))
goto finish;
if (!TEST_mem_eq(data, 16, "Hello World\nHi!\n", 16))
goto finish;
ok = 1;
finish:
BIO_free(bio);
return ok;
}
int global_init(void)
{
CRYPTO_set_mem_debug(1);
@@ -50,5 +192,9 @@ int global_init(void)
int setup_tests(void)
{
ADD_TEST(test_bio_memleak);
ADD_TEST(test_bio_get_mem);
ADD_TEST(test_bio_new_mem_buf);
ADD_TEST(test_bio_rdonly_mem_buf);
ADD_TEST(test_bio_rdwr_rdonly);
return 1;
}
+68
View File
@@ -1954,6 +1954,73 @@ static int test_rand(void)
return st;
}
/*
* Run some statistical tests to provide a degree confidence that the
* BN_rand_range() function works as expected. The critical value
* is computed using the R statistical suite:
*
* qchisq(alpha, df=iterations - 1)
*
* where alpha is the significance level (0.95 is used here) and iterations
* is the number of samples being drawn.
*/
static const struct {
unsigned int range;
unsigned int iterations;
double critical;
} rand_range_cases[] = {
{ 2, 100, 123.2252 /* = qchisq(.95, df=99) */ },
{ 12, 1000, 1073.643 /* = qchisq(.95, df=999) */ },
{ 1023, 100000, 100735.7 /* = qchisq(.95, df=99999) */ },
};
static int test_rand_range(int n)
{
const unsigned int range = rand_range_cases[n].range;
const unsigned int iterations = rand_range_cases[n].iterations;
const double critical = rand_range_cases[n].critical;
const double expected = iterations / (double)range;
double sum = 0;
BIGNUM *rng = NULL, *val = NULL;
size_t *counts;
unsigned int i, v;
int res = 0;
if (!TEST_ptr(counts = OPENSSL_zalloc(sizeof(*counts) * range))
|| !TEST_ptr(rng = BN_new())
|| !TEST_ptr(val = BN_new())
|| !TEST_true(BN_set_word(rng, range)))
goto err;
for (i = 0; i < iterations; i++) {
if (!TEST_true(BN_rand_range(val, rng))
|| !TEST_uint_lt(v = (unsigned int)BN_get_word(val), range))
goto err;
counts[v]++;
}
TEST_note("range %u iterations %u critical %.4f", range, iterations,
critical);
if (range < 20) {
TEST_note("frequencies (expected %.2f)", expected);
for (i = 0; i < range; i++)
TEST_note(" %2u %6zu", i, counts[i]);
}
for (i = 0; i < range; i++) {
const double delta = counts[i] - expected;
sum += delta * delta;
}
sum /= expected;
TEST_note("test statistic %.4f", sum);
if (TEST_double_lt(sum, critical))
res = 1;
err:
BN_free(rng);
BN_free(val);
OPENSSL_free(counts);
return res;
}
static int test_negzero(void)
{
BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL;
@@ -2432,6 +2499,7 @@ int setup_tests(void)
#endif
ADD_ALL_TESTS(test_is_prime, (int)OSSL_NELEM(primes));
ADD_ALL_TESTS(test_not_prime, (int)OSSL_NELEM(not_primes));
ADD_ALL_TESTS(test_rand_range, OSSL_NELEM(rand_range_cases));
} else {
ADD_ALL_TESTS(run_file_tests, n);
}
+4 -1
View File
@@ -1884,11 +1884,14 @@ static int check_ec_key_field_public_range_test(int id)
* be the same point on the curve). The add is different for char2 fields.
*/
type = EC_METHOD_get_field_type(meth);
#ifndef OPENSSL_NO_EC2M
if (type == NID_X9_62_characteristic_two_field) {
/* test for binary curves */
if (!TEST_true(BN_GF2m_add(x, x, field)))
goto err;
} else if (type == NID_X9_62_prime_field) {
} else
#endif
if (type == NID_X9_62_prime_field) {
/* test for prime curves */
if (!TEST_true(BN_add(x, x, field)))
goto err;
+55
View File
@@ -253,6 +253,60 @@ static int test_kdf_ss_kmac(void)
return ret;
}
static int test_kdf_sshkdf(void)
{
int ret;
EVP_KDF_CTX *kctx;
unsigned char out[8];
/* Test data from NIST CAVS 14.1 test vectors */
const unsigned char key[] = {
0x00, 0x00, 0x00, 0x81, 0x00, 0x87, 0x5c, 0x55, 0x1c, 0xef, 0x52, 0x6a,
0x4a, 0x8b, 0xe1, 0xa7, 0xdf, 0x27, 0xe9, 0xed, 0x35, 0x4b, 0xac, 0x9a,
0xfb, 0x71, 0xf5, 0x3d, 0xba, 0xe9, 0x05, 0x67, 0x9d, 0x14, 0xf9, 0xfa,
0xf2, 0x46, 0x9c, 0x53, 0x45, 0x7c, 0xf8, 0x0a, 0x36, 0x6b, 0xe2, 0x78,
0x96, 0x5b, 0xa6, 0x25, 0x52, 0x76, 0xca, 0x2d, 0x9f, 0x4a, 0x97, 0xd2,
0x71, 0xf7, 0x1e, 0x50, 0xd8, 0xa9, 0xec, 0x46, 0x25, 0x3a, 0x6a, 0x90,
0x6a, 0xc2, 0xc5, 0xe4, 0xf4, 0x8b, 0x27, 0xa6, 0x3c, 0xe0, 0x8d, 0x80,
0x39, 0x0a, 0x49, 0x2a, 0xa4, 0x3b, 0xad, 0x9d, 0x88, 0x2c, 0xca, 0xc2,
0x3d, 0xac, 0x88, 0xbc, 0xad, 0xa4, 0xb4, 0xd4, 0x26, 0xa3, 0x62, 0x08,
0x3d, 0xab, 0x65, 0x69, 0xc5, 0x4c, 0x22, 0x4d, 0xd2, 0xd8, 0x76, 0x43,
0xaa, 0x22, 0x76, 0x93, 0xe1, 0x41, 0xad, 0x16, 0x30, 0xce, 0x13, 0x14,
0x4e
};
const unsigned char xcghash[] = {
0x0e, 0x68, 0x3f, 0xc8, 0xa9, 0xed, 0x7c, 0x2f, 0xf0, 0x2d, 0xef, 0x23,
0xb2, 0x74, 0x5e, 0xbc, 0x99, 0xb2, 0x67, 0xda, 0xa8, 0x6a, 0x4a, 0xa7,
0x69, 0x72, 0x39, 0x08, 0x82, 0x53, 0xf6, 0x42
};
const unsigned char sessid[] = {
0x0e, 0x68, 0x3f, 0xc8, 0xa9, 0xed, 0x7c, 0x2f, 0xf0, 0x2d, 0xef, 0x23,
0xb2, 0x74, 0x5e, 0xbc, 0x99, 0xb2, 0x67, 0xda, 0xa8, 0x6a, 0x4a, 0xa7,
0x69, 0x72, 0x39, 0x08, 0x82, 0x53, 0xf6, 0x42
};
const unsigned char expected[sizeof(out)] = {
0x41, 0xff, 0x2e, 0xad, 0x16, 0x83, 0xf1, 0xe6
};
ret = TEST_ptr(kctx = EVP_KDF_CTX_new_id(EVP_KDF_SSHKDF))
&& TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_MD, EVP_sha256()),
0)
&& TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_KEY, key,
sizeof(key)), 0)
&& TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_SSHKDF_XCGHASH,
xcghash, sizeof(xcghash)), 0)
&& TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_SSHKDF_SESSION_ID,
sessid, sizeof(sessid)), 0)
&& TEST_int_gt(
EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_SSHKDF_TYPE,
(int)EVP_KDF_SSHKDF_TYPE_INITIAL_IV_CLI_TO_SRV),
0)
&& TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out)), 0)
&& TEST_mem_eq(out, sizeof(out), expected, sizeof(expected));
EVP_KDF_CTX_free(kctx);
return ret;
}
int setup_tests(void)
{
ADD_TEST(test_kdf_tls1_prf);
@@ -264,5 +318,6 @@ int setup_tests(void)
ADD_TEST(test_kdf_ss_hash);
ADD_TEST(test_kdf_ss_hmac);
ADD_TEST(test_kdf_ss_kmac);
ADD_TEST(test_kdf_sshkdf);
return 1;
}
+78
View File
@@ -0,0 +1,78 @@
#! /usr/bin/env perl
# Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
#
# Licensed under the OpenSSL license (the "License"). You may not use
# this file except in compliance with the License. You can obtain a copy
# in the file LICENSE in the source distribution or at
# https://www.openssl.org/source/license.html
use strict;
use warnings;
use OpenSSL::Test;
use OpenSSL::Test::Utils;
setup("test_kdf");
my @kdf_tests = (
{ cmd => [qw{openssl kdf -keylen 16 -kdfopt digest:SHA256 -kdfopt secret:secret -kdfopt seed:seed TLS1-PRF}],
expected => '8E:4D:93:25:30:D7:65:A0:AA:E9:74:C3:04:73:5E:CC',
desc => 'TLS1-PRF SHA256' },
{ cmd => [qw{openssl kdf -keylen 16 -kdfopt digest:MD5-SHA1 -kdfopt secret:secret -kdfopt seed:seed TLS1-PRF}],
expected => '65:6F:31:CB:04:03:D6:51:E2:E8:71:F8:20:04:AB:BA',
desc => 'TLS1-PRF MD5-SHA1' },
{ cmd => [qw{openssl kdf -keylen 10 -kdfopt digest:SHA256 -kdfopt key:secret -kdfopt salt:salt -kdfopt info:label HKDF}],
expected => '2a:c4:36:9f:52:59:96:f8:de:13',
desc => 'HKDF SHA256' },
{ cmd => [qw{openssl kdf -keylen 32 -kdfopt digest:SHA256 -kdfopt pass:password -kdfopt salt:salt -kdfopt iter:2 PBKDF2}],
expected => 'ae:4d:0c:95:af:6b:46:d3:2d:0a:df:f9:28:f0:6d:d0:2a:30:3f:8e:f3:c2:51:df:d6:e2:d8:5a:95:47:4c:43',
desc => 'PBKDF2 SHA256'},
{ cmd => [qw{openssl kdf -keylen 64 -kdfopt mac:KMAC128 -kdfopt maclen:20 -kdfopt hexkey:b74a149a161546f8c20b06ac4ed4 -kdfopt hexinfo:348a37a27ef1282f5f020dcc -kdfopt hexsalt:3638271ccd68a25dc24ecddd39ef3f89 SSKDF}],
expected => 'e9:c1:84:53:a0:62:b5:3b:db:fc:bb:5a:34:bd:b8:e5:e7:07:ee:bb:5d:d1:34:42:43:d8:cf:c2:c2:e6:33:2f:91:bd:a5:86:f3:7d:e4:8a:65:d4:c5:14:fd:ef:aa:1e:67:54:f3:73:d2:38:e1:95:ae:15:7e:1d:e8:14:98:03',
desc => 'SSKDF KMAC128'},
{ cmd => [qw{openssl kdf -keylen 16 -kdfopt mac:HMAC -kdfopt digest:SHA256 -kdfopt hexkey:b74a149a161546f8c20b06ac4ed4 -kdfopt hexinfo:348a37a27ef1282f5f020dcc -kdfopt hexsalt:3638271ccd68a25dc24ecddd39ef3f89 SSKDF}],
expected => '44:f6:76:e8:5c:1b:1a:8b:bc:3d:31:92:18:63:1c:a3',
desc => 'SSKDF HMAC SHA256'},
{ cmd => [qw{openssl kdf -keylen 14 -kdfopt digest:SHA224 -kdfopt hexkey:6dbdc23f045488e4062757b06b9ebae183fc5a5946d80db93fec6f62ec07e3727f0126aed12ce4b262f47d48d54287f81d474c7c3b1850e9 -kdfopt hexinfo:a1b2c3d4e54341565369643c832e9849dcdba71e9a3139e606e095de3c264a66e98a165854cd07989b1ee0ec3f8dbe SSKDF}],
expected => 'a4:62:de:16:a8:9d:e8:46:6e:f5:46:0b:47:b8',
desc => 'SSKDF HASH SHA224'},
{ cmd => [qw{openssl kdf -keylen 16 -kdfopt md:SHA256 -kdfopt hexkey:0102030405 -kdfopt hexxcghash:06090A -kdfopt hexsession_id:01020304 -kdfopt type:A SSHKDF}],
expected => '5C:49:94:47:3B:B1:53:3A:58:EB:19:42:04:D3:78:16',
desc => 'SSHKDF SHA256'},
);
my @scrypt_tests = (
{ cmd => [qw{openssl kdf -keylen 64 -kdfopt pass:password -kdfopt salt:NaCl -kdfopt N:1024 -kdfopt r:8 -kdfopt p:16 -kdfopt maxmem_bytes:10485760 id-scrypt}],
expected => 'fd:ba:be:1c:9d:34:72:00:78:56:e7:19:0d:01:e9:fe:7c:6a:d7:cb:c8:23:78:30:e7:73:76:63:4b:37:31:62:2e:af:30:d9:2e:22:a3:88:6f:f1:09:27:9d:98:30:da:c7:27:af:b9:4a:83:ee:6d:83:60:cb:df:a2:cc:06:40',
desc => 'SCRYPT' },
);
push @kdf_tests, @scrypt_tests unless disabled("scrypt");
plan tests => scalar @kdf_tests;
foreach (@kdf_tests) {
ok(compareline($_->{cmd}, $_->{expected}), $_->{desc});
}
# Check that the stdout output matches the expected value.
sub compareline {
my ($cmdarray, $expect) = @_;
if (defined($expect)) {
$expect = uc $expect;
}
my @lines = run(app($cmdarray), capture => 1);
if (defined($expect)) {
if ($lines[0] =~ m|^\Q${expect}\E\R$|) {
return 1;
} else {
print "Got: $lines[0]";
print "Exp: $expect\n";
return 0;
}
}
return 0;
}
+28 -1
View File
@@ -2388,14 +2388,41 @@ Operation = ENCRYPT
Plaintext = B41E6BE2EBA84A148E2EED84593C5EC7
Ciphertext = 9B9B7BFCD1813CB95D0B3618F40F5122
Title = Chacha20
Title = Chacha20 test vectors from RFC7539
# A.1 Test Vector 1
Cipher = chacha20
Key = 0000000000000000000000000000000000000000000000000000000000000000
IV = 00000000000000000000000000000000
Plaintext = 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Ciphertext = 76b8e0ada0f13d90405d6ae55386bd28bdd219b8a08ded1aa836efcc8b770dc7da41597c5157488d7724e03fb8d84a376a43b8f41518a11cc387b669b2ee6586
# A.1 Test Vector 2
Cipher = chacha20
Key = 0000000000000000000000000000000000000000000000000000000000000000
IV = 01000000000000000000000000000000
Plaintext = 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Ciphertext = 9f07e7be5551387a98ba977c732d080dcb0f29a048e3656912c6533e32ee7aed29b721769ce64e43d57133b074d839d531ed1f28510afb45ace10a1f4b794d6f
# A.2 Test Vector 1 is the same as A.1 Test Vector 1
# A.2 Test Vector 2
Cipher = chacha20
Key = 0000000000000000000000000000000000000000000000000000000000000001
#Counter (first 4 bytes) expressed in little-endian order
IV = 01000000000000000000000000000002
Plaintext = 416e79207375626d697373696f6e20746f20746865204945544620696e74656e6465642062792074686520436f6e7472696275746f7220666f72207075626c69636174696f6e20617320616c6c206f722070617274206f6620616e204945544620496e7465726e65742d4472616674206f722052464320616e6420616e792073746174656d656e74206d6164652077697468696e2074686520636f6e74657874206f6620616e204945544620616374697669747920697320636f6e7369646572656420616e20224945544620436f6e747269627574696f6e222e20537563682073746174656d656e747320696e636c756465206f72616c2073746174656d656e747320696e20494554462073657373696f6e732c2061732077656c6c206173207772697474656e20616e6420656c656374726f6e696320636f6d6d756e69636174696f6e73206d61646520617420616e792074696d65206f7220706c6163652c207768696368206172652061646472657373656420746f
Ciphertext = a3fbf07df3fa2fde4f376ca23e82737041605d9f4f4f57bd8cff2c1d4b7955ec2a97948bd3722915c8f3d337f7d370050e9e96d647b7c39f56e031ca5eb6250d4042e02785ececfa4b4bb5e8ead0440e20b6e8db09d881a7c6132f420e52795042bdfa7773d8a9051447b3291ce1411c680465552aa6c405b7764d5e87bea85ad00f8449ed8f72d0d662ab052691ca66424bc86d2df80ea41f43abf937d3259dc4b2d0dfb48a6c9139ddd7f76966e928e635553ba76c5c879d7b35d49eb2e62b0871cdac638939e25e8a1e0ef9d5280fa8ca328b351c3c765989cbcf3daa8b6ccc3aaf9f3979c92b3720fc88dc95ed84a1be059c6499b9fda236e7e818b04b0bc39c1e876b193bfe5569753f88128cc08aaa9b63d1a16f80ef2554d7189c411f5869ca52c5b83fa36ff216b9c1d30062bebcfd2dc5bce0911934fda79a86f6e698ced759c3ff9b6477338f3da4f9cd8514ea9982ccafb341b2384dd902f3d1ab7ac61dd29c6f21ba5b862f3730e37cfdc4fd806c22f221
# A.2 Test Vector 3
Cipher = chacha20
Key = 1c9240a5eb55d38af333888604f6b5f0473917c1402b80099dca5cbc207075c0
#Counter (first 4 bytes) expressed in little-endian order
IV = 2a000000000000000000000000000002
Plaintext = 2754776173206272696c6c69672c20616e642074686520736c6974687920746f7665730a446964206779726520616e642067696d626c6520696e2074686520776162653a0a416c6c206d696d737920776572652074686520626f726f676f7665732c0a416e6420746865206d6f6d65207261746873206f757467726162652e
Ciphertext = 62e6347f95ed87a45ffae7426f27a1df5fb69110044c0d73118effa95b01e5cf166d3df2d721caf9b21e5fb14c616871fd84c54f9d65b283196c7fe4f60553ebf39c6402c42234e32a356b3e764312a61a5532055716ead6962568f87d3f3f7704c6a8d1bcd1bf4d50d6154b6da731b187b58dfd728afa36757a797ac188d1
Title = Chacha20
Cipher = chacha20
Key = 0000000000000000000000000000000000000000000000000000000000000001
IV = 00000000000000000000000000000000