![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
15:21 9nov
main.cpp@1:c8f7fe71d151, 2018-11-09 (annotated)
- Committer:
- kpan
- Date:
- Fri Nov 09 09:56:53 2018 +0000
- Revision:
- 1:c8f7fe71d151
- Parent:
- 0:5d1b6c97e2fe
- Child:
- 2:cee7ee508f77
LED out; ;
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" |
preyaa4 | 0:5d1b6c97e2fe | 4 | |
kpan | 1:c8f7fe71d151 | 5 | PwmOut r(LED_RED); |
kpan | 1:c8f7fe71d151 | 6 | PwmOut g(LED_GREEN); |
kpan | 1:c8f7fe71d151 | 7 | PwmOut b(LED_BLUE); |
preyaa4 | 0:5d1b6c97e2fe | 8 | |
kpan | 1:c8f7fe71d151 | 9 | r = g = b = 1; |
kpan | 1:c8f7fe71d151 | 10 | g = 0; |
preyaa4 | 0:5d1b6c97e2fe | 11 | int main(){ |
preyaa4 | 0:5d1b6c97e2fe | 12 | int ret = 0; |
preyaa4 | 0:5d1b6c97e2fe | 13 | mbedtls_pk_context pk; |
preyaa4 | 0:5d1b6c97e2fe | 14 | |
preyaa4 | 0:5d1b6c97e2fe | 15 | mbedtls_pk_init( &pk ); |
preyaa4 | 0:5d1b6c97e2fe | 16 | const unsigned char key[]="-----BEGIN RSA PUBLIC KEY-----" |
preyaa4 | 0:5d1b6c97e2fe | 17 | "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDMYfnvWtC8Id5bPKae5yXSxQTt" |
preyaa4 | 0:5d1b6c97e2fe | 18 | "+Zpul6AnnZWfI2TtIarvjHBFUtXRo96y7hoL4VWOPKGCsRqMFDkrbeUjRrx8iL91" |
preyaa4 | 0:5d1b6c97e2fe | 19 | "4/srnyf6sh9c8Zk04xEOpK1ypvBz+Ks4uZObtjnnitf0NBGdjMKxveTq+VE7BWUI" |
preyaa4 | 0:5d1b6c97e2fe | 20 | "yQjtQ8mbDOsiLLvh7wIDAQAB" |
preyaa4 | 0:5d1b6c97e2fe | 21 | "-----END RSA PUBLIC KEY-----"; |
preyaa4 | 0:5d1b6c97e2fe | 22 | |
preyaa4 | 0:5d1b6c97e2fe | 23 | |
preyaa4 | 0:5d1b6c97e2fe | 24 | /* |
preyaa4 | 0:5d1b6c97e2fe | 25 | * Read the RSA public key |
preyaa4 | 0:5d1b6c97e2fe | 26 | */ |
preyaa4 | 0:5d1b6c97e2fe | 27 | if( ( ret = mbedtls_pk_parse_public_key( &pk, key,sizeof(key)) ) != 0 ) |
preyaa4 | 0:5d1b6c97e2fe | 28 | { |
preyaa4 | 0:5d1b6c97e2fe | 29 | printf( " failed\n ! mbedtls_pk_parse_public_key returned -0x%04x\n", -ret ); |
kpan | 1:c8f7fe71d151 | 30 | g = b = 1; |
kpan | 1:c8f7fe71d151 | 31 | r = 0; |
preyaa4 | 0:5d1b6c97e2fe | 32 | goto exit; |
preyaa4 | 0:5d1b6c97e2fe | 33 | } |
preyaa4 | 0:5d1b6c97e2fe | 34 | |
preyaa4 | 0:5d1b6c97e2fe | 35 | unsigned char buf[MBEDTLS_MPI_MAX_SIZE]; |
preyaa4 | 0:5d1b6c97e2fe | 36 | size_t olen = 0; |
preyaa4 | 0:5d1b6c97e2fe | 37 | |
preyaa4 | 0:5d1b6c97e2fe | 38 | /* |
preyaa4 | 0:5d1b6c97e2fe | 39 | * Calculate the RSA encryption of the data. |
preyaa4 | 0:5d1b6c97e2fe | 40 | */ |
preyaa4 | 0:5d1b6c97e2fe | 41 | printf( "\n . Generating the encrypted value" ); |
preyaa4 | 0:5d1b6c97e2fe | 42 | fflush( stdout ); |
preyaa4 | 0:5d1b6c97e2fe | 43 | const unsigned char to_encrypt[] ="HelloWorld"; |
preyaa4 | 0:5d1b6c97e2fe | 44 | mbedtls_ctr_drbg_context ctr_drbg; |
preyaa4 | 0:5d1b6c97e2fe | 45 | |
preyaa4 | 0:5d1b6c97e2fe | 46 | if( ( ret = mbedtls_pk_encrypt( &pk, to_encrypt, sizeof(to_encrypt), |
preyaa4 | 0:5d1b6c97e2fe | 47 | buf, &olen, sizeof(buf), |
preyaa4 | 0:5d1b6c97e2fe | 48 | mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 ) |
preyaa4 | 0:5d1b6c97e2fe | 49 | { |
preyaa4 | 0:5d1b6c97e2fe | 50 | printf( " failed\n ! mbedtls_pk_encrypt returned -0x%04x\n", -ret ); |
kpan | 1:c8f7fe71d151 | 51 | r = g = 1; |
kpan | 1:c8f7fe71d151 | 52 | b = 0; |
preyaa4 | 0:5d1b6c97e2fe | 53 | goto exit; |
preyaa4 | 0:5d1b6c97e2fe | 54 | } |
preyaa4 | 0:5d1b6c97e2fe | 55 | exit: |
preyaa4 | 0:5d1b6c97e2fe | 56 | return 0; |
preyaa4 | 0:5d1b6c97e2fe | 57 | } |