Skip to content

Commit 90446f2

Browse files
ifranzkip-steuer
authored andcommitted
Add support for SHA-512/224 and SHA-512/256
Signed-off-by: Ingo Franzki <[email protected]>
1 parent 56cad1a commit 90446f2

File tree

9 files changed

+364
-3
lines changed

9 files changed

+364
-3
lines changed

include/ica_api.h

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ typedef ica_adapter_handle_t ICA_ADAPTER_HANDLE;
125125
#define RSA_KEY_GEN_ME 92
126126
#define RSA_KEY_GEN_CRT 93
127127
#define SHA512_DRNG 94
128+
#define SHA512_224 95
129+
#define SHA512_256 96
128130

129131
/*
130132
* Key length for DES/3DES encryption/decryption
@@ -156,6 +158,8 @@ typedef ica_adapter_handle_t ICA_ADAPTER_HANDLE;
156158
#define SHA256_HASH_LENGTH 32
157159
#define SHA384_HASH_LENGTH 48
158160
#define SHA512_HASH_LENGTH 64
161+
#define SHA512_224_HASH_LENGTH SHA224_HASH_LENGTH
162+
#define SHA512_256_HASH_LENGTH SHA256_HASH_LENGTH
159163
#define SHA3_224_HASH_LENGTH SHA224_HASH_LENGTH
160164
#define SHA3_256_HASH_LENGTH SHA256_HASH_LENGTH
161165
#define SHA3_384_HASH_LENGTH SHA384_HASH_LENGTH
@@ -715,6 +719,93 @@ unsigned int ica_sha512(unsigned int message_part,
715719
sha512_context_t *sha512_context,
716720
unsigned char *output_data);
717721

722+
/**
723+
* Perform secure hash on input data using the SHA-512/224 algorithm.
724+
*
725+
* Required HW Support
726+
* KIMD-SHA-512, or KLMD-SHA-512
727+
*
728+
* @param message_part
729+
* The message chaining state. Must be one of the following:
730+
* SHA_MSG_PART_ONLY - A single hash operation
731+
* SHA_MSG_PART_FIRST - The first part
732+
* SHA_MSG_PART_MIDDLE - The middle part
733+
* SHA_MSG_PART_FINAL - The last part
734+
* @param input_length
735+
* The byte length of the input data to be SHA-512/224 hashed and must be greater
736+
* than zero.
737+
* Note: For SHA_MSG_PART_FIRST and SHA_MSG_PART_MIDDLE calls, the byte length
738+
* must be a multiple of 128 i.e., SHA-512 block size.
739+
* @param input_data
740+
* Pointer to the input data.
741+
* @param sha512_context
742+
* Pointer to the SHA-512 context structure used to store intermediate values
743+
* needed when chaining is used. The contents are ignored for message part
744+
* SHA_MSG_PART_ONLY and SHA_MSG_PART_FIRST. This structure must
745+
* contain the returned value of the preceding call to ica_sha512_224 for message
746+
* part SHA_MSG_PART_MIDDLE and SHA_MSG_PART_FINAL. For message part
747+
* SHA_MSG_PART_FIRST and SHA_MSG_PART_FINAL, the returned value can
748+
* be used for a chained call of ica_sha512_224. Therefore, the application must
749+
* not modify the contents of this structure in between chained calls.
750+
* @param output_data
751+
* Pointer to the buffer to contain the resulting hash data. The resulting
752+
* output data will have a length of SHA512_224_HASH_LENGTH. Make sure buffer has
753+
* at least this size.
754+
*
755+
* @return 0 if successful.
756+
* EINVAL if at least one invalid parameter is given
757+
* EIO if the operation fails. This should never happen.
758+
*/
759+
ICA_EXPORT
760+
unsigned int ica_sha512_224(unsigned int message_part,
761+
uint64_t input_length,
762+
unsigned char *input_data,
763+
sha512_context_t *sha512_context,
764+
unsigned char *output_data);
765+
766+
/**
767+
* Perform secure hash on input data using the SHA-512/256 algorithm.
768+
*
769+
* Required HW Support
770+
* KIMD-SHA-512, or KLMD-SHA-512
771+
*
772+
* @param message_part
773+
* The message chaining state. Must be one of the following:
774+
* SHA_MSG_PART_ONLY - A single hash operation
775+
* SHA_MSG_PART_FIRST - The first part
776+
* SHA_MSG_PART_MIDDLE - The middle part
777+
* SHA_MSG_PART_FINAL - The last part
778+
* @param input_length
779+
* The byte length of the input data to be SHA-512/256 hashed and must be greater
780+
* than zero.
781+
* Note: For SHA_MSG_PART_FIRST and SHA_MSG_PART_MIDDLE calls, the byte length
782+
* must be a multiple of 128 i.e., SHA-512 block size.
783+
* @param input_data
784+
* Pointer to the input data.
785+
* @param sha512_context
786+
* Pointer to the SHA-512 context structure used to store intermediate values
787+
* needed when chaining is used. The contents are ignored for message part
788+
* SHA_MSG_PART_ONLY and SHA_MSG_PART_FIRST. This structure must
789+
* contain the returned value of the preceding call to ica_sha512_256 for message
790+
* part SHA_MSG_PART_MIDDLE and SHA_MSG_PART_FINAL. For message part
791+
* SHA_MSG_PART_FIRST and SHA_MSG_PART_FINAL, the returned value can
792+
* be used for a chained call of ica_sha512_256. Therefore, the application must
793+
* not modify the contents of this structure in between chained calls.
794+
* @param output_data
795+
* Pointer to the buffer to contain the resulting hash data. The resulting
796+
* output data will have a length of SHA512_256_HASH_LENGTH. Make sure buffer has
797+
* at least this size.
798+
*
799+
* @return 0 if successful.
800+
* EINVAL if at least one invalid parameter is given
801+
* EIO if the operation fails. This should never happen.
802+
*/ICA_EXPORT
803+
unsigned int ica_sha512_256(unsigned int message_part,
804+
uint64_t input_length,
805+
unsigned char *input_data,
806+
sha512_context_t *sha512_context,
807+
unsigned char *output_data);
808+
718809
ICA_EXPORT
719810
unsigned int ica_sha3_224(unsigned int message_part,
720811
unsigned int input_length,

libica.map

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,10 @@ LIBICA_3.3.0 {
126126
ica_mp_sqr512;
127127
local: *;
128128
} LIBICA_3.2.0;
129+
130+
LIBICA_3.4.0 {
131+
global:
132+
ica_sha512_224;
133+
ica_sha512_256;
134+
local: *;
135+
} LIBICA_3.3.0;

src/ica_api.c

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,84 @@ unsigned int ica_sha512(unsigned int message_part,
561561
(uint64_t *) &sha512_context->runningLengthHigh);
562562
}
563563

