zuc.c File Reference

ZUC stream cipher (ZUC-128 and ZUC-256) More...

#include "core/crypto.h"
#include "cipher/zuc.h"

Go to the source code of this file.

Macros

#define TRACE_LEVEL   CRYPTO_TRACE_LEVEL
 
#define ADD31(x, a, b)
 
#define MUL31(x, a, n)
 
#define SHIFT496(s, s16)
 
#define LFSR_WITH_INIT_MODE(s, u)
 
#define LFSR_WITH_WORKING_MODE(s)
 
#define BIT_REORGANIZATION(x0, x1, x2, x3, s)
 
#define S(x)
 
#define L1(x)   ((x) ^ ROL32(x, 2) ^ ROL32(x, 10) ^ ROL32(x, 18) ^ ROL32(x, 24))
 
#define L2(x)   ((x) ^ ROL32(x, 8) ^ ROL32(x, 14) ^ ROL32(x, 22) ^ ROL32(x, 30))
 
#define F(w, x0, x1, x2, r1, r2)
 
#define LOAD1(a, b, c)
 
#define LOAD2(a, b, c, d)
 

Functions

error_t zucInit (ZucContext *context, const uint8_t *key, size_t keyLen, const uint8_t *iv, size_t ivLen)
 Initialize ZUC context using the supplied key and IV. More...
 
void zucGenerateKeyStream (ZucContext *context, uint32_t *output, size_t length)
 Generate key stream. More...
 
void zucCipher (ZucContext *context, const uint8_t *input, uint8_t *output, size_t length)
 Encrypt/decrypt data with the ZUC algorithm. More...
 
void zucDeinit (ZucContext *context)
 Release ZUC context. More...
 

Detailed Description

ZUC stream cipher (ZUC-128 and ZUC-256)

License

SPDX-License-Identifier: GPL-2.0-or-later

Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.

This file is part of CycloneCRYPTO Open.

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

Description

ZUC-128 is a word-oriented stream cipher. It takes a 128-bit initial key and a 128-bit initialization vector (IV) as input, and outputs a key stream of 32-bit words. This key stream can be used for encryption/decryption. ZUC-256 is the successor of ZUC-128. It works with a 256-bit key and a 128 or 184-bit initialization vector (IV)

Author
Oryx Embedded SARL (www.oryx-embedded.com)
Version
2.4.4

Definition in file zuc.c.

Macro Definition Documentation

◆ ADD31

#define ADD31 (   x,
  a,
  b 
)
Value:
{ \
x = (a) + (b); \
x = (x) + ((x) >> 31); \
x &= 0x7FFFFFFF; \
}

Definition at line 50 of file zuc.c.

◆ BIT_REORGANIZATION

#define BIT_REORGANIZATION (   x0,
  x1,
  x2,
  x3,
  s 
)
Value:
{ \
x0 = ((s[15] << 1) & 0xFFFF0000) | (s[14] & 0xFFFF); \
x1 = ((s[11] << 16) & 0xFFFF0000) | ((s[9] >> 15) & 0xFFFF); \
x2 = ((s[7] << 16) & 0xFFFF0000) | ((s[5] >> 15) & 0xFFFF); \
x3 = ((s[2] << 16) & 0xFFFF0000) | ((s[0] >> 15) & 0xFFFF); \
}

Definition at line 125 of file zuc.c.

◆ F

#define F (   w,
  x0,
  x1,
  x2,
  r1,
  r2 
)
Value:
{ \
uint32_t w1; \
uint32_t w2; \
uint32_t temp; \
w = ((x0) ^ (r1)) + (r2); \
w1 = (r1) + (x1); \
w2 = (r2) ^ (x2); \
temp = (w1 << 16) | (w2 >> 16); \
temp = L1(temp); \
r1 = S(temp); \
temp = (w2 << 16) | (w1 >> 16); \
temp = L2(temp); \
r2 = S(temp); \
}

Definition at line 144 of file zuc.c.

◆ L1

#define L1 (   x)    ((x) ^ ROL32(x, 2) ^ ROL32(x, 10) ^ ROL32(x, 18) ^ ROL32(x, 24))

Definition at line 140 of file zuc.c.

◆ L2

#define L2 (   x)    ((x) ^ ROL32(x, 8) ^ ROL32(x, 14) ^ ROL32(x, 22) ^ ROL32(x, 30))

Definition at line 141 of file zuc.c.

◆ LFSR_WITH_INIT_MODE

#define LFSR_WITH_INIT_MODE (   s,
  u 
)
Value:
{ \
uint32_t s16; \
uint32_t temp; \
s16 = s[0]; \
MUL31(temp, s[0], 8); \
ADD31(s16, s16, temp); \
MUL31(temp, s[4], 20); \
ADD31(s16, s16, temp); \
MUL31(temp, s[10], 21); \
ADD31(s16, s16, temp); \
MUL31(temp, s[13], 17); \
ADD31(s16, s16, temp); \
MUL31(temp, s[15], 15); \
ADD31(s16, s16, temp); \
ADD31(s16, s16, u); \
SHIFT496(s, s16); \
}

