1
mirror of https://github.com/jedisct1/libsodium.git synced 2024-12-24 20:45:17 -07:00

Do not invent your own types

This commit is contained in:
Frank Denis 2017-02-23 10:34:32 +01:00
parent a60ac31ba4
commit 14d54b9d22
2 changed files with 102 additions and 101 deletions

View File

@ -9,26 +9,23 @@
#include <stdlib.h>
#include <string.h>
#include "utils.h"
#include "crypto_stream_chacha20.h"
#include "stream_chacha20_ref.h"
#include "../stream_chacha20.h"
#include "crypto_stream_chacha20.h"
#include "private/common.h"
#include "stream_chacha20_ref.h"
#include "utils.h"
struct chacha_ctx {
uint32_t input[16];
};
typedef uint8_t u8;
typedef uint32_t u32;
typedef struct chacha_ctx chacha_ctx;
#define U8C(v) (v##U)
#define U32C(v) (v##U)
#define U8V(v) ((u8)(v) & U8C(0xFF))
#define U32V(v) ((u32)(v) & U32C(0xFFFFFFFF))
#define U8V(v) ((uint8_t)(v) &U8C(0xFF))
#define U32V(v) ((uint32_t)(v) &U32C(0xFFFFFFFF))
#define ROTATE(v, c) (ROTL32(v, c))
#define XOR(v, w) ((v) ^ (w))
@ -36,13 +33,17 @@ typedef struct chacha_ctx chacha_ctx;
#define PLUSONE(v) (PLUS((v), 1))
#define QUARTERROUND(a, b, c, d) \
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);
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);
static void
chacha_keysetup(chacha_ctx *ctx, const u8 *k)
chacha_keysetup(chacha_ctx *ctx, const uint8_t *k)
{
ctx->input[0] = U32C(0x61707865);
ctx->input[1] = U32C(0x3320646e);
@ -59,7 +60,7 @@ chacha_keysetup(chacha_ctx *ctx, const u8 *k)
}
static void
chacha_ivsetup(chacha_ctx *ctx, const u8 *iv, const u8 *counter)
chacha_ivsetup(chacha_ctx *ctx, const uint8_t *iv, const uint8_t *counter)
{
ctx->input[12] = counter == NULL ? 0 : LOAD32_LE(counter + 0);
ctx->input[13] = counter == NULL ? 0 : LOAD32_LE(counter + 4);
@ -68,7 +69,7 @@ chacha_ivsetup(chacha_ctx *ctx, const u8 *iv, const u8 *counter)
}
static void
chacha_ietf_ivsetup(chacha_ctx *ctx, const u8 *iv, const u8 *counter)
chacha_ietf_ivsetup(chacha_ctx *ctx, const uint8_t *iv, const uint8_t *counter)
{
ctx->input[12] = counter == NULL ? 0 : LOAD32_LE(counter);
ctx->input[13] = LOAD32_LE(iv + 0);
@ -77,12 +78,15 @@ chacha_ietf_ivsetup(chacha_ctx *ctx, const u8 *iv, const u8 *counter)
}
static void
chacha_encrypt_bytes(chacha_ctx *ctx, const u8 *m, u8 *c, unsigned long long bytes)
chacha_encrypt_bytes(chacha_ctx *ctx, const uint8_t *m, uint8_t *c,
unsigned long long bytes)
{
u32 x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15;
u32 j0, j1, j2, j3, j4, j5, j6, j7, j8, j9, j10, j11, j12, j13, j14, j15;
u8 *ctarget = NULL;
u8 tmp[64];
uint32_t x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14,
x15;
uint32_t j0, j1, j2, j3, j4, j5, j6, j7, j8, j9, j10, j11, j12, j13, j14,
j15;
uint8_t * ctarget = NULL;
uint8_t tmp[64];
unsigned int i;
if (!bytes) {
@ -210,6 +214,7 @@ chacha_encrypt_bytes(chacha_ctx *ctx, const u8 *m, u8 *c, unsigned long long byt
}
ctx->input[12] = j12;
ctx->input[13] = j13;
return;
}
bytes -= 64;
@ -219,8 +224,8 @@ chacha_encrypt_bytes(chacha_ctx *ctx, const u8 *m, u8 *c, unsigned long long byt
}
static int
stream_ref(unsigned char *c, unsigned long long clen,
const unsigned char *n, const unsigned char *k)
stream_ref(unsigned char *c, unsigned long long clen, const unsigned char *n,
const unsigned char *k)
{
struct chacha_ctx ctx;
@ -258,8 +263,7 @@ stream_ietf_ref(unsigned char *c, unsigned long long clen,
static int
stream_ref_xor_ic(unsigned char *c, const unsigned char *m,
unsigned long long mlen,
const unsigned char *n, uint64_t ic,
unsigned long long mlen, const unsigned char *n, uint64_t ic,
const unsigned char *k)
{
struct chacha_ctx ctx;
@ -284,9 +288,8 @@ stream_ref_xor_ic(unsigned char *c, const unsigned char *m,
static int
stream_ietf_ref_xor_ic(unsigned char *c, const unsigned char *m,
unsigned long long mlen,
const unsigned char *n, uint32_t ic,
const unsigned char *k)
unsigned long long mlen, const unsigned char *n,
uint32_t ic, const unsigned char *k)
{
struct chacha_ctx ctx;
uint8_t ic_bytes[4];

View File

@ -1,28 +1,26 @@
#include <stdint.h>
#include "crypto_stream_chacha20.h"
#include "../stream_chacha20.h"
#include "crypto_stream_chacha20.h"
extern struct crypto_stream_chacha20_implementation
crypto_stream_chacha20_ref_implementation;
int
crypto_stream_chacha20_ref(unsigned char *c, unsigned long long clen,
int crypto_stream_chacha20_ref(unsigned char *c, unsigned long long clen,
const unsigned char *n, const unsigned char *k);
int
crypto_stream_chacha20_ref_xor_ic(unsigned char *c, const unsigned char *m,
int crypto_stream_chacha20_ref_xor_ic(unsigned char *c, const unsigned char *m,
unsigned long long mlen,
const unsigned char *n, uint64_t ic,
const unsigned char *k);
int
crypto_stream_chacha20_ietf_ref(unsigned char *c, unsigned long long clen,
const unsigned char *n, const unsigned char *k);
int crypto_stream_chacha20_ietf_ref(unsigned char *c, unsigned long long clen,
const unsigned char *n,
const unsigned char *k);
int
crypto_stream_chacha20_ietf_ref_xor_ic(unsigned char *c, const unsigned char *m,
int crypto_stream_chacha20_ietf_ref_xor_ic(unsigned char * c,
const unsigned char *m,
unsigned long long mlen,
const unsigned char *n, uint32_t ic,
const unsigned char *k);