-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrite nistkatrng and calculate nistkat checksum on board (#30)
* nistkatrng license under apache-2.0 Signed-off-by: Thing-han, Lim <[email protected]> * add sha3-256 checksum to META file and update the tests script for nistkat testing Signed-off-by: Thing-han, Lim <[email protected]> * calculate sha3-256 sum on the board instead Signed-off-by: Thing-han, Lim <[email protected]> * minor refactoring of nistkat.c Signed-off-by: Matthias J. Kannwischer <[email protected]> * use shake256-256 instead of sha3 Signed-off-by: Thing-han, Lim <[email protected]> --------- Signed-off-by: Thing-han, Lim <[email protected]> Signed-off-by: Matthias J. Kannwischer <[email protected]> Co-authored-by: Matthias J. Kannwischer <[email protected]>
- Loading branch information
1 parent
544e38a
commit 4e03a70
Showing
6 changed files
with
129 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,98 +1,88 @@ | ||
// SPDX-License-Identifier: CC0-1.0 | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#include <assert.h> | ||
#include <string.h> | ||
|
||
#include "aes.h" | ||
#include "randombytes.h" | ||
#include "nistkatrng.h" | ||
|
||
typedef struct { | ||
uint8_t Key[32]; | ||
uint8_t V[16]; | ||
int reseed_counter; | ||
} AES256_CTR_DRBG_struct; | ||
|
||
static AES256_CTR_DRBG_struct DRBG_ctx; | ||
static void AES256_CTR_DRBG_Update(const uint8_t *provided_data, uint8_t *Key, uint8_t *V); | ||
|
||
// Use whatever AES implementation you have. This uses AES from openSSL library | ||
// key - 256-bit AES key | ||
// ctr - a 128-bit plaintext value | ||
// buffer - a 128-bit ciphertext value | ||
static void AES256_ECB(uint8_t *key, uint8_t *ctr, uint8_t *buffer) { | ||
aes256ctx ctx; | ||
aes256_ecb_keyexp(&ctx, key); | ||
aes256_ecb(buffer, ctr, 1, &ctx); | ||
aes256_ctx_release(&ctx); | ||
unsigned char key[AES256_KEYBYTES]; | ||
unsigned char ctr[AES_BLOCKBYTES]; | ||
} nistkatctx; | ||
|
||
static nistkatctx ctx; | ||
|
||
static void _aes256_ecb(unsigned char key[AES256_KEYBYTES], unsigned char ctr[AES_BLOCKBYTES], unsigned char buffer[AES_BLOCKBYTES]) { | ||
aes256ctx aesctx; | ||
aes256_ecb_keyexp(&aesctx, key); | ||
aes256_ecb(buffer, ctr, 1, &aesctx); | ||
aes256_ctx_release(&aesctx); | ||
} | ||
|
||
void nist_kat_init(uint8_t *entropy_input, const uint8_t *personalization_string, int security_strength); | ||
void nist_kat_init(uint8_t *entropy_input, const uint8_t *personalization_string, int security_strength) { | ||
uint8_t seed_material[48]; | ||
static void aes256_block_update(uint8_t block[AES_BLOCKBYTES]) { | ||
for (int j = AES_BLOCKBYTES - 1; j >= 0; j--) { | ||
ctx.ctr[j]++; | ||
|
||
assert(security_strength == 256); | ||
memcpy(seed_material, entropy_input, 48); | ||
if (personalization_string) { | ||
for (int i = 0; i < 48; i++) { | ||
seed_material[i] ^= personalization_string[i]; | ||
if (ctx.ctr[j] != 0x00) { | ||
break; | ||
} | ||
} | ||
memset(DRBG_ctx.Key, 0x00, 32); | ||
memset(DRBG_ctx.V, 0x00, 16); | ||
AES256_CTR_DRBG_Update(seed_material, DRBG_ctx.Key, DRBG_ctx.V); | ||
DRBG_ctx.reseed_counter = 1; | ||
|
||
_aes256_ecb(ctx.key, ctx.ctr, block); | ||
} | ||
|
||
int randombytes(uint8_t *buf, size_t n) { | ||
uint8_t block[16]; | ||
int i = 0; | ||
|
||
while (n > 0) { | ||
//increment V | ||
for (int j = 15; j >= 0; j--) { | ||
if (DRBG_ctx.V[j] == 0xff) { | ||
DRBG_ctx.V[j] = 0x00; | ||
} else { | ||
DRBG_ctx.V[j]++; | ||
break; | ||
} | ||
} | ||
AES256_ECB(DRBG_ctx.Key, DRBG_ctx.V, block); | ||
if (n > 15) { | ||
memcpy(buf + i, block, 16); | ||
i += 16; | ||
n -= 16; | ||
} else { | ||
memcpy(buf + i, block, n); | ||
n = 0; | ||
static void nistkat_update(const unsigned char *provided_data, unsigned char *key, unsigned char *ctr) { | ||
int len = AES256_KEYBYTES + AES_BLOCKBYTES; | ||
uint8_t tmp[len]; | ||
|
||
for (int i = 0; i < len / AES_BLOCKBYTES; i++) { | ||
aes256_block_update(tmp + AES_BLOCKBYTES * i); | ||
} | ||
|
||
if (provided_data) { | ||
for (int i = 0; i < len; i++) { | ||
tmp[i] ^= provided_data[i]; | ||
} | ||
} | ||
AES256_CTR_DRBG_Update(NULL, DRBG_ctx.Key, DRBG_ctx.V); | ||
DRBG_ctx.reseed_counter++; | ||
return 0; | ||
|
||
memcpy(key, tmp, AES256_KEYBYTES); | ||
memcpy(ctr, tmp + AES256_KEYBYTES, AES_BLOCKBYTES); | ||
} | ||
|
||
static void AES256_CTR_DRBG_Update(const uint8_t *provided_data, uint8_t *Key, uint8_t *V) { | ||
uint8_t temp[48]; | ||
|
||
for (int i = 0; i < 3; i++) { | ||
//increment V | ||
for (int j = 15; j >= 0; j--) { | ||
if (V[j] == 0xff) { | ||
V[j] = 0x00; | ||
} else { | ||
V[j]++; | ||
break; | ||
} | ||
void randombytes_init(unsigned char entropy_input[AES256_KEYBYTES + AES_BLOCKBYTES], const unsigned char personalization_string[AES256_KEYBYTES + AES_BLOCKBYTES], int security_strength) { | ||
int len = AES256_KEYBYTES + AES_BLOCKBYTES; | ||
uint8_t seed_material[len]; | ||
(void) security_strength; | ||
|
||
memcpy(seed_material, entropy_input, len); | ||
if (personalization_string) { | ||
for (int i = 0; i < len; i++) { | ||
seed_material[i] ^= personalization_string[i]; | ||
} | ||
} | ||
memset(ctx.key, 0x00, AES256_KEYBYTES); | ||
memset(ctx.ctr, 0x00, AES_BLOCKBYTES); | ||
nistkat_update(seed_material, ctx.key, ctx.ctr); | ||
} | ||
|
||
int randombytes(uint8_t *buf, size_t n) { | ||
uint8_t block[AES_BLOCKBYTES]; | ||
|
||
size_t nb = n / AES_BLOCKBYTES; | ||
size_t tail = n % AES_BLOCKBYTES; | ||
|
||
AES256_ECB(Key, V, temp + 16 * i); | ||
for (size_t i = 0; i < nb; i++) { | ||
aes256_block_update(block); | ||
memcpy(buf + i * AES_BLOCKBYTES, block, AES_BLOCKBYTES); | ||
} | ||
if (provided_data != NULL) { | ||
for (int i = 0; i < 48; i++) { | ||
temp[i] ^= provided_data[i]; | ||
} | ||
|
||
if (tail > 0) { | ||
aes256_block_update(block); | ||
memcpy(buf + nb * AES_BLOCKBYTES, block, tail); | ||
} | ||
memcpy(Key, temp, 32); | ||
memcpy(V, temp + 32, 16); | ||
|
||
nistkat_update(NULL, ctx.key, ctx.ctr); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#ifndef NISTKATRNG_H | ||
#define NISTKATRNG_H | ||
|
||
#include "aes.h" | ||
|
||
void randombytes_init(unsigned char entropy_input[AES256_KEYBYTES + AES_BLOCKBYTES], const unsigned char personalization_string[AES256_KEYBYTES + AES_BLOCKBYTES], int security_strength); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters