Embedded src 0004 - JohnHau/mis GitHub Wiki

//build.sh======================================================== #!/bin/bash

gcc -o taes.exe taes.c -L . -lmbedtls -lmbedcrypto -lmbedx509 -I.

//crypt_and_hash.c============================================================== /*

  • \brief Generic file encryption program using generic wrappers for configured
  •      security.
    
  • Copyright The Mbed TLS Contributors
  • SPDX-License-Identifier: Apache-2.0
  • Licensed under the Apache License, Version 2.0 (the "License"); you may
  • not use this file except in compliance with the License.
  • You may obtain a copy of the License at
  • http://www.apache.org/licenses/LICENSE-2.0
  • Unless required by applicable law or agreed to in writing, software
  • distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  • WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  • See the License for the specific language governing permissions and
  • limitations under the License. */

/* Enable definition of fileno() even when compiling with -std=c99. Must be

  • set before mbedtls_config.h, which pulls in glibc's features.h indirectly.
  • Harmless on other platforms. */ #define _POSIX_C_SOURCE 200112L

#include "mbedtls/build_info.h"

#include "mbedtls/platform.h"

#if defined(MBEDTLS_CIPHER_C) && defined(MBEDTLS_MD_C) &&
defined(MBEDTLS_FS_IO) #include "mbedtls/cipher.h" #include "mbedtls/md.h" #include "mbedtls/platform_util.h"

#include <stdio.h> #include <stdlib.h> #include <string.h> #endif

#if defined(_WIN32) #include <windows.h> #if !defined(_WIN32_WCE) #include <io.h> #endif #else #include <sys/types.h> #include <unistd.h> #endif

#define MODE_ENCRYPT 0 #define MODE_DECRYPT 1

#define USAGE
"\n crypt_and_hash <mbedtls_md> \n"
"\n : 0 = encrypt, 1 = decrypt\n"
"\n example: crypt_and_hash 0 file file.aes AES-128-CBC SHA1 hex:E76B2413958B00E193\n"
"\n"

#if !defined(MBEDTLS_CIPHER_C) || !defined(MBEDTLS_MD_C) ||
!defined(MBEDTLS_FS_IO) int main(void) { mbedtls_printf("MBEDTLS_CIPHER_C and/or MBEDTLS_MD_C and/or MBEDTLS_FS_IO not defined.\n"); mbedtls_exit(0); } #else

