301N November 2018
/
encrypt
15:21 9nov
main.cpp@5:cc8680cda38f, 2018-11-09 (annotated)
- Committer:
- kpan
- Date:
- Fri Nov 09 10:27:44 2018 +0000
- Revision:
- 5:cc8680cda38f
- Parent:
- 4:98332985e06f
- Child:
- 6:13ef02c7dd17
W/ errString;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
preyaa4 | 0:5d1b6c97e2fe | 1 | #include "mbed.h" |
preyaa4 | 0:5d1b6c97e2fe | 2 | #include "mbedtls/pk.h" |
preyaa4 | 0:5d1b6c97e2fe | 3 | #include "mbedtls/ctr_drbg.h" |
kpan | 1:c8f7fe71d151 | 4 | PwmOut r(LED_RED); |
kpan | 1:c8f7fe71d151 | 5 | PwmOut g(LED_GREEN); |
kpan | 1:c8f7fe71d151 | 6 | PwmOut b(LED_BLUE); |
kpan | 3:7a6787f99da2 | 7 | Serial pc(USBTX, USBRX); |
kpan | 3:7a6787f99da2 | 8 | int main() |
kpan | 3:7a6787f99da2 | 9 | { |
kpan | 3:7a6787f99da2 | 10 | pc.printf("INITIALIZING..."); |
kpan | 3:7a6787f99da2 | 11 | wait(1); |
kpan | 3:7a6787f99da2 | 12 | r = g = b = 1; |
kpan | 3:7a6787f99da2 | 13 | g = 0; |
preyaa4 | 0:5d1b6c97e2fe | 14 | int ret = 0; |
kpan | 5:cc8680cda38f | 15 | char errbuf[] = ""; |
preyaa4 | 0:5d1b6c97e2fe | 16 | mbedtls_pk_context pk; |
preyaa4 | 0:5d1b6c97e2fe | 17 | |
preyaa4 | 0:5d1b6c97e2fe | 18 | mbedtls_pk_init( &pk ); |
preyaa4 | 0:5d1b6c97e2fe | 19 | const unsigned char key[]="-----BEGIN RSA PUBLIC KEY-----" |
kpan | 3:7a6787f99da2 | 20 | "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDMYfnvWtC8Id5bPKae5yXSxQTt" |
kpan | 3:7a6787f99da2 | 21 | "+Zpul6AnnZWfI2TtIarvjHBFUtXRo96y7hoL4VWOPKGCsRqMFDkrbeUjRrx8iL91" |
kpan | 3:7a6787f99da2 | 22 | "4/srnyf6sh9c8Zk04xEOpK1ypvBz+Ks4uZObtjnnitf0NBGdjMKxveTq+VE7BWUI" |
kpan | 3:7a6787f99da2 | 23 | "yQjtQ8mbDOsiLLvh7wIDAQAB" |
kpan | 3:7a6787f99da2 | 24 | "-----END RSA PUBLIC KEY-----"; |
kpan | 3:7a6787f99da2 | 25 | |
preyaa4 | 0:5d1b6c97e2fe | 26 | |
preyaa4 | 0:5d1b6c97e2fe | 27 | /* |
preyaa4 | 0:5d1b6c97e2fe | 28 | * Read the RSA public key |
preyaa4 | 0:5d1b6c97e2fe | 29 | */ |
kpan | 3:7a6787f99da2 | 30 | if( ( ret = mbedtls_pk_parse_public_key( &pk, key,sizeof(key)) ) != 0 ) { |
preyaa4 | 0:5d1b6c97e2fe | 31 | printf( " failed\n ! mbedtls_pk_parse_public_key returned -0x%04x\n", -ret ); |
kpan | 1:c8f7fe71d151 | 32 | g = b = 1; |
kpan | 1:c8f7fe71d151 | 33 | r = 0; |
preyaa4 | 0:5d1b6c97e2fe | 34 | goto exit; |
preyaa4 | 0:5d1b6c97e2fe | 35 | } |
kpan | 3:7a6787f99da2 | 36 | |
kpan | 3:7a6787f99da2 | 37 | unsigned char buf[MBEDTLS_MPI_MAX_SIZE]; |
preyaa4 | 0:5d1b6c97e2fe | 38 | size_t olen = 0; |
preyaa4 | 0:5d1b6c97e2fe | 39 | |
preyaa4 | 0:5d1b6c97e2fe | 40 | /* |
preyaa4 | 0:5d1b6c97e2fe | 41 | * Calculate the RSA encryption of the data. |
preyaa4 | 0:5d1b6c97e2fe | 42 | */ |
kpan | 4:98332985e06f | 43 | pc.printf("\n . Generating the encrypted value" ); |
preyaa4 | 0:5d1b6c97e2fe | 44 | fflush( stdout ); |
kpan | 3:7a6787f99da2 | 45 | const unsigned char to_encrypt[] = "HelloWorld"; |
preyaa4 | 0:5d1b6c97e2fe | 46 | mbedtls_ctr_drbg_context ctr_drbg; |
kpan | 3:7a6787f99da2 | 47 | |
preyaa4 | 0:5d1b6c97e2fe | 48 | if( ( ret = mbedtls_pk_encrypt( &pk, to_encrypt, sizeof(to_encrypt), |
preyaa4 | 0:5d1b6c97e2fe | 49 | buf, &olen, sizeof(buf), |
kpan | 3:7a6787f99da2 | 50 | mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 ) { |
kpan | 5:cc8680cda38f | 51 | mbedtls_strerror( ret, errbuf, 200 ); |
kpan | 5:cc8680cda38f | 52 | pc.printf( " failed\n ! mbedtls_pk_encrypt returned -0x%04x\n%s\n", -ret, errbuf ); |
kpan | 5:cc8680cda38f | 53 | mbedtls_printf("Last error was: -0x%04x - %s\n\n", (int) -ret, errbuf ); |
kpan | 5:cc8680cda38f | 54 | |
kpan | 1:c8f7fe71d151 | 55 | r = g = 1; |
kpan | 1:c8f7fe71d151 | 56 | b = 0; |
preyaa4 | 0:5d1b6c97e2fe | 57 | goto exit; |
preyaa4 | 0:5d1b6c97e2fe | 58 | } |
kpan | 3:7a6787f99da2 | 59 | exit: |
preyaa4 | 0:5d1b6c97e2fe | 60 | return 0; |
kpan | 2:cee7ee508f77 | 61 | } |