diff --git a/src/libsodium/Makefile.am b/src/libsodium/Makefile.am index f59e0e84..87e01b28 100644 --- a/src/libsodium/Makefile.am +++ b/src/libsodium/Makefile.am @@ -175,6 +175,8 @@ libsodium_la_SOURCES += \ crypto_core/salsa2012/core_salsa2012_api.c \ crypto_core/salsa208/ref/core_salsa208.c \ crypto_core/salsa208/core_salsa208_api.c \ + crypto_secretbox/xchacha20poly1305/secretbox_xchacha20poly1305_api.c \ + crypto_secretbox/xchacha20poly1305/sodium/box_xchacha20poly1305.c \ crypto_sign/ed25519/ref10/obsolete.c \ crypto_stream/aes128ctr/portable/afternm_aes128ctr.c \ crypto_stream/aes128ctr/stream_aes128ctr_api.c \ diff --git a/src/libsodium/crypto_secretbox/xchacha20poly1305/secretbox_xchacha20poly1305_api.c b/src/libsodium/crypto_secretbox/xchacha20poly1305/secretbox_xchacha20poly1305_api.c new file mode 100644 index 00000000..f8a9958c --- /dev/null +++ b/src/libsodium/crypto_secretbox/xchacha20poly1305/secretbox_xchacha20poly1305_api.c @@ -0,0 +1,31 @@ +#include "crypto_secretbox_xchacha20poly1305.h" + +size_t +crypto_secretbox_xchacha20poly1305_keybytes(void) +{ + return crypto_secretbox_xchacha20poly1305_KEYBYTES; +} + +size_t +crypto_secretbox_xchacha20poly1305_noncebytes(void) +{ + return crypto_secretbox_xchacha20poly1305_NONCEBYTES; +} + +size_t +crypto_secretbox_xchacha20poly1305_zerobytes(void) +{ + return crypto_secretbox_xchacha20poly1305_ZEROBYTES; +} + +size_t +crypto_secretbox_xchacha20poly1305_boxzerobytes(void) +{ + return crypto_secretbox_xchacha20poly1305_BOXZEROBYTES; +} + +size_t +crypto_secretbox_xchacha20poly1305_macbytes(void) +{ + return crypto_secretbox_xchacha20poly1305_MACBYTES; +} diff --git a/src/libsodium/crypto_secretbox/xchacha20poly1305/sodium/box_xchacha20poly1305.c b/src/libsodium/crypto_secretbox/xchacha20poly1305/sodium/box_xchacha20poly1305.c new file mode 100644 index 00000000..e5480ca8 --- /dev/null +++ b/src/libsodium/crypto_secretbox/xchacha20poly1305/sodium/box_xchacha20poly1305.c @@ -0,0 +1,46 @@ +#include "crypto_onetimeauth_poly1305.h" +#include "crypto_secretbox_xchacha20poly1305.h" +#include "crypto_stream_xchacha20.h" + +int +crypto_secretbox_xchacha20poly1305(unsigned char *c, const unsigned char *m, + unsigned long long mlen, + const unsigned char *n, + const unsigned char *k) +{ + int i; + + if (mlen < 32) { + return -1; + } + crypto_stream_xchacha20_xor(c, m, mlen, n, k); + crypto_onetimeauth_poly1305(c + 16, c + 32, mlen - 32, c); + for (i = 0; i < 16; ++i) { + c[i] = 0; + } + return 0; +} + +int +crypto_secretbox_xchacha20poly1305_open(unsigned char *m, const unsigned char *c, + unsigned long long clen, + const unsigned char *n, + const unsigned char *k) +{ + unsigned char subkey[32]; + int i; + + if (clen < 32) { + return -1; + } + crypto_stream_xchacha20(subkey, 32, n, k); + if (crypto_onetimeauth_poly1305_verify(c + 16, c + 32, + clen - 32, subkey) != 0) { + return -1; + } + crypto_stream_xchacha20_xor(m, c, clen, n, k); + for (i = 0; i < 32; ++i) { + m[i] = 0; + } + return 0; +} diff --git a/src/libsodium/include/Makefile.am b/src/libsodium/include/Makefile.am index 4cb58c8e..3d062ffe 100644 --- a/src/libsodium/include/Makefile.am +++ b/src/libsodium/include/Makefile.am @@ -28,6 +28,7 @@ SODIUM_EXPORT = \ sodium/crypto_scalarmult.h \ sodium/crypto_scalarmult_curve25519.h \ sodium/crypto_secretbox.h \ + sodium/crypto_secretbox_xchacha20poly1305.h \ sodium/crypto_secretbox_xsalsa20poly1305.h \ sodium/crypto_shorthash.h \ sodium/crypto_shorthash_siphash24.h \ diff --git a/src/libsodium/include/sodium.h b/src/libsodium/include/sodium.h index 9fbbf99e..b7388ad0 100644 --- a/src/libsodium/include/sodium.h +++ b/src/libsodium/include/sodium.h @@ -30,6 +30,7 @@ #include "sodium/crypto_scalarmult_curve25519.h" #include "sodium/crypto_secretbox.h" #include "sodium/crypto_secretbox_xsalsa20poly1305.h" +#include "sodium/crypto_secretbox_xchacha20poly1305.h" #include "sodium/crypto_shorthash.h" #include "sodium/crypto_shorthash_siphash24.h" #include "sodium/crypto_sign.h" diff --git a/src/libsodium/include/sodium/crypto_secretbox_xchacha20poly1305.h b/src/libsodium/include/sodium/crypto_secretbox_xchacha20poly1305.h new file mode 100644 index 00000000..3129cd1f --- /dev/null +++ b/src/libsodium/include/sodium/crypto_secretbox_xchacha20poly1305.h @@ -0,0 +1,54 @@ +#ifndef crypto_secretbox_xchacha20poly1305_H +#define crypto_secretbox_xchacha20poly1305_H + +#include +#include "export.h" + +#ifdef __cplusplus +# ifdef __GNUC__ +# pragma GCC diagnostic ignored "-Wlong-long" +# endif +extern "C" { +#endif + +#define crypto_secretbox_xchacha20poly1305_KEYBYTES 32U +SODIUM_EXPORT +size_t crypto_secretbox_xchacha20poly1305_keybytes(void); + +#define crypto_secretbox_xchacha20poly1305_NONCEBYTES 24U +SODIUM_EXPORT +size_t crypto_secretbox_xchacha20poly1305_noncebytes(void); + +#define crypto_secretbox_xchacha20poly1305_MACBYTES 16U +SODIUM_EXPORT +size_t crypto_secretbox_xchacha20poly1305_macbytes(void); + +#define crypto_secretbox_xchacha20poly1305_BOXZEROBYTES 16U +SODIUM_EXPORT +size_t crypto_secretbox_xchacha20poly1305_boxzerobytes(void); + +#define crypto_secretbox_xchacha20poly1305_ZEROBYTES \ + (crypto_secretbox_xchacha20poly1305_BOXZEROBYTES + \ + crypto_secretbox_xchacha20poly1305_MACBYTES) +SODIUM_EXPORT +size_t crypto_secretbox_xchacha20poly1305_zerobytes(void); + +SODIUM_EXPORT +int crypto_secretbox_xchacha20poly1305(unsigned char *c, + const unsigned char *m, + unsigned long long mlen, + const unsigned char *n, + const unsigned char *k); + +SODIUM_EXPORT +int crypto_secretbox_xchacha20poly1305_open(unsigned char *m, + const unsigned char *c, + unsigned long long clen, + const unsigned char *n, + const unsigned char *k); + +#ifdef __cplusplus +} +#endif + +#endif