| 64 | } |
| 65 | |
| 66 | int JLSRTP::pseudorandomFunction(std::vector<unsigned char> iv, int n, std::vector<unsigned char> &output) |
| 67 | { |
| 68 | int rc = 0; |
| 69 | unsigned int num_loops = 0; |
| 70 | std::vector<unsigned char> block; |
| 71 | std::vector<unsigned char> input; |
| 72 | unsigned int ivSize = 0; |
| 73 | unsigned int keySize = 0; |
| 74 | int retVal = 0; |
| 75 | |
| 76 | switch (_active_crypto) |
| 77 | { |
| 78 | case PRIMARY_CRYPTO: |
| 79 | { |
| 80 | ivSize = iv.size(); |
| 81 | keySize = _primary_crypto.master_key.size(); |
| 82 | |
| 83 | assert(ivSize == JLSRTP_SALTING_KEY_LENGTH); |
| 84 | assert(keySize == JLSRTP_ENCRYPTION_KEY_LENGTH); |
| 85 | if (ivSize == JLSRTP_SALTING_KEY_LENGTH) |
| 86 | { |
| 87 | if (keySize == JLSRTP_ENCRYPTION_KEY_LENGTH) |
| 88 | { |
| 89 | input.resize(AES_BLOCK_SIZE); |
| 90 | output.clear(); |
| 91 | |
| 92 | // Determine how many AES_BLOCK_SIZE-byte encryption loops will be necessary to achieve at least n/8 bytes of pseudorandom ciphertext |
| 93 | num_loops = (n % JLSRTP_PSEUDORANDOM_BITS) ? ((n / JLSRTP_PSEUDORANDOM_BITS) + 1) : (n / JLSRTP_PSEUDORANDOM_BITS); |
| 94 | |
| 95 | // Set encryption key |
| 96 | rc = setAESPseudoRandomFunctionKey(_active_crypto); |
| 97 | if (rc == 0) |
| 98 | { |
| 99 | // Reset IV/counter state |
| 100 | resetPseudoRandomState(iv); |
| 101 | |
| 102 | for (unsigned int i = 0; i < num_loops; i++) |
| 103 | { |
| 104 | // Encrypt given _pseudorandomstate.ivec input using aes_key to block |
| 105 | block.clear(); |
| 106 | block.resize(AES_BLOCK_SIZE); |
| 107 | AES_ctr128_pseudorandom_EVPencrypt(input.data(), block.data(), AES_BLOCK_SIZE, _pseudorandomstate.ivec, _pseudorandomstate.ecount, &_pseudorandomstate.num); |
| 108 | output.insert(output.end(), block.begin(), block.end()); |
| 109 | } |
| 110 | |
| 111 | // Truncate output to n/8 bytes |
| 112 | output.resize(n / 8); |
| 113 | |
| 114 | retVal = 0; |
| 115 | } |
| 116 | else |
| 117 | { |
| 118 | retVal = -3; |
| 119 | } |
| 120 | } |
| 121 | else |
| 122 | { |
| 123 | retVal = -2; |