iPXE
des.c File Reference

DES algorithm. More...

#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <byteswap.h>
#include <ipxe/rotate.h>
#include <ipxe/crypto.h>
#include <ipxe/ecb.h>
#include <ipxe/cbc.h>
#include <ipxe/init.h>
#include <ipxe/des.h>

Go to the source code of this file.

Data Structures

struct  des_generator
 A DES systematic permutation generator. More...

Macros

#define DES_SCHEDULE   0x0d556aab
 DES shift schedule.
#define SBYTE(x, y)
 Define an element pair in a DES S-box.
#define SBOX(x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, xa, xb, xc, xd, xe, xf, y0, y1, y2, y3, y4, y5, y6, y7, y8, y9, ya, yb, yc, yd, ye, yf)
 Define a row pair in a DES S-box.
#define DES_PC2(x)
 Define a bit index within permuted choice 2 (PC2).
#define DES_PC2R(r1, c3, c2, c1, c0, r0)
 Define six bits of permuted choice 2 (PC2).
#define DES_GENERATOR(PERMUTATION, OFFSET, INV5, BIT5, INV4, BIT4, INV3, BIT3, INV2, BIT2, INV1, BIT1, INV0, BIT0)
 Define a DES permutation generator.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 FILE_SECBOOT (PERMITTED)
static void des_generate (struct des_generator *generator)
 Generate DES permutation.
static void des_init (void)
 Initialise permutations.
struct init_fn des_init_fn __init_fn (INIT_NORMAL)
 Initialisation function.
static void des_permute (const uint8_t *permutation, const uint8_t *in, uint8_t *out)
 Perform bit permutation.
static uint32_t des_sbox (uint32_t in, const union des_round_key *rkey)
 Perform DES S-box substitution.
static void des_round (union des_block *block, const union des_round_key *rkey)
 Perform a single DES round.
static void des_rounds (const union des_block *in, union des_block *out, const union des_round_key *rkey, ssize_t offset)
 Perform all DES rounds.
static uint32_t des_rol28 (uint32_t dword)
 Rotate 28-bit word.
static int des_setkey (struct cipher_algorithm *cipher __unused, void *ctx, const void *key, size_t keylen)
 Set key.
static void des_encrypt (struct cipher_algorithm *cipher __unused, void *ctx, const void *src, void *dst, size_t len)
 Encrypt data.
static void des_decrypt (struct cipher_algorithm *cipher __unused, void *ctx, const void *src, void *dst, size_t len)
 Decrypt data.
 ECB_CIPHER (des_ecb, des_ecb_algorithm, des_algorithm, struct des_context, DES_BLOCKSIZE)
 CBC_CIPHER (des_cbc, des_cbc_algorithm, des_algorithm, struct des_context, DES_BLOCKSIZE)

Variables

static const uint8_t des_s [8][32]
 DES S-boxes S1..S8.
static uint8_t des_pc1c [29]
 DES permuted choice 1 (PC1) "C" register.
static uint8_t des_pc1d [33]
 DES permuted choice 1 (PC1) "D" register.
static const uint8_t des_pc2 [65]
 DES permuted choice 2 (PC2).
static uint8_t des_ip [65]
 DES initial permutation (IP).
static const uint8_t des_p [33]
 DES data permutation (P).
static uint8_t des_fp [65]
 DES final / inverse initial permutation (FP / IP^-1).
static struct des_generator des_generators []
 DES permutation generators.
struct cipher_algorithm des_algorithm
 Basic DES algorithm.

Detailed Description

DES algorithm.

DES was not designed to be implemented in software, and therefore contains a large number of bit permutation operations that are essentially free in hardware (requiring only wires, no gates) but expensive in software.

Since DES is no longer used as a practical block cipher for large volumes of data, we optimise for code size, and do not attempt to obtain fast throughput.

The algorithm is specified in NIST SP 800-67, downloadable from https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-67r2.pdf

Definition in file des.c.

Macro Definition Documentation

◆ DES_SCHEDULE

#define DES_SCHEDULE   0x0d556aab

DES shift schedule.

The DES shift schedule (ordered from round 16 down to round 1) is {1,2,2,2,2,2,2,1,2,2,2,2,2,2,1,1}. In binary, this may be represented as {1,10,10,10,10,10,10,1,10,10,10,10,10,10,1,1} and concatenated (without padding) to produce a single binary integer 1101010101010110101010101011 (equal to 0x0d556aab in hexadecimal).

This integer may then be consumed LSB-first, where a 1 bit indicates a shift and the generation of a round key, and a 0 bit indicates a shift without the generation of a round key.

Definition at line 68 of file des.c.

Referenced by des_setkey().

◆ SBYTE

#define SBYTE ( x,
y )
Value:
( ( (y) << 4 ) | (x) )
static unsigned int unsigned int y
Definition pixbuf.h:63
static unsigned int x
Definition pixbuf.h:63

Define an element pair in a DES S-box.

Parameters
xUpper element of element pair
yLower element of element pair

