serpent.c File Reference

Serpent encryption algorithm. More...

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

Go to the source code of this file.

Macros

#define TRACE_LEVEL   CRYPTO_TRACE_LEVEL
 
#define PHI   0x9E3779B9
 
#define SBOX0(r0, r1, r2, r3)
 
#define SBOX0_INV(r0, r1, r2, r3)
 
#define SBOX1(r0, r1, r2, r3)
 
#define SBOX1_INV(r0, r1, r2, r3)
 
#define SBOX2(r0, r1, r2, r3)
 
#define SBOX2_INV(r0, r1, r2, r3)
 
#define SBOX3(r0, r1, r2, r3)
 
#define SBOX3_INV(r0, r1, r2, r3)
 
#define SBOX4(r0, r1, r2, r3)
 
#define SBOX4_INV(r0, r1, r2, r3)
 
#define SBOX5(r0, r1, r2, r3)
 
#define SBOX5_INV(r0, r1, r2, r3)
 
#define SBOX6(r0, r1, r2, r3)
 
#define SBOX6_INV(r0, r1, r2, r3)
 
#define SBOX7(r0, r1, r2, r3)
 
#define SBOX7_INV(r0, r1, r2, r3)
 
#define LT(x0, x1, x2, x3)
 
#define LT_INV(x0, x1, x2, x3)
 
#define XOR(x0, x1, x2, x3, k)
 
#define ROUND(n, x0, x1, x2, x3, k)
 
#define ROUND_INV(n, x0, x1, x2, x3, k)
 

Functions

error_t serpentInit (SerpentContext *context, const uint8_t *key, size_t keyLen)
 Key expansion. More...
 
void serpentEncryptBlock (SerpentContext *context, const uint8_t *input, uint8_t *output)
 Encrypt a 16-byte block using Serpent algorithm. More...
 
void serpentDecryptBlock (SerpentContext *context, const uint8_t *input, uint8_t *output)
 Decrypt a 16-byte block using Serpent algorithm. More...
 
void serpentDeinit (SerpentContext *context)
 Release Serpent context. More...
 

Variables

const CipherAlgo serpentCipherAlgo
 

Detailed Description

Serpent encryption algorithm.

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

Serpent is a block cipher algorithm which supports a key size of 128, 192 or 256 bits. S-box functions are implemented as per Dag Arne Osvik's paper "Speeding up Serpent"

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

Definition in file serpent.c.

Macro Definition Documentation

◆ LT

#define LT (   x0,
  x1,
  x2,
  x3 
)
Value:
{ \
x0 = ROL32(x0, 13); \
x2 = ROL32(x2, 3); \
x1 ^= x0 ^ x2; \
x3 ^= x2 ^ (x0 << 3); \
x1 = ROL32(x1, 1); \
x3 = ROL32(x3, 7); \
x0 ^= x1 ^ x3; \
x2 ^= x3 ^ (x1 << 7); \
x0 = ROL32(x0, 5); \
x2 = ROL32(x2, 22); \
}
#define ROL32(a, n)
Definition: crypto.h:776

Definition at line 324 of file serpent.c.

◆ LT_INV

#define LT_INV (   x0,
  x1,
  x2,
  x3 
)
Value:
{ \
x2 = ROR32(x2, 22); \
x0 = ROR32(x0, 5); \
x2 ^= x3 ^ (x1 << 7); \
x0 ^= x1 ^ x3; \
x3 = ROR32(x3, 7); \
x1 = ROR32(x1, 1); \
x3 ^= x2 ^ (x0 << 3); \
x1 ^= x0 ^ x2; \
x2 = ROR32(x2, 3); \
x0 = ROR32(x0, 13); \
}
#define ROR32(a, n)
Definition: crypto.h:782

Definition at line 339 of file serpent.c.

◆ PHI

#define PHI   0x9E3779B9

Definition at line 48 of file serpent.c.

◆ ROUND

