rsa_misc.c File Reference

Helper routines for RSA. More...

#include "core/crypto.h"
#include "pkc/rsa.h"
#include "pkc/rsa_misc.h"
#include "encoding/asn1.h"
#include "debug.h"

Go to the source code of this file.

Macros

#define TRACE_LEVEL   CRYPTO_TRACE_LEVEL
 

Functions

__weak_func error_t rsaep (const RsaPublicKey *key, const Mpi *m, Mpi *c)
 RSA encryption primitive. More...
 
__weak_func error_t rsadp (const RsaPrivateKey *key, const Mpi *c, Mpi *m)
 RSA decryption primitive. More...
 
error_t rsasp1 (const RsaPrivateKey *key, const Mpi *m, Mpi *s)
 RSA signature primitive. More...
 
error_t rsavp1 (const RsaPublicKey *key, const Mpi *s, Mpi *m)
 RSA verification primitive. More...
 
error_t emePkcs1v15Encode (const PrngAlgo *prngAlgo, void *prngContext, const uint8_t *message, size_t messageLen, uint8_t *em, size_t k)
 EME-PKCS1-v1_5 encoding operation. More...
 
uint32_t emePkcs1v15Decode (uint8_t *em, size_t k, size_t *messageLen)
 EME-PKCS1-v1_5 decoding operation. More...
 
error_t emeOaepEncode (const PrngAlgo *prngAlgo, void *prngContext, const HashAlgo *hash, const char_t *label, const uint8_t *message, size_t messageLen, uint8_t *em, size_t k)
 EME-OAEP encoding operation. More...
 
uint32_t emeOaepDecode (const HashAlgo *hash, const char_t *label, uint8_t *em, size_t k, size_t *messageLen)
 EME-OAEP decoding operation. More...
 
error_t emsaPkcs1v15Encode (const HashAlgo *hash, const uint8_t *digest, uint8_t *em, size_t emLen)
 EMSA-PKCS1-v1_5 encoding operation. More...
 
error_t emsaPkcs1v15Verify (const HashAlgo *hash, const uint8_t *digest, const uint8_t *em, size_t emLen)
 EMSA-PKCS1-v1_5 verification operation. More...
 
error_t emsaPssEncode (const PrngAlgo *prngAlgo, void *prngContext, const HashAlgo *hash, size_t saltLen, const uint8_t *digest, uint8_t *em, uint_t emBits)
 EMSA-PSS encoding operation. More...
 
error_t emsaPssVerify (const HashAlgo *hash, size_t saltLen, const uint8_t *digest, uint8_t *em, uint_t emBits)
 EMSA-PSS verification operation. More...
 
void mgf1 (const HashAlgo *hash, HashContext *hashContext, const uint8_t *seed, size_t seedLen, uint8_t *data, size_t dataLen)
 MGF1 mask generation function. More...
 

Detailed Description

Helper routines for RSA.

License

SPDX-License-Identifier: GPL-2.0-or-later

Copyright (C) 2010-2025 Oryx Embedded SARL. All rights reserved.

This file is part of CycloneCRYPTO Open.

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

Description

RSA is an algorithm for public-key cryptography which is suitable for signing as well as encryption. Refer to the following RFCs for complete details:

  • RFC 2313: PKCS #1: RSA Encryption Version 1.5
  • RFC 3447: PKCS #1: RSA Cryptography Specifications Version 2.1
  • RFC 8017: PKCS #1: RSA Cryptography Specifications Version 2.2
Author
Oryx Embedded SARL (www.oryx-embedded.com)
Version
2.5.0

Definition in file rsa_misc.c.

Macro Definition Documentation

◆ TRACE_LEVEL

#define TRACE_LEVEL   CRYPTO_TRACE_LEVEL

Definition at line 40 of file rsa_misc.c.

Function Documentation

◆ emeOaepDecode()

uint32_t emeOaepDecode ( const HashAlgo hash,
const char_t label,
uint8_t *  em,
size_t  k,
size_t *  messageLen 
)

EME-OAEP decoding operation.

Parameters
[in]hashUnderlying hash function
[in]labelOptional label to be associated with the message
[in]emEncoded message
[in]kLength of the encoded message
[out]messageLenLength of the decrypted message
Returns
The function returns 0 on success, 1 on failure

Definition at line 421 of file rsa_misc.c.

◆ emeOaepEncode()

error_t emeOaepEncode ( const PrngAlgo prngAlgo,
void *  prngContext,
const HashAlgo hash,
const char_t label,
const uint8_t *  message,
size_t  messageLen,
uint8_t *  em,
size_t  k 
)

EME-OAEP encoding operation.

Parameters
[in]prngAlgoPRNG algorithm
[in]prngContextPointer to the PRNG context
[in]hashUnderlying hash function
[in]labelOptional label to be associated with the message
[in]messageMessage to be encrypted
[in]messageLenLength of the message to be encrypted
[out]emEncoded message
[in]kLength of the encoded message
Returns
Error code

Definition at line 331 of file rsa_misc.c.

◆ emePkcs1v15Decode()

uint32_t emePkcs1v15Decode ( uint8_t *  em,
size_t  k,
size_t *  messageLen 
)

