mirror of
https://github.com/jedisct1/libsodium.git
synced 2026-08-27 12:17:12 +09:00
Add HChaCha20
This commit is contained in:
@@ -168,6 +168,8 @@ endif
|
||||
|
||||
if !MINIMAL
|
||||
libsodium_la_SOURCES += \
|
||||
crypto_core/hchacha20/core_hchacha20.c \
|
||||
crypto_core/hchacha20/core_hchacha20.h \
|
||||
crypto_core/salsa2012/ref/core_salsa2012.c \
|
||||
crypto_core/salsa2012/core_salsa2012_api.c \
|
||||
crypto_core/salsa208/ref/core_salsa208.c \
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "core_hchacha20.h"
|
||||
#include "crypto_core_hchacha20.h"
|
||||
|
||||
static const unsigned char sigma[16] = {
|
||||
'e', 'x', 'p', 'a', 'n', 'd', ' ', '3', '2', '-', 'b', 'y', 't', 'e', ' ', 'k'
|
||||
};
|
||||
|
||||
int
|
||||
crypto_core_hchacha20(unsigned char *out, const unsigned char *in,
|
||||
const unsigned char *k, const unsigned char *c)
|
||||
{
|
||||
int i;
|
||||
uint32_t x0, x1, x2, x3, x4, x5, x6, x7;
|
||||
uint32_t x8, x9, x10, x11, x12, x13, x14, x15;
|
||||
|
||||
if (c == NULL) {
|
||||
c = sigma;
|
||||
}
|
||||
x0 = U8TO32_LITTLE(c + 0);
|
||||
x1 = U8TO32_LITTLE(c + 4);
|
||||
x2 = U8TO32_LITTLE(c + 8);
|
||||
x3 = U8TO32_LITTLE(c + 12);
|
||||
x4 = U8TO32_LITTLE(k + 0);
|
||||
x5 = U8TO32_LITTLE(k + 4);
|
||||
x6 = U8TO32_LITTLE(k + 8);
|
||||
x7 = U8TO32_LITTLE(k + 12);
|
||||
x8 = U8TO32_LITTLE(k + 16);
|
||||
x9 = U8TO32_LITTLE(k + 20);
|
||||
x10 = U8TO32_LITTLE(k + 24);
|
||||
x11 = U8TO32_LITTLE(k + 28);
|
||||
x12 = U8TO32_LITTLE(in + 0);
|
||||
x13 = U8TO32_LITTLE(in + 4);
|
||||
x14 = U8TO32_LITTLE(in + 8);
|
||||
x15 = U8TO32_LITTLE(in + 12);
|
||||
|
||||
for (i = 0; i < 10; i++) {
|
||||
QUARTERROUND(x0, x4, x8, x12);
|
||||
QUARTERROUND(x1, x5, x9, x13);
|
||||
QUARTERROUND(x2, x6, x10, x14);
|
||||
QUARTERROUND(x3, x7, x11, x15);
|
||||
QUARTERROUND(x0, x5, x10, x15);
|
||||
QUARTERROUND(x1, x6, x11, x12);
|
||||
QUARTERROUND(x2, x7, x8, x13);
|
||||
QUARTERROUND(x3, x4, x9, x14);
|
||||
}
|
||||
|
||||
U32TO8_LITTLE(out + 0, x0);
|
||||
U32TO8_LITTLE(out + 4, x1);
|
||||
U32TO8_LITTLE(out + 8, x2);
|
||||
U32TO8_LITTLE(out + 12, x3);
|
||||
U32TO8_LITTLE(out + 16, x12);
|
||||
U32TO8_LITTLE(out + 20, x13);
|
||||
U32TO8_LITTLE(out + 24, x14);
|
||||
U32TO8_LITTLE(out + 28, x15);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t
|
||||
crypto_core_hchacha20_outputbytes(void)
|
||||
{
|
||||
return crypto_core_hchacha20_OUTPUTBYTES;
|
||||
}
|
||||
|
||||
size_t
|
||||
crypto_core_hchacha20_inputbytes(void)
|
||||
{
|
||||
return crypto_core_hchacha20_INPUTBYTES;
|
||||
}
|
||||
|
||||
size_t
|
||||
crypto_core_hchacha20_keybytes(void)
|
||||
{
|
||||
return crypto_core_hchacha20_KEYBYTES;
|
||||
}
|
||||
|
||||
size_t
|
||||
crypto_core_hchacha20_constbytes(void)
|
||||
{
|
||||
return crypto_core_hchacha20_CONSTBYTES;
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
#ifndef core_hchacha20_H
|
||||
#define core_hchacha20_H
|
||||
|
||||
#define U8C(v) (v##U)
|
||||
#define U32C(v) (v##U)
|
||||
|
||||
#define U8V(v) ((uint8_t)(v) & U8C(0xFF))
|
||||
#define U32V(v) ((uint32_t)(v) & U32C(0xFFFFFFFF))
|
||||
|
||||
#define ROTL32(v, n) (U32V((v) << (n)) | ((v) >> (32 - (n))))
|
||||
|
||||
#define U8TO32_LITTLE(p) \
|
||||
(((uint32_t)((p)[0]) ) | \
|
||||
((uint32_t)((p)[1]) << 8) | \
|
||||
((uint32_t)((p)[2]) << 16) | \
|
||||
((uint32_t)((p)[3]) << 24))
|
||||
|
||||
#define U32TO8_LITTLE(p, v) \
|
||||
do { \
|
||||
(p)[0] = U8V((v) ); \
|
||||
(p)[1] = U8V((v) >> 8); \
|
||||
(p)[2] = U8V((v) >> 16); \
|
||||
(p)[3] = U8V((v) >> 24); \
|
||||
} while (0)
|
||||
|
||||
#define ROTATE(v, c) (ROTL32(v, c))
|
||||
#define XOR(v, w) ((v) ^ (w))
|
||||
#define PLUS(v, w) (U32V((v) + (w)))
|
||||
|
||||
#define QUARTERROUND(a, b, c, d) \
|
||||
do { \
|
||||
a = PLUS(a, b); d = ROTATE(XOR(d, a), 16); \
|
||||
c = PLUS(c, d); b = ROTATE(XOR(b, c), 12); \
|
||||
a = PLUS(a, b); d = ROTATE(XOR(d, a), 8); \
|
||||
c = PLUS(c, d); b = ROTATE(XOR(b, c), 7); \
|
||||
} while(0)
|
||||
|
||||
#endif
|
||||
@@ -10,6 +10,7 @@ SODIUM_EXPORT = \
|
||||
sodium/crypto_auth_hmacsha512256.h \
|
||||
sodium/crypto_box.h \
|
||||
sodium/crypto_box_curve25519xsalsa20poly1305.h \
|
||||
sodium/crypto_core_hchacha20.h \
|
||||
sodium/crypto_core_hsalsa20.h \
|
||||
sodium/crypto_core_salsa20.h \
|
||||
sodium/crypto_core_salsa2012.h \
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include "sodium/crypto_box.h"
|
||||
#include "sodium/crypto_box_curve25519xsalsa20poly1305.h"
|
||||
#include "sodium/crypto_core_hsalsa20.h"
|
||||
#include "sodium/crypto_core_hchacha20.h"
|
||||
#include "sodium/crypto_core_salsa20.h"
|
||||
#include "sodium/crypto_core_salsa2012.h"
|
||||
#include "sodium/crypto_core_salsa208.h"
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
#ifndef crypto_core_hchacha20_H
|
||||
#define crypto_core_hchacha20_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include "export.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define crypto_core_hchacha20_OUTPUTBYTES 32U
|
||||
SODIUM_EXPORT
|
||||
size_t crypto_core_hchacha20_outputbytes(void);
|
||||
|
||||
#define crypto_core_hchacha20_INPUTBYTES 16U
|
||||
SODIUM_EXPORT
|
||||
size_t crypto_core_hchacha20_inputbytes(void);
|
||||
|
||||
#define crypto_core_hchacha20_KEYBYTES 32U
|
||||
SODIUM_EXPORT
|
||||
size_t crypto_core_hchacha20_keybytes(void);
|
||||
|
||||
#define crypto_core_hchacha20_CONSTBYTES 16U
|
||||
SODIUM_EXPORT
|
||||
size_t crypto_core_hchacha20_constbytes(void);
|
||||
|
||||
SODIUM_EXPORT
|
||||
int crypto_core_hchacha20(unsigned char *out, const unsigned char *in,
|
||||
const unsigned char *k, const unsigned char *c);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user