Skip to content

Commit

Permalink
ED25519: add self-tests
Browse files Browse the repository at this point in the history
Signed-off-by: Stephan Mueller <[email protected]>
  • Loading branch information
smuellerDD committed Nov 11, 2023
1 parent 1007702 commit d194e9b
Showing 1 changed file with 157 additions and 7 deletions.
164 changes: 157 additions & 7 deletions curve25519/src/ed25519.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,57 @@
* Frank Denis <j at pureftpd dot org>
*/

#include "compare.h"
#include "ed25519.h"
#include "ed25519_ref10.h"
#include "ext_headers.h"
#include "lc_sha512.h"
#include "ret_checkers.h"
#include "selftest_rng.h"

static void lc_ed25519_keypair_selftest(int *tested)
{
static const uint8_t sk_exp[] = {
0x7f, 0x9c, 0x2b, 0xa4, 0xe8, 0x8f, 0x82, 0x7d, 0x61, 0x60,
0x45, 0x50, 0x76, 0x05, 0x85, 0x3e, 0xd7, 0x3b, 0x80, 0x93,
0xf6, 0xef, 0xbc, 0x88, 0xeb, 0x1a, 0x6e, 0xac, 0xfa, 0x66,
0xef, 0x26, 0xa1, 0xa6, 0xe4, 0xd8, 0x85, 0xce, 0x8e, 0x12,
0x05, 0x02, 0xb0, 0x19, 0x08, 0x1b, 0xd3, 0x1b, 0x24, 0x82,
0x0b, 0xac, 0x05, 0xa7, 0xc8, 0xf3, 0x64, 0x86, 0x2b, 0xb5,
0x31, 0x96, 0x3c, 0x8d
};
static const uint8_t pk_exp[] = { 0xa1, 0xa6, 0xe4, 0xd8, 0x85, 0xce,
0x8e, 0x12, 0x05, 0x02, 0xb0, 0x19,
0x08, 0x1b, 0xd3, 0x1b, 0x24, 0x82,
0x0b, 0xac, 0x05, 0xa7, 0xc8, 0xf3,
0x64, 0x86, 0x2b, 0xb5, 0x31, 0x96,
0x3c, 0x8d };
struct lc_ed25519_pk pk;
struct lc_ed25519_sk sk;
LC_SELFTEST_DRNG_CTX_ON_STACK(selftest_rng);

LC_SELFTEST_RUN(tested);

lc_ed25519_keypair(&pk, &sk, selftest_rng);
lc_compare_selftest(pk.pk, pk_exp, sizeof(pk_exp),
"ED25519 keypair pubkey\n");
lc_compare_selftest(sk.sk, sk_exp, sizeof(sk_exp),
"ED25519 keypair seckey\n");
}

