1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 #include "tomcrypt_private.h"
4 
5 /**
6   @file pkcs_1_i2osp.c
7   Integer to Octet I2OSP, Tom St Denis
8 */
9 
10 #ifdef LTC_PKCS_1
11 
12 /* always stores the same # of bytes, pads with leading zero bytes
13    as required
14  */
15 
16 /**
17    PKCS #1 Integer to binary
18    @param n             The integer to store
19    @param modulus_len   The length of the RSA modulus
20    @param out           [out] The destination for the integer
21    @return CRYPT_OK if successful
22 */
pkcs_1_i2osp(void * n,unsigned long modulus_len,unsigned char * out)23 int pkcs_1_i2osp(void *n, unsigned long modulus_len, unsigned char *out)
24 {
25    unsigned long size;
26 
27    size = mp_unsigned_bin_size(n);
28 
29    if (size > modulus_len) {
30       return CRYPT_BUFFER_OVERFLOW;
31    }
32 
33    /* store it */
34    zeromem(out, modulus_len);
35    return mp_to_unsigned_bin(n, out+(modulus_len-size));
36 }
37 
38 #endif /* LTC_PKCS_1 */
39 
40