1
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
3
* LibTomCrypt is a library that provides various cryptographic
4
* algorithms in a highly modular and flexible manner.
6
* The library is free for all purposes without any express
9
* Tom St Denis, tomstdenis@iahu.ca, http://libtomcrypt.org
13
/* v1.5 Encryption Padding for PKCS #1 -- Tom St Denis */
17
int pkcs_1_v15_es_encode(const unsigned char *msg, unsigned long msglen,
18
unsigned long modulus_bitlen,
19
prng_state *prng, int prng_idx,
20
unsigned char *out, unsigned long *outlen)
22
unsigned long modulus_bytelen, x, y;
26
_ARGCHK(outlen != NULL);
29
modulus_bytelen = (modulus_bitlen >> 3) + (modulus_bitlen & 7 ? 1 : 0);
30
if (modulus_bytelen < 12) {
31
return CRYPT_INVALID_ARG;
35
if (msglen > (modulus_bytelen - 11) || *outlen < modulus_bytelen) {
36
return CRYPT_PK_INVALID_SIZE;
39
/* 0x00 0x02 PS 0x00 M */
43
y = modulus_bytelen - msglen - 3;
44
if (prng_descriptor[prng_idx].read(out+x, y, prng) != y) {
45
return CRYPT_ERROR_READPRNG;
49
XMEMCPY(out+x, msg, msglen);
50
*outlen = modulus_bytelen;