Mercurial > dropbear
annotate 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 |
rev | line source |
---|---|
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1 /* |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
2 * Dropbear - a SSH2 server |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
3 * |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
4 * Copyright (c) 2002,2003 Matt Johnston |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
5 * All rights reserved. |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
6 * |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
7 * Permission is hereby granted, free of charge, to any person obtaining a copy |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
8 * of this software and associated documentation files (the "Software"), to deal |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
9 * in the Software without restriction, including without limitation the rights |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
11 * copies of the Software, and to permit persons to whom the Software is |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
12 * furnished to do so, subject to the following conditions: |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
13 * |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
14 * The above copyright notice and this permission notice shall be included in |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
15 * all copies or substantial portions of the Software. |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
16 * |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
23 * SOFTWARE. */ |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
24 |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
188
diff
changeset
|
25 /* Contains helper functions for fp_int handling */ |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
26 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
27 #include "includes.h" |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
28 #include "dbutil.h" |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
29 |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
188
diff
changeset
|
30 /* wrapper for fp_init, failing fatally on errors (memory allocation) */ |
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
188
diff
changeset
|
31 void m_fp_init(fp_int *fp) { |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
32 |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
188
diff
changeset
|
33 fp_init(fp); |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
34 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
35 |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
188
diff
changeset
|
36 /* simplified duplication of bn_fp_multi's fp_init_multi, but die fatally |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
37 * on error */ |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
188
diff
changeset
|
38 void m_fp_init_multi(fp_int *fp, ...) |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
39 { |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
188
diff
changeset
|
40 fp_int* cur_arg = fp; |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
41 va_list args; |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
42 |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
188
diff
changeset
|
43 va_start(args, fp); /* init args to next argument from caller */ |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
44 while (cur_arg != NULL) { |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
188
diff
changeset
|
45 fp_init(cur_arg); |
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
188
diff
changeset
|
46 cur_arg = va_arg(args, fp_int*); |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
47 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
48 va_end(args); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
49 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
50 |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
188
diff
changeset
|
51 /* simplified duplication of bn_fp_multi's fp_init_multi, but die fatally |
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
188
diff
changeset
|
52 * on error */ |
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
188
diff
changeset
|
53 void m_fp_zero_multi(fp_int *fp, ...) |
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
188
diff
changeset
|
54 { |
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
188
diff
changeset
|
55 fp_int* cur_arg = fp; |
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
188
diff
changeset
|
56 va_list args; |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
57 |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
188
diff
changeset
|
58 va_start(args, fp); /* init args to next argument from caller */ |
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
188
diff
changeset
|
59 while (cur_arg != NULL) { |
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
188
diff
changeset
|
60 fp_zero(cur_arg); |
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
188
diff
changeset
|
61 cur_arg = va_arg(args, fp_int*); |
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
188
diff
changeset
|
62 } |
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
188
diff
changeset
|
63 va_end(args); |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
64 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
65 |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
188
diff
changeset
|
66 void bytes_to_fp(fp_int *fp, const unsigned char* bytes, unsigned int len) { |
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
188
diff
changeset
|
67 |
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
188
diff
changeset
|
68 fp_read_unsigned_bin(fp, (unsigned char*)bytes, len); |
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
188
diff
changeset
|
69 } |
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
188
diff
changeset
|
70 |
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
188
diff
changeset
|
71 /* hash the ssh representation of the fp_int fp */ |
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
188
diff
changeset
|
72 void sha1_process_fp(hash_state *hs, fp_int *fp) { |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
73 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
74 int i; |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
75 buffer * buf; |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
76 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
77 buf = buf_new(512 + 20); /* max buffer is a 4096 bit key, |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
78 plus header + some leeway*/ |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
188
diff
changeset
|
79 buf_putfpint(buf, fp); |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
80 i = buf->pos; |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
81 buf_setpos(buf, 0); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
82 sha1_process(hs, buf_getptr(buf, i), i); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
83 buf_free(buf); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
84 } |