Mercurial > dropbear
comparison src/pk/pkcs1/pkcs_1_v15_es_encode.c @ 191:1c15b283127b libtomcrypt-orig
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Fri, 06 May 2005 13:23:02 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
143:5d99163f7e32 | 191:1c15b283127b |
---|---|
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_encode.c | |
15 v1.5 Encryption Padding for PKCS #1, Tom St Denis | |
16 */ | |
17 | |
18 #ifdef PKCS_1 | |
19 | |
20 /** | |
21 PKCS #1 v1.5 Encryption Padding | |
22 @param msg The data to encode | |
23 @param msglen The length of the data (octets) | |
24 @param modulus_bitlen The bit length of the RSA modulus | |
25 @param prng An active PRNG | |
26 @param prng_idx The index of the PRNG desired | |
27 @param out [out] The destination of the padding | |
28 @param outlen [in/out] The max size and resulting size of the padding | |
29 @return CRYPT_OK if successful | |
30 */ | |
31 int pkcs_1_v15_es_encode(const unsigned char *msg, unsigned long msglen, | |
32 unsigned long modulus_bitlen, | |
33 prng_state *prng, int prng_idx, | |
34 unsigned char *out, unsigned long *outlen) | |
35 { | |
36 unsigned long modulus_bytelen, x, y; | |
37 | |
38 LTC_ARGCHK(msg != NULL); | |
39 LTC_ARGCHK(out != NULL); | |
40 LTC_ARGCHK(outlen != NULL); | |
41 | |
42 /* get modulus len */ | |
43 modulus_bytelen = (modulus_bitlen >> 3) + (modulus_bitlen & 7 ? 1 : 0); | |
44 if (modulus_bytelen < 12) { | |
45 return CRYPT_INVALID_ARG; | |
46 } | |
47 | |
48 /* verify length */ | |
49 if (msglen > (modulus_bytelen - 11) || *outlen < modulus_bytelen) { | |
50 return CRYPT_PK_INVALID_SIZE; | |
51 } | |
52 | |
53 /* 0x00 0x02 PS 0x00 M */ | |
54 x = 0; | |
55 out[x++] = 0x00; | |
56 out[x++] = 0x02; | |
57 y = modulus_bytelen - msglen - 3; | |
58 if (prng_descriptor[prng_idx].read(out+x, y, prng) != y) { | |
59 return CRYPT_ERROR_READPRNG; | |
60 } | |
61 x += y; | |
62 out[x++] = 0x00; | |
63 XMEMCPY(out+x, msg, msglen); | |
64 *outlen = modulus_bytelen; | |
65 | |
66 return CRYPT_OK; | |
67 } | |
68 | |
69 #endif /* PKCS_1 */ |