Definition at line 86 of file zuc.c.

◆ LFSR_WITH_WORKING_MODE

#define LFSR_WITH_WORKING_MODE (   s)
Value:
{ \
uint32_t s16; \
uint32_t temp; \
s16 = s[0]; \
MUL31(temp, s[0], 8); \
ADD31(s16, s16, temp); \
MUL31(temp, s[4], 20); \
ADD31(s16, s16, temp); \
MUL31(temp, s[10], 21); \
ADD31(s16, s16, temp); \
MUL31(temp, s[13], 17); \
ADD31(s16, s16, temp); \
MUL31(temp, s[15], 15); \
ADD31(s16, s16, temp); \
SHIFT496(s, s16); \
}

Definition at line 106 of file zuc.c.

◆ LOAD1

#define LOAD1 (   a,
  b,
  c 
)
Value:
(((uint32_t) (a) << 23) | \
((uint32_t) (b) << 8) | (uint32_t) (c))

Definition at line 161 of file zuc.c.

◆ LOAD2

#define LOAD2 (   a,
  b,
  c,
 
)
Value:
(((uint32_t) (a) << 23) | \
((uint32_t) ((b) & 0x7F) << 16) | \
((uint32_t) (c) << 8) | (uint32_t) (d))

Definition at line 165 of file zuc.c.

◆ MUL31

#define MUL31 (   x,
  a,
  n 
)
Value:
{ \
x = ((a) << (n)) | ((a) >> (31 - (n))); \
x &= 0x7FFFFFFF; \
}

Definition at line 58 of file zuc.c.

◆ S

#define S (   x)
Value:
((uint32_t) s1[(x) & 0xFF] | \
((uint32_t) s0[((x) >> 8) & 0xFF] << 8) | \
((uint32_t) s1[((x) >> 16) & 0xFF] << 16) | \
((uint32_t) s0[((x) >> 24) & 0xFF] << 24))

Definition at line 134 of file zuc.c.

◆ SHIFT496

#define SHIFT496 (   s,
  s16 
)
Value:
{ \
s[0] = s[1]; \
s[1] = s[2]; \
s[2] = s[3]; \
s[3] = s[4]; \
s[4] = s[5]; \
s[5] = s[6]; \
s[6] = s[7]; \
s[7] = s[8]; \
s[8] = s[9]; \
s[9] = s[10]; \
s[10] = s[11]; \
s[11] = s[12]; \
s[12] = s[13]; \
s[13] = s[14]; \
s[14] = s[15]; \
s[15] = s16; \
}

Definition at line 65 of file zuc.c.

◆ TRACE_LEVEL

#define TRACE_LEVEL   CRYPTO_TRACE_LEVEL

Definition at line 40 of file zuc.c.

Function Documentation

◆ zucCipher()

void zucCipher ( ZucContext context,
const uint8_t *  input,
uint8_t *  output,
size_t  length 
)

Encrypt/decrypt data with the ZUC algorithm.

Parameters
[in]contextPointer to the ZUC context
[in]inputPointer to the data to encrypt/decrypt (optional)
[in]outputPointer to the resulting data (optional)
[in]lengthLength of the input data

Definition at line 395 of file zuc.c.

◆ zucDeinit()

void zucDeinit ( ZucContext context)

Release ZUC context.

Parameters
[in]contextPointer to the ZUC context

Definition at line 438 of file zuc.c.

◆ zucGenerateKeyStream()

void zucGenerateKeyStream ( ZucContext context,
uint32_t *  output,
size_t  length 
)

Generate key stream.

Parameters
[in]contextPointer to the ZUC context
[in]outputPointer to the resulting key stream (optional)
[in]lengthNumber of 32-bit words to be generate

Definition at line 358 of file zuc.c.

◆ zucInit()

error_t zucInit ( ZucContext context,
const uint8_t *  key,
size_t  keyLen,
const uint8_t *  iv,
size_t  ivLen 
)

Initialize ZUC context using the supplied key and IV.

Parameters
[in]contextPointer to the ZUC context to initialize
[in]keyPointer to the key
[in]keyLenLength of the key
[in]ivPointer to the initialization vector
[in]ivLenLength of the initialization vector
Returns
Error code

Definition at line 241 of file zuc.c.

uint8_t b
Definition: nbns_common.h:104
uint8_t a
Definition: ndp.h:411
#define L1(x)
Definition: zuc.c:140
uint8_t x
Definition: lldp_ext_med.h:211
#define L2(x)
Definition: zuc.c:141
uint8_t u
Definition: lldp_ext_med.h:213
uint8_t n
uint8_t s
Definition: igmp_common.h:234
#define S(x)
Definition: zuc.c:134
uint8_t c
Definition: ndp.h:514