Some checks failed
C/C++ CI / build-alpine-static (map[arch:riscv64 branch:latest-stable flags:-ffunction-sections]) (push) Has been cancelled
C/C++ CI / build-alpine-static (map[arch:x86_64 branch:latest-stable flags:-ffunction-sections]) (push) Has been cancelled
C/C++ CI / build-ubuntu (map[c:gcc-10 cpp:g++-10 flags: os:ubuntu-22.04]) (push) Has been cancelled
C/C++ CI / build-ubuntu (map[c:gcc-11 cpp:g++-11 flags: os:ubuntu-22.04]) (push) Has been cancelled
C/C++ CI / build-ubuntu (map[c:gcc-12 cpp:g++-12 flags: os:ubuntu-22.04]) (push) Has been cancelled
C/C++ CI / build-ubuntu (map[c:gcc-13 cpp:g++-13 flags: os:ubuntu-24.04]) (push) Has been cancelled
C/C++ CI / build-ubuntu (map[c:gcc-14 cpp:g++-14 flags: os:ubuntu-24.04]) (push) Has been cancelled
C/C++ CI / build-ubuntu (map[c:gcc-9 cpp:g++-9 flags: os:ubuntu-22.04]) (push) Has been cancelled
C/C++ CI / build-ubuntu-static-libs (map[flags:-fuse-linker-plugin -ffunction-sections]) (push) Has been cancelled
C/C++ CI / build-ubuntu-aarch64 (map[flags:-fuse-linker-plugin -ffunction-sections -mfix-cortex-a53-835769 -mfix-cortex-a53-843419 os:ubuntu-22.04-arm]) (push) Has been cancelled
C/C++ CI / build-ubuntu-riscv64 (map[flags:-fuse-linker-plugin -ffunction-sections os:ubuntu-22.04]) (push) Has been cancelled
C/C++ CI / build-windows-msys2 (map[c:clang cxx:clang++ flags:-fuse-ld=lld -Wno-unused-command-line-argument -Wno-nan-infinity-disabled -Wno-attributes]) (push) Has been cancelled
C/C++ CI / build-windows-msys2 (map[c:gcc cxx:g++ flags:-ffunction-sections -Wno-error=maybe-uninitialized -Wno-error=attributes -Wno-attributes]) (push) Has been cancelled
C/C++ CI / build-windows-msbuild (map[grpc:OFF os:2022 rx:OFF tls:OFF upnp:OFF vs:Visual Studio 17 2022 vspath:C:\Program Files\Microsoft Visual Studio\2022\Enterprise]) (push) Has been cancelled
C/C++ CI / build-alpine-static (map[arch:aarch64 branch:latest-stable flags:-ffunction-sections -mfix-cortex-a53-835769 -mfix-cortex-a53-843419]) (push) Has been cancelled
C/C++ CI / build-windows-msbuild (map[grpc:OFF os:2022 rx:OFF tls:ON upnp:OFF vs:Visual Studio 17 2022 vspath:C:\Program Files\Microsoft Visual Studio\2022\Enterprise]) (push) Has been cancelled
C/C++ CI / build-windows-msbuild (map[grpc:OFF os:2022 rx:OFF tls:ON upnp:ON vs:Visual Studio 17 2022 vspath:C:\Program Files\Microsoft Visual Studio\2022\Enterprise]) (push) Has been cancelled
C/C++ CI / build-windows-msbuild (map[grpc:OFF os:2022 rx:ON tls:ON upnp:ON vs:Visual Studio 17 2022 vspath:C:\Program Files\Microsoft Visual Studio\2022\Enterprise]) (push) Has been cancelled
C/C++ CI / build-windows-msbuild (map[grpc:ON os:2022 rx:ON tls:ON upnp:ON vs:Visual Studio 17 2022 vspath:C:\Program Files\Microsoft Visual Studio\2022\Enterprise]) (push) Has been cancelled
C/C++ CI / build-macos (push) Has been cancelled
C/C++ CI / build-macos-aarch64 (push) Has been cancelled
C/C++ CI / build-freebsd (map[architecture:x86-64 host:ubuntu-latest name:freebsd version:13.3]) (push) Has been cancelled
C/C++ CI / build-openbsd (map[architecture:x86-64 host:ubuntu-latest name:openbsd version:7.4]) (push) Has been cancelled
clang-tidy / clang-tidy (push) Has been cancelled
Code coverage / coverage (push) Has been cancelled
Microsoft C++ Code Analysis / Analyze (push) Has been cancelled
source-snapshot / source-snapshot (push) Failing after 3s
CodeQL / Analyze (cpp) (push) Failing after 14m7s
cppcheck / cppcheck-ubuntu (push) Failing after 7m20s
Sync test / sync-test-ubuntu-tsan (push) Failing after 12m6s
Sync test / sync-test-ubuntu-msan (push) Failing after 17m56s
Sync test / sync-test-ubuntu-ubsan (push) Failing after 12m18s
Sync test / sync-test-ubuntu-asan (push) Failing after 12m9s
cppcheck / cppcheck-windows (push) Has been cancelled
Sync test / sync-test-macos (map[flags: os:macos-15-intel]) (push) Has been cancelled
Sync test / sync-test-macos (map[flags:-target arm64-apple-macos-11 os:macos-15]) (push) Has been cancelled
Sync test / sync-test-windows-debug-asan (push) Has been cancelled
Sync test / sync-test-windows-leaks (push) Has been cancelled
139 lines
4.2 KiB
C
139 lines
4.2 KiB
C
/* Copyright (c) 2022 tevador <tevador@gmail.com>
|
|
*
|
|
* This file is part of mx25519, which is released under LGPLv3.
|
|
* See LICENSE for full license details.
|
|
*/
|
|
|
|
#ifndef MX25519_H
|
|
#define MX25519_H
|
|
|
|
#include <stdint.h>
|
|
#include <stddef.h>
|
|
|
|
/*
|
|
* X25519 scalar (private key).
|
|
* All private keys are implicitly multiples of 8 as the library only uses
|
|
* bits 3-254. Bits 0-2 and 255 are internally set to 0.
|
|
* Note that the key clamping procedure of this library differs from RFC 7748
|
|
* by not setting the value of bit 254 to 1. This is done to support inverted
|
|
* keys, which might have a zero bit in that position.
|
|
*/
|
|
typedef struct mx25519_privkey {
|
|
uint8_t data[32];
|
|
} mx25519_privkey;
|
|
|
|
/*
|
|
* X25519 X-coordinate (public key).
|
|
*/
|
|
typedef struct mx25519_pubkey {
|
|
uint8_t data[32];
|
|
} mx25519_pubkey;
|
|
|
|
/*
|
|
* Opaque struct holding a scalar multiplication implementation.
|
|
*/
|
|
typedef struct mx25519_impl mx25519_impl;
|
|
|
|
/*
|
|
* Implementation types.
|
|
*/
|
|
typedef enum mx25519_type {
|
|
MX25519_TYPE_AUTO = -1, /* select automatically */
|
|
MX25519_TYPE_PORTABLE, /* portable C implementation */
|
|
MX25519_TYPE_ARM64, /* ARM64 assembly */
|
|
MX25519_TYPE_AMD64, /* AMD64 assembly */
|
|
MX25519_TYPE_AMD64X, /* AMD64 assembly with MULX+ADX */
|
|
} mx25519_type;
|
|
|
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
|
#define MX25519_WIN
|
|
#endif
|
|
|
|
/* Shared/static library definitions */
|
|
#ifdef MX25519_WIN
|
|
#ifdef MX25519_SHARED
|
|
#define MX25519_API __declspec(dllexport)
|
|
#elif !defined(MX25519_STATIC)
|
|
#define MX25519_API __declspec(dllimport)
|
|
#else
|
|
#define MX25519_API
|
|
#endif
|
|
#define MX25519_PRIVATE
|
|
#else
|
|
#ifdef MX25519_SHARED
|
|
#define MX25519_API __attribute__ ((visibility ("default")))
|
|
#else
|
|
#define MX25519_API __attribute__ ((visibility ("hidden")))
|
|
#endif
|
|
#define MX25519_PRIVATE __attribute__ ((visibility ("hidden")))
|
|
#endif
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
/*
|
|
* Selects an implementation.
|
|
*
|
|
* @param type is the requested implementation type. If MX25519_TYPE_AUTO
|
|
* is specified, the best implementation for the current machine
|
|
* will be selected.
|
|
*
|
|
* @return pointer to an internal implementation structure. Returns NULL
|
|
* if the requested implementation is not supported.
|
|
*/
|
|
MX25519_API const mx25519_impl* mx25519_select_impl(mx25519_type type);
|
|
|
|
/*
|
|
* @param impl is a pointer to an implementation. Must not be NULL.
|
|
*
|
|
* @return the type of the implementation.
|
|
*/
|
|
MX25519_API mx25519_type mx25519_impl_type(const mx25519_impl* impl);
|
|
|
|
/*
|
|
* Calculates x(key*G), where G is the generator point of Curve25519.
|
|
*
|
|
* @param impl is a pointer to an implementation. Must not be NULL.
|
|
* @param result is the pointer where the resulting public key will be stored.
|
|
* Must not be NULL.
|
|
* @param key is a pointer to the private key. Must not be NULL.
|
|
*/
|
|
MX25519_API void mx25519_scmul_base(const mx25519_impl* impl,
|
|
mx25519_pubkey* result, const mx25519_privkey* key);
|
|
|
|
/*
|
|
* Calculates x(key*P), where P is a given public key.
|
|
*
|
|
* @param impl is a pointer to an implementation. Must not be NULL.
|
|
* @param result is the pointer where the resulting public key will be stored.
|
|
* Must not be NULL.
|
|
* @param key is a pointer to the private key. Must not be NULL.
|
|
* @param p is a pointer to the base point P. Must not be NULL.
|
|
*/
|
|
MX25519_API void mx25519_scmul_key(const mx25519_impl* impl, mx25519_pubkey* result,
|
|
const mx25519_privkey* key, const mx25519_pubkey* p);
|
|
|
|
/*
|
|
* Calculates invkey = 1/(key[0]*key[1]*...). This private key can be used
|
|
* to remove the respective private key components from a public key.
|
|
* (This only works for public keys that lie on Curve25519 and not on
|
|
* its quadratic twist.)
|
|
*
|
|
* @param invkey is the pointer where the resulting private key will be stored.
|
|
* Must not be NULL.
|
|
* @param key is an array of private keys to invert. Must not be NULL.
|
|
* @param num_keys is the number of private keys in the array.
|
|
*
|
|
* @return zero on success, a non-zero value in case of a failure. A failure
|
|
* can occur with a probability of approx. 2^(-124).
|
|
*/
|
|
MX25519_API int mx25519_invkey(mx25519_privkey* invkey,
|
|
const mx25519_privkey keys[], size_t num_keys);
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#endif
|