CC26xx Driver Library
sw_chacha.c File Reference
#include "sw_ecrypt-sync.h"

Macros

#define ECRYPT_LITTLE_ENDIAN
 
#define ROTATE(v, c)   (ROTL32(v,c))
 
#define XOR(v, w)   ((v) ^ (w))
 
#define PLUS(v, w)   (U32V((v) + (w)))
 
#define PLUSONE(v)   (PLUS((v),1))
 
#define QUARTERROUND(a, b, c, d)
 

Functions

static void salsa20_wordtobyte (u8 output[64], const u32 input[16])
 
void ECRYPT_init (void)
 
void ECRYPT_keysetup (ECRYPT_ctx *x, const u8 *k, u32 kbits, u32 ivbits)
 
void ECRYPT_ivsetup (ECRYPT_ctx *x, const u8 *iv)
 
void ECRYPT_encrypt_bytes (ECRYPT_ctx *x, const u8 *m, u8 *c, u32 bytes)
 
void ECRYPT_decrypt_bytes (ECRYPT_ctx *x, const u8 *c, u8 *m, u32 bytes)
 
void ECRYPT_keystream_bytes (ECRYPT_ctx *x, u8 *stream, u32 bytes)
 

Variables

static const char sigma [16] = "expand 32-byte k"
 
static const char tau [16] = "expand 16-byte k"
 

Macro Definition Documentation

#define ECRYPT_LITTLE_ENDIAN
#define PLUS (   v,
 
)    (U32V((v) + (w)))

Referenced by salsa20_wordtobyte().

#define PLUSONE (   v)    (PLUS((v),1))

Referenced by ECRYPT_encrypt_bytes().

#define QUARTERROUND (   a,
  b,
  c,
 
)
Value:
x[a] = PLUS(x[a],x[b]); x[d] = ROTATE(XOR(x[d],x[a]),16); \
x[c] = PLUS(x[c],x[d]); x[b] = ROTATE(XOR(x[b],x[c]),12); \
x[a] = PLUS(x[a],x[b]); x[d] = ROTATE(XOR(x[d],x[a]), 8); \
x[c] = PLUS(x[c],x[d]); x[b] = ROTATE(XOR(x[b],x[c]), 7);
#define PLUS(v, w)
Definition: sw_chacha.c:18
#define XOR(v, w)
Definition: sw_chacha.c:17
#define ROTATE(v, c)
Definition: sw_chacha.c:16

Referenced by salsa20_wordtobyte().

#define ROTATE (   v,
 
)    (ROTL32(v,c))
#define XOR (   v,
 
)    ((v) ^ (w))

Function Documentation

void ECRYPT_decrypt_bytes ( ECRYPT_ctx x,
const u8 *  c,
u8 *  m,
u32  bytes 
)
112 {
113  ECRYPT_encrypt_bytes(x,c,m,bytes);
114 }
void ECRYPT_encrypt_bytes(ECRYPT_ctx *x, const u8 *m, u8 *c, u32 bytes)
Definition: sw_chacha.c:87

Here is the call graph for this function:

void ECRYPT_encrypt_bytes ( ECRYPT_ctx x,
const u8 *  m,
u8 *  c,
u32  bytes 
)

Referenced by ECRYPT_decrypt_bytes(), and ECRYPT_keystream_bytes().

88 {
89  u8 output[64];
90  int i;
91 
92  if (!bytes) return;
93  for (;;) {
94  salsa20_wordtobyte(output,x->input);
95  x->input[12] = PLUSONE(x->input[12]);
96  if (!x->input[12]) {
97  x->input[13] = PLUSONE(x->input[13]);
98  /* stopping at 2^70 bytes per nonce is user's responsibility */
99  }
100  if (bytes <= 64) {
101  for (i = 0;i < bytes;++i) c[i] = m[i] ^ output[i];
102  return;
103  }
104  for (i = 0;i < 64;++i) c[i] = m[i] ^ output[i];
105  bytes -= 64;
106  c += 64;
107  m += 64;
108  }
109 }
static void salsa20_wordtobyte(u8 output[64], const u32 input[16])
Definition: sw_chacha.c:27
#define PLUSONE(v)
Definition: sw_chacha.c:19
u32 input[16]
Definition: sw_ecrypt-sync.h:62

