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
|
#
# basic cryptography routines implemented in C
#
Crypt: module
{
PATH: con "$Crypt";
# state held while creating digests
DigestState: adt
{
x: int; # dummy for C compiler for runt.h
# all the state is hidden
copy: fn(d: self ref DigestState): ref DigestState;
};
# expanded AES key + state for chaining
AESstate: adt
{
x: int; # dummy for C compiler for runt.h
# all the state is hidden
};
# expanded DES key + state for chaining
DESstate: adt
{
x: int; # dummy for C compiler for runt.h
# all the state is hidden
};
# expanded IDEA key + state for chaining
IDEAstate: adt
{
x: int; # dummy for C compiler for runt.h
# all the state is hidden
};
# expanded RC4 key + encryption state
RC4state: adt
{
x: int; # dummy for C compiler for runt.h
# all the state is hidden
};
# expanded Blowfish key + state for chaining
BFstate: adt
{
x: int; # dummy for C compiler for runt.h
# all the state is hidden
};
# digests
sha1: fn(buf: array of byte, n: int, digest: array of byte, state: ref DigestState):
ref DigestState;
sha224: fn(buf: array of byte, n: int, digest: array of byte, state: ref DigestState):
ref DigestState;
sha256: fn(buf: array of byte, n: int, digest: array of byte, state: ref DigestState):
ref DigestState;
sha384: fn(buf: array of byte, n: int, digest: array of byte, state: ref DigestState):
ref DigestState;
sha512: fn(buf: array of byte, n: int, digest: array of byte, state: ref DigestState):
ref DigestState;
md4: fn(buf: array of byte, n: int, digest: array of byte, state: ref DigestState):
ref DigestState;
md5: fn(buf: array of byte, n: int, digest: array of byte, state: ref DigestState):
ref DigestState;
hmac_sha1: fn(data: array of byte, n: int, key: array of byte, digest: array of byte, state: ref DigestState):
ref DigestState;
hmac_md5: fn(data: array of byte, n: int, key: array of byte, digest: array of byte, state: ref DigestState):
ref DigestState;
SHA1dlen: con 20;
SHA224dlen: con 28;
SHA256dlen: con 32;
SHA384dlen: con 48;
SHA512dlen: con 64;
MD5dlen: con 16;
MD4dlen: con 16;
# encryption interfaces
Encrypt: con 0;
Decrypt: con 1;
AESbsize: con 16;
aessetup: fn(key: array of byte, ivec: array of byte): ref AESstate;
aescbc: fn(state: ref AESstate, buf: array of byte, n: int, direction: int);
DESbsize: con 8;
dessetup: fn(key: array of byte, ivec: array of byte): ref DESstate;
desecb: fn(state: ref DESstate, buf: array of byte, n: int, direction: int);
descbc: fn(state: ref DESstate, buf: array of byte, n: int, direction: int);
IDEAbsize: con 8;
ideasetup: fn(key: array of byte, ivec: array of byte): ref IDEAstate;
ideaecb: fn(state: ref IDEAstate, buf: array of byte, n: int, direction: int);
ideacbc: fn(state: ref IDEAstate, buf: array of byte, n: int, direction: int);
BFbsize: con 8;
blowfishsetup: fn(key: array of byte, ivec: array of byte): ref BFstate;
# blowfishecb: fn(state: ref BFstate, buf: array of byte, n: int, direction: int);
blowfishcbc: fn(state: ref BFstate, buf: array of byte, n: int, direction: int);
rc4setup: fn(seed: array of byte): ref RC4state;
rc4: fn(state: ref RC4state, buf: array of byte, n: int);
rc4skip: fn(state: ref RC4state, n: int);
rc4back: fn(state: ref RC4state, n: int);
# create an alpha and p for diffie helman exchanges
dhparams: fn(nbits: int): (ref IPints->IPint, ref IPints->IPint);
# public key
PK: adt
{
pick {
RSA =>
n: ref IPints->IPint; # modulus
ek: ref IPints->IPint; # exp (encryption key)
Elgamal =>
p: ref IPints->IPint; # modulus
alpha: ref IPints->IPint; # generator
key: ref IPints->IPint; # encryption key (alpha**secret mod p)
DSA =>
p: ref IPints->IPint; # modulus
q: ref IPints->IPint; # group order, q divides p-1
alpha: ref IPints->IPint; # group generator
key: ref IPints->IPint; # encryption key (alpha**secret mod p)
}
};
# secret key (private/public key pair)
SK: adt
{
pick {
RSA =>
pk: ref PK.RSA;
dk: ref IPints->IPint; # exp (decryption key)
p: ref IPints->IPint; # q in pkcs
q: ref IPints->IPint; # p in pkcs
# precomputed crt values
kp: ref IPints->IPint; # k mod p-1
kq: ref IPints->IPint; # k mod q-1
c2: ref IPints->IPint; # for converting residues to number
Elgamal =>
pk: ref PK.Elgamal;
secret: ref IPints->IPint; # decryption key
DSA =>
pk: ref PK.DSA;
secret: ref IPints->IPint; # decryption key
}
};
# public key signature
PKsig: adt
{
# could just have list or array of ref IPints->IPint
pick {
RSA =>
n: ref IPints->IPint;
Elgamal =>
r: ref IPints->IPint;
s: ref IPints->IPint;
DSA =>
r: ref IPints->IPint;
s: ref IPints->IPint;
}
};
# RSA keys
rsagen: fn(nlen: int, elen: int, nrep: int): ref SK.RSA;
rsafill: fn(n: ref IPints->IPint, ek: ref IPints->IPint, dk: ref IPints->IPint, p: ref IPints->IPint, q: ref IPints->IPint): ref SK.RSA;
rsadecrypt: fn(k: ref SK.RSA, m: ref IPints->IPint): ref IPints->IPint;
rsaencrypt: fn(k: ref PK.RSA, m: ref IPints->IPint): ref IPints->IPint;
# Elgamal
eggen: fn(nlen: int, nrep: int): ref SK.Elgamal;
# DSA
dsagen: fn(oldpk: ref PK.DSA): ref SK.DSA;
# generic signature functions
genSK: fn(algname: string, length: int): ref SK;
genSKfromPK: fn(pk: ref PK): ref SK;
sign: fn(sk: ref SK, m: ref IPints->IPint): ref PKsig;
verify: fn(pk: ref PK, sig: ref PKsig, m: ref IPints->IPint): int;
sktopk: fn(sk: ref SK): ref PK;
};
|