28 lines
961 B
C
28 lines
961 B
C
// x25519.c
|
|
|
|
#include "crypto/x25519.h"
|
|
|
|
#include <assert.h>
|
|
#include <sodium.h>
|
|
|
|
static_assert(crypto_scalarmult_BYTES == BRAVAIS_X25519_KEY_LENGTH, "X25519 shared secret size");
|
|
static_assert(crypto_scalarmult_SCALARBYTES == BRAVAIS_X25519_KEY_LENGTH, "X25519 private key size");
|
|
|
|
void bravais_x25519_keypair(uint8_t* outPrivateKey, uint8_t* outPublicKey) {
|
|
randombytes_buf(outPrivateKey, BRAVAIS_X25519_KEY_LENGTH);
|
|
crypto_scalarmult_base(outPublicKey, outPrivateKey);
|
|
}
|
|
|
|
void bravais_x25519_public_key(const uint8_t* privateKey, uint8_t* outPublicKey) {
|
|
crypto_scalarmult_base(outPublicKey, privateKey);
|
|
}
|
|
|
|
bravais_result_t bravais_x25519_dh(const uint8_t* privateKey, const uint8_t* publicKey, uint8_t* outShared) {
|
|
// libsodium returns -1 when the output is all zeros, i.e. the peer sent a low-order point
|
|
if (crypto_scalarmult(outShared, privateKey, publicKey) != 0) {
|
|
return BRAVAIS_ERR_AUTH;
|
|
}
|
|
|
|
return BRAVAIS_OK;
|
|
}
|