DES S-box elements are 4-bit values. We encode two values per byte, ordering the elements so that the six-bit input value may be used directly as a lookup index.

Specifically, if the input value is {r1,c3,c2,c1,c0,r0}, where {r1,r0} is the table row index and {c3,c2,c1,c0} is the table column index (as used in the DES specification), then:

  • {r1,c3,c2,c1,c0} is the byte index into the table
  • (4*r0) is the required bit shift to extract the 4-bit value

Definition at line 88 of file des.c.

◆ SBOX

#define SBOX ( x0,
x1,
x2,
x3,
x4,
x5,
x6,
x7,
x8,
x9,
xa,
xb,
xc,
xd,
xe,
xf,
y0,
y1,
y2,
y3,
y4,
y5,
y6,
y7,
y8,
y9,
ya,
yb,
yc,
yd,
ye,
yf )
Value:
SBYTE ( x0, y0 ), SBYTE ( x1, y1 ), SBYTE ( x2, y2 ), SBYTE ( x3, y3 ),\
SBYTE ( x4, y4 ), SBYTE ( x5, y5 ), SBYTE ( x6, y6 ), SBYTE ( x7, y7 ),\
SBYTE ( x8, y8 ), SBYTE ( x9, y9 ), SBYTE ( xa, ya ), SBYTE ( xb, yb ),\
SBYTE ( xc, yc ), SBYTE ( xd, yd ), SBYTE ( xe, ye ), SBYTE ( xf, yf )
#define SBYTE(x, y)
Define an element pair in a DES S-box.
Definition des.c:88

Define a row pair in a DES S-box.

Parameters
x0..xfUpper row of row pair
y0..yfLower row of row pair

Definition at line 96 of file des.c.

◆ DES_PC2

#define DES_PC2 ( x)
Value:
( (x) + ( ( (x) > 28 ) ? 4 : 0 ) )

Define a bit index within permuted choice 2 (PC2).

Parameters
bitBit index

Permuted choice 2 (PC2) is used to select bits from a concatenated pair of 28-bit registers ("C" and "D") as part of the key schedule. We store these as 32-bit registers and so must add 4 to indexes above 28.

Definition at line 164 of file des.c.

◆ DES_PC2R

#define DES_PC2R ( r1,
c3,
c2,
c1,
c0,
r0 )
Value:
DES_PC2 ( r0 ), /* LSB stored in sign bit */ \
DES_PC2 ( r0 ), /* Unused bit */ \
DES_PC2 ( r0 ), /* Unused bit */ \
DES_PC2 ( r1 ), /* Remaining 5 bits */ \
DES_PC2 ( c3 ), /* ... */ \
DES_PC2 ( c2 ), /* ... */ \
DES_PC2 ( c1 ), /* ... */ \
DES_PC2 ( c0 ) /* ... */
#define DES_PC2(x)
Define a bit index within permuted choice 2 (PC2).
Definition des.c:164

Define six bits of permuted choice 2 (PC2).

Parameters
r1:r0Bits corresponding to S-box row index
c3:c0Bits corresponding to S-box column index

There are 8 steps within a DES round (one step per S-box). Each step requires six bits of the round key, corresponding to the S-box input value {r1,c3,c2,c1,c0,r0}, where {r1,r0} is the table row index and {c3,c2,c1,c0} is the table column index.

As an optimisation, we store the least significant of the 6 bits in the sign bit of a signed 8-bit value, and the remaining 5 bits in the least significant 5 bits of the 8-bit value. See the comments in des_sbox() for further details.

Definition at line 182 of file des.c.

182#define DES_PC2R( r1, c3, c2, c1, c0, r0 ) \
183 DES_PC2 ( r0 ), /* LSB stored in sign bit */ \
184 DES_PC2 ( r0 ), /* Unused bit */ \
185 DES_PC2 ( r0 ), /* Unused bit */ \
186 DES_PC2 ( r1 ), /* Remaining 5 bits */ \
187 DES_PC2 ( c3 ), /* ... */ \
188 DES_PC2 ( c2 ), /* ... */ \
189 DES_PC2 ( c1 ), /* ... */ \
190 DES_PC2 ( c0 ) /* ... */

◆ DES_GENERATOR

#define DES_GENERATOR ( PERMUTATION,
OFFSET,
INV5,
BIT5,
INV4,
BIT4,
INV3,
BIT3,
INV2,
BIT2,
INV1,
BIT1,
INV0,
BIT0 )
Value:
{ \
.permutation = (PERMUTATION), \
.seed = ( ( ( ( unsigned int ) (INV0) ) << 31 ) | \
( ( ( unsigned int ) (BIT0) ) << 28 ) | \
( ( ( unsigned int ) (INV1) ) << 27 ) | \
( ( ( unsigned int ) (BIT1) ) << 24 ) | \
( ( ( unsigned int ) (INV2) ) << 23 ) | \
( ( ( unsigned int ) (BIT2) ) << 20 ) | \
( ( ( unsigned int ) (INV3) ) << 19 ) | \
( ( ( unsigned int ) (BIT3) ) << 16 ) | \
( ( ( unsigned int ) (INV4) ) << 15 ) | \
( ( ( unsigned int ) (BIT4) ) << 12 ) | \
( ( ( unsigned int ) (INV5) ) << 11 ) | \
( ( ( unsigned int ) (BIT5) ) << 8 ) | \
( ( uint32_t ) sizeof (PERMUTATION) - 1 ) | \
(OFFSET) ), \
}
unsigned int uint32_t
Definition stdint.h:12
#define BIT1
#define BIT3
#define BIT2
#define BIT4
#define BIT0
#define BIT5

