330ec7b9fd10907785bb935fae267b27025c2f8f
[catacomb] / symm / gcm.c
1 /* -*-c-*-
2 *
3 * The GCM authenticated encryption mode
4 *
5 * (c) 2017 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of Catacomb.
11 *
12 * Catacomb is free software: you can redistribute it and/or modify it
13 * under the terms of the GNU Library General Public License as published
14 * by the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * Catacomb is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Library General Public License for more details.
21 *
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb. If not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
25 * USA.
26 */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include "config.h"
31
32 #include <stdio.h>
33
34 #include <mLib/bits.h>
35
36 #include "gcm.h"
37 #include "gcm-def.h"
38
39 /*----- Overall strategy --------------------------------------------------*
40 *
41 * GCM is pretty awful to implement in software. (This presentation is going
42 * to be somewhat different to that in the specification, but I think it
43 * makes more sense like this.)
44 *
45 * We're given a %$w$%-bit blockcipher %$E$% with a key %$K$%.
46 *
47 * The main part is arithmetic in the finite field %$k = \gf{2^w}$%, which we
48 * represent as the quotient ring %$\gf{2}[t]/(p_w(t))$% for some irreducible
49 * degree-%$w$% polynomial %$p(t)$%, whose precise value isn't very important
50 * right now. We choose a secret point %$x = E_K(0^w)$%.
51 *
52 * We choose a length size %$z$% as follows: if %$w < 96%$ then %$z = w$%;
53 * otherwise %$z = w/2$%. Format a message pair as follows:
54 *
55 * %$F(a, b) = P_w(a) \cat P_w(b) \cat [\ell(a)]_z \cat [\ell(b)]_z$%
56 *
57 * where %$P_w(x) = x \cat 0^n$% where $%0 \le n < w$% such that
58 * %$\ell(x) + n \equiv 0 \pmod{w}$%.
59 *
60 * Hash a (block-aligned) message %$u$% as follows. First, split %$u$% into
61 * %$w$%-bit blocks %$u_0$%, %$u_1$%, %%\ldots%%, %$u_{n-1}$%. Interpret
62 * these as elements of %$k$%. Then
63 *
64 * %$G_x(u) = u_0 t^n + u_1 t^{n-1} + \cdots + u_{n-1} t$%
65 *
66 * converted back to a %$w$%-bit string.
67 *
68 * We're ready to go now. Suppose we're to encrypt a message %$M$% with
69 * header %$H$% and nonce %$N$%. If %$\ell(N) + 32 = w$% then let
70 * %$N' = N$% and let %$i_0 = 1$%; otherwise, let %$U = G_t(F(\epsilon, N))$%
71 * and split this into %$N' = U[0 \bitsto w - 32]$% and
72 * %$[i_0]_{32} = U[w - 32 \bitsto w]$%.
73 *
74 * Let %$n = \lceil \ell(M)/w \rceil$%. Compute
75 *
76 * %$y_j = E_K(N' \cat [i_0 + j]_{32})$%
77 *
78 * for %$0 \le j \le n$%. Let
79 *
80 * %$s = (y_1 \cat y_2 \cat \cdots \cat y_n)[0 \bitsto \ell(M)$%
81 *
82 * Let %$C = M \xor s$% and let %$T = G_x(F(H, C)) \xor y_0$%. These are the
83 * ciphertext and tag respectively.
84 *
85 * So why is this awful?
86 *
87 * For one thing, the bits are in a completely terrible order. The bytes are
88 * arranged in little-endian order, so the unit coefficient is in the first
89 * byte, and the degree-127 coefficient is in the last byte. But within each
90 * byte, the lowest-degree coefficient is in the most significant bit. It's
91 * therefore better to think of GCM as using a big-endian byte-ordering
92 * convention, but with the bits backwards.
93 *
94 * But messing about with byte ordering is expensive, so let's not do that in
95 * the inner loop. But multiplication in %$k$% is not easy either. Some
96 * kind of precomputed table would be nice, but that will leak secrets
97 * through the cache.
98 *
99 * I choose a particularly simple table: given %$x$%, let %$X[i'] = x t^i$%.
100 * Then $%$x y = \sum_{0\le i<w} y_i X[i']$% which is just a bunch of
101 * bitmasking. But the natural order for examining bits of %$y$% is not
102 * necessarily the obvious one. We'll have already loaded %$y$% into
103 * internal form, as 32-bit words. The good order to process these is left
104 * to right, from high to low bits. But now the order of degrees depends on
105 * the endianness of our conversion of bytes to words. Oh, well.
106 *
107 * If we've adopted a big-endian convention, then we'll see the degrees in
108 * order, 0, 1, ..., all the way up to %$w - 1$% and everything is fine. If
109 * we've adopted a little-endian convention, though, we'll see an ordering
110 * like this:
111 *
112 * 24, 25, ..., 31, 16, 17, ..., 23, 8, 9, ..., 15, 0, 1, ..., 7,
113 * 56, 57, ..., 63, 48, 49, ..., 55, 40, 41, ..., 47, 32, 33, ..., 39,
114 * etc.
115 *
116 * which is the ordinary order with 0x18 = 24 XORed into the index. That is,
117 * %$i' = i$% if we've adopted a big-endian convention, and
118 * %$i' = i \xor 24$% if we've adopted a little-endian convention.
119 */
120
121 /*----- Low-level utilities -----------------------------------------------*/
122
123 /* --- @mult@ --- *
124 *
125 * Arguments: @const gcm_params *p@ = pointer to the parameters
126 * @uint32 *z@ = where to write the result
127 * @const uint32 *x@ = input field element
128 *
129 * Returns: ---
130 *
131 * Use: Multiply the input field element by %$t$%, and write the
132 * product to @z@. It's safe for @x@ and @z@ to be equal, but
133 * they should not otherwise overlap. Both input and output are
134 * in big-endian form, i.e., with the lowest-degree coefficients
135 * in the most significant bits.
136 */
137
138 static void mult(const gcm_params *p, uint32 *z, const uint32 *x)
139 {
140 uint32 m, c, t;
141 unsigned i;
142
143 t = x[p->n - 1]; m = -(t&1u); c = m&p->poly;
144 for (i = 0; i < p->n; i++) { t = x[i]; z[i] = (t >> 1) ^ c; c = t << 31; }
145 }
146
147 /* --- @mul@ --- *
148 *
149 * Arguments: @const gcm_params *p@ = pointer to the parameters
150 * @uint32 *z@ = where to write the result
151 * @const uint32 *x, *y@ = input field elements
152 *
153 * Returns: ---
154 *
155 * Use: Multiply the input field elements together, and write the
156 * product to @z@. It's safe for the operands to overlap. Both
157 * inputs and the output are in big-endian form, i.e., with the
158 * lowest-degree coefficients in the most significant bits.
159 */
160
161 static void mul(const gcm_params *p, uint32 *z,
162 const uint32 *x, const uint32 *y)
163 {
164 uint32 m, t, u[GCM_NMAX], v[GCM_NMAX];
165 unsigned i, j, k;
166
167 /* We can't do this in-place at all, so use temporary space. Make a copy
168 * of @x@ in @u@, where we can clobber it, and build the product in @v@.
169 */
170 for (i = 0; i < p->n; i++) { u[i] = x[i]; v[i] = 0; }
171
172 /* Repeatedly multiply @x@ (in @u@) by %$t$%, and add together those
173 * %$x t^i$% selected by the bits of @y@. This is basically what you get
174 * by streaming the result of @gcm_mktable@ into @gcm_mulk_...@.
175 */
176 for (i = 0; i < p->n; i++) {
177 t = y[i];
178 for (j = 0; j < 32; j++) {
179 m = -((t >> 31)&1u);
180 for (k = 0; k < p->n; k++) v[k] ^= u[k]&m;
181 mult(p, u, u); t <<= 1;
182 }
183 }
184
185 /* Write out the result now that it's ready. */
186 for (i = 0; i < p->n; i++) z[i] = v[i];
187 }
188
189 /*----- Table-based multiplication ----------------------------------------*/
190
191 /* --- @gcm_mktable@ --- *
192 *
193 * Arguments: @const gcm_params *p@ = pointer to the parameters
194 * @uint32 *ktab@ = where to write the table; there must be
195 * space for %$32 n$% $%n$%-word entries, i.e.,
196 * %$32 n^2$% 32-bit words in total, where %$n$% is
197 * @p->n@, the block size in words
198 * @const uint32 *k@ = input field element
199 *
200 * Returns: ---
201 *
202 * Use: Construct a table for use by @gcm_mulk_...@ below, to
203 * multiply (vaguely) efficiently by @k@.
204 */
205
206 void gcm_mktable(const gcm_params *p, uint32 *ktab, const uint32 *k)
207 {
208 unsigned m = (p->f&GCMF_SWAP ? 0x18 : 0);
209 unsigned i, j, o = m*p->n;
210
211 /* As described above, the table stores entries %$K[i \xor m] = k t^i$%,
212 * where %$m = 0$% (big-endian cipher) or %$m = 24$% (little-endian).
213 * The first job is to store %$K[m] = k$%.
214 *
215 * We initially build the table with the entries in big-endian order, and
216 * then swap them if necessary. This makes the arithmetic functions more
217 * amenable for use by @gcm_concat@ below.
218 */
219 if (!(p->f&GCMF_SWAP)) for (i = 0; i < p->n; i++) ktab[o + i] = k[i];
220 else for (i = 0; i < p->n; i++) ktab[o + i] = ENDSWAP32(k[i]);
221
222 /* Fill in the rest of the table by repeatedly multiplying the previous
223 * entry by %$t$%.
224 */
225 for (i = 1; i < 32*p->n; i++)
226 { j = (i ^ m)*p->n; mult(p, ktab + j, ktab + o); o = j; }
227
228 /* Finally, if the cipher uses a little-endian convention, then swap all of
229 * the individual words.
230 */
231 if (p->f&GCMF_SWAP)
232 for (i = 0; i < 32*p->n*p->n; i++) ktab[i] = ENDSWAP32(ktab[i]);
233 }
234
235 /* --- @gcm_mulk_N@ --- *
236 *
237 * Arguments: @uint32 *a@ = accumulator to multiply
238 * @const uint32 *ktab@ = table constructed by @gcm_mktable@
239 *
240 * Returns: ---
241 *
242 * Use: Multiply @a@ by @k@ (implicitly represented in @ktab@),
243 * updating @a@ in-place. There are separate functions for each
244 * supported block size because this is the function whose
245 * performance actually matters.
246 */
247
248 #define DEF_MULK(nbits) \
249 void gcm_mulk_##nbits(uint32 *a, const uint32 *ktab) \
250 { \
251 uint32 m, t; \
252 uint32 z[nbits/32]; \
253 unsigned i, j, k; \
254 \
255 for (i = 0; i < nbits/32; i++) z[i] = 0; \
256 \
257 for (i = 0; i < nbits/32; i++) { \
258 t = a[i]; \
259 for (j = 0; j < 32; j++) { \
260 m = -((t >> 31)&1u); \
261 for (k = 0; k < nbits/32; k++) z[k] ^= *ktab++&m; \
262 t <<= 1; \
263 } \
264 } \
265 \
266 for (i = 0; i < nbits/32; i++) a[i] = z[i]; \
267 }
268 GCM_WIDTHS(DEF_MULK)
269
270 #define GCM_MULK_CASE(nbits) \
271 case nbits/32: gcm_mulk_##nbits(_a, _ktab); break;
272 #define MULK(n, a, ktab) do { \
273 uint32 *_a = (a); const uint32 *_ktab = (ktab); \
274 switch (n) { \
275 GCM_WIDTHS(GCM_MULK_CASE) \
276 default: abort(); \
277 } \
278 } while (0)
279
280 /*----- Other utilities ---------------------------------------------------*/
281
282 /* --- @putlen@ --- *
283 *
284 * Arguments: @octet *p@ = pointer to output buffer
285 * @unsigned w@ = size of output buffer
286 * @unsigned blksz@ = block size (assumed fairly small)
287 * @unsigned long nblocks@ = number of blocks
288 * @unsigned nbytes@ = tail size in bytes (assumed small)
289 *
290 * Returns: ---
291 *
292 * Use: Store the overall length in %$\emph{bits}$% (i.e.,
293 * @3*(nblocks*blksz + nbytes)@ in big-endian form in the
294 * buffer @p@.
295 */
296
297 static void putlen(octet *p, unsigned w, unsigned blksz,
298 unsigned long nblocks, unsigned nbytes)
299 {
300 unsigned long nblo = nblocks&((1ul << (ULONG_BITS/2)) - 1),
301 nbhi = nblocks >> ULONG_BITS/2;
302 unsigned long nlo = nblo*blksz + nbytes, nhi = nbhi*blksz;
303
304 /* This is fiddly. Split @nblocks@, which is the big number, into high and
305 * low halves, multiply those separately by @blksz@, propagate carries, and
306 * then multiply by eight.
307 */
308 nhi += nlo >> ULONG_BITS/2;
309 nlo &= (1ul << (ULONG_BITS/2)) - 1;
310 nlo <<= 3;
311
312 /* Now write out the size, feeding bits in from @nhi@ as necessary. */
313 p += w;
314 while (w--) {
315 *--p = U8(nlo);
316 nlo = (nlo >> 8) | ((nhi&0xff) << (ULONG_BITS/2 - 5));
317 nhi >>= 8;
318 }
319 }
320
321 /* --- @mix@ --- *
322 *
323 * Arguments: @const gcm_params *p@ = pointer to the parameters
324 * @uint32 *a@ = GHASH accumulator
325 * @const octet *q@ = pointer to an input block
326 * @const uint32 *ktab@ = multiplication table, built by
327 * @gcm_mktable@
328 *
329 * Returns: ---
330 *
331 * Use: Fold the block @q@ into the GHASH accumulator. The
332 * calculation is %$a' = k (a + q)$%.
333 */
334
335 static void mix(const gcm_params *p, uint32 *a,
336 const octet *q, const uint32 *ktab)
337 {
338 unsigned i;
339
340 if (p->f&GCMF_SWAP)
341 for (i = 0; i < p->n; i++) { a[i] ^= LOAD32_L(q); q += 4; }
342 else
343 for (i = 0; i < p->n; i++) { a[i] ^= LOAD32_B(q); q += 4; }
344 MULK(p->n, a, ktab);
345 }
346
347 /* --- @gcm_ghashdone@ --- *
348 *
349 * Arguments: @const gcm_params *p@ = pointer to the parameters
350 * @uint32 *a@ = GHASH accumulator
351 * @const uint32 *ktab@ = multiplication table, built by
352 * @gcm_mktable@
353 * @unsigned long xblocks, yblocks@ = number of whole blocks in
354 * the two inputs
355 * @unsigned xbytes, ybytes@ = number of trailing bytes in the
356 * two inputs
357 *
358 * Returns: ---
359 *
360 * Use: Finishes a GHASH operation by appending the appropriately
361 * encoded lengths of the two constituent messages.
362 */
363
364 void gcm_ghashdone(const gcm_params *p, uint32 *a, const uint32 *ktab,
365 unsigned long xblocks, unsigned xbytes,
366 unsigned long yblocks, unsigned ybytes)
367 {
368 octet b[4*GCM_NMAX];
369 unsigned w = p->n < 3 ? 4*p->n : 2*p->n;
370
371 /* Construct the encoded lengths. Note that smaller-block versions of GCM
372 * encode the lengths in separate blocks. GCM is only officially defined
373 * for 64- and 128-bit blocks; I've placed the cutoff somewhat arbitrarily
374 * at 96 bits.
375 */
376 putlen(b, w, 4*p->n, xblocks, xbytes);
377 putlen(b + w, w, 4*p->n, yblocks, ybytes);
378
379 /* Feed the lengths into the accumulator. */
380 mix(p, a, b, ktab);
381 if (p->n < 3) mix(p, a, b + w, ktab);
382 }
383
384 /* --- @gcm_concat@ --- *
385 *
386 * Arguments: @const gcm_params *p@ = pointer to the parameters
387 * @uint32 *z@ = GHASH accumulator for suffix, updated
388 * @const uint32 *x@ = GHASH accumulator for prefix
389 * @const uint32 *ktab@ = multiplication table, built by
390 * @gcm_mktable@
391 * @unsigned long n@ = length of suffix in whole blocks
392 *
393 * Returns: ---
394 *
395 * Use: On entry, @x@ and @z@ are the results of hashing two strings
396 * %$a$% and %$b$%, each a whole number of blocks long; in
397 * particular, %$b$% is @n@ blocks long. On exit, @z@ is
398 * updated to be the hash of %$a \cat b$%.
399 */
400
401 void gcm_concat(const gcm_params *p, uint32 *z, const uint32 *x,
402 const uint32 *ktab, unsigned long n)
403 {
404 uint32 t[GCM_NMAX], u[GCM_NMAX];
405 unsigned i, j;
406
407 if (!n) {
408 /* If @n@ is zero, then there's not much to do. The mathematics
409 * (explained below) still works, but the code takes a shortcut which
410 * doesn't handle this case: so set %$z' = z + x k^n = z + x$%.
411 */
412
413 for (j = 0; j < p->n; j++) z[j] ^= x[j];
414 } else {
415 /* We have %$x = a_0 t^m + \cdots + a_{m-2} t^2 + a_{m-1} t$% and
416 * %$z = b_0 t^n + \cdots + b_{n-2} t^2 + b_{n-1} t$%. What we'd like is
417 * the hash of %$a \cat b$%, which is %$z + x k^n$%.
418 *
419 * The first job, then, is to calculate %$k^n$%, and for this we use a
420 * simple left-to-right square-and-multiply algorithm. There's no need
421 * to keep %$n$% secret here.
422 */
423
424 /* Start by retrieving %$k$% from the table, and convert it to big-endian
425 * form.
426 */
427 if (!(p->f&GCMF_SWAP)) for (j = 0; j < p->n; j++) u[j] = ktab[j];
428 else for (j = 0; j < p->n; j++) u[j] = ENDSWAP32(ktab[24*p->n + j]);
429
430 /* Now calculate %$k^n$%. */
431 i = ULONG_BITS;
432 #define BIT (1ul << (ULONG_BITS - 1))
433 while (!(n&BIT)) { n <<= 1; i--; }
434 n <<= 1; i--; for (j = 0; j < p->n; j++) t[j] = u[j];
435 while (i--) { mul(p, t, t, t); if (n&BIT) mul(p, t, t, u); n <<= 1; }
436 #undef BIT
437
438 /* Next, calculate %$x k^n$%. If we're using a little-endian convention
439 * then we must convert %$x$%; otherwise we can just use it in place.
440 */
441 if (!(p->f&GCMF_SWAP))
442 mul(p, t, t, x);
443 else {
444 for (j = 0; j < p->n; j++) u[j] = ENDSWAP32(x[j]);
445 mul(p, t, t, u);
446 }
447
448 /* Finally, add %$x k^n$% onto %$z$%, converting back to little-endian if
449 * necessary.
450 */
451 if (!(p->f&GCMF_SWAP)) for (j = 0; j < p->n; j++) z[j] ^= t[j];
452 else for (j = 0; j < p->n; j++) z[j] ^= ENDSWAP32(t[j]);
453 }
454 }
455
456 /*----- Test rig ----------------------------------------------------------*/
457
458 #ifdef TEST_RIG
459
460 #include <mLib/quis.h>
461 #include <mLib/testrig.h>
462
463 static void report_failure(const char *test, unsigned nbits,
464 dstr v[], dstr *d)
465 {
466 printf("test %s failed (nbits = %u)", test, nbits);
467 printf("\n\tx = "); type_hex.dump(&v[0], stdout);
468 printf("\n\ty = "); type_hex.dump(&v[1], stdout);
469 printf("\n\tz = "); type_hex.dump(&v[2], stdout);
470 printf("\n\tz' = "); type_hex.dump(d, stdout);
471 putchar('\n');
472 }
473
474 static void mulk(unsigned nbits, uint32 *x, const uint32 *ktab)
475 { MULK(nbits/32, x, ktab); }
476
477 static int test_mul(uint32 poly, dstr v[])
478 {
479 uint32 x[GCM_NMAX], y[GCM_NMAX], z[GCM_NMAX], ktab[32*GCM_NMAX*GCM_NMAX];
480 gcm_params p;
481 dstr d = DSTR_INIT;
482 unsigned i, nbits;
483 int ok = 1;
484
485 nbits = 8*v[0].len; p.f = 0; p.n = nbits/32; p.poly = poly;
486 dstr_ensure(&d, nbits/8); d.len = nbits/8;
487
488 /* First, test plain multiply. */
489 for (i = 0; i < nbits/32; i++)
490 { x[i] = LOAD32_B(v[0].buf + 4*i); y[i] = LOAD32_B(v[1].buf + 4*i); }
491 mul(&p, z, x, y);
492 for (i = 0; i < nbits/32; i++) STORE32_B(d.buf + 4*i, z[i]);
493 if (memcmp(d.buf, v[2].buf, nbits/8) != 0)
494 { ok = 0; report_failure("gcm_mul", nbits, v, &d); }
495
496 /* Next, test big-endian prepared key. */
497 for (i = 0; i < nbits/32; i++)
498 { x[i] = LOAD32_B(v[0].buf + 4*i); y[i] = LOAD32_B(v[1].buf + 4*i); }
499 gcm_mktable(&p, ktab, y);
500 mulk(nbits, x, ktab);
501 for (i = 0; i < nbits/32; i++) STORE32_B(d.buf + 4*i, x[i]);
502 if (memcmp(d.buf, v[2].buf, nbits/8) != 0)
503 { ok = 0; report_failure("gcm_kmul_b(k = y)", nbits, v, &d); }
504
505 for (i = 0; i < nbits/32; i++)
506 { x[i] = LOAD32_B(v[0].buf + 4*i); y[i] = LOAD32_B(v[1].buf + 4*i); }
507 gcm_mktable(&p, ktab, x);
508 mulk(nbits, y, ktab);
509 for (i = 0; i < nbits/32; i++) STORE32_B(d.buf + 4*i, y[i]);
510 if (memcmp(d.buf, v[2].buf, nbits/8) != 0)
511 { ok = 0; report_failure("gcm_kmul_b(k = x)", nbits, v, &d); }
512
513 /* Finally, test little-endian prepared key. */
514 p.f = GCMF_SWAP;
515 for (i = 0; i < nbits/32; i++)
516 { x[i] = LOAD32_L(v[0].buf + 4*i); y[i] = LOAD32_L(v[1].buf + 4*i); }
517 gcm_mktable(&p, ktab, y);
518 mulk(nbits, x, ktab);
519 for (i = 0; i < nbits/32; i++) STORE32_L(d.buf + 4*i, x[i]);
520 if (memcmp(d.buf, v[2].buf, nbits/8) != 0)
521 { ok = 0; report_failure("gcm_kmul_l(k = y)", nbits, v, &d); }
522
523 for (i = 0; i < nbits/32; i++)
524 { x[i] = LOAD32_L(v[0].buf + 4*i); y[i] = LOAD32_L(v[1].buf + 4*i); }
525 gcm_mktable(&p, ktab, x);
526 mulk(nbits, y, ktab);
527 for (i = 0; i < nbits/32; i++) STORE32_L(d.buf + 4*i, y[i]);
528 if (memcmp(d.buf, v[2].buf, nbits/8) != 0)
529 { ok = 0; report_failure("gcm_kmul_l(k = x)", nbits, v, &d); }
530
531 /* All done. */
532 return (ok);
533 }
534
535 #define TEST(nbits) \
536 static int test_mul_##nbits(dstr v[]) \
537 { return (test_mul(GCM_POLY_##nbits, v)); }
538 GCM_WIDTHS(TEST)
539 #undef TEST
540
541 static test_chunk defs[] = {
542 #define TEST(nbits) \
543 { "gcm-mul" #nbits, test_mul_##nbits, \
544 { &type_hex, &type_hex, &type_hex, 0 } },
545 GCM_WIDTHS(TEST)
546 #undef TEST
547 { 0, 0, { 0 } }
548 };
549
550 int main(int argc, char *argv[])
551 {
552 ego(argv[0]);
553 test_run(argc, argv, defs, SRCDIR"/t/gcm");
554 return (0);
555 }
556
557 #endif
558
559 /*----- That's all, folks -------------------------------------------------*/