4 * s390 implementation of the GHASH algorithm for GCM (Galois/Counter Mode).
6 * Copyright IBM Corp. 2011
7 * Author(s): Gerald Schaefer <gerald.schaefer@de.ibm.com>
10 #include <crypto/internal/hash.h>
11 #include <linux/module.h>
12 #include <linux/cpufeature.h>
14 #include "crypt_s390.h"
16 #define GHASH_BLOCK_SIZE 16
17 #define GHASH_DIGEST_SIZE 16
20 u8 key
[GHASH_BLOCK_SIZE
];
23 struct ghash_desc_ctx
{
24 u8 icv
[GHASH_BLOCK_SIZE
];
25 u8 key
[GHASH_BLOCK_SIZE
];
26 u8 buffer
[GHASH_BLOCK_SIZE
];
30 static int ghash_init(struct shash_desc
*desc
)
32 struct ghash_desc_ctx
*dctx
= shash_desc_ctx(desc
);
33 struct ghash_ctx
*ctx
= crypto_shash_ctx(desc
->tfm
);
35 memset(dctx
, 0, sizeof(*dctx
));
36 memcpy(dctx
->key
, ctx
->key
, GHASH_BLOCK_SIZE
);
41 static int ghash_setkey(struct crypto_shash
*tfm
,
42 const u8
*key
, unsigned int keylen
)
44 struct ghash_ctx
*ctx
= crypto_shash_ctx(tfm
);
46 if (keylen
!= GHASH_BLOCK_SIZE
) {
47 crypto_shash_set_flags(tfm
, CRYPTO_TFM_RES_BAD_KEY_LEN
);
51 memcpy(ctx
->key
, key
, GHASH_BLOCK_SIZE
);
56 static int ghash_update(struct shash_desc
*desc
,
57 const u8
*src
, unsigned int srclen
)
59 struct ghash_desc_ctx
*dctx
= shash_desc_ctx(desc
);
61 u8
*buf
= dctx
->buffer
;
65 u8
*pos
= buf
+ (GHASH_BLOCK_SIZE
- dctx
->bytes
);
67 n
= min(srclen
, dctx
->bytes
);
75 ret
= crypt_s390_kimd(KIMD_GHASH
, dctx
, buf
,
77 if (ret
!= GHASH_BLOCK_SIZE
)
82 n
= srclen
& ~(GHASH_BLOCK_SIZE
- 1);
84 ret
= crypt_s390_kimd(KIMD_GHASH
, dctx
, src
, n
);
92 dctx
->bytes
= GHASH_BLOCK_SIZE
- srclen
;
93 memcpy(buf
, src
, srclen
);
99 static int ghash_flush(struct ghash_desc_ctx
*dctx
)
101 u8
*buf
= dctx
->buffer
;
105 u8
*pos
= buf
+ (GHASH_BLOCK_SIZE
- dctx
->bytes
);
107 memset(pos
, 0, dctx
->bytes
);
109 ret
= crypt_s390_kimd(KIMD_GHASH
, dctx
, buf
, GHASH_BLOCK_SIZE
);
110 if (ret
!= GHASH_BLOCK_SIZE
)
119 static int ghash_final(struct shash_desc
*desc
, u8
*dst
)
121 struct ghash_desc_ctx
*dctx
= shash_desc_ctx(desc
);
124 ret
= ghash_flush(dctx
);
126 memcpy(dst
, dctx
->icv
, GHASH_BLOCK_SIZE
);
130 static struct shash_alg ghash_alg
= {
131 .digestsize
= GHASH_DIGEST_SIZE
,
133 .update
= ghash_update
,
134 .final
= ghash_final
,
135 .setkey
= ghash_setkey
,
136 .descsize
= sizeof(struct ghash_desc_ctx
),
139 .cra_driver_name
= "ghash-s390",
140 .cra_priority
= CRYPT_S390_PRIORITY
,
141 .cra_flags
= CRYPTO_ALG_TYPE_SHASH
,
142 .cra_blocksize
= GHASH_BLOCK_SIZE
,
143 .cra_ctxsize
= sizeof(struct ghash_ctx
),
144 .cra_module
= THIS_MODULE
,
148 static int __init
ghash_mod_init(void)
150 if (!crypt_s390_func_available(KIMD_GHASH
,
151 CRYPT_S390_MSA
| CRYPT_S390_MSA4
))
154 return crypto_register_shash(&ghash_alg
);
157 static void __exit
ghash_mod_exit(void)
159 crypto_unregister_shash(&ghash_alg
);
162 module_cpu_feature_match(MSA
, ghash_mod_init
);
163 module_exit(ghash_mod_exit
);
165 MODULE_ALIAS_CRYPTO("ghash");
167 MODULE_LICENSE("GPL");
168 MODULE_DESCRIPTION("GHASH Message Digest Algorithm, s390 implementation");