Define a DES permutation generator.

Parameters
PERMUTATIONPermutation
OFFSETFixed input bit offset (0 or 1)
INV<n>Input bit index bit <n> should be inverted
BIT<n>Source bit for input bit index bit <n>
Return values
generatorPermutation generator

Definition at line 215 of file des.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED )

◆ des_generate()

void des_generate ( struct des_generator * generator)
static

Generate DES permutation.

Parameters
generatorGenerator

Definition at line 313 of file des.c.

313 {
314 uint8_t *permutation = generator->permutation;
315 uint32_t seed = generator->seed;
316 unsigned int index = 0;
317 uint8_t accum;
318 uint8_t bit;
319
320 /* Generate permutations
321 *
322 * This loop is optimised for code size on a
323 * register-constrained architecture such as i386.
324 */
325 do {
326 /* Rotate seed to access MSB's bit descriptor */
327 seed = ror32 ( seed, 8 );
328
329 /* Initialise accumulator with six flag bits */
330 accum = 0xfc;
331
332 /* Accumulate bits until all six flag bits are cleared */
333 do {
334 /* Extract specified bit from index. Use a
335 * rotation instead of a shift, since this
336 * will allow the mask to be elided.
337 */
338 bit = ror8 ( index, ( seed & 0x07 ) );
339 seed = ror32 ( seed, 3 );
340
341 /* Toggle bit if applicable */
342 bit ^= seed;
343 seed = ror32 ( seed, 1 );
344
345 /* Add bit to accumulator and clear one flag bit */
346 accum <<= 1;
347 accum |= ( bit & 0x01 );
348
349 } while ( accum & 0x80 );
350
351 /* Add constant offset if applicable */
352 accum += ( seed & 0x01 );
353
354 /* Store permutation */
355 permutation[index] = accum;
356
357 /* Loop until reaching length (which is always even) */
358 } while ( ++index < ( seed & 0xfe ) );
359 DBGC2 ( permutation, "DES generated permutation %p:\n", permutation );
360 DBGC2_HDA ( permutation, 0, permutation,
361 ( ( seed & 0xfe ) + 1 /* zero terminator */ ) );
362}
unsigned char uint8_t
Definition stdint.h:10
long index
Definition bigint.h:30
#define DBGC2(...)
Definition compiler.h:547
#define DBGC2_HDA(...)
Definition compiler.h:548
static unsigned int unsigned int bit
Definition bigint.h:390
uint32_t seed
Seed value.
Definition des.c:203
uint8_t * permutation
Permutation.
Definition des.c:201
static u32 ror32(u32 v, int bits)
Rotate 32-bit value right.
Definition wpa_tkip.c:162

References bit, DBGC2, DBGC2_HDA, index, des_generator::permutation, ror32(), and des_generator::seed.

Referenced by des_init().

◆ des_init()

void des_init ( void )
static

Initialise permutations.

Definition at line 367 of file des.c.

367 {
368 unsigned int i;
369
370 /* Generate all generated permutations */
371 for ( i = 0 ; i < ( sizeof ( des_generators ) /
372 sizeof ( des_generators[0] ) ) ; i++ ) {
374 }
375}
static struct des_generator des_generators[]
DES permutation generators.
Definition des.c:268
static void des_generate(struct des_generator *generator)
Generate DES permutation.
Definition des.c:313

References des_generate(), and des_generators.

Referenced by __init_fn().

◆ __init_fn()

struct init_fn des_init_fn __init_fn ( INIT_NORMAL )

Initialisation function.

References __init_fn, des_init(), and INIT_NORMAL.

◆ des_permute()

void des_permute ( const uint8_t * permutation,
const uint8_t * in,
uint8_t * out )
static

Perform bit permutation.

Parameters
permutationBit permutation (zero-terminated)
inInput value
outOutput value

Definition at line 390 of file des.c.

391 {
392 uint8_t mask = 0x80;
393 uint8_t accum = 0;
394 unsigned int bit;
395
396 /* Extract individual input bits to construct output value */
397 while ( ( bit = *(permutation++) ) ) {
398 bit--;
399 if ( in[ bit / 8 ] & ( 0x80 >> ( bit % 8 ) ) )
400 accum |= mask;
401 *out = accum;
402 mask = ror8 ( mask, 1 );
403 if ( mask == 0x80 ) {
404 out++;
405 accum = 0;
406 }
407 }
408}
__be32 out[4]
Definition CIB_PRM.h:8
__be32 in[4]
Definition CIB_PRM.h:7

