Skip to content

Commit f854795

Browse files
authored
Merge pull request #9263 from holtrop/rsa-const-pointers
RSA API: use const pointers and clean up some comments
2 parents dcafe9a + c36c39a commit f854795

8 files changed

Lines changed: 40 additions & 40 deletions

File tree

doc/dox_comments/header_files/rsa.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ int wc_InitRsaKey(RsaKey* key, void* heap);
6161
\code
6262
RsaKey enc;
6363
unsigned char* id = (unsigned char*)"RSA2048";
64-
int len = 6;
64+
int len = 7;
6565
int devId = 1;
6666
int ret;
6767
ret = wc_CryptoDev_RegisterDevice(devId, wc_Pkcs11_CryptoDevCb,
@@ -173,7 +173,7 @@ int wc_FreeRsaKey(RsaKey* key);
173173
\sa wc_RsaPublicEncrypt
174174
\sa wc_RsaPrivateDecrypt
175175
*/
176-
int wc_RsaDirect(byte* in, word32 inLen, byte* out, word32* outSz,
176+
int wc_RsaDirect(const byte* in, word32 inLen, byte* out, word32* outSz,
177177
RsaKey* key, int type, WC_RNG* rng);
178178

179179
/*!
@@ -1471,10 +1471,10 @@ int wc_RsaKeyToPublicDer_ex(RsaKey* key, byte* output, word32 inLen,
14711471
\brief This function generates a RSA private key of length size (in bits)
14721472
and given exponent (e). It then stores this key in the provided RsaKey
14731473
structure, so that it may be used for encryption/decryption. A secure
1474-
number to use for e is 65537. size is required to be greater than
1475-
RSA_MIN_SIZE and less than RSA_MAX_SIZE. For this function to be
1476-
available, the option WOLFSSL_KEY_GEN must be enabled at compile time.
1477-
This can be accomplished with --enable-keygen if using ./configure.
1474+
number to use for e is 65537. size is required to be greater than or equal
1475+
to RSA_MIN_SIZE and less than or equal to RSA_MAX_SIZE. For this function
1476+
to be available, the option WOLFSSL_KEY_GEN must be enabled at compile
1477+
time. This can be accomplished with --enable-keygen if using ./configure.
14781478
14791479
\return 0 Returned upon successfully generating a RSA private key
14801480
\return BAD_FUNC_ARG Returned if any of the input arguments are NULL,

wolfcrypt/src/integer.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ int mp_to_unsigned_bin_at_pos(int x, mp_int *t, unsigned char *b)
297297
}
298298

299299
/* store in unsigned [big endian] format */
300-
int mp_to_unsigned_bin (mp_int * a, unsigned char *b)
300+
int mp_to_unsigned_bin (const mp_int * a, unsigned char *b)
301301
{
302302
int x, res;
303303
mp_int t;
@@ -335,7 +335,7 @@ int mp_to_unsigned_bin_len(mp_int * a, unsigned char *b, int c)
335335
}
336336

337337
/* creates "a" then copies b into it */
338-
int mp_init_copy (mp_int * a, mp_int * b)
338+
int mp_init_copy (mp_int * a, const mp_int * b)
339339
{
340340
int res;
341341

wolfcrypt/src/port/st/stm32.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,7 @@ static int stm32_get_from_mp_int(uint8_t *dst, const mp_int *a, int sz)
724724
XMEMSET(dst, 0, offset);
725725

726726
/* convert mp_int to array of bytes */
727-
res = mp_to_unsigned_bin((mp_int*)a, dst + offset);
727+
res = mp_to_unsigned_bin(a, dst + offset);
728728
return res;
729729
}
730730

wolfcrypt/src/rsa.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2934,7 +2934,7 @@ static int wc_RsaFunctionAsync(const byte* in, word32 inLen, byte* out,
29342934
/* Performs direct RSA computation without padding. The input and output must
29352935
* match the key size (ex: 2048-bits = 256 bytes). Returns the size of the
29362936
* output on success or negative value on failure. */
2937-
int wc_RsaDirect(byte* in, word32 inLen, byte* out, word32* outSz,
2937+
int wc_RsaDirect(const byte* in, word32 inLen, byte* out, word32* outSz,
29382938
RsaKey* key, int type, WC_RNG* rng)
29392939
{
29402940
int ret;
@@ -4002,7 +4002,7 @@ int wc_RsaPSS_VerifyInline_ex(byte* in, word32 inLen, byte** out,
40024002
* key Public RSA key.
40034003
* returns the length of the PSS data on success and negative indicates failure.
40044004
*/
4005-
int wc_RsaPSS_Verify(byte* in, word32 inLen, byte* out, word32 outLen,
4005+
int wc_RsaPSS_Verify(const byte* in, word32 inLen, byte* out, word32 outLen,
40064006
enum wc_HashType hash, int mgf, RsaKey* key)
40074007
{
40084008
#ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
@@ -4027,7 +4027,7 @@ int wc_RsaPSS_Verify(byte* in, word32 inLen, byte* out, word32 outLen,
40274027
* indicates salt length is determined from the data.
40284028
* returns the length of the PSS data on success and negative indicates failure.
40294029
*/
4030-
int wc_RsaPSS_Verify_ex(byte* in, word32 inLen, byte* out, word32 outLen,
4030+
int wc_RsaPSS_Verify_ex(const byte* in, word32 inLen, byte* out, word32 outLen,
40314031
enum wc_HashType hash, int mgf, int saltLen,
40324032
RsaKey* key)
40334033
{
@@ -4062,7 +4062,7 @@ int wc_RsaPSS_Verify_ex(byte* in, word32 inLen, byte* out, word32 outLen,
40624062
* NULL is passed in to in or sig or inSz is not the same as the hash
40634063
* algorithm length and 0 on success.
40644064
*/
4065-
int wc_RsaPSS_CheckPadding(const byte* in, word32 inSz, byte* sig,
4065+
int wc_RsaPSS_CheckPadding(const byte* in, word32 inSz, const byte* sig,
40664066
word32 sigSz, enum wc_HashType hashType)
40674067
{
40684068
#ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
@@ -4087,7 +4087,7 @@ int wc_RsaPSS_CheckPadding(const byte* in, word32 inSz, byte* sig,
40874087
* NULL is passed in to in or sig or inSz is not the same as the hash
40884088
* algorithm length and 0 on success.
40894089
*/
4090-
int wc_RsaPSS_CheckPadding_ex2(const byte* in, word32 inSz, byte* sig,
4090+
int wc_RsaPSS_CheckPadding_ex2(const byte* in, word32 inSz, const byte* sig,
40914091
word32 sigSz, enum wc_HashType hashType,
40924092
int saltLen, int bits, void* heap)
40934093
{
@@ -4186,7 +4186,7 @@ int wc_RsaPSS_CheckPadding_ex2(const byte* in, word32 inSz, byte* sig,
41864186
(void)heap; /* unused if memory is disabled */
41874187
return ret;
41884188
}
4189-
int wc_RsaPSS_CheckPadding_ex(const byte* in, word32 inSz, byte* sig,
4189+
int wc_RsaPSS_CheckPadding_ex(const byte* in, word32 inSz, const byte* sig,
41904190
word32 sigSz, enum wc_HashType hashType,
41914191
int saltLen, int bits)
41924192
{
@@ -4257,7 +4257,7 @@ int wc_RsaPSS_VerifyCheckInline(byte* in, word32 inLen, byte** out,
42574257
* key Public RSA key.
42584258
* returns the length of the PSS data on success and negative indicates failure.
42594259
*/
4260-
int wc_RsaPSS_VerifyCheck(byte* in, word32 inLen, byte* out, word32 outLen,
4260+
int wc_RsaPSS_VerifyCheck(const byte* in, word32 inLen, byte* out, word32 outLen,
42614261
const byte* digest, word32 digestLen,
42624262
enum wc_HashType hash, int mgf,
42634263
RsaKey* key)
@@ -4383,7 +4383,7 @@ int wc_RsaEncryptSize(const RsaKey* key)
43834383

43844384
#ifndef WOLFSSL_RSA_VERIFY_ONLY
43854385
/* flatten RsaKey structure into individual elements (e, n) */
4386-
int wc_RsaFlattenPublicKey(RsaKey* key, byte* e, word32* eSz, byte* n,
4386+
int wc_RsaFlattenPublicKey(const RsaKey* key, byte* e, word32* eSz, byte* n,
43874387
word32* nSz)
43884388
{
43894389
int sz, ret;
@@ -4413,7 +4413,7 @@ int wc_RsaFlattenPublicKey(RsaKey* key, byte* e, word32* eSz, byte* n,
44134413
#endif
44144414

44154415
#ifndef WOLFSSL_RSA_VERIFY_ONLY
4416-
static int RsaGetValue(mp_int* in, byte* out, word32* outSz)
4416+
static int RsaGetValue(const mp_int* in, byte* out, word32* outSz)
44174417
{
44184418
word32 sz;
44194419
int ret = 0;
@@ -4434,7 +4434,7 @@ static int RsaGetValue(mp_int* in, byte* out, word32* outSz)
44344434
}
44354435

44364436

4437-
int wc_RsaExportKey(RsaKey* key,
4437+
int wc_RsaExportKey(const RsaKey* key,
44384438
byte* e, word32* eSz, byte* n, word32* nSz,
44394439
byte* d, word32* dSz, byte* p, word32* pSz,
44404440
byte* q, word32* qSz)

wolfcrypt/src/tfm.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4184,7 +4184,7 @@ int fp_to_unsigned_bin_at_pos(int x, fp_int *t, unsigned char *b)
41844184
#endif
41854185
}
41864186

4187-
int fp_to_unsigned_bin(fp_int *a, unsigned char *b)
4187+
int fp_to_unsigned_bin(const fp_int *a, unsigned char *b)
41884188
{
41894189
int x;
41904190
#ifndef WOLFSSL_SMALL_STACK
@@ -4882,7 +4882,7 @@ int mp_to_unsigned_bin_at_pos(int x, fp_int *t, unsigned char *b)
48824882
}
48834883

48844884
/* store in unsigned [big endian] format */
4885-
int mp_to_unsigned_bin (mp_int * a, unsigned char *b)
4885+
int mp_to_unsigned_bin(const mp_int * a, unsigned char *b)
48864886
{
48874887
return fp_to_unsigned_bin(a,b);
48884888
}
@@ -4968,14 +4968,14 @@ void fp_copy(const fp_int *a, fp_int *b)
49684968
}
49694969
}
49704970

4971-
int mp_init_copy(fp_int * a, fp_int * b)
4971+
int mp_init_copy(fp_int * a, const fp_int * b)
49724972
{
49734973
fp_init_copy(a, b);
49744974
return MP_OKAY;
49754975
}
49764976

49774977
/* Copy (dst = a) from (src = b) */
4978-
void fp_init_copy(fp_int *a, fp_int* b)
4978+
void fp_init_copy(fp_int *a, const fp_int* b)
49794979
{
49804980
if (a != b) {
49814981
fp_init(a);

wolfssl/wolfcrypt/integer.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,15 +307,15 @@ typedef int ltm_prime_callback(unsigned char *dst, int len, void *dat);
307307
extern const char *mp_s_rmap;
308308
#endif
309309

310-
/* 6 functions needed by Rsa */
310+
/* functions needed by Rsa */
311311
MP_API int mp_init (mp_int * a);
312312
MP_API void mp_clear (mp_int * a);
313313
MP_API void mp_free (mp_int * a);
314314
MP_API void mp_forcezero(mp_int * a);
315315
MP_API int mp_unsigned_bin_size(const mp_int * a);
316316
MP_API int mp_read_unsigned_bin (mp_int * a, const unsigned char *b, int c);
317317
MP_API int mp_to_unsigned_bin_at_pos(int x, mp_int *t, unsigned char *b);
318-
MP_API int mp_to_unsigned_bin (mp_int * a, unsigned char *b);
318+
MP_API int mp_to_unsigned_bin(const mp_int * a, unsigned char *b);
319319
#define mp_to_unsigned_bin_len_ct mp_to_unsigned_bin_len
320320
MP_API int mp_to_unsigned_bin_len(mp_int * a, unsigned char *b, int c);
321321
MP_API int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y);
@@ -326,7 +326,7 @@ MP_API int mp_exptmod_ex (mp_int * G, mp_int * X, int digits, mp_int * P,
326326
/* functions added to support above needed, removed TOOM and KARATSUBA */
327327
MP_API int mp_count_bits (const mp_int * a);
328328
MP_API int mp_leading_bit (mp_int * a);
329-
MP_API int mp_init_copy (mp_int * a, mp_int * b);
329+
MP_API int mp_init_copy (mp_int * a, const mp_int * b);
330330
MP_API int mp_copy (const mp_int * a, mp_int * b);
331331
MP_API int mp_grow (mp_int * a, int size);
332332
MP_API int mp_div_2d (mp_int * a, int b, mp_int * c, mp_int * d);

wolfssl/wolfcrypt/rsa.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -352,28 +352,28 @@ WOLFSSL_API int wc_RsaPSS_VerifyInline(byte* in, word32 inLen, byte** out,
352352
WOLFSSL_API int wc_RsaPSS_VerifyInline_ex(byte* in, word32 inLen, byte** out,
353353
enum wc_HashType hash, int mgf,
354354
int saltLen, RsaKey* key);
355-
WOLFSSL_API int wc_RsaPSS_Verify(byte* in, word32 inLen, byte* out,
355+
WOLFSSL_API int wc_RsaPSS_Verify(const byte* in, word32 inLen, byte* out,
356356
word32 outLen, enum wc_HashType hash, int mgf,
357357
RsaKey* key);
358-
WOLFSSL_API int wc_RsaPSS_Verify_ex(byte* in, word32 inLen, byte* out,
358+
WOLFSSL_API int wc_RsaPSS_Verify_ex(const byte* in, word32 inLen, byte* out,
359359
word32 outLen, enum wc_HashType hash,
360360
int mgf, int saltLen, RsaKey* key);
361-
WOLFSSL_API int wc_RsaPSS_CheckPadding(const byte* in, word32 inLen, byte* sig,
362-
word32 sigSz,
361+
WOLFSSL_API int wc_RsaPSS_CheckPadding(const byte* in, word32 inLen,
362+
const byte* sig, word32 sigSz,
363363
enum wc_HashType hashType);
364364
WOLFSSL_API int wc_RsaPSS_CheckPadding_ex(const byte* in, word32 inLen,
365-
byte* sig, word32 sigSz,
365+
const byte* sig, word32 sigSz,
366366
enum wc_HashType hashType,
367367
int saltLen, int bits);
368368
WOLFSSL_API int wc_RsaPSS_CheckPadding_ex2(const byte* in, word32 inLen,
369-
byte* sig, word32 sigSz,
369+
const byte* sig, word32 sigSz,
370370
enum wc_HashType hashType,
371371
int saltLen, int bits, void* heap);
372372
WOLFSSL_API int wc_RsaPSS_VerifyCheckInline(byte* in, word32 inLen, byte** out,
373373
const byte* digest, word32 digentLen,
374374
enum wc_HashType hash, int mgf,
375375
RsaKey* key);
376-
WOLFSSL_API int wc_RsaPSS_VerifyCheck(byte* in, word32 inLen,
376+
WOLFSSL_API int wc_RsaPSS_VerifyCheck(const byte* in, word32 inLen,
377377
byte* out, word32 outLen,
378378
const byte* digest, word32 digestLen,
379379
enum wc_HashType hash, int mgf,
@@ -440,15 +440,15 @@ WOLFSSL_API int wc_RsaPrivateDecryptInline_ex(byte* in, word32 inLen,
440440
byte** out, RsaKey* key, int type, enum wc_HashType hash,
441441
int mgf, byte* label, word32 labelSz);
442442
#if defined(WC_RSA_DIRECT) || defined(WC_RSA_NO_PADDING) || defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
443-
WOLFSSL_API int wc_RsaDirect(byte* in, word32 inLen, byte* out, word32* outSz,
443+
WOLFSSL_API int wc_RsaDirect(const byte* in, word32 inLen, byte* out, word32* outSz,
444444
RsaKey* key, int type, WC_RNG* rng);
445445
#endif
446446

447447
#endif /* HAVE_FIPS */
448448

449-
WOLFSSL_API int wc_RsaFlattenPublicKey(RsaKey* key, byte* e, word32* eSz,
449+
WOLFSSL_API int wc_RsaFlattenPublicKey(const RsaKey* key, byte* e, word32* eSz,
450450
byte* n, word32* nSz);
451-
WOLFSSL_API int wc_RsaExportKey(RsaKey* key,
451+
WOLFSSL_API int wc_RsaExportKey(const RsaKey* key,
452452
byte* e, word32* eSz,
453453
byte* n, word32* nSz,
454454
byte* d, word32* dSz,

wolfssl/wolfcrypt/tfm.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ int fp_set_bit (fp_int * a, fp_digit b);
520520

521521
/* copy from a to b */
522522
void fp_copy(const fp_int *a, fp_int *b);
523-
void fp_init_copy(fp_int *a, fp_int *b);
523+
void fp_init_copy(fp_int *a, const fp_int *b);
524524

525525
/* clamp digits */
526526
#define fp_clamp(a) { while ((a)->used && (a)->dp[(a)->used-1] == 0) --((a)->used); (a)->sign = (a)->used ? (a)->sign : FP_ZPOS; }
@@ -727,7 +727,7 @@ int fp_leading_bit(fp_int *a);
727727

728728
int fp_unsigned_bin_size(const fp_int *a);
729729
int fp_read_unsigned_bin(fp_int *a, const unsigned char *b, int c);
730-
int fp_to_unsigned_bin(fp_int *a, unsigned char *b);
730+
int fp_to_unsigned_bin(const fp_int *a, unsigned char *b);
731731
int fp_to_unsigned_bin_len_ct(fp_int *a, unsigned char *out, int outSz);
732732
int fp_to_unsigned_bin_len(fp_int *a, unsigned char *b, int c);
733733
int fp_to_unsigned_bin_at_pos(int x, fp_int *t, unsigned char *b);
@@ -813,7 +813,7 @@ int fp_sqr_comba64(fp_int *a, fp_int *b);
813813
#define mp_tohex(M, S) mp_toradix((M), (S), MP_RADIX_HEX)
814814

815815
MP_API int mp_init (mp_int * a);
816-
MP_API int mp_init_copy(fp_int * a, fp_int * b);
816+
MP_API int mp_init_copy(fp_int * a, const fp_int * b);
817817
MP_API void mp_clear (mp_int * a);
818818
MP_API void mp_free (mp_int * a);
819819
MP_API void mp_forcezero (mp_int * a);
@@ -850,7 +850,7 @@ MP_API int mp_cmp_d(mp_int *a, mp_digit b);
850850
MP_API int mp_unsigned_bin_size(const mp_int * a);
851851
MP_API int mp_read_unsigned_bin (mp_int * a, const unsigned char *b, int c);
852852
MP_API int mp_to_unsigned_bin_at_pos(int x, mp_int *t, unsigned char *b);
853-
MP_API int mp_to_unsigned_bin (mp_int * a, unsigned char *b);
853+
MP_API int mp_to_unsigned_bin(const mp_int * a, unsigned char *b);
854854
MP_API int mp_to_unsigned_bin_len_ct(mp_int * a, unsigned char *b, int c);
855855
MP_API int mp_to_unsigned_bin_len(mp_int * a, unsigned char *b, int c);
856856

0 commit comments

Comments
 (0)