#define ROUND (   n,
  x0,
  x1,
  x2,
  x3,
 
)
Value:
{ \
XOR(x0, x1, x2, x3, k); \
SBOX##n(x0, x1, x2, x3); \
LT(x0, x1, x2, x3); \
}
uint8_t n

Definition at line 363 of file serpent.c.

◆ ROUND_INV

#define ROUND_INV (   n,
  x0,
  x1,
  x2,
  x3,
 
)
Value:
{ \
LT_INV(x0, x1, x2, x3); \
SBOX##n##_INV(x0, x1, x2, x3); \
XOR(x0, x1, x2, x3, k); \
}

Definition at line 371 of file serpent.c.

◆ SBOX0

#define SBOX0 (   r0,
  r1,
  r2,
  r3 
)
Value:
{ \
uint32_t r4; \
r3 ^= r0; r4 = r1; \
r1 &= r3; r4 ^= r2; \
r1 ^= r0; r0 |= r3; \
r0 ^= r4; r4 ^= r3; \
r3 ^= r2; r2 |= r1; \
r2 ^= r4; r4 = ~r4; \
r4 |= r1; r1 ^= r3; \
r1 ^= r4; r3 |= r0; \
r1 ^= r3; r4 ^= r3; \
r3 = r0; r0 = r1; r1 = r4; \
}

Definition at line 51 of file serpent.c.

◆ SBOX0_INV

#define SBOX0_INV (   r0,
  r1,
  r2,
  r3 
)
Value:
{ \
uint32_t r4; \
r2 = ~r2; r4 = r1; \
r1 |= r0; r4 = ~r4; \
r1 ^= r2; r2 |= r4; \
r1 ^= r3; r0 ^= r4; \
r2 ^= r0; r0 &= r3; \
r4 ^= r0; r0 |= r1; \
r0 ^= r2; r3 ^= r4; \
r2 ^= r1; r3 ^= r0; \
r3 ^= r1; \
r2 &= r3; \
r4 ^= r2; \
r2 = r1; r1 = r4; \
}

Definition at line 67 of file serpent.c.

◆ SBOX1

#define SBOX1 (   r0,
  r1,
  r2,
  r3 
)
Value:
{ \
uint32_t r4; \
r0 = ~r0; r2 = ~r2; \
r4 = r0; r0 &= r1; \
r2 ^= r0; r0 |= r3; \
r3 ^= r2; r1 ^= r0; \
r0 ^= r4; r4 |= r1; \
r1 ^= r3; r2 |= r0; \
r2 &= r4; r0 ^= r1; \
r1 &= r2; \
r1 ^= r0; r0 &= r2; \
r0 ^= r4; \
r4 = r0; r0 = r2; r2 = r3; r3 = r1; r1 = r4; \
}

Definition at line 85 of file serpent.c.

◆ SBOX1_INV

#define SBOX1_INV (   r0,
  r1,
  r2,
  r3 
)
Value:
{ \
uint32_t r4; \
r4 = r1; r1 ^= r3; \
r3 &= r1; r4 ^= r2; \
r3 ^= r0; r0 |= r1; \
r2 ^= r3; r0 ^= r4; \
r0 |= r2; r1 ^= r3; \
r0 ^= r1; r1 |= r3; \
r1 ^= r0; r4 = ~r4; \
r4 ^= r1; r1 |= r0; \
r1 ^= r0; \
r1 |= r4; \
r3 ^= r1; \
r1 = r0; r0 = r4; r4 = r2; r2 = r3; r3 = r4; \
}

Definition at line 102 of file serpent.c.

◆ SBOX2

#define SBOX2 (   r0,
  r1,
  r2,
  r3 
)
Value:
{ \
uint32_t r4; \
r4 = r0; r0 &= r2; \
r0 ^= r3; r2 ^= r1; \
r2 ^= r0; r3 |= r4; \
r3 ^= r1; r4 ^= r2; \
r1 = r3; r3 |= r4; \
r3 ^= r0; r0 &= r1; \
r4 ^= r0; r1 ^= r3; \
r1 ^= r4; r4 = ~r4; \
r0 = r2; r2 = r1; r1 = r3; r3 = r4; \
}

