f7e363e5c3022b51c9631c282995e1ab5b2e0a2b
[openssl.git] / providers / common / ciphers / cipher_aes_hw_t4.inc
1 /*
2  * Copyright 2001-2019 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
15 static 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
21     dat->ks = &adat->ks.ks;
22
23     bits = keylen * 8;
24     if ((dat->mode == EVP_CIPH_ECB_MODE || dat->mode == EVP_CIPH_CBC_MODE)
25         && !dat->enc) {
26         ret = 0;
27         aes_t4_set_decrypt_key(key, bits, dat->ks);
28         dat->block = (block128_f) aes_t4_decrypt;
29         switch (bits) {
30         case 128:
31             dat->stream.cbc = dat->mode == EVP_CIPH_CBC_MODE ?
32                 (cbc128_f) aes128_t4_cbc_decrypt : NULL;
33             break;
34         case 192:
35             dat->stream.cbc = dat->mode == EVP_CIPH_CBC_MODE ?
36                 (cbc128_f) aes192_t4_cbc_decrypt : NULL;
37             break;
38         case 256:
39             dat->stream.cbc = dat->mode == EVP_CIPH_CBC_MODE ?
40                 (cbc128_f) aes256_t4_cbc_decrypt : NULL;
41             break;
42         default:
43             ret = -1;
44         }
45     } else {
46         ret = 0;
47         aes_t4_set_encrypt_key(key, bits, dat->ks);
48         dat->block = (block128_f)aes_t4_encrypt;
49         switch (bits) {
50         case 128:
51             if (dat->mode == EVP_CIPH_CBC_MODE)
52                 dat->stream.cbc = (cbc128_f)aes128_t4_cbc_encrypt;
53             else if (dat->mode == EVP_CIPH_CTR_MODE)
54                 dat->stream.ctr = (ctr128_f)aes128_t4_ctr32_encrypt;
55             else
56                 dat->stream.cbc = NULL;
57             break;
58         case 192:
59             if (dat->mode == EVP_CIPH_CBC_MODE)
60                 dat->stream.cbc = (cbc128_f)aes192_t4_cbc_encrypt;
61             else if (dat->mode == EVP_CIPH_CTR_MODE)
62                 dat->stream.ctr = (ctr128_f)aes192_t4_ctr32_encrypt;
63             else
64                 dat->stream.cbc = NULL;
65             break;
66         case 256:
67             if (dat->mode == EVP_CIPH_CBC_MODE)
68                 dat->stream.cbc = (cbc128_f)aes256_t4_cbc_encrypt;
69             else if (dat->mode == EVP_CIPH_CTR_MODE)
70                 dat->stream.ctr = (ctr128_f)aes256_t4_ctr32_encrypt;
71             else
72                 dat->stream.cbc = NULL;
73             break;
74         default:
75             ret = -1;
76         }
77     }
78
79     if (ret < 0) {
80         ERR_raise(ERR_LIB_PROV, PROV_R_AES_KEY_SETUP_FAILED);
81         return 0;
82     }
83
84     return 1;
85 }
86
87 #define PROV_CIPHER_HW_declare(mode)                                           \
88 static const PROV_CIPHER_HW aes_t4_##mode = {                                  \
89     cipher_hw_aes_t4_initkey,                                                  \
90     cipher_hw_generic_##mode                                                   \
91 };
92 #define PROV_CIPHER_HW_select(mode)                                            \
93     if (SPARC_AES_CAPABLE)                                                     \
94         return aes_t4_##mode;