Просмотр исходного кода

Add CTR-DRBG.

This isn't actually used yet, but implements CTR-DRBG from SP 800-90Ar1.
Specifically, it always uses AES-256 and no derivation function.

Change-Id: Ie82b829590226addd7c165eac410a5d584858bfd
Reviewed-on: https://boringssl-review.googlesource.com/14891
Reviewed-by: Adam Langley <agl@google.com>
kris/onging/CECPQ3_patch15
Adam Langley 7 лет назад
committed by Adam Langley
Родитель
Сommit
730d69e159
9 измененных файлов: 2343 добавлений и 1 удалений
  1. +1
    -0
      crypto/CMakeLists.txt
  2. +12
    -0
      crypto/rand/CMakeLists.txt
  3. +200
    -0
      crypto/rand/ctrdrbg.c
  4. +83
    -0
      crypto/rand/ctrdrbg_test.cc
  5. +73
    -0
      crypto/rand/ctrdrbg_vector_test.cc
  6. +1922
    -0
      crypto/rand/ctrdrbg_vectors.txt
  7. +51
    -0
      crypto/rand/internal.h
  8. +0
    -1
      include/openssl/base.h
  9. +1
    -0
      util/all_tests.json

+ 1
- 0
crypto/CMakeLists.txt Просмотреть файл

