Codebase list libcryptx-perl / 169b0d2
ˇltm tweaking Karel Miko 8 years ago
7 changed file(s) with 51 addition(s) and 80 deletion(s). Raw diff Collapse all Expand all
22 use strict;
33 use warnings ;
44
5 our $VERSION = '0.026_35';
5 our $VERSION = '0.026_36';
66
77 use Exporter 'import';
88 our @EXPORT_OK = qw( _encode_base64url _decode_base64url _encode_base64 _decode_base64 _decode_json _encode_json);
8282 void gcm_gf_mult(const unsigned char *a, const unsigned char *b, unsigned char *c)
8383 {
8484 unsigned char Z[16], V[16];
85 unsigned char x, y, z;
85 unsigned x, y;
86 unsigned char z;
8687
8788 zeromem(Z, 16);
8889 XMEMCPY(V, a, 16);
6464 clock_t t1;
6565 int l, acc, bits, a, b;
6666
67 if (XCLOCKS_PER_SEC < 100 || XCLOCKS_PER_SEC > 10000) {
67 if (XCLOCKS_PER_SEC < 100 || XCLOCKS_PER_SEC > 1000000) {
6868 return 0;
6969 }
7070
1818 unsigned long mp_get_int(mp_int * a)
1919 {
2020 int i;
21 mp_min_u32 res;
21 mp_uint64 res;
2222
2323 if (a->used == 0) {
2424 return 0;
3535 }
3636
3737 /* force result to 32-bits always so it is consistent on non 32-bit platforms */
38 return res & 0xFFFFFFFFUL;
38 return (unsigned long)(res & 0xFFFFFFFFUL);
3939 }
4040 #endif
4141
1414 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1515 */
1616
17 /* get the lower mp_uint64 of an mp_int, platform dependent */
18 mp_uint64 mp_get_long_long (mp_int * a)
17 /* get the lower unsigned long long of an mp_int, platform dependent */
18 unsigned long long mp_get_long_long (mp_int * a)
1919 {
2020 int i;
21 mp_uint64 res;
21 unsigned long long res;
2222
2323 if (a->used == 0) {
2424 return 0;
2525 }
2626
2727 /* get number of digits of the lsb we have to read */
28 i = MIN(a->used,(int)(((sizeof(mp_uint64) * CHAR_BIT) + DIGIT_BIT - 1) / DIGIT_BIT)) - 1;
28 i = MIN(a->used,(int)(((sizeof(unsigned long long) * CHAR_BIT) + DIGIT_BIT - 1) / DIGIT_BIT)) - 1;
2929
3030 /* get most significant digit of result */
3131 res = DIGIT(a,i);
1414 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1515 */
1616
17 /* set a platform dependent mp_uint64 int */
18 MP_SET_XLONG(mp_set_long_long, mp_uint64)
17 /* set a platform dependent unsigned long long int */
18 MP_SET_XLONG(mp_set_long_long, unsigned long long)
1919 #endif
2020
2121 /* $Source$ */
1616
1717 #include <stdio.h>
1818 #include <stdlib.h>
19 #ifdef LTM_NO_STDINT_H
20 typedef unsigned char mp_uint8;
21 typedef unsigned short mp_uint16;
22 typedef unsigned int mp_uint32;
23 typedef unsigned int mp_uint_least32;
24 #ifdef _MSC_VER
25 typedef unsigned __int64 mp_uint64;
26 #else
27 typedef unsigned long long mp_uint64;
28 #endif
29 #else
30 #include <stdint.h>
31 typedef uint8_t mp_uint8;
32 typedef uint16_t mp_uint16;
33 typedef uint32_t mp_uint32;
34 typedef uint_least32_t mp_uint_least32;
35 typedef uint64_t mp_uint64;
36 #endif
3719 #include <limits.h>
3820
3921 #include <tommath_class.h>
4224 extern "C" {
4325 #endif
4426
27 /* unsigned int types */
28 typedef unsigned char mp_uint8;
29 typedef unsigned short mp_uint16;
30 typedef unsigned int mp_uint32;
31 #ifdef _MSC_VER
32 typedef unsigned __int64 mp_uint64;
33 #else
34 typedef unsigned long long mp_uint64;
35 #endif
36
4537 /* detect 64-bit mode if possible */
46 #if defined(__x86_64__)
47 #if !(defined(MP_32BIT) || defined(MP_16BIT) || defined(MP_8BIT))
48 #define MP_64BIT
38 #if !(defined(MP_32BIT) || defined(MP_16BIT) || defined(MP_8BIT))
39 #if defined(__x86_64__)
40 #if defined(__GNUC__)
41 typedef unsigned long mp_uint128 __attribute__ ((mode(TI)));
42 #define MP_64BIT
43 #elif defined(_MSC_VER)
44 typedef unsigned __int128 mp_uint128;
45 #define MP_64BIT
46 #endif
4947 #endif
5048 #endif
5149
5856 * [any size beyond that is ok provided it doesn't overflow the data type]
5957 */
6058 #ifdef MP_8BIT
61 typedef mp_uint8 mp_digit;
62 typedef mp_uint16 mp_word;
63 #define MP_SIZEOF_MP_DIGIT 1
64 #ifdef DIGIT_BIT
65 #error You must not define DIGIT_BIT when using MP_8BIT
66 #endif
59 typedef mp_uint8 mp_digit;
60 typedef mp_uint16 mp_word;
61 #define DIGIT_BIT 7
6762 #elif defined(MP_16BIT)
68 typedef mp_uint16 mp_digit;
69 typedef mp_uint32 mp_word;
70 #define MP_SIZEOF_MP_DIGIT 2
71 #ifdef DIGIT_BIT
72 #error You must not define DIGIT_BIT when using MP_16BIT
73 #endif
63 typedef mp_uint16 mp_digit;
64 typedef mp_uint32 mp_word;
65 #define DIGIT_BIT 15
7466 #elif defined(MP_64BIT)
75 /* for GCC only on supported platforms */
76 typedef mp_uint64 mp_digit;
77 #if defined(_WIN32)
78 typedef unsigned __int128 mp_word;
79 #elif defined(__GNUC__)
80 typedef unsigned long mp_word __attribute__ ((mode(TI)));
67 typedef mp_uint64 mp_digit;
68 typedef mp_uint128 mp_word;
69 #define DIGIT_BIT 60
70 #elif defined(MP_32BIT)
71 typedef mp_uint32 mp_digit;
72 typedef mp_uint64 mp_word;
73 #define DIGIT_BIT 31
8174 #else
82 /* it seems you have a problem
83 * but we assume you can somewhere define your own uint128_t */
84 typedef uint128_t mp_word;
85 #endif
86
87 #define DIGIT_BIT 60
88 #else
89 /* this is the default case, 28-bit digits */
90 typedef mp_uint32 mp_digit;
91 typedef mp_uint64 mp_word;
92
93 #ifdef MP_31BIT
94 /* this is an extension that uses 31-bit digits */
95 #define DIGIT_BIT 31
96 #else
97 /* default case is 28-bit digits, defines MP_28BIT as a handy macro to test */
98 #define DIGIT_BIT 28
75 typedef mp_uint32 mp_digit;
76 typedef mp_uint64 mp_word;
77 #define DIGIT_BIT 28
9978 #define MP_28BIT
100 #endif
101 #endif
102
103 /* otherwise the bits per digit is calculated automatically from the size of a mp_digit */
104 #ifndef DIGIT_BIT
105 #define DIGIT_BIT (((CHAR_BIT * MP_SIZEOF_MP_DIGIT) - 1)) /* bits per digit */
106 typedef mp_uint_least32 mp_min_u32;
107 #else
108 typedef mp_digit mp_min_u32;
10979 #endif
11080
11181 /* platforms that can use a better rand function */
228198 /* set a platform dependent unsigned long value */
229199 int mp_set_long(mp_int *a, unsigned long b);
230200
231 /* set a platform dependent mp_uint64 value */
232 int mp_set_long_long(mp_int *a, mp_uint64 b);
201 /* set a platform dependent unsigned long long value */
202 int mp_set_long_long(mp_int *a, unsigned long long b);
233203
234204 /* get a 32-bit value */
235205 unsigned long mp_get_int(mp_int * a);
237207 /* get a platform dependent unsigned long value */
238208 unsigned long mp_get_long(mp_int * a);
239209
240 /* get a platform dependent mp_uint64 value */
241 mp_uint64 mp_get_long_long(mp_int * a);
210 /* get a platform dependent unsigned long long value */
211 unsigned long long mp_get_long_long(mp_int * a);
242212
243213 /* initialize and set a digit */
244214 int mp_init_set (mp_int * a, mp_digit b);