Definition at line 120 of file serpent.c.

◆ SBOX2_INV

#define SBOX2_INV (   r0,
  r1,
  r2,
  r3 
)
Value:
{ \
uint32_t r4; \
r2 ^= r3; r3 ^= r0; \
r4 = r3; r3 &= r2; \
r3 ^= r1; r1 |= r2; \
r1 ^= r4; r4 &= r3; \
r2 ^= r3; r4 &= r0; \
r4 ^= r2; r2 &= r1; \
r2 |= r0; r3 = ~r3; \
r2 ^= r3; r0 ^= r3; \
r0 &= r1; r3 ^= r4; \
r3 ^= r0; \
r0 = r1; r1 = r4; \
}

Definition at line 135 of file serpent.c.

◆ SBOX3

#define SBOX3 (   r0,
  r1,
  r2,
  r3 
)
Value:
{ \
uint32_t r4; \
r4 = r0; r0 |= r3; \
r3 ^= r1; r1 &= r4; \
r4 ^= r2; r2 ^= r3; \
r3 &= r0; r4 |= r1; \
r3 ^= r4; r0 ^= r1; \
r4 &= r0; r1 ^= r3; \
r4 ^= r2; r1 |= r0; \
r1 ^= r2; r0 ^= r3; \
r2 = r1; r1 |= r3; \
r1 ^= r0; \
r0 = r1; r1 = r2; r2 = r3; r3 = r4; \
}

Definition at line 152 of file serpent.c.

◆ SBOX3_INV

#define SBOX3_INV (   r0,
  r1,
  r2,
  r3 
)
Value:
{ \
uint32_t r4; \
r4 = r2; r2 ^= r1; \
r0 ^= r2; r4 &= r2; \
r4 ^= r0; r0 &= r1; \
r1 ^= r3; r3 |= r4; \
r2 ^= r3; r0 ^= r3; \
r1 ^= r4; r3 &= r2; \
r3 ^= r1; r1 ^= r0; \
r1 |= r2; r0 ^= r3; \
r1 ^= r4; \
r0 ^= r1; \
r4 = r0; r0 = r2; r2 = r3; r3 = r4; \
}

Definition at line 169 of file serpent.c.

◆ SBOX4

#define SBOX4 (   r0,
  r1,
  r2,
  r3 
)
Value:
{ \
uint32_t r4; \
r1 ^= r3; r3 = ~r3; \
r2 ^= r3; r3 ^= r0; \
r4 = r1; r1 &= r3; \
r1 ^= r2; r4 ^= r3; \
r0 ^= r4; r2 &= r4; \
r2 ^= r0; r0 &= r1; \
r3 ^= r0; r4 |= r1; \
r4 ^= r0; r0 |= r3; \
r0 ^= r2; r2 &= r3; \
r0 = ~r0; r4 ^= r2; \
r2 = r0; r0 = r1; r1 = r4; \
}

Definition at line 186 of file serpent.c.

◆ SBOX4_INV

#define SBOX4_INV (   r0,
  r1,
  r2,
  r3 
)
Value:
{ \
uint32_t r4; \
r4 = r2; r2 &= r3; \
r2 ^= r1; r1 |= r3; \
r1 &= r0; r4 ^= r2; \
r4 ^= r1; r1 &= r2; \
r0 = ~r0; r3 ^= r4; \
r1 ^= r3; r3 &= r0; \
r3 ^= r2; r0 ^= r1; \
r2 &= r0; r3 ^= r0; \
r2 ^= r4; \
r2 |= r3; r3 ^= r0; \
r2 ^= r1; \
r1 = r3; r3 = r4; \
}

