1 // SPDX-License-Identifier: BSD-2-Clause
2 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3  *
4  * LibTomCrypt is a library that provides various cryptographic
5  * algorithms in a highly modular and flexible manner.
6  *
7  * The library is free for all purposes without any express
8  * guarantee it works.
9  */
10 #include "tomcrypt_private.h"
11 
12 /**
13   @file pkcs_1_i2osp.c
14   Integer to Octet I2OSP, Tom St Denis
15 */
16 
17 #ifdef LTC_PKCS_1
18 
19 /* always stores the same # of bytes, pads with leading zero bytes
20    as required
21  */
22 
23 /**
24    PKCS #1 Integer to binary
25    @param n             The integer to store
26    @param modulus_len   The length of the RSA modulus
27    @param out           [out] The destination for the integer
28    @return CRYPT_OK if successful
29 */
pkcs_1_i2osp(void * n,unsigned long modulus_len,unsigned char * out)30 int pkcs_1_i2osp(void *n, unsigned long modulus_len, unsigned char *out)
31 {
32    unsigned long size;
33 
34    size = mp_unsigned_bin_size(n);
35 
36    if (size > modulus_len) {
37       return CRYPT_BUFFER_OVERFLOW;
38    }
39 
40    /* store it */
41    zeromem(out, modulus_len);
42    return mp_to_unsigned_bin(n, out+(modulus_len-size));
43 }
44 
45 #endif /* LTC_PKCS_1 */
46 
47 
48 /* ref:         $Format:%D$ */
49 /* git commit:  $Format:%H$ */
50 /* commit time: $Format:%ai$ */
51