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