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@gmail.com, http://libtomcrypt.com
14 @file pkcs_1_oaep_encode.c
15 OAEP Padding for PKCS #1, Tom St Denis
21 PKCS #1 v2.00 OAEP encode
22 @param msg The data to encode
23 @param msglen The length of the data to encode (octets)
24 @param lparam A session or system parameter (can be NULL)
25 @param lparamlen The length of the lparam data
26 @param modulus_bitlen The bit length of the RSA modulus
27 @param prng An active PRNG state
28 @param prng_idx The index of the PRNG desired
29 @param hash_idx The index of the hash desired
30 @param out [out] The destination for the encoded data
31 @param outlen [in/out] The max size and resulting size of the encoded data
32 @return CRYPT_OK if successful
34 int pkcs_1_oaep_encode(const unsigned char *msg, unsigned long msglen,
35 const unsigned char *lparam, unsigned long lparamlen,
36 unsigned long modulus_bitlen, prng_state *prng,
37 int prng_idx, int hash_idx,
38 unsigned char *out, unsigned long *outlen)
40 unsigned char *DB, *seed, *mask;
41 unsigned long hLen, x, y, modulus_len;
44 LTC_ARGCHK(msg != NULL);
45 LTC_ARGCHK(out != NULL);
46 LTC_ARGCHK(outlen != NULL);
49 if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) {
54 if ((err = prng_is_valid(prng_idx)) != CRYPT_OK) {
58 hLen = hash_descriptor[hash_idx].hashsize;
59 modulus_len = (modulus_bitlen >> 3) + (modulus_bitlen & 7 ? 1 : 0);
61 /* test message size */
62 if ((2*hLen >= (modulus_len - 2)) || (msglen > (modulus_len - 2*hLen - 2))) {
63 return CRYPT_PK_INVALID_SIZE;
66 /* allocate ram for DB/mask/salt of size modulus_len */
67 DB = XMALLOC(modulus_len);
68 mask = XMALLOC(modulus_len);
70 if (DB == NULL || mask == NULL || seed == NULL) {
84 /* DB == lhash || PS || 0x01 || M, PS == k - mlen - 2hlen - 2 zeroes */
87 if ((err = hash_memory(hash_idx, lparam, lparamlen, DB, &x)) != CRYPT_OK) {
91 /* can't pass hash_memory a NULL so use DB with zero length */
92 if ((err = hash_memory(hash_idx, DB, 0, DB, &x)) != CRYPT_OK) {
97 /* append PS then 0x01 (to lhash) */
99 y = modulus_len - msglen - 2*hLen - 2;
106 /* message (length = msglen) */
107 XMEMCPY(DB+x, msg, msglen);
110 /* now choose a random seed */
111 if (prng_descriptor[prng_idx].read(seed, hLen, prng) != hLen) {
112 err = CRYPT_ERROR_READPRNG;
116 /* compute MGF1 of seed (k - hlen - 1) */
117 if ((err = pkcs_1_mgf1(hash_idx, seed, hLen, mask, modulus_len - hLen - 1)) != CRYPT_OK) {
122 for (y = 0; y < (modulus_len - hLen - 1); y++) {
126 /* compute MGF1 of maskedDB (hLen) */
127 if ((err = pkcs_1_mgf1(hash_idx, DB, modulus_len - hLen - 1, mask, hLen)) != CRYPT_OK) {
131 /* XOR against seed */
132 for (y = 0; y < hLen; y++) {
136 /* create string of length modulus_len */
137 if (*outlen < modulus_len) {
138 *outlen = modulus_len;
139 err = CRYPT_BUFFER_OVERFLOW;
143 /* start output which is 0x00 || maskedSeed || maskedDB */
146 XMEMCPY(out+x, seed, hLen);
148 XMEMCPY(out+x, DB, modulus_len - hLen - 1);
149 x += modulus_len - hLen - 1;
155 #ifdef LTC_CLEAN_STACK
156 zeromem(DB, modulus_len);
158 zeromem(mask, modulus_len);
171 /* $Source: /cvs/libtom/libtomcrypt/src/pk/pkcs1/pkcs_1_oaep_encode.c,v $ */
172 /* $Revision: 1.7 $ */
173 /* $Date: 2006/06/16 21:53:41 $ */