564+
unsigned int ica_sha512_224(unsigned int message_part,
565+
uint64_t input_length,
566+
unsigned char *input_data,
567+
sha512_context_t *sha512_context,
568+
unsigned char *output_data)
569+
{
570+
unsigned int rc;
571+
572+
#ifdef ICA_FIPS
573+
if (fips >> 1)
574+
return EACCES;
575+
#endif /* ICA_FIPS */
576+
577+
/* check for obvious errors in parms */
578+
if ((input_data == NULL) ||
579+
(sha512_context == NULL) ||
580+
(output_data == NULL))
581+
return EINVAL;
582+
583+
/* make sure some message part is specified */
584+
rc = check_message_part(message_part);
585+
if (rc)
586+
return rc;
587+
588+
/*
589+
* for FIRST or MIDDLE calls the input
590+
* data length must be a multiple of 128 bytes.
591+
*/
592+
if (input_length & 0x7f &&
593+
(message_part == SHA_MSG_PART_FIRST ||
594+
message_part == SHA_MSG_PART_MIDDLE))
595+
return EINVAL;
596+
597+
return s390_sha512_224((unsigned char *)&sha512_context->sha512Hash,
598+
input_data, input_length, output_data, message_part,
599+
(uint64_t *) &sha512_context->runningLengthLow,
600+
(uint64_t *) &sha512_context->runningLengthHigh);
601+
}
602+
603+
unsigned int ica_sha512_256(unsigned int message_part,
604+
uint64_t input_length,
605+
unsigned char *input_data,
606+
sha512_context_t *sha512_context,
607+
unsigned char *output_data)
608+
{
609+
unsigned int rc;
610+
611+
#ifdef ICA_FIPS
612+
if (fips >> 1)
613+
return EACCES;
614+
#endif /* ICA_FIPS */
615+
616+
/* check for obvious errors in parms */
617+
if ((input_data == NULL) ||
618+
(sha512_context == NULL) ||
619+
(output_data == NULL))
620+
return EINVAL;
621+
622+
/* make sure some message part is specified */
623+
rc = check_message_part(message_part);
624+
if (rc)
625+
return rc;
626+
627+
/*
628+
* for FIRST or MIDDLE calls the input
629+
* data length must be a multiple of 128 bytes.
630+
*/
631+
if (input_length & 0x7f &&
632+
(message_part == SHA_MSG_PART_FIRST ||
633+
message_part == SHA_MSG_PART_MIDDLE))
634+
return EINVAL;
635+
636+
return s390_sha512_256((unsigned char *)&sha512_context->sha512Hash,
637+
input_data, input_length, output_data, message_part,
638+
(uint64_t *) &sha512_context->runningLengthLow,
639+
(uint64_t *) &sha512_context->runningLengthHigh);
640+
}
641+
564642
unsigned int ica_sha3_224(unsigned int message_part,
565643
unsigned int input_length,
566644
unsigned char *input_data,

src/icainfo.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ static struct crypt_pair crypt_map[] = {
7676
{"SHA-256", SHA256},
7777
{"SHA-384", SHA384},
7878
{"SHA-512", SHA512},
79+
{"SHA-512/224", SHA512_224},
80+
{"SHA-512/256", SHA512_256},
7981
{"SHA3-224", SHA3_224},
8082
{"SHA3-256", SHA3_256},
8183
{"SHA3-384", SHA3_384},

src/include/icastats.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ typedef enum stats_fields {
3535
ICA_STATS_SHA256,
3636
ICA_STATS_SHA384,
3737
ICA_STATS_SHA512,
38+
ICA_STATS_SHA512_224,
39+
ICA_STATS_SHA512_256,
3840
ICA_STATS_SHA3_224,
3941
ICA_STATS_SHA3_256,
4042
ICA_STATS_SHA3_384,
@@ -84,6 +86,8 @@ typedef enum stats_fields {
8486
"SHA-256", \
8587
"SHA-384", \
8688
"SHA-512", \
89+
"SHA-512/224", \
90+
"SHA-512/256", \
8791
"SHA3-224", \
8892
"SHA3-256", \
8993
"SHA3-384", \

src/include/s390_crypto.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,9 @@ typedef enum {
125125
SHA_3_512,
126126
SHAKE_128,
127127
SHAKE_256,
128-
GHASH
128+
GHASH,
129+
SHA_512_224,
130+
SHA_512_256
129131
} kimd_functions_t;
130132

131133
typedef enum {

src/include/s390_sha.h

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,22 @@ static unsigned char SHA_512_DEFAULT_IV[] = {
4444
0x1f, 0x83, 0xd9, 0xab, 0xfb, 0x41, 0xbd, 0x6b, 0x5b, 0xe0, 0xcd, 0x19,
4545
0x13, 0x7e, 0x21, 0x79 };
4646

47+
static unsigned char SHA_512_224_DEFAULT_IV[] = {
48+
0x8C, 0x3D, 0x37, 0xC8, 0x19, 0x54, 0x4D, 0xA2, 0x73, 0xE1, 0x99, 0x66,
49+
0x89, 0xDC, 0xD4, 0xD6, 0x1D, 0xFA, 0xB7, 0xAE, 0x32, 0xFF, 0x9C, 0x82,
50+
0x67, 0x9D, 0xD5, 0x14, 0x58, 0x2F, 0x9F, 0xCF, 0x0F, 0x6D, 0x2B, 0x69,
51+
0x7B, 0xD4, 0x4D, 0xA8, 0x77, 0xE3, 0x6F, 0x73, 0x04, 0xC4, 0x89, 0x42,
52+
0x3F, 0x9D, 0x85, 0xA8, 0x6A, 0x1D, 0x36, 0xC8, 0x11, 0x12, 0xE6, 0xAD,
53+
0x91, 0xD6, 0x92, 0xA1 };
54+
55+
static unsigned char SHA_512_256_DEFAULT_IV[] = {
56+
0x22, 0x31, 0x21, 0x94, 0xFC, 0x2B, 0xF7, 0x2C, 0x9F, 0x55, 0x5F, 0xA3,
57+
0xC8, 0x4C, 0x64, 0xC2, 0x23, 0x93, 0xB8, 0x6B, 0x6F, 0x53, 0xB1, 0x51,
58+
0x96, 0x38, 0x77, 0x19, 0x59, 0x40, 0xEA, 0xBD, 0x96, 0x28, 0x3E, 0xE2,
59+
0xA8, 0x8E, 0xFF, 0xE3, 0xBE, 0x5E, 0x1E, 0x25, 0x53, 0x86, 0x39, 0x92,
60+
0x2B, 0x01, 0x99, 0xFC, 0x2C, 0x85, 0xB8, 0xAA, 0x0E, 0xB7, 0x2D, 0xDC,
61+
0x81, 0xC5, 0x2C, 0xA2 };
62+
4763
static unsigned char SHA_3_DEFAULT_IV[] = {
4864
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4965
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
@@ -82,7 +98,10 @@ static const SHA_CONSTANTS sha_constants[] = {
8298
{S390_CRYPTO_SHA_3_384, 48, 200, 104, SHA_3_DEFAULT_IV},
8399
{S390_CRYPTO_SHA_3_512, 64, 200, 72, SHA_3_DEFAULT_IV},
84100
{S390_CRYPTO_SHAKE_128, 0, 200, 168, SHA_3_DEFAULT_IV},
85-
{S390_CRYPTO_SHAKE_256, 0, 200, 136, SHA_3_DEFAULT_IV}
101+
{S390_CRYPTO_SHAKE_256, 0, 200, 136, SHA_3_DEFAULT_IV},
102+
{ 0, 0, 0, 0, NULL }, /* Dummy line for GHASH */
103+
{S390_CRYPTO_SHA_512, 28, 64, 128, SHA_512_224_DEFAULT_IV},
104+
{S390_CRYPTO_SHA_512, 32, 64, 128, SHA_512_256_DEFAULT_IV},
86105
};
87106

88107
int s390_sha1(unsigned char *iv, unsigned char *input_data,
@@ -107,6 +126,16 @@ int s390_sha512(unsigned char *iv, unsigned char *input_data,
107126
unsigned int message_part, uint64_t *running_length_lo,
108127
uint64_t *running_length_hi);
109128

129+
int s390_sha512_224(unsigned char *iv, unsigned char *input_data,
130+
uint64_t input_length, unsigned char *output_data,
131+
unsigned int message_part, uint64_t *running_length_lo,
132+
uint64_t *running_length_hi);
133+
134+
int s390_sha512_256(unsigned char *iv, unsigned char *input_data,
135+
uint64_t input_length, unsigned char *output_data,
136+
unsigned int message_part, uint64_t *running_length_lo,
137+
uint64_t *running_length_hi);
138+
110139
int s390_sha3_224(unsigned char *iv, unsigned char *input_data,
111140
unsigned int input_length, unsigned char *output_data,
112141
unsigned int message_part, uint64_t *running_length);

src/s390_crypto.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ s390_supported_function_t s390_kimd_functions[] = {
4747
{SHA_3_512, S390_CRYPTO_SHA_3_512, &sha3_switch},
4848
{SHAKE_128, S390_CRYPTO_SHAKE_128, &sha3_switch},
4949
{SHAKE_256, S390_CRYPTO_SHAKE_256, &sha3_switch},
50-
{GHASH, S390_CRYPTO_GHASH, &msa4_switch}
50+
{GHASH, S390_CRYPTO_GHASH, &msa4_switch},
51+
{SHA_512_224, S390_CRYPTO_SHA_512, &sha512_switch},
52+
{SHA_512_256, S390_CRYPTO_SHA_512, &sha512_switch}
5153
};
5254

5355
s390_supported_function_t s390_kmc_functions[] = {
@@ -298,6 +300,8 @@ libica_func_list_element_int icaList[] = {
298300
{SHA256, KIMD, SHA_256, ICA_FLAG_SW, 0},
299301
{SHA384, KIMD, SHA_512, ICA_FLAG_SW, 0},
300302
{SHA512, KIMD, SHA_512, ICA_FLAG_SW, 0},
303+
{SHA512_224, KIMD, SHA_512_224, ICA_FLAG_SW, 0},
304+
{SHA512_256, KIMD, SHA_512_256, ICA_FLAG_SW, 0},
301305
{SHA3_224, KIMD, SHA_3_224, 0, 0},
302306
{SHA3_256, KIMD, SHA_3_256, 0, 0},
303307
{SHA3_384, KIMD, SHA_3_384, 0, 0},

0 commit comments

Comments
 (0)