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 : 34 : /* 35 : * Implementation of the Red Pike block cipher algorithm 36 : */ 37 : #include <cstdint> 38 : #include <openGPMP/nt/redpike.hpp> 39 : 40 0 : uint32_t gpmp::nt::RedPike::rotl(uint32_t X, int R) { 41 0 : return ((X) << ((R)&31)) | ((X) >> (32 - ((R)&31))); 42 : } 43 : 44 0 : uint32_t gpmp::nt::RedPike::rotr(uint32_t X, int R) { 45 0 : return ((X) >> ((R)&31)) | ((X) << (32 - ((R)&31))); 46 : } 47 : 48 0 : void gpmp::nt::RedPike::encrypt(uint32_t *x, const uint32_t *k) { 49 0 : uint32_t rk0 = k[0]; 50 0 : uint32_t rk1 = k[1]; 51 : 52 0 : for (int i = 0; i < ROUNDS; i++) { 53 0 : rk0 += CONST; 54 0 : rk1 -= CONST; 55 : 56 0 : x[0] ^= rk0; 57 0 : x[0] += x[1]; 58 0 : x[0] = rotl(x[0], x[1]); 59 : 60 0 : x[1] = rotr(x[1], x[0]); 61 0 : x[1] -= x[0]; 62 0 : x[1] ^= rk1; 63 : } 64 : 65 0 : rk0 = x[0]; 66 0 : x[0] = x[1]; 67 0 : x[1] = rk0; 68 0 : } 69 : 70 0 : void gpmp::nt::RedPike::decrypt(uint32_t *x, const uint32_t *k) { 71 0 : uint32_t dk[2] = {k[1] - CONST * (ROUNDS + 1), k[0] + CONST * (ROUNDS + 1)}; 72 : 73 0 : encrypt(x, dk); 74 0 : }