EME-PKCS1-v1_5 decoding operation.

Parameters
[in]emEncoded message
[in]kLength of the encoded message
[out]messageLenLength of the decrypted message
Returns
The function returns 0 on success, 1 on failure

Definition at line 277 of file rsa_misc.c.

◆ emePkcs1v15Encode()

error_t emePkcs1v15Encode ( const PrngAlgo prngAlgo,
void *  prngContext,
const uint8_t *  message,
size_t  messageLen,
uint8_t *  em,
size_t  k 
)

EME-PKCS1-v1_5 encoding operation.

Parameters
[in]prngAlgoPRNG algorithm
[in]prngContextPointer to the PRNG context
[in]messageMessage to be encrypted
[in]messageLenLength of the message to be encrypted
[out]emEncoded message
[in]kLength of the encoded message
Returns
Error code

Definition at line 209 of file rsa_misc.c.

◆ emsaPkcs1v15Encode()

error_t emsaPkcs1v15Encode ( const HashAlgo hash,
const uint8_t *  digest,
uint8_t *  em,
size_t  emLen 
)

EMSA-PKCS1-v1_5 encoding operation.

Parameters
[in]hashHash function used to digest the message
[in]digestDigest of the message to be signed
[out]emEncoded message
[in]emLenIntended length of the encoded message
Returns
Error code

Definition at line 519 of file rsa_misc.c.

◆ emsaPkcs1v15Verify()

error_t emsaPkcs1v15Verify ( const HashAlgo hash,
const uint8_t *  digest,
const uint8_t *  em,
size_t  emLen 
)

EMSA-PKCS1-v1_5 verification operation.

Parameters
[in]hashHash function
[in]digestDigest value
[in]emEncoded message
[in]emLenLength of the encoded message
Returns
Error code

Definition at line 583 of file rsa_misc.c.

◆ emsaPssEncode()

error_t emsaPssEncode ( const PrngAlgo prngAlgo,
void *  prngContext,
const HashAlgo hash,
size_t  saltLen,
const uint8_t *  digest,
uint8_t *  em,
uint_t  emBits 
)

EMSA-PSS encoding operation.

Parameters
[in]prngAlgoPRNG algorithm
[in]prngContextPointer to the PRNG context
[in]hashUnderlying hash function
[in]saltLenLength of the salt, in bytes
[in]digestDigest of the message to be signed
[out]emEncoded message
[in]emBitsMaximal bit length of the integer OS2IP(EM)
Returns
Error code

Definition at line 659 of file rsa_misc.c.

◆ emsaPssVerify()

error_t emsaPssVerify ( const HashAlgo hash,
size_t  saltLen,
const uint8_t *  digest,
uint8_t *  em,
uint_t  emBits 
)

EMSA-PSS verification operation.

Parameters
[in]hashUnderlying hash function
[in]saltLenLength of the salt, in bytes
[in]digestDigest of the message to be signed
[out]emEncoded message
[in]emBitsMaximal bit length of the integer OS2IP(EM)
Returns
Error code

Definition at line 749 of file rsa_misc.c.

◆ mgf1()

void mgf1 ( const HashAlgo hash,
HashContext hashContext,
const uint8_t *  seed,
size_t  seedLen,
uint8_t *  data,
size_t  dataLen 
)

MGF1 mask generation function.

Parameters
[in]hashHash function
[in]hashContextHash function context
[in]seedSeed from which the mask is generated
[in]seedLenLength of the seed in bytes
[in,out]dataData block to be masked
[in]dataLenLength of the data block in bytes

Definition at line 855 of file rsa_misc.c.

◆ rsadp()

__weak_func error_t rsadp ( const RsaPrivateKey key,
const Mpi c,
Mpi m 
)

RSA decryption primitive.

The RSA decryption primitive recovers the message representative from the ciphertext representative under the control of a private key

Parameters
[in]keyRSA private key
[in]cCiphertext representative
[out]mMessage representative
Returns
Error code

Definition at line 98 of file rsa_misc.c.

◆ rsaep()

__weak_func error_t rsaep ( const RsaPublicKey key,
const Mpi m,
Mpi c 
)

RSA encryption primitive.

The RSA encryption primitive produces a ciphertext representative from a message representative under the control of a public key

Parameters
[in]keyRSA public key
[in]mMessage representative
[out]cCiphertext representative
Returns
Error code

Definition at line 71 of file rsa_misc.c.

◆ rsasp1()

error_t rsasp1 ( const RsaPrivateKey key,
const Mpi m,
Mpi s 
)

RSA signature primitive.

The RSA signature primitive produces a signature representative from a message representative under the control of a private key

Parameters
[in]keyRSA private key
[in]mMessage representative
[out]sSignature representative
Returns
Error code

Definition at line 168 of file rsa_misc.c.

◆ rsavp1()

error_t rsavp1 ( const RsaPublicKey key,
const Mpi s,
Mpi m 
)

RSA verification primitive.

The RSA verification primitive recovers the message representative from the signature representative under the control of a public key

Parameters
[in]keyRSA public key
[in]sSignature representative
[out]mMessage representative
Returns
Error code

Definition at line 189 of file rsa_misc.c.