142
|
1 #include <tommath.h> |
|
2 #ifdef BN_MP_TO_UNSIGNED_BIN_C |
2
|
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
4 * |
|
5 * LibTomMath is a library that provides multiple-precision |
|
6 * integer arithmetic as well as number theoretic functionality. |
|
7 * |
|
8 * The library was designed directly after the MPI library by |
|
9 * Michael Fromberger but has been written from scratch with |
|
10 * additional optimizations in place. |
|
11 * |
|
12 * The library is free for all purposes without any express |
|
13 * guarantee it works. |
|
14 * |
|
15 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
16 */ |
|
17 |
|
18 /* store in unsigned [big endian] format */ |
|
19 int |
|
20 mp_to_unsigned_bin (mp_int * a, unsigned char *b) |
|
21 { |
|
22 int x, res; |
|
23 mp_int t; |
|
24 |
|
25 if ((res = mp_init_copy (&t, a)) != MP_OKAY) { |
|
26 return res; |
|
27 } |
|
28 |
|
29 x = 0; |
|
30 while (mp_iszero (&t) == 0) { |
|
31 #ifndef MP_8BIT |
|
32 b[x++] = (unsigned char) (t.dp[0] & 255); |
|
33 #else |
|
34 b[x++] = (unsigned char) (t.dp[0] | ((t.dp[1] & 0x01) << 7)); |
|
35 #endif |
|
36 if ((res = mp_div_2d (&t, 8, &t, NULL)) != MP_OKAY) { |
|
37 mp_clear (&t); |
|
38 return res; |
|
39 } |
|
40 } |
|
41 bn_reverse (b, x); |
|
42 mp_clear (&t); |
|
43 return MP_OKAY; |
|
44 } |
142
|
45 #endif |