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
12 @file pkcs_1_oaep_encode.c
13 OAEP Padding for PKCS #1, Tom St Denis
19 PKCS #1 v2.00 OAEP encode
20 @param msg The data to encode
21 @param msglen The length of the data to encode (octets)
22 @param lparam A session or system parameter (can be NULL)
23 @param lparamlen The length of the lparam data
24 @param modulus_bitlen The bit length of the RSA modulus
25 @param prng An active PRNG state
26 @param prng_idx The index of the PRNG desired
27 @param hash_idx The index of the hash desired
28 @param out [out] The destination for the encoded data
29 @param outlen [in/out] The max size and resulting size of the encoded data
30 @return CRYPT_OK if successful
32 int pkcs_1_oaep_encode(const unsigned char *msg, unsigned long msglen,
33 const unsigned char *lparam, unsigned long lparamlen,
34 unsigned long modulus_bitlen, prng_state *prng,
35 int prng_idx, int hash_idx,
36 unsigned char *out, unsigned long *outlen)
38 unsigned char *DB, *seed, *mask;
39 unsigned long hLen, x, y, modulus_len;
42 LTC_ARGCHK(msg != NULL);
43 LTC_ARGCHK(out != NULL);
44 LTC_ARGCHK(outlen != NULL);
47 if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) {
52 if ((err = prng_is_valid(prng_idx)) != CRYPT_OK) {
56 hLen = hash_descriptor[hash_idx].hashsize;
57 modulus_len = (modulus_bitlen >> 3) + (modulus_bitlen & 7 ? 1 : 0);
59 /* test message size */
60 if ((2*hLen >= (modulus_len - 2)) || (msglen > (modulus_len - 2*hLen - 2))) {
61 return CRYPT_PK_INVALID_SIZE;
64 /* allocate ram for DB/mask/salt of size modulus_len */
65 DB = XMALLOC(modulus_len);
66 mask = XMALLOC(modulus_len);
68 if (DB == NULL || mask == NULL || seed == NULL) {
82 /* DB == lhash || PS || 0x01 || M, PS == k - mlen - 2hlen - 2 zeroes */
85 if ((err = hash_memory(hash_idx, lparam, lparamlen, DB, &x)) != CRYPT_OK) {
89 /* can't pass hash_memory a NULL so use DB with zero length */
90 if ((err = hash_memory(hash_idx, DB, 0, DB, &x)) != CRYPT_OK) {
95 /* append PS then 0x01 (to lhash) */
97 y = modulus_len - msglen - 2*hLen - 2;
104 /* message (length = msglen) */
105 XMEMCPY(DB+x, msg, msglen);
108 /* now choose a random seed */
109 if (prng_descriptor[prng_idx].read(seed, hLen, prng) != hLen) {
110 err = CRYPT_ERROR_READPRNG;
114 /* compute MGF1 of seed (k - hlen - 1) */
115 if ((err = pkcs_1_mgf1(hash_idx, seed, hLen, mask, modulus_len - hLen - 1)) != CRYPT_OK) {
120 for (y = 0; y < (modulus_len - hLen - 1); y++) {
124 /* compute MGF1 of maskedDB (hLen) */
125 if ((err = pkcs_1_mgf1(hash_idx, DB, modulus_len - hLen - 1, mask, hLen)) != CRYPT_OK) {
129 /* XOR against seed */
130 for (y = 0; y < hLen; y++) {
134 /* create string of length modulus_len */
135 if (*outlen < modulus_len) {
136 *outlen = modulus_len;
137 err = CRYPT_BUFFER_OVERFLOW;
141 /* start output which is 0x00 || maskedSeed || maskedDB */
144 XMEMCPY(out+x, seed, hLen);
146 XMEMCPY(out+x, DB, modulus_len - hLen - 1);
147 x += modulus_len - hLen - 1;
153 #ifdef LTC_CLEAN_STACK
154 zeromem(DB, modulus_len);
156 zeromem(mask, modulus_len);
166 #endif /* LTC_PKCS_1 */
169 /* ref: $Format:%D$ */
170 /* git commit: $Format:%H$ */
171 /* commit time: $Format:%ai$ */