Coverage Report

Created: 2025-08-26 06:41

/src/dropbear/libtomcrypt/src/mac/poly1305/poly1305.c
Line
Count
Source (jump to first uncovered line)
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
0
{
22
0
   const unsigned long hibit = (st->final) ? 0 : (1UL << 24); /* 1 << 128 */
23
0
   ulong32 r0,r1,r2,r3,r4;
24
0
   ulong32 s1,s2,s3,s4;
25
0
   ulong32 h0,h1,h2,h3,h4;
26
0
   ulong32 tmp;
27
0
   ulong64 d0,d1,d2,d3,d4;
28
0
   ulong32 c;
29
30
0
   r0 = st->r[0];
31
0
   r1 = st->r[1];
32
0
   r2 = st->r[2];
33
0
   r3 = st->r[3];
34
0
   r4 = st->r[4];
35
36
0
   s1 = r1 * 5;
37
0
   s2 = r2 * 5;
38
0
   s3 = r3 * 5;
39
0
   s4 = r4 * 5;
40
41
0
   h0 = st->h[0];
42
0
   h1 = st->h[1];
43
0
   h2 = st->h[2];
44
0
   h3 = st->h[3];
45
0
   h4 = st->h[4];
46
47
0
   while (inlen >= 16) {
48
      /* h += in[i] */
49
0
      LOAD32L(tmp, in+ 0); h0 += (tmp     ) & 0x3ffffff;
50
0
      LOAD32L(tmp, in+ 3); h1 += (tmp >> 2) & 0x3ffffff;
51
0
      LOAD32L(tmp, in+ 6); h2 += (tmp >> 4) & 0x3ffffff;
52
0
      LOAD32L(tmp, in+ 9); h3 += (tmp >> 6) & 0x3ffffff;
53
0
      LOAD32L(tmp, in+12); h4 += (tmp >> 8) | hibit;
54
55
      /* h *= r */
56
0
      d0 = ((ulong64)h0 * r0) + ((ulong64)h1 * s4) + ((ulong64)h2 * s3) + ((ulong64)h3 * s2) + ((ulong64)h4 * s1);
57
0
      d1 = ((ulong64)h0 * r1) + ((ulong64)h1 * r0) + ((ulong64)h2 * s4) + ((ulong64)h3 * s3) + ((ulong64)h4 * s2);
58
0
      d2 = ((ulong64)h0 * r2) + ((ulong64)h1 * r1) + ((ulong64)h2 * r0) + ((ulong64)h3 * s4) + ((ulong64)h4 * s3);
59
0
      d3 = ((ulong64)h0 * r3) + ((ulong64)h1 * r2) + ((ulong64)h2 * r1) + ((ulong64)h3 * r0) + ((ulong64)h4 * s4);
60
0
      d4 = ((ulong64)h0 * r4) + ((ulong64)h1 * r3) + ((ulong64)h2 * r2) + ((ulong64)h3 * r1) + ((ulong64)h4 * r0);
61
62
      /* (partial) h %= p */
63
0
                    c = (ulong32)(d0 >> 26); h0 = (ulong32)d0 & 0x3ffffff;
64
0
      d1 += c;      c = (ulong32)(d1 >> 26); h1 = (ulong32)d1 & 0x3ffffff;
65
0
      d2 += c;      c = (ulong32)(d2 >> 26); h2 = (ulong32)d2 & 0x3ffffff;
66
0
      d3 += c;      c = (ulong32)(d3 >> 26); h3 = (ulong32)d3 & 0x3ffffff;
67
0
      d4 += c;      c = (ulong32)(d4 >> 26); h4 = (ulong32)d4 & 0x3ffffff;
68
0
      h0 += c * 5;  c =          (h0 >> 26); h0 =          h0 & 0x3ffffff;
69
0
      h1 += c;
70
71
0
      in += 16;
72
0
      inlen -= 16;
73
0
   }
74
75
0
   st->h[0] = h0;
76
0
   st->h[1] = h1;
77
0
   st->h[2] = h2;
78
0
   st->h[3] = h3;
79
0
   st->h[4] = h4;
80
0
}
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
0
{
91
0
   LTC_ARGCHK(st  != NULL);
92
0
   LTC_ARGCHK(key != NULL);
93
0
   LTC_ARGCHK(keylen == 32);
94
95
   /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */
96
0
   LOAD32L(st->r[0], key +  0); st->r[0] = (st->r[0]     ) & 0x3ffffff;
97
0
   LOAD32L(st->r[1], key +  3); st->r[1] = (st->r[1] >> 2) & 0x3ffff03;
98
0
   LOAD32L(st->r[2], key +  6); st->r[2] = (st->r[2] >> 4) & 0x3ffc0ff;
99
0
   LOAD32L(st->r[3], key +  9); st->r[3] = (st->r[3] >> 6) & 0x3f03fff;
100
0
   LOAD32L(st->r[4], key + 12); st->r[4] = (st->r[4] >> 8) & 0x00fffff;
101
102
   /* h = 0 */
103
0
   st->h[0] = 0;
104
0
   st->h[1] = 0;
105
0
   st->h[2] = 0;
106
0
   st->h[3] = 0;
107
0
   st->h[4] = 0;
108
109
   /* save pad for later */
110
0
   LOAD32L(st->pad[0], key + 16);
111
0
   LOAD32L(st->pad[1], key + 20);
112
0
   LOAD32L(st->pad[2], key + 24);
113
0
   LOAD32L(st->pad[3], key + 28);
114
115
0
   st->leftover = 0;
116
0
   st->final = 0;
117
0
   return CRYPT_OK;
118
0
}
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
0
{
129
0
   unsigned long i;
130
131
0
   if (inlen == 0) return CRYPT_OK; /* nothing to do */
132
0
   LTC_ARGCHK(st != NULL);
133
0
   LTC_ARGCHK(in != NULL);
134
135
   /* handle leftover */
136
0
   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
0
   if (inlen >= 16) {
150
0
      unsigned long want = (inlen & ~(16 - 1));
151
0
      _poly1305_block(st, in, want);
152
0
      in += want;
153
0
      inlen -= want;
154
0
   }
155
156
   /* store leftover */
157
0
   if (inlen) {
158
0
      for (i = 0; i < inlen; i++) st->buffer[st->leftover + i] = in[i];
159
0
      st->leftover += inlen;
160
0
   }
161
0
   return CRYPT_OK;
162
0
}
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
0
{
173
0
   ulong32 h0,h1,h2,h3,h4,c;
174
0
   ulong32 g0,g1,g2,g3,g4;
175
0
   ulong64 f;
176
0
   ulong32 mask;
177
178
0
   LTC_ARGCHK(st     != NULL);
179
0
   LTC_ARGCHK(mac    != NULL);
180
0
   LTC_ARGCHK(maclen != NULL);
181
0
   LTC_ARGCHK(*maclen >= 16);
182
183
   /* process the remaining block */
184
0
   if (st->leftover) {
185
0
      unsigned long i = st->leftover;
186
0
      st->buffer[i++] = 1;
187
0
      for (; i < 16; i++) st->buffer[i] = 0;
188
0
      st->final = 1;
189
0
      _poly1305_block(st, st->buffer, 16);
190
0
   }
191
192
   /* fully carry h */
193
0
   h0 = st->h[0];
194
0
   h1 = st->h[1];
195
0
   h2 = st->h[2];
196
0
   h3 = st->h[3];
197
0
   h4 = st->h[4];
198
199
0
                c = h1 >> 26; h1 = h1 & 0x3ffffff;
200
0
   h2 +=     c; c = h2 >> 26; h2 = h2 & 0x3ffffff;
201
0
   h3 +=     c; c = h3 >> 26; h3 = h3 & 0x3ffffff;
202
0
   h4 +=     c; c = h4 >> 26; h4 = h4 & 0x3ffffff;
203
0
   h0 += c * 5; c = h0 >> 26; h0 = h0 & 0x3ffffff;
204
0
   h1 +=     c;
205
206
   /* compute h + -p */
207
0
   g0 = h0 + 5; c = g0 >> 26; g0 &= 0x3ffffff;
208
0
   g1 = h1 + c; c = g1 >> 26; g1 &= 0x3ffffff;
209
0
   g2 = h2 + c; c = g2 >> 26; g2 &= 0x3ffffff;
210
0
   g3 = h3 + c; c = g3 >> 26; g3 &= 0x3ffffff;
211
0
   g4 = h4 + c - (1UL << 26);
212
213
   /* select h if h < p, or h + -p if h >= p */
214
0
   mask = (g4 >> 31) - 1;
215
0
   g0 &= mask;
216
0
   g1 &= mask;
217
0
   g2 &= mask;
218
0
   g3 &= mask;
219
0
   g4 &= mask;
220
0
   mask = ~mask;
221
0
   h0 = (h0 & mask) | g0;
222
0
   h1 = (h1 & mask) | g1;
223
0
   h2 = (h2 & mask) | g2;
224
0
   h3 = (h3 & mask) | g3;
225
0
   h4 = (h4 & mask) | g4;
226
227
   /* h = h % (2^128) */
228
0
   h0 = ((h0      ) | (h1 << 26)) & 0xffffffff;
229
0
   h1 = ((h1 >>  6) | (h2 << 20)) & 0xffffffff;
230
0
   h2 = ((h2 >> 12) | (h3 << 14)) & 0xffffffff;
231
0
   h3 = ((h3 >> 18) | (h4 <<  8)) & 0xffffffff;
232
233
   /* mac = (h + pad) % (2^128) */
234
0
   f = (ulong64)h0 + st->pad[0]            ; h0 = (ulong32)f;
235
0
   f = (ulong64)h1 + st->pad[1] + (f >> 32); h1 = (ulong32)f;
236
0
   f = (ulong64)h2 + st->pad[2] + (f >> 32); h2 = (ulong32)f;
237
0
   f = (ulong64)h3 + st->pad[3] + (f >> 32); h3 = (ulong32)f;
238
239
0
   STORE32L(h0, mac +  0);
240
0
   STORE32L(h1, mac +  4);
241
0
   STORE32L(h2, mac +  8);
242
0
   STORE32L(h3, mac + 12);
243
244
   /* zero out the state */
245
0
   st->h[0] = 0;
246
0
   st->h[1] = 0;
247
0
   st->h[2] = 0;
248
0
   st->h[3] = 0;
249
0
   st->h[4] = 0;
250
0
   st->r[0] = 0;
251
0
   st->r[1] = 0;
252
0
   st->r[2] = 0;
253
0
   st->r[3] = 0;
254
0
   st->r[4] = 0;
255
0
   st->pad[0] = 0;
256
0
   st->pad[1] = 0;
257
0
   st->pad[2] = 0;
258
0
   st->pad[3] = 0;
259
260
0
   *maclen = 16;
261
0
   return CRYPT_OK;
262
0
}
263
264
#endif
265
266
/* ref:         $Format:%D$ */
267
/* git commit:  $Format:%H$ */
268
/* commit time: $Format:%ai$ */