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 * Fujitsu SPARC64 X support for camellia modes.
12 * This file is included by cipher_camellia_hw.c
13 */
14
15static int cipher_hw_camellia_t4_initkey(PROV_CIPHER_CTX *dat,
16                                         const unsigned char *key,
17                                         size_t keylen)
18{
19    int ret = 0, bits, mode = dat->mode;
20    PROV_CAMELLIA_CTX *adat = (PROV_CAMELLIA_CTX *)dat;
21    CAMELLIA_KEY *ks = &adat->ks.ks;
22
23    dat->ks = ks;
24    bits = keylen * 8;
25
26    cmll_t4_set_key(key, bits, ks);
27
28    if (dat->enc || (mode != EVP_CIPH_ECB_MODE && mode != EVP_CIPH_CBC_MODE)) {
29        dat->block = (block128_f) cmll_t4_encrypt;
30        switch (bits) {
31        case 128:
32            if (mode == EVP_CIPH_CBC_MODE)
33                dat->stream.cbc = (cbc128_f) cmll128_t4_cbc_encrypt;
34            else if (mode == EVP_CIPH_CTR_MODE)
35                dat->stream.ctr = (ctr128_f) cmll128_t4_ctr32_encrypt;
36            else
37                dat->stream.cbc = NULL;
38            break;
39        case 192:
40        case 256:
41            if (mode == EVP_CIPH_CBC_MODE)
42                dat->stream.cbc = (cbc128_f) cmll256_t4_cbc_encrypt;
43            else if (mode == EVP_CIPH_CTR_MODE)
44                dat->stream.ctr = (ctr128_f) cmll256_t4_ctr32_encrypt;
45            else
46                dat->stream.cbc = NULL;
47            break;
48        default:
49            ret = -1;
50            break;
51        }
52    } else {
53        dat->block = (block128_f) cmll_t4_decrypt;
54        switch (bits) {
55        case 128:
56            dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ?
57                (cbc128_f) cmll128_t4_cbc_decrypt : NULL;
58            break;
59        case 192:
60        case 256:
61            dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ?
62                (cbc128_f) cmll256_t4_cbc_decrypt : NULL;
63            break;
64        default:
65            ret = -1;
66            break;
67        }
68    }
69    if (ret < 0) {
70        ERR_raise(ERR_LIB_PROV, PROV_R_KEY_SETUP_FAILED);
71        return 0;
72    }
73    return 1;
74}
75
76#define PROV_CIPHER_HW_declare(mode)                                           \
77static const PROV_CIPHER_HW t4_camellia_##mode = {                             \
78    cipher_hw_camellia_t4_initkey,                                             \
79    ossl_cipher_hw_generic_##mode,                                             \
80    cipher_hw_camellia_copyctx                                                 \
81};
82#define PROV_CIPHER_HW_select(mode)                                            \
83if (SPARC_CMLL_CAPABLE)                                                        \
84    return &t4_camellia_##mode;
85