des.c File Reference

DES (Data Encryption Standard) More...

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

Go to the source code of this file.

Macros

#define TRACE_LEVEL   CRYPTO_TRACE_LEVEL
 
#define ROL28(a, n)   ((((a) << (n)) | ((a) >> (28 - (n)))) & 0x0FFFFFFF)
 
#define SWAPMOVE(a, b, n, m)
 
#define IP(l, r)
 
#define IP_INV(l, r)
 
#define PC1(c, d)
 
#define PC2_L(c, d)
 
#define PC2_R(c, d)
 
#define ROUND(l, r, k1, k2)
 

Functions

__weak_func error_t desInit (DesContext *context, const uint8_t *key, size_t keyLen)
 Initialize a DES context using the supplied key. More...
 
__weak_func void desEncryptBlock (DesContext *context, const uint8_t *input, uint8_t *output)
 Encrypt a 8-byte block using DES algorithm. More...
 
__weak_func void desDecryptBlock (DesContext *context, const uint8_t *input, uint8_t *output)
 Decrypt a 8-byte block using DES algorithm. More...
 
__weak_func void desDeinit (DesContext *context)
 Release DES context. More...
 

Variables

const uint8_t DES_CBC_OID [5] = {0x2B, 0x0E, 0x03, 0x02, 0x07}
 
const CipherAlgo desCipherAlgo
 

Detailed Description

DES (Data Encryption Standard)

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

DES is an encryption algorithm designed to encipher and decipher blocks of 64 bits under control of a 64-bit key. Refer to FIPS 46-3 for more details

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

Definition in file des.c.

Macro Definition Documentation

◆ IP

#define IP (   l,
  r 
)
Value:
{ \
SWAPMOVE(l, r, 4, 0x0F0F0F0F); \
SWAPMOVE(l, r, 16, 0x0000FFFF); \
SWAPMOVE(r, l, 2, 0x33333333); \
SWAPMOVE(r, l, 8, 0x00FF00FF); \
SWAPMOVE(l, r, 1, 0x55555555); \
l = ROL32(l, 1); \
r = ROL32(r, 1); \
}

Definition at line 58 of file des.c.

◆ IP_INV

#define IP_INV (   l,
  r 
)
Value:
{ \
l = ROR32(l, 1); \
r = ROR32(r, 1); \
SWAPMOVE(l, r, 1, 0x55555555); \
SWAPMOVE(r, l, 8, 0x00FF00FF); \
SWAPMOVE(r, l, 2, 0x33333333); \
SWAPMOVE(l, r, 16, 0x0000FFFF); \
SWAPMOVE(l, r, 4, 0x0F0F0F0F); \
}

Definition at line 70 of file des.c.

◆ PC1

#define PC1 (   c,
 
)
Value:
{ \
SWAPMOVE(c, d, 4, 0x0F0F0F0F); \
SWAPMOVE(c, d, 16, 0x0000FFFF); \
SWAPMOVE(d, c, 2, 0x33333333); \
SWAPMOVE(d, c, 8, 0x00FF00FF); \
SWAPMOVE(c, d, 1, 0x55555555); \
SWAPMOVE(d, c, 8, 0x00FF00FF); \
SWAPMOVE(c, d, 16, 0x0000FFFF); \
t = (c << 4) & 0x0FFFFFF0; \
t |= (d >> 24) & 0x0000000F; \
c = (d << 20) & 0x0FF00000; \
c |= (d << 4) & 0x000FF000; \
c |= (d >> 12) & 0x00000FF0; \
c |= (d >> 28) & 0x0000000F; \
d = t; \
}

Definition at line 82 of file des.c.

◆ PC2_L

#define PC2_L (   c,
 
)
Value:
(((c << 4) & 0x24000000) | \
((c << 28) & 0x10000000) | \
((c << 14) & 0x08000000) | \
((c << 18) & 0x02080000) | \
((c << 6) & 0x01000000) | \
((c << 9) & 0x00200000) | \
((c >> 1) & 0x00100000) | \
((c << 10) & 0x00040000) | \
((c << 2) & 0x00020000) | \
((c >> 10) & 0x00010000) | \
((d >> 13) & 0x00002000) | \
((d >> 4) & 0x00001000) | \
((d << 6) & 0x00000800) | \
((d >> 1) & 0x00000400) | \
((d >> 14) & 0x00000200) | \
((d >> 0) & 0x00000100) | \
((d >> 5) & 0x00000020) | \
((d >> 10) & 0x00000010) | \
((d >> 3) & 0x00000008) | \
((d >> 18) & 0x00000004) | \
((d >> 26) & 0x00000002) | \
((d >> 24) & 0x00000001))

Definition at line 101 of file des.c.

◆ PC2_R

