15
|
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis |
|
2 * |
|
3 * LibTomCrypt is a library that provides various cryptographic |
|
4 * algorithms in a highly modular and flexible manner. |
|
5 * |
|
6 * The library is free for all purposes without any express |
|
7 * guarantee it works. |
|
8 * |
|
9 * Tom St Denis, [email protected], http://libtomcrypt.org |
|
10 */ |
|
11 #include "mycrypt.h" |
|
12 |
|
13 /* v1.5 Encryption Padding for PKCS #1 -- Tom St Denis */ |
|
14 |
|
15 #ifdef PKCS_1 |
|
16 |
|
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) |
|
21 { |
|
22 unsigned long modulus_bytelen, x, y; |
|
23 |
|
24 _ARGCHK(msg != NULL); |
|
25 _ARGCHK(out != NULL); |
|
26 _ARGCHK(outlen != NULL); |
|
27 |
|
28 /* get modulus len */ |
|
29 modulus_bytelen = (modulus_bitlen >> 3) + (modulus_bitlen & 7 ? 1 : 0); |
|
30 if (modulus_bytelen < 12) { |
|
31 return CRYPT_INVALID_ARG; |
|
32 } |
|
33 |
|
34 /* verify length */ |
|
35 if (msglen > (modulus_bytelen - 11) || *outlen < modulus_bytelen) { |
|
36 return CRYPT_PK_INVALID_SIZE; |
|
37 } |
|
38 |
|
39 /* 0x00 0x02 PS 0x00 M */ |
|
40 x = 0; |
|
41 out[x++] = 0x00; |
|
42 out[x++] = 0x02; |
|
43 y = modulus_bytelen - msglen - 3; |
|
44 if (prng_descriptor[prng_idx].read(out+x, y, prng) != y) { |
|
45 return CRYPT_ERROR_READPRNG; |
|
46 } |
|
47 x += y; |
|
48 out[x++] = 0x00; |
|
49 memcpy(out+x, msg, msglen); |
|
50 *outlen = modulus_bytelen; |
|
51 |
|
52 return CRYPT_OK; |
|
53 } |
|
54 |
|
55 #endif /* PKCS_1 */ |