mars.c File Reference

MARS encryption algorithm. More...

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

Go to the source code of this file.

Macros

#define TRACE_LEVEL   CRYPTO_TRACE_LEVEL
 
#define S(n)   sbox[(n) & 0x1FF]
 
#define S0(n)   sbox[(n) & 0xFF]
 
#define S1(n)   sbox[((n) & 0xFF) + 256]
 
#define F_MIX(a, b, c, d)
 
#define B_MIX(a, b, c, d)
 
#define CORE(a, b, c, d, k1, k2)
 
#define CORE_INV(a, b, c, d, k1, k2)
 
#define MASK_GEN(m, w)
 

Functions

error_t marsInit (MarsContext *context, const uint8_t *key, size_t keyLen)
 Key expansion. More...
 
void marsEncryptBlock (MarsContext *context, const uint8_t *input, uint8_t *output)
 Encrypt a 16-byte block using MARS algorithm. More...
 
void marsDecryptBlock (MarsContext *context, const uint8_t *input, uint8_t *output)
 Decrypt a 16-byte block using MARS algorithm. More...
 
void marsDeinit (MarsContext *context)
 Release MARS context. More...
 

Variables

const CipherAlgo marsCipherAlgo
 

Detailed Description

MARS 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.

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

Definition in file mars.c.

Macro Definition Documentation

◆ B_MIX

#define B_MIX (   a,
  b,
  c,
 
)
Value:
{ \
uint32_t t; \
b ^= S1(a); \
t = ROL32(a, 8); \
c -= S0(t); \
t = ROL32(a, 16); \
d -= S1(t); \
a = ROL32(a, 24); \
d ^= S0(a); \
}
#define ROL32(a, n)
Definition: crypto.h:776
uint8_t t
Definition: lldp_ext_med.h:212
#define S0(n)
Definition: mars.c:44
#define S1(n)
Definition: mars.c:46
uint8_t a
Definition: ndp.h:411

Definition at line 62 of file mars.c.

◆ CORE

#define CORE (   a,
  b,
  c,
  d,
  k1,
  k2 
)
Value:
{ \
uint32_t r; \
uint32_t l; \
uint32_t m; \
m = a + k1; \
a = ROL32(a, 13); \
r = a * k2; \
r = ROL32(r, 5); \
c += ROL32(m, r & 0x1F); \
l = S(m) ^ r; \
r = ROL32(r, 5); \
l ^= r; \
d ^= r; \
b += ROL32(l, r & 0x1F); \
}
#define S(n)
Definition: mars.c:42
uint8_t r
Definition: ndp.h:346
uint8_t l
Definition: ndp.h:412
uint8_t m
Definition: ndp.h:304

Definition at line 75 of file mars.c.

◆ CORE_INV

#define CORE_INV (   a,
  b,
  c,
  d,
  k1,
  k2 
)
Value:
{ \
uint32_t r; \
uint32_t l; \
uint32_t m; \
r = a * k2; \
a = ROR32(a, 13); \
m = a + k1; \
r = ROL32(r, 5); \
c -= ROL32(m, r & 0x1F); \
l = S(m) ^ r; \
r = ROL32(r, 5); \
l ^= r; \
d ^= r; \
b -= ROL32(l, r & 0x1F); \
};
#define ROR32(a, n)
Definition: crypto.h:782

Definition at line 93 of file mars.c.

◆ F_MIX

#define F_MIX (   a,
  b,
  c,
 
)
Value:
{ \
uint32_t t; \
b ^= S0(a); \
t = ROR32(a, 8); \
b += S1(t); \
t = ROR32(a, 16); \
c += S0(t); \
a = ROR32(a, 24); \
d ^= S1(a); \
}

Definition at line 49 of file mars.c.

◆ MASK_GEN

#define MASK_GEN (   m,
 
)
Value:
{ \
m = ~w ^ (w >> 1); \
m &= 0x7FFFFFFF; \
m &= (m >> 1) & (m >> 2); \
m &= (m >> 3) & (m >> 6); \
if(m != 0) \
{ \
m <<= 1; \
m |= (m << 1); \
m |= (m << 2); \
m |= (m << 4); \
m &= 0xFFFFFFFC; \
} \
}

Definition at line 111 of file mars.c.

◆ S

#define S (   n)    sbox[(n) & 0x1FF]

Definition at line 42 of file mars.c.

◆ S0

#define S0 (   n)    sbox[(n) & 0xFF]

Definition at line 44 of file mars.c.

◆ S1

#define S1 (   n)    sbox[((n) & 0xFF) + 256]

Definition at line 46 of file mars.c.

◆ TRACE_LEVEL

#define TRACE_LEVEL   CRYPTO_TRACE_LEVEL

Definition at line 32 of file mars.c.

Function Documentation

◆ marsDecryptBlock()

void marsDecryptBlock ( MarsContext context,
const uint8_t *  input,
uint8_t *  output 
)

Decrypt a 16-byte block using MARS algorithm.

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

Definition at line 421 of file mars.c.

◆ marsDeinit()

void marsDeinit ( MarsContext context)

Release MARS context.

Parameters
[in]contextPointer to the MARS context

Definition at line 506 of file mars.c.

◆ marsEncryptBlock()

void marsEncryptBlock ( MarsContext context,
const uint8_t *  input,
uint8_t *  output 
)

Encrypt a 16-byte block using MARS algorithm.

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

Definition at line 334 of file mars.c.

◆ marsInit()

error_t marsInit ( MarsContext context,
const uint8_t *  key,
size_t  keyLen 
)

Key expansion.

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

Definition at line 226 of file mars.c.

Variable Documentation

◆ marsCipherAlgo

const CipherAlgo marsCipherAlgo
Initial value:
=
{
"MARS",
sizeof(MarsContext),
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 marsEncryptBlock(MarsContext *context, const uint8_t *input, uint8_t *output)
Encrypt a 16-byte block using MARS algorithm.
Definition: mars.c:334
error_t marsInit(MarsContext *context, const uint8_t *key, size_t keyLen)
Key expansion.
Definition: mars.c:226
void marsDecryptBlock(MarsContext *context, const uint8_t *input, uint8_t *output)
Decrypt a 16-byte block using MARS algorithm.
Definition: mars.c:421
void marsDeinit(MarsContext *context)
Release MARS context.
Definition: mars.c:506
#define MARS_BLOCK_SIZE
Definition: mars.h:38
MARS algorithm context.
Definition: mars.h:53

Definition at line 203 of file mars.c.