Line data Source code
1 : /************************************************************************* 2 : * 3 : * Project 4 : * _____ _____ __ __ _____ 5 : * / ____| __ \| \/ | __ \ 6 : * ___ _ __ ___ _ __ | | __| |__) | \ / | |__) | 7 : * / _ \| '_ \ / _ \ '_ \| | |_ | ___/| |\/| | ___/ 8 : *| (_) | |_) | __/ | | | |__| | | | | | | | 9 : * \___/| .__/ \___|_| |_|\_____|_| |_| |_|_| 10 : * | | 11 : * |_| 12 : * 13 : * Copyright (C) Akiel Aries, <akiel@akiel.org>, et al. 14 : * 15 : * This software is licensed as described in the file LICENSE, which 16 : * you should have received as part of this distribution. The terms 17 : * among other details are referenced in the official documentation 18 : * seen here : https://akielaries.github.io/openGPMP/ along with 19 : * important files seen in this project. 20 : * 21 : * You may opt to use, copy, modify, merge, publish, distribute 22 : * and/or sell copies of the Software, and permit persons to whom 23 : * the Software is furnished to do so, under the terms of the 24 : * LICENSE file. As this is an Open Source effort, all implementations 25 : * must be of the same methodology. 26 : * 27 : * 28 : * 29 : * This software is distributed on an AS IS basis, WITHOUT 30 : * WARRANTY OF ANY KIND, either express or implied. 31 : * 32 : ************************************************************************/ 33 : #include <cstdint> 34 : #include <openGPMP/nt/rc5.hpp> 35 : 36 0 : gpmp::nt::RC5::RC5(uint32_t rounds, uint32_t wordSize, uint32_t blockSize) 37 0 : : w(wordSize), r(rounds), b(blockSize) { 38 0 : init(); 39 0 : } 40 : 41 0 : void gpmp::nt::RC5::init() { 42 0 : S.resize(2 * r + 2); 43 0 : S[0] = P; 44 0 : for (uint32_t i = 1; i < 2 * r + 2; ++i) { 45 0 : S[i] = S[i - 1] + Q; 46 : } 47 0 : } 48 : 49 0 : void gpmp::nt::RC5::encrypt(uint32_t &A, uint32_t &B) { 50 0 : A += S[0]; 51 0 : B += S[1]; 52 0 : for (uint32_t i = 1; i <= r; ++i) { 53 0 : A = rotl((A ^ B), B) + S[2 * i]; 54 0 : B = rotl((B ^ A), A) + S[2 * i + 1]; 55 : } 56 0 : } 57 : 58 0 : void gpmp::nt::RC5::decrypt(uint32_t &A, uint32_t &B) { 59 0 : for (uint32_t i = r; i > 0; --i) { 60 0 : B = rotr(B - S[2 * i + 1], A) ^ A; 61 0 : A = rotr(A - S[2 * i], B) ^ B; 62 : } 63 0 : B -= S[1]; 64 0 : A -= S[0]; 65 0 : } 66 : 67 0 : uint32_t gpmp::nt::RC5::rotl(uint32_t value, uint32_t shift) { 68 0 : return (value << shift) | (value >> (w - shift)); 69 : } 70 : 71 0 : uint32_t gpmp::nt::RC5::rotr(uint32_t value, uint32_t shift) { 72 0 : return (value >> shift) | (value << (w - shift)); 73 : }