Skip to content

Commit 227a919

Browse files
hfreudeVasily Gorbik
authored andcommitted
s390/zcrypt: Rework MKVP fields and handling
In general all MKVPs (Master Key Verification Pattern) are binary data - usually some kind of shortened hash value e.g. sha256. Some code parts however used some u64 type which made compares a little bit easier. Anyway this is binary data and so all fields related to MKVP are now u8[] and function parameters use (const) u8 * now. The sysfs emit for the MKVPs also has been adapted to first format the MKVP as hex string into a buffer and then use %s with sysfs_emit_at() to generate the sysfs output. The patch also include a simple whitespace fix. Signed-off-by: Harald Freudenberger <freude@linux.ibm.com> Acked-by: Heiko Carstens <hca@linux.ibm.com> Signed-off-by: Vasily Gorbik <gor@linux.ibm.com>
1 parent ecd2fd1 commit 227a919

4 files changed

Lines changed: 123 additions & 85 deletions

File tree

drivers/s390/crypto/pkey_cca.c

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -87,50 +87,52 @@ static int cca_apqns4key(const u8 *key, u32 keylen, u32 flags,
8787
zcrypt_wait_api_operational();
8888

8989
if (hdr->type == TOKTYPE_CCA_INTERNAL) {
90-
u64 cur_mkvp = 0, old_mkvp = 0;
90+
const u8 *ptr_cur_mkvp = NULL;
91+
const u8 *ptr_old_mkvp = NULL;
9192
int minhwtype = ZCRYPT_CEX3C;
9293

9394
if (hdr->version == TOKVER_CCA_AES) {
9495
struct secaeskeytoken *t = (struct secaeskeytoken *)key;
9596

9697
if (flags & PKEY_FLAGS_MATCH_CUR_MKVP)
97-
cur_mkvp = t->mkvp;
98+
ptr_cur_mkvp = t->mkvp;
9899
if (flags & PKEY_FLAGS_MATCH_ALT_MKVP)
99-
old_mkvp = t->mkvp;
100+
ptr_old_mkvp = t->mkvp;
100101
} else if (hdr->version == TOKVER_CCA_VLSC) {
101102
struct cipherkeytoken *t = (struct cipherkeytoken *)key;
102103

103104
minhwtype = ZCRYPT_CEX6;
104105
if (flags & PKEY_FLAGS_MATCH_CUR_MKVP)
105-
cur_mkvp = t->mkvp0;
106+
ptr_cur_mkvp = t->mkvp0;
106107
if (flags & PKEY_FLAGS_MATCH_ALT_MKVP)
107-
old_mkvp = t->mkvp0;
108+
ptr_old_mkvp = t->mkvp0;
108109
} else {
109110
/* unknown CCA internal token type */
110111
return -EINVAL;
111112
}
112113
rc = cca_findcard2(_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
113114
minhwtype, AES_MK_SET,
114-
cur_mkvp, old_mkvp, xflags);
115+
ptr_cur_mkvp, ptr_old_mkvp, xflags);
115116
if (rc)
116117
goto out;
117118

118119
} else if (hdr->type == TOKTYPE_CCA_INTERNAL_PKA) {
119120
struct eccprivkeytoken *t = (struct eccprivkeytoken *)key;
120-
u64 cur_mkvp = 0, old_mkvp = 0;
121+
const u8 *ptr_cur_mkvp = NULL;
122+
const u8 *ptr_old_mkvp = NULL;
121123

122124
if (t->secid == 0x20) {
123125
if (flags & PKEY_FLAGS_MATCH_CUR_MKVP)
124-
cur_mkvp = t->mkvp;
126+
ptr_cur_mkvp = t->mkvp;
125127
if (flags & PKEY_FLAGS_MATCH_ALT_MKVP)
126-
old_mkvp = t->mkvp;
128+
ptr_old_mkvp = t->mkvp;
127129
} else {
128130
/* unknown CCA internal 2 token type */
129131
return -EINVAL;
130132
}
131133
rc = cca_findcard2(_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
132134
ZCRYPT_CEX7, APKA_MK_SET,
133-
cur_mkvp, old_mkvp, xflags);
135+
ptr_cur_mkvp, ptr_old_mkvp, xflags);
134136
if (rc)
135137
goto out;
136138