References bit, in, and out.

Referenced by des_round(), des_rounds(), and des_setkey().

◆ des_sbox()

uint32_t des_sbox ( uint32_t in,
const union des_round_key * rkey )
static

Perform DES S-box substitution.

Parameters
in32-bit input value (native endian)
rkey48-bit round key
Return values
out32-bit output value (native endian)

Definition at line 417 of file des.c.

417 {
418 uint32_t out = 0;
419 uint32_t lookup;
420 int32_t key;
421 uint8_t sub;
422 unsigned int i;
423
424 /* Perform input expansion, key addition, and S-box substitution */
425 for ( i = 0 ; i < 8 ; i++ ) {
426
427 /* Rotate input and output */
428 out = rol32 ( out, 4 );
429 in = rol32 ( in, 4 );
430
431 /* Extract step key from relevant 6 bits of round key
432 *
433 * The least significant of the 6 bits (corresponding
434 * to bit r0 in the S-box lookup index) is stored in
435 * the sign bit of the step key byte. It will
436 * therefore be propagated via sign extension to the
437 * MSB of the 32-bit step key.
438 *
439 * The remaining 5 of the 6 bits (corresponding to
440 * bits {r1,c3,c2,c1,c0} in the S-box lookup index)
441 * are stored in the least significant 5 bits of the
442 * step key byte and will end up in the least
443 * significant 5 bits of the 32-bit step key.
444 */
445 key = rkey->step[i];
446
447 /* Add step key to input to produce S-box lookup index
448 *
449 * We do not ever perform an explicit expansion of the
450 * input value from 32 to 48 bits. Instead, we rotate
451 * the 32-bit input value by 4 bits on each step, and
452 * extract the relevant 6 bits.
453 *
454 * The least significant of the 6 bits (corresponding
455 * to bit r0 in the S-box lookup index) is currently
456 * in the MSB of the 32-bit (rotated) input value.
457 *
458 * The remaining 5 of the 6 bits (corresponding to
459 * bits {r1,c3,c2,c1,c0} in the S-box lookup index)
460 * are currently in the least significant 5 bits of
461 * the 32-bit (rotated) input value.
462 *
463 * This aligns with the placement of the bits in the
464 * step key (see above), and we can therefore perform
465 * a single XOR to add the 6-bit step key to the
466 * relevant 6 bits of the input value.
467 */
468 lookup = ( in ^ key );
469
470 /* Look up S[i][in ^ key] from S-box
471 *
472 * We have bits {r1,c3,c2,c1,c0} in the least
473 * significant 5 bits of the lookup index, and so can
474 * use the masked lookup index directly as a byte
475 * index into the relevant S-box to extract the byte
476 * containing both {r1,c3,c2,c1,c0,'0'} and
477 * {r1,c3,c2,c1,c0,'1'}.
478 *
479 * We then use the MSB of the 32-bit lookup index to
480 * extract the relevant nibble for the full lookup
481 * index {r1,c3,c2,c1,c0,r0}.
482 */
483 sub = des_s[i][ lookup & 0x1f ];
484 sub >>= ( ( lookup >> 29 ) & 4 );
485 sub &= 0x0f;
486
487 /* Substitute S[i][input ^ key] into output */
488 out |= sub;
489 }
490
491 return out;
492}
union @162305117151260234136356364136041353210355154177 key
signed int int32_t
Definition stdint.h:17
static const uint8_t des_s[8][32]
DES S-boxes S1..S8.
Definition des.c:104
int8_t step[8]
6-bit step key byte
Definition des.h:73
static u32 rol32(u32 v, int bits)
Rotate 32-bit value left.
Definition wpa_tkip.c:174

References des_s, in, key, out, rol32(), and des_round_key::step.

Referenced by des_round().

◆ des_round()

void des_round ( union des_block * block,
const union des_round_key * rkey )
static

Perform a single DES round.

Parameters
blockDES block
rkey48-bit round key

Definition at line 500 of file des.c.

