Coverage Report

Created: 2026-06-08 06:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/poly1305/poly1305.c
Line
Count
Source
1
/*
2
 * Copyright 2015-2025 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include <stdint.h>
11
#include <stdlib.h>
12
#include <string.h>
13
#include <openssl/crypto.h>
14
15
#include "crypto/poly1305.h"
16
17
size_t Poly1305_ctx_size(void)
18
0
{
19
0
    return sizeof(struct poly1305_context);
20
0
}
21
22
/* pick 32-bit unsigned integer in little endian order */
23
static unsigned int U8TOU32(const unsigned char *p)
24
0
{
25
0
    return (((unsigned int)(p[0] & 0xff)) | ((unsigned int)(p[1] & 0xff) << 8) | ((unsigned int)(p[2] & 0xff) << 16) | ((unsigned int)(p[3] & 0xff) << 24));
26
0
}
27
28
/*
29
 * Implementations can be classified by amount of significant bits in
30
 * words making up the multi-precision value, or in other words radix
31
 * or base of numerical representation, e.g. base 2^64, base 2^32,
32
 * base 2^26. Complementary characteristic is how wide is the result of
33
 * multiplication of pair of digits, e.g. it would take 128 bits to
34
 * accommodate multiplication result in base 2^64 case. These are used
35
 * interchangeably. To describe implementation that is. But interface
36
 * is designed to isolate this so that low-level primitives implemented
37
 * in assembly can be self-contained/self-coherent.
38
 */
39
#ifndef POLY1305_ASM
40
/*
41
 * Even though there is __int128 reference implementation targeting
42
 * 64-bit platforms provided below, it's not obvious that it's optimal
43
 * choice for every one of them. Depending on instruction set overall
44
 * amount of instructions can be comparable to one in __int64
45
 * implementation. Amount of multiplication instructions would be lower,
46
 * but not necessarily overall. And in out-of-order execution context,
47
 * it is the latter that can be crucial...
48
 *
49
 * On related note. Poly1305 author, D. J. Bernstein, discusses and
50
 * provides floating-point implementations of the algorithm in question.
51
 * It made a lot of sense by the time of introduction, because most
52
 * then-modern processors didn't have pipelined integer multiplier.
53
 * [Not to mention that some had non-constant timing for integer
54
 * multiplications.] Floating-point instructions on the other hand could
55
 * be issued every cycle, which allowed to achieve better performance.
56
 * Nowadays, with SIMD and/or out-or-order execution, shared or
57
 * even emulated FPU, it's more complicated, and floating-point
58
 * implementation is not necessarily optimal choice in every situation,
59
 * rather contrary...
60
 *
61
 *                                              <https://github.com/dot-asm>
62
 */
63
64
/*
65
 * poly1305_blocks processes a multiple of POLY1305_BLOCK_SIZE blocks
66
 * of |inp| no longer than |len|. Behaviour for |len| not divisible by
67
 * block size is unspecified in general case, even though in reference
68
 * implementation the trailing chunk is simply ignored. Per algorithm
69
 * specification, every input block, complete or last partial, is to be
70
 * padded with a bit past most significant byte. The latter kind is then
71
 * padded with zeros till block size. This last partial block padding
72
 * is caller(*)'s responsibility, and because of this the last partial
73
 * block is always processed with separate call with |len| set to
74
 * POLY1305_BLOCK_SIZE and |padbit| to 0. In all other cases |padbit|
75
 * should be set to 1 to perform implicit padding with 128th bit.
76
 * poly1305_blocks does not actually check for this constraint though,
77
 * it's caller(*)'s responsibility to comply.
78
 *
79
 * (*)  In the context "caller" is not application code, but higher
80
 *      level Poly1305_* from this very module, so that quirks are
81
 *      handled locally.
82
 */
83
static void
84
poly1305_blocks(void *ctx, const unsigned char *inp, size_t len, uint32_t padbit);
85
86
/*
87
 * Type-agnostic "rip-off" from constant_time.h
88
 */
