bn_mp_to_unsigned_bin.c

Go to the documentation of this file.
00001 #include <tommath.h>
00002 #ifdef BN_MP_TO_UNSIGNED_BIN_C
00003 /* LibTomMath, multiple-precision integer library -- Tom St Denis
00004  *
00005  * LibTomMath is a library that provides multiple-precision
00006  * integer arithmetic as well as number theoretic functionality.
00007  *
00008  * The library was designed directly after the MPI library by
00009  * Michael Fromberger but has been written from scratch with
00010  * additional optimizations in place.
00011  *
00012  * The library is free for all purposes without any express
00013  * guarantee it works.
00014  *
00015  * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
00016  */
00017 
00018 /* store in unsigned [big endian] format */
00019 int mp_to_unsigned_bin (mp_int * a, unsigned char *b)
00020 {
00021   int     x, res;
00022   mp_int  t;
00023 
00024   if ((res = mp_init_copy (&t, a)) != MP_OKAY) {
00025     return res;
00026   }
00027 
00028   x = 0;
00029   while (mp_iszero (&t) == 0) {
00030 #ifndef MP_8BIT
00031       b[x++] = (unsigned char) (t.dp[0] & 255);
00032 #else
00033       b[x++] = (unsigned char) (t.dp[0] | ((t.dp[1] & 0x01) << 7));
00034 #endif
00035     if ((res = mp_div_2d (&t, 8, &t, NULL)) != MP_OKAY) {
00036       mp_clear (&t);
00037       return res;
00038     }
00039   }
00040   bn_reverse (b, x);
00041   mp_clear (&t);
00042   return MP_OKAY;
00043 }
00044 #endif
00045 
00046 /* $Source: /cvsroot/tcl/libtommath/bn_mp_to_unsigned_bin.c,v $ */
00047 /* $Revision: 1.1.1.4 $ */
00048 /* $Date: 2006/12/01 00:08:11 $ */



Generated on Wed Mar 12 12:18:25 2008 by  doxygen 1.5.1