1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) Hisilicon Technologies Co., Ltd. 2023. All rights reserved. 4 * 5 * SM4 optimization for ARMv8 6 */ 7 8 #include "sm4.h" 9 #include <assert.h> 10 #include <string.h> 11 #include <crypto/crypto_accel.h> 12 13 void sm4_setkey_enc(struct sm4_context *ctx, const uint8_t key[16]) 14 { 15 ctx->mode = SM4_ENCRYPT; 16 crypto_accel_sm4_setkey_enc(ctx->sk, key); 17 } 18 19 void sm4_setkey_dec(struct sm4_context *ctx, const uint8_t key[16]) 20 { 21 ctx->mode = SM4_DECRYPT; 22 crypto_accel_sm4_setkey_dec(ctx->sk, key); 23 } 24 25 void sm4_crypt_ecb(struct sm4_context *ctx, size_t length, const uint8_t *input, 26 uint8_t *output) 27 { 28 assert(!(length % 16)); 29 30 crypto_accel_sm4_ecb_enc(output, input, ctx->sk, length); 31 } 32 33 void sm4_crypt_cbc(struct sm4_context *ctx, size_t length, uint8_t iv[16], 34 const uint8_t *input, uint8_t *output) 35 { 36 assert(!(length % 16)); 37 38 if (ctx->mode == SM4_ENCRYPT) 39 crypto_accel_sm4_cbc_enc(output, input, ctx->sk, length, iv); 40 else 41 /* SM4_DECRYPT */ 42 crypto_accel_sm4_cbc_dec(output, input, ctx->sk, length, iv); 43 } 44 45 void sm4_crypt_ctr(struct sm4_context *ctx, size_t length, uint8_t ctr[16], 46 const uint8_t *input, uint8_t *output) 47 { 48 assert(!(length % 16)); 49 50 crypto_accel_sm4_ctr_enc(output, input, ctx->sk, length, ctr); 51 } 52 53 void sm4_crypt_xts(struct sm4_context *ctx, struct sm4_context *ctx_ek, 54 struct sm4_context *ctx_dk __unused, size_t len, uint8_t *iv, 55 const uint8_t *input, uint8_t *output) 56 { 57 assert(len >= 16); 58 59 if (ctx->mode == SM4_ENCRYPT) 60 crypto_accel_sm4_xts_enc(output, input, ctx->sk, ctx_ek->sk, 61 len, iv); 62 else 63 crypto_accel_sm4_xts_dec(output, input, ctx->sk, ctx_ek->sk, 64 len, iv); 65 } 66