comparison src/pk/pkcs1/pkcs_1_v15_es_decode.c @ 192:9cc34777b479 libtomcrypt

propagate from branch 'au.asn.ucc.matt.ltc-orig' (head 9ba8f01f44320e9cb9f19881105ae84f84a43ea9) to branch 'au.asn.ucc.matt.dropbear.ltc' (head dbf51c569bc34956ad948e4cc87a0eeb2170b768)
author Matt Johnston <matt@ucc.asn.au>
date Sun, 08 May 2005 06:36:47 +0000
parents 1c15b283127b
children
comparison
equal deleted inserted replaced
164:cd1143579f00 192:9cc34777b479
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 "tomcrypt.h"
12
13 /**
14 @file pkcs_1_v15_es_decode.c
15 PKCS #1 v1.5 Encryption Padding, Tom St Denis
16 */
17
18 #ifdef PKCS_1
19
20 /**
21 PKCS #1 v1.5 Encryption Decoding
22 @param msg The padded data
23 @param msglen The length of the padded data (octets)
24 @param modulus_bitlen The bit length of the RSA modulus
25 @param out [out] Where to store the decoded data
26 @param outlen The length of the decoded data
27 @param res [out] Result of the decoding, 1==valid, 0==invalid
28 @return CRYPT_OK if successful
29 */
30 int pkcs_1_v15_es_decode(const unsigned char *msg, unsigned long msglen,
31 unsigned long modulus_bitlen,
32 unsigned char *out, unsigned long outlen,
33 int *res)
34 {
35 unsigned long x, modulus_bytelen;
36
37 LTC_ARGCHK(msg != NULL);
38 LTC_ARGCHK(out != NULL);
39 LTC_ARGCHK(res != NULL);
40
41 /* default to failed */
42 *res = 0;
43
44 modulus_bytelen = (modulus_bitlen>>3) + (modulus_bitlen & 7 ? 1 : 0);
45
46 /* must be at least modulus_bytelen bytes long */
47 if (msglen != modulus_bytelen) {
48 return CRYPT_INVALID_ARG;
49 }
50
51 /* should start with 0x00 0x02 */
52 if (msg[0] != 0x00 || msg[1] != 0x02) {
53 return CRYPT_OK;
54 }
55
56 /* skip over PS */
57 x = 2 + (modulus_bytelen - outlen - 3);
58
59 /* should be 0x00 */
60 if (msg[x++] != 0x00) {
61 return CRYPT_OK;
62 }
63
64 /* the message is left */
65 if (x + outlen > modulus_bytelen) {
66 return CRYPT_PK_INVALID_SIZE;
67 }
68 XMEMCPY(out, msg + x, outlen);
69 *res = 1;
70 return CRYPT_OK;
71 }
72
73 #endif
74