charm-crypto-lite 0.61.1__cp314-cp314-macosx_10_15_universal2.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- charm_crypto_lite-0.61.1.dist-info/METADATA +158 -0
- charm_crypto_lite-0.61.1.dist-info/RECORD +21 -0
- charm_crypto_lite-0.61.1.dist-info/WHEEL +5 -0
- charm_crypto_lite-0.61.1.dist-info/top_level.txt +1 -0
- charm_lite/__init__.py +22 -0
- charm_lite/core/__init__.py +2 -0
- charm_lite/core/benchmark/benchmark_util.c +353 -0
- charm_lite/core/benchmark/benchmark_util.h +61 -0
- charm_lite/core/benchmark/benchmarkmodule.c +476 -0
- charm_lite/core/benchmark/benchmarkmodule.h +162 -0
- charm_lite/core/benchmark.cpython-314-darwin.so +0 -0
- charm_lite/core/math/__init__.py +2 -0
- charm_lite/core/math/elliptic_curve/ecmodule.c +1986 -0
- charm_lite/core/math/elliptic_curve/ecmodule.h +230 -0
- charm_lite/core/math/elliptic_curve.cpython-314-darwin.so +0 -0
- charm_lite/core/utilities/base64.c +248 -0
- charm_lite/core/utilities/base64.h +15 -0
- charm_lite/toolbox/PKEnc.py +60 -0
- charm_lite/toolbox/__init__.py +15 -0
- charm_lite/toolbox/eccurve.py +12 -0
- charm_lite/toolbox/ecgroup.py +147 -0
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Charm-Crypto is a framework for rapidly prototyping cryptosystems.
|
|
3
|
+
*
|
|
4
|
+
* Charm-Crypto is free software; you can redistribute it and/or
|
|
5
|
+
* modify it under the terms of the GNU Lesser General Public
|
|
6
|
+
* License as published by the Free Software Foundation; either
|
|
7
|
+
* version 2.1 of the License, or (at your option) any later version.
|
|
8
|
+
*
|
|
9
|
+
* Charm-Crypto is distributed in the hope that it will be useful,
|
|
10
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
12
|
+
* Lesser General Public License for more details.
|
|
13
|
+
*
|
|
14
|
+
* You should have received a copy of the GNU Lesser General Public License
|
|
15
|
+
* along with Charm-Crypto. If not, see <http://www.gnu.org/licenses/>.
|
|
16
|
+
*
|
|
17
|
+
* Please contact the charm-crypto dev team at support@charm-crypto.com
|
|
18
|
+
* for any questions.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/*
|
|
22
|
+
* @file ecmodule.h
|
|
23
|
+
*
|
|
24
|
+
* @brief charm interface over OpenSSL Ellipic-curve module
|
|
25
|
+
*
|
|
26
|
+
* @author jakinye3@jhu.edu
|
|
27
|
+
*
|
|
28
|
+
************************************************************************/
|
|
29
|
+
|
|
30
|
+
#ifndef ECMODULE_H
|
|
31
|
+
#define ECMODULE_H
|
|
32
|
+
|
|
33
|
+
#ifndef PY_SSIZE_T_CLEAN
|
|
34
|
+
#define PY_SSIZE_T_CLEAN
|
|
35
|
+
#endif
|
|
36
|
+
|
|
37
|
+
#include <Python.h>
|
|
38
|
+
#include <structmember.h>
|
|
39
|
+
|
|
40
|
+
#if PY_MINOR_VERSION <= 10
|
|
41
|
+
#include <longintrepr.h>
|
|
42
|
+
#else
|
|
43
|
+
#include <cpython/longintrepr.h> /* for conversions */
|
|
44
|
+
#endif
|
|
45
|
+
|
|
46
|
+
#include <math.h>
|
|
47
|
+
#include "benchmarkmodule.h"
|
|
48
|
+
#include "base64.h"
|
|
49
|
+
|
|
50
|
+
/* Openssl header files */
|
|
51
|
+
#include <openssl/ec.h>
|
|
52
|
+
#include <openssl/err.h>
|
|
53
|
+
#include <openssl/obj_mac.h>
|
|
54
|
+
#include <openssl/objects.h>
|
|
55
|
+
#include <openssl/rand.h>
|
|
56
|
+
#include <openssl/bn.h>
|
|
57
|
+
#include <openssl/sha.h>
|
|
58
|
+
#include <openssl/evp.h>
|
|
59
|
+
#ifdef BENCHMARK_ENABLED
|
|
60
|
+
#include "benchmark_util.h"
|
|
61
|
+
#endif
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
//#define DEBUG 1
|
|
65
|
+
#define TRUE 1
|
|
66
|
+
#define FALSE 0
|
|
67
|
+
#define BYTE 8
|
|
68
|
+
#define ID_LEN BYTE
|
|
69
|
+
#define BASE_DEC 10
|
|
70
|
+
#define BASE_HEX 16
|
|
71
|
+
#define MAX_BUF 256
|
|
72
|
+
#define RAND_MAX_BYTES 2048
|
|
73
|
+
/* Index numbers for different hash functions. These are all implemented as SHA1(index || message). */
|
|
74
|
+
#define HASH_FUNCTION_STR_TO_ZR_CRH 10
|
|
75
|
+
#define HASH_FUNCTION_STR_TO_G_CRH 11
|
|
76
|
+
#define HASH_FUNCTION_KEM_DERIVE 12
|
|
77
|
+
#define HASH_LEN SHA256_DIGEST_LENGTH
|
|
78
|
+
#define RESERVED_ENCODING_BYTES 4
|
|
79
|
+
|
|
80
|
+
PyTypeObject ECType;
|
|
81
|
+
PyTypeObject ECGroupType;
|
|
82
|
+
PyTypeObject OperationType;
|
|
83
|
+
static PyObject *PyECErrorObject;
|
|
84
|
+
#define PyEC_Check(obj) PyObject_TypeCheck(obj, &ECType)
|
|
85
|
+
#define PyECGroup_Check(obj) PyObject_TypeCheck(obj, &ECGroupType)
|
|
86
|
+
enum Group {ZR = 0, G, NONE_G};
|
|
87
|
+
typedef enum Group GroupType;
|
|
88
|
+
|
|
89
|
+
PyMethodDef ECElement_methods[];
|
|
90
|
+
PyNumberMethods ecc_number;
|
|
91
|
+
|
|
92
|
+
#ifdef BENCHMARK_ENABLED
|
|
93
|
+
typedef struct {
|
|
94
|
+
PyObject_HEAD
|
|
95
|
+
int op_init;
|
|
96
|
+
int exp_ZR, exp_G;
|
|
97
|
+
int mul_ZR, mul_G;
|
|
98
|
+
int div_ZR, div_G;
|
|
99
|
+
|
|
100
|
+
int add_ZR, add_G;
|
|
101
|
+
int sub_ZR, sub_G;
|
|
102
|
+
} Operations;
|
|
103
|
+
#endif
|
|
104
|
+
|
|
105
|
+
typedef struct {
|
|
106
|
+
PyObject_HEAD
|
|
107
|
+
EC_GROUP *ec_group;
|
|
108
|
+
int group_init;
|
|
109
|
+
int nid;
|
|
110
|
+
BN_CTX *ctx;
|
|
111
|
+
BIGNUM *order;
|
|
112
|
+
#ifdef BENCHMARK_ENABLED
|
|
113
|
+
Benchmark *dBench;
|
|
114
|
+
Operations *gBench;
|
|
115
|
+
uint8_t bench_id[ID_LEN+1];
|
|
116
|
+
#endif
|
|
117
|
+
} ECGroup;
|
|
118
|
+
|
|
119
|
+
typedef struct {
|
|
120
|
+
PyObject_HEAD
|
|
121
|
+
GroupType type;
|
|
122
|
+
ECGroup *group;
|
|
123
|
+
EC_POINT *P;
|
|
124
|
+
BIGNUM *elemZ;
|
|
125
|
+
int point_init;
|
|
126
|
+
} ECElement;
|
|
127
|
+
|
|
128
|
+
#define PyLong_ToUnsignedLong(o) PyLong_AsUnsignedLong(o)
|
|
129
|
+
#define PyLongCheck(o) PyLong_Check(o)
|
|
130
|
+
|
|
131
|
+
#define ErrorMsg(msg) \
|
|
132
|
+
PyErr_SetString(PyECErrorObject, msg); \
|
|
133
|
+
debug("%s: %d error occured here!", __FUNCTION__, __LINE__); \
|
|
134
|
+
return NULL;
|
|
135
|
+
|
|
136
|
+
#define Check_Types2(o1, o2, lhs, rhs, foundLHS, foundRHS) \
|
|
137
|
+
if(PyEC_Check(o1)) { \
|
|
138
|
+
lhs = (ECElement *) o1; \
|
|
139
|
+
debug("found a lhs object.\n"); \
|
|
140
|
+
} \
|
|
141
|
+
else if(PyLongCheck(o1)) { \
|
|
142
|
+
foundLHS = TRUE; } \
|
|
143
|
+
else { ErrorMsg("invalid type specified."); \
|
|
144
|
+
} \
|
|
145
|
+
if(PyEC_Check(o2)) { \
|
|
146
|
+
rhs = (ECElement *) o2; \
|
|
147
|
+
debug("found a rhs object.\n"); \
|
|
148
|
+
} \
|
|
149
|
+
else if(PyLongCheck(o2)) { \
|
|
150
|
+
foundRHS = TRUE; } \
|
|
151
|
+
else { ErrorMsg("invalid type specified."); \
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
#define Group_NULL(obj) if(obj->ec_group == NULL) { \
|
|
155
|
+
PyErr_SetString(PyECErrorObject, "group object not allocated."); \
|
|
156
|
+
return NULL; }
|
|
157
|
+
|
|
158
|
+
#define VERIFY_GROUP(obj) \
|
|
159
|
+
if(!PyECGroup_Check(obj)) { \
|
|
160
|
+
PyErr_SetString(PyECErrorObject, "not an ecc object."); return NULL; } \
|
|
161
|
+
if(obj->group_init == FALSE || obj->ec_group == NULL) { \
|
|
162
|
+
PyErr_SetString(PyECErrorObject, "group object not initialized."); \
|
|
163
|
+
return NULL; }
|
|
164
|
+
|
|
165
|
+
#define Point_Init(obj) if(!obj->point_init) { \
|
|
166
|
+
printf("ERROR: element not initialized.\n"); \
|
|
167
|
+
return NULL; }
|
|
168
|
+
|
|
169
|
+
#define isPoint(a) a->type == G
|
|
170
|
+
#define ElementG(a, b) a->type == G && b->type == G
|
|
171
|
+
#define ElementZR(a, b) a->type == ZR && b->type == ZR
|
|
172
|
+
|
|
173
|
+
void setBigNum(PyLongObject *obj, BIGNUM **value);
|
|
174
|
+
PyObject *ECElement_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
|
|
175
|
+
int ECElement_init(ECElement *self, PyObject *args, PyObject *kwds);
|
|
176
|
+
PyObject *ECElement_call(ECElement *intObject, PyObject *args, PyObject *kwds);
|
|
177
|
+
PyObject *ECElement_print(ECElement *self);
|
|
178
|
+
void ECElement_dealloc(ECElement* self);
|
|
179
|
+
|
|
180
|
+
ECElement *negatePoint(ECElement *self);
|
|
181
|
+
ECElement *invertECElement(ECElement *self);
|
|
182
|
+
int hash_to_bytes(uint8_t *input_buf, int input_len, uint8_t *output_buf, int hash_len, uint8_t hash_prefix);
|
|
183
|
+
void set_element_from_hash(ECElement *self, uint8_t *input, int input_len);
|
|
184
|
+
|
|
185
|
+
#define EXIT_IF(check, msg) \
|
|
186
|
+
if(check) { \
|
|
187
|
+
PyErr_SetString(PyECErrorObject, msg); \
|
|
188
|
+
return NULL; }
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
#ifdef BENCHMARK_ENABLED
|
|
192
|
+
|
|
193
|
+
#define IS_SAME_GROUP(a, b) \
|
|
194
|
+
if(a->group->nid != b->group->nid) { \
|
|
195
|
+
PyErr_SetString(PyECErrorObject, "mixing group elements from different curves."); \
|
|
196
|
+
return NULL; \
|
|
197
|
+
} \
|
|
198
|
+
if(strncmp((const char *) a->group->bench_id, (const char *) b->group->bench_id, ID_LEN) != 0) { \
|
|
199
|
+
PyErr_SetString(PyECErrorObject, "mixing benchmark objects not allowed."); \
|
|
200
|
+
return NULL; \
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
#define IsBenchSet(obj) obj->dBench != NULL
|
|
204
|
+
|
|
205
|
+
#define Update_Op(name, op_type, elem_type, bench_obj) \
|
|
206
|
+
Op_ ##name(op_type, elem_type, ZR, bench_obj) \
|
|
207
|
+
Op_ ##name(op_type, elem_type, G, bench_obj) \
|
|
208
|
+
|
|
209
|
+
#define CLEAR_ALLDBENCH(bench_obj) \
|
|
210
|
+
CLEAR_DBENCH(bench_obj, ZR); \
|
|
211
|
+
CLEAR_DBENCH(bench_obj, G);
|
|
212
|
+
|
|
213
|
+
#else
|
|
214
|
+
|
|
215
|
+
#define IS_SAME_GROUP(a, b) \
|
|
216
|
+
if(a->group->nid != b->group->nid) { \
|
|
217
|
+
PyErr_SetString(PyECErrorObject, "mixing group elements from different curves."); \
|
|
218
|
+
return NULL; \
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
#define UPDATE_BENCH(op_type, elem_type, bench_obj) /* ... */
|
|
222
|
+
// #define UPDATE_BENCHMARK(op_type, bench_obj) /* ... */
|
|
223
|
+
#define CLEAR_ALLDBENCH(bench_obj) /* ... */
|
|
224
|
+
#define GetField(count, type, group, bench_obj) /* ... */
|
|
225
|
+
|
|
226
|
+
#endif
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
#endif
|
|
Binary file
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
#include "base64.h"
|
|
2
|
+
|
|
3
|
+
//
|
|
4
|
+
// NSData+Base64.m
|
|
5
|
+
// base64
|
|
6
|
+
//
|
|
7
|
+
// Created by Matt Gallagher on 2009/06/03.
|
|
8
|
+
// Copyright 2009 Matt Gallagher. All rights reserved.
|
|
9
|
+
//
|
|
10
|
+
// Permission is given to use this source code file, free of charge, in any
|
|
11
|
+
// project, commercial or otherwise, entirely at your risk, with the condition
|
|
12
|
+
// that any redistribution (in part or whole) of source code must retain
|
|
13
|
+
// this copyright and permission notice. Attribution in compiled projects is
|
|
14
|
+
// appreciated but not required.
|
|
15
|
+
|
|
16
|
+
//
|
|
17
|
+
// Mapping from 6 bit pattern to ASCII character.
|
|
18
|
+
//
|
|
19
|
+
static unsigned char base64EncodeLookup[65] =
|
|
20
|
+
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
21
|
+
|
|
22
|
+
//
|
|
23
|
+
// Definition for "masked-out" areas of the base64DecodeLookup mapping
|
|
24
|
+
//
|
|
25
|
+
#define xx 65
|
|
26
|
+
|
|
27
|
+
//
|
|
28
|
+
// Mapping from ASCII character to 6 bit pattern.
|
|
29
|
+
//
|
|
30
|
+
static unsigned char base64DecodeLookup[256] =
|
|
31
|
+
{
|
|
32
|
+
xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx,
|
|
33
|
+
xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx,
|
|
34
|
+
xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, 62, xx, xx, xx, 63,
|
|
35
|
+
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, xx, xx, xx, xx, xx, xx,
|
|
36
|
+
xx, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
|
|
37
|
+
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, xx, xx, xx, xx, xx,
|
|
38
|
+
xx, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
|
|
39
|
+
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, xx, xx, xx, xx, xx,
|
|
40
|
+
xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx,
|
|
41
|
+
xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx,
|
|
42
|
+
xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx,
|
|
43
|
+
xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx,
|
|
44
|
+
xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx,
|
|
45
|
+
xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx,
|
|
46
|
+
xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx,
|
|
47
|
+
xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx,
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
//
|
|
51
|
+
// Fundamental sizes of the binary and base64 encode/decode units in bytes
|
|
52
|
+
//
|
|
53
|
+
#define BINARY_UNIT_SIZE 3
|
|
54
|
+
#define BASE64_UNIT_SIZE 4
|
|
55
|
+
|
|
56
|
+
//
|
|
57
|
+
// NewBase64Decode
|
|
58
|
+
//
|
|
59
|
+
// Decodes the base64 ASCII string in the inputBuffer to a newly malloced
|
|
60
|
+
// output buffer.
|
|
61
|
+
//
|
|
62
|
+
// inputBuffer - the source ASCII string for the decode
|
|
63
|
+
// length - the length of the string or -1 (to specify strlen should be used)
|
|
64
|
+
// outputLength - if not-NULL, on output will contain the decoded length
|
|
65
|
+
//
|
|
66
|
+
// returns the decoded buffer. Must be free'd by caller. Length is given by
|
|
67
|
+
// outputLength.
|
|
68
|
+
//
|
|
69
|
+
void *NewBase64Decode(
|
|
70
|
+
const char *inputBuffer,
|
|
71
|
+
size_t length,
|
|
72
|
+
size_t *outputLength)
|
|
73
|
+
{
|
|
74
|
+
if ((int) length == -1)
|
|
75
|
+
{
|
|
76
|
+
length = strlen(inputBuffer);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
size_t outputBufferSize =
|
|
80
|
+
((length+BASE64_UNIT_SIZE-1) / BASE64_UNIT_SIZE) * BINARY_UNIT_SIZE;
|
|
81
|
+
unsigned char *outputBuffer = (unsigned char *)malloc(outputBufferSize);
|
|
82
|
+
|
|
83
|
+
size_t i = 0;
|
|
84
|
+
size_t j = 0;
|
|
85
|
+
while (i < length)
|
|
86
|
+
{
|
|
87
|
+
//
|
|
88
|
+
// Accumulate 4 valid characters (ignore everything else)
|
|
89
|
+
//
|
|
90
|
+
unsigned char accumulated[BASE64_UNIT_SIZE];
|
|
91
|
+
size_t accumulateIndex = 0;
|
|
92
|
+
while (i < length)
|
|
93
|
+
{
|
|
94
|
+
unsigned char decode = base64DecodeLookup[(unsigned char)inputBuffer[i++]];
|
|
95
|
+
if (decode != xx)
|
|
96
|
+
{
|
|
97
|
+
accumulated[accumulateIndex] = decode;
|
|
98
|
+
accumulateIndex++;
|
|
99
|
+
|
|
100
|
+
if (accumulateIndex == BASE64_UNIT_SIZE)
|
|
101
|
+
{
|
|
102
|
+
break;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
//
|
|
108
|
+
// Store the 6 bits from each of the 4 characters as 3 bytes
|
|
109
|
+
//
|
|
110
|
+
outputBuffer[j] = (accumulated[0] << 2) | (accumulated[1] >> 4);
|
|
111
|
+
outputBuffer[j + 1] = (accumulated[1] << 4) | (accumulated[2] >> 2);
|
|
112
|
+
outputBuffer[j + 2] = (accumulated[2] << 6) | accumulated[3];
|
|
113
|
+
j += accumulateIndex - 1;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (outputLength)
|
|
117
|
+
{
|
|
118
|
+
*outputLength = j;
|
|
119
|
+
}
|
|
120
|
+
return outputBuffer;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
//
|
|
124
|
+
// NewBase64Decode
|
|
125
|
+
//
|
|
126
|
+
// Encodes the arbitrary data in the inputBuffer as base64 into a newly malloced
|
|
127
|
+
// output buffer.
|
|
128
|
+
//
|
|
129
|
+
// inputBuffer - the source data for the encode
|
|
130
|
+
// length - the length of the input in bytes
|
|
131
|
+
// separateLines - if zero, no CR/LF characters will be added. Otherwise
|
|
132
|
+
// a CR/LF pair will be added every 64 encoded chars.
|
|
133
|
+
// outputLength - if not-NULL, on output will contain the encoded length
|
|
134
|
+
// (not including terminating 0 char)
|
|
135
|
+
//
|
|
136
|
+
// returns the encoded buffer. Must be free'd by caller. Length is given by
|
|
137
|
+
// outputLength.
|
|
138
|
+
//
|
|
139
|
+
char *NewBase64Encode(
|
|
140
|
+
const void *buffer,
|
|
141
|
+
size_t length,
|
|
142
|
+
int separateLines,
|
|
143
|
+
size_t *outputLength)
|
|
144
|
+
{
|
|
145
|
+
const unsigned char *inputBuffer = (const unsigned char *)buffer;
|
|
146
|
+
|
|
147
|
+
#define MAX_NUM_PADDING_CHARS 2
|
|
148
|
+
#define OUTPUT_LINE_LENGTH 64
|
|
149
|
+
#define INPUT_LINE_LENGTH ((OUTPUT_LINE_LENGTH / BASE64_UNIT_SIZE) * BINARY_UNIT_SIZE)
|
|
150
|
+
#define CR_LF_SIZE 2
|
|
151
|
+
|
|
152
|
+
//
|
|
153
|
+
// Byte accurate calculation of final buffer size
|
|
154
|
+
//
|
|
155
|
+
size_t outputBufferSize =
|
|
156
|
+
((length / BINARY_UNIT_SIZE)
|
|
157
|
+
+ ((length % BINARY_UNIT_SIZE) ? 1 : 0))
|
|
158
|
+
* BASE64_UNIT_SIZE;
|
|
159
|
+
if (separateLines)
|
|
160
|
+
{
|
|
161
|
+
outputBufferSize +=
|
|
162
|
+
(outputBufferSize / OUTPUT_LINE_LENGTH) * CR_LF_SIZE;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
//
|
|
166
|
+
// Include space for a terminating zero
|
|
167
|
+
//
|
|
168
|
+
outputBufferSize += 1;
|
|
169
|
+
|
|
170
|
+
//
|
|
171
|
+
// Allocate the output buffer
|
|
172
|
+
//
|
|
173
|
+
char *outputBuffer = (char *)malloc(outputBufferSize);
|
|
174
|
+
if (!outputBuffer)
|
|
175
|
+
{
|
|
176
|
+
return NULL;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
size_t i = 0;
|
|
180
|
+
size_t j = 0;
|
|
181
|
+
const size_t lineLength = separateLines ? INPUT_LINE_LENGTH : length;
|
|
182
|
+
size_t lineEnd = lineLength;
|
|
183
|
+
|
|
184
|
+
while (TRUE)
|
|
185
|
+
{
|
|
186
|
+
if (lineEnd > length)
|
|
187
|
+
{
|
|
188
|
+
lineEnd = length;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
for (; i + BINARY_UNIT_SIZE - 1 < lineEnd; i += BINARY_UNIT_SIZE)
|
|
192
|
+
{
|
|
193
|
+
//
|
|
194
|
+
// Inner loop: turn 48 bytes into 64 base64 characters
|
|
195
|
+
//
|
|
196
|
+
outputBuffer[j++] = base64EncodeLookup[(inputBuffer[i] & 0xFC) >> 2];
|
|
197
|
+
outputBuffer[j++] = base64EncodeLookup[((inputBuffer[i] & 0x03) << 4)
|
|
198
|
+
| ((inputBuffer[i + 1] & 0xF0) >> 4)];
|
|
199
|
+
outputBuffer[j++] = base64EncodeLookup[((inputBuffer[i + 1] & 0x0F) << 2)
|
|
200
|
+
| ((inputBuffer[i + 2] & 0xC0) >> 6)];
|
|
201
|
+
outputBuffer[j++] = base64EncodeLookup[inputBuffer[i + 2] & 0x3F];
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
if (lineEnd == length)
|
|
205
|
+
{
|
|
206
|
+
break;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
//
|
|
210
|
+
// Add the newline
|
|
211
|
+
//
|
|
212
|
+
outputBuffer[j++] = '\r';
|
|
213
|
+
outputBuffer[j++] = '\n';
|
|
214
|
+
lineEnd += lineLength;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
if (i + 1 < length)
|
|
218
|
+
{
|
|
219
|
+
//
|
|
220
|
+
// Handle the single '=' case
|
|
221
|
+
//
|
|
222
|
+
outputBuffer[j++] = base64EncodeLookup[(inputBuffer[i] & 0xFC) >> 2];
|
|
223
|
+
outputBuffer[j++] = base64EncodeLookup[((inputBuffer[i] & 0x03) << 4)
|
|
224
|
+
| ((inputBuffer[i + 1] & 0xF0) >> 4)];
|
|
225
|
+
outputBuffer[j++] = base64EncodeLookup[(inputBuffer[i + 1] & 0x0F) << 2];
|
|
226
|
+
outputBuffer[j++] = '=';
|
|
227
|
+
}
|
|
228
|
+
else if (i < length)
|
|
229
|
+
{
|
|
230
|
+
//
|
|
231
|
+
// Handle the double '=' case
|
|
232
|
+
//
|
|
233
|
+
outputBuffer[j++] = base64EncodeLookup[(inputBuffer[i] & 0xFC) >> 2];
|
|
234
|
+
outputBuffer[j++] = base64EncodeLookup[(inputBuffer[i] & 0x03) << 4];
|
|
235
|
+
outputBuffer[j++] = '=';
|
|
236
|
+
outputBuffer[j++] = '=';
|
|
237
|
+
}
|
|
238
|
+
outputBuffer[j] = 0;
|
|
239
|
+
|
|
240
|
+
//
|
|
241
|
+
// Set the output length and return the buffer
|
|
242
|
+
//
|
|
243
|
+
if (outputLength)
|
|
244
|
+
{
|
|
245
|
+
*outputLength = j;
|
|
246
|
+
}
|
|
247
|
+
return outputBuffer;
|
|
248
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
#ifndef __BASE64_H__
|
|
2
|
+
#define __BASE64_H__
|
|
3
|
+
|
|
4
|
+
#include <stdio.h>
|
|
5
|
+
#include <string.h>
|
|
6
|
+
#include <stdlib.h>
|
|
7
|
+
|
|
8
|
+
#define TRUE 1
|
|
9
|
+
#define FALSE 0
|
|
10
|
+
|
|
11
|
+
void *NewBase64Decode(const char *inputBuffer, size_t length, size_t *outputLength);
|
|
12
|
+
|
|
13
|
+
char *NewBase64Encode(const void *inputBuffer, size_t length, int separateLines, size_t *outputLength);
|
|
14
|
+
|
|
15
|
+
#endif
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"""
|
|
2
|
+
PKEnc: Public Key Encryption base class for charm-crypto-lite.
|
|
3
|
+
|
|
4
|
+
This is a minimal base class for public key encryption schemes.
|
|
5
|
+
Subclass this to implement your own EC-based encryption schemes.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from charm_lite.toolbox.ecgroup import ECGroup
|
|
9
|
+
|
|
10
|
+
__all__ = ['PKEnc']
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class PKEnc:
|
|
14
|
+
"""Base class for public key encryption schemes.
|
|
15
|
+
|
|
16
|
+
This is an empty base class that can be subclassed to implement
|
|
17
|
+
EC-based public key encryption schemes on secp256k1.
|
|
18
|
+
|
|
19
|
+
Example:
|
|
20
|
+
from charm_lite.toolbox.PKEnc import PKEnc
|
|
21
|
+
from charm_lite.toolbox.ecgroup import ECGroup, ZR, G
|
|
22
|
+
from charm_lite.toolbox.eccurve import secp256k1
|
|
23
|
+
|
|
24
|
+
class MyEncryptionScheme(PKEnc):
|
|
25
|
+
def __init__(self):
|
|
26
|
+
super().__init__()
|
|
27
|
+
self.group = ECGroup(secp256k1)
|
|
28
|
+
|
|
29
|
+
def keygen(self):
|
|
30
|
+
# Generate key pair
|
|
31
|
+
x = self.group.random(ZR) # private key
|
|
32
|
+
g = self.group.generator()
|
|
33
|
+
h = g ** x # public key
|
|
34
|
+
return {'pk': h, 'sk': x}
|
|
35
|
+
|
|
36
|
+
def encrypt(self, pk, msg):
|
|
37
|
+
# Implement encryption
|
|
38
|
+
pass
|
|
39
|
+
|
|
40
|
+
def decrypt(self, sk, ct):
|
|
41
|
+
# Implement decryption
|
|
42
|
+
pass
|
|
43
|
+
"""
|
|
44
|
+
|
|
45
|
+
def __init__(self):
|
|
46
|
+
"""Initialize the PKEnc base class."""
|
|
47
|
+
pass
|
|
48
|
+
|
|
49
|
+
def keygen(self):
|
|
50
|
+
"""Generate a key pair. Override in subclass."""
|
|
51
|
+
raise NotImplementedError("keygen must be implemented by subclass")
|
|
52
|
+
|
|
53
|
+
def encrypt(self, pk, msg):
|
|
54
|
+
"""Encrypt a message. Override in subclass."""
|
|
55
|
+
raise NotImplementedError("encrypt must be implemented by subclass")
|
|
56
|
+
|
|
57
|
+
def decrypt(self, sk, ct):
|
|
58
|
+
"""Decrypt a ciphertext. Override in subclass."""
|
|
59
|
+
raise NotImplementedError("decrypt must be implemented by subclass")
|
|
60
|
+
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# charm_lite.toolbox package
|
|
2
|
+
"""
|
|
3
|
+
charm-crypto-lite toolbox module.
|
|
4
|
+
|
|
5
|
+
Provides minimal EC operations on secp256k1:
|
|
6
|
+
- ECGroup: Elliptic curve group abstraction
|
|
7
|
+
- PKEnc: Base class for public key encryption schemes
|
|
8
|
+
- secp256k1: Curve constant
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from charm_lite.toolbox.ecgroup import ECGroup, ZR, G, ec_element
|
|
12
|
+
from charm_lite.toolbox.eccurve import secp256k1
|
|
13
|
+
from charm_lite.toolbox.PKEnc import PKEnc
|
|
14
|
+
|
|
15
|
+
__all__ = ['ECGroup', 'ZR', 'G', 'ec_element', 'secp256k1', 'PKEnc']
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"""
|
|
2
|
+
secp256k1 curve constant for charm-crypto-lite.
|
|
3
|
+
|
|
4
|
+
This is the only curve supported by charm-crypto-lite.
|
|
5
|
+
For additional curves, use the full charm-crypto-framework package.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
# secp256k1: The curve used by Bitcoin and Ethereum
|
|
9
|
+
# OpenSSL NID (Numeric ID) for secp256k1
|
|
10
|
+
secp256k1 = 714
|
|
11
|
+
|
|
12
|
+
__all__ = ['secp256k1']
|