|
| 1 | +# wolfSM (ShangMi) |
| 2 | + |
| 3 | +This appendix provides information about the Chinese National Standard's cryptographic algorithms known as ShangMi (SM) in wolfSSL. |
| 4 | + |
| 5 | +wolfSM support includes: |
| 6 | +* SM3 - Hash Function |
| 7 | +* SM4 - Cipher |
| 8 | +* SM2 - ECDH key agreement and a signature scheme using the specified 256-bit elliptic curve. |
| 9 | + |
| 10 | +The code must be installed into wolfSSL in order to be used. |
| 11 | + |
| 12 | +Note that the test and build configuration code is already in wolfSSL. |
| 13 | + |
| 14 | +## Getting and Installing wolfSM |
| 15 | + |
| 16 | +### Get wolfSM from GitHub |
| 17 | + |
| 18 | +Clone the wolfSM repository from GitHub: |
| 19 | + |
| 20 | +```sh |
| 21 | +git clone https://github.com/wolfssl/wolfsm.git |
| 22 | +``` |
| 23 | + |
| 24 | +### Get wolfSSL from GitHub |
| 25 | + |
| 26 | +wolfSSL is needed to build and test the SM algorithm implementations. |
| 27 | +Checkout the wolfSSL repository from GitHub beside wolfSM: |
| 28 | + |
| 29 | +```sh |
| 30 | +# Directory structure should be: |
| 31 | +# <install-dir> |
| 32 | +# ├── wolfsm |
| 33 | +# └── wolfssl |
| 34 | +``` |
| 35 | + |
| 36 | +```sh |
| 37 | +cd .. # To directory containing wolfsm |
| 38 | +git clone https://github.com/wolfssl/wolfssl.git |
| 39 | +``` |
| 40 | + |
| 41 | +### Install SM code into wolfSSL |
| 42 | + |
| 43 | +To install the SM code into wolfSSL, use the install script: |
| 44 | + |
| 45 | +```sh |
| 46 | +cd wolfsm |
| 47 | +./install.sh |
| 48 | +``` |
| 49 | + |
| 50 | +## Building wolfSM |
| 51 | + |
| 52 | +Once the wolfSM files have been installed into wolfSSL, you can build SM algorithms into wolfSSL. |
| 53 | + |
| 54 | +Choose which algorithms you require on the configure line: |
| 55 | +* `--enable-sm3` |
| 56 | +* `--enable-sm4-ecb` |
| 57 | +* `--enable-sm4-cbc` |
| 58 | +* `--enable-sm4-ctr` |
| 59 | +* `--enable-sm4-gcm` |
| 60 | +* `--enable-sm4-ccm` |
| 61 | +* `--enable-sm2` |
| 62 | + |
| 63 | +For example, to include SM3, SM4-GCM and SM2: |
| 64 | + |
| 65 | +```sh |
| 66 | +./autogen.sh |
| 67 | +./configure --enable-sm3 --enable-sm4-gcm --enable-sm2 |
| 68 | +make |
| 69 | +sudo make install |
| 70 | +``` |
| 71 | + |
| 72 | +### Optimized SM2 |
| 73 | + |
| 74 | +To use optimized implementations of SM2 you can either use C only code or C code with the faster assembly code. |
| 75 | + |
| 76 | +For C code only: `--enable-sp` |
| 77 | +For C and assembly code: `--enable-sp --enable-sp-asm` |
| 78 | + |
| 79 | +Optimized C code is available for 32 and 64 bit CPUs. |
| 80 | + |
| 81 | +Assembly code is available for the following platforms: |
| 82 | +* Intel x64 |
| 83 | +* Aarch64 |
| 84 | +* ARM 32-bit |
| 85 | +* ARM Thumb2 |
| 86 | +* ARM Thumb |
| 87 | + |
| 88 | +## Testing wolfSM |
| 89 | + |
| 90 | +To test that the SM ciphers are working use the following command: |
| 91 | + |
| 92 | +```sh |
| 93 | +make test |
| 94 | +``` |
| 95 | + |
| 96 | +To benchmark the algorithms enabled: |
| 97 | + |
| 98 | +```sh |
| 99 | +./wolfcrypt/benchmark/benchmark |
| 100 | +``` |
| 101 | + |
| 102 | +To benchmark specific algorithms, add to the command line the option/s matching the algorithm/s: |
| 103 | +* SM2: `-sm2` |
| 104 | +* SM3: `-sm3` |
| 105 | +* SM4: `-sm4` or |
| 106 | + * SM4-CBC: `-sm4-cbc` |
| 107 | + * SM4-GCM: `-sm4-gcm` |
| 108 | + * SM4-CCM: `-sm4-ccm` |
| 109 | + |
| 110 | +### Testing TLS |
| 111 | + |
| 112 | +SM ciphers are able to be used with TLSv1.2 and TLSv1.3. |
| 113 | + |
| 114 | +Note: SM2, SM3 and at least one SM4 cipher must be built in order for SM ciphers suite to work. All algorithms must be SM. |
| 115 | + |
| 116 | +The cipher suites added are: |
| 117 | + - ECDHE-ECDSA-SM4-CBC-SM3 (TLSv1.2, `--enable-sm2 --enable-sm3 --enable-sm4-cbc`) |
| 118 | + - ECDHE-ECDSA-SM4-GCM-SM3 (TLSv1.2, `--enable-sm2 --enable-sm3 --enable-sm4-gcm`) |
| 119 | + - ECDHE-ECDSA-SM4-CCM-SM3 (TLSv1.2, `--enable-sm2 --enable-sm3 --enable-sm4-ccm`) |
| 120 | + - TLS13-SM4-GCM-SM3 (TLSv1.3, `--enable-sm2 --enable-sm3 --enable-sm4-gcm`) |
| 121 | + - TLS13-SM4-CCM-SM3 (TLSv1.3, `--enable-sm2 --enable-sm3 --enable-sm4-ccm`) |
| 122 | + |
| 123 | +#### Example of using SM cipher suites with TLSv1.2 |
| 124 | + |
| 125 | +An example of testing TLSv1.2 with "ECDHE-ECDSA-SM4-CBC-SM3" cipher suite: |
| 126 | + |
| 127 | +```sh |
| 128 | +./examples/server/server -v 3 -l ECDHE-ECDSA-SM4-CBC-SM3 \ |
| 129 | + -c ./certs/sm2/server-sm2.pem -k ./certs/sm2/server-sm2-priv.pem \ |
| 130 | + -A ./certs/sm2/client-sm2.pem -V & |
| 131 | +./examples/client/client -v 3 -l ECDHE-ECDSA-SM4-CBC-SM3 \ |
| 132 | + -c ./certs/sm2/client-sm2.pem -k ./certs/sm2/client-sm2-priv.pem \ |
| 133 | + -A ./certs/sm2/root-sm2.pem -C |
| 134 | +``` |
| 135 | + |
| 136 | +The output using the commands above will be: |
| 137 | + |
| 138 | +``` |
| 139 | +SSL version is TLSv1.2 |
| 140 | +SSL cipher suite is TLS_ECDHE_ECDSA_WITH_SM4_CBC_SM3 |
| 141 | +SSL curve name is SM2P256V1 |
| 142 | +SSL version is TLSv1.2 |
| 143 | +SSL cipher suite is TLS_ECDHE_ECDSA_WITH_SM4_CBC_SM3 |
| 144 | +SSL curve name is SM2P256V1 |
| 145 | +Client message: hello wolfssl! |
| 146 | +I hear you fa shizzle! |
| 147 | +``` |
| 148 | + |
| 149 | +#### Example of using SM cipher suites with TLSv1.3 |
| 150 | + |
| 151 | +An example of testing TLSv1.3 with "TLS13-SM4-GCM-SM3" cipher suite: |
| 152 | + |
| 153 | +```sh |
| 154 | +./examples/server/server -v 4 -l TLS13-SM4-GCM-SM3 \ |
| 155 | + -c ./certs/sm2/server-sm2.pem -k ./certs/sm2/server-sm2-priv.pem \ |
| 156 | + -A ./certs/sm2/client-sm2.pem -V & |
| 157 | +./examples/client/client -v 4 -l TLS13-SM4-GCM-SM3 \ |
| 158 | + -c ./certs/sm2/client-sm2.pem -k ./certs/sm2/client-sm2-priv.pem \ |
| 159 | + -A ./certs/sm2/root-sm2.pem -C |
| 160 | +``` |
| 161 | + |
| 162 | +The output using the commands above will be: |
| 163 | + |
| 164 | +``` |
| 165 | +SSL version is TLSv1.3 |
| 166 | +SSL cipher suite is TLS_SM4_GCM_SM3 |
| 167 | +SSL curve name is SM2P256V1 |
| 168 | +SSL version is TLSv1.3 |
| 169 | +SSL cipher suite is TLS_SM4_GCM_SM3 |
| 170 | +SSL curve name is SM2P256V1 |
| 171 | +Client message: hello wolfssl! |
| 172 | +I hear you fa shizzle! |
| 173 | +``` |
0 commit comments