1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 2001-2021 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci/*-
11e1051a39Sopenharmony_ci * Sparc t4 support for AES modes ecb, cbc, ofb, cfb, ctr.
12e1051a39Sopenharmony_ci * This file is included by cipher_aes_hw.c
13e1051a39Sopenharmony_ci */
14e1051a39Sopenharmony_ci
15e1051a39Sopenharmony_cistatic int cipher_hw_aes_t4_initkey(PROV_CIPHER_CTX *dat,
16e1051a39Sopenharmony_ci                                    const unsigned char *key, size_t keylen)
17e1051a39Sopenharmony_ci{
18e1051a39Sopenharmony_ci    int ret, bits;
19e1051a39Sopenharmony_ci    PROV_AES_CTX *adat = (PROV_AES_CTX *)dat;
20e1051a39Sopenharmony_ci    AES_KEY *ks = &adat->ks.ks;
21e1051a39Sopenharmony_ci
22e1051a39Sopenharmony_ci    dat->ks = (const void *)ks; /* used by cipher_hw_generic_XXX */
23e1051a39Sopenharmony_ci
24e1051a39Sopenharmony_ci    bits = keylen * 8;
25e1051a39Sopenharmony_ci    if ((dat->mode == EVP_CIPH_ECB_MODE || dat->mode == EVP_CIPH_CBC_MODE)
26e1051a39Sopenharmony_ci        && !dat->enc) {
27e1051a39Sopenharmony_ci        ret = 0;
28e1051a39Sopenharmony_ci        aes_t4_set_decrypt_key(key, bits, ks);
29e1051a39Sopenharmony_ci        dat->block = (block128_f)aes_t4_decrypt;
30e1051a39Sopenharmony_ci        switch (bits) {
31e1051a39Sopenharmony_ci        case 128:
32e1051a39Sopenharmony_ci            dat->stream.cbc = dat->mode == EVP_CIPH_CBC_MODE ?
33e1051a39Sopenharmony_ci                (cbc128_f)aes128_t4_cbc_decrypt : NULL;
34e1051a39Sopenharmony_ci            break;
35e1051a39Sopenharmony_ci        case 192:
36e1051a39Sopenharmony_ci            dat->stream.cbc = dat->mode == EVP_CIPH_CBC_MODE ?
37e1051a39Sopenharmony_ci                (cbc128_f)aes192_t4_cbc_decrypt : NULL;
38e1051a39Sopenharmony_ci            break;
39e1051a39Sopenharmony_ci        case 256:
40e1051a39Sopenharmony_ci            dat->stream.cbc = dat->mode == EVP_CIPH_CBC_MODE ?
41e1051a39Sopenharmony_ci                (cbc128_f)aes256_t4_cbc_decrypt : NULL;
42e1051a39Sopenharmony_ci            break;
43e1051a39Sopenharmony_ci        default:
44e1051a39Sopenharmony_ci            ret = -1;
45e1051a39Sopenharmony_ci        }
46e1051a39Sopenharmony_ci    } else {
47e1051a39Sopenharmony_ci        ret = 0;
48e1051a39Sopenharmony_ci        aes_t4_set_encrypt_key(key, bits, ks);
49e1051a39Sopenharmony_ci        dat->block = (block128_f)aes_t4_encrypt;
50e1051a39Sopenharmony_ci        switch (bits) {
51e1051a39Sopenharmony_ci        case 128:
52e1051a39Sopenharmony_ci            if (dat->mode == EVP_CIPH_CBC_MODE)
53e1051a39Sopenharmony_ci                dat->stream.cbc = (cbc128_f)aes128_t4_cbc_encrypt;
54e1051a39Sopenharmony_ci            else if (dat->mode == EVP_CIPH_CTR_MODE)
55e1051a39Sopenharmony_ci                dat->stream.ctr = (ctr128_f)aes128_t4_ctr32_encrypt;
56e1051a39Sopenharmony_ci            else
57e1051a39Sopenharmony_ci                dat->stream.cbc = NULL;
58e1051a39Sopenharmony_ci            break;
59e1051a39Sopenharmony_ci        case 192:
60e1051a39Sopenharmony_ci            if (dat->mode == EVP_CIPH_CBC_MODE)
61e1051a39Sopenharmony_ci                dat->stream.cbc = (cbc128_f)aes192_t4_cbc_encrypt;
62e1051a39Sopenharmony_ci            else if (dat->mode == EVP_CIPH_CTR_MODE)
63e1051a39Sopenharmony_ci                dat->stream.ctr = (ctr128_f)aes192_t4_ctr32_encrypt;
64e1051a39Sopenharmony_ci            else
65e1051a39Sopenharmony_ci                dat->stream.cbc = NULL;
66e1051a39Sopenharmony_ci            break;
67e1051a39Sopenharmony_ci        case 256:
68e1051a39Sopenharmony_ci            if (dat->mode == EVP_CIPH_CBC_MODE)
69e1051a39Sopenharmony_ci                dat->stream.cbc = (cbc128_f)aes256_t4_cbc_encrypt;
70e1051a39Sopenharmony_ci            else if (dat->mode == EVP_CIPH_CTR_MODE)
71e1051a39Sopenharmony_ci                dat->stream.ctr = (ctr128_f)aes256_t4_ctr32_encrypt;
72e1051a39Sopenharmony_ci            else
73e1051a39Sopenharmony_ci                dat->stream.cbc = NULL;
74e1051a39Sopenharmony_ci            break;
75e1051a39Sopenharmony_ci        default:
76e1051a39Sopenharmony_ci            ret = -1;
77e1051a39Sopenharmony_ci        }
78e1051a39Sopenharmony_ci    }
79e1051a39Sopenharmony_ci
80e1051a39Sopenharmony_ci    if (ret < 0) {
81e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_PROV, PROV_R_KEY_SETUP_FAILED);
82e1051a39Sopenharmony_ci        return 0;
83e1051a39Sopenharmony_ci    }
84e1051a39Sopenharmony_ci
85e1051a39Sopenharmony_ci    return 1;
86e1051a39Sopenharmony_ci}
87e1051a39Sopenharmony_ci
88e1051a39Sopenharmony_ci#define PROV_CIPHER_HW_declare(mode)                                           \
89e1051a39Sopenharmony_cistatic const PROV_CIPHER_HW aes_t4_##mode = {                                  \
90e1051a39Sopenharmony_ci    cipher_hw_aes_t4_initkey,                                                  \
91e1051a39Sopenharmony_ci    ossl_cipher_hw_generic_##mode,                                             \
92e1051a39Sopenharmony_ci    cipher_hw_aes_copyctx                                                      \
93e1051a39Sopenharmony_ci};
94e1051a39Sopenharmony_ci#define PROV_CIPHER_HW_select(mode)                                            \
95e1051a39Sopenharmony_ci    if (SPARC_AES_CAPABLE)                                                     \
96e1051a39Sopenharmony_ci        return &aes_t4_##mode;
97