1/* 2 * Copyright 2022-2023 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 * RISC-V 64 ZKND ZKNE support for AES modes ecb, cbc, ofb, cfb, ctr. 12 * This file is included by cipher_aes_hw.c 13 */ 14 15#define cipher_hw_rv64i_zknd_zkne_cbc ossl_cipher_hw_generic_cbc 16#define cipher_hw_rv64i_zknd_zkne_ecb ossl_cipher_hw_generic_ecb 17#define cipher_hw_rv64i_zknd_zkne_ofb128 ossl_cipher_hw_generic_ofb128 18#define cipher_hw_rv64i_zknd_zkne_cfb128 ossl_cipher_hw_generic_cfb128 19#define cipher_hw_rv64i_zknd_zkne_cfb8 ossl_cipher_hw_generic_cfb8 20#define cipher_hw_rv64i_zknd_zkne_cfb1 ossl_cipher_hw_generic_cfb1 21#define cipher_hw_rv64i_zknd_zkne_ctr ossl_cipher_hw_generic_ctr 22 23static int cipher_hw_rv64i_zknd_zkne_initkey(PROV_CIPHER_CTX *dat, 24 const unsigned char *key, size_t keylen) 25{ 26 int ret; 27 PROV_AES_CTX *adat = (PROV_AES_CTX *)dat; 28 AES_KEY *ks = &adat->ks.ks; 29 30 dat->ks = ks; 31 32 if ((dat->mode == EVP_CIPH_ECB_MODE || dat->mode == EVP_CIPH_CBC_MODE) 33 && !dat->enc) { 34 ret = rv64i_zknd_set_decrypt_key(key, keylen * 8, ks); 35 dat->block = (block128_f) rv64i_zknd_decrypt; 36 dat->stream.cbc = NULL; 37 } else { 38 ret = rv64i_zkne_set_encrypt_key(key, keylen * 8, ks); 39 dat->block = (block128_f) rv64i_zkne_encrypt; 40 dat->stream.cbc = NULL; 41 } 42 43 if (ret < 0) { 44 ERR_raise(ERR_LIB_PROV, PROV_R_KEY_SETUP_FAILED); 45 return 0; 46 } 47 48 return 1; 49} 50 51/*- 52 * RISC-V RV64 ZVKNED support for AES modes ecb, cbc, ofb, cfb, ctr. 53 * This file is included by cipher_aes_hw.c 54 */ 55 56#define cipher_hw_rv64i_zvkned_cbc ossl_cipher_hw_generic_cbc 57#define cipher_hw_rv64i_zvkned_ecb ossl_cipher_hw_generic_ecb 58#define cipher_hw_rv64i_zvkned_ofb128 ossl_cipher_hw_generic_ofb128 59#define cipher_hw_rv64i_zvkned_cfb128 ossl_cipher_hw_generic_cfb128 60#define cipher_hw_rv64i_zvkned_cfb8 ossl_cipher_hw_generic_cfb8 61#define cipher_hw_rv64i_zvkned_cfb1 ossl_cipher_hw_generic_cfb1 62#define cipher_hw_rv64i_zvkned_ctr ossl_cipher_hw_generic_ctr 63 64static int cipher_hw_rv64i_zvkned_initkey(PROV_CIPHER_CTX *dat, 65 const unsigned char *key, 66 size_t keylen) 67{ 68 int ret; 69 PROV_AES_CTX *adat = (PROV_AES_CTX *)dat; 70 AES_KEY *ks = &adat->ks.ks; 71 72 dat->ks = ks; 73 74 /* 75 * Zvkned only supports 128 and 256 bit keys for key schedule generation. 76 * For AES-192 case, we could fallback to `AES_set_encrypt_key`. 77 * All Zvkned-based implementations use the same `encrypt-key` scheduling 78 * for both encryption and decryption. 79 */ 80 if (keylen * 8 == 128 || keylen * 8 == 256) { 81 ret = rv64i_zvkned_set_encrypt_key(key, keylen * 8, ks); 82 } else { 83 ret = AES_set_encrypt_key(key, keylen * 8, ks); 84 } 85 86 if (dat->mode == EVP_CIPH_CBC_MODE) { 87 if (dat->enc) { 88 dat->stream.cbc = (cbc128_f) rv64i_zvkned_cbc_encrypt; 89 } else { 90 dat->stream.cbc = (cbc128_f) rv64i_zvkned_cbc_decrypt; 91 } 92 } else if (dat->mode == EVP_CIPH_CTR_MODE) { 93 if (RISCV_HAS_ZVKB()) { 94 dat->stream.ctr = (ctr128_f) rv64i_zvkb_zvkned_ctr32_encrypt_blocks; 95 } 96 } else if (dat->mode == EVP_CIPH_ECB_MODE) { 97 if (dat->enc) { 98 dat->stream.ecb = (ecb128_f) rv64i_zvkned_ecb_encrypt; 99 } else { 100 dat->stream.ecb = (ecb128_f) rv64i_zvkned_ecb_decrypt; 101 } 102 } 103 104 /* Zvkned supports aes-128/192/256 encryption and decryption. */ 105 if ((dat->mode == EVP_CIPH_ECB_MODE || dat->mode == EVP_CIPH_CBC_MODE) && 106 !dat->enc) { 107 dat->block = (block128_f) rv64i_zvkned_decrypt; 108 } else { 109 dat->block = (block128_f) rv64i_zvkned_encrypt; 110 } 111 112 if (ret < 0) { 113 ERR_raise(ERR_LIB_PROV, PROV_R_KEY_SETUP_FAILED); 114 return 0; 115 } 116 117 return 1; 118} 119 120#define PROV_CIPHER_HW_declare(mode) \ 121static const PROV_CIPHER_HW rv64i_zknd_zkne_##mode = { \ 122 cipher_hw_rv64i_zknd_zkne_initkey, \ 123 cipher_hw_rv64i_zknd_zkne_##mode, \ 124 cipher_hw_aes_copyctx \ 125}; \ 126static const PROV_CIPHER_HW rv64i_zvkned_##mode = { \ 127 cipher_hw_rv64i_zvkned_initkey, \ 128 cipher_hw_rv64i_zvkned_##mode, \ 129 cipher_hw_aes_copyctx \ 130}; 131#define PROV_CIPHER_HW_select(mode) \ 132if (RISCV_HAS_ZVKNED() && riscv_vlen() >= 128) \ 133 return &rv64i_zvkned_##mode; \ 134else if (RISCV_HAS_ZKND_AND_ZKNE()) \ 135 return &rv64i_zknd_zkne_##mode; 136