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