| 153 | ////////////////////////////////////// |
| 154 | |
| 155 | void SRP6A::createSessionKey(const uint8_t *publicKey, size_t len){ |
| 156 | |
| 157 | TempBuffer<uint8_t> tBuf(768); // temporary buffer for staging |
| 158 | TempBuffer<uint8_t> tHash(64); // temporary buffer for storing SHA-512 results |
| 159 | |
| 160 | mbedtls_mpi_read_binary(&A,publicKey,len); // load client PublicKey into A |
| 161 | |
| 162 | // compute u = SHA512( PAD(A) | PAD(B) ) |
| 163 | |
| 164 | mbedtls_mpi_write_binary(&A,tBuf,384); // write A into first half of staging buffer (padding with initial zeros is less than 384 bytes) |
| 165 | mbedtls_mpi_write_binary(&B,tBuf+384,384); // write B into second half of staging buffer (padding with initial zeros is less than 384 bytes) |
| 166 | mbedtls_sha512(tBuf,768,tHash,0); // create hash of data |
| 167 | mbedtls_mpi_read_binary(&u,tHash,64); // load hash result into mpi structure u |
| 168 | |
| 169 | // compute S = (A * v^u)^b %N |
| 170 | |
| 171 | mbedtls_mpi_exp_mod(&t1,&v,&u,&N,&_rr); // t1 = v^u %N |
| 172 | mbedtls_mpi_mul_mpi(&t2,&A,&t1); // t2 = A*t1 |
| 173 | mbedtls_mpi_mod_mpi(&t1,&t2,&N); // t1 = t2 %N (this is needed to reduce size of t2 before next calculation) |
| 174 | mbedtls_mpi_exp_mod(&S,&t1,&b,&N,&_rr); // S = t1^b %N |
| 175 | |
| 176 | // compute K = SHA512( PAD(S) ) |
| 177 | |
| 178 | mbedtls_mpi_write_binary(&S,tBuf,384); // write S into staging buffer (only first half of buffer will be used) |
| 179 | mbedtls_sha512(tBuf,384,K,0); // create hash of data - this is the SRP SHARED SESSION KEY, K |
| 180 | |
| 181 | } |
| 182 | |
| 183 | ////////////////////////////////////// |
| 184 | |