48#if !defined(MBEDTLS_CONFIG_FILE)
51#include MBEDTLS_CONFIG_FILE
54#if defined(MBEDTLS_POLY1305_C)
61#if defined(MBEDTLS_SELF_TEST)
62#if defined(MBEDTLS_PLATFORM_C)
66#define mbedtls_printf printf
70#if !defined(MBEDTLS_POLY1305_ALT)
72#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
73 !defined(inline) && !defined(__cplusplus)
74#define inline __inline
78#define POLY1305_VALIDATE_RET( cond ) \
79 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA )
80#define POLY1305_VALIDATE( cond ) \
81 MBEDTLS_INTERNAL_VALIDATE( cond )
83#define POLY1305_BLOCK_SIZE_BYTES ( 16U )
85#define BYTES_TO_U32_LE( data, offset ) \
86 ( (uint32_t) (data)[offset] \
87 | (uint32_t) ( (uint32_t) (data)[( offset ) + 1] << 8 ) \
88 | (uint32_t) ( (uint32_t) (data)[( offset ) + 2] << 16 ) \
89 | (uint32_t) ( (uint32_t) (data)[( offset ) + 3] << 24 ) \
96#if defined(MBEDTLS_NO_64BIT_MULTIPLICATION)
110 return( lo + ( me << 16 ) + ( (
uint64_t) hi << 32 ) );
133 const unsigned char *
input,
137 uint32_t acc0, acc1, acc2, acc3, acc4;
148 rs1 =
r1 + (
r1 >> 2U );
149 rs2 =
r2 + (
r2 >> 2U );
150 rs3 =
r3 + (
r3 >> 2U );
159 for(
i = 0
U;
i < nblocks;
i++ )
169 d1 += (
uint64_t) acc1 + ( d0 >> 32U );
170 d2 += (
uint64_t) acc2 + ( d1 >> 32U );
171 d3 += (
uint64_t) acc3 + ( d2 >> 32U );
176 acc4 += (
uint32_t) ( d3 >> 32U ) + needs_padding;
179 d0 = mul64( acc0, r0 ) +
183 d1 = mul64( acc0,
r1 ) +
188 d2 = mul64( acc0,
r2 ) +
193 d3 = mul64( acc0,
r3 ) +
208 acc4 = (
uint32_t) ( d3 >> 32 ) + acc4;
210 d0 = (
uint64_t) acc0 + ( acc4 >> 2 ) + ( acc4 & 0xFFFFFFFCU );
213 d0 = (
uint64_t) acc1 + ( d0 >> 32U );
215 d0 = (
uint64_t) acc2 + ( d0 >> 32U );
217 d0 = (
uint64_t) acc3 + ( d0 >> 32U );
219 d0 = (
uint64_t) acc4 + ( d0 >> 32U );
222 offset += POLY1305_BLOCK_SIZE_BYTES;
240 unsigned char mac[16] )
244 uint32_t acc0, acc1, acc2, acc3, acc4;
275 acc0 = ( acc0 & mask_inv ) | ( g0 &
mask );
276 acc1 = ( acc1 & mask_inv ) | ( g1 &
mask );
277 acc2 = ( acc2 & mask_inv ) | ( g2 &
mask );
278 acc3 = ( acc3 & mask_inv ) | ( g3 &
mask );
290 mac[ 0] = (
unsigned char)( acc0 );
291 mac[ 1] = (
unsigned char)( acc0 >> 8 );
292 mac[ 2] = (
unsigned char)( acc0 >> 16 );
293 mac[ 3] = (
unsigned char)( acc0 >> 24 );
294 mac[ 4] = (
unsigned char)( acc1 );
295 mac[ 5] = (
unsigned char)( acc1 >> 8 );
296 mac[ 6] = (
unsigned char)( acc1 >> 16 );
297 mac[ 7] = (
unsigned char)( acc1 >> 24 );
298 mac[ 8] = (
unsigned char)( acc2 );
299 mac[ 9] = (
unsigned char)( acc2 >> 8 );
300 mac[10] = (
unsigned char)( acc2 >> 16 );
301 mac[11] = (
unsigned char)( acc2 >> 24 );
302 mac[12] = (
unsigned char)( acc3 );
303 mac[13] = (
unsigned char)( acc3 >> 8 );
304 mac[14] = (
unsigned char)( acc3 >> 16 );
305 mac[15] = (
unsigned char)( acc3 >> 24 );
310 POLY1305_VALIDATE(
ctx !=
NULL );
324 const unsigned char key[32] )
326 POLY1305_VALIDATE_RET(
ctx !=
NULL );
327 POLY1305_VALIDATE_RET(
key !=
NULL );
330 ctx->r[0] = BYTES_TO_U32_LE(
key, 0 ) & 0x0FFFFFFFU;
331 ctx->r[1] = BYTES_TO_U32_LE(
key, 4 ) & 0x0FFFFFFCU;
332 ctx->r[2] = BYTES_TO_U32_LE(
key, 8 ) & 0x0FFFFFFCU;
333 ctx->r[3] = BYTES_TO_U32_LE(
key, 12 ) & 0x0FFFFFFCU;
335 ctx->s[0] = BYTES_TO_U32_LE(
key, 16 );
336 ctx->s[1] = BYTES_TO_U32_LE(
key, 20 );
337 ctx->s[2] = BYTES_TO_U32_LE(
key, 24 );
338 ctx->s[3] = BYTES_TO_U32_LE(
key, 28 );
355 const unsigned char *
input,
359 size_t remaining = ilen;
360 size_t queue_free_len;
362 POLY1305_VALIDATE_RET(
ctx !=
NULL );
363 POLY1305_VALIDATE_RET( ilen == 0 ||
input !=
NULL );
365 if( ( remaining > 0
U ) && (
ctx->queue_len > 0
U ) )
367 queue_free_len = ( POLY1305_BLOCK_SIZE_BYTES -
ctx->queue_len );
369 if( ilen < queue_free_len )
378 ctx->queue_len += ilen;
391 poly1305_process(
ctx, 1U,
ctx->queue, 1U );
394 remaining -= queue_free_len;
398 if( remaining >= POLY1305_BLOCK_SIZE_BYTES )
400 nblocks = remaining / POLY1305_BLOCK_SIZE_BYTES;
404 offset += nblocks * POLY1305_BLOCK_SIZE_BYTES;
405 remaining %= POLY1305_BLOCK_SIZE_BYTES;
411 ctx->queue_len = remaining;
419 unsigned char mac[16] )
421 POLY1305_VALIDATE_RET(
ctx !=
NULL );
422 POLY1305_VALIDATE_RET( mac !=
NULL );
425 if(
ctx->queue_len > 0
U )
428 ctx->queue[
ctx->queue_len] = 1U;
434 POLY1305_BLOCK_SIZE_BYTES -
ctx->queue_len );
436 poly1305_process(
ctx, 1U,
440 poly1305_compute_mac(
ctx, mac );
446 const unsigned char *
input,
448 unsigned char mac[16] )
452 POLY1305_VALIDATE_RET(
key !=
NULL );
453 POLY1305_VALIDATE_RET( mac !=
NULL );
454 POLY1305_VALIDATE_RET( ilen == 0 ||
input !=
NULL );
475#if defined(MBEDTLS_SELF_TEST)
477static const unsigned char test_keys[2][32] =
480 0x85, 0xd6, 0xbe, 0x78, 0x57, 0x55, 0x6d, 0x33,
481 0x7f, 0x44, 0x52, 0xfe, 0x42, 0xd5, 0x06, 0xa8,
482 0x01, 0x03, 0x80, 0x8a, 0xfb, 0x0d, 0xb2, 0xfd,
483 0x4a, 0xbf, 0xf6, 0xaf, 0x41, 0x49, 0xf5, 0x1b
486 0x1c, 0x92, 0x40, 0xa5, 0xeb, 0x55, 0xd3, 0x8a,
487 0xf3, 0x33, 0x88, 0x86, 0x04, 0xf6, 0xb5, 0xf0,
488 0x47, 0x39, 0x17, 0xc1, 0x40, 0x2b, 0x80, 0x09,
489 0x9d, 0xca, 0x5c, 0xbc, 0x20, 0x70, 0x75, 0xc0
493static const unsigned char test_data[2][127] =
496 0x43, 0x72, 0x79, 0x70, 0x74, 0x6f, 0x67, 0x72,
497 0x61, 0x70, 0x68, 0x69, 0x63, 0x20, 0x46, 0x6f,
498 0x72, 0x75, 0x6d, 0x20, 0x52, 0x65, 0x73, 0x65,
499 0x61, 0x72, 0x63, 0x68, 0x20, 0x47, 0x72, 0x6f,
503 0x27, 0x54, 0x77, 0x61, 0x73, 0x20, 0x62, 0x72,
504 0x69, 0x6c, 0x6c, 0x69, 0x67, 0x2c, 0x20, 0x61,
505 0x6e, 0x64, 0x20, 0x74, 0x68, 0x65, 0x20, 0x73,
506 0x6c, 0x69, 0x74, 0x68, 0x79, 0x20, 0x74, 0x6f,
507 0x76, 0x65, 0x73, 0x0a, 0x44, 0x69, 0x64, 0x20,
508 0x67, 0x79, 0x72, 0x65, 0x20, 0x61, 0x6e, 0x64,
509 0x20, 0x67, 0x69, 0x6d, 0x62, 0x6c, 0x65, 0x20,
510 0x69, 0x6e, 0x20, 0x74, 0x68, 0x65, 0x20, 0x77,
511 0x61, 0x62, 0x65, 0x3a, 0x0a, 0x41, 0x6c, 0x6c,
512 0x20, 0x6d, 0x69, 0x6d, 0x73, 0x79, 0x20, 0x77,
513 0x65, 0x72, 0x65, 0x20, 0x74, 0x68, 0x65, 0x20,
514 0x62, 0x6f, 0x72, 0x6f, 0x67, 0x6f, 0x76, 0x65,
515 0x73, 0x2c, 0x0a, 0x41, 0x6e, 0x64, 0x20, 0x74,
516 0x68, 0x65, 0x20, 0x6d, 0x6f, 0x6d, 0x65, 0x20,
517 0x72, 0x61, 0x74, 0x68, 0x73, 0x20, 0x6f, 0x75,
518 0x74, 0x67, 0x72, 0x61, 0x62, 0x65, 0x2e
522static const size_t test_data_len[2] =
528static const unsigned char test_mac[2][16] =
531 0xa8, 0x06, 0x1d, 0xc1, 0x30, 0x51, 0x36, 0xc6,
532 0xc2, 0x2b, 0x8b, 0xaf, 0x0c, 0x01, 0x27, 0xa9
535 0x45, 0x41, 0x66, 0x9a, 0x7e, 0xaa, 0xee, 0x61,
536 0xe7, 0x08, 0xdc, 0x7c, 0xbc, 0xc5, 0xeb, 0x62
540#define ASSERT( cond, args ) \
546 mbedtls_printf args; \
553int mbedtls_poly1305_self_test(
int verbose )
555 unsigned char mac[16];
559 for(
i = 0
U;
i < 2U;
i++ )
int memcmp(void *Buffer1, void *Buffer2, ACPI_SIZE Count)
unsigned short int uint16_t
static void cleanup(void)
GLboolean GLboolean GLboolean b
GLboolean GLboolean GLboolean GLboolean a
GLenum GLenum GLenum input
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
#define memcpy(s1, s2, n)
static void test_mac(void)
This file contains Poly1305 definitions and functions.
int mbedtls_poly1305_update(mbedtls_poly1305_context *ctx, const unsigned char *input, size_t ilen)
This functions feeds an input buffer into an ongoing Poly1305 computation.
int mbedtls_poly1305_mac(const unsigned char key[32], const unsigned char *input, size_t ilen, unsigned char mac[16])
This function calculates the Poly1305 MAC of the input buffer with the provided key.
int mbedtls_poly1305_finish(mbedtls_poly1305_context *ctx, unsigned char mac[16])
This function generates the Poly1305 Message Authentication Code (MAC).
void mbedtls_poly1305_init(mbedtls_poly1305_context *ctx)
This function initializes the specified Poly1305 context.
int mbedtls_poly1305_starts(mbedtls_poly1305_context *ctx, const unsigned char key[32])
This function sets the one-time authentication key.
void mbedtls_poly1305_free(mbedtls_poly1305_context *ctx)
This function releases and clears the specified Poly1305 context.
Configuration options (set of defines)