Skip to content

Commit

Permalink
tests/core/crypto: Start adding comprehensive curve25519 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Yawning committed Apr 9, 2024
1 parent 893c3be commit fa1d681
Show file tree
Hide file tree
Showing 7 changed files with 842 additions and 71 deletions.
72 changes: 1 addition & 71 deletions tests/core/crypto/test_core_crypto.odin
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import "core:testing"
import "core:crypto"
import "core:crypto/chacha20"
import "core:crypto/chacha20poly1305"
import "core:crypto/x25519"

import tc "tests:common"

Expand All @@ -32,10 +31,10 @@ main :: proc() {
test_hash(&t)
test_mac(&t)
test_kdf(&t) // After hash/mac tests because those should pass first.
test_ecc25519(&t)

test_chacha20(&t)
test_chacha20poly1305(&t)
test_x25519(&t)
test_sha3_variants(&t)

bench_crypto(&t)
Expand Down Expand Up @@ -274,75 +273,6 @@ test_chacha20poly1305 :: proc(t: ^testing.T) {
tc.expect(t, !ok, "Expected false for decrypt(tag, corrupted_aad, ciphertext)")
}

@(test)
test_x25519 :: proc(t: ^testing.T) {
tc.log(t, "Testing X25519")

// Local copy of this so that the base point doesn't need to be exported.
_BASE_POINT: [32]byte = {
9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
}

test_vectors := []struct{
scalar: string,
point: string,
product: string,
} {
// Test vectors from RFC 7748
{
"a546e36bf0527c9d3b16154b82465edd62144c0ac1fc5a18506a2244ba449ac4",
"e6db6867583030db3594c1a424b15f7c726624ec26b3353b10a903a6d0ab1c4c",
"c3da55379de9c6908e94ea4df28d084f32eccf03491c71f754b4075577a28552",
},
{
"4b66e9d4d1b4673c5ad22691957d6af5c11b6421e0ea01d42ca4169e7918ba0d",
"e5210f12786811d3f4b7959d0538ae2c31dbe7106fc03c3efc4cd549c715a493",
"95cbde9476e8907d7aade45cb4b873f88b595a68799fa152e6f8f7647aac7957",
},
}
for v, _ in test_vectors {
scalar, _ := hex.decode(transmute([]byte)(v.scalar), context.temp_allocator)
point, _ := hex.decode(transmute([]byte)(v.point), context.temp_allocator)

derived_point: [x25519.POINT_SIZE]byte
x25519.scalarmult(derived_point[:], scalar[:], point[:])
derived_point_str := string(hex.encode(derived_point[:], context.temp_allocator))

tc.expect(
t,
derived_point_str == v.product,
fmt.tprintf(
"Expected %s for %s * %s, but got %s instead",
v.product,
v.scalar,
v.point,
derived_point_str,
),
)

// Abuse the test vectors to sanity-check the scalar-basepoint multiply.
p1, p2: [x25519.POINT_SIZE]byte
x25519.scalarmult_basepoint(p1[:], scalar[:])
x25519.scalarmult(p2[:], scalar[:], _BASE_POINT[:])
p1_str := string(hex.encode(p1[:], context.temp_allocator))
p2_str := string(hex.encode(p2[:], context.temp_allocator))
tc.expect(
t,
p1_str == p2_str,
fmt.tprintf(
"Expected %s for %s * basepoint, but got %s instead",
p2_str,
v.scalar,
p1_str,
),
)
}

// TODO/tests: Run the wycheproof test vectors, once I figure out
// how to work with JSON.
}

@(test)
test_rand_bytes :: proc(t: ^testing.T) {
tc.log(t, "Testing rand_bytes")
Expand Down
Loading

0 comments on commit fa1d681

Please sign in to comment.