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 /* PKCS #1 v1.5 Encryption Padding -- Tom St Denis */ |
|
14 |
|
15 #ifdef PKCS_1 |
|
16 |
|
17 int pkcs_1_v15_es_decode(const unsigned char *msg, unsigned long msglen, |
|
18 unsigned long modulus_bitlen, |
|
19 unsigned char *out, unsigned long outlen, |
|
20 int *res) |
|
21 { |
|
22 unsigned long x, modulus_bytelen; |
|
23 |
|
24 _ARGCHK(msg != NULL); |
|
25 _ARGCHK(out != NULL); |
|
26 _ARGCHK(res != NULL); |
|
27 |
|
28 /* default to failed */ |
|
29 *res = 0; |
|
30 |
|
31 /* must be at least 12 bytes long */ |
|
32 if (msglen < 12) { |
|
33 return CRYPT_INVALID_ARG; |
|
34 } |
|
35 |
|
36 modulus_bytelen = (modulus_bitlen>>3) + (modulus_bitlen & 7 ? 1 : 0); |
|
37 |
|
38 /* should start with 0x00 0x02 */ |
|
39 if (msg[0] != 0x00 || msg[1] != 0x02) { |
|
40 return CRYPT_OK; |
|
41 } |
|
42 |
|
43 /* skip over PS */ |
|
44 x = 2 + (modulus_bytelen - outlen - 3); |
|
45 |
|
46 /* should be 0x00 */ |
|
47 if (msg[x++] != 0x00) { |
|
48 return CRYPT_OK; |
|
49 } |
|
50 |
|
51 /* the message is left */ |
|
52 if (x + outlen > modulus_bytelen) { |
|
53 return CRYPT_PK_INVALID_SIZE; |
|
54 } |
|
55 memcpy(out, msg + x, outlen); |
|
56 *res = 1; |
|
57 return CRYPT_OK; |
|
58 } |
|
59 |
|
60 #endif |
|
61 |