89
0
#define CONSTANT_TIME_CARRY(a, b) ( \
90
0
    (a ^ ((a ^ b) | ((a - b) ^ b))) >> (sizeof(a) * 8 - 1))
91
92
#if defined(INT64_MAX) && defined(INT128_MAX)
93
94
typedef uint128_t u128;
95
96
typedef struct {
97
    uint64_t h[3];
98
    uint64_t r[2];
99
} poly1305_internal;
100
101
/* pick 32-bit unsigned integer in little endian order */
102
static uint64_t U8TOU64(const unsigned char *p)
103
{
104
    return (((uint64_t)(p[0] & 0xff)) | ((uint64_t)(p[1] & 0xff) << 8) | ((uint64_t)(p[2] & 0xff) << 16) | ((uint64_t)(p[3] & 0xff) << 24) | ((uint64_t)(p[4] & 0xff) << 32) | ((uint64_t)(p[5] & 0xff) << 40) | ((uint64_t)(p[6] & 0xff) << 48) | ((uint64_t)(p[7] & 0xff) << 56));
105
}
106
107
/* store a 32-bit unsigned integer in little endian */
108
static void U64TO8(unsigned char *p, uint64_t v)
109
{
110
    p[0] = (unsigned char)((v) & 0xff);
111
    p[1] = (unsigned char)((v >> 8) & 0xff);
112
    p[2] = (unsigned char)((v >> 16) & 0xff);
113
    p[3] = (unsigned char)((v >> 24) & 0xff);
114
    p[4] = (unsigned char)((v >> 32) & 0xff);
115
    p[5] = (unsigned char)((v >> 40) & 0xff);
116
    p[6] = (unsigned char)((v >> 48) & 0xff);
117
    p[7] = (unsigned char)((v >> 56) & 0xff);
118
}
119
120
static void poly1305_init(void *ctx, const unsigned char key[16])
121
{
122
    poly1305_internal *st = (poly1305_internal *)ctx;
123
124
    /* h = 0 */
125
    st->h[0] = 0;
126
    st->h[1] = 0;
127
    st->h[2] = 0;
128
129
    /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */
130
    st->r[0] = U8TOU64(&key[0]) & 0x0ffffffc0fffffff;
131
    st->r[1] = U8TOU64(&key[8]) & 0x0ffffffc0ffffffc;
132
}
133
134
static void
135
poly1305_blocks(void *ctx, const unsigned char *inp, size_t len, uint32_t padbit)
136
{
137
    poly1305_internal *st = (poly1305_internal *)ctx;
138
    uint64_t r0, r1;
139
    uint64_t s1;
140
    uint64_t h0, h1, h2, c;
141
    u128 d0, d1;
142
143
    r0 = st->r[0];
144
    r1 = st->r[1];
145
146
    s1 = r1 + (r1 >> 2);
147
148
    h0 = st->h[0];
149
    h1 = st->h[1];
150
    h2 = st->h[2];
151
152
    while (len >= POLY1305_BLOCK_SIZE) {
153
        /* h += m[i] */
154
        h0 = (uint64_t)(d0 = (u128)h0 + U8TOU64(inp + 0));
155
        h1 = (uint64_t)(d1 = (u128)h1 + (d0 >> 64) + U8TOU64(inp + 8));
156
        /*
157
         * padbit can be zero only when original len was
158
         * POLY1305_BLOCK_SIZE, but we don't check
159
         */
160
        h2 += (uint64_t)(d1 >> 64) + padbit;
161
162
        /* h *= r "%" p, where "%" stands for "partial remainder" */
163
        d0 = ((u128)h0 * r0) + ((u128)h1 * s1);
164
        d1 = ((u128)h0 * r1) + ((u128)h1 * r0) + (h2 * s1);
165
        h2 = (h2 * r0);
166
167
        /* last reduction step: */
168
        /* a) h2:h0 = h2<<128 + d1<<64 + d0 */
169
        h0 = (uint64_t)d0;
170
        h1 = (uint64_t)(d1 += d0 >> 64);
171
        h2 += (uint64_t)(d1 >> 64);
172
        /* b) (h2:h0 += (h2:h0>>130) * 5) %= 2^130 */
173
        c = (h2 >> 2) + (h2 & ~3UL);
174
        h2 &= 3;
175
        h0 += c;
176
        h1 += (c = CONSTANT_TIME_CARRY(h0, c));
177
        h2 += CONSTANT_TIME_CARRY(h1, c);
178
        /*
179
         * Occasional overflows to 3rd bit of h2 are taken care of
180
         * "naturally". If after this point we end up at the top of
181
         * this loop, then the overflow bit will be accounted for
182
         * in next iteration. If we end up in poly1305_emit, then
183
         * comparison to modulus below will still count as "carry
184
         * into 131st bit", so that properly reduced value will be
185
         * picked in conditional move.
186
         */
187
188
        inp += POLY1305_BLOCK_SIZE;
189
        len -= POLY1305_BLOCK_SIZE;
190
    }
191
192
    st->h[0] = h0;
193
    st->h[1] = h1;
194
    st->h[2] = h2;
195
}
196
197
static void poly1305_emit(void *ctx, unsigned char mac[16],
198
    const uint32_t nonce[4])
