Go to the documentation of this file.
40 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
50 #if (RSA_SUPPORT == ENABLED)
53 static const uint8_t padding[] =
55 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
210 const uint8_t *
message,
size_t messageLen, uint8_t *em,
size_t k)
219 if((messageLen + 11) > k)
231 n = k - messageLen - 3;
238 error = prngAlgo->read(prngContext,
p,
n);
244 for(i = 0, j = 0; j <
n; j++)
286 for(
m = 0, i = 2; i < k; i++)
333 size_t messageLen, uint8_t *em,
size_t k)
339 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
346 if(messageLen > (k - 2 * hash->
digestSize - 2))
355 error = prngAlgo->read(prngContext, seed, hash->
digestSize);
360 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
364 if(hashContext == NULL)
375 hash->
init(hashContext);
377 hash->
final(hashContext, db);
401 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
422 size_t k,
size_t *messageLen)
432 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
438 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
442 if(hashContext == NULL)
453 hash->
init(hashContext);
455 hash->
final(hashContext, lHash);
470 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
520 const uint8_t *digest, uint8_t *em,
size_t emLen)
552 em[i++] = (uint8_t) (hash->
oidSize + 4);
554 em[i++] = (uint8_t) hash->
oidSize;
584 const uint8_t *em,
size_t emLen)
601 bad |= em[i++] ^ 0x01;
607 for(j = 0; j <
n; j++)
609 bad |= em[i++] ^ 0xFF;
619 bad |= em[i++] ^ (uint8_t) (hash->
oidSize + 4);
621 bad |= em[i++] ^ (uint8_t) hash->
oidSize;
624 for(j = 0; j < hash->oidSize; j++)
626 bad |= em[i++] ^ hash->
oid[j];
637 for(j = 0; j < hash->digestSize; j++)
639 bad |= em[i++] ^ digest[j];
660 const HashAlgo *hash,
size_t saltLen,
const uint8_t *digest,
661 uint8_t *em,
uint_t emBits)
669 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
676 emLen = (emBits + 7) / 8;
691 error = prngAlgo->read(prngContext, salt, saltLen);
696 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
700 if(hashContext == NULL)
705 hash->
init(hashContext);
706 hash->
update(hashContext, padding,
sizeof(padding));
708 hash->
update(hashContext, salt, saltLen);
709 hash->
final(hashContext,
h);
723 db[0] &= 0xFF >> (8 * emLen - emBits);
729 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
750 const uint8_t *digest, uint8_t *em,
uint_t emBits)
761 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
768 emLen = (emBits + 7) / 8;
774 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
778 if(hashContext == NULL)
784 bad = em[emLen - 1] ^ 0xBC;
793 mask = 0xFF >> (8 * emLen - emBits);
797 bad |= db[0] & ~
mask;
810 for(i = 0; i <
n; i++)
823 hash->
init(hashContext);
824 hash->
update(hashContext, padding,
sizeof(padding));
826 hash->
update(hashContext, salt, saltLen);
827 hash->
final(hashContext, h2);
835 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
865 for(counter = 0;
dataLen > 0; counter++)
874 hash->
init(hashContext);
875 hash->
update(hashContext, seed, seedLen);
876 hash->
update(hashContext,
c,
sizeof(
c));
877 hash->
final(hashContext, digest);
880 for(i = 0; i <
n; i++)
882 data[i] ^= digest[i];
Generic hash algorithm context.
#define CRYPTO_TEST_Z_32(a)
Arbitrary precision integer.
#define CRYPTO_SELECT_32(a, b, c)
uint32_t emePkcs1v15Decode(uint8_t *em, size_t k, size_t *messageLen)
EME-PKCS1-v1_5 decoding operation.
void mpiInit(Mpi *r)
Initialize a multiple precision integer.
#define CRYPTO_TEST_Z_8(a)
#define ASN1_ENCODING_CONSTRUCTED
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.
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.
error_t mpiMod(Mpi *r, const Mpi *a, const Mpi *p)
Modulo operation.
#define MAX_HASH_DIGEST_SIZE
error_t mpiMul(Mpi *r, const Mpi *a, const Mpi *b)
Multiple precision multiplication.
error_t emsaPkcs1v15Verify(const HashAlgo *hash, const uint8_t *digest, const uint8_t *em, size_t emLen)
EMSA-PKCS1-v1_5 verification operation.
@ ERROR_INVALID_PARAMETER
Invalid parameter.
error_t emsaPkcs1v15Encode(const HashAlgo *hash, const uint8_t *digest, uint8_t *em, size_t emLen)
EMSA-PKCS1-v1_5 encoding operation.
#define osMemcpy(dest, src, length)
#define CRYPTO_TEST_NZ_8(a)
error_t mpiSub(Mpi *r, const Mpi *a, const Mpi *b)
Multiple precision subtraction.
void mgf1(const HashAlgo *hash, HashContext *hashContext, const uint8_t *seed, size_t seedLen, uint8_t *data, size_t dataLen)
MGF1 mask generation function.
error_t mpiAdd(Mpi *r, const Mpi *a, const Mpi *b)
Multiple precision addition.
#define CRYPTO_TEST_LT_32(a, b)
General definitions for cryptographic algorithms.
RSA public-key cryptography standard.
error_t rsavp1(const RsaPublicKey *key, const Mpi *s, Mpi *m)
RSA verification primitive.
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.
Mpi dq
Second factor's CRT exponent.
uint_t mpiGetLength(const Mpi *a)
Get the actual length in words.
__weak_func error_t rsadp(const RsaPrivateKey *key, const Mpi *c, Mpi *m)
RSA decryption primitive.
error_t emsaPssVerify(const HashAlgo *hash, size_t saltLen, const uint8_t *digest, uint8_t *em, uint_t emBits)
EMSA-PSS verification operation.
error_t mpiExpModFast(Mpi *r, const Mpi *a, const Mpi *e, const Mpi *p)
Modular exponentiation (fast calculation)
__weak_func error_t rsaep(const RsaPublicKey *key, const Mpi *m, Mpi *c)
RSA encryption primitive.
#define cryptoAllocMem(size)
@ ASN1_TYPE_OBJECT_IDENTIFIER
error_t rsasp1(const RsaPrivateKey *key, const Mpi *m, Mpi *s)
RSA signature primitive.
Common interface for hash algorithms.
int_t mpiComp(const Mpi *a, const Mpi *b)
Compare two multiple precision integers.
Mpi dp
First factor's CRT exponent.
int_t mpiCompInt(const Mpi *a, mpi_sword_t b)
Compare a multiple precision integer with an integer.
uint32_t emeOaepDecode(const HashAlgo *hash, const char_t *label, uint8_t *em, size_t k, size_t *messageLen)
EME-OAEP decoding operation.
#define osMemset(p, value, length)
error_t mpiMulMod(Mpi *r, const Mpi *a, const Mpi *b, const Mpi *p)
Modular multiplication.
#define CRYPTO_TEST_NEQ_8(a, b)
@ ERROR_INCONSISTENT_VALUE
error_t mpiExpModRegular(Mpi *r, const Mpi *a, const Mpi *e, const Mpi *p)
Modular exponentiation (regular calculation)
ASN.1 (Abstract Syntax Notation One)
void mpiFree(Mpi *r)
Release a multiple precision integer.