Skip to content

Commit

Permalink
Generate compiler compatible assembly symbols
Browse files Browse the repository at this point in the history
Typically when compiling a function `foo`,  GCC will add a prefix `_` to the symbol, i.e., generate `_foo`. But on some platforms, the compiler will not add prefix. option `ENABLE_ASM_UNDERSCORE_PREFIX` change the default name of global symbols in assembly code.
  • Loading branch information
guanzhi committed Apr 13, 2024
1 parent 8e2c4eb commit 0daba2e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ option(ENABLE_SM4_CFB "Enable SM4 CFB mode" OFF)
option(ENABLE_SM4_CBC_MAC "Enable SM4-CBC-MAC" OFF)
option(ENABLE_SM4_CCM "Enable SM4 CCM mode" OFF)

option(ENABLE_ASM_UNDERSCORE_PREFIX "Add prefix `_` to assembly symbols" ON)
option(ENABLE_GMUL_AARCH64 "Enable GF(2^128) Multiplication AArch64 assembly" OFF)



set(src
src/version.c
src/debug.c
Expand Down Expand Up @@ -257,6 +259,10 @@ if (ENABLE_SM2_ALGOR_ID_ENCODE_NULL)
add_definitions(-DENABLE_SM2_ALGOR_ID_ENCODE_NULL)
endif()

if (ENABLE_ASM_UNDERSCORE_PREFIX)
message(STATUS "ENABLE_ASM_UNDERSCORE_PREFIX is ON")
add_definitions(-DENABLE_ASM_UNDERSCORE_PREFIX)
endif()

if (ENABLE_GMUL_AARCH64)
message(STATUS "ENABLE_GMUL_AARCH64 is ON")
Expand Down
20 changes: 20 additions & 0 deletions include/gmssl/asm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 2014-2024 The GmSSL Project. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
*
* http://www.apache.org/licenses/LICENSE-2.0
*/


#ifndef GMSSL_ASM_H
#define GMSSL_ASM_H

#ifdef ENABLE_ASM_UNDERSCORE_PREFIX
# define func(foo) _##foo
#else
# define func(foo) foo
#endif

#endif
9 changes: 7 additions & 2 deletions src/gf128_aarch64.S
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
*/


#include <gmssl/asm.h>


/* GF(2^128) defined by f(x) = x^128 + x^7 + x^2 + x + 1
f0 = x^128 = x^7 + x^2 + x + 1
Expand All @@ -29,9 +32,11 @@
= c + (e0 + w0) * x^64 + (d0 + w1) * f0
*/
.text
.globl _gf128_mul

.globl func(gf128_mul)
.align 4
_gf128_mul:

func(gf128_mul):
// load (a0, a1)
ld1 {v1.2d},[x1]
// load (b0, b1)
Expand Down
15 changes: 9 additions & 6 deletions src/sm4_aarch64.S
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* http://www.apache.org/licenses/LICENSE-2.0
*/

#include <gmssl/asm.h>

.align 7

Expand Down Expand Up @@ -62,9 +63,10 @@ Llshift:
.byte 4,5,6,7, 8,9,10,11, 12,13,14,15, 0,1,2,3


.globl _sm4_set_encrypt_key
.globl func(sm4_set_encrypt_key)
.align 4
_sm4_set_encrypt_key:

func(sm4_set_encrypt_key):

// load const v16..v31 = SBox
adr x3, LSBOX
Expand Down Expand Up @@ -140,9 +142,10 @@ _sm4_set_encrypt_key:
ret


.globl _sm4_set_decrypt_key
.globl func(sm4_set_decrypt_key)
.align 4
_sm4_set_decrypt_key:

func(sm4_set_decrypt_key):

// load const v16..v31 = SBox
adr x3,LSBOX
Expand Down Expand Up @@ -221,10 +224,10 @@ _sm4_set_decrypt_key:
ret


.globl _sm4_encrypt
.globl func(sm4_encrypt)

.align 5
_sm4_encrypt:
func(sm4_encrypt):

// load sbox
adr x3, LSBOX
Expand Down

0 comments on commit 0daba2e

Please sign in to comment.