1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 
4 /* The implementation is based on:
5  * "Extending the Salsa20 nonce", https://cr.yp.to/snuffle/xsalsa-20081128.pdf
6  * "Salsa20 specification", http://cr.yp.to/snuffle/spec.pdf
7  * and salsa20-ref.c version 20051118
8  * Public domain from D. J. Bernstein
9  */
10 
11 #include "tomcrypt.h"
12 
13 #ifdef LTC_XSALSA20
14 
15 static const char * const constants = "expand 32-byte k";
16 
17 #define QUARTERROUND(a,b,c,d) \
18     x[b] ^= (ROL((x[a] + x[d]),  7)); \
19     x[c] ^= (ROL((x[b] + x[a]),  9)); \
20     x[d] ^= (ROL((x[c] + x[b]), 13)); \
21     x[a] ^= (ROL((x[d] + x[c]), 18));
22 
23 /* use modified salsa20 doubleround (no final addition as in salsa20) */
s_xsalsa20_doubleround(ulong32 * x,int rounds)24 static void s_xsalsa20_doubleround(ulong32 *x, int rounds)
25 {
26    int i;
27 
28    for (i = rounds; i > 0; i -= 2) {
29       /* columnround */
30       QUARTERROUND( 0, 4, 8,12)
31       QUARTERROUND( 5, 9,13, 1)
32       QUARTERROUND(10,14, 2, 6)
33       QUARTERROUND(15, 3, 7,11)
34       /* rowround */
35       QUARTERROUND( 0, 1, 2, 3)
36       QUARTERROUND( 5, 6, 7, 4)
37       QUARTERROUND(10,11, 8, 9)
38       QUARTERROUND(15,12,13,14)
39    }
40 }
41 
42 #undef QUARTERROUND
43 
44 /**
45    Initialize an XSalsa20 context
46    @param st        [out] The destination of the XSalsa20 state
47    @param key       The secret key
48    @param keylen    The length of the secret key, must be 32 (octets)
49    @param nonce     The nonce
50    @param noncelen  The length of the nonce, must be 24 (octets)
51    @param rounds    Number of rounds (must be evenly divisible by 2, default is 20)
52    @return CRYPT_OK if successful
53 */
xsalsa20_setup(salsa20_state * st,const unsigned char * key,unsigned long keylen,const unsigned char * nonce,unsigned long noncelen,int rounds)54 int xsalsa20_setup(salsa20_state *st, const unsigned char *key, unsigned long keylen,
55                                       const unsigned char *nonce, unsigned long noncelen,
56                                       int rounds)
57 {
58    const int sti[] = {0, 5, 10, 15, 6, 7, 8, 9};  /* indices used to build subkey fm x */
59    ulong32       x[64];                           /* input to & output fm doubleround */
60    unsigned char subkey[32];
61    int i;
62 
63    LTC_ARGCHK(st        != NULL);
64    LTC_ARGCHK(key       != NULL);
65    LTC_ARGCHK(keylen    == 32);
66    LTC_ARGCHK(nonce     != NULL);
67    LTC_ARGCHK(noncelen  == 24);
68    if (rounds == 0) rounds = 20;
69    LTC_ARGCHK(rounds % 2 == 0);     /* number of rounds must be evenly divisible by 2 */
70 
71    /* load the state to "hash" the key */
72    LOAD32L(x[ 0], constants +  0);
73    LOAD32L(x[ 5], constants +  4);
74    LOAD32L(x[10], constants +  8);
75    LOAD32L(x[15], constants + 12);
76    LOAD32L(x[ 1], key +  0);
77    LOAD32L(x[ 2], key +  4);
78    LOAD32L(x[ 3], key +  8);
79    LOAD32L(x[ 4], key + 12);
80    LOAD32L(x[11], key + 16);
81    LOAD32L(x[12], key + 20);
82    LOAD32L(x[13], key + 24);
83    LOAD32L(x[14], key + 28);
84    LOAD32L(x[ 6], nonce +  0);
85    LOAD32L(x[ 7], nonce +  4);
86    LOAD32L(x[ 8], nonce +  8);
87    LOAD32L(x[ 9], nonce + 12);
88 
89    /* use modified salsa20 doubleround (no final addition) */
90    s_xsalsa20_doubleround(x, rounds);
91 
92    /* extract the subkey */
93    for (i = 0; i < 8; ++i) {
94      STORE32L(x[sti[i]], subkey + 4 * i);
95    }
96 
97    /* load the final initial state */
98    LOAD32L(st->input[ 0], constants +  0);
99    LOAD32L(st->input[ 5], constants +  4);
100    LOAD32L(st->input[10], constants +  8);
101    LOAD32L(st->input[15], constants + 12);
102    LOAD32L(st->input[ 1], subkey +  0);
103    LOAD32L(st->input[ 2], subkey +  4);
104    LOAD32L(st->input[ 3], subkey +  8);
105    LOAD32L(st->input[ 4], subkey + 12);
106    LOAD32L(st->input[11], subkey + 16);
107    LOAD32L(st->input[12], subkey + 20);
108    LOAD32L(st->input[13], subkey + 24);
109    LOAD32L(st->input[14], subkey + 28);
110    LOAD32L(st->input[ 6], &(nonce[16]) + 0);
111    LOAD32L(st->input[ 7], &(nonce[16]) + 4);
112    st->input[ 8] = 0;
113    st->input[ 9] = 0;
114    st->rounds = rounds;
115    st->ksleft = 0;
116    st->ivlen  = 24;           /* set switch to say nonce/IV has been loaded */
117 
118 #ifdef LTC_CLEAN_STACK
119    zeromem(x, sizeof(x));
120    zeromem(subkey, sizeof(subkey));
121 #endif
122 
123    return CRYPT_OK;
124 }
125 
126 
127 #endif
128