summaryrefslogtreecommitdiff
path: root/module/pkcs.m
blob: cf164e593aece7283a25c0e9b623a16746a74185 (plain)
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
#
# Public-Key Cryptography Standards (PKCS)
#
#	Ref: 	http://www.rsa.com
#		RFC1423
#

PKCS: module {

	PATH: con "/dis/lib/crypt/pkcs.dis";

	init: fn(): string;

	# PKCS Object Identifiers

	objIdTab			: array of ASN1->Oid;

	id_pkcs,
	id_pkcs_1,
	id_pkcs_rsaEncryption,
	id_pkcs_md2WithRSAEncryption,
	id_pkcs_md4WithRSAEncryption,
	id_pkcs_md5WithRSAEncryption,
	id_pkcs_3,
	id_pkcs_dhKeyAgreement,
	id_pkcs_5,
	id_pkcs_pbeWithMD2AndDESCBC,
	id_pkcs_pbeWithMD5AndDESCBC,
	id_pkcs_7,
	id_pkcs_data,
	id_pkcs_singnedData,
	id_pkcs_envelopedData,
	id_pkcs_signedAndEnvelopedData,
	id_pkcs_digestData,
	id_pkcs_encryptedData,
	id_pkcs_9,
	id_pkcs_emailAddress,
	id_pkcs_unstructuredName,
	id_pkcs_contentType,
	id_pkcs_messageDigest,
	id_pkcs_signingTime,
	id_pkcs_countersignature,
	id_pkcs_challengePassword,
	id_pkcs_unstructuredAddress,
	id_pkcs_extCertAttrs,
	id_algorithm_shaWithDSS		: con iota;

	# PKCS1

	RSAParams: adt {
		modulus			: ref Keyring->IPint;
		exponent		: ref Keyring->IPint;
	};

	RSAKey: adt {
		modulus			: ref Keyring->IPint;
		modlen			: int;
		exponent		: ref Keyring->IPint;

		bits: fn(k: self ref RSAKey): int;
		#tostring: fn(k: self ref RSAKey): string;
	};

	MD2_WithRSAEncryption		: con 0;
	MD5_WithRSAEncryption		: con 1;	

	rsa_encrypt: fn(data: array of byte, key: ref RSAKey, blocktype: int): (string, array of byte); 
	rsa_decrypt: fn(data: array of byte, key: ref RSAKey, public: int): (string, array of byte); 
	rsa_sign: fn(data: array of byte, sk: ref RSAKey, algid: int): (string, array of byte);
	rsa_verify: fn(data, signature: array of byte, pk: ref RSAKey, algid: int): int;
	decode_rsapubkey: fn(a: array of byte): (string, ref RSAKey);

	# Note:
	#	DSS included here is only for completeness.

	DSSParams: adt {
		p			: ref Keyring->IPint;
		q			: ref Keyring->IPint;
		alpha			: ref Keyring->IPint;
	};

	DSSPublicKey: adt {
		params			: ref DSSParams;
		y			: ref Keyring->IPint;
	};

	DSSPrivateKey: adt {
		params			: ref DSSParams;
		x			: ref Keyring->IPint;
	};

	generateDSSKeyPair: fn(strength: int): (ref DSSPublicKey, ref DSSPrivateKey);
	dss_sign: fn(a: array of byte, sk: ref DSSPrivateKey): (string, array of byte);
	dss_verify: fn(a, signa: array of byte, pk: ref DSSPublicKey): int;
	decode_dsspubkey: fn(a: array of byte): (string, ref DSSPublicKey);

	# PKCS3

	DHParams: adt {
		prime			: ref Keyring->IPint; # prime (p)
		base			: ref Keyring->IPint; # generator (alpha)
		privateValueLength	: int;
	};

	DHPublicKey: adt {
		param			: ref DHParams;
		pk			: ref Keyring->IPint;
	};

	DHPrivateKey: adt {
		param			: ref DHParams;
		pk			: ref Keyring->IPint;
		sk			: ref Keyring->IPint;
	};

	generateDHParams: fn(primelen: int): ref DHParams; 
	setupDHAgreement: fn(dh: ref DHParams): (ref DHPrivateKey, ref DHPublicKey);
	computeDHAgreedKey: fn(dh: ref DHParams, mysk, upk: ref Keyring->IPint): array of byte;
	decode_dhpubkey: fn(a: array of byte): (string, ref DHPublicKey);

	# PKCS5

	PBEParams: adt {
		salt			: array of byte; # [8]
		iterationCount		: int;
	};	

	PBE_MD2_DESCBC			: con 0;
	PBE_MD5_DESCBC			: con 1;

	generateDESKey: fn(pw: array of byte, param: ref PBEParams, alg: int)
		: (ref Keyring->DESstate, array of byte, array of byte);
	pbe_encrypt: fn(state: ref Keyring->DESstate, b: array of byte): array of byte;
	pbe_decrypt: fn(state: ref Keyring->DESstate, eb: array of byte): array of byte;

	# PKCS6

	ExtCertInfo: adt {
  		version 		: int;
  		cert 			: array of byte; # der encoded x509 Certificate
  		attrs 			: list of array of byte; # attribute as array of byte 
	};

	# PKCS7
	#	See module X509

	# PKCS8

	PrivateKeyInfo: adt {		# as SEQUENCE
		version			: int; # should be 0
		privateKeyAlgorithm	: ref AlgIdentifier;
		privateKey		: array of byte; # octet string
		attrs			: list of array of byte; # [0] IMPLICIT Attributes OPTIONAL 

		encode: fn(p: self ref PrivateKeyInfo): (string, array of byte);
		decode: fn(a: array of byte): (string, ref PrivateKeyInfo);		
	};

	EncryptedPrivateKeyInfo: adt {	# as SEQUENCE
  		encryptionAlgorithm 	: ref AlgIdentifier;
  		encryptedData 		: array of byte; # octet string

		encode: fn(ep: self ref EncryptedPrivateKeyInfo): (string, array of byte);
		decode: fn(a: array of byte): (string, ref EncryptedPrivateKeyInfo);
	};

	AlgIdentifier: adt {		# TODO: move this to ASN1
		oid			: ref ASN1->Oid;
		parameter		: array of byte;
	};

	# PKCS10
	#	See module X509
};