Definition at line 204 of file serpent.c.

◆ SBOX5

#define SBOX5 (   r0,
  r1,
  r2,
  r3 
)
Value:
{ \
uint32_t r4; \
r0 ^= r1; r1 ^= r3; \
r3 = ~r3; r4 = r1; \
r1 &= r0; r2 ^= r3; \
r1 ^= r2; r2 |= r4; \
r4 ^= r3; r3 &= r1; \
r3 ^= r0; r4 ^= r1; \
r4 ^= r2; r2 ^= r0; \
r0 &= r3; r2 = ~r2; \
r0 ^= r4; r4 |= r3; \
r2 ^= r4; \
r4 = r0; r0 = r1; r1 = r3; r3 = r2; r2 = r4; \
}

Definition at line 222 of file serpent.c.

◆ SBOX5_INV

#define SBOX5_INV (   r0,
  r1,
  r2,
  r3 
)
Value:
{ \
uint32_t r4; \
r1 = ~r1; r4 = r3; \
r2 ^= r1; r3 |= r0; \
r3 ^= r2; r2 |= r1; \
r2 &= r0; r4 ^= r3; \
r2 ^= r4; r4 |= r0; \
r4 ^= r1; r1 &= r2; \
r1 ^= r3; r4 ^= r2; \
r3 &= r4; r4 ^= r1; \
r3 ^= r4; r4 = ~r4; \
r3 ^= r0; \
r0 = r1; r1 = r4; r4 = r2; r2 = r3; r3 = r4; \
}

Definition at line 239 of file serpent.c.

◆ SBOX6

#define SBOX6 (   r0,
  r1,
  r2,
  r3 
)
Value:
{ \
uint32_t r4; \
r2 = ~r2; r4 = r3; \
r3 &= r0; r0 ^= r4; \
r3 ^= r2; r2 |= r4; \
r1 ^= r3; r2 ^= r0; \
r0 |= r1; r2 ^= r1; \
r4 ^= r0; r0 |= r3; \
r0 ^= r2; r4 ^= r3; \
r4 ^= r0; r3 = ~r3; \
r2 &= r4; \
r2 ^= r3; \
r3 = r2; r2 = r4; \
}

Definition at line 256 of file serpent.c.

◆ SBOX6_INV

#define SBOX6_INV (   r0,
  r1,
  r2,
  r3 
)
Value:
{ \
uint32_t r4; \
r0 ^= r2; r4 = r2; \
r2 &= r0; r4 ^= r3; \
r2 = ~r2; r3 ^= r1; \
r2 ^= r3; r4 |= r0; \
r0 ^= r2; r3 ^= r4; \
r4 ^= r1; r1 &= r3; \
r1 ^= r0; r0 ^= r3; \
r0 |= r2; r3 ^= r1; \
r4 ^= r0; \
r0 = r1; r1 = r2; r2 = r4; \
}

Definition at line 273 of file serpent.c.

◆ SBOX7

#define SBOX7 (   r0,
  r1,
  r2,
  r3 
)
Value:
{ \
uint32_t r4; \
r4 = r1; r1 |= r2; \
r1 ^= r3; r4 ^= r2; \
r2 ^= r1; r3 |= r4; \
r3 &= r0; r4 ^= r2; \
r3 ^= r1; r1 |= r4; \
r1 ^= r0; r0 |= r4; \
r0 ^= r2; r1 ^= r4; \
r2 ^= r1; r1 &= r0; \
r1 ^= r4; r2 = ~r2; \
r2 |= r0; \
r4 ^= r2; \
r2 = r1; r1 = r3; r3 = r0; r0 = r4; \
}

Definition at line 289 of file serpent.c.

◆ SBOX7_INV

