Git Product home page Git Product logo

curve25519's People

Contributors

linusu avatar matbech avatar msotoodeh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

curve25519's Issues

Fantastic!!

this is not really an issue, but I was wondering if you had plans to vectorize your code. I think most of the code would work as a portable (probably not using ms compilers though) vectorized implementation using
attribute((ext_vector_type(4)));

but not sure how it would compare to your asm version. Of course an asm vector implementation could get ~3x the speed

Anyway, wanted to say thanks! this is quite nice implementation.

James

Mac support?

I develop on mac and it's not currently supported -- can you guide me on what needs to change to compile this library on Apple machines(x86_64-apple-darwin20.3.0) ?

Secret key changed first byte after public key creation

I can't understand why after public key creation secret key first byte changed. For example:

unsigned char aliceSecret[32];
unsigned char alicePublic[32];

memset(aliceSecret, 'a', 32);

for (int i = 0; i < 32; i++)
{
    cout << hex << (int)aliceSecret[i] << ' ';
}
cout << endl;

curve25519_dh_CalculatePublicKey_fast(alicePublic, aliceSecret); 

for (int i = 0; i < 32; i++)
{
    cout << hex << (int)aliceSecret[i] << ' ';
}

Prints:
61 61 61 61 61 ... 61
60 61 61 61 61 ... 61

I will try another library and test if I get some result.

building for wasm failed

Hi, when i build curve25519 for wasm, some error occurred

The following warnings were emitted during compilation:

warning: error: unable to create target: 'No available targets are compatible with triple "wasm32-unknown-unknown"'
warning: 1 error generated.

error: failed to run custom build command for `clear_on_drop v0.2.5`

Is this module support for wasm?

Comparison with OpenSSL

Hey πŸ‘‹

I see that you are talking about curve25519-donna, but I was wondering if you have compared it to OpenSSL which gained curve25519 support in version 1.1.0.

I've started to see how some function calls translate to OpenSSL and figured I should post them here in case you are interested.

If this is not something that you are interested in, then feel free to just close out the issue without action ☺️

#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <openssl/evp.h>

#include "curve25519/include/curve25519_dh.h"
#include "curve25519/include/ed25519_signature.h"

void print_hex(uint8_t *data, size_t length) {
  printf("  ");
  for (size_t i = 0; i < length; i++) printf("%02X", data[i]);
  printf("\n");
}

void openssl_CreateKeyPair() {
  int status;

  uint8_t secret[32] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F };

  EVP_PKEY *key = EVP_PKEY_new_raw_private_key(EVP_PKEY_ED25519, NULL, secret, 32);

  uint8_t private[64] = { 0 };
  size_t private_size = 32;

  status = EVP_PKEY_get_raw_private_key(key, private, &private_size);
  assert(status == 1);
  assert(private_size == 32);

  status = EVP_PKEY_get_raw_public_key(key, &private[32], &private_size);
  assert(status == 1);
  assert(private_size == 32);

  uint8_t public[32] = { 0 };
  size_t public_size = 32;
  status = EVP_PKEY_get_raw_public_key(key, public, &public_size);
  assert(status == 1);
  assert(public_size == 32);

  printf("OpenSSL CreateKeyPair:\n");
  print_hex(private, 64);
  print_hex(public, 32);
}

void curve25519_CreateKeyPair() {
  uint8_t secret[32] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F };

  uint8_t private[64] = { 0 };
  uint8_t public[64] = { 0 };
  ed25519_CreateKeyPair(public, private, NULL, secret);

  printf("Curve25519 CreateKeyPair:\n");
  print_hex(private, 64);
  print_hex(public, 32);
}

void openssl_CalculatePublicKey() {
  int status;

  uint8_t secret[32] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F };

  EVP_PKEY *key = EVP_PKEY_new_raw_private_key(EVP_PKEY_X25519, NULL, secret, 32);

  uint8_t public[32] = { 0 };
  size_t public_size = 32;
  status = EVP_PKEY_get_raw_public_key(key, public, &public_size);
  assert(status == 1);
  assert(public_size == 32);

  printf("OpenSSL CalculatePublicKey:\n");
  print_hex(public, 32);
}

void curve25519_CalculatePublicKey() {
  uint8_t secret[32] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F };
  uint8_t public[32] = { 0 };

  curve25519_dh_CalculatePublicKey(public, secret);

  printf("Curve25519 CalculatePublicKey:\n");
  print_hex(public, 32);
}

void openssl_CreateSharedKey() {
  int status;

  uint8_t secret[32] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F };
  uint8_t peer_public[32] = { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F };

  EVP_PKEY *key = EVP_PKEY_new_raw_private_key(EVP_PKEY_X25519, NULL, secret, 32);
  EVP_PKEY *peer_key = EVP_PKEY_new_raw_public_key(EVP_PKEY_X25519, NULL, peer_public, 32);

  EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new(key, NULL);

  status = EVP_PKEY_derive_init(ctx);
  assert(status == 1);

  status = EVP_PKEY_derive_set_peer(ctx, peer_key);
  assert(status == 1);

  uint8_t result[32] = { 0 };
  size_t size = 32;
  status = EVP_PKEY_derive(ctx, result, &size);
  assert(status == 1);
  assert(size == 32);

  printf("OpenSSL CreateSharedKey:\n");
  print_hex(result, 32);
}

void curve25519_CreateSharedKey() {
  uint8_t secret[32] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F };
  uint8_t peer_public[32] = { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F };

  uint8_t result[32] = { 0 };
  curve25519_dh_CreateSharedKey(result, peer_public, secret);

  printf("Curve25519 CreateSharedKey:\n");
  print_hex(result, 32);
}

void openssl_SignMessage() {
  int status;

  uint8_t secret[32] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F };
  uint8_t msg[32] = { 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F };

  EVP_MD_CTX *ctx = EVP_MD_CTX_new();
  assert(ctx != NULL);
  EVP_PKEY *pkey = EVP_PKEY_new_raw_private_key(EVP_PKEY_ED25519, NULL, secret, 32);
  assert(pkey != NULL);

  status = EVP_DigestSignInit(ctx, NULL, NULL, NULL, pkey);
  assert(status == 1);

  uint8_t signature[64] = { 0 };
  size_t signature_length = 64;

  status = EVP_DigestSign(ctx, signature, &signature_length, msg, 32);
  assert(status == 1);
  assert(signature_length == 64);

  printf("OpenSSL SignMessage:\n");
  print_hex(signature, 64);
}

void curve25519_SignMessage() {
  uint8_t key[64] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x03, 0xA1, 0x07, 0xBF, 0xF3, 0xCE, 0x10, 0xBE, 0x1D, 0x70, 0xDD, 0x18, 0xE7, 0x4B, 0xC0, 0x99, 0x67, 0xE4, 0xD6, 0x30, 0x9B, 0xA5, 0x0D, 0x5F, 0x1D, 0xDC, 0x86, 0x64, 0x12, 0x55, 0x31, 0xB8 };
  uint8_t msg[32] = { 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F };

  uint8_t signature[64] = { 0 };
  ed25519_SignMessage(signature, key, NULL, msg, 32);

  printf("Curve25519 SignMessage:\n");
  print_hex(signature, 64);
}

int main() {
  printf("\n");
  curve25519_CreateKeyPair();
  openssl_CreateKeyPair();
  printf("\n");
  curve25519_CalculatePublicKey();
  openssl_CalculatePublicKey();
  printf("\n");
  curve25519_CreateSharedKey();
  openssl_CreateSharedKey();
  printf("\n");
  curve25519_SignMessage();
  openssl_SignMessage();
  printf("\n");
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.