Here is the call graph for this function:

void ECRYPT_init ( void  )
48 {
49  return;
50 }
void ECRYPT_ivsetup ( ECRYPT_ctx x,
const u8 *  iv 
)
80 {
81  x->input[12] = 0;
82  x->input[13] = 0;
83  x->input[14] = U8TO32_LITTLE(iv + 0);
84  x->input[15] = U8TO32_LITTLE(iv + 4);
85 }
#define U8TO32_LITTLE(p)
Definition: sw_ecrypt-portable.h:188
u32 input[16]
Definition: sw_ecrypt-sync.h:62
void ECRYPT_keysetup ( ECRYPT_ctx x,
const u8 *  k,
u32  kbits,
u32  ivbits 
)
56 {
57  const char *constants;
58 
59  x->input[4] = U8TO32_LITTLE(k + 0);
60  x->input[5] = U8TO32_LITTLE(k + 4);
61  x->input[6] = U8TO32_LITTLE(k + 8);
62  x->input[7] = U8TO32_LITTLE(k + 12);
63  if (kbits == 256) { /* recommended */
64  k += 16;
65  constants = sigma;
66  } else { /* kbits == 128 */
67  constants = tau;
68  }
69  x->input[8] = U8TO32_LITTLE(k + 0);
70  x->input[9] = U8TO32_LITTLE(k + 4);
71  x->input[10] = U8TO32_LITTLE(k + 8);
72  x->input[11] = U8TO32_LITTLE(k + 12);
73  x->input[0] = U8TO32_LITTLE(constants + 0);
74  x->input[1] = U8TO32_LITTLE(constants + 4);
75  x->input[2] = U8TO32_LITTLE(constants + 8);
76  x->input[3] = U8TO32_LITTLE(constants + 12);
77 }
static const char tau[16]
Definition: sw_chacha.c:53
static const char sigma[16]
Definition: sw_chacha.c:52
#define U8TO32_LITTLE(p)
Definition: sw_ecrypt-portable.h:188
u32 input[16]
Definition: sw_ecrypt-sync.h:62
void ECRYPT_keystream_bytes ( ECRYPT_ctx x,
u8 *  stream,
u32  bytes 
)
117 {
118  u32 i;
119  for (i = 0;i < bytes;++i) stream[i] = 0;
120  ECRYPT_encrypt_bytes(x,stream,stream,bytes);
121 }
void ECRYPT_encrypt_bytes(ECRYPT_ctx *x, const u8 *m, u8 *c, u32 bytes)
Definition: sw_chacha.c:87

Here is the call graph for this function:

static void salsa20_wordtobyte ( u8  output[64],
const u32  input[16] 
)
static

Referenced by ECRYPT_encrypt_bytes().

28 {
29  u32 x[16];
30  int i;
31 
32  for (i = 0;i < 16;++i) x[i] = input[i];
33  for (i = 8;i > 0;i -= 2) {
34  QUARTERROUND( 0, 4, 8,12)
35  QUARTERROUND( 1, 5, 9,13)
36  QUARTERROUND( 2, 6,10,14)
37  QUARTERROUND( 3, 7,11,15)
38  QUARTERROUND( 0, 5,10,15)
39  QUARTERROUND( 1, 6,11,12)
40  QUARTERROUND( 2, 7, 8,13)
41  QUARTERROUND( 3, 4, 9,14)
42  }
43  for (i = 0;i < 16;++i) x[i] = PLUS(x[i],input[i]);
44  for (i = 0;i < 16;++i) U32TO8_LITTLE(output + 4 * i,x[i]);
45 }
#define U32TO8_LITTLE(p, v)
Definition: sw_ecrypt-portable.h:240
#define PLUS(v, w)
Definition: sw_chacha.c:18
#define QUARTERROUND(a, b, c, d)
Definition: sw_chacha.c:21

Variable Documentation

const char sigma[16] = "expand 32-byte k"
static

Referenced by ECRYPT_keysetup().

const char tau[16] = "expand 16-byte k"
static

Referenced by ECRYPT_keysetup().