mirror of
https://github.com/wolfSSL/wolfssl.git
synced 2025-07-30 02:37:28 +02:00
Cleanup code and align labels
This commit is contained in:
@ -24,7 +24,6 @@
|
||||
* and Daniel J. Bernstein
|
||||
*/
|
||||
|
||||
#ifndef WOLFSSL_ARMASM
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
@ -246,8 +245,9 @@ static void U32TO64(word32 v, byte* p)
|
||||
p[3] = (v >> 24) & 0xFF;
|
||||
}
|
||||
|
||||
static void poly1305_blocks(Poly1305* ctx, const unsigned char *m,
|
||||
size_t bytes)
|
||||
#if !defined(WOLFSSL_ARMASM) || !defined(__aarch64__)
|
||||
void poly1305_blocks(Poly1305* ctx, const unsigned char *m,
|
||||
size_t bytes)
|
||||
{
|
||||
#ifdef USE_INTEL_SPEEDUP
|
||||
/* AVX2 is handled in wc_Poly1305Update. */
|
||||
@ -370,7 +370,7 @@ static void poly1305_blocks(Poly1305* ctx, const unsigned char *m,
|
||||
#endif /* end of 64 bit cpu blocks or 32 bit cpu */
|
||||
}
|
||||
|
||||
static void poly1305_block(Poly1305* ctx, const unsigned char *m)
|
||||
void poly1305_block(Poly1305* ctx, const unsigned char *m)
|
||||
{
|
||||
#ifdef USE_INTEL_SPEEDUP
|
||||
/* No call to poly1305_block when AVX2, AVX2 does 4 blocks at a time. */
|
||||
@ -379,8 +379,9 @@ static void poly1305_block(Poly1305* ctx, const unsigned char *m)
|
||||
poly1305_blocks(ctx, m, POLY1305_BLOCK_SIZE);
|
||||
#endif
|
||||
}
|
||||
#endif /* !defined(WOLFSSL_ARMASM) || !defined(__aarch64__) */
|
||||
|
||||
|
||||
#if !defined(WOLFSSL_ARMASM) || !defined(__aarch64__)
|
||||
int wc_Poly1305SetKey(Poly1305* ctx, const byte* key, word32 keySz)
|
||||
{
|
||||
#if defined(POLY130564)
|
||||
@ -467,7 +468,6 @@ int wc_Poly1305SetKey(Poly1305* ctx, const byte* key, word32 keySz)
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int wc_Poly1305Final(Poly1305* ctx, byte* mac)
|
||||
{
|
||||
#ifdef USE_INTEL_SPEEDUP
|
||||
@ -648,6 +648,7 @@ int wc_Poly1305Final(Poly1305* ctx, byte* mac)
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif /* !defined(WOLFSSL_ARMASM) || !defined(__aarch64__) */
|
||||
|
||||
|
||||
int wc_Poly1305Update(Poly1305* ctx, const byte* m, word32 bytes)
|
||||
@ -820,4 +821,3 @@ int wc_Poly1305_MAC(Poly1305* ctx, byte* additional, word32 addSz,
|
||||
|
||||
}
|
||||
#endif /* HAVE_POLY1305 */
|
||||
#endif /* WOLFSSL_ARMASM */
|
||||
|
@ -25,7 +25,7 @@
|
||||
*/
|
||||
|
||||
|
||||
#ifdef WOLFSSL_ARMASM
|
||||
#if defined(WOLFSSL_ARMASM) && defined(__aarch64__)
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
@ -48,110 +48,9 @@
|
||||
#include <stdio.h>
|
||||
#endif
|
||||
|
||||
#ifdef _MSC_VER
|
||||
/* 4127 warning constant while(1) */
|
||||
#pragma warning(disable: 4127)
|
||||
#endif
|
||||
|
||||
#if defined(POLY130564)
|
||||
#if defined(_MSC_VER)
|
||||
#define POLY1305_NOINLINE __declspec(noinline)
|
||||
#elif defined(__GNUC__)
|
||||
#define POLY1305_NOINLINE __attribute__((noinline))
|
||||
#else
|
||||
#define POLY1305_NOINLINE
|
||||
#endif
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#include <intrin.h>
|
||||
|
||||
typedef struct word128 {
|
||||
word64 lo;
|
||||
word64 hi;
|
||||
} word128;
|
||||
|
||||
#define MUL(out, x, y) out.lo = _umul128((x), (y), &out.hi)
|
||||
#define ADD(out, in) { word64 t = out.lo; out.lo += in.lo; \
|
||||
out.hi += (out.lo < t) + in.hi; }
|
||||
#define ADDLO(out, in) { word64 t = out.lo; out.lo += in; \
|
||||
out.hi += (out.lo < t); }
|
||||
#define SHR(in, shift) (__shiftright128(in.lo, in.hi, (shift)))
|
||||
#define LO(in) (in.lo)
|
||||
|
||||
#elif defined(__GNUC__)
|
||||
#if defined(__SIZEOF_INT128__)
|
||||
typedef unsigned __int128 word128;
|
||||
#else
|
||||
typedef unsigned word128 __attribute__((mode(TI)));
|
||||
#endif
|
||||
|
||||
#define MUL(out, x, y) out = ((word128)x * y)
|
||||
#define ADD(out, in) out += in
|
||||
#define ADDLO(out, in) out += in
|
||||
#define SHR(in, shift) (word64)(in >> (shift))
|
||||
#define LO(in) (word64)(in)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
//#if !defined(POLY130564)
|
||||
//
|
||||
// static word64 U8TO64(const byte* p)
|
||||
// {
|
||||
// return
|
||||
// (((word64)(p[0] & 0xff) ) |
|
||||
// ((word64)(p[1] & 0xff) << 8) |
|
||||
// ((word64)(p[2] & 0xff) << 16) |
|
||||
// ((word64)(p[3] & 0xff) << 24) |
|
||||
// ((word64)(p[4] & 0xff) << 32) |
|
||||
// ((word64)(p[5] & 0xff) << 40) |
|
||||
// ((word64)(p[6] & 0xff) << 48) |
|
||||
// ((word64)(p[7] & 0xff) << 56));
|
||||
// }
|
||||
//
|
||||
// static void U64TO8(byte* p, word64 v) {
|
||||
// p[0] = (v ) & 0xff;
|
||||
// p[1] = (v >> 8) & 0xff;
|
||||
// p[2] = (v >> 16) & 0xff;
|
||||
// p[3] = (v >> 24) & 0xff;
|
||||
// p[4] = (v >> 32) & 0xff;
|
||||
// p[5] = (v >> 40) & 0xff;
|
||||
// p[6] = (v >> 48) & 0xff;
|
||||
// p[7] = (v >> 56) & 0xff;
|
||||
// }
|
||||
//
|
||||
//#else /* if not 64 bit then use 32 bit */
|
||||
|
||||
static word32 U8TO32(const byte *p)
|
||||
{
|
||||
return
|
||||
(((word32)(p[0] & 0xff) ) |
|
||||
((word32)(p[1] & 0xff) << 8) |
|
||||
((word32)(p[2] & 0xff) << 16) |
|
||||
((word32)(p[3] & 0xff) << 24));
|
||||
}
|
||||
|
||||
static void U32TO8(byte *p, word32 v) {
|
||||
p[0] = (v ) & 0xff;
|
||||
p[1] = (v >> 8) & 0xff;
|
||||
p[2] = (v >> 16) & 0xff;
|
||||
p[3] = (v >> 24) & 0xff;
|
||||
}
|
||||
//#endif
|
||||
|
||||
|
||||
static void U32TO64(word32 v, byte* p)
|
||||
{
|
||||
XMEMSET(p, 0, 8);
|
||||
p[0] = (v & 0xFF);
|
||||
p[1] = (v >> 8) & 0xFF;
|
||||
p[2] = (v >> 16) & 0xFF;
|
||||
p[3] = (v >> 24) & 0xFF;
|
||||
}
|
||||
|
||||
static WC_INLINE void poly1305_blocks_16(Poly1305* ctx, const unsigned char *m,
|
||||
size_t bytes)
|
||||
{
|
||||
#if defined(POLY130564)
|
||||
__asm__ __volatile__ (
|
||||
"CMP %[bytes], %[POLY1305_BLOCK_SIZE] \n\t"
|
||||
"BLO L_poly1305_16_64_done_%= \n\t"
|
||||
@ -182,6 +81,7 @@ static WC_INLINE void poly1305_blocks_16(Poly1305* ctx, const unsigned char *m,
|
||||
"MOV x14, #0x3ffffff \n\t"
|
||||
"MUL w10, w25, w15 \n\t"
|
||||
"\n"
|
||||
".align 2 \n\t"
|
||||
"L_poly1305_16_64_loop_%=: \n\t"
|
||||
/* t0 = U8TO64(&m[0]); */
|
||||
/* t1 = U8TO64(&m[8]); */
|
||||
@ -268,6 +168,7 @@ static WC_INLINE void poly1305_blocks_16(Poly1305* ctx, const unsigned char *m,
|
||||
"STP x2, x4, %[ctx_h] \n\t"
|
||||
"STR w6, %[ctx_h_4] \n\t"
|
||||
"\n"
|
||||
".align 2 \n\t"
|
||||
"L_poly1305_16_64_done_%=: \n\t"
|
||||
: [ctx_h] "+m" (ctx->h[0]),
|
||||
[ctx_h_4] "+m" (ctx->h[4]),
|
||||
@ -283,79 +184,11 @@ static WC_INLINE void poly1305_blocks_16(Poly1305* ctx, const unsigned char *m,
|
||||
"x7", "x8", "x9", "x10", "x14", "x15", "x16", "x17", "x18", "x19",
|
||||
"x20", "x21", "x22", "x23", "x24", "x25"
|
||||
);
|
||||
|
||||
#else /* if not 64 bit then use 32 bit */
|
||||
const word32 hibit = (ctx->finished) ? 0 : ((word32)1 << 24); /* 1 << 128 */
|
||||
word32 r0,r1,r2,r3,r4;
|
||||
word32 s1,s2,s3,s4;
|
||||
word32 h0,h1,h2,h3,h4;
|
||||
word64 d0,d1,d2,d3,d4;
|
||||
word32 c;
|
||||
|
||||
|
||||
r0 = ctx->r[0];
|
||||
r1 = ctx->r[1];
|
||||
r2 = ctx->r[2];
|
||||
r3 = ctx->r[3];
|
||||
r4 = ctx->r[4];
|
||||
|
||||
s1 = r1 * 5;
|
||||
s2 = r2 * 5;
|
||||
s3 = r3 * 5;
|
||||
s4 = r4 * 5;
|
||||
|
||||
h0 = ctx->h[0];
|
||||
h1 = ctx->h[1];
|
||||
h2 = ctx->h[2];
|
||||
h3 = ctx->h[3];
|
||||
h4 = ctx->h[4];
|
||||
|
||||
while (bytes >= POLY1305_BLOCK_SIZE) {
|
||||
/* h += m[i] */
|
||||
h0 += (U8TO32(m+ 0) ) & 0x3ffffff;
|
||||
h1 += (U8TO32(m+ 3) >> 2) & 0x3ffffff;
|
||||
h2 += (U8TO32(m+ 6) >> 4) & 0x3ffffff;
|
||||
h3 += (U8TO32(m+ 9) >> 6) & 0x3ffffff;
|
||||
h4 += (U8TO32(m+12) >> 8) | hibit;
|
||||
|
||||
/* h *= r */
|
||||
d0 = ((word64)h0 * r0) + ((word64)h1 * s4) + ((word64)h2 * s3) +
|
||||
((word64)h3 * s2) + ((word64)h4 * s1);
|
||||
d1 = ((word64)h0 * r1) + ((word64)h1 * r0) + ((word64)h2 * s4) +
|
||||
((word64)h3 * s3) + ((word64)h4 * s2);
|
||||
d2 = ((word64)h0 * r2) + ((word64)h1 * r1) + ((word64)h2 * r0) +
|
||||
((word64)h3 * s4) + ((word64)h4 * s3);
|
||||
d3 = ((word64)h0 * r3) + ((word64)h1 * r2) + ((word64)h2 * r1) +
|
||||
((word64)h3 * r0) + ((word64)h4 * s4);
|
||||
d4 = ((word64)h0 * r4) + ((word64)h1 * r3) + ((word64)h2 * r2) +
|
||||
((word64)h3 * r1) + ((word64)h4 * r0);
|
||||
|
||||
/* (partial) h %= p */
|
||||
c = (word32)(d0 >> 26); h0 = (word32)d0 & 0x3ffffff;
|
||||
d1 += c; c = (word32)(d1 >> 26); h1 = (word32)d1 & 0x3ffffff;
|
||||
d2 += c; c = (word32)(d2 >> 26); h2 = (word32)d2 & 0x3ffffff;
|
||||
d3 += c; c = (word32)(d3 >> 26); h3 = (word32)d3 & 0x3ffffff;
|
||||
d4 += c; c = (word32)(d4 >> 26); h4 = (word32)d4 & 0x3ffffff;
|
||||
h0 += c * 5; c = (h0 >> 26); h0 = h0 & 0x3ffffff;
|
||||
h1 += c;
|
||||
|
||||
m += POLY1305_BLOCK_SIZE;
|
||||
bytes -= POLY1305_BLOCK_SIZE;
|
||||
}
|
||||
|
||||
ctx->h[0] = h0;
|
||||
ctx->h[1] = h1;
|
||||
ctx->h[2] = h2;
|
||||
ctx->h[3] = h3;
|
||||
ctx->h[4] = h4;
|
||||
|
||||
#endif /* end of 64 bit cpu blocks or 32 bit cpu */
|
||||
}
|
||||
|
||||
static void poly1305_blocks(Poly1305* ctx, const unsigned char *m,
|
||||
void poly1305_blocks(Poly1305* ctx, const unsigned char *m,
|
||||
size_t bytes)
|
||||
{
|
||||
#if defined(POLY130564)
|
||||
__asm__ __volatile__ (
|
||||
/* If less than 4 blocks to process then use regular method */
|
||||
"CMP %[bytes], %[POLY1305_BLOCK_SIZE]*4 \n\t"
|
||||
@ -457,6 +290,7 @@ static void poly1305_blocks(Poly1305* ctx, const unsigned char *m,
|
||||
"ADD v18.2S, v18.2S, v13.2S \n\t"
|
||||
"ADD v19.2S, v19.2S, v14.2S \n\t"
|
||||
"\n"
|
||||
".align 2 \n\t"
|
||||
"L_poly1305_64_loop_128_%=: \n\t"
|
||||
/* d0 = h0*r0 + h1*s4 + h2*s3 + h3*s2 + h4*s1 */
|
||||
/* d1 = h0*r1 + h1*r0 + h2*s4 + h3*s3 + h4*s2 */
|
||||
@ -631,6 +465,7 @@ static void poly1305_blocks(Poly1305* ctx, const unsigned char *m,
|
||||
"MOV v19.S[1], v19.S[2] \n\t"
|
||||
"B L_poly1305_64_loop_128_%= \n\t"
|
||||
"\n"
|
||||
".align 2 \n\t"
|
||||
"L_poly1305_64_loop_128_final_%=: \n\t"
|
||||
/* Load m */
|
||||
/* Load two message blocks to NEON v10, v11, v12, v13, v14 */
|
||||
@ -711,6 +546,7 @@ static void poly1305_blocks(Poly1305* ctx, const unsigned char *m,
|
||||
/* Else go to one loop of L_poly1305_64_loop_64 */
|
||||
"B L_poly1305_64_loop_64_%= \n\t"
|
||||
"\n"
|
||||
".align 2 \n\t"
|
||||
"L_poly1305_64_start_block_size_64_%=: \n\t"
|
||||
/* Load r^2 to NEON v0, v1, v2, v3, v4 */
|
||||
"LD4R { v0.2S-v3.2S }, [%[r_2]], #16 \n\t"
|
||||
@ -762,6 +598,7 @@ static void poly1305_blocks(Poly1305* ctx, const unsigned char *m,
|
||||
"ADD v18.2S, v18.2S, v13.2S \n\t"
|
||||
"ADD v19.2S, v19.2S, v14.2S \n\t"
|
||||
"\n"
|
||||
".align 2 \n\t"
|
||||
"L_poly1305_64_loop_64_%=: \n\t"
|
||||
/* d0 = h0*r0 + h1*s4 + h2*s3 + h3*s2 + h4*s1 */
|
||||
/* d1 = h0*r1 + h1*r0 + h2*s4 + h3*s3 + h4*s2 */
|
||||
@ -893,6 +730,7 @@ static void poly1305_blocks(Poly1305* ctx, const unsigned char *m,
|
||||
"CMP %[bytes], %[POLY1305_BLOCK_SIZE]*2 \n\t"
|
||||
"BHS L_poly1305_64_loop_64_%= \n\t"
|
||||
"\n"
|
||||
".align 2 \n\t"
|
||||
"L_poly1305_64_last_mult_%=: \n\t"
|
||||
/* Load r */
|
||||
"LD4 { v0.S-v3.S }[1], [%[r]], #16 \n\t"
|
||||
@ -1029,6 +867,7 @@ static void poly1305_blocks(Poly1305* ctx, const unsigned char *m,
|
||||
"ST1 { v9.S }[0], [%[h]] \n\t"
|
||||
"SUB %[h], %[h], #16 \n\t"
|
||||
"\n"
|
||||
".align 2 \n\t"
|
||||
"L_poly1305_64_done_%=: \n\t"
|
||||
: [bytes] "+r" (bytes),
|
||||
[m] "+r" (m),
|
||||
@ -1049,20 +888,19 @@ static void poly1305_blocks(Poly1305* ctx, const unsigned char *m,
|
||||
"x20", "x21", "x22", "x23", "x24", "x25", "x26", "x27", "x28", "x29"
|
||||
);
|
||||
poly1305_blocks_16(ctx, m, bytes);
|
||||
#else
|
||||
poly1305_blocks_16(ctx, m, bytes);
|
||||
#endif /* POLY130564 */
|
||||
}
|
||||
|
||||
static void poly1305_block(Poly1305* ctx, const unsigned char *m)
|
||||
void poly1305_block(Poly1305* ctx, const unsigned char *m)
|
||||
{
|
||||
poly1305_blocks_16(ctx, m, POLY1305_BLOCK_SIZE);
|
||||
}
|
||||
|
||||
#if defined(POLY130564)
|
||||
static word64 clamp[] = {
|
||||
0x0ffffffc0fffffff,
|
||||
0x0ffffffc0ffffffc,
|
||||
};
|
||||
#endif /* POLY130564 */
|
||||
|
||||
|
||||
int wc_Poly1305SetKey(Poly1305* ctx, const byte* key, word32 keySz)
|
||||
@ -1084,7 +922,6 @@ int wc_Poly1305SetKey(Poly1305* ctx, const byte* key, word32 keySz)
|
||||
if (keySz != 32 || ctx == NULL)
|
||||
return BAD_FUNC_ARG;
|
||||
|
||||
#if defined(POLY130564)
|
||||
__asm__ __volatile__ (
|
||||
/* Load key material */
|
||||
"LDP x8, x9, [%[key]] \n\t"
|
||||
@ -1256,52 +1093,16 @@ int wc_Poly1305SetKey(Poly1305* ctx, const byte* key, word32 keySz)
|
||||
"x18", "x19", "x20", "x21", "x22", "x23", "x24", "x25", "x26", "x27"
|
||||
);
|
||||
|
||||
#else /* if not 64 bit then use 32 bit */
|
||||
|
||||
/* r &= 0xffffffc0ffffffc0ffffffc0fffffff */
|
||||
ctx->r[0] = (U8TO32(key + 0) ) & 0x3ffffff;
|
||||
ctx->r[1] = (U8TO32(key + 3) >> 2) & 0x3ffff03;
|
||||
ctx->r[2] = (U8TO32(key + 6) >> 4) & 0x3ffc0ff;
|
||||
ctx->r[3] = (U8TO32(key + 9) >> 6) & 0x3f03fff;
|
||||
ctx->r[4] = (U8TO32(key + 12) >> 8) & 0x00fffff;
|
||||
|
||||
/* h = 0 */
|
||||
ctx->h[0] = 0;
|
||||
ctx->h[1] = 0;
|
||||
ctx->h[2] = 0;
|
||||
ctx->h[3] = 0;
|
||||
ctx->h[4] = 0;
|
||||
|
||||
/* save pad for later */
|
||||
ctx->pad[0] = U8TO32(key + 16);
|
||||
ctx->pad[1] = U8TO32(key + 20);
|
||||
ctx->pad[2] = U8TO32(key + 24);
|
||||
ctx->pad[3] = U8TO32(key + 28);
|
||||
|
||||
ctx->leftover = 0;
|
||||
ctx->finished = 0;
|
||||
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int wc_Poly1305Final(Poly1305* ctx, byte* mac)
|
||||
{
|
||||
#if !defined(POLY130564)
|
||||
|
||||
word32 h0,h1,h2,h3,h4,c;
|
||||
word32 g0,g1,g2,g3,g4;
|
||||
word64 f;
|
||||
word32 mask;
|
||||
|
||||
#endif
|
||||
|
||||
if (ctx == NULL)
|
||||
return BAD_FUNC_ARG;
|
||||
|
||||
#if defined(POLY130564)
|
||||
/* process the remaining block */
|
||||
if (ctx->leftover) {
|
||||
size_t i = ctx->leftover;
|
||||
@ -1375,217 +1176,9 @@ int wc_Poly1305Final(Poly1305* ctx, byte* mac)
|
||||
ctx->pad[2] = 0;
|
||||
ctx->pad[3] = 0;
|
||||
|
||||
#else /* if not 64 bit then use 32 bit */
|
||||
|
||||
/* process the remaining block */
|
||||
if (ctx->leftover) {
|
||||
size_t i = ctx->leftover;
|
||||
ctx->buffer[i++] = 1;
|
||||
for (; i < POLY1305_BLOCK_SIZE; i++)
|
||||
ctx->buffer[i] = 0;
|
||||
ctx->finished = 1;
|
||||
poly1305_block(ctx, ctx->buffer);
|
||||
}
|
||||
|
||||
/* fully carry h */
|
||||
h0 = ctx->h[0];
|
||||
h1 = ctx->h[1];
|
||||
h2 = ctx->h[2];
|
||||
h3 = ctx->h[3];
|
||||
h4 = ctx->h[4];
|
||||
|
||||
c = h1 >> 26; h1 = h1 & 0x3ffffff;
|
||||
h2 += c; c = h2 >> 26; h2 = h2 & 0x3ffffff;
|
||||
h3 += c; c = h3 >> 26; h3 = h3 & 0x3ffffff;
|
||||
h4 += c; c = h4 >> 26; h4 = h4 & 0x3ffffff;
|
||||
h0 += c * 5; c = h0 >> 26; h0 = h0 & 0x3ffffff;
|
||||
h1 += c;
|
||||
|
||||
/* compute h + -p */
|
||||
g0 = h0 + 5; c = g0 >> 26; g0 &= 0x3ffffff;
|
||||
g1 = h1 + c; c = g1 >> 26; g1 &= 0x3ffffff;
|
||||
g2 = h2 + c; c = g2 >> 26; g2 &= 0x3ffffff;
|
||||
g3 = h3 + c; c = g3 >> 26; g3 &= 0x3ffffff;
|
||||
g4 = h4 + c - ((word32)1 << 26);
|
||||
|
||||
/* select h if h < p, or h + -p if h >= p */
|
||||
mask = ((word32)g4 >> ((sizeof(word32) * 8) - 1)) - 1;
|
||||
g0 &= mask;
|
||||
g1 &= mask;
|
||||
g2 &= mask;
|
||||
g3 &= mask;
|
||||
g4 &= mask;
|
||||
mask = ~mask;
|
||||
h0 = (h0 & mask) | g0;
|
||||
h1 = (h1 & mask) | g1;
|
||||
h2 = (h2 & mask) | g2;
|
||||
h3 = (h3 & mask) | g3;
|
||||
h4 = (h4 & mask) | g4;
|
||||
|
||||
/* h = h % (2^128) */
|
||||
h0 = ((h0 ) | (h1 << 26)) & 0xffffffff;
|
||||
h1 = ((h1 >> 6) | (h2 << 20)) & 0xffffffff;
|
||||
h2 = ((h2 >> 12) | (h3 << 14)) & 0xffffffff;
|
||||
h3 = ((h3 >> 18) | (h4 << 8)) & 0xffffffff;
|
||||
|
||||
/* mac = (h + pad) % (2^128) */
|
||||
f = (word64)h0 + ctx->pad[0] ; h0 = (word32)f;
|
||||
f = (word64)h1 + ctx->pad[1] + (f >> 32); h1 = (word32)f;
|
||||
f = (word64)h2 + ctx->pad[2] + (f >> 32); h2 = (word32)f;
|
||||
f = (word64)h3 + ctx->pad[3] + (f >> 32); h3 = (word32)f;
|
||||
|
||||
U32TO8(mac + 0, h0);
|
||||
U32TO8(mac + 4, h1);
|
||||
U32TO8(mac + 8, h2);
|
||||
U32TO8(mac + 12, h3);
|
||||
|
||||
/* zero out the state */
|
||||
ctx->h[0] = 0;
|
||||
ctx->h[1] = 0;
|
||||
ctx->h[2] = 0;
|
||||
ctx->h[3] = 0;
|
||||
ctx->h[4] = 0;
|
||||
ctx->r[0] = 0;
|
||||
ctx->r[1] = 0;
|
||||
ctx->r[2] = 0;
|
||||
ctx->r[3] = 0;
|
||||
ctx->r[4] = 0;
|
||||
ctx->pad[0] = 0;
|
||||
ctx->pad[1] = 0;
|
||||
ctx->pad[2] = 0;
|
||||
ctx->pad[3] = 0;
|
||||
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int wc_Poly1305Update(Poly1305* ctx, const byte* m, word32 bytes)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
#ifdef CHACHA_AEAD_TEST
|
||||
word32 k;
|
||||
printf("Raw input to poly:\n");
|
||||
for (k = 0; k < bytes; k++) {
|
||||
printf("%02x", m[k]);
|
||||
if ((k+1) % 16 == 0)
|
||||
printf("\n");
|
||||
}
|
||||
printf("\n");
|
||||
#endif
|
||||
|
||||
if (ctx == NULL)
|
||||
return BAD_FUNC_ARG;
|
||||
|
||||
{
|
||||
/* handle leftover */
|
||||
if (ctx->leftover) {
|
||||
size_t want = (POLY1305_BLOCK_SIZE - ctx->leftover);
|
||||
if (want > bytes)
|
||||
want = bytes;
|
||||
for (i = 0; i < want; i++)
|
||||
ctx->buffer[ctx->leftover + i] = m[i];
|
||||
bytes -= (word32)want;
|
||||
m += want;
|
||||
ctx->leftover += want;
|
||||
if (ctx->leftover < POLY1305_BLOCK_SIZE)
|
||||
return 0;
|
||||
poly1305_block(ctx, ctx->buffer);
|
||||
ctx->leftover = 0;
|
||||
}
|
||||
|
||||
/* process full blocks */
|
||||
if (bytes >= POLY1305_BLOCK_SIZE) {
|
||||
size_t want = (bytes & ~(POLY1305_BLOCK_SIZE - 1));
|
||||
poly1305_blocks(ctx, m, want);
|
||||
m += want;
|
||||
bytes -= (word32)want;
|
||||
}
|
||||
|
||||
/* store leftover */
|
||||
if (bytes) {
|
||||
for (i = 0; i < bytes; i++)
|
||||
ctx->buffer[ctx->leftover + i] = m[i];
|
||||
ctx->leftover += bytes;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/* Takes in an initialized Poly1305 struct that has a key loaded and creates
|
||||
a MAC (tag) using recent TLS AEAD padding scheme.
|
||||
ctx : Initialized Poly1305 struct to use
|
||||
additional : Additional data to use
|
||||
addSz : Size of additional buffer
|
||||
input : Input buffer to create tag from
|
||||
sz : Size of input buffer
|
||||
tag : Buffer to hold created tag
|
||||
tagSz : Size of input tag buffer (must be at least
|
||||
WC_POLY1305_MAC_SZ(16))
|
||||
*/
|
||||
int wc_Poly1305_MAC(Poly1305* ctx, byte* additional, word32 addSz,
|
||||
byte* input, word32 sz, byte* tag, word32 tagSz)
|
||||
{
|
||||
int ret;
|
||||
byte padding[WC_POLY1305_PAD_SZ - 1];
|
||||
word32 paddingLen;
|
||||
byte little64[16];
|
||||
|
||||
XMEMSET(padding, 0, sizeof(padding));
|
||||
|
||||
/* sanity check on arguments */
|
||||
if (ctx == NULL || input == NULL || tag == NULL ||
|
||||
tagSz < WC_POLY1305_MAC_SZ) {
|
||||
return BAD_FUNC_ARG;
|
||||
}
|
||||
|
||||
/* additional allowed to be 0 */
|
||||
if (addSz > 0) {
|
||||
if (additional == NULL)
|
||||
return BAD_FUNC_ARG;
|
||||
|
||||
/* additional data plus padding */
|
||||
if ((ret = wc_Poly1305Update(ctx, additional, addSz)) != 0) {
|
||||
return ret;
|
||||
}
|
||||
paddingLen = -((int)addSz) & (WC_POLY1305_PAD_SZ - 1);
|
||||
if (paddingLen) {
|
||||
if ((ret = wc_Poly1305Update(ctx, padding, paddingLen)) != 0) {
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* input plus padding */
|
||||
if ((ret = wc_Poly1305Update(ctx, input, sz)) != 0) {
|
||||
return ret;
|
||||
}
|
||||
paddingLen = -((int)sz) & (WC_POLY1305_PAD_SZ - 1);
|
||||
if (paddingLen) {
|
||||
if ((ret = wc_Poly1305Update(ctx, padding, paddingLen)) != 0) {
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
/* size of additional data and input as little endian 64 bit types */
|
||||
U32TO64(addSz, little64);
|
||||
U32TO64(sz, little64 + 8);
|
||||
ret = wc_Poly1305Update(ctx, little64, sizeof(little64));
|
||||
if (ret)
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Finalize the auth tag */
|
||||
ret = wc_Poly1305Final(ctx, tag);
|
||||
|
||||
return ret;
|
||||
|
||||
}
|
||||
#endif /* HAVE_POLY1305 */
|
||||
#endif /* WOLFSSL_ARMASM */
|
||||
|
||||
|
@ -4322,9 +4322,6 @@ int chacha_test(void)
|
||||
ChaCha dec;
|
||||
byte cipher[128];
|
||||
byte plain[128];
|
||||
byte cipher_big[1305] = {0};
|
||||
byte plain_big[1305] = {0};
|
||||
byte input_big[1305] = {0};
|
||||
byte sliver[64];
|
||||
byte input[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
|
||||
word32 keySz = 32;
|
||||
|
@ -118,6 +118,10 @@ WOLFSSL_API int wc_Poly1305Update(Poly1305* poly1305, const byte*, word32);
|
||||
WOLFSSL_API int wc_Poly1305Final(Poly1305* poly1305, byte* tag);
|
||||
WOLFSSL_API int wc_Poly1305_MAC(Poly1305* ctx, byte* additional, word32 addSz,
|
||||
byte* input, word32 sz, byte* tag, word32 tagSz);
|
||||
|
||||
void poly1305_block(Poly1305* ctx, const unsigned char *m);
|
||||
void poly1305_blocks(Poly1305* ctx, const unsigned char *m,
|
||||
size_t bytes);
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user