#define SBOX7_INV (   r0,
  r1,
  r2,
  r3 
)
Value:
{ \
uint32_t r4; \
r4 = r2; r2 ^= r0; \
r0 &= r3; r4 |= r3; \
r2 = ~r2; r3 ^= r1; \
r1 |= r0; r0 ^= r2; \
r2 &= r4; r3 &= r4; \
r1 ^= r2; r2 ^= r0; \
r0 |= r2; r4 ^= r1; \
r0 ^= r3; r3 ^= r4; \
r4 |= r0; r3 ^= r2; \
r4 ^= r2; \
r2 = r1; r1 = r0; r0 = r3; r3 = r4; \
}

Definition at line 307 of file serpent.c.

◆ TRACE_LEVEL

#define TRACE_LEVEL   CRYPTO_TRACE_LEVEL

Definition at line 38 of file serpent.c.

◆ XOR

#define XOR (   x0,
  x1,
  x2,
  x3,
 
)
Value:
{ \
x0 ^= k[0]; \
x1 ^= k[1]; \
x2 ^= k[2]; \
x3 ^= k[3]; \
}

Definition at line 354 of file serpent.c.

Function Documentation

◆ serpentDecryptBlock()

void serpentDecryptBlock ( SerpentContext context,
const uint8_t *  input,
uint8_t *  output 
)

Decrypt a 16-byte block using Serpent algorithm.

Parameters
[in]contextPointer to the Serpent context
[in]inputCiphertext block to decrypt
[out]outputPlaintext block resulting from decryption

Definition at line 543 of file serpent.c.

◆ serpentDeinit()

void serpentDeinit ( SerpentContext context)

Release Serpent context.

Parameters
[in]contextPointer to the Serpent context

Definition at line 591 of file serpent.c.

◆ serpentEncryptBlock()

void serpentEncryptBlock ( SerpentContext context,
const uint8_t *  input,
uint8_t *  output 
)

Encrypt a 16-byte block using Serpent algorithm.

Parameters
[in]contextPointer to the Serpent context
[in]inputPlaintext block to encrypt
[out]outputCiphertext block resulting from encryption

Definition at line 495 of file serpent.c.

◆ serpentInit()

error_t serpentInit ( SerpentContext context,
const uint8_t *  key,
size_t  keyLen 
)

Key expansion.

Parameters
[in]contextPointer to the Serpent context to initialize
[in]keyPointer to the key
[in]keyLenLength of the key
Returns
Error code

Definition at line 402 of file serpent.c.

Variable Documentation

◆ serpentCipherAlgo

const CipherAlgo serpentCipherAlgo
Initial value:
=
{
"Serpent",
sizeof(SerpentContext),
NULL,
NULL,
}
void(* CipherAlgoDeinit)(void *context)
Definition: crypto.h:983
void(* CipherAlgoDecryptBlock)(void *context, const uint8_t *input, uint8_t *output)
Definition: crypto.h:980
error_t(* CipherAlgoInit)(void *context, const uint8_t *key, size_t keyLen)
Definition: crypto.h:968
void(* CipherAlgoEncryptBlock)(void *context, const uint8_t *input, uint8_t *output)
Definition: crypto.h:977
@ CIPHER_ALGO_TYPE_BLOCK
Definition: crypto.h:932
void serpentDeinit(SerpentContext *context)
Release Serpent context.
Definition: serpent.c:591
error_t serpentInit(SerpentContext *context, const uint8_t *key, size_t keyLen)
Key expansion.
Definition: serpent.c:402
void serpentEncryptBlock(SerpentContext *context, const uint8_t *input, uint8_t *output)
Encrypt a 16-byte block using Serpent algorithm.
Definition: serpent.c:495
void serpentDecryptBlock(SerpentContext *context, const uint8_t *input, uint8_t *output)
Decrypt a 16-byte block using Serpent algorithm.
Definition: serpent.c:543
#define SERPENT_BLOCK_SIZE
Definition: serpent.h:38
Serpent algorithm context.
Definition: serpent.h:53

Definition at line 379 of file serpent.c.