34 #if defined(POLARSSL_RSA_C)
38 #if defined(POLARSSL_PKCS1_V21)
58 #if defined(POLARSSL_GENPRIME)
64 int (*f_rng)(
void *,
unsigned char *,
size_t),
66 unsigned int nbits,
int exponent )
71 if( f_rng == NULL || nbits < 128 || exponent < 3 )
140 if( !ctx->
N.
p || !ctx->
E.
p )
143 if( ( ctx->
N.
p[0] & 1 ) == 0 ||
144 ( ctx->
E.
p[0] & 1 ) == 0 )
164 mpi PQ, DE, P1, Q1, H, I, G, G2, L1, L2, DP, DQ, QP;
169 if( !ctx->
P.
p || !ctx->
Q.
p || !ctx->
D.
p )
224 const unsigned char *input,
225 unsigned char *output )
259 const unsigned char *input,
260 unsigned char *output )
276 #if defined(POLARSSL_RSA_NO_CRT)
315 #if defined(POLARSSL_PKCS1_V21)
325 static void mgf_mask(
unsigned char *dst,
size_t dlen,
unsigned char *src,
size_t slen,
329 unsigned char counter[4];
335 memset( counter, 0, 4 );
354 for( i = 0; i < use_len; ++i )
364 #if defined(POLARSSL_PKCS1_V21)
369 int (*f_rng)(
void *,
unsigned char *,
size_t),
372 const unsigned char *label,
size_t label_len,
374 const unsigned char *input,
375 unsigned char *output )
379 unsigned char *p = output;
389 if( md_info == NULL )
395 if( olen < ilen + 2 * hlen + 2 || f_rng == NULL )
398 memset( output, 0, olen );
404 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
411 md( md_info, label, label_len, p );
413 p += olen - 2 * hlen - 2 - ilen;
415 memcpy( p, input, ilen );
421 mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
426 mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
441 int (*f_rng)(
void *,
unsigned char *,
size_t),
443 int mode,
size_t ilen,
444 const unsigned char *input,
445 unsigned char *output )
449 unsigned char *p = output;
456 if( olen < ilen + 11 )
459 nb_pad = olen - 3 - ilen;
466 while( nb_pad-- > 0 )
471 ret = f_rng( p_rng, p, 1 );
472 }
while( *p == 0 && --rng_dl && ret == 0 );
476 if( rng_dl == 0 || ret != 0)
486 while( nb_pad-- > 0 )
491 memcpy( p, input, ilen );
502 int (*f_rng)(
void *,
unsigned char *,
size_t),
504 int mode,
size_t ilen,
505 const unsigned char *input,
506 unsigned char *output )
514 #if defined(POLARSSL_PKCS1_V21)
517 ilen, input, output );
525 #if defined(POLARSSL_PKCS1_V21)
531 const unsigned char *label,
size_t label_len,
533 const unsigned char *input,
534 unsigned char *output,
535 size_t output_max_len )
551 if( ilen < 16 || ilen >
sizeof( buf ) )
567 if( md_info == NULL )
576 md( md_info, label, label_len, lhash );
580 mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
585 mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
593 if( memcmp( lhash, p, hlen ) != 0 )
598 while( *p == 0 && p < buf + ilen )
601 if( p == buf + ilen )
607 if (ilen - (p - buf) > output_max_len)
610 *olen = ilen - (p - buf);
611 memcpy( output, p, *olen );
621 int mode,
size_t *olen,
622 const unsigned char *input,
623 unsigned char *output,
624 size_t output_max_len)
626 int ret, correct = 1;
627 size_t ilen, pad_count = 0;
628 unsigned char *p, *q;
637 if( ilen < 16 || ilen >
sizeof( buf ) )
661 while( *p != 0 && p < buf + ilen - 1 )
662 pad_count += ( *p++ != 0 );
664 correct &= ( *p == 0 && p < buf + ilen - 1 );
670 while ( q < buf + ilen - 1 )
671 pad_count += ( *q++ != 0 );
675 correct |= pad_count & 0x100000;
680 while( *p == 0xFF && p < buf + ilen - 1 )
681 pad_count += ( *p++ == 0xFF );
683 correct &= ( *p == 0 && p < buf + ilen - 1 );
689 while ( q < buf + ilen - 1 )
690 pad_count += ( *q++ != 0 );
694 correct |= pad_count & 0x100000;
701 if (ilen - (p - buf) > output_max_len)
704 *olen = ilen - (p - buf);
705 memcpy( output, p, *olen );
714 int mode,
size_t *olen,
715 const unsigned char *input,
716 unsigned char *output,
717 size_t output_max_len)
725 #if defined(POLARSSL_PKCS1_V21)
728 output, output_max_len );
736 #if defined(POLARSSL_PKCS1_V21)
741 int (*f_rng)(
void *,
unsigned char *,
size_t),
745 unsigned int hashlen,
746 const unsigned char *hash,
750 unsigned char *p = sig;
752 unsigned int slen, hlen, offset = 0;
796 if( md_info == NULL )
802 if( olen < hlen + slen + 2 )
805 memset( sig, 0, olen );
811 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
817 p += olen - hlen * 2 - 2;
819 memcpy( p, salt, slen );
839 mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
844 sig[0] &= 0xFF >> ( olen * 8 - msb );
864 unsigned int hashlen,
865 const unsigned char *hash,
869 unsigned char *p = sig;
879 nb_pad = olen - 3 - hashlen;
885 nb_pad = olen - 3 - 34;
889 nb_pad = olen - 3 - 35;
893 nb_pad = olen - 3 - 47;
897 nb_pad = olen - 3 - 51;
901 nb_pad = olen - 3 - 67;
905 nb_pad = olen - 3 - 83;
913 if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
918 memset( p, 0xFF, nb_pad );
925 memcpy( p, hash, hashlen );
930 memcpy( p + 18, hash, 16 );
935 memcpy( p + 18, hash, 16 );
940 memcpy( p + 18, hash, 16 );
945 memcpy( p + 15, hash, 20 );
950 memcpy( p + 19, hash, 28 );
951 p[1] += 28; p[14] = 4; p[18] += 28;
break;
955 memcpy( p + 19, hash, 32 );
956 p[1] += 32; p[14] = 1; p[18] += 32;
break;
960 memcpy( p + 19, hash, 48 );
961 p[1] += 48; p[14] = 2; p[18] += 48;
break;
965 memcpy( p + 19, hash, 64 );
966 p[1] += 64; p[14] = 3; p[18] += 64;
break;
981 int (*f_rng)(
void *,
unsigned char *,
size_t),
985 unsigned int hashlen,
986 const unsigned char *hash,
993 hashlen, hash, sig );
995 #if defined(POLARSSL_PKCS1_V21)
998 hashlen, hash, sig );
1006 #if defined(POLARSSL_PKCS1_V21)
1013 unsigned int hashlen,
1014 const unsigned char *hash,
1015 unsigned char *sig )
1022 unsigned char zeros[8];
1033 if( siglen < 16 || siglen >
sizeof( buf ) )
1045 if( buf[siglen - 1] != 0xBC )
1081 if( md_info == NULL )
1085 slen = siglen - hlen - 1;
1087 memset( zeros, 0, 8 );
1100 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
1105 mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen, &md_ctx );
1107 buf[0] &= 0xFF >> ( siglen * 8 - msb );
1109 while( *p == 0 && p < buf + siglen )
1112 if( p == buf + siglen ||
1131 if( memcmp( p + slen, result, hlen ) == 0 )
1144 unsigned int hashlen,
1145 const unsigned char *hash,
1146 unsigned char *sig )
1150 unsigned char *p, c;
1158 if( siglen < 16 || siglen >
sizeof( buf ) )
1170 if( *p++ != 0 || *p++ !=
RSA_SIGN )
1175 if( p >= buf + siglen - 1 || *p != 0xFF )
1181 len = siglen - ( p - buf );
1186 memcmp( p + 13, hash, 20 ) == 0 )
1203 if( memcmp( p + 18, hash, 16 ) == 0 )
1213 memcmp( p + 15, hash, 20 ) == 0 )
1218 if( ( len == 19 + 28 && p[14] == 4 && hash_id ==
SIG_RSA_SHA224 ) ||
1229 memcmp( p + 19, hash, c ) == 0 )
1237 if( memcmp( p, hash, hashlen ) == 0 )
1252 unsigned int hashlen,
1253 const unsigned char *hash,
1254 unsigned char *sig )
1260 hashlen, hash, sig );
1262 #if defined(POLARSSL_PKCS1_V21)
1265 hashlen, hash, sig );
1284 #if defined(POLARSSL_SELF_TEST)
1293 #define RSA_N "9292758453063D803DD603D5E777D788" \
1294 "8ED1D5BF35786190FA2F23EBC0848AEA" \
1295 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
1296 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
1297 "93A89813FBF3C4F8066D2D800F7C38A8" \
1298 "1AE31942917403FF4946B0A83D3D3E05" \
1299 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
1300 "5E94BB77B07507233A0BC7BAC8F90F79"
1302 #define RSA_E "10001"
1304 #define RSA_D "24BF6185468786FDD303083D25E64EFC" \
1305 "66CA472BC44D253102F8B4A9D3BFA750" \
1306 "91386C0077937FE33FA3252D28855837" \
1307 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
1308 "DF79C5CE07EE72C7F123142198164234" \
1309 "CABB724CF78B8173B9F880FC86322407" \
1310 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
1311 "071513A1E85B5DFA031F21ECAE91A34D"
1313 #define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
1314 "2C01CAD19EA484A87EA4377637E75500" \
1315 "FCB2005C5C7DD6EC4AC023CDA285D796" \
1316 "C3D9E75E1EFC42488BB4F1D13AC30A57"
1318 #define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
1319 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
1320 "910E4168387E3C30AA1E00C339A79508" \
1321 "8452DD96A9A5EA5D9DCA68DA636032AF"
1323 #define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
1324 "3C94D22288ACD763FD8E5600ED4A702D" \
1325 "F84198A5F06C2E72236AE490C93F07F8" \
1326 "3CC559CD27BC2D1CA488811730BB5725"
1328 #define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
1329 "D8AAEA56749EA28623272E4F7D0592AF" \
1330 "7C1F1313CAC9471B5C523BFE592F517B" \
1331 "407A1BD76C164B93DA2D32A383E58357"
1333 #define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
1334 "F38D18D2B2F0E2DD275AA977E2BF4411" \
1335 "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
1336 "A74206CEC169D74BF5A8C50D6F48EA08"
1339 #define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
1340 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
1342 static int myrand(
void *rng_state,
unsigned char *output,
size_t len )
1346 if( rng_state != NULL )
1349 for( i = 0; i < len; ++i )
1362 unsigned char rsa_plaintext[PT_LEN];
1363 unsigned char rsa_decrypted[PT_LEN];
1364 unsigned char rsa_ciphertext[KEY_LEN];
1365 #if defined(POLARSSL_SHA1_C)
1366 unsigned char sha1sum[20];
1382 printf(
" RSA key validation: " );
1388 printf(
"failed\n" );
1394 printf(
"passed\n PKCS#1 encryption : " );
1396 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
1399 rsa_plaintext, rsa_ciphertext ) != 0 )
1402 printf(
"failed\n" );
1408 printf(
"passed\n PKCS#1 decryption : " );
1411 rsa_ciphertext, rsa_decrypted,
1412 sizeof(rsa_decrypted) ) != 0 )
1415 printf(
"failed\n" );
1420 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
1423 printf(
"failed\n" );
1428 #if defined(POLARSSL_SHA1_C)
1430 printf(
"passed\n PKCS#1 data sign : " );
1432 sha1( rsa_plaintext, PT_LEN, sha1sum );
1435 sha1sum, rsa_ciphertext ) != 0 )
1438 printf(
"failed\n" );
1444 printf(
"passed\n PKCS#1 sig. verify: " );
1447 sha1sum, rsa_ciphertext ) != 0 )
1450 printf(
"failed\n" );
1456 printf(
"passed\n\n" );
int md(const md_info_t *md_info, const unsigned char *input, size_t ilen, unsigned char *output)
Output = message_digest( input buffer )
int mpi_cmp_int(const mpi *X, t_sint z)
Compare signed values.
#define POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE
The output buffer for decryption is not large enough.
void mpi_swap(mpi *X, mpi *Y)
Swap the contents of X and Y.
int rsa_rsassa_pkcs1_v15_verify(rsa_context *ctx, int mode, int hash_id, unsigned int hashlen, const unsigned char *hash, unsigned char *sig)
Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY)
int rsa_private(rsa_context *ctx, const unsigned char *input, unsigned char *output)
Do an RSA private key operation.
int rsa_self_test(int verbose)
Checkup routine.
int rsa_rsaes_oaep_encrypt(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, const unsigned char *label, size_t label_len, size_t ilen, const unsigned char *input, unsigned char *output)
Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT)
int rsa_rsassa_pkcs1_v15_sign(rsa_context *ctx, int mode, int hash_id, unsigned int hashlen, const unsigned char *hash, unsigned char *sig)
Perform a PKCS#1 v1.5 signature (RSASSA-PKCS1-v1_5-SIGN)
#define POLARSSL_MPI_MAX_SIZE
Maximum number of bytes for usable MPIs.
int rsa_check_privkey(const rsa_context *ctx)
Check a private RSA key.
int mpi_gcd(mpi *G, const mpi *A, const mpi *B)
Greatest common divisor: G = gcd(A, B)
void sha1(const unsigned char *input, size_t ilen, unsigned char output[20])
Output = SHA-1( input buffer )
int md_starts(md_context_t *ctx)
Set-up the given context for a new message digest.
int rsa_rsaes_pkcs1_v15_encrypt(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, size_t ilen, const unsigned char *input, unsigned char *output)
Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT)
int rsa_pkcs1_sign(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, int hash_id, unsigned int hashlen, const unsigned char *hash, unsigned char *sig)
Generic wrapper to perform a PKCS#1 signature using the mode from the context.
int md_init_ctx(md_context_t *ctx, const md_info_t *md_info)
Initialises and fills the message digest context structure with the appropriate values.
int rsa_rsaes_oaep_decrypt(rsa_context *ctx, int mode, const unsigned char *label, size_t label_len, size_t *olen, const unsigned char *input, unsigned char *output, size_t output_max_len)
Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT)
Configuration options (set of defines)
int rsa_check_pubkey(const rsa_context *ctx)
Check a public RSA key.
int mpi_div_mpi(mpi *Q, mpi *R, const mpi *A, const mpi *B)
Division by mpi: A = Q * B + R.
static unsigned char md_get_size(const md_info_t *md_info)
Returns the size of the message digest output.
int mpi_lset(mpi *X, t_sint z)
Set value from integer.
#define POLARSSL_ERR_RSA_RNG_FAILED
The random generator failed to generate non-zeros.
int rsa_pkcs1_verify(rsa_context *ctx, int mode, int hash_id, unsigned int hashlen, const unsigned char *hash, unsigned char *sig)
Generic wrapper to perform a PKCS#1 verification using the mode from the context. ...
void mpi_init(mpi *X)
Initialize one MPI.
int mpi_cmp_mpi(const mpi *X, const mpi *Y)
Compare signed values.
const md_info_t * md_info
Information about the associated message digest.
int mpi_add_mpi(mpi *X, const mpi *A, const mpi *B)
Signed addition: X = A + B.
const md_info_t * md_info_from_type(md_type_t md_type)
Returns the message digest information associated with the given digest type.
void rsa_free(rsa_context *ctx)
Free the components of an RSA key.
int rsa_pkcs1_encrypt(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, size_t ilen, const unsigned char *input, unsigned char *output)
Generic wrapper to perform a PKCS#1 encryption using the mode from the context.
#define POLARSSL_ERR_RSA_INVALID_PADDING
Input data contains invalid padding and is rejected.
int rsa_pkcs1_decrypt(rsa_context *ctx, int mode, size_t *olen, const unsigned char *input, unsigned char *output, size_t output_max_len)
Generic wrapper to perform a PKCS#1 decryption using the mode from the context.
int mpi_inv_mod(mpi *X, const mpi *A, const mpi *N)
Modular inverse: X = A^-1 mod N.
void mpi_free(mpi *X)
Unallocate one MPI.
int mpi_exp_mod(mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR)
Sliding-window exponentiation: X = A^E mod N.
#define POLARSSL_ERR_RSA_VERIFY_FAILED
The PKCS#1 verification failed.
int rsa_rsassa_pss_verify(rsa_context *ctx, int mode, int hash_id, unsigned int hashlen, const unsigned char *hash, unsigned char *sig)
Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
int mpi_gen_prime(mpi *X, size_t nbits, int dh_flag, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Prime number generation.
#define ASN1_HASH_SHA1_ALT
size_t mpi_msb(const mpi *X)
Return the number of bits up to and including the most significant '1' bit'.
#define POLARSSL_MPI_MAX_BITS
Maximum number of bits for usable MPIs.
int mpi_read_string(mpi *X, int radix, const char *s)
Import from an ASCII string.
Generic message digest wrapper.
int mpi_read_binary(mpi *X, const unsigned char *buf, size_t buflen)
Import X from unsigned binary data, big endian.
The RSA public-key cryptosystem.
#define POLARSSL_ERR_RSA_BAD_INPUT_DATA
Bad input parameters to function.
#define POLARSSL_ERR_RSA_PRIVATE_FAILED
The private key operation failed.
#define POLARSSL_MD_MAX_SIZE
SHA-1 cryptographic hash function.
#define POLARSSL_ERR_RSA_KEY_CHECK_FAILED
Key failed to pass the libraries validity check.
int rsa_gen_key(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, unsigned int nbits, int exponent)
Generate an RSA keypair.
void rsa_init(rsa_context *ctx, int padding, int hash_id)
Initialize an RSA context.
int rsa_rsaes_pkcs1_v15_decrypt(rsa_context *ctx, int mode, size_t *olen, const unsigned char *input, unsigned char *output, size_t output_max_len)
Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT)
int mpi_mod_mpi(mpi *R, const mpi *A, const mpi *B)
Modulo: R = A mod B.
int mpi_write_binary(const mpi *X, unsigned char *buf, size_t buflen)
Export X into unsigned binary data, big endian.
int rsa_rsassa_pss_sign(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, int hash_id, unsigned int hashlen, const unsigned char *hash, unsigned char *sig)
Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN)
int size
Output length of the digest function.
#define POLARSSL_ERR_RSA_KEY_GEN_FAILED
Something failed during generation of a key.
int md_finish(md_context_t *ctx, unsigned char *output)
Generic message digest final digest.
int mpi_mul_mpi(mpi *X, const mpi *A, const mpi *B)
Baseline multiplication: X = A * B.
#define POLARSSL_ERR_RSA_PUBLIC_FAILED
The public key operation failed.
int mpi_sub_mpi(mpi *X, const mpi *A, const mpi *B)
Signed substraction: X = A - B.
int md_free_ctx(md_context_t *ctx)
Free the message-specific context of ctx.
int mpi_sub_int(mpi *X, const mpi *A, t_sint b)
Signed substraction: X = A - b.
Message digest information.
int md_update(md_context_t *ctx, const unsigned char *input, size_t ilen)
Generic message digest process buffer.
Generic message digest context.
int rsa_public(rsa_context *ctx, const unsigned char *input, unsigned char *output)
Do an RSA public key operation.