199
{
200
    poly1305_internal *st = (poly1305_internal *)ctx;
201
    uint64_t h0, h1, h2;
202
    uint64_t g0, g1, g2;
203
    u128 t;
204
    uint64_t mask;
205
206
    h0 = st->h[0];
207
    h1 = st->h[1];
208
    h2 = st->h[2];
209
210
    /* compare to modulus by computing h + -p */
211
    g0 = (uint64_t)(t = (u128)h0 + 5);
212
    g1 = (uint64_t)(t = (u128)h1 + (t >> 64));
213
    g2 = h2 + (uint64_t)(t >> 64);
214
215
    /* if there was carry into 131st bit, h1:h0 = g1:g0 */
216
    mask = 0 - (g2 >> 2);
217
    g0 &= mask;
218
    g1 &= mask;
219
    mask = ~mask;
220
    h0 = (h0 & mask) | g0;
221
    h1 = (h1 & mask) | g1;
222
223
    /* mac = (h + nonce) % (2^128) */
224
    h0 = (uint64_t)(t = (u128)h0 + nonce[0] + ((uint64_t)nonce[1] << 32));
225
    h1 = (uint64_t)(t = (u128)h1 + nonce[2] + ((uint64_t)nonce[3] << 32) + (t >> 64));
226
227
    U64TO8(mac + 0, h0);
228
    U64TO8(mac + 8, h1);
229
}
230
231
#else
232
233
typedef struct {
234
    uint32_t h[5];
235
    uint32_t r[4];
236
} poly1305_internal;
237
238
/* store a 32-bit unsigned integer in little endian */
239
static void U32TO8(unsigned char *p, unsigned int v)
240
0
{
241
0
    p[0] = (unsigned char)((v) & 0xff);
242
0
    p[1] = (unsigned char)((v >> 8) & 0xff);
243
0
    p[2] = (unsigned char)((v >> 16) & 0xff);
244
0
    p[3] = (unsigned char)((v >> 24) & 0xff);
245
0
}
246
247
static void poly1305_init(void *ctx, const unsigned char key[16])
248
0
{
249
0
    poly1305_internal *st = (poly1305_internal *)ctx;
250
251
    /* h = 0 */
252
0
    st->h[0] = 0;
253
0
    st->h[1] = 0;
254
0
    st->h[2] = 0;
255
0
    st->h[3] = 0;
256
0
    st->h[4] = 0;
257
258
    /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */
259
0
    st->r[0] = U8TOU32(&key[0]) & 0x0fffffff;
260
0
    st->r[1] = U8TOU32(&key[4]) & 0x0ffffffc;
261
0
    st->r[2] = U8TOU32(&key[8]) & 0x0ffffffc;
262
0
    st->r[3] = U8TOU32(&key[12]) & 0x0ffffffc;
263
0
}
264
265
static void
266
poly1305_blocks(void *ctx, const unsigned char *inp, size_t len, uint32_t padbit)
267
0
{
268
0
    poly1305_internal *st = (poly1305_internal *)ctx;
269
0
    uint32_t r0, r1, r2, r3;
270
0
    uint32_t s1, s2, s3;
271
0
    uint32_t h0, h1, h2, h3, h4, c;
272
0
    uint64_t d0, d1, d2, d3;
273
274
0
    r0 = st->r[0];
275
0
    r1 = st->r[1];
276
0
    r2 = st->r[2];
277
0
    r3 = st->r[3];
278
279
0
    s1 = r1 + (r1 >> 2);
280
0
    s2 = r2 + (r2 >> 2);
281
0
    s3 = r3 + (r3 >> 2);
282
283
0
    h0 = st->h[0];
284
0
    h1 = st->h[1];
285
0
    h2 = st->h[2];
286
0
    h3 = st->h[3];
287
0
    h4 = st->h[4];
288
289
0
    while (len >= POLY1305_BLOCK_SIZE) {
290
        /* h += m[i] */
291
0
        h0 = (uint32_t)(d0 = (uint64_t)h0 + U8TOU32(inp + 0));
292
0
        h1 = (uint32_t)(d1 = (uint64_t)h1 + (d0 >> 32) + U8TOU32(inp + 4));
293
0
        h2 = (uint32_t)(d2 = (uint64_t)h2 + (d1 >> 32) + U8TOU32(inp + 8));
294
0
        h3 = (uint32_t)(d3 = (uint64_t)h3 + (d2 >> 32) + U8TOU32(inp + 12));
295
0
        h4 += (uint32_t)(d3 >> 32) + padbit;
296
297
        /* h *= r "%" p, where "%" stands for "partial remainder" */
298
0
        d0 = ((uint64_t)h0 * r0) + ((uint64_t)h1 * s3) + ((uint64_t)h2 * s2) + ((uint64_t)h3 * s1);
299
0
        d1 = ((uint64_t)h0 * r1) + ((uint64_t)h1 * r0) + ((uint64_t)h2 * s3) + ((uint64_t)h3 * s2) + (h4 * s1);
300
0
        d2 = ((uint64_t)h0 * r2) + ((uint64_t)h1 * r1) + ((uint64_t)h2 * r0) + ((uint64_t)h3 * s3) + (h4 * s2);
301
0
        d3 = ((uint64_t)h0 * r3) + ((uint64_t)h1 * r2) + ((uint64_t)h2 * r1) + ((uint64_t)h3 * r0) + (h4 * s3);
302
0
        h4 = (h4 * r0);
303
304
        /* last reduction step: */
305
        /* a) h4:h0 = h4<<128 + d3<<96 + d2<<64 + d1<<32 + d0 */
306
0
        h0 = (uint32_t)d0;
307
0
        h1 = (uint32_t)(d1 += d0 >> 32);
308
0
        h2 = (uint32_t)(d2 += d1 >> 32);
309
0
        h3 = (uint32_t)(d3 += d2 >> 32);
310
0
        h4 += (uint32_t)(d3 >> 32);
311
        /* b) (h4:h0 += (h4:h0>>130) * 5) %= 2^130 */
312
0
        c = (h4 >> 2) + (h4 & ~3U);
313
0
        h4 &= 3;
314
0
        h0 += c;
315
0
        h1 += (c = CONSTANT_TIME_CARRY(h0, c));
316
0
        h2 += (c = CONSTANT_TIME_CARRY(h1, c));
317
0
        h3 += (c = CONSTANT_TIME_CARRY(h2, c));
318
0
        h4 += CONSTANT_TIME_CARRY(h3, c);
319
        /*
320
         * Occasional overflows to 3rd bit of h4 are taken care of
321
         * "naturally". If after this point we end up at the top of
322
         * this loop, then the overflow bit will be accounted for
323
         * in next iteration. If we end up in poly1305_emit, then
324
         * comparison to modulus below will still count as "carry
325
         * into 131st bit", so that properly reduced value will be
326
         * picked in conditional move.
327
         */
328
329
0
        inp += POLY1305_BLOCK_SIZE;
330
0
        len -= POLY1305_BLOCK_SIZE;
331
0
    }
332
333
0
    st->h[0] = h0;
334
0
    st->h[1] = h1;
335
0
    st->h[2] = h2;
336
0
    st->h[3] = h3;
337
0
    st->h[4] = h4;
338
0
}
339
340
static void poly1305_emit(void *ctx, unsigned char mac[16],
341
    const uint32_t nonce[4])
