PKA Modular Exponentiation not working as expected
Hello,
I'm trying to figure out how to properly use STM32WB55 PKA registers for RSA key generation and encryption/decryption. I have chosen small (63 bit) primes for generating RSA private key, which worked perfectly. However, I am having trouble with encryption. As far as I understand, simple encryption/decryption for RSA is just modular exponentiation using message as a base, public exponent for encryption, private for decryption and a modulus. However, I have wrong result for encryption and can't figure out what I do wrong.
Here is pseudocode, how I set up the process:
uint32_t message [] = [0x656C6C6F, 0xDD810048, 0x6D1F8EDA, 0x12327939];
uint32_t public_exp [] = [0x00010001, 0, 0, 0]; // 65537
uint32_t modulus [] = [0x1b8abb91, 0xe2c25d78, 0x5f41a975, 0x90275365]; // Calculated from primes
PKA->enable();
PKA->set_mode(MontgomeryParameterComputationThenModularExponentiation);
PKA->ram(0x400).write(32); // Exponent length, tried changing it already, many times
PKA->ram(0x404).write(32*4); // Operand and modulus length in bits
PKA->ram(0xA44).write(message);
// Write empty word after data
PKA->ram(0xBD0).write(public_exp);
// Write empty word after data
PKA->ram(0xD5C).write(modulus);
// Write empty word after data
PKA->start();
// Wait for finish
...
uint32_t buff[4];
PKA->ram(0x724).read(buff);
However, the result I get is wrong. I am getting an array of values:
[0x128b8968, 0xb14ac363, 0xf4179c8f, 0x149e3d10
Which can be translated to value 0x149e3d10f4179c8fb14ac363128b8968. Which is wrong. I manually calculated to correct result of such computation (and also used internet provided calculators as well as created simple python script to prove it) and it should be:
0xa1faaa802c5ec9bc3b254a9bf58c2a9
Also, using private exponent to decrypt given ciphertext doesn't give me original message, so clearly I'm doing something wrong, but just can't figure it out.