int main(int argc, char *argv[]) { int ret = 1, i; unsigned n; int exit_code = MBEDTLS_EXIT_FAILURE; int mode; size_t keylen, ilen, olen; FILE *fkey, *fin = NULL, *fout = NULL;

char *p;
unsigned char IV[16];
unsigned char key[512];
unsigned char digest[MBEDTLS_MD_MAX_SIZE];
unsigned char buffer[1024];
unsigned char output[1024];
unsigned char diff;

const mbedtls_cipher_info_t *cipher_info;
const mbedtls_md_info_t *md_info;
mbedtls_cipher_context_t cipher_ctx;
mbedtls_md_context_t md_ctx;

#if defined(_WIN32_WCE) long filesize, offset; #elif defined(_WIN32) LARGE_INTEGER li_size; __int64 filesize, offset; #else off_t filesize, offset; #endif

mbedtls_cipher_init(&cipher_ctx);
mbedtls_md_init(&md_ctx);

/*
 * Parse the command-line arguments.
 */
if (argc != 7) {
    const int *list;

    mbedtls_printf(USAGE);

    mbedtls_printf("Available ciphers:\n");
    list = mbedtls_cipher_list();
    while (*list) {
        cipher_info = mbedtls_cipher_info_from_type(*list);
        mbedtls_printf("  %s\n", mbedtls_cipher_info_get_name(cipher_info));
        list++;
    }

    mbedtls_printf("\nAvailable message digests:\n");
    list = mbedtls_md_list();
    while (*list) {
        md_info = mbedtls_md_info_from_type(*list);
        mbedtls_printf("  %s\n", mbedtls_md_get_name(md_info));
        list++;
    }

    goto exit;
}

mode = atoi(argv[1]);

if (mode != MODE_ENCRYPT && mode != MODE_DECRYPT) {
    mbedtls_fprintf(stderr, "invalid operation mode\n");
    goto exit;
}

if (strcmp(argv[2], argv[3]) == 0) {
    mbedtls_fprintf(stderr, "input and output filenames must differ\n");
    goto exit;
}

if ((fin = fopen(argv[2], "rb")) == NULL) {
    mbedtls_fprintf(stderr, "fopen(%s,rb) failed\n", argv[2]);
    goto exit;
}

if ((fout = fopen(argv[3], "wb+")) == NULL) {
    mbedtls_fprintf(stderr, "fopen(%s,wb+) failed\n", argv[3]);
    goto exit;
}

/* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
mbedtls_setbuf(fin, NULL);
mbedtls_setbuf(fout, NULL);

/*
 * Read the Cipher and MD from the command line
 */
cipher_info = mbedtls_cipher_info_from_string(argv[4]);
if (cipher_info == NULL) {
    mbedtls_fprintf(stderr, "Cipher '%s' not found\n", argv[4]);
    goto exit;
}
if ((ret = mbedtls_cipher_setup(&cipher_ctx, cipher_info)) != 0) {
    mbedtls_fprintf(stderr, "mbedtls_cipher_setup failed\n");
    goto exit;
}

md_info = mbedtls_md_info_from_string(argv[5]);
if (md_info == NULL) {
    mbedtls_fprintf(stderr, "Message Digest '%s' not found\n", argv[5]);
    goto exit;
}

if (mbedtls_md_setup(&md_ctx, md_info, 1) != 0) {
    mbedtls_fprintf(stderr, "mbedtls_md_setup failed\n");
    goto exit;
}

/*
 * Read the secret key from file or command line
 */
if ((fkey = fopen(argv[6], "rb")) != NULL) {
    keylen = fread(key, 1, sizeof(key), fkey);
    fclose(fkey);
} else {
    if (memcmp(argv[6], "hex:", 4) == 0) {
        p = &argv[6][4];
        keylen = 0;

        while (sscanf(p, "%02X", (unsigned int *) &n) > 0 &&
               keylen < (int) sizeof(key)) {
            key[keylen++] = (unsigned char) n;
            p += 2;
        }
    } else {
        keylen = strlen(argv[6]);

        if (keylen > (int) sizeof(key)) {
            keylen = (int) sizeof(key);
        }

        memcpy(key, argv[6], keylen);
    }
}

#if defined(_WIN32_WCE) filesize = fseek(fin, 0L, SEEK_END); #else #if defined(_WIN32) /* * Support large files (> 2Gb) on Win32 */ li_size.QuadPart = 0; li_size.LowPart = SetFilePointer((HANDLE) _get_osfhandle(_fileno(fin)), li_size.LowPart, &li_size.HighPart, FILE_END);

if (li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR) {
    mbedtls_fprintf(stderr, "SetFilePointer(0,FILE_END) failed\n");
    goto exit;
}

filesize = li_size.QuadPart;

#else if ((filesize = lseek(fileno(fin), 0, SEEK_END)) < 0) { perror("lseek"); goto exit; } #endif #endif

if (fseek(fin, 0, SEEK_SET) < 0) {
    mbedtls_fprintf(stderr, "fseek(0,SEEK_SET) failed\n");
    goto exit;
}

if (mode == MODE_ENCRYPT) {
    /*
     * Generate the initialization vector as:
     * IV = MD( filesize || filename )[0..15]
     */
    for (i = 0; i < 8; i++) {
        buffer[i] = (unsigned char) (filesize >> (i << 3));
    }

    p = argv[2];

    if (mbedtls_md_starts(&md_ctx) != 0) {
        mbedtls_fprintf(stderr, "mbedtls_md_starts() returned error\n");
        goto exit;
    }
    if (mbedtls_md_update(&md_ctx, buffer, 8) != 0) {
        mbedtls_fprintf(stderr, "mbedtls_md_update() returned error\n");
        goto exit;
    }
    if (mbedtls_md_update(&md_ctx, (unsigned char *) p, strlen(p))
        != 0) {
        mbedtls_fprintf(stderr, "mbedtls_md_update() returned error\n");
        goto exit;
    }
    if (mbedtls_md_finish(&md_ctx, digest) != 0) {
        mbedtls_fprintf(stderr, "mbedtls_md_finish() returned error\n");
        goto exit;
    }

    memcpy(IV, digest, 16);

    /*
     * Append the IV at the beginning of the output.
     */
    if (fwrite(IV, 1, 16, fout) != 16) {
        mbedtls_fprintf(stderr, "fwrite(%d bytes) failed\n", 16);
        goto exit;
    }

    /*
     * Hash the IV and the secret key together 8192 times
     * using the result to setup the AES context and HMAC.
     */
    memset(digest, 0,  32);
    memcpy(digest, IV, 16);

    for (i = 0; i < 8192; i++) {
        if (mbedtls_md_starts(&md_ctx) != 0) {
            mbedtls_fprintf(stderr,
                            "mbedtls_md_starts() returned error\n");
            goto exit;
        }
        if (mbedtls_md_update(&md_ctx, digest, 32) != 0) {
            mbedtls_fprintf(stderr,
                            "mbedtls_md_update() returned error\n");
            goto exit;
        }
        if (mbedtls_md_update(&md_ctx, key, keylen) != 0) {
            mbedtls_fprintf(stderr,
                            "mbedtls_md_update() returned error\n");
            goto exit;
        }
        if (mbedtls_md_finish(&md_ctx, digest) != 0) {
            mbedtls_fprintf(stderr,
                            "mbedtls_md_finish() returned error\n");
            goto exit;
        }

    }

    if (mbedtls_cipher_setkey(&cipher_ctx,
                              digest,
                              (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
                              MBEDTLS_ENCRYPT) != 0) {
        mbedtls_fprintf(stderr, "mbedtls_cipher_setkey() returned error\n");
        goto exit;
    }
    if (mbedtls_cipher_set_iv(&cipher_ctx, IV, 16) != 0) {
        mbedtls_fprintf(stderr, "mbedtls_cipher_set_iv() returned error\n");
        goto exit;
    }
    if (mbedtls_cipher_reset(&cipher_ctx) != 0) {
        mbedtls_fprintf(stderr, "mbedtls_cipher_reset() returned error\n");
        goto exit;
    }

    if (mbedtls_md_hmac_starts(&md_ctx, digest, 32) != 0) {
        mbedtls_fprintf(stderr, "mbedtls_md_hmac_starts() returned error\n");
        goto exit;
    }

    /*
     * Encrypt and write the ciphertext.
     */
    for (offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size(&cipher_ctx)) {
        ilen = ((unsigned int) filesize - offset > mbedtls_cipher_get_block_size(&cipher_ctx)) ?
               mbedtls_cipher_get_block_size(&cipher_ctx) : (unsigned int) (filesize - offset);

        if (fread(buffer, 1, ilen, fin) != ilen) {
            mbedtls_fprintf(stderr, "fread(%ld bytes) failed\n", (long) ilen);
            goto exit;
        }

        if (mbedtls_cipher_update(&cipher_ctx, buffer, ilen, output, &olen) != 0) {
            mbedtls_fprintf(stderr, "mbedtls_cipher_update() returned error\n");
            goto exit;
        }

        if (mbedtls_md_hmac_update(&md_ctx, output, olen) != 0) {
            mbedtls_fprintf(stderr, "mbedtls_md_hmac_update() returned error\n");
            goto exit;
        }

        if (fwrite(output, 1, olen, fout) != olen) {
            mbedtls_fprintf(stderr, "fwrite(%ld bytes) failed\n", (long) olen);
            goto exit;
        }
    }

    if (mbedtls_cipher_finish(&cipher_ctx, output, &olen) != 0) {
        mbedtls_fprintf(stderr, "mbedtls_cipher_finish() returned error\n");
        goto exit;
    }
    if (mbedtls_md_hmac_update(&md_ctx, output, olen) != 0) {
        mbedtls_fprintf(stderr, "mbedtls_md_hmac_update() returned error\n");
        goto exit;
    }

    if (fwrite(output, 1, olen, fout) != olen) {
        mbedtls_fprintf(stderr, "fwrite(%ld bytes) failed\n", (long) olen);
        goto exit;
    }

    /*
     * Finally write the HMAC.
     */
    if (mbedtls_md_hmac_finish(&md_ctx, digest) != 0) {
        mbedtls_fprintf(stderr, "mbedtls_md_hmac_finish() returned error\n");
        goto exit;
    }

    if (fwrite(digest, 1, mbedtls_md_get_size(md_info), fout) != mbedtls_md_get_size(md_info)) {
        mbedtls_fprintf(stderr, "fwrite(%d bytes) failed\n", mbedtls_md_get_size(md_info));
        goto exit;
    }
}

if (mode == MODE_DECRYPT) {
    /*
     *  The encrypted file must be structured as follows:
     *
     *        00 .. 15              Initialization Vector
     *        16 .. 31              Encrypted Block #1
     *           ..
     *      N*16 .. (N+1)*16 - 1    Encrypted Block #N
     *  (N+1)*16 .. (N+1)*16 + n    Hash(ciphertext)
     */
    if (filesize < 16 + mbedtls_md_get_size(md_info)) {
        mbedtls_fprintf(stderr, "File too short to be encrypted.\n");
        goto exit;
    }

    if (mbedtls_cipher_get_block_size(&cipher_ctx) == 0) {
        mbedtls_fprintf(stderr, "Invalid cipher block size: 0. \n");
        goto exit;
    }

    /*
     * Check the file size.
     */
    if (mbedtls_cipher_info_get_mode(cipher_info) != MBEDTLS_MODE_GCM &&
        ((filesize - mbedtls_md_get_size(md_info)) %
         mbedtls_cipher_get_block_size(&cipher_ctx)) != 0) {
        mbedtls_fprintf(stderr, "File content not a multiple of the block size (%u).\n",
                        mbedtls_cipher_get_block_size(&cipher_ctx));
        goto exit;
    }

    /*
     * Subtract the IV + HMAC length.
     */
    filesize -= (16 + mbedtls_md_get_size(md_info));

    /*
     * Read the IV and original filesize modulo 16.
     */
    if (fread(buffer, 1, 16, fin) != 16) {
        mbedtls_fprintf(stderr, "fread(%d bytes) failed\n", 16);
        goto exit;
    }

    memcpy(IV, buffer, 16);

    /*
     * Hash the IV and the secret key together 8192 times
     * using the result to setup the AES context and HMAC.
     */
    memset(digest, 0,  32);
    memcpy(digest, IV, 16);

    for (i = 0; i < 8192; i++) {
        if (mbedtls_md_starts(&md_ctx) != 0) {
            mbedtls_fprintf(stderr, "mbedtls_md_starts() returned error\n");
            goto exit;
        }
        if (mbedtls_md_update(&md_ctx, digest, 32) != 0) {
            mbedtls_fprintf(stderr, "mbedtls_md_update() returned error\n");
            goto exit;
        }
        if (mbedtls_md_update(&md_ctx, key, keylen) != 0) {
            mbedtls_fprintf(stderr, "mbedtls_md_update() returned error\n");
            goto exit;
        }
        if (mbedtls_md_finish(&md_ctx, digest) != 0) {
            mbedtls_fprintf(stderr, "mbedtls_md_finish() returned error\n");
            goto exit;
        }
    }

    if (mbedtls_cipher_setkey(&cipher_ctx,
                              digest,
                              (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
                              MBEDTLS_DECRYPT) != 0) {
        mbedtls_fprintf(stderr, "mbedtls_cipher_setkey() returned error\n");
        goto exit;
    }

    if (mbedtls_cipher_set_iv(&cipher_ctx, IV, 16) != 0) {
        mbedtls_fprintf(stderr, "mbedtls_cipher_set_iv() returned error\n");
        goto exit;
    }

    if (mbedtls_cipher_reset(&cipher_ctx) != 0) {
        mbedtls_fprintf(stderr, "mbedtls_cipher_reset() returned error\n");
        goto exit;
    }

    if (mbedtls_md_hmac_starts(&md_ctx, digest, 32) != 0) {
        mbedtls_fprintf(stderr, "mbedtls_md_hmac_starts() returned error\n");
        goto exit;
    }

    /*
     * Decrypt and write the plaintext.
     */
    for (offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size(&cipher_ctx)) {
        ilen = ((unsigned int) filesize - offset > mbedtls_cipher_get_block_size(&cipher_ctx)) ?
               mbedtls_cipher_get_block_size(&cipher_ctx) : (unsigned int) (filesize - offset);

        if (fread(buffer, 1, ilen, fin) != ilen) {
            mbedtls_fprintf(stderr, "fread(%u bytes) failed\n",
                            mbedtls_cipher_get_block_size(&cipher_ctx));
            goto exit;
        }

        if (mbedtls_md_hmac_update(&md_ctx, buffer, ilen) != 0) {
            mbedtls_fprintf(stderr, "mbedtls_md_hmac_update() returned error\n");
            goto exit;
        }
        if (mbedtls_cipher_update(&cipher_ctx, buffer, ilen, output,
                                  &olen) != 0) {
            mbedtls_fprintf(stderr, "mbedtls_cipher_update() returned error\n");
            goto exit;
        }

        if (fwrite(output, 1, olen, fout) != olen) {
            mbedtls_fprintf(stderr, "fwrite(%ld bytes) failed\n", (long) olen);
            goto exit;
        }
    }

    /*
     * Verify the message authentication code.
     */
    if (mbedtls_md_hmac_finish(&md_ctx, digest) != 0) {
        mbedtls_fprintf(stderr, "mbedtls_md_hmac_finish() returned error\n");
        goto exit;
    }

    if (fread(buffer, 1, mbedtls_md_get_size(md_info), fin) != mbedtls_md_get_size(md_info)) {
        mbedtls_fprintf(stderr, "fread(%d bytes) failed\n", mbedtls_md_get_size(md_info));
        goto exit;
    }

    /* Use constant-time buffer comparison */
    diff = 0;
    for (i = 0; i < mbedtls_md_get_size(md_info); i++) {
        diff |= digest[i] ^ buffer[i];
    }

    if (diff != 0) {
        mbedtls_fprintf(stderr, "HMAC check failed: wrong key, "
                                "or file corrupted.\n");
        goto exit;
    }

    /*
     * Write the final block of data
     */
    if (mbedtls_cipher_finish(&cipher_ctx, output, &olen) != 0) {
        mbedtls_fprintf(stderr, "mbedtls_cipher_finish() returned error\n");
        goto exit;
    }

    if (fwrite(output, 1, olen, fout) != olen) {
        mbedtls_fprintf(stderr, "fwrite(%ld bytes) failed\n", (long) olen);
        goto exit;
    }
}

exit_code = MBEDTLS_EXIT_SUCCESS;

exit: if (fin) { fclose(fin); } if (fout) { fclose(fout); }

/* Zeroize all command line arguments to also cover
   the case when the user has missed or reordered some,
   in which case the key might not be in argv[6]. */
for (i = 0; i < argc; i++) {
    mbedtls_platform_zeroize(argv[i], strlen(argv[i]));
}

mbedtls_platform_zeroize(IV,     sizeof(IV));
mbedtls_platform_zeroize(key,    sizeof(key));
mbedtls_platform_zeroize(buffer, sizeof(buffer));
mbedtls_platform_zeroize(output, sizeof(output));
mbedtls_platform_zeroize(digest, sizeof(digest));

mbedtls_cipher_free(&cipher_ctx);
mbedtls_md_free(&md_ctx);

mbedtls_exit(exit_code);

}

void jh_aes_encrypt_cbc() {

}

#endif /* MBEDTLS_CIPHER_C && MBEDTLS_MD_C && MBEDTLS_FS_IO */

//loop_queue.c======================================================================

#include<stdio.h> #include<string.h> #include<sys/types.h> #include<sys/stat.h> #include<unistd.h>

typedef struct {

uint8_t* const buffer;
int head;
int tail;
const int maxLen;

}circBuf_t;

#define BLE_CircularBuffSize 10//1000

uint8_t dataBuff[BLE_CircularBuffSize] = {0}; circBuf_t circularBuffer = {&dataBuff[0],0,0,BLE_CircularBuffSize};

int circBufPush(circBuf_t *c, uint8_t data) { int next = c->head + 1; if(next >= c->maxLen) next =0;

//Circular buffer is full
if(next == c->tail)
	return -1;

c->buffer[c->head] = data;
c->head = next;

return 0;

}

int circBufPop(circBuf_t *c, uint8_t *data) { //if the head isn't ahead of the tail,we dont have any characters if(c->head == c->tail) return -1;

*data = c->buffer[c->tail];
//c->buffer[c->tail] = 0;//clear the data (optional)

int next = c->tail +1;
if(next >= c->maxLen)
	next =0;

c->tail = next;

return 0;

}

int circBuf_read_all(circBuf_t *c, uint8_t *data) { int i=0; while(circBufPop(c,data+i) != -1)//reach tail { if(i >= BLE_CircularBuffSize) { return -1; }

}

return i;

}

int main(int argc,char* argv[]) { uint8_t testd[BLE_CircularBuffSize] = {0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};

uint8_t teste[BLE_CircularBuffSize] = 
            {0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69};
circBuf_t* ptr_circBuf = &circularBuffer; 

for(int i=0; i< BLE_CircularBuffSize;i++)
{
	if(circBufPush(ptr_circBuf,testd[i]) == -1)
	{
		printf("circular buffer is full. and i is %d\n",i);

	}
}



for(int i=0; i< BLE_CircularBuffSize;i++)
{

// printf("dataBuff[%d] = %02x\n",i,dataBuff[i]); }

int cnt=0;
while(1)
{
	uint8_t datav=0;
	int rv =  circBufPop(ptr_circBuf, &datav);
	if(rv == -1)
	{
		printf("queue is empty and quit\n");
		break;
	}
	else
	{
		printf("datav[%d] = %02x\n",cnt++,datav);

	}
}

return 0;

}

//openssl_aes_128.sh==================================================================

#!/bin/bash

echo -n -e '\x65\x66\x67\x33\x34\x35\x36\x37\x30\x31\x32\x33\x34\x35\x36\x37'
| openssl enc -v -aes-128-cbc -K 71727374757677786162636465666768
-iv 33333333333333333333333333333333 -nopad| od -Ax -tx1

//openssl_aes_128x.sh======================================================

#!/bin/bash

input='\x65\x66\x67\x33\x34\x35\x36\x37\x30\x31\x32\x33\x34\x35\x36\x37\x51\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

inputx='\x65\x66\x67\x33\x34\x35\x36\x37\x30\x31\x32\x33\x34\x35\x36\x37\x51' echo -n -e $inputx > inputx.log

echo -n -e $input
| openssl enc -v -aes-128-cbc -K 71727374757677786162636465666768
-iv 33333333333333333333333333333333 -nopad| od -Ax -tx1

//openssl_aes_128xy.sh============================================================ #!/bin/bash

inputx='\x65\x66\x67\x33\x34\x35\x36\x37\x30\x31\x32\x33\x34\x35\x36\x37\x51' #echo -n -e $inputx > inputx.log

openssl enc -v -aes-128-cbc -in inputx.log -out outputx.log -K 71727374757677786162636465666768 -iv 33333333333333333333333333333333 | od -Ax -tx1

//openssl_aes_128xy.sh=====================================================

#!/bin/bash

inputx='\x65\x66\x67\x33\x34\x35\x36\x37\x30\x31\x32\x33\x34\x35\x36\x37\x51' #echo -n -e $inputx > inputx.log

openssl enc -v -aes-128-cbc -in inputx.log -out outputx.log -K 71727374757677786162636465666768 -iv 33333333333333333333333333333333 | od -Ax -tx1

//taes.c======================================================================= #include<stdio.h> #include<string.h> #include"mbedtls/aes.h" #include"mbedtls/sha256.h" #include"mbedtls/gcm.h"

int main(int argc, char *argv[]) {

//unsigned char *key128="abcdef1234567890";
unsigned char key128[16]={0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,
                              0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68};

//unsigned char *iv="3333333333333333";
unsigned char iv[16]={0x33,0x33,0x33,0x33,0x33,0x33,0x33,0x33,
                         0x33,0x33,0x33,0x33,0x33,0x33,0x33,0x33};

unsigned char input[16+16]={

                             0x65,0x66,0x67,0x33,0x34,0x35,0x36,0x37,
                             0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,

			 0x51,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,
			 0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F
                        };

unsigned char output[100];

mbedtls_aes_context ctx;

memset(output,0x00,100);

mbedtls_aes_init(&ctx);

if(mbedtls_aes_setkey_enc(&ctx,key128,128) == 0)
{
	//mbedtls_aes_crypt_cbc(&ctx,MBEDTLS_AES_ENCRYPT,16+16,iv,input,output);
	mbedtls_aes_crypt_cbc(&ctx,MBEDTLS_AES_ENCRYPT,16,iv,input,output);

	for(int i=0;i<16;i++)
	{
		printf("output[i]= %x\n",output[i]);

	}
}



unsigned char* sha256_input = "hello world";
unsigned char  sha256_output[32] = {0};

mbedtls_sha256_context sha256_ctx;
mbedtls_sha256_init(&sha256_ctx);

mbedtls_sha256_starts(&sha256_ctx,0);
mbedtls_sha256_update(&sha256_ctx,sha256_input,strlen(sha256_input));
mbedtls_sha256_finish(&sha256_ctx,sha256_output);
mbedtls_sha256_free(&sha256_ctx);

for(int i=0;i<32;i++)
{
	printf("hash[%d] = %02x\n",i,sha256_output[i]);

}

//====================================== //aes128 gcm //====================================== unsigned char gcm_output[128]; unsigned char tag_output[16]; mbedtls_gcm_context gcm_ctx;

mbedtls_gcm_init(&gcm_ctx);

memset(gcm_output,0x00,128); memset(tag_output,0x00,16);

mbedtls_gcm_setkey(&gcm_ctx,MBEDTLS_CIPHER_ID_AES,key128,128); mbedtls_gcm_crypt_and_tag(&gcm_ctx,MBEDTLS_GCM_ENCRYPT,16,iv,12,NULL,0,input,gcm_output,16,tag_output); mbedtls_gcm_free(&gcm_ctx);

for(int i=0;i<16;i++) { printf("gcm_out[%d]= %02x\n",i,gcm_output[i]); }

for(int i=0;i<16;i++) { printf("tag_out[%d]= %02x\n",i,tag_output[i]); }

unsigned char xgcm_output[128]; unsigned char xtag_output[16];

memset(xgcm_output,0x00,128); memset(xtag_output,0x00,16);

mbedtls_gcm_setkey(&gcm_ctx,MBEDTLS_CIPHER_ID_AES,key128,128); mbedtls_gcm_crypt_and_tag(&gcm_ctx,MBEDTLS_GCM_DECRYPT,16,iv,12,NULL,0,gcm_output,xgcm_output,16,xtag_output); mbedtls_gcm_free(&gcm_ctx);

for(int i=0;i<16;i++) { printf("xgcm_out[%d]= %02x\n",i,xgcm_output[i]); }

for(int i=0;i<16;i++) { printf("xtag_out[%d]= %02x\n",i,xtag_output[i]); }

return 0;

}

⚠️ **GitHub.com Fallback** ⚠️