@@ -167,31 +169,33 @@ static int cca_apqns4type(enum pkey_key_type ktype,
167169
zcrypt_wait_api_operational();
168170

169171
if (ktype == PKEY_TYPE_CCA_DATA || ktype == PKEY_TYPE_CCA_CIPHER) {
170-
u64 cur_mkvp = 0, old_mkvp = 0;
172+
const u8 *ptr_cur_mkvp = NULL;
173+
const u8 *ptr_old_mkvp = NULL;
171174
int minhwtype = ZCRYPT_CEX3C;
172175

173176
if (flags & PKEY_FLAGS_MATCH_CUR_MKVP)
174-
cur_mkvp = *((u64 *)cur_mkvp);
177+
ptr_cur_mkvp = cur_mkvp;
175178
if (flags & PKEY_FLAGS_MATCH_ALT_MKVP)
176-
old_mkvp = *((u64 *)alt_mkvp);
179+
ptr_old_mkvp = alt_mkvp;
177180
if (ktype == PKEY_TYPE_CCA_CIPHER)
178181
minhwtype = ZCRYPT_CEX6;
179182
rc = cca_findcard2(_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
180183
minhwtype, AES_MK_SET,
181-
cur_mkvp, old_mkvp, xflags);
184+
ptr_cur_mkvp, ptr_old_mkvp, xflags);
182185
if (rc)
183186
goto out;
184187

185188
} else if (ktype == PKEY_TYPE_CCA_ECC) {
186-
u64 cur_mkvp = 0, old_mkvp = 0;
189+
const u8 *ptr_cur_mkvp = NULL;
190+
const u8 *ptr_old_mkvp = NULL;
187191

188192
if (flags & PKEY_FLAGS_MATCH_CUR_MKVP)
189-
cur_mkvp = *((u64 *)cur_mkvp);
193+
ptr_cur_mkvp = cur_mkvp;
190194
if (flags & PKEY_FLAGS_MATCH_ALT_MKVP)
191-
old_mkvp = *((u64 *)alt_mkvp);
195+
ptr_old_mkvp = alt_mkvp;
192196
rc = cca_findcard2(_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
193197
ZCRYPT_CEX7, APKA_MK_SET,
194-
cur_mkvp, old_mkvp, xflags);
198+
ptr_cur_mkvp, ptr_old_mkvp, xflags);
195199
if (rc)
196200
goto out;
197201

@@ -487,14 +491,14 @@ static int cca_verifykey(const u8 *key, u32 keylen,
487491
*keybitsize = t->bitsize;
488492
rc = cca_findcard2(apqns, &nr_apqns, *card, *dom,
489493
ZCRYPT_CEX3C, AES_MK_SET,
490-
t->mkvp, 0, xflags);
494+
t->mkvp, NULL, xflags);
491495
if (!rc)
492496
*flags = PKEY_FLAGS_MATCH_CUR_MKVP;
493497
if (rc == -ENODEV) {
494498
nr_apqns = ARRAY_SIZE(apqns);
495499
rc = cca_findcard2(apqns, &nr_apqns, *card, *dom,
496500
ZCRYPT_CEX3C, AES_MK_SET,
497-
0, t->mkvp, xflags);
501+
NULL, t->mkvp, xflags);
498502
if (!rc)
499503
*flags = PKEY_FLAGS_MATCH_ALT_MKVP;
500504
}
@@ -521,14 +525,14 @@ static int cca_verifykey(const u8 *key, u32 keylen,
521525
*keybitsize = PKEY_SIZE_AES_256;
522526
rc = cca_findcard2(apqns, &nr_apqns, *card, *dom,
523527
ZCRYPT_CEX6, AES_MK_SET,
524-
t->mkvp0, 0, xflags);
528+
t->mkvp0, NULL, xflags);
525529
if (!rc)
526530
*flags = PKEY_FLAGS_MATCH_CUR_MKVP;
527531
if (rc == -ENODEV) {
528532
nr_apqns = ARRAY_SIZE(apqns);
529533
rc = cca_findcard2(apqns, &nr_apqns, *card, *dom,
530534
ZCRYPT_CEX6, AES_MK_SET,
531-
0, t->mkvp0, xflags);
535+
NULL, t->mkvp0, xflags);
532536
if (!rc)
533537
*flags = PKEY_FLAGS_MATCH_ALT_MKVP;
534538
}

