Skip to content

Commit 8d0c2cb

Browse files
committed
functions in separate files
1 parent cffd5ae commit 8d0c2cb

5 files changed

+433
-1
lines changed

helper.pl

-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ sub check_source {
6262
$file !~ m|src/ciphers/.*\.c$| &&
6363
$file !~ m|src/hashes/.*\.c$| &&
6464
$file !~ m|src/math/.+_desc.c$| &&
65-
$file !~ m|src/stream/sober128/sober128_stream.c$| &&
6665
$l =~ /^static(\s+[a-zA-Z0-9_]+)+\s+([^_][a-zA-Z0-9_]+)\s*\(/) {
6766
push @{$troubles->{staticfunc_name}}, "$lineno($2)";
6867
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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$ */
+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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+
#include "tomcrypt_private.h"
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+
#define __LTC_SOBER128TAB_C__
20+
#include "sober128tab.c"
21+
22+
#include "sober128_stream_common.h"
23+
24+
25+
static void _xorword(ulong32 w, const unsigned char *in, unsigned char *out)
26+
{
27+
ulong32 t;
28+
LOAD32L(t, in);
29+
t ^= w;
30+
STORE32L(t, out);
31+
}
32+
33+
34+
/* XOR pseudo-random bytes into buffer
35+
*/
36+
#define SROUND(z) STEP(st->R,z); NLFUNC(st,(z+1)); _xorword(t, in+(z*4), out+(z*4));
37+
38+
/**
39+
Encrypt (or decrypt) bytes of ciphertext (or plaintext) with Sober128
40+
@param st The Sober128 state
41+
@param in The plaintext (or ciphertext)
42+
@param inlen The length of the input (octets)
43+
@param out [out] The ciphertext (or plaintext), length inlen
44+
@return CRYPT_OK if successful
45+
*/
46+
int sober128_stream_crypt(sober128_state *st, const unsigned char *in, unsigned long inlen, unsigned char *out)
47+
{
48+
ulong32 t;
49+
50+
if (inlen == 0) return CRYPT_OK; /* nothing to do */
51+
LTC_ARGCHK(out != NULL);
52+
LTC_ARGCHK(st != NULL);
53+
54+
/* handle any previously buffered bytes */
55+
while (st->nbuf != 0 && inlen != 0) {
56+
*out++ = *in++ ^ (unsigned char)(st->sbuf & 0xFF);
57+
st->sbuf >>= 8;
58+
st->nbuf -= 8;
59+
--inlen;
60+
}
61+
62+
#ifndef LTC_SMALL_CODE
63+
/* do lots at a time, if there's enough to do */
64+
while (inlen >= N*4) {
65+
SROUND(0);
66+
SROUND(1);
67+
SROUND(2);
68+
SROUND(3);
69+
SROUND(4);
70+
SROUND(5);
71+
SROUND(6);
72+
SROUND(7);
73+
SROUND(8);
74+
SROUND(9);
75+
SROUND(10);
76+
SROUND(11);
77+
SROUND(12);
78+
SROUND(13);
79+
SROUND(14);
80+
SROUND(15);
81+
SROUND(16);
82+
out += 4*N;
83+
in += 4*N;
84+
inlen -= 4*N;
85+
}
86+
#endif
87+
88+
/* do small or odd size buffers the slow way */
89+
while (4 <= inlen) {
90+
_cycle(st->R);
91+
t = _nltap(st);
92+
_xorword(t, in, out);
93+
out += 4;
94+
in += 4;
95+
inlen -= 4;
96+
}
97+
98+
/* handle any trailing bytes */
99+
if (inlen != 0) {
100+
_cycle(st->R);
101+
st->sbuf = _nltap(st);
102+
st->nbuf = 32;
103+
while (st->nbuf != 0 && inlen != 0) {
104+
*out++ = *in++ ^ (unsigned char)(st->sbuf & 0xFF);
105+
st->sbuf >>= 8;
106+
st->nbuf -= 8;
107+
--inlen;
108+
}
109+
}
110+
111+
return CRYPT_OK;
112+
}
113+
114+
115+
#endif
116+
117+
/* ref: $Format:%D$ */
118+
/* git commit: $Format:%H$ */
119+
/* commit time: $Format:%ai$ */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
#include "tomcrypt_private.h"
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+
#define __LTC_SOBER128TAB_C__
20+
#include "sober128tab.c"
21+
22+
#define LTC_SOBER128_STREAM_SETIV
23+
#include "sober128_stream_common.h"
24+
#undef LTC_SOBER128_STREAM_SETIV
25+
26+
27+
/* initialise to previously saved register state
28+
*/
29+
static void _s128_reloadstate(sober128_state *st)
30+
{
31+
int i;
32+
33+
for (i = 0; i < N; ++i) {
34+
st->R[i] = st->initR[i];
35+
}
36+
}
37+
38+
39+
/**
40+
Set IV to the Sober128 state
41+
@param st The Sober12820 state
42+
@param iv The IV data to add
43+
@param ivlen The length of the IV (must be 12)
44+
@return CRYPT_OK on success
45+
*/
46+
int sober128_stream_setiv(sober128_state *st, const unsigned char *iv, unsigned long ivlen)
47+
{
48+
ulong32 i, k;
49+
50+
LTC_ARGCHK(st != NULL);
51+
LTC_ARGCHK(iv != NULL);
52+
LTC_ARGCHK(ivlen > 0);
53+
54+
/* ok we are adding an IV then... */
55+
_s128_reloadstate(st);
56+
57+
/* ivlen must be multiple of 4 bytes */
58+
if ((ivlen & 3) != 0) {
59+
return CRYPT_INVALID_KEYSIZE;
60+
}
61+
62+
for (i = 0; i < ivlen; i += 4) {
63+
LOAD32L(k, (unsigned char *)&iv[i]);
64+
ADDKEY(k);
65+
_cycle(st->R);
66+
XORNL(_nltap(st));
67+
}
68+
69+
/* also fold in the length of the key */
70+
ADDKEY(ivlen);
71+
72+
/* now diffuse */
73+
_s128_diffuse(st);
74+
st->nbuf = 0;
75+
76+
return CRYPT_OK;
77+
}
78+
79+
80+
#endif
81+
82+
83+
/* ref: $Format:%D$ */
84+
/* git commit: $Format:%H$ */
85+
/* commit time: $Format:%ai$ */

0 commit comments

Comments
 (0)