From c565993885cdc9f6c0f2394f2d5be298b2d594ae Mon Sep 17 00:00:00 2001 From: Jan Varho Date: Fri, 23 May 2014 10:01:27 +0300 Subject: [PATCH 1/2] Add crypto_box_seed_keypair like crypto_sign_seed_keypair Uses sk = seed. --- src/libsodium/crypto_box/crypto_box.c | 13 +++++++++++++ .../box_curve25519xsalsa20poly1305_api.c | 5 +++++ .../crypto_box/curve25519xsalsa20poly1305/ref/api.h | 2 ++ .../ref/keypair_curve25519xsalsa20poly1305.c | 12 ++++++++++++ src/libsodium/include/sodium/crypto_box.h | 8 ++++++++ .../sodium/crypto_box_curve25519xsalsa20poly1305.h | 12 +++++++++++- 6 files changed, 51 insertions(+), 1 deletion(-) diff --git a/src/libsodium/crypto_box/crypto_box.c b/src/libsodium/crypto_box/crypto_box.c index 7ec820a4..7ae4297c 100644 --- a/src/libsodium/crypto_box/crypto_box.c +++ b/src/libsodium/crypto_box/crypto_box.c @@ -1,6 +1,12 @@ #include "crypto_box.h" +size_t +crypto_box_seedbytes(void) +{ + return crypto_box_SEEDBYTES; +} + size_t crypto_box_publickeybytes(void) { @@ -49,6 +55,13 @@ crypto_box_primitive(void) return crypto_box_PRIMITIVE; } +int +crypto_box_seed_keypair(unsigned char *pk, unsigned char *sk, + const unsigned char *seed) +{ + return crypto_box_curve25519xsalsa20poly1305_seed_keypair(pk, sk, seed); +} + int crypto_box_keypair(unsigned char *pk, unsigned char *sk) { diff --git a/src/libsodium/crypto_box/curve25519xsalsa20poly1305/box_curve25519xsalsa20poly1305_api.c b/src/libsodium/crypto_box/curve25519xsalsa20poly1305/box_curve25519xsalsa20poly1305_api.c index 6df56a17..1c002d2d 100644 --- a/src/libsodium/crypto_box/curve25519xsalsa20poly1305/box_curve25519xsalsa20poly1305_api.c +++ b/src/libsodium/crypto_box/curve25519xsalsa20poly1305/box_curve25519xsalsa20poly1305_api.c @@ -1,5 +1,10 @@ #include "crypto_box_curve25519xsalsa20poly1305.h" +size_t +crypto_box_curve25519xsalsa20poly1305_seedbytes(void) { + return crypto_box_curve25519xsalsa20poly1305_SEEDBYTES; +} + size_t crypto_box_curve25519xsalsa20poly1305_publickeybytes(void) { return crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES; diff --git a/src/libsodium/crypto_box/curve25519xsalsa20poly1305/ref/api.h b/src/libsodium/crypto_box/curve25519xsalsa20poly1305/ref/api.h index aa39d90f..7f320c6f 100644 --- a/src/libsodium/crypto_box/curve25519xsalsa20poly1305/ref/api.h +++ b/src/libsodium/crypto_box/curve25519xsalsa20poly1305/ref/api.h @@ -3,10 +3,12 @@ #define crypto_box crypto_box_curve25519xsalsa20poly1305 #define crypto_box_open crypto_box_curve25519xsalsa20poly1305_open +#define crypto_box_seed_keypair crypto_box_curve25519xsalsa20poly1305_seed_keypair #define crypto_box_keypair crypto_box_curve25519xsalsa20poly1305_keypair #define crypto_box_beforenm crypto_box_curve25519xsalsa20poly1305_beforenm #define crypto_box_afternm crypto_box_curve25519xsalsa20poly1305_afternm #define crypto_box_open_afternm crypto_box_curve25519xsalsa20poly1305_open_afternm +#define crypto_box_SEEDBYTES crypto_box_curve25519xsalsa20poly1305_SEEDBYTES #define crypto_box_PUBLICKEYBYTES crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES #define crypto_box_SECRETKEYBYTES crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES #define crypto_box_BEFORENMBYTES crypto_box_curve25519xsalsa20poly1305_BEFORENMBYTES diff --git a/src/libsodium/crypto_box/curve25519xsalsa20poly1305/ref/keypair_curve25519xsalsa20poly1305.c b/src/libsodium/crypto_box/curve25519xsalsa20poly1305/ref/keypair_curve25519xsalsa20poly1305.c index 69fb1cfc..9774915f 100644 --- a/src/libsodium/crypto_box/curve25519xsalsa20poly1305/ref/keypair_curve25519xsalsa20poly1305.c +++ b/src/libsodium/crypto_box/curve25519xsalsa20poly1305/ref/keypair_curve25519xsalsa20poly1305.c @@ -1,7 +1,19 @@ +#include + #include "crypto_scalarmult_curve25519.h" #include "api.h" #include "randombytes.h" +int crypto_box_seed_keypair( + unsigned char *pk, + unsigned char *sk, + const unsigned char *seed +) +{ + memmove(sk, seed, 32); + return crypto_scalarmult_curve25519_base(pk,sk); +} + int crypto_box_keypair( unsigned char *pk, unsigned char *sk diff --git a/src/libsodium/include/sodium/crypto_box.h b/src/libsodium/include/sodium/crypto_box.h index e65db71b..9c1a64bf 100644 --- a/src/libsodium/include/sodium/crypto_box.h +++ b/src/libsodium/include/sodium/crypto_box.h @@ -20,6 +20,10 @@ extern "C" { #endif +#define crypto_box_SEEDBYTES crypto_box_curve25519xsalsa20poly1305_SEEDBYTES +SODIUM_EXPORT +size_t crypto_box_seedbytes(void); + #define crypto_box_PUBLICKEYBYTES crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES SODIUM_EXPORT size_t crypto_box_publickeybytes(void); @@ -52,6 +56,10 @@ size_t crypto_box_macbytes(void); SODIUM_EXPORT const char *crypto_box_primitive(void); +SODIUM_EXPORT +int crypto_box_seed_keypair(unsigned char *pk, unsigned char *sk, + const unsigned char *seed); + SODIUM_EXPORT int crypto_box_keypair(unsigned char *pk, unsigned char *sk); diff --git a/src/libsodium/include/sodium/crypto_box_curve25519xsalsa20poly1305.h b/src/libsodium/include/sodium/crypto_box_curve25519xsalsa20poly1305.h index 54e73005..865602fa 100644 --- a/src/libsodium/include/sodium/crypto_box_curve25519xsalsa20poly1305.h +++ b/src/libsodium/include/sodium/crypto_box_curve25519xsalsa20poly1305.h @@ -11,6 +11,10 @@ extern "C" { #endif +#define crypto_box_curve25519xsalsa20poly1305_SEEDBYTES 32U +SODIUM_EXPORT +size_t crypto_box_curve25519xsalsa20poly1305_seedbytes(void); + #define crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES 32U SODIUM_EXPORT size_t crypto_box_curve25519xsalsa20poly1305_publickeybytes(void); @@ -58,7 +62,13 @@ int crypto_box_curve25519xsalsa20poly1305_open(unsigned char *m, const unsigned char *sk); SODIUM_EXPORT -int crypto_box_curve25519xsalsa20poly1305_keypair(unsigned char *pk, unsigned char *sk); +int crypto_box_curve25519xsalsa20poly1305_seed_keypair(unsigned char *pk, + unsigned char *sk, + const unsigned char *seed); + +SODIUM_EXPORT +int crypto_box_curve25519xsalsa20poly1305_keypair(unsigned char *pk, + unsigned char *sk); SODIUM_EXPORT int crypto_box_curve25519xsalsa20poly1305_beforenm(unsigned char *k, From f438116b6ba4f99c6a7ccafc4ac92623715ae73a Mon Sep 17 00:00:00 2001 From: Jan Varho Date: Fri, 23 May 2014 10:22:52 +0300 Subject: [PATCH 2/2] Test crypto_box_seed_keypair --- .gitignore | 1 + test/default/Makefile.am | 6 ++++++ test/default/box_seed.c | 28 ++++++++++++++++++++++++++++ test/default/box_seed.exp | 8 ++++++++ 4 files changed, 43 insertions(+) create mode 100644 test/default/box_seed.c create mode 100644 test/default/box_seed.exp diff --git a/.gitignore b/.gitignore index 23139844..925e6a20 100644 --- a/.gitignore +++ b/.gitignore @@ -65,6 +65,7 @@ test/default/box7 test/default/box8 test/default/box_easy test/default/box_easy2 +test/default/box_seed test/default/chacha20 test/default/core1 test/default/core2 diff --git a/test/default/Makefile.am b/test/default/Makefile.am index f4a4aab1..0b09a26d 100644 --- a/test/default/Makefile.am +++ b/test/default/Makefile.am @@ -14,6 +14,7 @@ EXTRA_DIST = \ box8.exp \ box_easy.exp \ box_easy2.exp \ + box_seed.exp \ chacha20.exp \ core1.exp \ core2.exp \ @@ -69,6 +70,7 @@ DISTCLEANFILES = \ box8.res \ box_easy.res \ box_easy2.res \ + box_seed.res \ chacha20.res \ core1.res \ core2.res \ @@ -132,6 +134,7 @@ TESTS_TARGETS = \ box8 \ box_easy \ box_easy2 \ + box_seed \ chacha20 \ core1 \ core2 \ @@ -217,6 +220,9 @@ box_easy_LDADD = $(TESTS_LDADD) box_easy2_SOURCE = cmptest.h box_easy2.c box_easy2_LDADD = $(TESTS_LDADD) +box_seed_SOURCE = cmptest.h box_seed.c +box_seed_LDADD = $(TESTS_LDADD) + chacha20_SOURCE = cmptest.h chacha20.c chacha20_LDADD = $(TESTS_LDADD) diff --git a/test/default/box_seed.c b/test/default/box_seed.c new file mode 100644 index 00000000..6533d8cb --- /dev/null +++ b/test/default/box_seed.c @@ -0,0 +1,28 @@ +#include + +#define TEST_NAME "box_seed" +#include "cmptest.h" + +unsigned char seed[32] = { + 0x77,0x07,0x6d,0x0a,0x73,0x18,0xa5,0x7d +,0x3c,0x16,0xc1,0x72,0x51,0xb2,0x66,0x45 +,0xdf,0x4c,0x2f,0x87,0xeb,0xc0,0x99,0x2a +,0xb1,0x77,0xfb,0xa5,0x1d,0xb9,0x2c,0x2a +}; + +int main(void) +{ + int i; + unsigned char sk[32]; + unsigned char pk[32]; + crypto_box_curve25519xsalsa20poly1305_seed_keypair(pk, sk, seed); + for (i = 0;i < 32;++i) { + printf(",0x%02x",(unsigned int) pk[i]); + if (i % 8 == 7) printf("\n"); + } + for (i = 0;i < 32;++i) { + printf(",0x%02x",(unsigned int) sk[i]); + if (i % 8 == 7) printf("\n"); + } + return 0; +} diff --git a/test/default/box_seed.exp b/test/default/box_seed.exp new file mode 100644 index 00000000..8a3803e4 --- /dev/null +++ b/test/default/box_seed.exp @@ -0,0 +1,8 @@ +,0x85,0x20,0xf0,0x09,0x89,0x30,0xa7,0x54 +,0x74,0x8b,0x7d,0xdc,0xb4,0x3e,0xf7,0x5a +,0x0d,0xbf,0x3a,0x0d,0x26,0x38,0x1a,0xf4 +,0xeb,0xa4,0xa9,0x8e,0xaa,0x9b,0x4e,0x6a +,0x77,0x07,0x6d,0x0a,0x73,0x18,0xa5,0x7d +,0x3c,0x16,0xc1,0x72,0x51,0xb2,0x66,0x45 +,0xdf,0x4c,0x2f,0x87,0xeb,0xc0,0x99,0x2a +,0xb1,0x77,0xfb,0xa5,0x1d,0xb9,0x2c,0x2a