forked from rbowler/spinhawk
-
Notifications
You must be signed in to change notification settings - Fork 3
/
crypto.c
277 lines (232 loc) · 8.98 KB
/
crypto.c
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/* CRYPTO.C (c) Copyright Jan Jaeger, 2000-2010 */
/* Cryptographic instructions */
#include "hstdinc.h"
#define _CRYPTO_C_
#define _HENGINE_DLL_
#include "hercules.h"
#if defined(FEATURE_MESSAGE_SECURITY_ASSIST)
#include "opcode.h"
#define CRYPTO_EXTERN
#include "crypto.h"
/*----------------------------------------------------------------------------*/
/* B93E KIMD - Compute Intermediate Message Digest [RRE] */
/*----------------------------------------------------------------------------*/
DEF_INST(compute_intermediate_message_digest_r)
{
if( ARCH_DEP(compute_intermediate_message_digest) )
ARCH_DEP(compute_intermediate_message_digest) (inst, regs);
else
{
int r1, r2; /* register values */
RRE(inst, regs, r1, r2);
ARCH_DEP(program_interrupt) (regs, PGM_OPERATION_EXCEPTION);
}
}
/*----------------------------------------------------------------------------*/
/* B93F KLMD - Compute Last Message Digest [RRE] */
/*----------------------------------------------------------------------------*/
DEF_INST(compute_last_message_digest_r)
{
if( ARCH_DEP(compute_last_message_digest) )
ARCH_DEP(compute_last_message_digest) (inst, regs);
else
{
int r1, r2; /* register values */
RRE(inst, regs, r1, r2);
ARCH_DEP(program_interrupt) (regs, PGM_OPERATION_EXCEPTION);
}
}
/*----------------------------------------------------------------------------*/
/* B92E KM - Cipher Message [RRE] */
/*----------------------------------------------------------------------------*/
DEF_INST(cipher_message_r)
{
if( ARCH_DEP(cipher_message) )
ARCH_DEP(cipher_message) (inst, regs);
else
{
int r1, r2; /* register values */
RRE(inst, regs, r1, r2);
ARCH_DEP(program_interrupt) (regs, PGM_OPERATION_EXCEPTION);
}
}
/*----------------------------------------------------------------------------*/
/* B91E KMAC - Compute Message Authentication Code [RRE] */
/*----------------------------------------------------------------------------*/
DEF_INST(compute_message_authentication_code_r)
{
if( ARCH_DEP(compute_message_authentication_code) )
ARCH_DEP(compute_message_authentication_code) (inst, regs);
else
{
int r1, r2; /* register values */
RRE(inst, regs, r1, r2);
ARCH_DEP(program_interrupt) (regs, PGM_OPERATION_EXCEPTION);
}
}
/*----------------------------------------------------------------------------*/
/* B92F KMC - Cipher Message with Chaining [RRE] */
/*----------------------------------------------------------------------------*/
DEF_INST(cipher_message_with_chaining_r)
{
if( ARCH_DEP(cipher_message_with_chaining) )
ARCH_DEP(cipher_message_with_chaining) (inst, regs);
else
{
int r1, r2; /* register values */
RRE(inst, regs, r1, r2);
ARCH_DEP(program_interrupt) (regs, PGM_OPERATION_EXCEPTION);
}
}
#endif /*defined(FEATURE_MESSAGE_SECURITY_ASSIST)*/
#ifdef FEATURE_MESSAGE_SECURITY_ASSIST_EXTENSION_4
/*----------------------------------------------------------------------------*/
/* B92D KMCTR - Cipher message with counter [RRF] */
/*----------------------------------------------------------------------------*/
DEF_INST(cipher_message_with_counter_r)
{
if( ARCH_DEP(cipher_message_with_counter) )
ARCH_DEP(cipher_message_with_counter) (inst, regs);
else
{
int r1, r2, r3; /* register values */
RRF_M(inst, regs, r1, r2, r3);
ARCH_DEP(program_interrupt) (regs, PGM_OPERATION_EXCEPTION);
}
}
/*----------------------------------------------------------------------------*/
/* B92A KMF - Cipher message with cipher feedback [RRE] */
/*----------------------------------------------------------------------------*/
DEF_INST(cipher_message_with_cipher_feedback_r)
{
if( ARCH_DEP(cipher_message_with_cipher_feedback) )
ARCH_DEP(cipher_message_with_cipher_feedback) (inst, regs);
else
{
int r1, r2; /* register values */
RRE(inst, regs, r1, r2);
ARCH_DEP(program_interrupt) (regs, PGM_OPERATION_EXCEPTION);
}
}
/*----------------------------------------------------------------------------*/
/* B92B KMO - Cipher message with output feedback [RRE] */
/*----------------------------------------------------------------------------*/
DEF_INST(cipher_message_with_output_feedback_r)
{
if( ARCH_DEP(cipher_message_with_output_feedback) )
ARCH_DEP(cipher_message_with_output_feedback) (inst, regs);
else
{
int r1, r2; /* register values */
RRE(inst, regs, r1, r2);
ARCH_DEP(program_interrupt) (regs, PGM_OPERATION_EXCEPTION);
}
}
/*----------------------------------------------------------------------------*/
/* B92C PCC - Perform cryptographic computation [RRE] */
/*----------------------------------------------------------------------------*/
DEF_INST(perform_cryptographic_computation_r)
{
if( ARCH_DEP(perform_cryptographic_computation) )
ARCH_DEP(perform_cryptographic_computation) (inst, regs);
else
{
int r1, r2; /* register values */
RRE(inst, regs, r1, r2);
ARCH_DEP(program_interrupt) (regs, PGM_OPERATION_EXCEPTION);
}
}
#endif /* FEATURE_MESSAGE_SECURITY_ASSIST_EXTENSION_4 */
#ifdef FEATURE_MESSAGE_SECURITY_ASSIST_EXTENSION_3
/*----------------------------------------------------------------------------*/
/* B928 PCKMO - Perform cryptographic key management operation [RRE] */
/*----------------------------------------------------------------------------*/
DEF_INST(perform_cryptographic_key_management_operation_r)
{
if( ARCH_DEP(perform_cryptographic_key_management_operation) )
ARCH_DEP(perform_cryptographic_key_management_operation) (inst, regs);
else
{
int r1, r2; /* register values */
RRE(inst, regs, r1, r2);
ARCH_DEP(program_interrupt) (regs, PGM_OPERATION_EXCEPTION);
}
}
#ifndef __WK__
#define __WK__
/*----------------------------------------------------------------------------*/
/* Function: renew_wrapping_keys */
/* */
/* Each time a clear reset is performed, a new set of wrapping keys and their */
/* associated verification patterns are generated. The contents of the two */
/* wrapping-key registers are kept internal to the model so that no program, */
/* including the operating system, can directly observe their clear value. */
/*----------------------------------------------------------------------------*/
void renew_wrapping_keys(void)
{
int i;
BYTE lparname[8];
U64 cpuid;
BYTE byte;
obtain_lock(&sysblk.wklock);
for(i = 0; i < 0x100; i++)
srandom(random() * host_tod()); /* Randomize related to time */
for(i = 0; i < 32; i++)
sysblk.wkaes_reg[i] = random();
for(i = 0; i < 24; i++)
sysblk.wkdea_reg[i] = random();
/* We set the verification pattern to */
/* cpuid (8 bytes) */
/* lpar name (8 bytes) */
/* lparnum (1 byte) */
/* random number 8 bytes at the end) */
memset(sysblk.wkvpaes_reg, 0, 32);
memset(sysblk.wkvpdea_reg, 0, 24);
cpuid = sysblk.cpuid;
for(i = 0; i < 8; i++)
{
sysblk.wkvpaes_reg[7 - i] = cpuid;
sysblk.wkvpdea_reg[7 - i] = cpuid;
cpuid >>= 8;
}
get_lparname(lparname);
memcpy(&sysblk.wkvpaes_reg[8], lparname, 8);
memcpy(&sysblk.wkvpdea_reg[8], lparname, 8);
sysblk.wkvpaes_reg[16] = sysblk.lparnum;
sysblk.wkvpdea_reg[16] = sysblk.lparnum;
for(i = 0; i < 8; i++)
{
byte = random();
sysblk.wkvpaes_reg[31 - i] = byte;
sysblk.wkvpdea_reg[23 - i] = byte;
}
release_lock(&sysblk.wklock);
#if 0
logmsg("AES wrapping key: ");
for(i = 0; i < 32; i++)
logmsg("%02X", sysblk.wkaes_reg[i]);
logmsg("\nAES wrapping key verification pattern: ");
for(i = 0; i < 32; i++)
logmsg("%02X", sysblk.wkvpaes_reg[i]);
logmsg("\nDEA wrapping key: ");
for(i = 0; i < 24; i++)
logmsg("%02X", sysblk.wkdea_reg[i]);
logmsg("\nDEA wrapping key verification pattern: ");
for(i = 0; i < 24; i++)
logmsg("%02X", sysblk.wkvpdea_reg[i]);
logmsg("\n");
#endif
}
#endif
#endif /* FEATURE_MESSAGE_SECURITY_ASSIST_EXTENSION_3 */
#if !defined(_GEN_ARCH)
#if defined(_ARCHMODE2)
#define _GEN_ARCH _ARCHMODE2
#include "crypto.c"
#endif
#if defined(_ARCHMODE3)
#undef _GEN_ARCH
#define _GEN_ARCH _ARCHMODE3
#include "crypto.c"
#endif
#endif /*!defined(_GEN_ARCH)*/