developer | 7043679 | 2023-05-19 09:31:20 +0800 | [diff] [blame] | 1 | #include "aesccm.h" |
| 2 | |
| 3 | |
| 4 | struct operator ccm_oper = { |
| 5 | .init = ccm_init, |
| 6 | .uninit = ccm_uninit, |
| 7 | .encrypt = ccm_encrypt, |
| 8 | .decrypt = ccm_decrypt, |
| 9 | .check = ccm_check, |
| 10 | }; |
| 11 | |
| 12 | static struct ccm_data data; |
| 13 | |
| 14 | int ccm_init(void) |
| 15 | { |
| 16 | hex2bin(&data.key, input_data.key, &(data.key_size)); |
| 17 | hex2bin(&data.nonce, input_data.nonce, &(data.nonce_size)); |
| 18 | hex2bin(&data.adata, input_data.adata, &(data.adata_size)); |
| 19 | hex2bin(&data.payload, input_data.payload, &(data.payload_size)); |
| 20 | hex2bin(&data.ct, input_data.ct, &(data.ct_size)); |
| 21 | if (input_data.oper == DECRYPT) { |
| 22 | hex2bin(&data.tag, input_data.tag, &(data.tag_size)); |
| 23 | } else if (input_data.oper == ENCRYPT) { |
| 24 | if (input_data.tag != NULL) |
| 25 | data.tag_size = atoi(input_data.tag); |
| 26 | else |
| 27 | data.tag_size = 16; |
| 28 | } |
| 29 | return 0; |
| 30 | } |
| 31 | |
| 32 | void init_ccm_evp_encrypt(EVP_CIPHER_CTX **ctx) |
| 33 | { |
| 34 | if (data.key_size == 16) |
| 35 | EVP_EncryptInit_ex(*ctx, EVP_aes_128_ccm(), NULL, NULL, NULL); |
| 36 | else if (data.key_size == 24) |
| 37 | EVP_EncryptInit_ex(*ctx, EVP_aes_192_ccm(), NULL, NULL, NULL); |
| 38 | else if (data.key_size == 32) |
| 39 | EVP_EncryptInit_ex(*ctx, EVP_aes_256_ccm(), NULL, NULL, NULL); |
| 40 | } |
| 41 | |
| 42 | void init_ccm_evp_decrypt(EVP_CIPHER_CTX **ctx) |
| 43 | { |
| 44 | if (data.key_size == 16) |
| 45 | EVP_DecryptInit_ex(*ctx, EVP_aes_128_ccm(), NULL, NULL, NULL); |
| 46 | else if (data.key_size == 24) |
| 47 | EVP_DecryptInit_ex(*ctx, EVP_aes_192_ccm(), NULL, NULL, NULL); |
| 48 | else if (data.key_size == 32) |
| 49 | EVP_DecryptInit_ex(*ctx, EVP_aes_256_ccm(), NULL, NULL, NULL); |
| 50 | } |
| 51 | int ccm_encrypt(void) |
| 52 | { |
| 53 | EVP_CIPHER_CTX *ctx; |
| 54 | int outlen; |
| 55 | unsigned char outbuf[1024]; |
| 56 | |
| 57 | ctx = EVP_CIPHER_CTX_new(); |
| 58 | init_ccm_evp_encrypt(&ctx); |
| 59 | |
| 60 | EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, data.nonce_size, NULL); |
| 61 | EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, data.tag_size, NULL); |
| 62 | EVP_EncryptInit_ex(ctx, NULL, NULL, data.key, data.nonce); |
| 63 | |
| 64 | EVP_EncryptUpdate(ctx, NULL, &outlen, NULL, data.payload_size); |
| 65 | if (data.adata != NULL) |
| 66 | EVP_EncryptUpdate(ctx, NULL, &outlen, data.adata, data.adata_size); |
| 67 | if (data.payload != NULL) { |
| 68 | EVP_EncryptUpdate(ctx, outbuf, &outlen, data.payload, data.payload_size); |
| 69 | printf("Ciphertext: "); |
| 70 | print_hex(outbuf, outlen); |
developer | 2d63632 | 2023-11-23 11:25:13 +0800 | [diff] [blame^] | 71 | } else { |
| 72 | EVP_EncryptUpdate(ctx, outbuf, &outlen, "", data.payload_size); |
developer | 7043679 | 2023-05-19 09:31:20 +0800 | [diff] [blame] | 73 | } |
developer | 2d63632 | 2023-11-23 11:25:13 +0800 | [diff] [blame^] | 74 | |
developer | 7043679 | 2023-05-19 09:31:20 +0800 | [diff] [blame] | 75 | EVP_EncryptFinal_ex(ctx, outbuf, &outlen); |
| 76 | EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, data.tag_size, outbuf); |
| 77 | printf("Tags: "); |
| 78 | print_hex(outbuf, data.tag_size); |
| 79 | EVP_CIPHER_CTX_free(ctx); |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | int ccm_decrypt(void) |
| 84 | { |
| 85 | EVP_CIPHER_CTX *ctx; |
| 86 | int outlen, rv; |
| 87 | unsigned char outbuf[1024]; |
| 88 | |
| 89 | ctx = EVP_CIPHER_CTX_new(); |
| 90 | |
| 91 | init_ccm_evp_decrypt(&ctx); |
| 92 | |
| 93 | EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, data.nonce_size, NULL); |
| 94 | EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, data.tag_size, data.tag); |
| 95 | EVP_DecryptInit_ex(ctx, NULL, NULL, data.key, data.nonce); |
| 96 | EVP_DecryptUpdate(ctx, NULL, &outlen, NULL, data.ct_size); |
| 97 | |
| 98 | if (data.adata != NULL) |
| 99 | EVP_DecryptUpdate(ctx, NULL, &outlen, data.adata, data.adata_size); |
| 100 | |
| 101 | if (data.ct == NULL) |
| 102 | rv = EVP_DecryptUpdate(ctx, outbuf, &outlen, "", data.ct_size); |
| 103 | else |
| 104 | rv = EVP_DecryptUpdate(ctx, outbuf, &outlen, data.ct, data.ct_size); |
| 105 | |
| 106 | if (rv > 0) { |
| 107 | printf("Tag Verify: Pass\n"); |
| 108 | if (data.ct != NULL) { |
| 109 | printf("Plaintext: "); |
| 110 | print_hex(outbuf, outlen); |
| 111 | } |
| 112 | } else { |
| 113 | printf("Tag Verify: Fail\n"); |
| 114 | } |
| 115 | |
| 116 | EVP_CIPHER_CTX_free(ctx); |
| 117 | return 0; |
| 118 | } |
| 119 | int ccm_uninit(void) |
| 120 | { |
| 121 | free_openssl_data(data.key); |
| 122 | free_openssl_data(data.nonce); |
| 123 | free_openssl_data(data.adata); |
| 124 | free_openssl_data(data.payload); |
| 125 | free_openssl_data(data.ct); |
| 126 | free_openssl_data(data.tag); |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | int ccm_check(void) |
| 131 | { |
| 132 | if (data.key == NULL || data.nonce == NULL) { |
| 133 | printf("ccm must have Key and IV\n"); |
| 134 | return -1; |
| 135 | } |
| 136 | |
| 137 | if (input_data.oper == DECRYPT && data.tag == NULL) { |
| 138 | printf("ccm decrypt must have Tag\n"); |
| 139 | return -1; |
| 140 | } |
| 141 | |
| 142 | return 0; |
| 143 | } |