Skip to content

Commit

Permalink
crypto:convert code style form openbsd to nuttx
Browse files Browse the repository at this point in the history
Signed-off-by: anjiahao <anjiahao@xiaomi.com>
  • Loading branch information
anjiahao1 authored and xiaoxiang781216 committed Dec 13, 2022
1 parent acd3350 commit 82956a2
Show file tree
Hide file tree
Showing 48 changed files with 12,170 additions and 9,743 deletions.
1,608 changes: 908 additions & 700 deletions crypto/aes.c

Large diffs are not rendered by default.

1,173 changes: 607 additions & 566 deletions crypto/blf.c

Large diffs are not rendered by default.

570 changes: 324 additions & 246 deletions crypto/cast.c

Large diffs are not rendered by default.

1,064 changes: 537 additions & 527 deletions crypto/castsb.h

Large diffs are not rendered by default.

396 changes: 235 additions & 161 deletions crypto/chacha_private.h

Large diffs are not rendered by default.

440 changes: 239 additions & 201 deletions crypto/chachapoly.c

Large diffs are not rendered by default.

211 changes: 125 additions & 86 deletions crypto/cmac.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* $OpenBSD: cmac.c,v 1.3 2017/05/02 17:07:06 mikeb Exp $ */

/*-
/****************************************************************************
* crypto/cmac.c
* $OpenBSD: cmac.c,v 1.3 2017/05/02 17:07:06 mikeb Exp $
*
* Copyright (c) 2008 Damien Bergamini <damien.bergamini@free.fr>
*
* Permission to use, copy, modify, and distribute this software for any
Expand All @@ -14,107 +15,145 @@
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
*
****************************************************************************/

/*
* This code implements the CMAC (Cipher-based Message Authentication)
/* This code implements the CMAC (Cipher-based Message Authentication)
* algorithm described in FIPS SP800-38B using the AES-128 cipher.
*/

/****************************************************************************
* Included Files
****************************************************************************/

#include <sys/param.h>
#include <sys/systm.h>

#include <crypto/aes.h>
#include <crypto/cmac.h>

#define LSHIFT(v, r) do { \
int i; \
for (i = 0; i < 15; i++) \
(r)[i] = (v)[i] << 1 | (v)[i + 1] >> 7; \
(r)[15] = (v)[15] << 1; \
} while (0)

#define XOR(v, r) do { \
int i; \
for (i = 0; i < 16; i++) \
(r)[i] ^= (v)[i]; \
} while (0)

void
AES_CMAC_Init(AES_CMAC_CTX *ctx)
#define LSHIFT(v, r) do \
{ \
int i; \
for (i = 0; i < 15; i++) \
(r)[i] = (v)[i] << 1 | (v)[i + 1] >> 7; \
(r)[15] = (v)[15] << 1; \
} while (0)

#define XOR(v, r) do \
{ \
int i; \
for (i = 0; i < 16; i++) \
(r)[i] ^= (v)[i]; \
} while (0)

/****************************************************************************
* Public Functions
****************************************************************************/

void aes_cmac_init(FAR AES_CMAC_CTX *ctx)
{
memset(ctx->X, 0, sizeof ctx->X);
ctx->M_n = 0;
memset(ctx->X, 0, sizeof ctx->X);
ctx->m_n = 0;
}

void
AES_CMAC_SetKey(AES_CMAC_CTX *ctx, const u_int8_t key[AES_CMAC_KEY_LENGTH])
void aes_cmac_setkey(FAR AES_CMAC_CTX *ctx,
FAR const uint8_t *key)
{
AES_Setkey(&ctx->aesctx, key, 16);
aes_setkey(&ctx->aesctx, key, 16);
}

void
AES_CMAC_Update(AES_CMAC_CTX *ctx, const u_int8_t *data, u_int len)
void aes_cmac_update(FAR AES_CMAC_CTX *ctx,
FAR const uint8_t *data,
u_int len)
{
u_int mlen;

if (ctx->M_n > 0) {
mlen = MIN(16 - ctx->M_n, len);
memcpy(ctx->M_last + ctx->M_n, data, mlen);
ctx->M_n += mlen;
if (ctx->M_n < 16 || len == mlen)
return;
XOR(ctx->M_last, ctx->X);
AES_Encrypt(&ctx->aesctx, ctx->X, ctx->X);
data += mlen;
len -= mlen;
}
while (len > 16) { /* not last block */
XOR(data, ctx->X);
AES_Encrypt(&ctx->aesctx, ctx->X, ctx->X);
data += 16;
len -= 16;
}
/* potential last block, save it */
memcpy(ctx->M_last, data, len);
ctx->M_n = len;
u_int mlen;

if (ctx->m_n > 0)
{
mlen = MIN(16 - ctx->m_n, len);
memcpy(ctx->m_last + ctx->m_n, data, mlen);
ctx->m_n += mlen;
if (ctx->m_n < 16 || len == mlen)
{
return;
}

XOR(ctx->m_last, ctx->X);
aes_encrypt(&ctx->aesctx, ctx->X, ctx->X);
data += mlen;
len -= mlen;
}

while (len > 16)
{
/* not last block */

XOR(data, ctx->X);
aes_encrypt(&ctx->aesctx, ctx->X, ctx->X);
data += 16;
len -= 16;
}

/* potential last block, save it */

memcpy(ctx->m_last, data, len);
ctx->m_n = len;
}

void
AES_CMAC_Final(u_int8_t digest[AES_CMAC_DIGEST_LENGTH], AES_CMAC_CTX *ctx)
void aes_cmac_final(FAR uint8_t *digest,
FAR AES_CMAC_CTX *ctx)
{
u_int8_t K[16];

/* generate subkey K1 */
memset(K, 0, sizeof K);
AES_Encrypt(&ctx->aesctx, K, K);

if (K[0] & 0x80) {
LSHIFT(K, K);
K[15] ^= 0x87;
} else
LSHIFT(K, K);

if (ctx->M_n == 16) {
/* last block was a complete block */
XOR(K, ctx->M_last);
} else {
/* generate subkey K2 */
if (K[0] & 0x80) {
LSHIFT(K, K);
K[15] ^= 0x87;
} else
LSHIFT(K, K);

/* padding(M_last) */
ctx->M_last[ctx->M_n] = 0x80;
while (++ctx->M_n < 16)
ctx->M_last[ctx->M_n] = 0;

XOR(K, ctx->M_last);
}
XOR(ctx->M_last, ctx->X);
AES_Encrypt(&ctx->aesctx, ctx->X, digest);

explicit_bzero(K, sizeof K);
uint8_t K[16];

/* generate subkey K1 */

memset(K, 0, sizeof K);
aes_encrypt(&ctx->aesctx, K, K);

if (K[0] & 0x80)
{
LSHIFT(K, K);
K[15] ^= 0x87;
}
else
{
LSHIFT(K, K);
}

if (ctx->m_n == 16)
{
/* last block was a complete block */

XOR(K, ctx->m_last);
}
else
{
/* generate subkey K2 */

if (K[0] & 0x80)
{
LSHIFT(K, K);
K[15] ^= 0x87;
}
else
{
LSHIFT(K, K);
}

/* padding(m_last) */

ctx->m_last[ctx->m_n] = 0x80;
while (++ctx->m_n < 16)
{
ctx->m_last[ctx->m_n] = 0;
}

XOR(K, ctx->m_last);
}

XOR(ctx->m_last, ctx->X);
aes_encrypt(&ctx->aesctx, ctx->X, digest);

explicit_bzero(K, sizeof K);
}
Loading

0 comments on commit 82956a2

Please sign in to comment.