501 {
502 union des_dword sbox;
503 uint32_t left;
504 uint32_t right;
505
506 /* Extract left and right halves L[n-1] and R[n-1] */
507 left = block->left.dword;
508 right = block->right.dword;
509 DBGC2 ( block, "DES L=%08x R=%08x K=%08x%08x", be32_to_cpu ( left ),
510 be32_to_cpu ( right ), be32_to_cpu ( rkey->dword[0] ),
511 be32_to_cpu ( rkey->dword[1] ) );
512
513 /* L[n] = R[n-1] */
514 block->left.dword = right;
515
516 /* Calculate Feistel function f(R[n-1], K[n]) */
517 sbox.dword = cpu_to_be32 ( des_sbox ( be32_to_cpu ( right ), rkey ) );
518 des_permute ( des_p, sbox.byte, block->right.byte );
519
520 /* R[n] = L[n-1] + f(R[n-1], K[n]) */
521 block->right.dword ^= left;
522 DBGC2 ( block, " => L=%08x R=%08x\n",
523 be32_to_cpu ( block->left.dword ),
524 be32_to_cpu ( block->right.dword ) );
525}
static uint32_t des_sbox(uint32_t in, const union des_round_key *rkey)
Perform DES S-box substitution.
Definition des.c:417
static void des_permute(const uint8_t *permutation, const uint8_t *in, uint8_t *out)
Perform bit permutation.
Definition des.c:390
static const uint8_t des_p[33]
DES data permutation (P).
Definition des.c:258
#define be32_to_cpu(value)
Definition byteswap.h:117
#define cpu_to_be32(value)
Definition byteswap.h:111
uint8_t block[3][8]
DES-encrypted blocks.
Definition mschapv2.h:1
A DES 32-bit dword value.
Definition des.h:20
uint32_t dword[2]
32-bit big-endian dwords
Definition des.h:61

References be32_to_cpu, block, des_dword::byte, cpu_to_be32, DBGC2, des_p, des_permute(), des_sbox(), des_dword::dword, and des_round_key::dword.

Referenced by des_rounds().

◆ des_rounds()

void des_rounds ( const union des_block * in,
union des_block * out,
const union des_round_key * rkey,
ssize_t offset )
static

Perform all DES rounds.

Parameters
inInput DES block
outOutput DES block
rkeyStarting 48-bit round key
offsetByte offset between round keys

Definition at line 535 of file des.c.

537 {
538 union des_block tmp;
539 unsigned int i;
540
541 /* Apply initial permutation */
542 des_permute ( des_ip, in->byte, tmp.byte );
543
544 /* Perform all DES rounds, consuming keys in the specified order */
545 for ( i = 0 ; i < DES_ROUNDS ; i++ ) {
546 des_round ( &tmp, rkey );
547 rkey = ( ( ( void * ) rkey ) + offset );
548 }
549
550 /* Apply final permutation */
551 DBGC ( &tmp, "DES %scrypted %08x%08x => ",
552 ( ( offset > 0 ) ? "en" : "de" ), be32_to_cpu ( in->dword[0] ),
553 be32_to_cpu ( in->dword[1] ) );
554 des_permute ( des_fp, tmp.byte, out->byte );
555 DBGC ( &tmp, "%08x%08x\n", be32_to_cpu ( out->dword[0] ),
556 be32_to_cpu ( out->dword[1] ) );
557}
uint16_t offset
Offset to command line.
Definition bzimage.h:3
static void des_round(union des_block *block, const union des_round_key *rkey)
Perform a single DES round.
Definition des.c:500
static uint8_t des_fp[65]
DES final / inverse initial permutation (FP / IP^-1).
Definition des.c:265
static uint8_t des_ip[65]
DES initial permutation (IP).
Definition des.c:255
#define DES_ROUNDS
Number of DES rounds.
Definition des.h:77
#define DBGC(...)
Definition compiler.h:530
unsigned long tmp
Definition linux_pci.h:65
A DES 64-bit block.
Definition des.h:28

References be32_to_cpu, DBGC, des_fp, des_ip, des_permute(), des_round(), DES_ROUNDS, in, offset, out, and tmp.

Referenced by des_decrypt(), and des_encrypt().

◆ des_rol28()

uint32_t des_rol28 ( uint32_t dword)
static

Rotate 28-bit word.

Parameters
dword28-bit dword value
Return values
dwordRotated 28-bit dword value

Definition at line 565 of file des.c.

565 {
566 int32_t sdword;
567
568 /* Convert to native-endian */
569 sdword = be32_to_cpu ( dword );
570
571 /* Signed shift right by 4 places to copy bit 31 to bits 27:31 */
572 sdword >>= 4;
573
574 /* Rotate left */
575 sdword = rol32 ( sdword, 1 );
576
577 /* Shift left by 4 places to restore bit positions */
578 sdword <<= 4;
579
580 /* Convert back to big-endian */
581 dword = cpu_to_be32 ( sdword );
582
583 return dword;
584}
unsigned long int dword
Definition smc9000.h:40

References be32_to_cpu, cpu_to_be32, and rol32().

Referenced by des_setkey().

◆ des_setkey()

int des_setkey ( struct cipher_algorithm *cipher __unused,
void * ctx,
const void * key,
size_t keylen )
static

Set key.

Parameters
cipherCipher algorithm
ctxContext
keyKey
keylenKey length
Return values
rcReturn status code

Definition at line 595 of file des.c.

