global: normalize -> clamp

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2019-01-11 01:13:35 -08:00
parent 3ac679e7a1
commit 777fe674c4
5 changed files with 9 additions and 9 deletions

View file

@ -1688,7 +1688,7 @@ static void invert(fe o, const fe i)
memzero_explicit(c, sizeof(c)); memzero_explicit(c, sizeof(c));
} }
static void normalize_key(uint8_t *z) static void clamp_key(uint8_t *z)
{ {
z[31] = (z[31] & 127) | 64; z[31] = (z[31] & 127) | 64;
z[0] &= 248; z[0] &= 248;
@ -1701,7 +1701,7 @@ void wg_generate_public_key(wg_key public_key, const wg_key private_key)
fe a = { 1 }, b = { 9 }, c = { 0 }, d = { 1 }, e, f; fe a = { 1 }, b = { 9 }, c = { 0 }, d = { 1 }, e, f;
memcpy(z, private_key, sizeof(z)); memcpy(z, private_key, sizeof(z));
normalize_key(z); clamp_key(z);
for (i = 254; i >= 0; --i) { for (i = 254; i >= 0; --i) {
r = (z[i >> 3] >> (i & 7)) & 1; r = (z[i >> 3] >> (i & 7)) & 1;
@ -1745,7 +1745,7 @@ void wg_generate_public_key(wg_key public_key, const wg_key private_key)
void wg_generate_private_key(wg_key private_key) void wg_generate_private_key(wg_key private_key)
{ {
wg_generate_preshared_key(private_key); wg_generate_preshared_key(private_key);
normalize_key(private_key); clamp_key(private_key);
} }
void wg_generate_preshared_key(wg_key preshared_key) void wg_generate_preshared_key(wg_key preshared_key)

View file

@ -91,7 +91,7 @@
o[a] = c[a]; o[a] = c[a];
} }
function normalizeKey(z) { function clamp(z) {
z[31] = (z[31] & 127) | 64; z[31] = (z[31] & 127) | 64;
z[0] &= 248; z[0] &= 248;
} }
@ -108,7 +108,7 @@
_9 = gf([9]); _9 = gf([9]);
for (var i = 0; i < 32; ++i) for (var i = 0; i < 32; ++i)
z[i] = privateKey[i]; z[i] = privateKey[i];
normalizeKey(z); clamp(z);
for (var i = 254; i >= 0; --i) { for (var i = 254; i >= 0; --i) {
r = (z[i >>> 3] >>> (i & 7)) & 1; r = (z[i >>> 3] >>> (i & 7)) & 1;
cswap(a, b, r); cswap(a, b, r);
@ -148,7 +148,7 @@
function generatePrivateKey() { function generatePrivateKey() {
var privateKey = generatePresharedKey(); var privateKey = generatePresharedKey();
normalizeKey(privateKey); clamp(privateKey);
return privateKey; return privateKey;
} }

View file

@ -68,7 +68,7 @@ static inline void put_unaligned_le64(u64 s, u8 *d)
#ifndef __force #ifndef __force
#define __force #define __force
#endif #endif
#define normalize_secret(a) curve25519_normalize_secret(a) #define clamp_secret(a) curve25519_clamp_secret(a)
static noinline void memzero_explicit(void *s, size_t count) static noinline void memzero_explicit(void *s, size_t count)
{ {

View file

@ -15,7 +15,7 @@ enum curve25519_lengths {
void curve25519(uint8_t mypublic[static CURVE25519_KEY_SIZE], const uint8_t secret[static CURVE25519_KEY_SIZE], const uint8_t basepoint[static CURVE25519_KEY_SIZE]); void curve25519(uint8_t mypublic[static CURVE25519_KEY_SIZE], const uint8_t secret[static CURVE25519_KEY_SIZE], const uint8_t basepoint[static CURVE25519_KEY_SIZE]);
void curve25519_generate_public(uint8_t pub[static CURVE25519_KEY_SIZE], const uint8_t secret[static CURVE25519_KEY_SIZE]); void curve25519_generate_public(uint8_t pub[static CURVE25519_KEY_SIZE], const uint8_t secret[static CURVE25519_KEY_SIZE]);
static inline void curve25519_normalize_secret(uint8_t secret[static CURVE25519_KEY_SIZE]) static inline void curve25519_clamp_secret(uint8_t secret[static CURVE25519_KEY_SIZE])
{ {
secret[0] &= 248; secret[0] &= 248;
secret[31] &= 127; secret[31] &= 127;

View file

@ -71,7 +71,7 @@ int genkey_main(int argc, char *argv[])
return 1; return 1;
} }
if (!strcmp(argv[0], "genkey")) if (!strcmp(argv[0], "genkey"))
curve25519_normalize_secret(key); curve25519_clamp_secret(key);
key_to_base64(base64, key); key_to_base64(base64, key);
puts(base64); puts(base64);