1*b077aed3SPierre Pronchery /*
2*b077aed3SPierre Pronchery * Copyright 2019-2020 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 * This file uses the low level AES functions (which are deprecated for
12*b077aed3SPierre Pronchery * non-internal use) in order to implement provider AES ciphers.
13*b077aed3SPierre Pronchery */
14*b077aed3SPierre Pronchery #include "internal/deprecated.h"
15*b077aed3SPierre Pronchery
16*b077aed3SPierre Pronchery #include "cipher_aes_ocb.h"
17*b077aed3SPierre Pronchery
18*b077aed3SPierre Pronchery #define OCB_SET_KEY_FN(fn_set_enc_key, fn_set_dec_key, \
19*b077aed3SPierre Pronchery fn_block_enc, fn_block_dec, \
20*b077aed3SPierre Pronchery fn_stream_enc, fn_stream_dec) \
21*b077aed3SPierre Pronchery CRYPTO_ocb128_cleanup(&ctx->ocb); \
22*b077aed3SPierre Pronchery fn_set_enc_key(key, keylen * 8, &ctx->ksenc.ks); \
23*b077aed3SPierre Pronchery fn_set_dec_key(key, keylen * 8, &ctx->ksdec.ks); \
24*b077aed3SPierre Pronchery if (!CRYPTO_ocb128_init(&ctx->ocb, &ctx->ksenc.ks, &ctx->ksdec.ks, \
25*b077aed3SPierre Pronchery (block128_f)fn_block_enc, (block128_f)fn_block_dec, \
26*b077aed3SPierre Pronchery ctx->base.enc ? (ocb128_f)fn_stream_enc : \
27*b077aed3SPierre Pronchery (ocb128_f)fn_stream_dec)) \
28*b077aed3SPierre Pronchery return 0; \
29*b077aed3SPierre Pronchery ctx->key_set = 1
30*b077aed3SPierre Pronchery
31*b077aed3SPierre Pronchery
cipher_hw_aes_ocb_generic_initkey(PROV_CIPHER_CTX * vctx,const unsigned char * key,size_t keylen)32*b077aed3SPierre Pronchery static int cipher_hw_aes_ocb_generic_initkey(PROV_CIPHER_CTX *vctx,
33*b077aed3SPierre Pronchery const unsigned char *key,
34*b077aed3SPierre Pronchery size_t keylen)
35*b077aed3SPierre Pronchery {
36*b077aed3SPierre Pronchery PROV_AES_OCB_CTX *ctx = (PROV_AES_OCB_CTX *)vctx;
37*b077aed3SPierre Pronchery
38*b077aed3SPierre Pronchery /*
39*b077aed3SPierre Pronchery * We set both the encrypt and decrypt key here because decrypt
40*b077aed3SPierre Pronchery * needs both. (i.e- AAD uses encrypt).
41*b077aed3SPierre Pronchery */
42*b077aed3SPierre Pronchery # ifdef HWAES_CAPABLE
43*b077aed3SPierre Pronchery if (HWAES_CAPABLE) {
44*b077aed3SPierre Pronchery OCB_SET_KEY_FN(HWAES_set_encrypt_key, HWAES_set_decrypt_key,
45*b077aed3SPierre Pronchery HWAES_encrypt, HWAES_decrypt,
46*b077aed3SPierre Pronchery HWAES_ocb_encrypt, HWAES_ocb_decrypt);
47*b077aed3SPierre Pronchery } else
48*b077aed3SPierre Pronchery # endif
49*b077aed3SPierre Pronchery # ifdef VPAES_CAPABLE
50*b077aed3SPierre Pronchery if (VPAES_CAPABLE) {
51*b077aed3SPierre Pronchery OCB_SET_KEY_FN(vpaes_set_encrypt_key, vpaes_set_decrypt_key,
52*b077aed3SPierre Pronchery vpaes_encrypt, vpaes_decrypt, NULL, NULL);
53*b077aed3SPierre Pronchery } else
54*b077aed3SPierre Pronchery # endif
55*b077aed3SPierre Pronchery {
56*b077aed3SPierre Pronchery OCB_SET_KEY_FN(AES_set_encrypt_key, AES_set_decrypt_key,
57*b077aed3SPierre Pronchery AES_encrypt, AES_decrypt, NULL, NULL);
58*b077aed3SPierre Pronchery }
59*b077aed3SPierre Pronchery return 1;
60*b077aed3SPierre Pronchery }
61*b077aed3SPierre Pronchery
62*b077aed3SPierre Pronchery # if defined(AESNI_CAPABLE)
63*b077aed3SPierre Pronchery
cipher_hw_aes_ocb_aesni_initkey(PROV_CIPHER_CTX * vctx,const unsigned char * key,size_t keylen)64*b077aed3SPierre Pronchery static int cipher_hw_aes_ocb_aesni_initkey(PROV_CIPHER_CTX *vctx,
65*b077aed3SPierre Pronchery const unsigned char *key,
66*b077aed3SPierre Pronchery size_t keylen)
67*b077aed3SPierre Pronchery {
68*b077aed3SPierre Pronchery PROV_AES_OCB_CTX *ctx = (PROV_AES_OCB_CTX *)vctx;
69*b077aed3SPierre Pronchery
70*b077aed3SPierre Pronchery OCB_SET_KEY_FN(aesni_set_encrypt_key, aesni_set_decrypt_key,
71*b077aed3SPierre Pronchery aesni_encrypt, aesni_decrypt,
72*b077aed3SPierre Pronchery aesni_ocb_encrypt, aesni_ocb_decrypt);
73*b077aed3SPierre Pronchery return 1;
74*b077aed3SPierre Pronchery }
75*b077aed3SPierre Pronchery
76*b077aed3SPierre Pronchery # define PROV_CIPHER_HW_declare() \
77*b077aed3SPierre Pronchery static const PROV_CIPHER_HW aesni_ocb = { \
78*b077aed3SPierre Pronchery cipher_hw_aes_ocb_aesni_initkey, \
79*b077aed3SPierre Pronchery NULL \
80*b077aed3SPierre Pronchery };
81*b077aed3SPierre Pronchery # define PROV_CIPHER_HW_select() \
82*b077aed3SPierre Pronchery if (AESNI_CAPABLE) \
83*b077aed3SPierre Pronchery return &aesni_ocb;
84*b077aed3SPierre Pronchery
85*b077aed3SPierre Pronchery #elif defined(SPARC_AES_CAPABLE)
86*b077aed3SPierre Pronchery
cipher_hw_aes_ocb_t4_initkey(PROV_CIPHER_CTX * vctx,const unsigned char * key,size_t keylen)87*b077aed3SPierre Pronchery static int cipher_hw_aes_ocb_t4_initkey(PROV_CIPHER_CTX *vctx,
88*b077aed3SPierre Pronchery const unsigned char *key,
89*b077aed3SPierre Pronchery size_t keylen)
90*b077aed3SPierre Pronchery {
91*b077aed3SPierre Pronchery PROV_AES_OCB_CTX *ctx = (PROV_AES_OCB_CTX *)vctx;
92*b077aed3SPierre Pronchery
93*b077aed3SPierre Pronchery OCB_SET_KEY_FN(aes_t4_set_encrypt_key, aes_t4_set_decrypt_key,
94*b077aed3SPierre Pronchery aes_t4_encrypt, aes_t4_decrypt, NULL, NULL);
95*b077aed3SPierre Pronchery return 1;
96*b077aed3SPierre Pronchery }
97*b077aed3SPierre Pronchery
98*b077aed3SPierre Pronchery # define PROV_CIPHER_HW_declare() \
99*b077aed3SPierre Pronchery static const PROV_CIPHER_HW aes_t4_ocb = { \
100*b077aed3SPierre Pronchery cipher_hw_aes_ocb_t4_initkey, \
101*b077aed3SPierre Pronchery NULL \
102*b077aed3SPierre Pronchery };
103*b077aed3SPierre Pronchery # define PROV_CIPHER_HW_select() \
104*b077aed3SPierre Pronchery if (SPARC_AES_CAPABLE) \
105*b077aed3SPierre Pronchery return &aes_t4_ocb;
106*b077aed3SPierre Pronchery #else
107*b077aed3SPierre Pronchery # define PROV_CIPHER_HW_declare()
108*b077aed3SPierre Pronchery # define PROV_CIPHER_HW_select()
109*b077aed3SPierre Pronchery # endif
110*b077aed3SPierre Pronchery
111*b077aed3SPierre Pronchery static const PROV_CIPHER_HW aes_generic_ocb = {
112*b077aed3SPierre Pronchery cipher_hw_aes_ocb_generic_initkey,
113*b077aed3SPierre Pronchery NULL
114*b077aed3SPierre Pronchery };
PROV_CIPHER_HW_declare()115*b077aed3SPierre Pronchery PROV_CIPHER_HW_declare()
116*b077aed3SPierre Pronchery const PROV_CIPHER_HW *ossl_prov_cipher_hw_aes_ocb(size_t keybits)
117*b077aed3SPierre Pronchery {
118*b077aed3SPierre Pronchery PROV_CIPHER_HW_select()
119*b077aed3SPierre Pronchery return &aes_generic_ocb;
120*b077aed3SPierre Pronchery }
121*b077aed3SPierre Pronchery
122*b077aed3SPierre Pronchery
123