| Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/openssl/src/crypto/evp/e_sm4.c |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /* | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved. | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | * Copyright 2017 Ribose Inc. All Rights Reserved. | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | * Ported from Ribose contributions from Botan. | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | * | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | * Licensed under the OpenSSL license (the "License"). You may not use | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | * this file except in compliance with the License. You can obtain a copy | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | * in the file LICENSE in the source distribution or at | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | * https://www.openssl.org/source/license.html | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | */ | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | - | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | #include "internal/cryptlib.h" | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | #ifndef OPENSSL_NO_SM4 | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | # include <openssl/evp.h> | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | # include <openssl/modes.h> | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | # include "internal/sm4.h" | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | # include "internal/evp_int.h" | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | - | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | typedef struct { | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | SM4_KEY ks; | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | } EVP_SM4_KEY; | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | - | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | static int sm4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | const unsigned char *iv, int enc) | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | { | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | SM4_set_key(key, EVP_CIPHER_CTX_get_cipher_data(ctx)); | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | return 1; executed 152 times by 1 test: return 1;Executed by:
| 152 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | } | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | - | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | static void sm4_cbc_encrypt(const unsigned char *in, unsigned char *out, | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | size_t len, const SM4_KEY *key, | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | unsigned char *ivec, const int enc) | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | { | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | if (enc)
| 74-96 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | CRYPTO_cbc128_encrypt(in, out, len, key, ivec, executed 96 times by 1 test: CRYPTO_cbc128_encrypt(in, out, len, key, ivec, (block128_f)SM4_encrypt);Executed by:
| 96 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | (block128_f)SM4_encrypt); executed 96 times by 1 test: CRYPTO_cbc128_encrypt(in, out, len, key, ivec, (block128_f)SM4_encrypt);Executed by:
| 96 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | else | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | CRYPTO_cbc128_decrypt(in, out, len, key, ivec, executed 74 times by 1 test: CRYPTO_cbc128_decrypt(in, out, len, key, ivec, (block128_f)SM4_decrypt);Executed by:
| 74 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | (block128_f)SM4_decrypt); executed 74 times by 1 test: CRYPTO_cbc128_decrypt(in, out, len, key, ivec, (block128_f)SM4_decrypt);Executed by:
| 74 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | } | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | - | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | static void sm4_cfb128_encrypt(const unsigned char *in, unsigned char *out, | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | size_t length, const SM4_KEY *key, | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | unsigned char *ivec, int *num, const int enc) | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | { | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | CRYPTO_cfb128_encrypt(in, out, length, key, ivec, num, enc, | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | (block128_f)SM4_encrypt); | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | } executed 118 times by 1 test: end of blockExecuted by:
| 118 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | - | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | static void sm4_ecb_encrypt(const unsigned char *in, unsigned char *out, | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | const SM4_KEY *key, const int enc) | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | { | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | if (enc)
| 405 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | SM4_encrypt(in, out, key); executed 405 times by 1 test: SM4_encrypt(in, out, key);Executed by:
| 405 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | else | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | SM4_decrypt(in, out, key); executed 405 times by 1 test: SM4_decrypt(in, out, key);Executed by:
| 405 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | } | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | - | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | static void sm4_ofb128_encrypt(const unsigned char *in, unsigned char *out, | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | size_t length, const SM4_KEY *key, | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | unsigned char *ivec, int *num) | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | { | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | CRYPTO_ofb128_encrypt(in, out, length, key, ivec, num, | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | (block128_f)SM4_encrypt); | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | } executed 118 times by 1 test: end of blockExecuted by:
| 118 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | - | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | IMPLEMENT_BLOCK_CIPHER(sm4, ks, sm4, EVP_SM4_KEY, NID_sm4, never executed: end of blockexecuted 170 times by 1 test: sm4_cbc_encrypt(in, out, (long)inl, &((EVP_SM4_KEY *)EVP_CIPHER_CTX_get_cipher_data(ctx))->ks, EVP_CIPHER_CTX_iv_noconst(ctx), EVP_CIPHER_CTX_encrypting(ctx));Executed by:
executed 170 times by 1 test: return 1;Executed by:
never executed: chunk >>= 3;executed 118 times by 1 test: chunk = inl;Executed by:
executed 118 times by 1 test: chunk = inl;Executed by:
executed 118 times by 1 test: end of blockExecuted by:
executed 118 times by 1 test: return 1;Executed by:
never executed: return 1;executed 810 times by 1 test: sm4_ecb_encrypt(in + i, out + i, &((EVP_SM4_KEY *)EVP_CIPHER_CTX_get_cipher_data(ctx))->ks, EVP_CIPHER_CTX_encrypting(ctx));Executed by:
executed 155 times by 1 test: return 1;Executed by:
never executed: end of blockexecuted 118 times by 1 test: end of blockExecuted by:
executed 118 times by 1 test: return 1;Executed by:
executed 1962 times by 1 test: return &sm4_cbc;Executed by:
executed 1962 times by 1 test: return &sm4_cfb128;Executed by:
executed 1962 times by 1 test: return &sm4_ofb;Executed by:
executed 1962 times by 1 test: return &sm4_ecb;Executed by:
| 0-1962 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 16, 16, 16, 128, EVP_CIPH_FLAG_DEFAULT_ASN1, | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | sm4_init_key, 0, 0, 0, 0) | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | - | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | static int sm4_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | const unsigned char *in, size_t len) | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | { | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | unsigned int num = EVP_CIPHER_CTX_num(ctx); | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | EVP_SM4_KEY *dat = EVP_C_DATA(EVP_SM4_KEY, ctx); | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | - | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | CRYPTO_ctr128_encrypt(in, out, len, &dat->ks, | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | EVP_CIPHER_CTX_iv_noconst(ctx), | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | EVP_CIPHER_CTX_buf_noconst(ctx), &num, | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | (block128_f)SM4_encrypt); | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | EVP_CIPHER_CTX_set_num(ctx, num); | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | return 1; executed 118 times by 1 test: return 1;Executed by:
| 118 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | } | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | - | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | static const EVP_CIPHER sm4_ctr_mode = { | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | NID_sm4_ctr, 1, 16, 16, | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | EVP_CIPH_CTR_MODE, | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | sm4_init_key, | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | sm4_ctr_cipher, | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | NULL, | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | sizeof(EVP_SM4_KEY), | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | NULL, NULL, NULL, NULL | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | }; | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | - | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | const EVP_CIPHER *EVP_sm4_ctr(void) | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | { | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | return &sm4_ctr_mode; executed 1962 times by 1 test: return &sm4_ctr_mode;Executed by:
| 1962 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | } | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | - | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | #endif | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Source code | Switch to Preprocessed file |