~increscent/aes-ccm

aes-ccm/encrypt.c -rw-r--r-- 2.5 KiB
6fdb84d8 — Robert Williams Found the issue: the msg_len parameter should be 16-bit, but it's 8-bit 4 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <stdio.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <string.h>
#include <stdlib.h>

#include "lib.h"

int main(int argc, char **argv)
{
    EVP_CIPHER_CTX *ctx;

    int len;

    int ciphertext_len;
    unsigned char ciphertext[1024];

    int plaintext_len = sizeof(PLAINTEXT);
    if (argc == 2) {
        plaintext_len = atoi(argv[1]);
    }

    hexToBytes(KEY_STR, strlen(KEY_STR), KEY);
    hexToBytes(IV_STR, strlen(IV_STR), IV);
    hexToBytes(PLAINTEXT_STR, strlen(PLAINTEXT_STR), PLAINTEXT);
    hexToBytes(CIPHERTEXT_STR, strlen(CIPHERTEXT_STR), CIPHERTEXT);
    hexToBytes(MIC_STR, strlen(MIC_STR), MIC);

    /* Create and initialise the context */
    if(!(ctx = EVP_CIPHER_CTX_new()))
        handleErrors("0");

    /* Initialise the encryption operation. */
    if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_128_ccm(), NULL, NULL, NULL))
        handleErrors("1");

    if(1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_L, 15 - sizeof(IV), NULL))
        handleErrors("2");

    if(1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_IVLEN, sizeof(IV), NULL))
        handleErrors("3");

    /* Set tag length */
    EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_TAG, sizeof(MIC), NULL);

    /* Initialise key and IV */
    if(1 != EVP_EncryptInit_ex(ctx, NULL, NULL, KEY, IV))
        handleErrors("4");

    /* Provide the total plaintext length */
    if(1 != EVP_EncryptUpdate(ctx, NULL, &len, NULL, plaintext_len))
        handleErrors("5");

//    /* Provide any AAD data. This can be called zero or one times as required */
//    if(1 != EVP_EncryptUpdate(ctx, NULL, &len, aad, aad_len))
//        handleErrors();

    /*
     * Provide the message to be encrypted, and obtain the encrypted output.
     * EVP_EncryptUpdate can only be called once for this.
     */
    if(1 != EVP_EncryptUpdate(ctx, ciphertext, &len, PLAINTEXT, plaintext_len))
        handleErrors("6");
    ciphertext_len = len;

    /*
     * Finalise the encryption. Normally ciphertext bytes may be written at
     * this stage, but this does not occur in CCM mode.
     */
    if(1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len))
        handleErrors("7");
    ciphertext_len += len;

    /* Get the tag */
    if(1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_GET_TAG, sizeof(MIC), MIC))
        handleErrors("8");

    /* Clean up */
    EVP_CIPHER_CTX_free(ctx);

    printHex(MIC, sizeof(MIC));
    printf("len: %d\n", ciphertext_len);
    printHex(ciphertext, ciphertext_len);

    return ciphertext_len;
}