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>
13 #include "crypt_s390.h"
15 #define GHASH_BLOCK_SIZE 16
16 #define GHASH_DIGEST_SIZE 16
19 u8 key
[GHASH_BLOCK_SIZE
];
22 struct ghash_desc_ctx
{
23 u8 icv
[GHASH_BLOCK_SIZE
];
24 u8 key
[GHASH_BLOCK_SIZE
];
25 u8 buffer
[GHASH_BLOCK_SIZE
];
29 static int ghash_init(struct shash_desc
*desc
)
31 struct ghash_desc_ctx
*dctx
= shash_desc_ctx(desc
);
32 struct ghash_ctx
*ctx
= crypto_shash_ctx(desc
->tfm
);
34 memset(dctx
, 0, sizeof(*dctx
));
35 memcpy(dctx
->key
, ctx
->key
, GHASH_BLOCK_SIZE
);
40 static int ghash_setkey(struct crypto_shash
*tfm
,
41 const u8
*key
, unsigned int keylen
)
43 struct ghash_ctx
*ctx
= crypto_shash_ctx(tfm
);
45 if (keylen
!= GHASH_BLOCK_SIZE
) {
46 crypto_shash_set_flags(tfm
, CRYPTO_TFM_RES_BAD_KEY_LEN
);
50 memcpy(ctx
->key
, key
, GHASH_BLOCK_SIZE
);
55 static int ghash_update(struct shash_desc
*desc
,
56 const u8
*src
, unsigned int srclen
)
58 struct ghash_desc_ctx
*dctx
= shash_desc_ctx(desc
);
60 u8
*buf
= dctx
->buffer
;
64 u8
*pos
= buf
+ (GHASH_BLOCK_SIZE
- dctx
->bytes
);
66 n
= min(srclen
, dctx
->bytes
);
74 ret
= crypt_s390_kimd(KIMD_GHASH
, dctx
, buf
,
76 if (ret
!= GHASH_BLOCK_SIZE
)
81 n
= srclen
& ~(GHASH_BLOCK_SIZE
- 1);
83 ret
= crypt_s390_kimd(KIMD_GHASH
, dctx
, src
, n
);
91 dctx
->bytes
= GHASH_BLOCK_SIZE
- srclen
;
92 memcpy(buf
, src
, srclen
);
98 static int ghash_flush(struct ghash_desc_ctx
*dctx
)
100 u8
*buf
= dctx
->buffer
;
104 u8
*pos
= buf
+ (GHASH_BLOCK_SIZE
- dctx
->bytes
);
106 memset(pos
, 0, dctx
->bytes
);
108 ret
= crypt_s390_kimd(KIMD_GHASH
, dctx
, buf
, GHASH_BLOCK_SIZE
);
109 if (ret
!= GHASH_BLOCK_SIZE
)
118 static int ghash_final(struct shash_desc
*desc
, u8
*dst
)
120 struct ghash_desc_ctx
*dctx
= shash_desc_ctx(desc
);
123 ret
= ghash_flush(dctx
);
125 memcpy(dst
, dctx
->icv
, GHASH_BLOCK_SIZE
);
129 static struct shash_alg ghash_alg
= {
130 .digestsize
= GHASH_DIGEST_SIZE
,
132 .update
= ghash_update
,
133 .final
= ghash_final
,
134 .setkey
= ghash_setkey
,
135 .descsize
= sizeof(struct ghash_desc_ctx
),
138 .cra_driver_name
= "ghash-s390",
139 .cra_priority
= CRYPT_S390_PRIORITY
,
140 .cra_flags
= CRYPTO_ALG_TYPE_SHASH
,
141 .cra_blocksize
= GHASH_BLOCK_SIZE
,
142 .cra_ctxsize
= sizeof(struct ghash_ctx
),
143 .cra_module
= THIS_MODULE
,
147 static int __init
ghash_mod_init(void)
149 if (!crypt_s390_func_available(KIMD_GHASH
,
150 CRYPT_S390_MSA
| CRYPT_S390_MSA4
))
153 return crypto_register_shash(&ghash_alg
);
156 static void __exit
ghash_mod_exit(void)
158 crypto_unregister_shash(&ghash_alg
);
161 module_init(ghash_mod_init
);
162 module_exit(ghash_mod_exit
);
164 MODULE_ALIAS_CRYPTO("ghash");
166 MODULE_LICENSE("GPL");
167 MODULE_DESCRIPTION("GHASH Message Digest Algorithm, s390 implementation");