#define PC2_R (   c,
 
)
Value:
(((c << 15) & 0x20000000) | \
((c << 17) & 0x10000000) | \
((c << 10) & 0x08000000) | \
((c << 22) & 0x04000000) | \
((c >> 2) & 0x02000000) | \
((c << 1) & 0x01000000) | \
((c << 16) & 0x00200000) | \
((c << 11) & 0x00100000) | \
((c << 3) & 0x00080000) | \
((c >> 6) & 0x00040000) | \
((c << 15) & 0x00020000) | \
((c >> 4) & 0x00010000) | \
((d >> 2) & 0x00002000) | \
((d << 8) & 0x00001000) | \
((d >> 14) & 0x00000808) | \
((d >> 9) & 0x00000400) | \
((d >> 0) & 0x00000200) | \
((d << 7) & 0x00000100) | \
((d >> 7) & 0x00000020) | \
((d >> 3) & 0x00000011) | \
((d << 2) & 0x00000004) | \
((d >> 21) & 0x00000002))

Definition at line 126 of file des.c.

◆ ROL28

#define ROL28 (   a,
  n 
)    ((((a) << (n)) | ((a) >> (28 - (n)))) & 0x0FFFFFFF)

Definition at line 47 of file des.c.

◆ ROUND

#define ROUND (   l,
  r,
  k1,
  k2 
)
Value:
{ \
t = r ^ k1; \
l ^= sp2[(t >> 24) & 0x3F]; \
l ^= sp4[(t >> 16) & 0x3F]; \
l ^= sp6[(t >> 8) & 0x3F]; \
l ^= sp8[t & 0x3F]; \
t = ROR32(r, 4) ^ k2; \
l ^= sp1[(t >> 24) & 0x3F]; \
l ^= sp3[(t >> 16) & 0x3F]; \
l ^= sp5[(t >> 8) & 0x3F]; \
l ^= sp7[t & 0x3F]; \
}

Definition at line 151 of file des.c.

◆ SWAPMOVE

#define SWAPMOVE (   a,
  b,
  n,
  m 
)
Value:
{ \
t = ((a >> n) ^ b) & m; \
b ^= t; \
a ^= t << n; \
}

Definition at line 50 of file des.c.

◆ TRACE_LEVEL

#define TRACE_LEVEL   CRYPTO_TRACE_LEVEL

Definition at line 37 of file des.c.

Function Documentation

◆ desDecryptBlock()

__weak_func void desDecryptBlock ( DesContext context,
const uint8_t *  input,
uint8_t *  output 
)

Decrypt a 8-byte block using DES algorithm.

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

Definition at line 391 of file des.c.

◆ desDeinit()

__weak_func void desDeinit ( DesContext context)

Release DES context.

Parameters
[in]contextPointer to the DES context

Definition at line 429 of file des.c.

◆ desEncryptBlock()

__weak_func void desEncryptBlock ( DesContext context,
const uint8_t *  input,
uint8_t *  output 
)

Encrypt a 8-byte block using DES algorithm.

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

Definition at line 351 of file des.c.

◆ desInit()

__weak_func error_t desInit ( DesContext context,
const uint8_t *  key,
size_t  keyLen 
)

Initialize a DES context using the supplied key.

Parameters
[in]contextPointer to the DES context to initialize
[in]keyPointer to the key
[in]keyLenLength of the key (must be set to 8)
Returns
Error code

Definition at line 296 of file des.c.

Variable Documentation

◆ DES_CBC_OID

const uint8_t DES_CBC_OID[5] = {0x2B, 0x0E, 0x03, 0x02, 0x07}

Definition at line 270 of file des.c.

◆ desCipherAlgo

const CipherAlgo desCipherAlgo
uint8_t b
Definition: nbns_common.h:104
#define ROR32(a, n)
Definition: crypto.h:803
uint8_t a
Definition: ndp.h:411
void(* CipherAlgoEncryptBlock)(void *context, const uint8_t *input, uint8_t *output)
Definition: crypto.h:1009
@ CIPHER_ALGO_TYPE_BLOCK
Definition: crypto.h:953
uint8_t t
Definition: lldp_ext_med.h:212
uint8_t r
Definition: ndp.h:346
DES algorithm context.
Definition: des.h:58
void(* CipherAlgoDecryptBlock)(void *context, const uint8_t *input, uint8_t *output)
Definition: crypto.h:1012
__weak_func error_t desInit(DesContext *context, const uint8_t *key, size_t keyLen)
Initialize a DES context using the supplied key.
Definition: des.c:296
__weak_func void desDecryptBlock(DesContext *context, const uint8_t *input, uint8_t *output)
Decrypt a 8-byte block using DES algorithm.
Definition: des.c:391
error_t(* CipherAlgoInit)(void *context, const uint8_t *key, size_t keyLen)
Definition: crypto.h:1000
uint8_t l
Definition: ndp.h:412
#define ROL32(a, n)
Definition: crypto.h:797
uint8_t m
Definition: ndp.h:304
uint8_t n
#define DES_BLOCK_SIZE
Definition: des.h:43
__weak_func void desEncryptBlock(DesContext *context, const uint8_t *input, uint8_t *output)
Encrypt a 8-byte block using DES algorithm.
Definition: des.c:351
__weak_func void desDeinit(DesContext *context)
Release DES context.
Definition: des.c:429
void(* CipherAlgoDeinit)(void *context)
Definition: crypto.h:1015
uint8_t c
Definition: ndp.h:514