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