1 /**************************************************************************/
5 modified: microBuilder.eu
9 Basic RSA-encryption using 64-bit math (32-bit keys).
11 Based on the examples from "Mastering Algorithms with C" by
12 Kyle Loudon (O'Reilly, 1999).
14 For details on how to generate a valid RSA key pair, see:
15 http://www.microbuilder.eu/Tutorials/SoftwareDevelopment/RSAEncryption.aspx
17 @warning Most versions of libc used for embedded systems do not
18 include support for 64-bit integers with printf, etc. (to
19 keep the compiled code size and memory usage as small as
20 possible). Unless you have explicitly added long long
21 support for printf, you should not try to display 64-bit
22 values with it (%lld, etc.). Using 32-bit values (changing
23 the definition of huge_t to uint32_t) will avoid this issue
24 entirely, though at the expense of weaker encryption.
26 /**************************************************************************/
30 huge_t
modexp(huge_t a
, huge_t b
, huge_t n
)
35 /* Compute pow(a, b) % n using the binary square and multiply method. */
38 /* For each 1 in b, accumulate y. */
44 /* Square a for each bit in b. */
47 /* Prepare for the next bit in b. */
56 huge_t rsaOrig
, rsaDecrypted
, rsaEncrypted
;
57 rsaPubKey_t publicKey
;
58 rsaPriKey_t privateKey
;
61 printf("Encrypting with RSA %s", CFG_PRINTF_NEWLINE
);
63 #if CFG_RSA_BITS == 64
64 // Values based on 64-bit math (huge_t = uint64_t)
65 // which will result in more secure encryption, but also
66 // increases the size of the encrypted text
68 publicKey
.n
= 16484947;
69 privateKey
.d
= 15689981;
70 privateKey
.n
= 16484947;
73 #if CFG_RSA_BITS == 32
74 // Alternative values with 32-bit math (huge_t = uint32_t)
75 // or when smaller encrypted text is desired
82 #if CFG_RSA_BITS == 64
83 printf("Starting RSA encryption/decryption test %s", CFG_PRINTF_NEWLINE
);
85 #if CFG_RSA_BITS == 32
86 printf("d=%u, e=%u, n=%u %s", (unsigned int)privateKey
.d
, (unsigned int)publicKey
.e
, (unsigned int)publicKey
.n
, CFG_PRINTF_NEWLINE
);
89 for (i
= 0; i
< 128; i
++)
92 rsaEncrypt(rsaOrig
, &rsaEncrypted
, publicKey
);
93 rsaDecrypt(rsaEncrypted
, &rsaDecrypted
, privateKey
);
95 if (rsaOrig
== rsaDecrypted
)
97 #if CFG_RSA_BITS == 64
98 printf("Encrypted and decrypted %d %s", i
, CFG_PRINTF_NEWLINE
);
100 #if CFG_RSA_BITS == 32
101 printf("In=%5u, Encrypted=%5u, Out=%5u (OK) %s", (unsigned int)rsaOrig
, (unsigned int)rsaEncrypted
, (unsigned int)rsaDecrypted
, CFG_PRINTF_NEWLINE
);
106 #if CFG_RSA_BITS == 64
107 printf("Failed to decrypt %d %s", i
, CFG_PRINTF_NEWLINE
);
109 #if CFG_RSA_BITS == 32
110 printf("In=%5u, Encrypted=%5u, Out=%5u (ERROR) %s", (unsigned int)rsaOrig
, (unsigned int)rsaEncrypted
, (unsigned int)rsaDecrypted
, CFG_PRINTF_NEWLINE
);
116 void rsaEncrypt(huge_t plaintext
, huge_t
*ciphertext
, rsaPubKey_t pubkey
)
118 *ciphertext
= modexp(plaintext
, pubkey
.e
, pubkey
.n
);
123 void rsaDecrypt(huge_t ciphertext
, huge_t
*plaintext
, rsaPriKey_t prikey
)
125 *plaintext
= modexp(ciphertext
, prikey
.d
, prikey
.n
);
This page took 0.060377 seconds and 5 git commands to generate.