secmpi.h (2926B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 #include "mpi.h" 6 7 #define CHECK_SEC_OK(func) \ 8 if (SECSuccess != (rv = func)) \ 9 goto cleanup 10 11 #define CHECK_MPI_OK(func) \ 12 if (MP_OKAY > (err = func)) \ 13 goto cleanup 14 15 #define OCTETS_TO_MPINT(oc, mp, len) \ 16 CHECK_MPI_OK(mp_read_unsigned_octets((mp), oc, len)) 17 18 #define SECITEM_TO_MPINT(it, mp) \ 19 CHECK_MPI_OK(mp_read_unsigned_octets((mp), (it).data, (it).len)) 20 21 #define MPINT_TO_SECITEM(mp, it, arena) \ 22 do { \ 23 int mpintLen = mp_unsigned_octet_size(mp); \ 24 if (mpintLen <= 0) { \ 25 err = MP_RANGE; \ 26 goto cleanup; \ 27 } \ 28 SECITEM_AllocItem(arena, (it), mpintLen); \ 29 if ((it)->data == NULL) { \ 30 err = MP_MEM; \ 31 goto cleanup; \ 32 } \ 33 err = mp_to_unsigned_octets(mp, (it)->data, (it)->len); \ 34 if (err < 0) \ 35 goto cleanup; \ 36 else \ 37 err = MP_OKAY; \ 38 } while (0) 39 40 #define MP_TO_SEC_ERROR(err) \ 41 switch (err) { \ 42 case MP_MEM: \ 43 PORT_SetError(SEC_ERROR_NO_MEMORY); \ 44 break; \ 45 case MP_RANGE: \ 46 PORT_SetError(SEC_ERROR_BAD_DATA); \ 47 break; \ 48 case MP_BADARG: \ 49 PORT_SetError(SEC_ERROR_INVALID_ARGS); \ 50 break; \ 51 default: \ 52 PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); \ 53 break; \ 54 } 55 56 /* Fill the `used` digits of an mp_int with random bits */ 57 mp_err mpp_random_secure(mp_int *a); 58 59 /* Pseudo-primality testing using `mpp_random_secure` to choose Miller-Rabin base */ 60 mp_err mpp_pprime_secure(mp_int *a, int nt); 61 62 /* Variant of `mpp_make_prime` using `mpp_random_secure` to choose Miller-Rabin base */ 63 mp_err mpp_make_prime_secure(mp_int *start, mp_size nBits, mp_size strong);