|
| 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 | + |
| 11 | +/** |
| 12 | + @file sober128_stream.c |
| 13 | + Implementation of SOBER-128 by Tom St Denis. |
| 14 | + Based on s128fast.c reference code supplied by Greg Rose of QUALCOMM. |
| 15 | +*/ |
| 16 | + |
| 17 | +#ifdef LTC_SOBER128 |
| 18 | + |
| 19 | + |
| 20 | +#if defined(LTC_SOBER128_STREAM_SETUP) || defined(LTC_SOBER128_STREAM_SETIV) |
| 21 | + |
| 22 | +/* local prototypes */ |
| 23 | +static void _s128_diffuse(sober128_state *st); |
| 24 | + |
| 25 | +#endif /* LTC_SOBER128_STREAM_SETUP || LTC_SOBER128_STREAM_SETIV */ |
| 26 | + |
| 27 | + |
| 28 | +/* don't change these... */ |
| 29 | +#define N 17 |
| 30 | +#define INITKONST 0x6996c53a /* value of KONST to use during key loading */ |
| 31 | +#define KEYP 15 /* where to insert key words */ |
| 32 | +#define FOLDP 4 /* where to insert non-linear feedback */ |
| 33 | + |
| 34 | + |
| 35 | +/* give correct offset for the current position of the register, |
| 36 | + * where logically R[0] is at position "zero". |
| 37 | + */ |
| 38 | +#define OFF(zero, i) (((zero)+(i)) % N) |
| 39 | + |
| 40 | +/* step the LFSR */ |
| 41 | +/* After stepping, "zero" moves right one place */ |
| 42 | +#define STEP(R,z) \ |
| 43 | + R[OFF(z,0)] = R[OFF(z,15)] ^ R[OFF(z,4)] ^ (R[OFF(z,0)] << 8) ^ Multab[(R[OFF(z,0)] >> 24) & 0xFF]; |
| 44 | + |
| 45 | +static void _cycle(ulong32 *R) |
| 46 | +{ |
| 47 | + ulong32 t; |
| 48 | + int i; |
| 49 | + |
| 50 | + STEP(R,0); |
| 51 | + t = R[0]; |
| 52 | + for (i = 1; i < N; ++i) { |
| 53 | + R[i-1] = R[i]; |
| 54 | + } |
| 55 | + R[N-1] = t; |
| 56 | +} |
| 57 | + |
| 58 | +/* Return a non-linear function of some parts of the register. |
| 59 | + */ |
| 60 | +#define NLFUNC(st,z) \ |
| 61 | +{ \ |
| 62 | + t = st->R[OFF(z,0)] + st->R[OFF(z,16)]; \ |
| 63 | + t ^= Sbox[(t >> 24) & 0xFF]; \ |
| 64 | + t = RORc(t, 8); \ |
| 65 | + t = ((t + st->R[OFF(z,1)]) ^ st->konst) + st->R[OFF(z,6)]; \ |
| 66 | + t ^= Sbox[(t >> 24) & 0xFF]; \ |
| 67 | + t = t + st->R[OFF(z,13)]; \ |
| 68 | +} |
| 69 | + |
| 70 | +static ulong32 _nltap(const sober128_state *st) |
| 71 | +{ |
| 72 | + ulong32 t; |
| 73 | + NLFUNC(st, 0); |
| 74 | + return t; |
| 75 | +} |
| 76 | + |
| 77 | + |
| 78 | +/* Load key material into the register |
| 79 | + */ |
| 80 | +#define ADDKEY(k) \ |
| 81 | + st->R[KEYP] += (k); |
| 82 | + |
| 83 | +#define XORNL(nl) \ |
| 84 | + st->R[FOLDP] ^= (nl); |
| 85 | + |
| 86 | + |
| 87 | +#if defined(LTC_SOBER128_STREAM_SETUP) || defined(LTC_SOBER128_STREAM_SETIV) |
| 88 | + |
| 89 | +/* nonlinear diffusion of register for key */ |
| 90 | +#define DROUND(z) STEP(st->R,z); NLFUNC(st,(z+1)); st->R[OFF((z+1),FOLDP)] ^= t; |
| 91 | + |
| 92 | +/* _s128_diffuse() used in sober128_stream_setup() and sober128_stream_setiv() */ |
| 93 | +static void _s128_diffuse(sober128_state *st) |
| 94 | +{ |
| 95 | + ulong32 t; |
| 96 | + /* relies on FOLD == N == 17! */ |
| 97 | + DROUND(0); |
| 98 | + DROUND(1); |
| 99 | + DROUND(2); |
| 100 | + DROUND(3); |
| 101 | + DROUND(4); |
| 102 | + DROUND(5); |
| 103 | + DROUND(6); |
| 104 | + DROUND(7); |
| 105 | + DROUND(8); |
| 106 | + DROUND(9); |
| 107 | + DROUND(10); |
| 108 | + DROUND(11); |
| 109 | + DROUND(12); |
| 110 | + DROUND(13); |
| 111 | + DROUND(14); |
| 112 | + DROUND(15); |
| 113 | + DROUND(16); |
| 114 | +} |
| 115 | + |
| 116 | +#endif /* LTC_SOBER128_STREAM_SETUP || LTC_SOBER128_STREAM_SETIV */ |
| 117 | + |
| 118 | + |
| 119 | +#endif /* LTC_SOBER128 */ |
| 120 | + |
| 121 | +/* ref: $Format:%D$ */ |
| 122 | +/* git commit: $Format:%H$ */ |
| 123 | +/* commit time: $Format:%ai$ */ |
0 commit comments