Mercurial > dropbear
comparison src/pk/rsa/rsa_v15_encrypt_key.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 rsa_v15_encrypt_key.c | |
15 RSA PKCS v1.5 Encryption, Tom St Denis | |
16 */ | |
17 | |
18 #ifdef MRSA | |
19 | |
20 /** | |
21 PKCS #1 v1.5 pad then encrypt | |
22 @param in The plaintext | |
23 @param inlen The length of the plaintext (octets) | |
24 @param out [out] The ciphertext | |
25 @param outlen [in/out] The max size and resulting size of the ciphertext | |
26 @param prng An active PRNG | |
27 @param prng_idx The index of the desired PRNG | |
28 @param key The public RSA key | |
29 @return CRYPT_OK if successful | |
30 */ | |
31 int rsa_v15_encrypt_key(const unsigned char *in, unsigned long inlen, | |
32 unsigned char *out, unsigned long *outlen, | |
33 prng_state *prng, int prng_idx, | |
34 rsa_key *key) | |
35 { | |
36 unsigned long modulus_bitlen, modulus_bytelen, x; | |
37 int err; | |
38 | |
39 LTC_ARGCHK(in != NULL); | |
40 LTC_ARGCHK(out != NULL); | |
41 LTC_ARGCHK(outlen != NULL); | |
42 LTC_ARGCHK(key != NULL); | |
43 | |
44 /* valid prng? */ | |
45 if ((err = prng_is_valid(prng_idx)) != CRYPT_OK) { | |
46 return err; | |
47 } | |
48 | |
49 /* get modulus len in bits */ | |
50 modulus_bitlen = mp_count_bits(&(key->N)); | |
51 | |
52 /* outlen must be at least the size of the modulus */ | |
53 modulus_bytelen = mp_unsigned_bin_size(&(key->N)); | |
54 if (modulus_bytelen > *outlen) { | |
55 return CRYPT_BUFFER_OVERFLOW; | |
56 } | |
57 | |
58 /* pad it */ | |
59 x = *outlen; | |
60 if ((err = pkcs_1_v15_es_encode(in, inlen, modulus_bitlen, prng, prng_idx, out, &x)) != CRYPT_OK) { | |
61 return err; | |
62 } | |
63 | |
64 /* encrypt it */ | |
65 return rsa_exptmod(out, x, out, outlen, PK_PUBLIC, key); | |
66 } | |
67 | |
68 #endif |