342
0
{
343
0
    poly1305_internal *st = (poly1305_internal *)ctx;
344
0
    uint32_t h0, h1, h2, h3, h4;
345
0
    uint32_t g0, g1, g2, g3, g4;
346
0
    uint64_t t;
347
0
    uint32_t mask;
348
349
0
    h0 = st->h[0];
350
0
    h1 = st->h[1];
351
0
    h2 = st->h[2];
352
0
    h3 = st->h[3];
353
0
    h4 = st->h[4];
354
355
    /* compare to modulus by computing h + -p */
356
0
    g0 = (uint32_t)(t = (uint64_t)h0 + 5);
357
0
    g1 = (uint32_t)(t = (uint64_t)h1 + (t >> 32));
358
0
    g2 = (uint32_t)(t = (uint64_t)h2 + (t >> 32));
359
0
    g3 = (uint32_t)(t = (uint64_t)h3 + (t >> 32));
360
0
    g4 = h4 + (uint32_t)(t >> 32);
361
362
    /* if there was carry into 131st bit, h3:h0 = g3:g0 */
363
0
    mask = 0 - (g4 >> 2);
364
0
    g0 &= mask;
365
0
    g1 &= mask;
366
0
    g2 &= mask;
367
0
    g3 &= mask;
368
0
    mask = ~mask;
369
0
    h0 = (h0 & mask) | g0;
370
0
    h1 = (h1 & mask) | g1;
371
0
    h2 = (h2 & mask) | g2;
372
0
    h3 = (h3 & mask) | g3;
373
374
    /* mac = (h + nonce) % (2^128) */
375
0
    h0 = (uint32_t)(t = (uint64_t)h0 + nonce[0]);
376
0
    h1 = (uint32_t)(t = (uint64_t)h1 + (t >> 32) + nonce[1]);
377
0
    h2 = (uint32_t)(t = (uint64_t)h2 + (t >> 32) + nonce[2]);
378
0
    h3 = (uint32_t)(t = (uint64_t)h3 + (t >> 32) + nonce[3]);
379
380
0
    U32TO8(mac + 0, h0);
381
0
    U32TO8(mac + 4, h1);
382
0
    U32TO8(mac + 8, h2);
383
0
    U32TO8(mac + 12, h3);
384
0
}
385
#endif
386
#else
387
int poly1305_init(void *ctx, const unsigned char key[16], void *func);
388
void poly1305_blocks(void *ctx, const unsigned char *inp, size_t len,
389
    unsigned int padbit);