596 {
597 struct des_context *des = ctx;
598 union des_round_key *rkey = des->rkey;
599 union des_block reg;
600 uint32_t schedule;
601
602 /* Validate key length */
603 if ( keylen != DES_BLOCKSIZE )
604 return -EINVAL;
605 DBGC ( des, "DES %p new key:\n", des );
606 DBGC_HDA ( des, 0, key, keylen );
607
608 /* Apply permuted choice 1 */
609 des_permute ( des_pc1c, key, reg.c.byte );
610 des_permute ( des_pc1d, key, reg.d.byte );
611 reg.d.byte[3] <<= 4; /* see comment for @c des_pc1d */
612 DBGC2 ( des, "DES %p C[ 0]=%07x D[ 0]=%07x\n",
613 des, ( be32_to_cpu ( reg.c.dword ) >> 4 ),
614 ( be32_to_cpu ( reg.d.dword ) >> 4 ) );
615
616 /* Generate round keys */
617 for ( schedule = DES_SCHEDULE ; schedule ; schedule >>= 1 ) {
618
619 /* Shift 28-bit words */
620 reg.c.dword = des_rol28 ( reg.c.dword );
621 reg.d.dword = des_rol28 ( reg.d.dword );
622
623 /* Skip rounds according to shift schedule */
624 if ( ! ( schedule & 1 ) )
625 continue;
626
627 /* Apply permuted choice 2 */
628 des_permute ( des_pc2, reg.byte, rkey->byte );
629 DBGC2 ( des, "DES %p C[%2zd]=%07x D[%2zd]=%07x K[%2zd]="
630 "%08x%08x\n", des, ( ( rkey - des->rkey ) + 1 ),
631 ( be32_to_cpu ( reg.c.dword ) >> 4 ),
632 ( ( rkey - des->rkey ) + 1 ),
633 ( be32_to_cpu ( reg.d.dword ) >> 4 ),
634 ( ( rkey - des->rkey ) + 1 ),
635 be32_to_cpu ( rkey->dword[0] ),
636 be32_to_cpu ( rkey->dword[1] ) );
637
638 /* Move to next key */
639 rkey++;
640 }
641
642 /* Sanity check */
643 assert ( rkey == &des->rkey[DES_ROUNDS] );
644
645 return 0;
646}
struct golan_eq_context ctx
Definition CIB_PRM.h:0
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:61
static uint32_t des_rol28(uint32_t dword)
Rotate 28-bit word.
Definition des.c:565
static uint8_t des_pc1c[29]
DES permuted choice 1 (PC1) "C" register.
Definition des.c:236
#define DES_SCHEDULE
DES shift schedule.
Definition des.c:68
static uint8_t des_pc1d[33]
DES permuted choice 1 (PC1) "D" register.
Definition des.c:239
static const uint8_t des_pc2[65]
DES permuted choice 2 (PC2).
Definition des.c:242
#define DES_BLOCKSIZE
DES blocksize.
Definition des.h:50
#define DBGC_HDA(...)
Definition compiler.h:531
#define EINVAL
Invalid argument.
Definition errno.h:472
static unsigned int unsigned int reg
Definition myson.h:162
DES context.
Definition des.h:80
union des_round_key rkey[DES_ROUNDS]
Round keys.
Definition des.h:82
A DES round key.
Definition des.h:57
uint8_t byte[8]
Raw bytes.
Definition des.h:59

References __unused, assert, be32_to_cpu, des_round_key::byte, ctx, DBGC, DBGC2, DBGC_HDA, DES_BLOCKSIZE, des_pc1c, des_pc1d, des_pc2, des_permute(), des_rol28(), DES_ROUNDS, DES_SCHEDULE, des_round_key::dword, EINVAL, key, reg, and des_context::rkey.

◆ des_encrypt()

void des_encrypt ( struct cipher_algorithm *cipher __unused,
void * ctx,
const void * src,
void * dst,
size_t len )
static

Encrypt data.

Parameters
cipherCipher algorithm
ctxContext
srcData to encrypt
dstBuffer for encrypted data
lenLength of data

Definition at line 657 of file des.c.

658 {
659 struct des_context *des = ctx;
660
661 /* Sanity check */
662 assert ( len == DES_BLOCKSIZE );
663
664 /* Cipher using keys in forward direction */
665 des_rounds ( src, dst, &des->rkey[0], sizeof ( des->rkey[0] ) );
666}
static const void * src
Definition string.h:48
static void des_rounds(const union des_block *in, union des_block *out, const union des_round_key *rkey, ssize_t offset)
Perform all DES rounds.
Definition des.c:535
ring len
Length.
Definition dwmac.h:226

References __unused, assert, ctx, DES_BLOCKSIZE, des_rounds(), len, des_context::rkey, and src.

◆ des_decrypt()

void des_decrypt ( struct cipher_algorithm *cipher __unused,
void * ctx,
const void * src,
void * dst,
size_t len )
static

Decrypt data.

Parameters
cipherCipher algorithm
ctxContext
srcData to decrypt
dstBuffer for decrypted data
lenLength of data

Definition at line 677 of file des.c.

