Update repo

This commit is contained in:
hangpersonal committed 2026-08-30 23:04:35 -07:00
1 parent 749dab5721
commit ce65a0f59a
14950 files changed
+4408250 -1

No files matched your search

@@ -0,0 +1,104 @@
//###########################################################################
//
// FILE: emsa_pkcs1_v1_5.h
//
// TITLE: EMSA-PKCS message encoding header file
//
//###########################################################################
// $Copyright:
// Copyright (C) 2024 Texas Instruments Incorporated - http://www.ti.com/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the
// distribution.
//
// Neither the name of Texas Instruments Incorporated nor the names of
// its contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// $
//###########################################################################
#ifndef __EMSA_PKCS1_V1_5_H__
#define __EMSA_PKCS1_V1_5_H__
#include <stdint.h>
//*****************************************************************************
//
// Defines
//
//*****************************************************************************
#define SHA256_HASH_SIZE 32 // 32 byte message digest output for SHA-256
#define SHA384_HASH_SIZE 48 // 48 byte message digest output for SHA-384
#define ASN_1_PAD_SIZE 30
#define PKCS_STATUS_SUCCESS 0
#define PKCS_STATUS_FAILURE 1
#define PKCS_STATUS_FAILURE_SIZE 2
#define PKCS_STATUS_FAILURE_NULL 3
#define PKCS_STATUS_FAILURE_READ 4
#define PKCS_STATUS_FAILURE_MEM 5
#define PKCS_STATUS_FAILURE_LEN 6
#define PK_STATUS_INVALID 6
#define STORE32H(x, y) \
do \
{ \
(y)[0] = (unsigned char)(((x) >> 24) & 255); \
(y)[1] = (unsigned char)(((x) >> 16) & 255); \
(y)[2] = (unsigned char)(((x) >> 8) & 255); \
(y)[3] = (unsigned char)((x)&255); \
} while (0)
//*****************************************************************************
//
//! EMSA-PKCS encode message
//!
//! \param emsa_out is the pointer to hold the encoded data
//! \param emsa_outlen is the output length of the encoded data
//! \param digest is the serialized hash data based on the SHA encoding type
//! \param hash_size is the length of the hash
//!
//! \return Returns PKCS_STATUS_SUCCESS if successful
//
//*****************************************************************************
int emsa_pkcs1_v1_5_encode(uint16_t* emsa_out, uint16_t* digest, uint16_t hash_size);
//*****************************************************************************
//
//! EMSA-PKCS decode message
//!
//! \param EM_Str is the pointer to the EMSA padding bytes
//! \param hash is the pointer to the hash to be compared (serialized digest)
//! \param hash_size is the size of the hash based on the SHA encoding type
//! \param result is a pointer to the variable to hold the verification result
//! 1 - Verification successful
//! 0 - Incorrect signature. Verification failed
//! -1 - Verification incomplete
//!
//! \return Returns PKCS_STATUS_SUCCESS if successful
//
//*****************************************************************************
int emsa_pkcs1_v1_5_decode(uint16_t* EM_Str, uint16_t *hash, uint16_t hash_size, int16_t* result);
#endif
@@ -0,0 +1,124 @@
//###########################################################################
//
// FILE: emsa_pss.h
//
// TITLE: EMSA-PSS message encoding header file
//
//###########################################################################
// $Copyright:
// Copyright (C) 2024 Texas Instruments Incorporated - http://www.ti.com/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the
// distribution.
//
// Neither the name of Texas Instruments Incorporated nor the names of
// its contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// $
//###########################################################################
#ifndef EMSA_PSS_H_
#define EMSA_PSS_H_
#include <stdint.h>
#include "yarrow.h"
//*****************************************************************************
//
// Defines
//
//*****************************************************************************
#define HASH_SIZE 32 // 32 byte message digest output for SHA-256
#define PKCS_STATUS_SUCCESS 0
#define PKCS_STATUS_FAILURE 1
#define PKCS_STATUS_FAILURE_SIZE 2
#define PKCS_STATUS_FAILURE_NULL 3
#define PKCS_STATUS_FAILURE_READ 4
#define PKCS_STATUS_FAILURE_MEM 5
#define PK_STATUS_INVALID 6
#define STORE32H(x, y) \
do \
{ \
(y)[0] = (unsigned char)(((x) >> 24) & 255); \
(y)[1] = (unsigned char)(((x) >> 16) & 255); \
(y)[2] = (unsigned char)(((x) >> 8) & 255); \
(y)[3] = (unsigned char)((x)&255); \
} while (0)
//*****************************************************************************
//
//! PKCS1_MGF mask generation using SHA-256
//!
//! \param seed is the pointer to the input seed
//! \param seedlen is the length of the seed in bytes
//! \param mask is the pointer to the output mask
//! \param masklen is the length of the output mask in bytes
//!
//! \return Returns PKCS_STATUS_SUCCESS if successful
//
//*****************************************************************************
int pkcs_1_mgf(uint16_t *seed, int16_t seedlen, uint16_t *mask, int16_t masklen);
//*****************************************************************************
//
//! EMSA-PSS encode message
//!
//! \param msghash is the pointer to the bytewise hash digest of the message
//! \param hashlen is the length of the hash digest of the message in bytes
//! \param saltlen is the length of the random salt in bytes
//! \param modulus_bitlen is the length of the RSA modulus in bits (not bytes)
//! \param prng is the pointer to the yarrow prng handler to generate
//! random salt
//! \param emsa_out is the pointer to hold the encoded data
//! \param emsa_outlen is the output length of the encoded data
//!
//! \return Returns PKCS_STATUS_SUCCESS if successful
//
//*****************************************************************************
int emsa_pss_encode(uint16_t *msghash, uint16_t msghashlen, uint16_t saltlen,
uint16_t modulus_bitlen, yarrow_prng *prng,
uint16_t *emsa_out, uint16_t emsa_outlen);
//*****************************************************************************
//
//! EMSA-PSS decode message
//!
//! \param sig is the pointer to the message encoding to be decoded
//! \param siglen is the length in bytes of the message encoding to be decoded
//! \param msghash is the pointer to the bytewise hash digest of the message
//! \param hashlen is the length of the hash digest of the message in bytes
//! \param saltlen is the length of the random salt in bytes
//! \param modulus_bitlen is the length of the RSA modulus in bits (not bytes)
//! \param res is a pointer to the variable to hold the verification result
//! 1 - Verification successful
//! 0 - Incorrect signature. Verification failed
//! -1 - Verification incomplete
//!
//! \return Returns PKCS_STATUS_SUCCESS if successful
//
//*****************************************************************************
int emsa_pss_decode(uint16_t *sig, uint16_t siglen, uint16_t *msghash, uint16_t msghashlen,
uint16_t saltlen, uint16_t modulus_bitlen, int16_t *res);
#endif /* EMSA_PSS_H_ */
@@ -0,0 +1,24 @@
const float s_logv_2[] = {
0.000000000, 0.000000000, 1.000000000, 0.630929754, /* 0 1 2 3 */
0.500000000, 0.430676558, 0.386852807, 0.356207187, /* 4 5 6 7 */
0.333333333, 0.315464877, 0.301029996, 0.289064826, /* 8 9 10 11 */
0.278942946, 0.270238154, 0.262649535, 0.255958025, /* 12 13 14 15 */
0.250000000, 0.244650542, 0.239812467, 0.235408913, /* 16 17 18 19 */
0.231378213, 0.227670249, 0.224243824, 0.221064729, /* 20 21 22 23 */
0.218104292, 0.215338279, 0.212746054, 0.210309918, /* 24 25 26 27 */
0.208014598, 0.205846832, 0.203795047, 0.201849087, /* 28 29 30 31 */
0.200000000, 0.198239863, 0.196561632, 0.194959022, /* 32 33 34 35 */
0.193426404, 0.191958720, 0.190551412, 0.189200360, /* 36 37 38 39 */
0.187901825, 0.186652411, 0.185449023, 0.184288833, /* 40 41 42 43 */
0.183169251, 0.182087900, 0.181042597, 0.180031327, /* 44 45 46 47 */
0.179052232, 0.178103594, 0.177183820, 0.176291434, /* 48 49 50 51 */
0.175425064, 0.174583430, 0.173765343, 0.172969690, /* 52 53 54 55 */
0.172195434, 0.171441601, 0.170707280, 0.169991616, /* 56 57 58 59 */
0.169293808, 0.168613099, 0.167948779, 0.167300179, /* 60 61 62 63 */
0.166666667
};
/* $Source$ */
/* $Revision$ */
/* $Date$ */
@@ -0,0 +1,90 @@
/* Default configuration for MPI library */
/* $Id$ */
#ifndef MPI_CONFIG_H_
#define MPI_CONFIG_H_
/*
For boolean options,
0 = no
1 = yes
Other options are documented individually.
*/
#ifndef MP_IOFUNC
#define MP_IOFUNC 0 /* include mp_print() ? */
#endif
#ifndef MP_MODARITH
#define MP_MODARITH 1 /* include modular arithmetic ? */
#endif
#ifndef MP_NUMTH
#define MP_NUMTH 1 /* include number theoretic functions? */
#endif
#ifndef MP_LOGTAB
#define MP_LOGTAB 1 /* use table of logs instead of log()? */
#endif
#ifndef MP_MEMSET
#define MP_MEMSET 1 /* use memset() to zero buffers? */
#endif
#ifndef MP_MEMCPY
#define MP_MEMCPY 1 /* use memcpy() to copy buffers? */
#endif
#ifndef MP_CRYPTO
#define MP_CRYPTO 1 /* erase memory on free? */
#endif
#ifndef MP_ARGCHK
/*
0 = no parameter checks
1 = runtime checks, continue execution and return an error to caller
2 = assertions; dump core on parameter errors
*/
#define MP_ARGCHK 2 /* how to check input arguments */
#endif
#ifndef MP_DEBUG
#define MP_DEBUG 0 /* print diagnostic output? */
#endif
#ifndef MP_DEFPREC
#define MP_DEFPREC 64 /* default precision, in digits */
#endif
#ifndef MP_MACRO
#define MP_MACRO 1 /* use macros for frequent calls? */
#endif
#ifndef MP_SQUARE
#define MP_SQUARE 1 /* use separate squaring code? */
#endif
#ifndef MP_PTAB_SIZE
/*
When building mpprime.c, we build in a table of small prime
values to use for primality testing. The more you include,
the more space they take up. See primes.c for the possible
values (currently 16, 32, 64, 128, 256, and 6542)
*/
#define MP_PTAB_SIZE 128 /* how many built-in primes? */
#endif
#ifndef MP_COMPAT_MACROS
#define MP_COMPAT_MACROS 1 /* define compatibility macros? */
#endif
#endif /* ifndef MPI_CONFIG_H_ */
/* crc==3287762869, version==2, Sat Feb 02 06:43:53 2002 */
/* $Source$ */
/* $Revision$ */
/* $Date$ */
@@ -0,0 +1,22 @@
#include <stdint.h>
/* Type definitions generated by 'types.pl' */
typedef char mp_sign;
typedef uint16_t mp_digit; /* 2 byte type */
typedef uint32_t mp_word; /* 4 byte type */
typedef unsigned int mp_size;
typedef int mp_err;
#define MP_DIGIT_BIT (CHAR_BIT*sizeof(mp_digit))
#define MP_DIGIT_MAX USHRT_MAX
#define MP_WORD_BIT (CHAR_BIT*sizeof(mp_word))
#define MP_WORD_MAX ULONG_MAX
#define MP_DIGIT_SIZE 2
#define DIGIT_FMT "%04X"
#define RADIX ((uint32_t)MP_DIGIT_MAX+1)
/* $Source$ */
/* $Revision$ */
/* $Date$ */
@@ -0,0 +1,232 @@
/*
mpi.h
by Michael J. Fromberger <sting@linguist.dartmouth.edu>
Copyright (C) 1998 Michael J. Fromberger
Arbitrary precision integer arithmetic library
SPDX-License-Identifier: Unlicense
$Id$
*/
#ifndef _H_MPI_
#define _H_MPI_
#include "mpi-config.h"
#define MP_LT -1
#define MP_EQ 0
#define MP_GT 1
#if MP_DEBUG
#undef MP_IOFUNC
#define MP_IOFUNC 1
#endif
#if MP_IOFUNC
#include <stdio.h>
#include <ctype.h>
#endif
#include <limits.h>
#define MP_NEG 1
#define MP_ZPOS 0
/* Included for compatibility... */
#define NEG MP_NEG
#define ZPOS MP_ZPOS
#define MP_OKAY 0 /* no error, all is well */
#define MP_YES 0 /* yes (boolean result) */
#define MP_NO -1 /* no (boolean result) */
#define MP_MEM -2 /* out of memory */
#define MP_RANGE -3 /* argument out of range */
#define MP_BADARG -4 /* invalid parameter */
#define MP_UNDEF -5 /* answer is undefined */
#define MP_LAST_CODE MP_UNDEF
#include "mpi-types.h"
/* Included for compatibility... */
#define DIGIT_BIT MP_DIGIT_BIT
#define DIGIT_MAX MP_DIGIT_MAX
/* Macros for accessing the mp_int internals */
#define SIGN(MP) ((MP)->sign)
#define USED(MP) ((MP)->used)
#define ALLOC(MP) ((MP)->alloc)
#define DIGITS(MP) ((MP)->dp)
#define DIGIT(MP,N) (MP)->dp[(N)]
#if MP_ARGCHK == 1
#define ARGCHK(X,Y) {if(!(X)){return (Y);}}
#elif MP_ARGCHK == 2
#include <assert.h>
#define ARGCHK(X,Y) assert(X)
#else
#define ARGCHK(X,Y) /* */
#endif
/* This defines the maximum I/O base (minimum is 2) */
#define MAX_RADIX 64
typedef struct {
mp_sign sign; /* sign of this quantity */
mp_size alloc; /* how many digits allocated */
mp_size used; /* how many digits used */
mp_digit *dp; /* the digits themselves */
} mp_int;
/*------------------------------------------------------------------------*/
/* Default precision */
unsigned int mp_get_prec(void);
void mp_set_prec(unsigned int prec);
/*------------------------------------------------------------------------*/
/* Memory management */
mp_err mp_init(mp_int *mp);
mp_err mp_init_array(mp_int mp[], int count);
mp_err mp_init_size(mp_int *mp, mp_size prec);
mp_err mp_init_copy(mp_int *mp, mp_int *from);
mp_err mp_copy(mp_int *from, mp_int *to);
void mp_exch(mp_int *mp1, mp_int *mp2);
void mp_clear(mp_int *mp);
void mp_clear_array(mp_int mp[], int count);
void mp_zero(mp_int *mp);
void mp_set(mp_int *mp, mp_digit d);
mp_err mp_set_int(mp_int *mp, long z);
mp_err mp_read_from_mem(mp_int *mp, uint16_t *data, uint16_t data_size);
mp_err mp_to_mem(mp_int *mp, uint16_t *data, uint16_t data_size);
mp_err mp_shrink(mp_int *a);
/*------------------------------------------------------------------------*/
/* Single digit arithmetic */
mp_err mp_add_d(mp_int *a, mp_digit d, mp_int *b);
mp_err mp_sub_d(mp_int *a, mp_digit d, mp_int *b);
mp_err mp_mul_d(mp_int *a, mp_digit d, mp_int *b);
mp_err mp_mul_2(mp_int *a, mp_int *c);
mp_err mp_div_d(mp_int *a, mp_digit d, mp_int *q, mp_digit *r);
mp_err mp_div_2(mp_int *a, mp_int *c);
mp_err mp_expt_d(mp_int *a, mp_digit d, mp_int *c);
/*------------------------------------------------------------------------*/
/* Sign manipulations */
mp_err mp_abs(mp_int *a, mp_int *b);
mp_err mp_neg(mp_int *a, mp_int *b);
/*------------------------------------------------------------------------*/
/* Full arithmetic */
mp_err mp_add(mp_int *a, mp_int *b, mp_int *c);
mp_err mp_sub(mp_int *a, mp_int *b, mp_int *c);
mp_err mp_mul(mp_int *a, mp_int *b, mp_int *c);
mp_err mp_mul_2d(mp_int *a, mp_digit d, mp_int *c);
#if MP_SQUARE
mp_err mp_sqr(mp_int *a, mp_int *b);
#else
#define mp_sqr(a, b) mp_mul(a, a, b)
#endif
mp_err mp_div(mp_int *a, mp_int *b, mp_int *q, mp_int *r);
mp_err mp_div_2d(mp_int *a, mp_digit d, mp_int *q, mp_int *r);
mp_err mp_expt(mp_int *a, mp_int *b, mp_int *c);
mp_err mp_2expt(mp_int *a, mp_digit k);
mp_err mp_sqrt(mp_int *a, mp_int *b);
/*------------------------------------------------------------------------*/
/* Modular arithmetic */
#if MP_MODARITH
mp_err mp_mod(mp_int *a, mp_int *m, mp_int *c);
mp_err mp_mod_d(mp_int *a, mp_digit d, mp_digit *c);
mp_err mp_addmod(mp_int *a, mp_int *b, mp_int *m, mp_int *c);
mp_err mp_submod(mp_int *a, mp_int *b, mp_int *m, mp_int *c);
mp_err mp_mulmod(mp_int *a, mp_int *b, mp_int *m, mp_int *c);
#if MP_SQUARE
mp_err mp_sqrmod(mp_int *a, mp_int *m, mp_int *c);
#else
#define mp_sqrmod(a, m, c) mp_mulmod(a, a, m, c)
#endif
mp_err mp_exptmod(mp_int *a, mp_int *b, mp_int *m, mp_int *c);
mp_err mp_exptmod_d(mp_int *a, mp_digit d, mp_int *m, mp_int *c);
#endif /* MP_MODARITH */
/*------------------------------------------------------------------------*/
/* Comparisons */
int mp_cmp_z(mp_int *a);
int mp_cmp_d(mp_int *a, mp_digit d);
int mp_cmp(mp_int *a, mp_int *b);
int mp_cmp_mag(mp_int *a, mp_int *b);
int mp_cmp_int(mp_int *a, long z);
int mp_isodd(mp_int *a);
int mp_iseven(mp_int *a);
/*------------------------------------------------------------------------*/
/* Number theoretic */
#if MP_NUMTH
mp_err mp_gcd(mp_int *a, mp_int *b, mp_int *c);
mp_err mp_lcm(mp_int *a, mp_int *b, mp_int *c);
mp_err mp_xgcd(mp_int *a, mp_int *b, mp_int *g, mp_int *x, mp_int *y);
mp_err mp_invmod(mp_int *a, mp_int *m, mp_int *c);
#endif /* end MP_NUMTH */
/*------------------------------------------------------------------------*/
/* Input and output */
#if MP_IOFUNC
void mp_print(mp_int *mp, FILE *ofp);
#endif /* end MP_IOFUNC */
/*------------------------------------------------------------------------*/
/* Base conversion */
#define BITS 1
#define BYTES CHAR_BIT
mp_err mp_read_signed_bin(mp_int *mp, unsigned char *str, int len);
mp_err mp_read_unsigned_bin(mp_int *mp, unsigned char *str, int len);
int mp_count_bits(mp_int *mp);
#if MP_COMPAT_MACROS
#define mp_read_raw(mp, str, len) mp_read_signed_bin((mp), (str), (len))
#define mp_raw_size(mp) mp_signed_bin_size(mp)
#define mp_toraw(mp, str) mp_to_signed_bin((mp), (str))
#define mp_read_mag(mp, str, len) mp_read_unsigned_bin((mp), (str), (len))
#define mp_mag_size(mp) mp_unsigned_bin_size(mp)
#define mp_tomag(mp, str) mp_to_unsigned_bin((mp), (str))
#endif
mp_err mp_read_radix(mp_int *mp, unsigned char *str, int radix);
int mp_radix_size(mp_int *mp, int radix);
int mp_value_radix_size(int num, int qty, int radix);
mp_err mp_toradix(mp_int *mp, char *str, int radix);
int mp_char2value(char ch, int r);
#define mp_tobinary(M, S) mp_toradix((M), (S), 2)
#define mp_tooctal(M, S) mp_toradix((M), (S), 8)
#define mp_todecimal(M, S) mp_toradix((M), (S), 10)
#define mp_tohex(M, S) mp_toradix((M), (S), 16)
#define mp_read_hex(M, S) mp_read_radix((M), (S), 16)
/*------------------------------------------------------------------------*/
/* Error strings */
const char *mp_strerror(mp_err ec);
#endif /* end _H_MPI_ */
/* $Source$ */
/* $Revision$ */
/* $Date$ */
@@ -0,0 +1,245 @@
//###########################################################################
//
// FILE: rsassa_pkcs1_v1_5.h
//
// TITLE: RSASSA-PKCS header file
//
//###########################################################################
// $Copyright:
// Copyright (C) 2024 Texas Instruments Incorporated - http://www.ti.com/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the
// distribution.
//
// Neither the name of Texas Instruments Incorporated nor the names of
// its contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// $
//###########################################################################
#ifndef __RSASSA_PKCS1_1V_5__
#define __RSASSA_PKCS1_1V_5__
#include "mpi.h"
//*****************************************************************************
//
// RSA Private Key data structure
//
//*****************************************************************************
typedef mp_int rsa_PrivateKey;
//*****************************************************************************
//
// RSA Public Key data structure
//
//*****************************************************************************
typedef mp_int rsa_PublicKey;
//*****************************************************************************
//
// RSA Modulus data structure
//
//*****************************************************************************
typedef mp_int rsa_Modulus;
//*****************************************************************************
//
// RSA Signature data structure
//
//*****************************************************************************
typedef mp_int rsa_Signature;
//*****************************************************************************
//
// RSA error data type
//
//*****************************************************************************
typedef mp_err rsa_err;
//*****************************************************************************
//
//! Initialize RSA Public Key data structure
//!
//! \param pub_key is the pointer to the rsa_PublicKey data structure
//!
//! \return Returns MP_OKAY if successful, MP_MEM if memory could not be
//! allocated for the structure.
//
//*****************************************************************************
#define rsa_init_pubkey(pub_key) mp_init(pub_key)
//*****************************************************************************
//
//! Initialize RSA Private Key data structure
//!
//! \param pvt_key is the pointer to the rsa_PrivateKey data structure
//!
//! \return Returns MP_OKAY if successful, MP_MEM if memory could not be
//! allocated for the structure.
//
//*****************************************************************************
#define rsa_init_pvtkey(pvt_key) mp_init(pvt_key)
//*****************************************************************************
//
//! Initialize RSA Modulus data structure
//!
//! \param modulus is the pointer to the rsa_Modulus data structure
//!
//! \return Returns MP_OKAY if successful, MP_MEM if memory could not be
//! allocated for the structure.
//
//*****************************************************************************
#define rsa_init_modulus(modulus) mp_init(modulus)
//*****************************************************************************
//
//! Initialize RSA signature data structure
//!
//! \param pub_key is the pointer to the rsa_Signature data structure
//!
//! \return Returns MP_OKAY if successful, MP_MEM if memory could not be
//! allocated for the structure.
//
//*****************************************************************************
#define rsa_init_signature(sign) mp_init(sign)
//*****************************************************************************
//
//! Set RSA public key value in the data structure from a hex string
//!
//! \param pub_key is the pointer to the rsa_PublicKey data structure
//! \param pub_key_hex is the public key hex string (without the '0x' prefix)
//!
//! \return Returns MP_OKAY if successful. One of the failure macros
//! defined in mpi.h depending on the type of failure.
//
//*****************************************************************************
#define rsa_set_pubkey_from_hex(pub_key, pub_key_hex) \
mp_read_hex(pub_key, pub_key_hex)
//*****************************************************************************
//
//! Set RSA private key value in the data structure from a hex string
//!
//! \param pvt_key is the pointer to the rsa_PrivateKey data structure
//! \param pvt_key_hex is the private key hex string (without the '0x' prefix)
//!
//! \return Returns MP_OKAY if successful. One of the failure macros
//! defined in mpi.h depending on the type of failure.
//
//*****************************************************************************
#define rsa_set_pvtkey_from_hex(pvt_key, pvt_key_hex) \
mp_read_hex(pvt_key, pvt_key_hex)
//*****************************************************************************
//
//! Set RSA modulus value in the data structure from a hex string
//!
//! \param modulus is the pointer to the rsa_Modulus data structure
//! \param modulus_hex is the modulus hex string (without the '0x' prefix)
//!
//! \return Returns MP_OKAY if successful. One of the failure macros
//! defined in mpi.h depending on the type of failure.
//
//*****************************************************************************
#define rsa_set_modulus_from_hex(modulus, modulus_hex ) \
mp_read_hex(modulus, modulus_hex)
//*****************************************************************************
//
//! Extract RSA signature value to a byte array
//!
//! \param sign is the pointer to the rsa_Signature data structure
//! \param sign_str is the pointer to the string where signature has to be
//! written
//! \param sign_len is the length of the signature in bytes
//!
//! \return Returns MP_OKAY if successful.
//
//*****************************************************************************
#define rsa_signature_to_mem(sign, sign_str, sign_len ) \
mp_to_mem(sign, sign_str, sign_len);
//*****************************************************************************
//
//! Function to write data represented in a hex string into the memory bytewise
//!
//! \param hex_str is the hex string (without the '0x' prefix)
//! \param data is the pointer to the memory location where the data will be
//! written
//!
//! \return Returns 0 if successful
//! 3 if the hex string is invalid
//
//*****************************************************************************
rsa_err rsa_hex_to_data(unsigned char* hex_str, uint16_t* data);
//*****************************************************************************
//
//! Function to generate a signature of the data using RSASSA-PKCS algorithm
//!
//! \param key_pvt is the pointer to the rsa_PrivateKey data structure holding
//! the RSA private key value
//! \param modulus is the pointer to the rsa_Modulus data structure holding
//! the RSA modulus value
//! \param sign is the pointer to the rsa_Signature data structure which
//! will hold the generated the signature
//! \param hash is the pointer to the byte wise hash digest of the message
//! \param hash_size is the size of the hash based on the SHA encoding type
//!
//! \return Returns PKCS_STATUS_SUCCESS if successful
//
//*****************************************************************************
rsa_err rsassa_pkcs1_v1_5_sign(rsa_PrivateKey* key_pvt, rsa_Modulus* modulus,
rsa_Signature* sign, uint16_t *hash, uint16_t hash_size);
//*****************************************************************************
//
//! Function to verify the signature of a data using RSASSA-PKCS algorithm
//!
//! \param sign is the pointer to the rsa_Signature data structure holding
//! the signature to be verified
//! \param hash is the pointer to the bytewise hash digest of the message
//! \param hashlen is the length of the hash digest of the message in bytes
//! \param key_pub is the pointer to the rsa_PublicKey data structure holding
//! the RSA public key value
//! \param modulus is the pointer to the rsa_Modulus data structure holding
//! the RSA modulus value
//! \param result is the pointer to the verification result
//! 1 - Verification successful
//! 0 - Incorrect signature. Verification failed
//! -1 - Verification incomplete
//!
//! \return Returns PKCS_STATUS_SUCCESS if successful
//
//*****************************************************************************
rsa_err rsassa_pkcs1_v1_5_verify(rsa_Signature* sign, uint16_t* hash, uint16_t hashlen,
uint16_t mod_len, rsa_PublicKey* key_pub,
rsa_Modulus* modulus,
int16_t* result);
#endif
@@ -0,0 +1,253 @@
//###########################################################################
//
// FILE: rsassa_pss.h
//
// TITLE: RSASSA-PSS header file
//
//###########################################################################
// $Copyright:
// Copyright (C) 2024 Texas Instruments Incorporated - http://www.ti.com/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the
// distribution.
//
// Neither the name of Texas Instruments Incorporated nor the names of
// its contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// $
//###########################################################################
#ifndef RSASSA_PSS_H_
#define RSASSA_PSS_H_
#include "mpi.h"
#include "yarrow.h"
//*****************************************************************************
//
// RSA Private Key data structure
//
//*****************************************************************************
typedef mp_int rsa_PrivateKey;
//*****************************************************************************
//
// RSA Public Key data structure
//
//*****************************************************************************
typedef mp_int rsa_PublicKey;
//*****************************************************************************
//
// RSA Modulus data structure
//
//*****************************************************************************
typedef mp_int rsa_Modulus;
//*****************************************************************************
//
// RSA Signature data structure
//
//*****************************************************************************
typedef mp_int rsa_Signature;
//*****************************************************************************
//
// RSA error data type
//
//*****************************************************************************
typedef mp_err rsa_err;
//*****************************************************************************
//
//! Initialize RSA Public Key data structure
//!
//! \param pub_key is the pointer to the rsa_PublicKey data structure
//!
//! \return Returns MP_OKAY if successful, MP_MEM if memory could not be
//! allocated for the structure.
//
//*****************************************************************************
#define rsa_init_pubkey(pub_key) mp_init(pub_key)
//*****************************************************************************
//
//! Initialize RSA Private Key data structure
//!
//! \param pvt_key is the pointer to the rsa_PrivateKey data structure
//!
//! \return Returns MP_OKAY if successful, MP_MEM if memory could not be
//! allocated for the structure.
//
//*****************************************************************************
#define rsa_init_pvtkey(pvt_key) mp_init(pvt_key)
//*****************************************************************************
//
//! Initialize RSA Modulus data structure
//!
//! \param modulus is the pointer to the rsa_Modulus data structure
//!
//! \return Returns MP_OKAY if successful, MP_MEM if memory could not be
//! allocated for the structure.
//
//*****************************************************************************
#define rsa_init_modulus(modulus) mp_init(modulus)
//*****************************************************************************
//
//! Initialize RSA signature data structure
//!
//! \param pub_key is the pointer to the rsa_Signature data structure
//!
//! \return Returns MP_OKAY if successful, MP_MEM if memory could not be
//! allocated for the structure.
//
//*****************************************************************************
#define rsa_init_signature(sign) mp_init(sign)
//*****************************************************************************
//
//! Set RSA public key value in the data structure from a hex string
//!
//! \param pub_key is the pointer to the rsa_PublicKey data structure
//! \param pub_key_hex is the public key hex string (without the '0x' prefix)
//!
//! \return Returns MP_OKAY if successful. One of the failure macros
//! defined in mpi.h depending on the type of failure.
//
//*****************************************************************************
#define rsa_set_pubkey_from_hex(pub_key, pub_key_hex) \
mp_read_hex(pub_key, pub_key_hex)
//*****************************************************************************
//
//! Set RSA private key value in the data structure from a hex string
//!
//! \param pvt_key is the pointer to the rsa_PrivateKey data structure
//! \param pvt_key_hex is the private key hex string (without the '0x' prefix)
//!
//! \return Returns MP_OKAY if successful. One of the failure macros
//! defined in mpi.h depending on the type of failure.
//
//*****************************************************************************
#define rsa_set_pvtkey_from_hex(pvt_key, pvt_key_hex) \
mp_read_hex(pvt_key, pvt_key_hex)
//*****************************************************************************
//
//! Set RSA modulus value in the data structure from a hex string
//!
//! \param modulus is the pointer to the rsa_Modulus data structure
//! \param modulus_hex is the modulus hex string (without the '0x' prefix)
//!
//! \return Returns MP_OKAY if successful. One of the failure macros
//! defined in mpi.h depending on the type of failure.
//
//*****************************************************************************
#define rsa_set_modulus_from_hex(modulus, modulus_hex ) \
mp_read_hex(modulus, modulus_hex)
//*****************************************************************************
//
//! Extract RSA signature value to a byte array
//!
//! \param sign is the pointer to the rsa_Signature data structure
//! \param sign_str is the pointer to the string where signature has to be
//! written
//! \param sign_len is the length of the signature in bytes
//!
//! \return Returns MP_OKAY if successful.
//
//*****************************************************************************
#define rsa_signature_to_mem(sign, sign_str, sign_len ) \
mp_to_mem(sign, sign_str, sign_len);
//*****************************************************************************
//
//! Function to write data represented in a hex string into the memory bytewise
//!
//! \param hex_str is the hex string (without the '0x' prefix)
//! \param data is the pointer to the memory location where the data will be
//! written
//!
//! \return Returns 0 if successful
//! 3 if the hex string is invalid
//
//*****************************************************************************
rsa_err rsa_hex_to_data(unsigned char *hex_str, uint16_t *data);
//*****************************************************************************
//
//! Function to generate a signature of the data using RSASSA-PSS algorithm
//!
//! \param hash is the pointer to the bytewise hash digest of the message
//! \param hashlen is the length of the hash digest of the message in bytes
//! \param saltlen is the length of the random salt in bytes
//! \param key_pvt is the pointer to the rsa_PrivateKey data structure holding
//! the RSA private key value
//! \param modulus is the pointer to the rsa_Modulus data structure holding
//! the RSA modulus value
//! \param modulus_bitlen is the length of the RSA modulus in bits (not bytes)
//! \param prng is the pointer to the yarrow prng handler to generate
//! random salt
//! \param sign is the pointer to the rsa_Signature data structure which
//! will hold the generated the signature
//!
//! \return Returns PKCS_STATUS_SUCCESS if successful
//
//*****************************************************************************
rsa_err rsassa_pss_sign(uint16_t *hash, uint16_t hashlen, uint16_t saltlen,
rsa_PrivateKey *key_pvt, rsa_Modulus *modulus,
uint16_t modulus_bitlen, yarrow_prng *prng,
rsa_Signature *sign);
//*****************************************************************************
//
//! Function to verify the signature of a data using RSASSA-PSS algorithm
//!
//! \param sign is the pointer to the rsa_Signature data structure holding
//! the signature to be verified
//! \param hash is the pointer to the bytewise hash digest of the message
//! \param hashlen is the length of the hash digest of the message in bytes
//! \param saltlen is the length of the random salt in bytes
//! \param key_pub is the pointer to the rsa_PublicKey data structure holding
//! the RSA public key value
//! \param modulus is the pointer to the rsa_Modulus data structure holding
//! the RSA modulus value
//! \param modulus_bitlen is the length of the RSA modulus in bits (not bytes)
//! \param result is the pointer to the verification result
//! 1 - Verification successful
//! 0 - Incorrect signature. Verification failed
//! -1 - Verification incomplete
//!
//! \return Returns PKCS_STATUS_SUCCESS if successful
//
//*****************************************************************************
rsa_err rsassa_pss_verify( rsa_Signature *sign, uint16_t *hash, uint16_t hashlen,
uint16_t saltlen, rsa_PublicKey *key_pub,
rsa_Modulus *modulus, uint16_t modulus_bitlen,
int16_t *result);
#endif /* RSASSA_PSS_H_ */
@@ -0,0 +1,136 @@
//###########################################################################
//
// FILE: yarrow.h
//
// TITLE: Yarrow PRNG algorithm header file
//
//###########################################################################
// $Copyright:
// Copyright (C) 2024 Texas Instruments Incorporated - http://www.ti.com/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the
// distribution.
//
// Neither the name of Texas Instruments Incorporated nor the names of
// its contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// $
//###########################################################################
#ifndef YARROW_H_
#define YARROW_H_
#ifdef __cplusplus
extern "C"
{
#endif
#include "aes_ctr.h"
#include "sha256.h"
typedef unsigned char uc;
#define MAXBLOCKSIZE 144
#define YARROW_STATUS_SUCCESS 1
#define YARROW_STATUS_ERROR_NULL -1
//*****************************************************************************
//
// Data structure to handle Yarrow PRNG algortithm
//
//*****************************************************************************
typedef struct yarrow_prng
{
uc pool[MAXBLOCKSIZE];
uc ready;
uc nounceCounter[16];
uc key[32];
} yarrow_prng;
//*****************************************************************************
//
//! Initialize yarrow handle with nounce counter values
//!
//! \param prng is the pointer to the yarrow PRNG handle
//! \param nounceCounter is the pointer to the array which contains the counter
//! values to be initialized with
//!
//! \return Returns YARROW_STATUS_SUCCESS if successful.
//
//*****************************************************************************
int yarrow_init_with_nounce(yarrow_prng *prng, const uc *nounceCounter);
//*****************************************************************************
//
//! Initialize yarrow handle with default nounce counter value '0'
//!
//! \param prng is the pointer to the yarrow PRNG handle
//!
//! \return Returns YARROW_STATUS_SUCCESS if successful.
//
//*****************************************************************************
int yarrow_init_default_nounce(yarrow_prng *prng);
//*****************************************************************************
//
//! Introduce entropy in the algorithm to generate pseudo-random values
//!
//! \param in is the random input string which will be used to introduce entropy
//! \param inlen is the length of the random input string
//! \param prng is the pointer to the yarrow PRNG handle
//!
//! \return Returns YARROW_STATUS_SUCCESS if successful.
//
//*****************************************************************************
int yarrow_add_entropy(uc *in, const uint32_t inlen, yarrow_prng *prng);
//*****************************************************************************
//
//! Prepare the yarrow PRNG handle to generate random values
//!
//! \param prng is the pointer to the yarrow PRNG handle
//!
//! \return Returns YARROW_STATUS_SUCCESS if successful.
//
//*****************************************************************************
int yarrow_ready(yarrow_prng *prng);
//*****************************************************************************
//
//! Read random values using yarrow PRNG
//!
//! \param prng is the pointer to the yarrow PRNG handle
//! \param out is the output string of random values
//! \param outlen is the length of the output string of random values
//!
//! \return Returns outlen if successful.
//
//*****************************************************************************
int yarrow_read(yarrow_prng *prng, uc *out, uc outlen);
#ifdef __cplusplus
}
#endif
#endif /* YARROW_H_ */
@@ -0,0 +1,165 @@
//###########################################################################
//
// FILE: emsa_pkcs1_v1_5.c
//
// TITLE: EMSA-PKCS message encoding and decoding
//
//###########################################################################
#include "emsa_pkcs1_v1_5.h"
#include <stdlib.h>
#include <string.h>
//*****************************************************************************
//
//ASN.1 defines
//
//*****************************************************************************
#define ASN_1_ENCODING 0x20
#define ASN_1_TYPE_BIT_STR 0x03
#define ASN_1_TYPE_OCTET_STR 0x04
#define ASN_1_TYPE_NULL 0x05
#define ASN_1_TYPE_OBJ_ID 0x06
#define ASN_1_TYPE_SEQ 0x10
//*****************************************************************************
//
//SHA256 related data for padding
//
//*****************************************************************************
#define SHA256_DIGEST_SIZE 0x20
//*****************************************************************************
//
//SHA384 related data for padding
//
//*****************************************************************************
#define SHA384_DIGEST_SIZE 0x30
//*****************************************************************************
//
//SHA384 digest size (384 bits = 32 bytes)
//
//*****************************************************************************
#define SHA256_DIGEST_SIZE 0x20
//*****************************************************************************
//
//SHA256 with ASN.1 padding
//
//*****************************************************************************
const uint16_t SHA256_ASN_1_PADDING[ASN_1_PAD_SIZE] = \
{ \
0x00, 0x01, \
0xFF, 0xFF, 0xFF, 0xFF, \
0xFF, 0xFF, 0xFF, 0xFF, \
0x00, \
0x30, 0x31, 0x30, 0x0D, \
0x06, 0x09, 0x60, 0x86, \
0x48, 0x01, 0x65, 0x03, \
0x04, 0x02, 0x01, 0x05, \
0x00, 0x04, 0x20
};
//*****************************************************************************
//
//SHA384 with ASN.1 padding
//
//*****************************************************************************
const uint16_t SHA384_ASN_1_PADDING[ASN_1_PAD_SIZE] = \
{ \
0x00, 0x01, \
0xFF, 0xFF, 0xFF, 0xFF, \
0xFF, 0xFF, 0xFF, 0xFF, \
0x00, \
0x30, 0x41, 0x30, 0x0D, \
0x06, 0x09, 0x60, 0x86, \
0x48, 0x01, 0x65, 0x03, \
0x04, 0x02, 0x02, 0x05, \
0x00, 0x04, 0x30
};
//*****************************************************************************
//
// EMSA-PKCS1 encode message
//
//*****************************************************************************
int emsa_pkcs1_v1_5_encode(uint16_t* emsa_out, uint16_t* hash, uint16_t hash_size)
{
const uint16_t* shaPadding = NULL;
if(emsa_out == NULL)
{
return PKCS_STATUS_FAILURE_MEM;
}
if(hash_size == SHA256_HASH_SIZE)
{
shaPadding = SHA256_ASN_1_PADDING;
}
else if(hash_size == SHA384_HASH_SIZE)
{
shaPadding = SHA384_ASN_1_PADDING;
}
// Pad the sequence as EM = 0x00 || 0x01 || PS || 0x00 || T
memcpy(emsa_out, shaPadding, ASN_1_PAD_SIZE);
//Append the hash value
memcpy(emsa_out + ASN_1_PAD_SIZE, hash, hash_size);
return PKCS_STATUS_SUCCESS;
}
//*****************************************************************************
//
// Function to verify the signature of a data using RSASSA-PKCS algorithm
//
//*****************************************************************************
int emsa_pkcs1_v1_5_decode(uint16_t* EM_Str, uint16_t *hash, uint16_t hash_size, int16_t* result)
{
int err = 0;
const uint16_t* shaPadding = NULL;
if ((EM_Str == NULL) || (result == NULL))
{
*result = -1;
return PKCS_STATUS_FAILURE_NULL;
}
*result = -1;
//Check signature length is more than the length of 0x00 || 0x01 || PS || 0x00 || T
if(hash_size == SHA256_HASH_SIZE)
{
shaPadding = SHA256_ASN_1_PADDING;
}
else if(hash_size == SHA384_HASH_SIZE)
{
shaPadding = SHA384_ASN_1_PADDING;
}
//
// Check Padding
//
if(memcmp(EM_Str, shaPadding, ASN_1_PAD_SIZE) != 0)
{
*result = 0;
return PKCS_STATUS_FAILURE;
}
// Compare expected hash vs computed hash
if(memcmp(EM_Str + ASN_1_PAD_SIZE, hash, hash_size) != 0)
{
*result = 0;
return PKCS_STATUS_FAILURE;
}
*result = 1;
return err;
}
//
// End of file
//
@@ -0,0 +1,555 @@
//###########################################################################
//
// FILE: emsa_pss.c
//
// TITLE: EMSA-PSS message encoding and decoding
//
//###########################################################################
// $Copyright:
// Copyright (C) 2024 Texas Instruments Incorporated - http://www.ti.com/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the
// distribution.
//
// Neither the name of Texas Instruments Incorporated nor the names of
// its contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// $
//###########################################################################
#include "emsa_pss.h"
#include "yarrow.h"
#include "sha256.h"
#include <stdlib.h>
#include <string.h>
//*****************************************************************************
//
// SHA-256 handle
//
//*****************************************************************************
extern SHA256_ObjectByteWise md;
//*****************************************************************************
//
// PKCS1_MGF mask generation using SHA-256
//
//*****************************************************************************
int pkcs_1_mgf(uint16_t *seed, int16_t seedlen, uint16_t *mask, int16_t masklen)
{
int16_t hLen, x, err;
uint32_t counter;
uint16_t *buf;
if (seed == NULL || mask == NULL)
{
return PKCS_STATUS_FAILURE_NULL;
}
hLen = HASH_SIZE;
buf = malloc(hLen * sizeof(uint16_t));
if (buf == NULL)
{
return PKCS_STATUS_FAILURE_MEM;
}
counter = 0;
while (masklen > 0)
{
//
// handle counter
//
STORE32H(counter, buf);
++counter;
uint32_t digest[8];
int i, j;
if ((err = SHA256_startByteWise(&md)) != SHA256_STATUS_SUCCESS)
{
return err;
}
if ((err = SHA256_addDataByteWise(&md, (uint16_t *)seed, seedlen)) != SHA256_STATUS_SUCCESS)
{
return err;
}
if ((err = SHA256_addDataByteWise(&md, (uint16_t *)buf, 4)) != SHA256_STATUS_SUCCESS)
{
return err;
}
if ((err = SHA256_finalizeByteWise(&md, digest)) != SHA256_STATUS_SUCCESS)
{
return err;
}
for (i = 0, j = 0; i < (HASH_SIZE/4) && j < HASH_SIZE; i++)
{
buf[j++] = (digest[i] >> 24) & 255;
buf[j++] = (digest[i] >> 16) & 255;
buf[j++] = (digest[i] >> 8) & 255;
buf[j++] = (digest[i]) & 255;
}
//
// store it
//
for (x = 0; x < hLen && masklen > 0; x++, masklen--)
{
*mask++ = buf[x];
}
}
free(buf);
return PKCS_STATUS_SUCCESS;
}
//*****************************************************************************
//
// EMSA-PSS encode message
//
//*****************************************************************************
int emsa_pss_encode(uint16_t *msghash, uint16_t msghashlen, uint16_t saltlen,
uint16_t modulus_bitlen, yarrow_prng *prng,
uint16_t *emsa_out, uint16_t emsa_outlen)
{
uint16_t *DB, *mask, *salt, *hash;
uint16_t x, y, hLen, modulus_len;
int err, i ,j;
if ((msghash == NULL) || (emsa_out == NULL) || (msghashlen <= 0))
{
return PKCS_STATUS_FAILURE_NULL;
}
hLen = HASH_SIZE;
modulus_bitlen--;
modulus_len = (modulus_bitlen >> 3) + (modulus_bitlen & 7 ? 1 : 0);
//
// check sizes
//
if ((saltlen > modulus_len) || (modulus_len < hLen + saltlen + 2))
{
return PKCS_STATUS_FAILURE_SIZE;
}
//
// allocate ram for DB/mask/salt/hash
//
DB = (uint16_t *)malloc((modulus_len - hLen - 1) * sizeof(uint16_t));
mask = (uint16_t *)malloc((modulus_len - hLen -1) * sizeof(uint16_t));
salt = (uint16_t *)malloc(saltlen * sizeof(uint16_t));
hash = (uint16_t *)malloc(hLen * sizeof(uint16_t));
if (DB == NULL || mask == NULL || salt == NULL || hash == NULL)
{
if (DB != NULL)
{
free(DB);
}
if (mask != NULL)
{
free(mask);
}
if (salt != NULL)
{
free(salt);
}
if (hash != NULL)
{
free(hash);
}
return PKCS_STATUS_FAILURE_MEM;
}
if (saltlen > 0)
{
//
// Generate random salt using yarrow PRNG
//
if (yarrow_read(prng, (uc *)salt, saltlen) != saltlen)
{
return PKCS_STATUS_FAILURE_READ;
}
}
for (i = 0; i < 8; i++)
DB[i] = '\0';
uint32_t digest[8];
//
// M = (eight) 0x00 || msghash || salt, hash = H(M)
//
if ((err = SHA256_startByteWise(&md)) != SHA256_STATUS_SUCCESS)
{
return err;
}
if ((err = SHA256_addDataByteWise(&md, (uint16_t *)DB, 8)) != SHA256_STATUS_SUCCESS)
{
return err;
}
if ((err = SHA256_addDataByteWise(&md, (uint16_t *)msghash, msghashlen)) != SHA256_STATUS_SUCCESS)
{
return err;
}
if ((err = SHA256_addDataByteWise(&md, (uint16_t *)salt, saltlen)) != SHA256_STATUS_SUCCESS)
{
return err;
}
if ((err = SHA256_finalizeByteWise(&md, digest)) != SHA256_STATUS_SUCCESS)
{
return err;
}
for (i = 0, j = 0; i < (HASH_SIZE/4) && j < HASH_SIZE; i++)
{
hash[j++] = (digest[i] >> 24) & 255;
hash[j++] = (digest[i] >> 16) & 255;
hash[j++] = (digest[i] >> 8) & 255;
hash[j++] = (digest[i]) & 255;
}
//
// generate DB = PS || 0x01 || salt, PS == modulus_len - saltlen - hLen - 2 zero bytes
//
x = 0;
//
// memset(DB+x, 0, modulus_len - saltlen - hLen - 2);
//
for (x = 0; x < modulus_len - saltlen - hLen - 2; x++)
{
DB[x] = 0;
}
DB[x++] = 0x01;
//
// memcpy(DB + x, salt, saltlen);
//
for (i = 0; i < saltlen; i++)
{
DB[x++] = salt[i];
}
//
// x += saltlen;
// generate mask of length modulus_len - hLen - 1 from hash
//
if ((err = pkcs_1_mgf(hash, hLen, mask, modulus_len - hLen - 1)) != PKCS_STATUS_SUCCESS)
{
return PKCS_STATUS_FAILURE;
}
//
// xor against DB
//
for (y = 0; y < (modulus_len - hLen - 1); y++)
{
DB[y] ^= mask[y];
}
//
// output is DB || hash || 0xBC
//
if (emsa_outlen < modulus_len)
{
emsa_outlen = modulus_len;
err = PKCS_STATUS_FAILURE_MEM;
return err;
}
//
// DB len = modulus_len - hLen - 1
//
y = 0;
//
// memcpy(out + y, DB, modulus_len - hLen - 1);
//
for (y = 0; y < modulus_len - hLen - 1; y++)
{
emsa_out[y] = DB[y];
}
//
// y += modulus_len - hLen - 1;
// hash: memcpy(out + y, hash, hLen);
//
for (i = 0; i < hLen; i++)
{
emsa_out[y++] = hash[i];
}
//
// y += hLen;
// 0xBC
//
emsa_out[y] = 0xBC;
//
// now clear the 8*modulus_len - modulus_bitlen most significant bits
//
emsa_out[0] &= 0xFF >> ((modulus_len << 3) - modulus_bitlen);
//
// store output size
//
emsa_outlen = modulus_len;
err = PKCS_STATUS_SUCCESS;
//
// Free allocated memory
//
free(hash);
free(salt);
free(mask);
free(DB);
return err;
}
//*****************************************************************************
//
// Function to verify the signature of a data using RSASSA-PSS algorithm
//
//*****************************************************************************
int emsa_pss_decode(uint16_t *sig, uint16_t siglen, uint16_t *msghash, uint16_t msghashlen,
uint16_t saltlen, uint16_t modulus_bitlen, int16_t *res)
{
uint16_t *DB, *mask, *salt, *hash;
int x, y, hLen, modulus_len, i, j;
int err;
*res = -1;
if ((msghash == NULL) || (sig == NULL))
{
return PKCS_STATUS_FAILURE_NULL;
}
hLen = HASH_SIZE;
modulus_bitlen--;
modulus_len = (modulus_bitlen >> 3) + (modulus_bitlen & 7 ? 1 : 0);
//
// check sizes
//
if ((saltlen > modulus_len) ||
(modulus_len < hLen + saltlen + 2))
{
return PKCS_STATUS_FAILURE_SIZE;
}
//
// allocate ram for DB/mask/salt/hash
//
DB = (uint16_t *)malloc((modulus_len - hLen - 1) * sizeof(uint16_t));
mask = (uint16_t *)malloc((modulus_len - hLen -1) * sizeof(uint16_t));
salt = (uint16_t *)malloc(saltlen * sizeof(uint16_t));
hash = (uint16_t *)malloc(hLen * sizeof(uint16_t));
if (DB == NULL || mask == NULL || salt == NULL || hash == NULL)
{
if (DB != NULL)
{
free(DB);
}
if (mask != NULL)
{
free(mask);
}
if (salt != NULL)
{
free(salt);
}
if (hash != NULL)
{
free(hash);
}
return PKCS_STATUS_FAILURE_MEM;
}
//
// ensure the 0xBC byte
//
if (sig[siglen - 1] != 0xBC)
{
err = PKCS_STATUS_FAILURE;
*res = 0;
return err;
}
//
// copy out the DB
//
x = 0;
//
// memcpy(DB, sig + x, modulus_len - hLen - 1);
//
for (x = 0; x < modulus_len - hLen - 1; x++)
{
DB[x] = sig[x];
}
//
// x += modulus_len - hLen - 1;
// copy out the hash
// memcpy(hash, sig + x, hLen);
//
for (i = 0; i < hLen; i++)
{
hash[i] = sig[x++];
}
//
// check the MSB
//
if ((sig[0] & ~(0xFF >> ((modulus_len << 3) - (modulus_bitlen)))) != 0)
{
err = PKCS_STATUS_FAILURE;
*res = 0;
return err;
}
//
// generate mask of length modulus_len - hLen - 1 from hash
//
if ((err = pkcs_1_mgf(hash, hLen, mask, modulus_len - hLen - 1)) != PKCS_STATUS_SUCCESS)
{
return PKCS_STATUS_FAILURE;
}
//
// xor against DB
//
for (y = 0; y < (modulus_len - hLen - 1); y++)
{
DB[y] ^= mask[y];
}
//
// now clear the first byte [make sure smaller than modulus
//
DB[0] &= 0xFF >> ((modulus_len << 3) - (modulus_bitlen));
for (x = 0; x < modulus_len - saltlen - hLen - 2; x++)
{
if (DB[x] != 0x00)
{
err = PKCS_STATUS_FAILURE;
*res = 0;
return err;
}
}
//
// check for the 0x01
//
if (DB[x++] != 0x01)
{
err = PKCS_STATUS_FAILURE;
*res = 0;
return err;
}
//
// M = (eight) 0x00 || msghash || salt, mask = H(M)
//
uint32_t digest[8];
if ((err = SHA256_startByteWise(&md)) != SHA256_STATUS_SUCCESS)
{
return err;
}
for (i = 0; i < 8; i++)
{
mask[i] = '\0';
}
if ((err = SHA256_addDataByteWise(&md, (uint16_t *)mask, 8)) != SHA256_STATUS_SUCCESS)
{
return err;
}
if ((err = SHA256_addDataByteWise(&md, (uint16_t *)msghash, msghashlen)) != SHA256_STATUS_SUCCESS)
{
return err;
}
if ((err = SHA256_addDataByteWise(&md, (uint16_t *)DB + x, saltlen)) != SHA256_STATUS_SUCCESS)
{
return err;
}
if ((err = SHA256_finalizeByteWise(&md, digest)) != SHA256_STATUS_SUCCESS)
{
return err;
}
for (i = 0, j = 0; i < (HASH_SIZE/4) && j < HASH_SIZE; i++)
{
mask[j++] = (digest[i] >> 24) & 255;
mask[j++] = (digest[i] >> 16) & 255;
mask[j++] = (digest[i] >> 8) & 255;
mask[j++] = (digest[i]) & 255;
}
for (i = 0; i < hLen; i++)
{
if (mask[i] != hash[i])
{
err = PK_STATUS_INVALID;
break;
}
}
if (err != PK_STATUS_INVALID)
{
err = PKCS_STATUS_SUCCESS;
*res = 1;
}
else
{
*res = 0;
}
//
// Free allocated memory
//
free(hash);
free(salt);
free(mask);
free(DB);
return err;
}
//
// End of file
//
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,157 @@
//###########################################################################
//
// FILE: rsassa_pkcs1_v1_5.c
//
// TITLE: RSASSA-PKCS algorithm to generate and verify digital signature
//
//###########################################################################
// $Copyright:
// Copyright (C) 2024 Texas Instruments Incorporated - http://www.ti.com/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the
// distribution.
//
// Neither the name of Texas Instruments Incorporated nor the names of
// its contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// $
//###########################################################################
#include "rsassa_pkcs1_v1_5.h"
#include "emsa_pkcs1_v1_5.h"
#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
rsa_err rsassa_pkcs1_v1_5_sign(rsa_PrivateKey* key_pvt, rsa_Modulus* modulus,
rsa_Signature* sign, uint16_t *hash, uint16_t hash_size)
{
//Array to hold the padding string
uint16_t EM_str[256];
//Padding length
uint16_t EM_len = ASN_1_PAD_SIZE + hash_size;
//
// Multiple precision integer to hold the encoded message
//
mp_int EM;
//
// EMSA-PKCS1 encoding of the message hash
//
if(PKCS_STATUS_SUCCESS != emsa_pkcs1_v1_5_encode(EM_str, hash, hash_size))
{
return -1;
}
if(0 != mp_init(&EM))
{
return -1;
}
if(0 != mp_read_from_mem(&EM, EM_str, EM_len))
{
mp_clear(&EM);
return -1;
}
//
// Modular exponentiation to generate signature
//
if(0 != mp_exptmod(&EM, key_pvt, modulus, sign))
{
mp_clear(&EM);
mp_clear(sign);
return -1;
}
mp_clear(&EM);
return 0;
}
rsa_err rsassa_pkcs1_v1_5_verify(rsa_Signature* sign, uint16_t* hash, uint16_t hash_size,
uint16_t mod_len, rsa_PublicKey* key_pub,
rsa_Modulus* modulus,
int16_t* result)
{
rsa_err rErr = 0;
//
// Array to hold the PKCS encoding of the message
//
uint16_t *EM_str = NULL;
//
// Multiple precision integer to hold the encoded message
//
mp_int EM;
if(mp_init(&EM) != 0)
{
*result = -1;
return -1;
}
//
// Modular exponentiation to generate the message encoding from the signature
//
if(mp_exptmod(sign, key_pub, modulus, &EM) != 0)
{
*result = -1;
mp_clear(&EM);
return -1;
}
EM_str = (uint16_t *)malloc(mod_len);
if(EM_str == NULL)
{
*result = -1;
return -1;
}
if(mp_to_mem(&EM, EM_str, mod_len) != 0)
{
*result = -1;
free(EM_str);
mp_clear(&EM);
return -1;
}
rErr = emsa_pkcs1_v1_5_decode(EM_str, hash, hash_size, result);
if(EM_str)
{
free(EM_str);
}
mp_clear(&EM);
return rErr;
}
@@ -0,0 +1,181 @@
//###########################################################################
//
// FILE: rsassa_pss.c
//
// TITLE: RSASSA-PSS algorithm to generate and verify digital signature
//
//###########################################################################
// $Copyright:
// Copyright (C) 2024 Texas Instruments Incorporated - http://www.ti.com/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the
// distribution.
//
// Neither the name of Texas Instruments Incorporated nor the names of
// its contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// $
//###########################################################################
#include "rsassa_pss.h"
#include "emsa_pss.h"
#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
//*****************************************************************************
//
// Function to write data represented in a hex string into the memory bytewise
//
//*****************************************************************************
rsa_err rsa_hex_to_data(unsigned char *hex_str, uint16_t *data)
{
rsa_err err = 0;
uint16_t i = 0, j = 0;
while(i < strlen((char *)hex_str))
{
if (((uint16_t)hex_str[i] >= 48) && ((uint16_t)hex_str[i] <= 57))
data[j] = (uint16_t)hex_str[i] - 48;
else if (((uint16_t)hex_str[i] >= 65) && ((uint16_t)hex_str[i] <= 70))
data[j] = (uint16_t)hex_str[i] - 55;
else if (((uint16_t)hex_str[i] >= 97) && ((uint16_t)hex_str[i] <= 102))
data[j] = (uint16_t)hex_str[i] - 87;
else
{
err = 3;
return err;
}
if(++i >= strlen((char *)hex_str))
break;
data[j] <<= 4;
if (((uint16_t)hex_str[i] >= 48) && ((uint16_t)hex_str[i] <= 57))
data[j] |= ((uint16_t)hex_str[i] - 48);
else if (((uint16_t)hex_str[i] >= 65) && ((uint16_t)hex_str[i] <= 70))
data[j] |= ((uint16_t)hex_str[i] - 55);
else if (((uint16_t)hex_str[i] >= 97) && ((uint16_t)hex_str[i] <= 102))
data[j] |= ((uint16_t)hex_str[i] - 87);
else
{
err = 3;
return err;
}
i++;
j++;
}
return err;
}
//*****************************************************************************
//
// Function to generate a signature of the data using RSASSA-PSS algorithm
//
//*****************************************************************************
rsa_err rsassa_pss_sign(uint16_t *hash, uint16_t hashlen, uint16_t saltlen,
rsa_PrivateKey *key_pvt, rsa_Modulus *modulus,
uint16_t modulus_bitlen, yarrow_prng *prng,
rsa_Signature *sign)
{
rsa_err err;
uint16_t EM_len = modulus_bitlen/8;
//
// Array to hold the pss encoding of the message
//
uint16_t *EM_str = (uint16_t *)malloc(EM_len);
//
// Multiple precision integer to hold the encoded message
//
mp_int EM;
//
// EMSA-PSS encoding of the message hash
//
err = emsa_pss_encode(hash, hashlen, saltlen, modulus_bitlen, prng, \
EM_str, EM_len);
mp_init(&EM);
mp_read_from_mem(&EM, EM_str, EM_len);
mp_init(sign);
//
// Modular exponentiation to generate signature
//
mp_exptmod(&EM, key_pvt, modulus, sign);
return err;
}
//*****************************************************************************
//
// Function to verify the signature of a data using RSASSA-PSS algorithm
//
//
//*****************************************************************************
rsa_err rsassa_pss_verify( rsa_Signature *sign, uint16_t *hash, uint16_t hashlen,
uint16_t saltlen, rsa_PublicKey *key_pub,
rsa_Modulus *modulus, uint16_t modulus_bitlen,
int16_t *result)
{
rsa_err err;
uint16_t EM_len = modulus_bitlen/8;
//
// Array to hold the pss encoding of the message
//
uint16_t *EM_str = (uint16_t *)malloc(EM_len);
//
// Multiple precision integer to hold the encoded message
//
mp_int EM;
mp_init(&EM);
//
// Modular exponentiation to generate the message encoding from the signature
//
mp_exptmod(sign, key_pub, modulus, &EM);
mp_to_mem(&EM, EM_str, EM_len);
//
// EMSA-PSS decoding to verify the message encoding
//
err = emsa_pss_decode(EM_str, EM_len, hash, hashlen, saltlen, modulus_bitlen, result);
return err;
}
//
// End of file
//
@@ -0,0 +1,251 @@
//###########################################################################
//
// FILE: yarrow.c
//
// TITLE: Yarrow PRNG algorithm
//
//###########################################################################
// $Copyright:
// Copyright (C) 2024 Texas Instruments Incorporated - http://www.ti.com/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the
// distribution.
//
// Neither the name of Texas Instruments Incorporated nor the names of
// its contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// $
//###########################################################################
#include "yarrow.h"
//
// SHA-256 handle
//
SHA256_ObjectByteWise hash;
//*****************************************************************************
//
// Initialize yarrow handle with nounce counter values
//
//*****************************************************************************
int yarrow_init_with_nounce(yarrow_prng *prng, const uc *nounceCounter)
{
if(prng == NULL || nounceCounter == NULL)
{
return YARROW_STATUS_ERROR_NULL;
}
uint32_t i = 0;
while (i < MAXBLOCKSIZE)
{
prng->pool[i] = 0;
i++;
}
prng->ready = 0;
for (i = 0; i < 16U; i++)
{
prng->nounceCounter[i] = nounceCounter[i];
}
return YARROW_STATUS_SUCCESS;
}
//*****************************************************************************
//
// Initialize yarrow handle with default nounce counter value '0'
//
//*****************************************************************************
int yarrow_init_default_nounce(yarrow_prng *prng)
{
if(prng == NULL)
{
return YARROW_STATUS_ERROR_NULL;
}
uint32_t i = 0;
while (i < MAXBLOCKSIZE)
{
prng->pool[i] = 0;
i++;
}
prng->ready = 0;
for (i = 0; i < 16U; i++)
{
prng->nounceCounter[i] = 0;
}
return YARROW_STATUS_SUCCESS;
}
//*****************************************************************************
//
// Introduce entropy in the algorithm to generate pseudo-random values
//
//*****************************************************************************
int yarrow_add_entropy(uc *in, const uint32_t inlen, yarrow_prng *prng)
{
if(prng == NULL || in == NULL || inlen <= 0)
{
return YARROW_STATUS_ERROR_NULL;
}
uc error_msg;
//
// Initialize a hash object
//
if ((error_msg = SHA256_startByteWise(&hash)) != SHA256_STATUS_SUCCESS)
{
return error_msg;
}
//
// Hash the initial pool
//
if ((error_msg = SHA256_addDataByteWise(&hash, (uint16_t*)prng->pool, 32U)) != SHA256_STATUS_SUCCESS)
{
return error_msg;
}
//
// Now add the entropy to the pool
//
if ((error_msg = SHA256_addDataByteWise(&hash, (uint16_t*)in, inlen)) != SHA256_STATUS_SUCCESS)
{
return error_msg;
}
//
// Now shift the data from sha256 handle to a temporary digest
//
uint32_t digest[8];
if ((error_msg = SHA256_finalizeByteWise(&hash, digest)) != SHA256_STATUS_SUCCESS)
{
return error_msg;
}
//
// Shift the data from digest to prng->pool
//
uint32_t i, j;
for (i = 0, j = 0; i < 8 && j < 32; i++)
{
prng->pool[j++] = (digest[i] >> 24) & 255;
prng->pool[j++] = (digest[i] >> 16) & 255;
prng->pool[j++] = (digest[i] >> 8) & 255;
prng->pool[j++] = (digest[i]) & 255;
}
return YARROW_STATUS_SUCCESS;
}
//*****************************************************************************
//
// Prepare the yarrow PRNG handle to generate random values
//
//*****************************************************************************
int yarrow_ready(yarrow_prng *prng)
{
if(prng == NULL)
{
return YARROW_STATUS_ERROR_NULL;
}
uint32_t i;
for (i = 0; i < 32; i++)
{
prng->key[i] = prng->pool[i];
}
prng->ready = 1;
return YARROW_STATUS_SUCCESS;
}
//*****************************************************************************
//
// Read random values using yarrow PRNG
//
//*****************************************************************************
int yarrow_read( yarrow_prng *prng, uc *out, uc outlen)
{
//
// Check for invalid input parameters
//
if(prng == NULL || out == NULL || outlen <= 0)
{
return YARROW_STATUS_ERROR_NULL;
}
//
// Check if prng is ready
//
if (prng->ready != 1)
{
return 0;
}
//
// Initialize the out array to 0
//
int i;
for (i = 0; i < outlen; i++)
{
out[i] = 0;
}
//
// Populate the out array with pseudo-random values
//
i = 0;
int len = 0;
int outlenCopy = outlen;
while (outlen > 0)
{
int currlen = (outlen < 32) ? outlen : 32;
AES_performCTR((uint16_t*)prng->pool, 2, (uint16_t*)prng->key,\
AES_256, AES_OPMODE_ENCRYPT,\
(uint16_t*)prng->nounceCounter);
int ptr;
for (ptr = 0; ptr < currlen; ptr++)
{
out[i++] = prng->pool[ptr];
}
outlen -= currlen;
len += currlen;
}
if (outlenCopy == len)
return len;
else
return 0;
}
//
// End of file
//