390
void poly1305_emit(void *ctx, unsigned char mac[16],
391
    const unsigned int nonce[4]);
392
#endif
393
394
void Poly1305_Init(POLY1305 *ctx, const unsigned char key[32])
395
0
{
396
0
    ctx->nonce[0] = U8TOU32(&key[16]);
397
0
    ctx->nonce[1] = U8TOU32(&key[20]);
398
0
    ctx->nonce[2] = U8TOU32(&key[24]);
399
0
    ctx->nonce[3] = U8TOU32(&key[28]);
400
401
0
#ifndef POLY1305_ASM
402
0
    poly1305_init(ctx->opaque, key);
403
#else
404
    /*
405
     * Unlike reference poly1305_init assembly counterpart is expected
406
     * to return a value: non-zero if it initializes ctx->func, and zero
407
     * otherwise. Latter is to simplify assembly in cases when there no
408
     * multiple code paths to switch between.
409
     */
410
    if (!poly1305_init(ctx->opaque, key, &ctx->func)) {
411
        ctx->func.blocks = poly1305_blocks;
412
        ctx->func.emit = poly1305_emit;
413
    }
414
#endif
415
416
0
    ctx->num = 0;
417
0
}
418
419
#ifdef POLY1305_ASM
420
/*
421
 * This "eclipses" poly1305_blocks and poly1305_emit, but it's
422
 * conscious choice imposed by -Wshadow compiler warnings.
423
 */