678 {
679 struct des_context *des = ctx;
680
681 /* Sanity check */
682 assert ( len == DES_BLOCKSIZE );
683
684 /* Cipher using keys in reverse direction */
685 des_rounds ( src, dst, &des->rkey[ DES_ROUNDS - 1 ],
686 -sizeof ( des->rkey[0] ) );
687}

References __unused, assert, ctx, DES_BLOCKSIZE, DES_ROUNDS, des_rounds(), len, des_context::rkey, and src.

◆ ECB_CIPHER()

ECB_CIPHER ( des_ecb ,
des_ecb_algorithm ,
des_algorithm ,
struct des_context ,
DES_BLOCKSIZE  )

◆ CBC_CIPHER()

CBC_CIPHER ( des_cbc ,
des_cbc_algorithm ,
des_algorithm ,
struct des_context ,
DES_BLOCKSIZE  )

Variable Documentation

◆ des_s

const uint8_t des_s[8][32]
static

DES S-boxes S1..S8.

Definition at line 104 of file des.c.

104 { {
105 /* S1 */
106 SBOX ( 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
107 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8 ),
108 SBOX ( 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
109 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 )
110}, {
111 /* S2 */
112 SBOX ( 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
113 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5 ),
114 SBOX ( 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
115 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 )
116}, {
117 /* S3 */
118 SBOX ( 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
119 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1 ),
120 SBOX ( 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
121 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 )
122}, {
123 /* S4 */
124 SBOX ( 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
125 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9 ),
126 SBOX ( 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
127 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 )
128}, {
129 /* S5 */
130 SBOX ( 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
131 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6 ),
132 SBOX ( 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
133 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 )
134}, {
135 /* S6 */
136 SBOX ( 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
137 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8 ),
138 SBOX ( 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
139 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 )
140}, {
141 /* S7 */
142 SBOX ( 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
143 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6 ),
144 SBOX ( 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
145 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 )
146}, {
147 /* S8 */
148 SBOX ( 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
149 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2 ),
150 SBOX ( 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
151 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 )
152} };
#define SBOX(x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, xa, xb, xc, xd, xe, xf, y0, y1, y2, y3, y4, y5, y6, y7, y8, y9, ya, yb, yc, yd, ye, yf)
Define a row pair in a DES S-box.
Definition des.c:96

Referenced by des_sbox().

◆ des_pc1c

uint8_t des_pc1c[29]
static

DES permuted choice 1 (PC1) "C" register.

Definition at line 236 of file des.c.

Referenced by des_setkey().

◆ des_pc1d

uint8_t des_pc1d[33]
static

DES permuted choice 1 (PC1) "D" register.

Definition at line 239 of file des.c.

Referenced by des_setkey().

◆ des_pc2

const uint8_t des_pc2[65]
static
Initial value:
= {
DES_PC2R ( 14, 17, 11, 24, 1, 5 ),
DES_PC2R ( 3, 28, 15, 6, 21, 10 ),
DES_PC2R ( 23, 19, 12, 4, 26, 8 ),
DES_PC2R ( 16, 7, 27, 20, 13, 2 ),
DES_PC2R ( 41, 52, 31, 37, 47, 55 ),
DES_PC2R ( 30, 40, 51, 45, 33, 48 ),
DES_PC2R ( 44, 49, 39, 56, 34, 53 ),
DES_PC2R ( 46, 42, 50, 36, 29, 32 ),
0
}
#define DES_PC2R(r1, c3, c2, c1, c0, r0)
Define six bits of permuted choice 2 (PC2).
Definition des.c:182

DES permuted choice 2 (PC2).

Definition at line 242 of file des.c.

242 {
243 DES_PC2R ( 14, 17, 11, 24, 1, 5 ),
244 DES_PC2R ( 3, 28, 15, 6, 21, 10 ),
245 DES_PC2R ( 23, 19, 12, 4, 26, 8 ),
246 DES_PC2R ( 16, 7, 27, 20, 13, 2 ),
247 DES_PC2R ( 41, 52, 31, 37, 47, 55 ),
248 DES_PC2R ( 30, 40, 51, 45, 33, 48 ),
249 DES_PC2R ( 44, 49, 39, 56, 34, 53 ),
250 DES_PC2R ( 46, 42, 50, 36, 29, 32 ),
251 0 /* terminator */
252};

Referenced by des_setkey().

◆ des_ip

uint8_t des_ip[65]
static

DES initial permutation (IP).

Definition at line 255 of file des.c.

Referenced by des_rounds().

◆ des_p

const uint8_t des_p[33]
static
Initial value:
= {
16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10,
2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25,
0
}

DES data permutation (P).

Definition at line 258 of file des.c.

258 {
259 16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10,
260 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25,
261 0 /* terminator */
262};

Referenced by des_round().

◆ des_fp

uint8_t des_fp[65]
static

DES final / inverse initial permutation (FP / IP^-1).

Definition at line 265 of file des.c.

Referenced by des_rounds().

◆ des_generators

