-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto.red
243 lines (200 loc) · 5.37 KB
/
crypto.red
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
Red/System []
comment {
用 Red/System 对系统加解密库的封装。各平台使用的底层库如下:
* Windows:CNG
* Linux:libcrypto (OpenSSL)
* macOS: libcrypto (OpenSSL) installed by homebrew
由于只是对底层库的封装,而且CNG和libcrypto的API大同小异。所以R/S的API应
接近其中一个底层库。这样在其中一个平台可以很容易实现。平台的API有差别时,
选择容易使用的一个。比如Symmetric Algorithms的API,选择了接近libcrypto的
设计。
}
#enum crypt-algorithm! [
AES
DES
_3DES
RC4
]
#enum block-chaining! [
ECB
CBC
;CFB8
;CFB64
;CFB128
GCM
CCM
;GMAC
;CMAC
]
cipher: context [
verbose: 0
#define DPrint(msg) [#if debug? = yes [if verbose > 0 [print-line msg]]]
#either OS = 'Windows [
#import [
]
;== Symmetric Algorithms
make: func [return: [handle!]][]
reset: func [hCrypt [handle!] return: [integer!]][]
free: func [hCrypt [handle!]][]
encypt-init: func [
hCrypt [handle!]
alg [crypt-algorithm!]
IV [byte-ptr!] ;-- initialization vector
key [byte-ptr!]
key-len [uint!] ;-- AES: 128, 192 or 256, DES: 56, 3DES: 56, 112, 168
chain-mode [block-chaining!] ;-- CBC, ECB, GCM, etc
return: [integer!] ;-- error code
][
DPrint("encypt-init")
]
encypt-update: func [
hCrypt [handle!] ;-- handle returned by make
data [byte-ptr!]
data-len [uint!]
outbuf [byte-ptr!]
outlen [int-ptr!]
return: [integer!] ;-- error code
][]
encrypt-final: func [
hCrypt [handle!] ;-- handle returned by make
outbuf [byte-ptr!]
outlen [int-ptr!]
return: [integer!]
][]
decrypt-init: func [
hCrypt [handle!]
alg [crypt-algorithm!]
IV [byte-ptr!] ;-- initialization vector
key [byte-ptr!]
key-len [uint!] ;-- AES: 128, 192 or 256, DES: 56, 3DES: 56, 112, 168
chain-mode [block-chaining!] ;-- CBC, ECB, GCM, etc
return: [integer!] ;-- error code
][]
decrypt-update: func [
hCrypt [handle!] ;-- handle returned by make
data [byte-ptr!]
data-len [uint!]
outbuf [byte-ptr!]
outlen [int-ptr!]
return: [integer!] ;-- error code
][]
decrypt-final: func [
hCrypt [handle!] ;-- handle returned by make
outbuf [byte-ptr!]
outlen [int-ptr!]
return: [integer!]
][]
set-property: func [ ;-- set IV length, tag length, etc.
hCrypt [handle!] ;-- handle returned by make
property [integer!]
input [byte-ptr!]
input-len [integer!]
return: [integer!]
]
get-property: func [
hCrypt [handle!] ;-- handle returned by make
property [integer!]
output [byte-ptr!]
output-len [int-ptr!]
return: [integer!]
]
;== Asymmetric Algorithms
rsa-make:
;== Key Exchange Algorithms
;-- ECDH, ECDSA, etc
;== Hashing Algorithms
;-- SHA, MD5, etc
][ ;@@ #either Linux & macOS use OpenSSL
#switch OS [
macOS [
#define LIBCRYPTO-file "libcrypto.dylib"
]
FreeBSD [
#define LIBCRYPTO-file "libcrypto.so.8"
]
#default [
#switch config-name [
RPi [#define LIBCRYPTO-file "libcrypto.so.1.1"]
Linux-ARM [#define LIBCRYPTO-file "libcrypto.so.1.1"]
#default [#define LIBCRYPTO-file "libcrypto.so.1.0.2"]
]
]
]
#import [
LIBCRYPTO-file cdecl [
]
]
;== Symmetric Algorithms
make: func [return: [handle!]][]
reset: func [hCrypt [handle!] return: [integer!]][]
free: func [hCrypt [handle!]][]
encypt-init: func [
hCrypt [handle!]
alg [crypt-algorithm!]
IV [byte-ptr!] ;-- initialization vector
key [byte-ptr!]
key-len [uint!] ;-- AES: 128, 192 or 256, DES: 56, 3DES: 56, 112, 168
chain-mode [block-chaining!] ;-- CBC, ECB, GCM, etc
return: [integer!] ;-- error code
][
DPrint("encypt-init")
]
encypt-update: func [
hCrypt [handle!] ;-- handle returned by make
data [byte-ptr!]
data-len [uint!]
outbuf [byte-ptr!]
outlen [int-ptr!]
return: [integer!] ;-- error code
][]
encrypt-final: func [
hCrypt [handle!] ;-- handle returned by make
outbuf [byte-ptr!]
outlen [int-ptr!]
return: [integer!]
][]
decrypt-init: func [
hCrypt [handle!]
alg [crypt-algorithm!]
IV [byte-ptr!] ;-- initialization vector
key [byte-ptr!]
key-len [uint!] ;-- AES: 128, 192 or 256, DES: 56, 3DES: 56, 112, 168
chain-mode [block-chaining!] ;-- CBC, ECB, GCM, etc
return: [integer!] ;-- error code
][]
decrypt-update: func [
hCrypt [handle!] ;-- handle returned by make
data [byte-ptr!]
data-len [uint!]
outbuf [byte-ptr!]
outlen [int-ptr!]
return: [integer!] ;-- error code
][]
decrypt-final: func [
hCrypt [handle!] ;-- handle returned by make
outbuf [byte-ptr!]
outlen [int-ptr!]
return: [integer!]
][]
set-property: func [ ;-- set IV length, tag length, etc.
hCrypt [handle!] ;-- handle returned by make
property [integer!]
input [byte-ptr!]
input-len [integer!]
return: [integer!]
]
get-property: func [
hCrypt [handle!] ;-- handle returned by make
property [integer!]
output [byte-ptr!]
output-len [int-ptr!]
return: [integer!]
]
;== Asymmetric Algorithms
;-- RSA
;-- DSA
;== Key Exchange Algorithms
;-- ECDH, ECDSA, etc
;== Hashing Algorithms
;-- SHA, MD5, etc
]]