Coverage Report

Created: 2025-12-10 06:24

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