struct des_generator des_generators[]
static
Initial value:
= {
DES_GENERATOR ( des_ip, 1, 1, 2, 1, 1, 1, 0, 0, 4, 0, 3, 1, 5 ),
DES_GENERATOR ( des_fp, 1, 0, 0, 0, 2, 0, 1, 1, 5, 1, 4, 1, 3 ),
DES_GENERATOR ( des_pc1c, 1, 1, 2, 1, 1, 1, 0, 0, 5, 0, 4, 0, 3 ),
DES_GENERATOR ( des_pc1d, 0, 1, 2, 1, 1, 1, 0, 1, 5, 1, 4, 1, 3 ),
}
#define DES_GENERATOR(PERMUTATION, OFFSET, INV5, BIT5, INV4, BIT4, INV3, BIT3, INV2, BIT2, INV1, BIT1, INV0, BIT0)
Define a DES permutation generator.
Definition des.c:215

DES permutation generators.

Definition at line 268 of file des.c.

268 {
269
270 /* The DES initial permutation transforms the bit index
271 * {x5,x4,x3,x2,x1,x0}+1 into {~x2,~x1,~x0,x4,x3,~x5}+1
272 */
273 DES_GENERATOR ( des_ip, 1, 1, 2, 1, 1, 1, 0, 0, 4, 0, 3, 1, 5 ),
274
275 /* The DES final permutation transforms the bit index
276 * {x5,x4,x3,x2,x1,x0}+1 into {~x0,x2,x1,~x5,~x4,~x3}+1
277 *
278 * There is an asymmetry in the DES block diagram for the last
279 * of the 16 rounds, which is functionally equivalent to
280 * performing 16 identical rounds and then swapping the left
281 * and right halves before applying the final permutation. We
282 * may therefore account for this asymmetry by inverting the
283 * MSB in each bit index, to point to the corresponding bit in
284 * the other half.
285 *
286 * This is equivalent to using a permutation that transforms
287 * {x5,x4,x3,x2,x1,x0}+1 into {x0,x2,x1,~x5,~x4,~x3}+1
288 */
289 DES_GENERATOR ( des_fp, 1, 0, 0, 0, 2, 0, 1, 1, 5, 1, 4, 1, 3 ),
290
291 /* The "C" half of DES permuted choice 1 (PC1) transforms the
292 * bit index {x5,x4,x3,x2,x1,x0}+1 into {~x2,~x1,~x0,x5,x4,x3}+1
293 */
294 DES_GENERATOR ( des_pc1c, 1, 1, 2, 1, 1, 1, 0, 0, 5, 0, 4, 0, 3 ),
295
296 /* The "D" half of DES permuted choice 1 (PC1) transforms the
297 * bit index {x5,x4,x3,x2,x1,x0}+1 into {~x2,~x1,~x0,~x5,~x4,~x3}+0
298 *
299 * Due to the idosyncratic design choice of using 28-bit
300 * registers in the DES key expansion schedule, the final four
301 * permutation values appear at indices [28:31] instead of
302 * [24:27]. This is adjusted for in @c des_setkey().
303 */
304 DES_GENERATOR ( des_pc1d, 0, 1, 2, 1, 1, 1, 0, 1, 5, 1, 4, 1, 3 ),
305};

Referenced by des_init().

◆ des_algorithm

struct cipher_algorithm des_algorithm
Initial value:
= {
.name = "des",
.ctxsize = sizeof ( struct des_context ),
.blocksize = DES_BLOCKSIZE,
.alignsize = 0,
.authsize = 0,
.confidential = 1,
.setkey = des_setkey,
.encrypt = des_encrypt,
.decrypt = des_decrypt,
}
int cipher_null_setiv(struct cipher_algorithm *cipher __unused, void *ctx __unused, const void *iv __unused, size_t ivlen __unused)
Definition crypto_null.c:70
void cipher_null_auth(struct cipher_algorithm *cipher __unused, void *ctx __unused, void *auth __unused)
Definition crypto_null.c:89
static void des_encrypt(struct cipher_algorithm *cipher __unused, void *ctx, const void *src, void *dst, size_t len)
Encrypt data.
Definition des.c:657
static void des_decrypt(struct cipher_algorithm *cipher __unused, void *ctx, const void *src, void *dst, size_t len)
Decrypt data.
Definition des.c:677
static int des_setkey(struct cipher_algorithm *cipher __unused, void *ctx, const void *key, size_t keylen)
Set key.
Definition des.c:595

Basic DES algorithm.

Definition at line 690 of file des.c.

690 {
691 .name = "des",
692 .ctxsize = sizeof ( struct des_context ),
693 .blocksize = DES_BLOCKSIZE,
694 .alignsize = 0,
695 .authsize = 0,
696 .confidential = 1,
697 .setkey = des_setkey,
698 .setiv = cipher_null_setiv,
699 .encrypt = des_encrypt,
700 .decrypt = des_decrypt,
701 .auth = cipher_null_auth,
702};

Referenced by CBC_CIPHER(), ECB_CIPHER(), and mschapv2_challenge_response().