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

§ ECRYPT_LITTLE_ENDIAN

#define ECRYPT_LITTLE_ENDIAN

§ PLUS

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

Referenced by salsa20_wordtobyte().

§ PLUSONE

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

Referenced by ECRYPT_encrypt_bytes().

§ QUARTERROUND

#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:16
#define XOR(v, w)
Definition: sw_chacha.c:15
#define ROTATE(v, c)
Definition: sw_chacha.c:14

Referenced by salsa20_wordtobyte().

§ ROTATE

#define ROTATE (   v,
 
)    (ROTL32(v,c))

§ XOR

#define XOR (   v,
 
)    ((v) ^ (w))

Function Documentation

§ ECRYPT_decrypt_bytes()

void ECRYPT_decrypt_bytes ( ECRYPT_ctx x,
const u8 *  c,
u8 *  m,
u32  bytes 
)
110 {
111  ECRYPT_encrypt_bytes(x,c,m,bytes);
112 }
void ECRYPT_encrypt_bytes(ECRYPT_ctx *x, const u8 *m, u8 *c, u32 bytes)
Definition: sw_chacha.c:85
Here is the call graph for this function:

§ ECRYPT_encrypt_bytes()

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

Referenced by ECRYPT_decrypt_bytes(), and ECRYPT_keystream_bytes().

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

§ ECRYPT_init()

void ECRYPT_init ( void  )
46 {
47  return;
48 }

§ ECRYPT_ivsetup()

void ECRYPT_ivsetup ( ECRYPT_ctx x,
const u8 *  iv 
)
78 {
79  x->input[12] = 0;
80  x->input[13] = 0;
81  x->input[14] = U8TO32_LITTLE(iv + 0);
82  x->input[15] = U8TO32_LITTLE(iv + 4);
83 }
#define U8TO32_LITTLE(p)
Definition: sw_ecrypt-portable.h:186
u32 input[16]
Definition: sw_ecrypt-sync.h:60

§ ECRYPT_keysetup()

void ECRYPT_keysetup ( ECRYPT_ctx x,
const u8 *  k,
u32  kbits,
u32  ivbits 
)
54 {
55  const char *constants;
56 
57  x->input[4] = U8TO32_LITTLE(k + 0);
58  x->input[5] = U8TO32_LITTLE(k + 4);
59  x->input[6] = U8TO32_LITTLE(k + 8);
60  x->input[7] = U8TO32_LITTLE(k + 12);
61  if (kbits == 256) { /* recommended */
62  k += 16;
63  constants = sigma;
64  } else { /* kbits == 128 */
65  constants = tau;
66  }
67  x->input[8] = U8TO32_LITTLE(k + 0);
68  x->input[9] = U8TO32_LITTLE(k + 4);
69  x->input[10] = U8TO32_LITTLE(k + 8);
70  x->input[11] = U8TO32_LITTLE(k + 12);
71  x->input[0] = U8TO32_LITTLE(constants + 0);
72  x->input[1] = U8TO32_LITTLE(constants + 4);
73  x->input[2] = U8TO32_LITTLE(constants + 8);
74  x->input[3] = U8TO32_LITTLE(constants + 12);
75 }
static const char tau[16]
Definition: sw_chacha.c:51
static const char sigma[16]
Definition: sw_chacha.c:50
#define U8TO32_LITTLE(p)
Definition: sw_ecrypt-portable.h:186
u32 input[16]
Definition: sw_ecrypt-sync.h:60

§ ECRYPT_keystream_bytes()

void ECRYPT_keystream_bytes ( ECRYPT_ctx x,
u8 *  stream,
u32  bytes 
)
115 {
116  u32 i;
117  for (i = 0;i < bytes;++i) stream[i] = 0;
118  ECRYPT_encrypt_bytes(x,stream,stream,bytes);
119 }
void ECRYPT_encrypt_bytes(ECRYPT_ctx *x, const u8 *m, u8 *c, u32 bytes)
Definition: sw_chacha.c:85
Here is the call graph for this function:

§ salsa20_wordtobyte()

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

Referenced by ECRYPT_encrypt_bytes().

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

Variable Documentation

§ sigma

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

Referenced by ECRYPT_keysetup().

§ tau

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

Referenced by ECRYPT_keysetup().