Coverage Report

Created: 2026-06-05 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/dropbear/libtomcrypt/src/mac/poly1305/poly1305.c
Line
Count
Source
1
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
2
 *
3
 * LibTomCrypt is a library that provides various cryptographic
4
 * algorithms in a highly modular and flexible manner.
5
 *
6
 * The library is free for all purposes without any express
7
 * guarantee it works.
8
 */
9
10
/* The implementation is based on:
11
 * Public Domain poly1305 from Andrew Moon
12
 * https://github.com/floodyberry/poly1305-donna
13
 */
14
15
#include "tomcrypt.h"
16
17
#ifdef LTC_POLY1305
18
19
/* internal only */
20
static void _poly1305_block(poly1305_state *st, const unsigned char *in, unsigned long inlen)
21
79.1k
{
22
79.1k
   const unsigned long hibit = (st->final) ? 0 : (1UL << 24); /* 1 << 128 */
23
79.1k
   ulong32 r0,r1,r2,r3,r4;
24
79.1k
   ulong32 s1,s2,s3,s4;
25
79.1k
   ulong32 h0,h1,h2,h3,h4;
26
79.1k
   ulong32 tmp;
27
79.1k
   ulong64 d0,d1,d2,d3,d4;
28
79.1k
   ulong32 c;
29
30
79.1k
   r0 = st->r[0];
31
79.1k
   r1 = st->r[1];
32
79.1k
   r2 = st->r[2];
33
79.1k
   r3 = st->r[3];
34
79.1k
   r4 = st->r[4];
35
36
79.1k
   s1 = r1 * 5;
37
79.1k
   s2 = r2 * 5;
38
79.1k
   s3 = r3 * 5;
39
79.1k
   s4 = r4 * 5;
40
41
79.1k
   h0 = st->h[0];
42
79.1k
   h1 = st->h[1];
43
79.1k
   h2 = st->h[2];
44
79.1k
   h3 = st->h[3];
45
79.1k
   h4 = st->h[4];
46
47
10.3M
   while (inlen >= 16) {
48
      /* h += in[i] */
49
10.2M
      LOAD32L(tmp, in+ 0); h0 += (tmp     ) & 0x3ffffff;
50
10.2M
      LOAD32L(tmp, in+ 3); h1 += (tmp >> 2) & 0x3ffffff;
51
10.2M
      LOAD32L(tmp, in+ 6); h2 += (tmp >> 4) & 0x3ffffff;
52
10.2M
      LOAD32L(tmp, in+ 9); h3 += (tmp >> 6) & 0x3ffffff;
53
10.2M
      LOAD32L(tmp, in+12); h4 += (tmp >> 8) | hibit;
54
55
      /* h *= r */
56
10.2M
      d0 = ((ulong64)h0 * r0) + ((ulong64)h1 * s4) + ((ulong64)h2 * s3) + ((ulong64)h3 * s2) + ((ulong64)h4 * s1);
57
10.2M
      d1 = ((ulong64)h0 * r1) + ((ulong64)h1 * r0) + ((ulong64)h2 * s4) + ((ulong64)h3 * s3) + ((ulong64)h4 * s2);
58
10.2M
      d2 = ((ulong64)h0 * r2) + ((ulong64)h1 * r1) + ((ulong64)h2 * r0) + ((ulong64)h3 * s4) + ((ulong64)h4 * s3);
59
10.2M
      d3 = ((ulong64)h0 * r3) + ((ulong64)h1 * r2) + ((ulong64)h2 * r1) + ((ulong64)h3 * r0) + ((ulong64)h4 * s4);
60
10.2M
      d4 = ((ulong64)h0 * r4) + ((ulong64)h1 * r3) + ((ulong64)h2 * r2) + ((ulong64)h3 * r1) + ((ulong64)h4 * r0);
61
62
      /* (partial) h %= p */
63
10.2M
                    c = (ulong32)(d0 >> 26); h0 = (ulong32)d0 & 0x3ffffff;
64
10.2M
      d1 += c;      c = (ulong32)(d1 >> 26); h1 = (ulong32)d1 & 0x3ffffff;
65
10.2M
      d2 += c;      c = (ulong32)(d2 >> 26); h2 = (ulong32)d2 & 0x3ffffff;
66
10.2M
      d3 += c;      c = (ulong32)(d3 >> 26); h3 = (ulong32)d3 & 0x3ffffff;
67
10.2M
      d4 += c;      c = (ulong32)(d4 >> 26); h4 = (ulong32)d4 & 0x3ffffff;
68
10.2M
      h0 += c * 5;  c =          (h0 >> 26); h0 =          h0 & 0x3ffffff;
69
10.2M
      h1 += c;
70
71
10.2M
      in += 16;
72
10.2M
      inlen -= 16;
73
10.2M
   }
74
75
79.1k
   st->h[0] = h0;
76
79.1k
   st->h[1] = h1;
77
79.1k
   st->h[2] = h2;
78
79.1k
   st->h[3] = h3;
79
79.1k
   st->h[4] = h4;
80
79.1k
}
81
82
/**
83
   Initialize an POLY1305 context.
84
   @param st       The POLY1305 state
85
   @param key      The secret key
86
   @param keylen   The length of the secret key (octets)
87
   @return CRYPT_OK if successful
88
*/
89
int poly1305_init(poly1305_state *st, const unsigned char *key, unsigned long keylen)
90
39.5k
{
91
39.5k
   LTC_ARGCHK(st  != NULL);
92
39.5k
   LTC_ARGCHK(key != NULL);
93
39.5k
   LTC_ARGCHK(keylen == 32);
94
95
   /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */
96
39.5k
   LOAD32L(st->r[0], key +  0); st->r[0] = (st->r[0]     ) & 0x3ffffff;
97
39.5k
   LOAD32L(st->r[1], key +  3); st->r[1] = (st->r[1] >> 2) & 0x3ffff03;
98
39.5k
   LOAD32L(st->r[2], key +  6); st->r[2] = (st->r[2] >> 4) & 0x3ffc0ff;
99
39.5k
   LOAD32L(st->r[3], key +  9); st->r[3] = (st->r[3] >> 6) & 0x3f03fff;
100
39.5k
   LOAD32L(st->r[4], key + 12); st->r[4] = (st->r[4] >> 8) & 0x00fffff;
101
102
   /* h = 0 */
103
39.5k
   st->h[0] = 0;
104
39.5k
   st->h[1] = 0;
105
39.5k
   st->h[2] = 0;
106
39.5k
   st->h[3] = 0;
107
39.5k
   st->h[4] = 0;
108
109
   /* save pad for later */
110
39.5k
   LOAD32L(st->pad[0], key + 16);
111
39.5k
   LOAD32L(st->pad[1], key + 20);
112
39.5k
   LOAD32L(st->pad[2], key + 24);
113
39.5k
   LOAD32L(st->pad[3], key + 28);
114
115
39.5k
   st->leftover = 0;
116
39.5k
   st->final = 0;
117
39.5k
   return CRYPT_OK;
118
39.5k
}
119
120
/**
121
  Process data through POLY1305
122
  @param st      The POLY1305 state
123
  @param in      The data to send through HMAC
124
  @param inlen   The length of the data to HMAC (octets)
125
  @return CRYPT_OK if successful
126
*/
127
int poly1305_process(poly1305_state *st, const unsigned char *in, unsigned long inlen)
128
39.5k
{
129
39.5k
   unsigned long i;
130
131
39.5k
   if (inlen == 0) return CRYPT_OK; /* nothing to do */
132
39.5k
   LTC_ARGCHK(st != NULL);
133
39.5k
   LTC_ARGCHK(in != NULL);
134
135
   /* handle leftover */
136
39.5k
   if (st->leftover) {
137
0
      unsigned long want = (16 - st->leftover);
138
0
      if (want > inlen) want = inlen;
139
0
      for (i = 0; i < want; i++) st->buffer[st->leftover + i] = in[i];
140
0
      inlen -= want;
141
0
      in += want;
142
0
      st->leftover += want;
143
0
      if (st->leftover < 16) return CRYPT_OK;
144
0
      _poly1305_block(st, st->buffer, 16);
145
0
      st->leftover = 0;
146
0
   }
147
148
   /* process full blocks */
149
39.5k
   if (inlen >= 16) {
150
39.5k
      unsigned long want = (inlen & ~(16 - 1));
151
39.5k
      _poly1305_block(st, in, want);
152
39.5k
      in += want;
153
39.5k
      inlen -= want;
154
39.5k
   }
155
156
   /* store leftover */
157
39.5k
   if (inlen) {
158
329k
      for (i = 0; i < inlen; i++) st->buffer[st->leftover + i] = in[i];
159
39.5k
      st->leftover += inlen;
160
39.5k
   }
161
39.5k
   return CRYPT_OK;
162
39.5k
}
163
164
/**
165
   Terminate a POLY1305 session
166
   @param st      The POLY1305 state
167
   @param mac     [out] The destination of the POLY1305 authentication tag
168
   @param maclen  [in/out]  The max size and resulting size of the POLY1305 authentication tag
169
   @return CRYPT_OK if successful
170
*/
171
int poly1305_done(poly1305_state *st, unsigned char *mac, unsigned long *maclen)
172
39.5k
{
173
39.5k
   ulong32 h0,h1,h2,h3,h4,c;
174
39.5k
   ulong32 g0,g1,g2,g3,g4;
175
39.5k
   ulong64 f;
176
39.5k
   ulong32 mask;
177
178
39.5k
   LTC_ARGCHK(st     != NULL);
179
39.5k
   LTC_ARGCHK(mac    != NULL);
180
39.5k
   LTC_ARGCHK(maclen != NULL);
181
39.5k
   LTC_ARGCHK(*maclen >= 16);
182
183
   /* process the remaining block */
184
39.5k
   if (st->leftover) {
185
39.5k
      unsigned long i = st->leftover;
186
39.5k
      st->buffer[i++] = 1;
187
342k
      for (; i < 16; i++) st->buffer[i] = 0;
188
39.5k
      st->final = 1;
189
39.5k
      _poly1305_block(st, st->buffer, 16);
190
39.5k
   }
191
192
   /* fully carry h */
193
39.5k
   h0 = st->h[0];
194
39.5k
   h1 = st->h[1];
195
39.5k
   h2 = st->h[2];
196
39.5k
   h3 = st->h[3];
197
39.5k
   h4 = st->h[4];
198
199
39.5k
                c = h1 >> 26; h1 = h1 & 0x3ffffff;
200
39.5k
   h2 +=     c; c = h2 >> 26; h2 = h2 & 0x3ffffff;
201
39.5k
   h3 +=     c; c = h3 >> 26; h3 = h3 & 0x3ffffff;
202
39.5k
   h4 +=     c; c = h4 >> 26; h4 = h4 & 0x3ffffff;
203
39.5k
   h0 += c * 5; c = h0 >> 26; h0 = h0 & 0x3ffffff;
204
39.5k
   h1 +=     c;
205
206
   /* compute h + -p */
207
39.5k
   g0 = h0 + 5; c = g0 >> 26; g0 &= 0x3ffffff;
208
39.5k
   g1 = h1 + c; c = g1 >> 26; g1 &= 0x3ffffff;
209
39.5k
   g2 = h2 + c; c = g2 >> 26; g2 &= 0x3ffffff;
210
39.5k
   g3 = h3 + c; c = g3 >> 26; g3 &= 0x3ffffff;
211
39.5k
   g4 = h4 + c - (1UL << 26);
212
213
   /* select h if h < p, or h + -p if h >= p */
214
39.5k
   mask = (g4 >> 31) - 1;
215
39.5k
   g0 &= mask;
216
39.5k
   g1 &= mask;
217
39.5k
   g2 &= mask;
218
39.5k
   g3 &= mask;
219
39.5k
   g4 &= mask;
220
39.5k
   mask = ~mask;
221
39.5k
   h0 = (h0 & mask) | g0;
222
39.5k
   h1 = (h1 & mask) | g1;
223
39.5k
   h2 = (h2 & mask) | g2;
224
39.5k
   h3 = (h3 & mask) | g3;
225
39.5k
   h4 = (h4 & mask) | g4;
226
227
   /* h = h % (2^128) */
228
39.5k
   h0 = ((h0      ) | (h1 << 26)) & 0xffffffff;
229
39.5k
   h1 = ((h1 >>  6) | (h2 << 20)) & 0xffffffff;
230
39.5k
   h2 = ((h2 >> 12) | (h3 << 14)) & 0xffffffff;
231
39.5k
   h3 = ((h3 >> 18) | (h4 <<  8)) & 0xffffffff;
232
233
   /* mac = (h + pad) % (2^128) */
234
39.5k
   f = (ulong64)h0 + st->pad[0]            ; h0 = (ulong32)f;
235
39.5k
   f = (ulong64)h1 + st->pad[1] + (f >> 32); h1 = (ulong32)f;
236
39.5k
   f = (ulong64)h2 + st->pad[2] + (f >> 32); h2 = (ulong32)f;
237
39.5k
   f = (ulong64)h3 + st->pad[3] + (f >> 32); h3 = (ulong32)f;
238
239
39.5k
   STORE32L(h0, mac +  0);
240
39.5k
   STORE32L(h1, mac +  4);
241
39.5k
   STORE32L(h2, mac +  8);
242
39.5k
   STORE32L(h3, mac + 12);
243
244
   /* zero out the state */
245
39.5k
   st->h[0] = 0;
246
39.5k
   st->h[1] = 0;
247
39.5k
   st->h[2] = 0;
248
39.5k
   st->h[3] = 0;
249
39.5k
   st->h[4] = 0;
250
39.5k
   st->r[0] = 0;
251
39.5k
   st->r[1] = 0;
252
39.5k
   st->r[2] = 0;
253
39.5k
   st->r[3] = 0;
254
39.5k
   st->r[4] = 0;
255
39.5k
   st->pad[0] = 0;
256
39.5k
   st->pad[1] = 0;
257
39.5k
   st->pad[2] = 0;
258
39.5k
   st->pad[3] = 0;
259
260
39.5k
   *maclen = 16;
261
39.5k
   return CRYPT_OK;
262
39.5k
}
263
264
#endif
265
266
/* ref:         $Format:%D$ */
267
/* git commit:  $Format:%H$ */
268
/* commit time: $Format:%ai$ */