diff options
| author | tb <> | 2021-08-18 16:06:56 +0000 |
|---|---|---|
| committer | tb <> | 2021-08-18 16:06:56 +0000 |
| commit | 18b9c1bcab7c37d8c5bd05b8e0d14d0c59d96650 (patch) | |
| tree | 4c8c274fa412a2c828b63050128d911a1fafa9f9 /src/regress/lib/libcrypto/sm2/sm2evptest.c | |
| parent | a20accd2bfd8ef02b4b4e6d1e5e7cb57e0a536e4 (diff) | |
| download | openbsd-18b9c1bcab7c37d8c5bd05b8e0d14d0c59d96650.tar.gz openbsd-18b9c1bcab7c37d8c5bd05b8e0d14d0c59d96650.tar.bz2 openbsd-18b9c1bcab7c37d8c5bd05b8e0d14d0c59d96650.zip | |
Import regress tests for SM2. Not yet linked to the build.
Part of Github PR #105
Diffstat (limited to 'src/regress/lib/libcrypto/sm2/sm2evptest.c')
| -rw-r--r-- | src/regress/lib/libcrypto/sm2/sm2evptest.c | 256 |
1 files changed, 256 insertions, 0 deletions
diff --git a/src/regress/lib/libcrypto/sm2/sm2evptest.c b/src/regress/lib/libcrypto/sm2/sm2evptest.c new file mode 100644 index 0000000000..93024612f4 --- /dev/null +++ b/src/regress/lib/libcrypto/sm2/sm2evptest.c | |||
| @@ -0,0 +1,256 @@ | |||
| 1 | /* $OpenBSD: sm2evptest.c,v 1.1.1.1 2021/08/18 16:06:56 tb Exp $ */ | ||
| 2 | /* | ||
| 3 | * Copyright (c) 2017, 2019 Ribose Inc | ||
| 4 | * | ||
| 5 | * Permission to use, copy, modify, and/or distribute this software for any | ||
| 6 | * purpose with or without fee is hereby granted, provided that the above | ||
| 7 | * copyright notice and this permission notice appear in all copies. | ||
| 8 | * | ||
| 9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
| 10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
| 11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
| 12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
| 13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
| 14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
| 15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
| 16 | */ | ||
| 17 | |||
| 18 | #include <stdio.h> | ||
| 19 | #include <stdlib.h> | ||
| 20 | #include <string.h> | ||
| 21 | |||
| 22 | #include <openssl/ec.h> | ||
| 23 | #include <openssl/evp.h> | ||
| 24 | #include <openssl/pem.h> | ||
| 25 | |||
| 26 | #include "tests.h" | ||
| 27 | |||
| 28 | #ifdef OPENSSL_NO_SM2 | ||
| 29 | int | ||
| 30 | main(int argc, char *argv[]) | ||
| 31 | { | ||
| 32 | printf("No SM2 support\n"); | ||
| 33 | return (0); | ||
| 34 | } | ||
| 35 | #else | ||
| 36 | static int | ||
| 37 | test_EVP_SM2_verify(void) | ||
| 38 | { | ||
| 39 | /* From https://tools.ietf.org/html/draft-shen-sm2-ecdsa-02#appendix-A */ | ||
| 40 | const char *pubkey = | ||
| 41 | "-----BEGIN PUBLIC KEY-----\n" | ||
| 42 | "MIIBMzCB7AYHKoZIzj0CATCB4AIBATAsBgcqhkjOPQEBAiEAhULWnkwETxjouSQ1\n" | ||
| 43 | "v2/33kVyg5FcRVF9ci7biwjx38MwRAQgeHlotPoyw/0kF4Quc7v+/y88hItoMdfg\n" | ||
| 44 | "7GUiizk35JgEIGPkxtOyOwyEnPhCQUhL/kj2HVmlsWugbm4S0donxSSaBEEEQh3r\n" | ||
| 45 | "1hti6rZ0ZDTrw8wxXjIiCzut1QvcTE5sFH/t1D0GgFEry7QsB9RzSdIVO3DE5df9\n" | ||
| 46 | "/L+jbqGoWEG55G4JogIhAIVC1p5MBE8Y6LkkNb9v990pdyBjBIVijVrnTufDLnm3\n" | ||
| 47 | "AgEBA0IABArkx3mKoPEZRxvuEYJb5GICu3nipYRElel8BP9N8lSKfAJA+I8c1OFj\n" | ||
| 48 | "Uqc8F7fxbwc1PlOhdtaEqf4Ma7eY6Fc=\n" | ||
| 49 | "-----END PUBLIC KEY-----\n"; | ||
| 50 | |||
| 51 | const char *input = "message digest"; | ||
| 52 | const char *user_id = "ALICE123@YAHOO.COM"; | ||
| 53 | |||
| 54 | const uint8_t signature[] = { | ||
| 55 | 0x30, 0x44, 0x02, 0x20, | ||
| 56 | 0x40, 0xF1, 0xEC, 0x59, 0xF7, 0x93, 0xD9, 0xF4, 0x9E, 0x09, 0xDC, | ||
| 57 | 0xEF, 0x49, 0x13, 0x0D, 0x41, 0x94, 0xF7, 0x9F, 0xB1, 0xEE, 0xD2, | ||
| 58 | 0xCA, 0xA5, 0x5B, 0xAC, 0xDB, 0x49, 0xC4, 0xE7, 0x55, 0xD1, | ||
| 59 | 0x02, 0x20, | ||
| 60 | 0x6F, 0xC6, 0xDA, 0xC3, 0x2C, 0x5D, 0x5C, 0xF1, 0x0C, 0x77, 0xDF, | ||
| 61 | 0xB2, 0x0F, 0x7C, 0x2E, 0xB6, 0x67, 0xA4, 0x57, 0x87, 0x2F, 0xB0, | ||
| 62 | 0x9E, 0xC5, 0x63, 0x27, 0xA6, 0x7E, 0xC7, 0xDE, 0xEB, 0xE7 | ||
| 63 | }; | ||
| 64 | |||
| 65 | int rc = 0; | ||
| 66 | BIO *bufio = NULL; | ||
| 67 | EVP_PKEY *pkey = NULL; | ||
| 68 | EVP_MD_CTX *md_ctx_verify = NULL; | ||
| 69 | EVP_PKEY_CTX *verify_ctx = NULL; | ||
| 70 | |||
| 71 | bufio = BIO_new_mem_buf(pubkey, strlen(pubkey)); | ||
| 72 | CHECK_GOTO(bufio != NULL); | ||
| 73 | |||
| 74 | pkey = PEM_read_bio_PUBKEY(bufio, NULL, NULL, NULL); | ||
| 75 | CHECK_GOTO(pkey != NULL); | ||
| 76 | |||
| 77 | CHECK_GOTO(EVP_PKEY_set_alias_type(pkey, EVP_PKEY_SM2)); | ||
| 78 | |||
| 79 | md_ctx_verify = EVP_MD_CTX_new(); | ||
| 80 | CHECK_GOTO(md_ctx_verify != NULL); | ||
| 81 | |||
| 82 | CHECK_GOTO(EVP_DigestVerifyInit(md_ctx_verify, &verify_ctx, EVP_sm3(), NULL, pkey)); | ||
| 83 | |||
| 84 | CHECK_GOTO(EVP_PKEY_CTX_set_sm2_uid(verify_ctx, user_id, strlen(user_id)) > 0); | ||
| 85 | |||
| 86 | CHECK_GOTO(EVP_PKEY_CTX_hash_sm2_uid(verify_ctx) > 0); | ||
| 87 | |||
| 88 | CHECK_GOTO(EVP_DigestVerifyUpdate(md_ctx_verify, input, strlen(input))); | ||
| 89 | |||
| 90 | CHECK_GOTO(EVP_DigestVerifyFinal(md_ctx_verify, signature, sizeof(signature))); | ||
| 91 | |||
| 92 | rc = 1; | ||
| 93 | err: | ||
| 94 | BIO_free(bufio); | ||
| 95 | EVP_PKEY_free(pkey); | ||
| 96 | EVP_MD_CTX_free(md_ctx_verify); | ||
| 97 | return rc; | ||
| 98 | } | ||
| 99 | |||
| 100 | static int | ||
| 101 | test_EVP_SM2(void) | ||
| 102 | { | ||
| 103 | int ret = 0; | ||
| 104 | EVP_PKEY *pkey = NULL; | ||
| 105 | EVP_PKEY *params = NULL; | ||
| 106 | EVP_PKEY_CTX *pctx = NULL; | ||
| 107 | EVP_PKEY_CTX *sign_ctx = NULL; | ||
| 108 | EVP_PKEY_CTX *verify_ctx = NULL; | ||
| 109 | EVP_PKEY_CTX *kctx = NULL; | ||
| 110 | size_t sig_len = 0; | ||
| 111 | unsigned char *sig = NULL; | ||
| 112 | EVP_MD_CTX *md_ctx = NULL; | ||
| 113 | EVP_MD_CTX *md_ctx_verify = NULL; | ||
| 114 | EVP_PKEY_CTX *cctx = NULL; | ||
| 115 | int useid; | ||
| 116 | const char *uid_str = "nobody@example.com"; | ||
| 117 | uint8_t uid_buf[32] = {0}; | ||
| 118 | size_t uid_len = 0; | ||
| 119 | |||
| 120 | uint8_t ciphertext[128]; | ||
| 121 | size_t ctext_len = sizeof(ciphertext); | ||
| 122 | |||
| 123 | uint8_t plaintext[8]; | ||
| 124 | size_t ptext_len = sizeof(plaintext); | ||
| 125 | |||
| 126 | uint8_t kMsg[4] = {1, 2, 3, 4}; | ||
| 127 | |||
| 128 | pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL); | ||
| 129 | CHECK_GOTO(pctx != NULL); | ||
| 130 | |||
| 131 | CHECK_GOTO(EVP_PKEY_paramgen_init(pctx) == 1); | ||
| 132 | |||
| 133 | CHECK_GOTO(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, NID_sm2)); | ||
| 134 | |||
| 135 | CHECK_GOTO(EVP_PKEY_paramgen(pctx, ¶ms)); | ||
| 136 | |||
| 137 | kctx = EVP_PKEY_CTX_new(params, NULL); | ||
| 138 | CHECK_GOTO(kctx != NULL); | ||
| 139 | |||
| 140 | CHECK_GOTO(EVP_PKEY_keygen_init(kctx)); | ||
| 141 | |||
| 142 | CHECK_GOTO(EVP_PKEY_keygen(kctx, &pkey)); | ||
| 143 | |||
| 144 | CHECK_GOTO(EVP_PKEY_set_alias_type(pkey, EVP_PKEY_SM2)); | ||
| 145 | |||
| 146 | md_ctx = EVP_MD_CTX_new(); | ||
| 147 | CHECK_GOTO(md_ctx != NULL); | ||
| 148 | |||
| 149 | md_ctx_verify = EVP_MD_CTX_new(); | ||
| 150 | CHECK_GOTO(md_ctx_verify != NULL); | ||
| 151 | |||
| 152 | for (useid = 0; useid <= 1; ++useid) { | ||
| 153 | CHECK_GOTO(EVP_DigestSignInit(md_ctx, &sign_ctx, EVP_sm3(), NULL, pkey)); | ||
| 154 | |||
| 155 | if (useid) { | ||
| 156 | CHECK_GOTO(EVP_PKEY_CTX_set_sm2_uid(sign_ctx, uid_str, strlen(uid_str)) > 0); | ||
| 157 | |||
| 158 | CHECK_GOTO(EVP_PKEY_CTX_get_sm2_uid_len(sign_ctx, &uid_len) > 0); | ||
| 159 | |||
| 160 | CHECK_GOTO(uid_len == strlen(uid_str)); | ||
| 161 | |||
| 162 | CHECK_GOTO(EVP_PKEY_CTX_get_sm2_uid(sign_ctx, uid_buf) > 0); | ||
| 163 | |||
| 164 | CHECK_GOTO(memcmp(uid_buf, uid_str, uid_len) == 0); | ||
| 165 | |||
| 166 | CHECK_GOTO(EVP_PKEY_CTX_hash_sm2_uid(sign_ctx) > 0); | ||
| 167 | } | ||
| 168 | |||
| 169 | CHECK_GOTO(EVP_DigestSignUpdate(md_ctx, kMsg, sizeof(kMsg))); | ||
| 170 | |||
| 171 | /* Determine the size of the signature. */ | ||
| 172 | CHECK_GOTO(EVP_DigestSignFinal(md_ctx, NULL, &sig_len)); | ||
| 173 | |||
| 174 | CHECK_GOTO(sig_len == (size_t) EVP_PKEY_size(pkey)); | ||
| 175 | |||
| 176 | sig = malloc(sig_len); | ||
| 177 | CHECK_GOTO(sig != NULL); | ||
| 178 | |||
| 179 | CHECK_GOTO(EVP_DigestSignFinal(md_ctx, sig, &sig_len)); | ||
| 180 | |||
| 181 | /* Ensure that the signature round-trips. */ | ||
| 182 | |||
| 183 | CHECK_GOTO(EVP_DigestVerifyInit(md_ctx_verify, &verify_ctx, EVP_sm3(), NULL, pkey)); | ||
| 184 | |||
| 185 | if (useid) { | ||
| 186 | CHECK_GOTO(EVP_PKEY_CTX_set_sm2_uid(verify_ctx, uid_str, strlen(uid_str)) > 0); | ||
| 187 | |||
| 188 | CHECK_GOTO(EVP_PKEY_CTX_get_sm2_uid_len(verify_ctx, &uid_len) > 0); | ||
| 189 | |||
| 190 | CHECK_GOTO(uid_len == strlen(uid_str)); | ||
| 191 | |||
| 192 | CHECK_GOTO(EVP_PKEY_CTX_get_sm2_uid(verify_ctx, uid_buf) > 0); | ||
| 193 | |||
| 194 | CHECK_GOTO(memcmp(uid_buf, uid_str, uid_len) == 0); | ||
| 195 | |||
| 196 | CHECK_GOTO(EVP_PKEY_CTX_hash_sm2_uid(verify_ctx) > 0); | ||
| 197 | } | ||
| 198 | |||
| 199 | CHECK_GOTO(EVP_DigestVerifyUpdate(md_ctx_verify, kMsg, sizeof(kMsg))); | ||
| 200 | |||
| 201 | CHECK_GOTO(EVP_DigestVerifyFinal(md_ctx_verify, sig, sig_len)); | ||
| 202 | |||
| 203 | free(sig); | ||
| 204 | sig = NULL; | ||
| 205 | } | ||
| 206 | |||
| 207 | /* now check encryption/decryption */ | ||
| 208 | |||
| 209 | cctx = EVP_PKEY_CTX_new(pkey, NULL); | ||
| 210 | CHECK_GOTO(cctx != NULL); | ||
| 211 | |||
| 212 | CHECK_GOTO(EVP_PKEY_encrypt_init(cctx)); | ||
| 213 | |||
| 214 | CHECK_GOTO(EVP_PKEY_encrypt(cctx, ciphertext, &ctext_len, kMsg, sizeof(kMsg))); | ||
| 215 | |||
| 216 | CHECK_GOTO(EVP_PKEY_decrypt_init(cctx)); | ||
| 217 | |||
| 218 | CHECK_GOTO(EVP_PKEY_decrypt(cctx, plaintext, &ptext_len, ciphertext, ctext_len)); | ||
| 219 | |||
| 220 | CHECK_GOTO(ptext_len == sizeof(kMsg)); | ||
| 221 | |||
| 222 | CHECK_GOTO(memcmp(plaintext, kMsg, sizeof(kMsg)) == 0); | ||
| 223 | |||
| 224 | ret = 1; | ||
| 225 | err: | ||
| 226 | EVP_PKEY_free(params); | ||
| 227 | EVP_MD_CTX_free(md_ctx); | ||
| 228 | EVP_MD_CTX_free(md_ctx_verify); | ||
| 229 | EVP_PKEY_CTX_free(pctx); | ||
| 230 | EVP_PKEY_CTX_free(kctx); | ||
| 231 | EVP_PKEY_CTX_free(cctx); | ||
| 232 | EVP_PKEY_free(pkey); | ||
| 233 | free(sig); | ||
| 234 | return ret; | ||
| 235 | } | ||
| 236 | |||
| 237 | int | ||
| 238 | main(int argc, char *argv[]) | ||
| 239 | { | ||
| 240 | if (!test_EVP_SM2()) { | ||
| 241 | fprintf(stderr, "test_EVP_SM2() failed.\n"); | ||
| 242 | fflush(stderr); | ||
| 243 | return 1; | ||
| 244 | } | ||
| 245 | if (!test_EVP_SM2_verify()) { | ||
| 246 | fprintf(stderr, "test_EVP_SM2_verify() failed.\n"); | ||
| 247 | fflush(stderr); | ||
| 248 | return 1; | ||
| 249 | } | ||
| 250 | |||
| 251 | printf("SUCCESS\n"); | ||
| 252 | |||
| 253 | return 0; | ||
| 254 | } | ||
| 255 | |||
| 256 | #endif | ||