424
#define poly1305_blocks (*poly1305_blocks_p)
425
#define poly1305_emit (*poly1305_emit_p)
426
#endif
427
428
void Poly1305_Update(POLY1305 *ctx, const unsigned char *inp, size_t len)
429
0
{
430
#ifdef POLY1305_ASM
431
    /*
432
     * As documented, poly1305_blocks is never called with input
433
     * longer than single block and padbit argument set to 0. This
434
     * property is fluently used in assembly modules to optimize
435
     * padbit handling on loop boundary.
436
     */
437
    poly1305_blocks_f poly1305_blocks_p = ctx->func.blocks;
438
#endif
439
0
    size_t rem, num;
440
441
0
    if ((num = ctx->num)) {
442
0
        rem = POLY1305_BLOCK_SIZE - num;
443
0
        if (len >= rem) {
444
0
            memcpy(ctx->data + num, inp, rem);
445
0
            poly1305_blocks(ctx->opaque, ctx->data, POLY1305_BLOCK_SIZE, 1);
446
0
            inp += rem;
447
0
            len -= rem;
448
0
        } else {
449
            /* Still not enough data to process a block. */
450
0
            memcpy(ctx->data + num, inp, len);
451
0
            ctx->num = num + len;
452
0
            return;
453
0
        }
454
0
    }
455
456
0
    rem = len % POLY1305_BLOCK_SIZE;
457
0
    len -= rem;
458
459
0
    if (len >= POLY1305_BLOCK_SIZE) {
460
0
        poly1305_blocks(ctx->opaque, inp, len, 1);
461
0
        inp += len;
462
0
    }
463
464
0
    if (rem)
465
0
        memcpy(ctx->data, inp, rem);
466
467
0
    ctx->num = rem;
468
0
}
469
470
void Poly1305_Final(POLY1305 *ctx, unsigned char mac[16])
471
0
{
472
#ifdef POLY1305_ASM
473
    poly1305_blocks_f poly1305_blocks_p = ctx->func.blocks;
474
    poly1305_emit_f poly1305_emit_p = ctx->func.emit;
475
#endif
476
0
    size_t num;
477
478
0
    if ((num = ctx->num)) {
479
0
        ctx->data[num++] = 1; /* pad bit */
480
0
        while (num < POLY1305_BLOCK_SIZE)
481
0
            ctx->data[num++] = 0;
482
0
        poly1305_blocks(ctx->opaque, ctx->data, POLY1305_BLOCK_SIZE, 0);
483
0
    }
484
485
0
    poly1305_emit(ctx->opaque, mac, ctx->nonce);
486
487
    /* zero out the state */
488
0
    OPENSSL_cleanse(ctx, sizeof(*ctx));
489
0
}