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