int lc_ed25519_keypair(struct lc_ed25519_pk *pk, struct lc_ed25519_sk *sk,
struct lc_rng_ctx *rng_ctx)
{
ge25519_p3 A;
uint8_t tmp[LC_SHA512_SIZE_DIGEST];
int ret;
static int tested = 0;

CKNULL(sk, -EINVAL);
CKNULL(pk, -EINVAL);

lc_ed25519_keypair_selftest(&tested);
;

CKINT(lc_rng_generate(rng_ctx, NULL, 0, sk->sk, 32));
lc_hash(lc_sha512, sk->sk, 32, tmp);
Expand All @@ -55,6 +94,53 @@ int lc_ed25519_keypair(struct lc_ed25519_pk *pk, struct lc_ed25519_sk *sk,
return ret;
}

/* Test vector generated with libsodium using the ACVP parser tool */
static void lc_ed25519_sign_tester(int *tested)
{
static const struct lc_ed25519_sk sk = {
.sk = { 0x42, 0x58, 0x0d, 0x49, 0xbe, 0x95, 0x1f, 0x95,
0xdf, 0xca, 0x13, 0x60, 0xda, 0x43, 0x09, 0x58,
0xd9, 0x30, 0xc7, 0xa1, 0x71, 0xbd, 0xa0, 0x99,
0x92, 0x5a, 0xb5, 0xb7, 0xcd, 0x88, 0x51, 0xae,
0x5d, 0x10, 0xd0, 0x95, 0x66, 0xa2, 0xd8, 0x75,
0xea, 0xcf, 0xa0, 0x87, 0x73, 0x9a, 0xcd, 0xb9,
0x5c, 0xfb, 0xfa, 0x94, 0x05, 0x5a, 0x14, 0xd7,
0x59, 0x0b, 0xd4, 0xb1, 0x06, 0xe8, 0x09, 0xbd }
};
static const struct lc_ed25519_sig exp_sig = {
.sig = { 0xb6, 0x53, 0xe0, 0x0b, 0xf2, 0x07, 0xd1, 0x83,
0xdd, 0x7b, 0xef, 0x59, 0xaa, 0x7b, 0x23, 0xb5,
0xfe, 0x76, 0x9c, 0x2a, 0x6b, 0xf2, 0x10, 0xd6,
0xa7, 0xa2, 0x17, 0xf2, 0xb1, 0xa5, 0x5d, 0xd6,
0x92, 0xdf, 0xec, 0x22, 0xf0, 0x18, 0xac, 0x7f,
0x21, 0x9c, 0xe1, 0xb8, 0x74, 0x30, 0x9d, 0xe9,
0xa4, 0x2d, 0x1b, 0x89, 0x1c, 0xb3, 0xb9, 0x47,
0xb8, 0xc6, 0xbb, 0xd4, 0xcf, 0xb7, 0xa4, 0x0b }
};
static const uint8_t msg[] = {
0x67, 0xB1, 0x9B, 0xA7, 0x05, 0xCF, 0xEE, 0x74, 0x82, 0x10,
0xCC, 0xB6, 0x98, 0xFF, 0x84, 0xBC, 0x8C, 0x59, 0x8E, 0x45,
0x26, 0x2C, 0x39, 0xDF, 0xB7, 0x8B, 0xAA, 0x9A, 0x4E, 0xA9,
0x6C, 0x83, 0x46, 0x65, 0x84, 0x92, 0x7E, 0xD2, 0x90, 0xB3,
0x9E, 0x80, 0x18, 0xC8, 0x4B, 0xEB, 0x84, 0x24, 0x82, 0x00,
0x83, 0x2F, 0xC4, 0x69, 0xE1, 0xEC, 0x44, 0x19, 0x7A, 0x96,
0x82, 0x8C, 0xF4, 0x9B, 0xD9, 0x18, 0xA2, 0x1D, 0x24, 0x07,
0xBC, 0x0F, 0x89, 0x53, 0xAE, 0x07, 0x18, 0x7D, 0xF9, 0x31,
0x21, 0x2D, 0x26, 0x43, 0x45, 0x46, 0x9B, 0xE9, 0x82, 0xA8,
0x99, 0x3A, 0xE2, 0x19, 0x06, 0x4C, 0x87, 0x31, 0x46, 0x44,
0x1D, 0xA5, 0x51, 0xA9, 0x43, 0xC8, 0x75, 0x60, 0x52, 0x63,
0x94, 0xDD, 0x54, 0x5C, 0xAF, 0x88, 0xD9, 0x7C, 0xCD, 0x1F,
0x5D, 0xC0, 0xC3, 0x76, 0xB0, 0x00, 0xD7, 0xFE
};
struct lc_ed25519_sig sig;

LC_SELFTEST_RUN(tested);

lc_ed25519_sign(&sig, msg, sizeof(msg), &sk, NULL);
lc_compare_selftest(sig.sig, exp_sig.sig, sizeof(exp_sig.sig),
"ED25519 Signature generation\n");
}

int lc_ed25519_sign(struct lc_ed25519_sig *sig, const uint8_t *msg, size_t mlen,
const struct lc_ed25519_sk *sk, struct lc_rng_ctx *rng_ctx)
{
Expand All @@ -63,8 +149,14 @@ int lc_ed25519_sign(struct lc_ed25519_sig *sig, const uint8_t *msg, size_t mlen,
uint8_t hram[LC_SHA512_SIZE_DIGEST];
ge25519_p3 R;
int ret = 0;
static int tested = 0;
LC_HASH_CTX_ON_STACK(hash_ctx, lc_sha512);

CKNULL(sig, -EINVAL);
CKNULL(sk, -EINVAL);

lc_ed25519_sign_tester(&tested);

lc_hash(lc_sha512, sk->sk, 32, az);

lc_hash_init(hash_ctx);
Expand Down Expand Up @@ -107,6 +199,50 @@ int lc_ed25519_sign(struct lc_ed25519_sig *sig, const uint8_t *msg, size_t mlen,
return ret;
}

/* Test vector obtained from NIST ACVP demo server */
static void lc_ed25519_verify_tester(int *tested)
{
static const struct lc_ed25519_pk pk = {
.pk = { 0xDE, 0xE0, 0x76, 0xAD, 0x68, 0xDC, 0x56, 0x56,
0xAA, 0x3E, 0xF7, 0x93, 0x37, 0xFD, 0xFD, 0x3E,
0x4F, 0x8D, 0xB9, 0x4A, 0xFF, 0xEE, 0xF4, 0xEA,
0xDA, 0xA8, 0x08, 0x1D, 0x00, 0x6E, 0x5A, 0xC0 }
};
static const struct lc_ed25519_sig sig = {
.sig = { 0x9F, 0xB9, 0x57, 0x68, 0xE6, 0x87, 0x91, 0xFC,
0xD6, 0x04, 0xF0, 0x68, 0x5F, 0x57, 0xC4, 0x33,
0xEF, 0xBE, 0x0A, 0xE6, 0x6F, 0x89, 0x90, 0xA1,
0xB1, 0xFF, 0x62, 0xA2, 0x50, 0x7F, 0xB2, 0xA0,
0xEA, 0xB7, 0x6C, 0xDD, 0x37, 0x3B, 0x9C, 0x20,
0x5E, 0x15, 0x63, 0xF4, 0xA9, 0xAE, 0xCB, 0x25,
0x61, 0xDF, 0xAD, 0x89, 0x61, 0xC5, 0x73, 0xB8,
0xC8, 0x34, 0x24, 0xF6, 0x47, 0x56, 0x79, 0x08 }
};
static const uint8_t msg[] = {
0xA6, 0x3D, 0xEB, 0x88, 0x01, 0x0E, 0xFD, 0x0B, 0x43, 0x92,
0x48, 0x38, 0x12, 0xF5, 0x03, 0xA9, 0xD1, 0x99, 0xA9, 0xCF,
0xA7, 0x08, 0x5F, 0x68, 0x31, 0xFE, 0xE3, 0x21, 0xA9, 0x28,
0x46, 0x8E, 0x55, 0x74, 0x26, 0x7E, 0xB2, 0xBD, 0x9C, 0xB8,
0x1E, 0xD3, 0x7A, 0x88, 0xF2, 0x18, 0x0D, 0x8D, 0x6A, 0x07,
0xD9, 0xC5, 0x87, 0xFF, 0xB1, 0xCD, 0xBB, 0x9E, 0x46, 0x9D,
0xC6, 0x1C, 0xDE, 0xBE, 0x1A, 0x3A, 0x51, 0x1F, 0x82, 0x6E,
0xB0, 0xAA, 0x5F, 0x30, 0xCF, 0x58, 0xD5, 0x1B, 0x06, 0x77,
0x9C, 0xAA, 0x3D, 0x88, 0xE6, 0x61, 0xC9, 0xA6, 0x94, 0xA9,
0xEC, 0x63, 0x68, 0xFB, 0xE9, 0xEE, 0x2C, 0x4F, 0xA3, 0xF9,
0x8F, 0xAA, 0x38, 0xA7, 0x8F, 0xBF, 0x26, 0x50, 0xA6, 0x45,
0x76, 0xA7, 0x01, 0xAE, 0x99, 0xB0, 0x0A, 0x0A, 0x0D, 0xBE,
0x34, 0xE1, 0xC9, 0xBB, 0x15, 0x40, 0x6A, 0x86
};
int exp, ret;

LC_SELFTEST_RUN(tested);

exp = 0;
ret = lc_ed25519_verify(&sig, msg, sizeof(msg), &pk);
lc_compare_selftest((uint8_t *)&exp, (uint8_t *)&ret, sizeof(exp),
"ED25519 Signature verification\n");
}

int lc_ed25519_verify(const struct lc_ed25519_sig *sig, const uint8_t *msg,
size_t mlen, const struct lc_ed25519_pk *pk)
{
Expand All @@ -116,29 +252,41 @@ int lc_ed25519_verify(const struct lc_ed25519_sig *sig, const uint8_t *msg,
ge25519_p3 A;
ge25519_p3 sb_ah;
ge25519_p2 sb_ah_p2;
int ret = 0;
static int tested = 0;
LC_HASH_CTX_ON_STACK(hash_ctx, lc_sha512);

CKNULL(sig, -EINVAL);
CKNULL(pk, -EINVAL);

lc_ed25519_verify_tester(&tested);

#if 0
//ED25519_COMPAT
if (sig->sig[63] & 224) {
return -EINVAL;
ret = -EINVAL;
goto out;
}
#else
if ((sig->sig[63] & 240) != 0 &&
sc25519_is_canonical(sig->sig + 32) == 0) {
return -EINVAL;
ret = -EINVAL;
goto out;
}
if (ge25519_is_canonical(pk->pk) == 0) {
return -EINVAL;
ret = -EINVAL;
goto out;
}
#endif
if (ge25519_frombytes_negate_vartime(&A, pk->pk) != 0 ||
ge25519_has_small_order(&A) != 0) {
return -EINVAL;
ret = -EINVAL;
goto out;
}
if (ge25519_frombytes(&expected_r, sig->sig) != 0 ||
ge25519_has_small_order(&expected_r) != 0) {
return -EINVAL;
ret = -EINVAL;
goto out;
}

lc_hash_init(hash_ctx);
Expand All @@ -154,6 +302,8 @@ int lc_ed25519_verify(const struct lc_ed25519_sig *sig, const uint8_t *msg,
ge25519_p3_sub(&check, &expected_r, &sb_ah);

if ((ge25519_has_small_order(&check) - 1) != 0)
return -EBADMSG;
return 0;
ret = -EBADMSG;

out:
return ret;
}

0 comments on commit d194e9b

Please sign in to comment.