@@ -245,6 +245,7 @@ add_executable(
ec/ec_test.cc
err/err_test.cc
evp/evp_extra_test.cc
rand/ctrdrbg_test.cc
rsa/rsa_test.cc

$<TARGET_OBJECTS:gtest_main>


+ 12
- 0
crypto/rand/CMakeLists.txt Просмотреть файл

@@ -13,6 +13,7 @@ add_library(

OBJECT

ctrdrbg.c
deterministic.c
fuchsia.c
rand.c
@@ -23,3 +24,14 @@ add_library(
)

perlasm(rdrand-x86_64.${ASM_EXT} asm/rdrand-x86_64.pl)

add_executable(
ctrdrbg_vector_test

ctrdrbg_vector_test.cc

$<TARGET_OBJECTS:test_support>
)

target_link_libraries(ctrdrbg_vector_test crypto)
add_dependencies(all_tests ctrdrbg_vector_test)

+ 200
- 0
crypto/rand/ctrdrbg.c Просмотреть файл

@@ -0,0 +1,200 @@
/* Copyright (c) 2017, Google Inc.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */

#include <openssl/rand.h>

#include <openssl/type_check.h>
#include <openssl/mem.h>

#include "internal.h"
#include "../cipher/internal.h"


/* Section references in this file refer to SP 800-90Ar1:
* http://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-90Ar1.pdf */

/* See table 3. */
static const uint64_t kMaxReseedCount = UINT64_C(1) << 48;

int CTR_DRBG_init(CTR_DRBG_STATE *drbg,
const uint8_t entropy[CTR_DRBG_ENTROPY_LEN],
const uint8_t *personalization, size_t personalization_len) {
/* Section 10.2.1.3.1 */
if (personalization_len > CTR_DRBG_ENTROPY_LEN) {
return 0;
}

uint8_t seed_material[CTR_DRBG_ENTROPY_LEN];
OPENSSL_memcpy(seed_material, entropy, CTR_DRBG_ENTROPY_LEN);

for (size_t i = 0; i < personalization_len; i++) {
seed_material[i] ^= personalization[i];
}

/* Section 10.2.1.2 */

/* kInitMask is the result of encrypting blocks with big-endian value 1, 2
* and 3 with the all-zero AES-256 key. */
static const uint8_t kInitMask[CTR_DRBG_ENTROPY_LEN] = {
0x53, 0x0f, 0x8a, 0xfb, 0xc7, 0x45, 0x36, 0xb9, 0xa9, 0x63, 0xb4, 0xf1,
0xc4, 0xcb, 0x73, 0x8b, 0xce, 0xa7, 0x40, 0x3d, 0x4d, 0x60, 0x6b, 0x6e,
0x07, 0x4e, 0xc5, 0xd3, 0xba, 0xf3, 0x9d, 0x18, 0x72, 0x60, 0x03, 0xca,
0x37, 0xa6, 0x2a, 0x74, 0xd1, 0xa2, 0xf5, 0x8e, 0x75, 0x06, 0x35, 0x8e,
};

for (size_t i = 0; i < sizeof(kInitMask); i++) {
seed_material[i] ^= kInitMask[i];
}

drbg->ctr = aes_ctr_set_key(&drbg->ks, NULL, &drbg->block, seed_material, 32);
OPENSSL_memcpy(drbg->counter.bytes, seed_material + 32, 16);
drbg->reseed_counter = 1;

return 1;
}

OPENSSL_COMPILE_ASSERT(CTR_DRBG_ENTROPY_LEN % AES_BLOCK_SIZE == 0,
not_a_multiple_of_block_size);

/* ctr_inc adds |n| to the last four bytes of |drbg->counter|, treated as a
* big-endian number. */
static void ctr32_add(CTR_DRBG_STATE *drbg, uint32_t n) {
drbg->counter.words[3] =
CRYPTO_bswap4(CRYPTO_bswap4(drbg->counter.words[3]) + n);
}

static int CTR_DRBG_update(CTR_DRBG_STATE *drbg, const uint8_t *data,
size_t data_len) {
/* Section 10.2.1.2. A value of |data_len| which less than
* |CTR_DRBG_ENTROPY_LEN| is permitted and acts the same as right-padding
* with zeros. This can save a copy. */
if (data_len > CTR_DRBG_ENTROPY_LEN) {
return 0;
}

uint8_t temp[CTR_DRBG_ENTROPY_LEN];
for (size_t i = 0; i < CTR_DRBG_ENTROPY_LEN; i += AES_BLOCK_SIZE) {
ctr32_add(drbg, 1);
drbg->block(drbg->counter.bytes, temp + i, &drbg->ks);
}

for (size_t i = 0; i < data_len; i++) {
temp[i] ^= data[i];
}

drbg->ctr = aes_ctr_set_key(&drbg->ks, NULL, &drbg->block, temp, 32);
OPENSSL_memcpy(drbg->counter.bytes, temp + 32, 16);

return 1;
}

int CTR_DRBG_reseed(CTR_DRBG_STATE *drbg,
const uint8_t entropy[CTR_DRBG_ENTROPY_LEN],
const uint8_t *additional_data,
size_t additional_data_len) {
/* Section 10.2.1.4 */
uint8_t entropy_copy[CTR_DRBG_ENTROPY_LEN];

if (additional_data_len > 0) {
if (additional_data_len > CTR_DRBG_ENTROPY_LEN) {
return 0;
}

OPENSSL_memcpy(entropy_copy, entropy, CTR_DRBG_ENTROPY_LEN);
for (size_t i = 0; i < additional_data_len; i++) {
entropy_copy[i] ^= additional_data[i];
}

entropy = entropy_copy;
}

if (!CTR_DRBG_update(drbg, entropy, CTR_DRBG_ENTROPY_LEN)) {
return 0;
}

drbg->reseed_counter = 1;

return 1;
}

int CTR_DRBG_generate(CTR_DRBG_STATE *drbg, uint8_t *out, size_t out_len,
const uint8_t *additional_data,
size_t additional_data_len) {
/* See 9.3.1 */
if (out_len > CTR_DRBG_MAX_GENERATE_LENGTH) {
return 0;
}

/* See 10.2.1.5.1 */
if (drbg->reseed_counter > kMaxReseedCount) {
return 0;
}

if (additional_data_len != 0 &&
!CTR_DRBG_update(drbg, additional_data, additional_data_len)) {
return 0;
}

/* kChunkSize is used to interact better with the cache. Since the AES-CTR
* code assumes that it's encrypting rather than just writing keystream, the
* buffer has to be zeroed first. Without chunking, large reads would zero
* the whole buffer, flushing the L1 cache, and then do another pass (missing
* the cache every time) to “encrypt” it. The code can avoid this by
* chunking. */
static const size_t kChunkSize = 8 * 1024;

while (out_len >= AES_BLOCK_SIZE) {
size_t todo = kChunkSize;
if (todo > out_len) {
todo = out_len;
}

todo &= ~(AES_BLOCK_SIZE-1);
const size_t num_blocks = todo / AES_BLOCK_SIZE;

if (drbg->ctr) {
OPENSSL_memset(out, 0, todo);
ctr32_add(drbg, 1);
drbg->ctr(out, out, num_blocks, &drbg->ks, drbg->counter.bytes);
ctr32_add(drbg, num_blocks - 1);
} else {
for (size_t i = 0; i < todo; i += AES_BLOCK_SIZE) {
ctr32_add(drbg, 1);
drbg->block(drbg->counter.bytes, out + i, &drbg->ks);
}
}

out += todo;
out_len -= todo;
}

if (out_len > 0) {
uint8_t block[AES_BLOCK_SIZE];
ctr32_add(drbg, 1);
drbg->block(drbg->counter.bytes, block, &drbg->ks);

OPENSSL_memcpy(out, block, out_len);
}

if (!CTR_DRBG_update(drbg, additional_data, additional_data_len)) {
return 0;
}

drbg->reseed_counter++;
return 1;
}

void CTR_DRBG_clear(CTR_DRBG_STATE *drbg) {
OPENSSL_cleanse(drbg, sizeof(CTR_DRBG_STATE));
}

+ 83
- 0
crypto/rand/ctrdrbg_test.cc Просмотреть файл

@@ -0,0 +1,83 @@
/* Copyright (c) 2017, Google Inc.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */

#include <gtest/gtest.h>

#include <openssl/rand.h>
#include <openssl/sha.h>

#include "internal.h"
#include "../test/test_util.h"


TEST(CTRDRBGTest, Basic) {
const uint8_t kSeed[CTR_DRBG_ENTROPY_LEN] = {
0xe4, 0xbc, 0x23, 0xc5, 0x08, 0x9a, 0x19, 0xd8, 0x6f, 0x41, 0x19, 0xcb,
0x3f, 0xa0, 0x8c, 0x0a, 0x49, 0x91, 0xe0, 0xa1, 0xde, 0xf1, 0x7e, 0x10,
0x1e, 0x4c, 0x14, 0xd9, 0xc3, 0x23, 0x46, 0x0a, 0x7c, 0x2f, 0xb5, 0x8e,
0x0b, 0x08, 0x6c, 0x6c, 0x57, 0xb5, 0x5f, 0x56, 0xca, 0xe2, 0x5b, 0xad,
};

CTR_DRBG_STATE drbg;
ASSERT_TRUE(CTR_DRBG_init(&drbg, kSeed, nullptr, 0));

const uint8_t kReseed[CTR_DRBG_ENTROPY_LEN] = {
0xfd, 0x85, 0xa8, 0x36, 0xbb, 0xa8, 0x50, 0x19, 0x88, 0x1e, 0x8c, 0x6b,
0xad, 0x23, 0xc9, 0x06, 0x1a, 0xdc, 0x75, 0x47, 0x76, 0x59, 0xac, 0xae,
0xa8, 0xe4, 0xa0, 0x1d, 0xfe, 0x07, 0xa1, 0x83, 0x2d, 0xad, 0x1c, 0x13,
0x6f, 0x59, 0xd7, 0x0f, 0x86, 0x53, 0xa5, 0xdc, 0x11, 0x86, 0x63, 0xd6,
};

ASSERT_TRUE(CTR_DRBG_reseed(&drbg, kReseed, nullptr, 0));

uint8_t out[64];
ASSERT_TRUE(CTR_DRBG_generate(&drbg, out, sizeof(out), nullptr, 0));
ASSERT_TRUE(CTR_DRBG_generate(&drbg, out, sizeof(out), nullptr, 0));

const uint8_t kExpected[64] = {
0xb2, 0xcb, 0x89, 0x05, 0xc0, 0x5e, 0x59, 0x50, 0xca, 0x31, 0x89,
0x50, 0x96, 0xbe, 0x29, 0xea, 0x3d, 0x5a, 0x3b, 0x82, 0xb2, 0x69,
0x49, 0x55, 0x54, 0xeb, 0x80, 0xfe, 0x07, 0xde, 0x43, 0xe1, 0x93,
0xb9, 0xe7, 0xc3, 0xec, 0xe7, 0x3b, 0x80, 0xe0, 0x62, 0xb1, 0xc1,
0xf6, 0x82, 0x02, 0xfb, 0xb1, 0xc5, 0x2a, 0x04, 0x0e, 0xa2, 0x47,
0x88, 0x64, 0x29, 0x52, 0x82, 0x23, 0x4a, 0xaa, 0xda,
};

EXPECT_EQ(Bytes(kExpected), Bytes(out));

CTR_DRBG_clear(&drbg);
}

TEST(CTRDRBGTest, Large) {
const uint8_t kSeed[CTR_DRBG_ENTROPY_LEN] = {0};

CTR_DRBG_STATE drbg;
ASSERT_TRUE(CTR_DRBG_init(&drbg, kSeed, nullptr, 0));

std::unique_ptr<uint8_t[]> buf(new uint8_t[CTR_DRBG_MAX_GENERATE_LENGTH]);
ASSERT_TRUE(CTR_DRBG_generate(&drbg, buf.get(), CTR_DRBG_MAX_GENERATE_LENGTH,
nullptr, 0));

uint8_t digest[SHA256_DIGEST_LENGTH];
SHA256(buf.get(), CTR_DRBG_MAX_GENERATE_LENGTH, digest);

const uint8_t kExpected[SHA256_DIGEST_LENGTH] = {
0x69, 0x78, 0x15, 0x96, 0xca, 0xc0, 0x3f, 0x6a, 0x6d, 0xed, 0x22,
0x1e, 0x26, 0xd0, 0x75, 0x49, 0xa0, 0x4b, 0x91, 0x58, 0x3c, 0xf4,
0xe3, 0x6d, 0xff, 0x41, 0xbf, 0xb9, 0xf8, 0xa8, 0x1c, 0x2b,
};
EXPECT_EQ(Bytes(kExpected), Bytes(digest));

CTR_DRBG_clear(&drbg);
}

+ 73
- 0
crypto/rand/ctrdrbg_vector_test.cc Просмотреть файл

@@ -0,0 +1,73 @@
/* Copyright (c) 2017, Google Inc.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */

#include <openssl/rand.h>

#include <openssl/crypto.h>

#include "internal.h"
#include "../test/test_util.h"
#include "../test/file_test.h"


static bool TestCTRDRBG(FileTest *t, void *arg) {
std::vector<uint8_t> seed, personalisation, reseed, ai_reseed, ai1, ai2,
expected;
if (!t->GetBytes(&seed, "EntropyInput") ||
!t->GetBytes(&personalisation, "PersonalizationString") ||
!t->GetBytes(&reseed, "EntropyInputReseed") ||
!t->GetBytes(&ai_reseed, "AdditionalInputReseed") ||
!t->GetBytes(&ai1, "AdditionalInput1") ||
!t->GetBytes(&ai2, "AdditionalInput2") ||
!t->GetBytes(&expected, "ReturnedBits")) {
t->PrintLine("missing value");
return false;
}

if (seed.size() != CTR_DRBG_ENTROPY_LEN ||
reseed.size() != CTR_DRBG_ENTROPY_LEN) {
t->PrintLine("bad seed length");
return false;
}

CTR_DRBG_STATE drbg;
CTR_DRBG_init(&drbg, seed.data(),
personalisation.size() > 0 ? personalisation.data() : nullptr,
personalisation.size());
CTR_DRBG_reseed(&drbg, reseed.data(),
ai_reseed.size() > 0 ? ai_reseed.data() : nullptr,
ai_reseed.size());

std::vector<uint8_t> out;
out.resize(expected.size());

CTR_DRBG_generate(&drbg, out.data(), out.size(),
ai1.size() > 0 ? ai1.data() : nullptr, ai1.size());
CTR_DRBG_generate(&drbg, out.data(), out.size(),
ai2.size() > 0 ? ai2.data() : nullptr, ai2.size());

return t->ExpectBytesEqual(expected.data(), expected.size(), out.data(),
out.size());
}

int main(int argc, char **argv) {
CRYPTO_library_init();

if (argc != 2) {
fprintf(stderr, "%s <test file>\n", argv[0]);
return 1;
}

return FileTestMain(TestCTRDRBG, nullptr, argv[1]);
}

+ 1922
- 0
crypto/rand/ctrdrbg_vectors.txt
Разница между файлами не показана из-за своего большого размера
Просмотреть файл


+ 51
- 0
crypto/rand/internal.h Просмотреть файл

@@ -15,6 +15,11 @@
#ifndef OPENSSL_HEADER_CRYPTO_RAND_INTERNAL_H
#define OPENSSL_HEADER_CRYPTO_RAND_INTERNAL_H

#include <openssl/aes.h>

#include "../internal.h"
#include "../modes/internal.h"

#if defined(__cplusplus)
extern "C" {
#endif
@@ -24,6 +29,52 @@ extern "C" {
* system. */
void CRYPTO_sysrand(uint8_t *buf, size_t len);

/* CTR_DRBG_STATE contains the state of a CTR_DRBG based on AES-256. See SP
* 800-90Ar1. */
typedef struct {
alignas(16) AES_KEY ks;
block128_f block;
ctr128_f ctr;
union {
uint8_t bytes[16];
uint32_t words[4];
} counter;
uint64_t reseed_counter;
} CTR_DRBG_STATE;

/* See SP 800-90Ar1, table 3. */
#define CTR_DRBG_ENTROPY_LEN 48
#define CTR_DRBG_MAX_GENERATE_LENGTH 65536

/* CTR_DRBG_init initialises |*drbg| given |CTR_DRBG_ENTROPY_LEN| bytes of
* entropy in |entropy| and, optionally, a personalization string up to
* |CTR_DRBG_ENTROPY_LEN| bytes in length. It returns one on success and zero
* on error. */
OPENSSL_EXPORT int CTR_DRBG_init(CTR_DRBG_STATE *drbg,
const uint8_t entropy[CTR_DRBG_ENTROPY_LEN],
const uint8_t *personalization,
size_t personalization_len);

/* CTR_DRBG_reseed reseeds |drbg| given |CTR_DRBG_ENTROPY_LEN| bytes of entropy
* in |entropy| and, optionally, up to |CTR_DRBG_ENTROPY_LEN| bytes of
* additional data. It returns one on success or zero on error. */
OPENSSL_EXPORT int CTR_DRBG_reseed(CTR_DRBG_STATE *drbg,
const uint8_t entropy[CTR_DRBG_ENTROPY_LEN],
const uint8_t *additional_data,
size_t additional_data_len);

/* CTR_DRBG_generate processes to up |CTR_DRBG_ENTROPY_LEN| bytes of additional
* data (if any) and then writes |out_len| random bytes to |out|, where
* |out_len| <= |CTR_DRBG_MAX_GENERATE_LENGTH|. It returns one on success or
* zero on error. */
OPENSSL_EXPORT int CTR_DRBG_generate(CTR_DRBG_STATE *drbg, uint8_t *out,
size_t out_len,
const uint8_t *additional_data,
size_t additional_data_len);

/* CTR_DRBG_clear zeroises the state of |drbg|. */
OPENSSL_EXPORT void CTR_DRBG_clear(CTR_DRBG_STATE *drbg);


#if defined(__cplusplus)
} /* extern C */


+ 0
- 1
include/openssl/base.h Просмотреть файл

@@ -227,7 +227,6 @@ typedef struct asn1_string_st ASN1_UTCTIME;
typedef struct asn1_string_st ASN1_UTF8STRING;
typedef struct asn1_string_st ASN1_VISIBLESTRING;
typedef struct asn1_type_st ASN1_TYPE;

typedef struct AUTHORITY_KEYID_st AUTHORITY_KEYID;
typedef struct BASIC_CONSTRAINTS_st BASIC_CONSTRAINTS;
typedef struct DIST_POINT_st DIST_POINT;


+ 1
- 0
util/all_tests.json Просмотреть файл

@@ -69,6 +69,7 @@
["crypto/pkcs8/pkcs8_test"],
["crypto/poly1305/poly1305_test", "crypto/poly1305/poly1305_tests.txt"],
["crypto/pool/pool_test"],
["crypto/rand/ctrdrbg_vector_test", "crypto/rand/ctrdrbg_vectors.txt"],
["crypto/refcount_test"],
["crypto/thread_test"],
["crypto/x509/pkcs7_test"],


Загрузка…
Отмена
Сохранить