drivers/s390/crypto/zcrypt_ccamisc.c

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1708,8 +1708,8 @@ int cca_get_info(u16 cardnr, u16 domain, struct cca_info *ci, u32 xflags)
17081708
EXPORT_SYMBOL(cca_get_info);
17091709

17101710
int cca_findcard2(u32 *apqns, u32 *nr_apqns, u16 cardnr, u16 domain,
1711-
int minhwtype, int mktype, u64 cur_mkvp, u64 old_mkvp,
1712-
u32 xflags)
1711+
int minhwtype, int mktype,
1712+
const u8 *ptr_cur_mkvp, const u8 *ptr_old_mkvp, u32 xflags)
17131713
{
17141714
struct zcrypt_device_status_ext *device_status;
17151715
int i, card, dom, curmatch, oldmatch;
@@ -1753,20 +1753,28 @@ int cca_findcard2(u32 *apqns, u32 *nr_apqns, u16 cardnr, u16 domain,
17531753
/* check min hardware type */
17541754
if (minhwtype > 0 && minhwtype > ci.hwtype)
17551755
continue;
1756-
if (cur_mkvp || old_mkvp) {
1756+
if (ptr_cur_mkvp || ptr_old_mkvp) {
17571757
/* check mkvps */
17581758
curmatch = oldmatch = 0;
17591759
if (mktype == AES_MK_SET) {
1760-
if (cur_mkvp && cur_mkvp == ci.cur_aes_mkvp)
1760+
if (ptr_cur_mkvp &&
1761+
!memcmp(ptr_cur_mkvp, ci.cur_aes_mkvp,
1762+
sizeof(ci.cur_aes_mkvp)))
17611763
curmatch = 1;
1762-
if (old_mkvp && ci.old_aes_mk_state == '2' &&
1763-
old_mkvp == ci.old_aes_mkvp)
1764+
if (ptr_old_mkvp &&
1765+
ci.old_aes_mk_state == '2' &&
1766+
!memcmp(ptr_old_mkvp, ci.old_aes_mkvp,
1767+
sizeof(ci.old_aes_mkvp)))
17641768
oldmatch = 1;
17651769
} else {
1766-
if (cur_mkvp && cur_mkvp == ci.cur_apka_mkvp)
1770+
if (ptr_cur_mkvp &&
1771+
!memcmp(ptr_cur_mkvp, ci.cur_apka_mkvp,
1772+
sizeof(ci.cur_apka_mkvp)))
17671773
curmatch = 1;
1768-
if (old_mkvp && ci.old_apka_mk_state == '2' &&
1769-
old_mkvp == ci.old_apka_mkvp)
1774+
if (ptr_old_mkvp &&
1775+
ci.old_apka_mk_state == '2' &&
1776+
!memcmp(ptr_old_mkvp, ci.old_apka_mkvp,
1777+
sizeof(ci.old_apka_mkvp)))
17701778
oldmatch = 1;
17711779
}
17721780
if (curmatch + oldmatch < 1)

drivers/s390/crypto/zcrypt_ccamisc.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ struct secaeskeytoken {
4747
u8 res1[1];
4848
u8 flag; /* key flags */
4949
u8 res2[1];
50-
u64 mkvp; /* master key verification pattern */
50+
u8 mkvp[8]; /* master key verification pattern */
5151
u8 key[32]; /* key value (encrypted) */
5252
u8 cv[8]; /* control vector */
5353
u16 bitsize; /* key bit size */
@@ -64,8 +64,8 @@ struct cipherkeytoken {
6464
u8 res1[3];
6565
u8 kms; /* key material state, 0x03 means wrapped with MK */
6666
u8 kvpt; /* key verification pattern type, should be 0x01 */
67-
u64 mkvp0; /* master key verification pattern, lo part */
68-
u64 mkvp1; /* master key verification pattern, hi part (unused) */
67+
u8 mkvp0[8]; /* master key verification pattern, lo part */
68+
u8 mkvp1[8]; /* master key verification pattern, hi part (unused) */
6969
u8 eskwm; /* encrypted section key wrapping method */
7070
u8 hashalg; /* hash algorithmus used for wrapping key */
7171
u8 plfver; /* pay load format version */
@@ -113,7 +113,7 @@ struct eccprivkeytoken {
113113
u8 ksrc; /* key source */
114114
u16 pbitlen; /* length of prime p in bits */
115115
u16 ibmadlen; /* IBM associated data length in bytes */
116-
u64 mkvp; /* master key verification pattern */
116+
u8 mkvp[8]; /* master key verification pattern */
117117
u8 opk[48]; /* encrypted object protection key data */
118118
u16 adatalen; /* associated data length in bytes */
119119
u16 fseclen; /* formatted section length in bytes */
@@ -227,8 +227,8 @@ int cca_query_crypto_facility(u16 cardnr, u16 domain,
227227
* If no apqn meeting the criteria is found, -ENODEV is returned.
228228
*/
229229
int cca_findcard2(u32 *apqns, u32 *nr_apqns, u16 cardnr, u16 domain,
230-
int minhwtype, int mktype, u64 cur_mkvp, u64 old_mkvp,
231-
u32 xflags);
230+
int minhwtype, int mktype,
231+
const u8 *cur_mkvp, const u8 *old_mkvp, u32 xflags);
232232

233233
#define AES_MK_SET 0
234234
#define APKA_MK_SET 1
@@ -245,12 +245,12 @@ struct cca_info {
245245
char new_asym_mk_state; /* '1' empty, '2' partially full, '3' full */
246246
char cur_asym_mk_state; /* '1' invalid, '2' valid */
247247
char old_asym_mk_state; /* '1' invalid, '2' valid */
248-
u64 new_aes_mkvp; /* truncated sha256 of new aes master key */
249-
u64 cur_aes_mkvp; /* truncated sha256 of current aes master key */
250-
u64 old_aes_mkvp; /* truncated sha256 of old aes master key */
251-
u64 new_apka_mkvp; /* truncated sha256 of new apka master key */
252-
u64 cur_apka_mkvp; /* truncated sha256 of current apka mk */
253-
u64 old_apka_mkvp; /* truncated sha256 of old apka mk */
248+
u8 new_aes_mkvp[8]; /* truncated sha256 of new aes master key */
249+
u8 cur_aes_mkvp[8]; /* truncated sha256 of current aes master key */
250+
u8 old_aes_mkvp[8]; /* truncated sha256 of old aes master key */
251+
u8 new_apka_mkvp[8]; /* truncated sha256 of new apka master key */
252+
u8 cur_apka_mkvp[8]; /* truncated sha256 of current apka mk */
253+
u8 old_apka_mkvp[8]; /* truncated sha256 of old apka mk */
254254
u8 new_asym_mkvp[16]; /* verify pattern of new asym master key */
255255
u8 cur_asym_mkvp[16]; /* verify pattern of current asym master key */
256256
u8 old_asym_mkvp[16]; /* verify pattern of old asym master key */

0 commit comments

Comments
 (0)