comparison bignum.c @ 640:76097ec1a29a dropbear-tfm

- Bring in original tomsfastmath patch against 0.52 from Peter Turczak in 2008
author Matt Johnston <matt@ucc.asn.au>
date Mon, 21 Nov 2011 19:19:57 +0800
parents c9483550701b
children 2b1bb792cd4d
comparison
equal deleted inserted replaced
518:ce104c8b0be1 640:76097ec1a29a
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE. */ 23 * SOFTWARE. */
24 24
25 /* Contains helper functions for mp_int handling */ 25 /* Contains helper functions for fp_int handling */
26 26
27 #include "includes.h" 27 #include "includes.h"
28 #include "dbutil.h" 28 #include "dbutil.h"
29 29
30 /* wrapper for mp_init, failing fatally on errors (memory allocation) */ 30 /* wrapper for fp_init, failing fatally on errors (memory allocation) */
31 void m_mp_init(mp_int *mp) { 31 void m_fp_init(fp_int *fp) {
32 32
33 if (mp_init(mp) != MP_OKAY) { 33 fp_init(fp);
34 dropbear_exit("mem alloc error");
35 }
36 } 34 }
37 35
38 /* simplified duplication of bn_mp_multi's mp_init_multi, but die fatally 36 /* simplified duplication of bn_fp_multi's fp_init_multi, but die fatally
39 * on error */ 37 * on error */
40 void m_mp_init_multi(mp_int *mp, ...) 38 void m_fp_init_multi(fp_int *fp, ...)
41 { 39 {
42 mp_int* cur_arg = mp; 40 fp_int* cur_arg = fp;
43 va_list args; 41 va_list args;
44 42
45 va_start(args, mp); /* init args to next argument from caller */ 43 va_start(args, fp); /* init args to next argument from caller */
46 while (cur_arg != NULL) { 44 while (cur_arg != NULL) {
47 if (mp_init(cur_arg) != MP_OKAY) { 45 fp_init(cur_arg);
48 dropbear_exit("mem alloc error"); 46 cur_arg = va_arg(args, fp_int*);
49 }
50 cur_arg = va_arg(args, mp_int*);
51 } 47 }
52 va_end(args); 48 va_end(args);
53 } 49 }
54 50
55 void bytes_to_mp(mp_int *mp, const unsigned char* bytes, unsigned int len) { 51 /* simplified duplication of bn_fp_multi's fp_init_multi, but die fatally
52 * on error */
53 void m_fp_zero_multi(fp_int *fp, ...)
54 {
55 fp_int* cur_arg = fp;
56 va_list args;
56 57
57 if (mp_read_unsigned_bin(mp, (unsigned char*)bytes, len) != MP_OKAY) { 58 va_start(args, fp); /* init args to next argument from caller */
58 dropbear_exit("mem alloc error"); 59 while (cur_arg != NULL) {
59 } 60 fp_zero(cur_arg);
61 cur_arg = va_arg(args, fp_int*);
62 }
63 va_end(args);
60 } 64 }
61 65
62 /* hash the ssh representation of the mp_int mp */ 66 void bytes_to_fp(fp_int *fp, const unsigned char* bytes, unsigned int len) {
63 void sha1_process_mp(hash_state *hs, mp_int *mp) { 67
68 fp_read_unsigned_bin(fp, (unsigned char*)bytes, len);
69 }
70
71 /* hash the ssh representation of the fp_int fp */
72 void sha1_process_fp(hash_state *hs, fp_int *fp) {
64 73
65 int i; 74 int i;
66 buffer * buf; 75 buffer * buf;
67 76
68 buf = buf_new(512 + 20); /* max buffer is a 4096 bit key, 77 buf = buf_new(512 + 20); /* max buffer is a 4096 bit key,
69 plus header + some leeway*/ 78 plus header + some leeway*/
70 buf_putmpint(buf, mp); 79 buf_putfpint(buf, fp);
71 i = buf->pos; 80 i = buf->pos;
72 buf_setpos(buf, 0); 81 buf_setpos(buf, 0);
73 sha1_process(hs, buf_getptr(buf, i), i); 82 sha1_process(hs, buf_getptr(buf, i), i);
74 buf_free(buf); 83 buf_free(buf);
75 } 84 }