internal-pyjamask.c 8.74 KB
Newer Older
Rhys Weatherley committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
/*
 * Copyright (C) 2020 Southern Storm Software, Pty Ltd.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include "internal-pyjamask.h"
#include "internal-util.h"

/**
 * \brief Performs a circulant binary matrix multiplication.
 *
 * \param x The matrix.
 * \param y The vector to multiply with the matrix.
 *
 * \return The vector result of multiplying x by y.
 */
STATIC_INLINE uint32_t pyjamask_matrix_multiply(uint32_t x, uint32_t y)
{
    uint32_t result = 0;
    int bit;
    for (bit = 31; bit >= 0; --bit) {
#if defined(ESP32)
        /* This version has slightly better performance on ESP32 */
        y = leftRotate1(y);
        result ^= x & -(y & 1);
        x = rightRotate1(x);
#else
        result ^= x & -((y >> bit) & 1);
        x = rightRotate1(x);
#endif
    }
    return result;
}

void pyjamask_setup_key(pyjamask_key_schedule_t *ks, const unsigned char *key)
{
    uint32_t *rk = ks->k;
    uint32_t k0, k1, k2, k3;
    uint32_t temp;
    uint8_t round;

    /* Load the words of the key */
    k0 = be_load_word32(key);
    k1 = be_load_word32(key + 4);
    k2 = be_load_word32(key + 8);
    k3 = be_load_word32(key + 12);

    /* The first round key is the same as the key itself */
    rk[0] = k0;
    rk[1] = k1;
    rk[2] = k2;
    rk[3] = k3;
    rk += 4;

    /* Derive the round keys for all of the other rounds */
    for (round = 0; round < PYJAMASK_ROUNDS; ++round, rk += 4) {
        /* Mix the columns */
        temp = k0 ^ k1 ^ k2 ^ k3;
        k0 ^= temp;
        k1 ^= temp;
        k2 ^= temp;
        k3 ^= temp;

        /* Mix the rows and add the round constants.  Note that the Pyjamask
         * specification says that k1/k2/k3 should be rotated left by 8, 15,
         * and 18 bits.  But the reference code actually rotates the words
         * right.  And the test vectors in the specification match up with
         * right rotations, not left.  We match the reference code here */
        k0 = pyjamask_matrix_multiply(0xb881b9caU, k0) ^ 0x00000080U ^ round;
        k1 = rightRotate8(k1)  ^ 0x00006a00U;
        k2 = rightRotate15(k2) ^ 0x003f0000U;
        k3 = rightRotate18(k3) ^ 0x24000000U;

        /* Write the round key to the schedule */
        rk[0] = k0;
        rk[1] = k1;
        rk[2] = k2;
        rk[3] = k3;
    }
}

void pyjamask_128_encrypt
    (const pyjamask_key_schedule_t *ks, unsigned char *output,
     const unsigned char *input)
{
    const uint32_t *rk = ks->k;
    uint32_t s0, s1, s2, s3;
    uint8_t round;

    /* Load the plaintext from the input buffer */
    s0 = be_load_word32(input);
    s1 = be_load_word32(input + 4);
    s2 = be_load_word32(input + 8);
    s3 = be_load_word32(input + 12);

    /* Perform all encryption rounds */
    for (round = 0; round < PYJAMASK_ROUNDS; ++round, rk += 4) {
        /* Add the round key to the state */
        s0 ^= rk[0];
        s1 ^= rk[1];
        s2 ^= rk[2];
        s3 ^= rk[3];

        /* Apply the 128-bit Pyjamask sbox */
        s0 ^= s3;
        s3 ^= s0 & s1;
        s0 ^= s1 & s2;
        s1 ^= s2 & s3;
        s2 ^= s0 & s3;
        s2 ^= s1;
        s1 ^= s0;
        s3 = ~s3;
        s2 ^= s3;
        s3 ^= s2;
        s2 ^= s3;

        /* Mix the rows of the state */
        s0 = pyjamask_matrix_multiply(0xa3861085U, s0);
        s1 = pyjamask_matrix_multiply(0x63417021U, s1);
        s2 = pyjamask_matrix_multiply(0x692cf280U, s2);
        s3 = pyjamask_matrix_multiply(0x48a54813U, s3);
    }

    /* Mix in the key one last time */
    s0 ^= rk[0];
    s1 ^= rk[1];
    s2 ^= rk[2];
    s3 ^= rk[3];

    /* Write the ciphertext to the output buffer */
    be_store_word32(output,      s0);
    be_store_word32(output + 4,  s1);
    be_store_word32(output + 8,  s2);
    be_store_word32(output + 12, s3);
}

void pyjamask_128_decrypt
    (const pyjamask_key_schedule_t *ks, unsigned char *output,
     const unsigned char *input)
{
    const uint32_t *rk = ks->k + 4 * PYJAMASK_ROUNDS;
    uint32_t s0, s1, s2, s3;
    uint8_t round;

    /* Load the ciphertext from the input buffer */
    s0 = be_load_word32(input);
    s1 = be_load_word32(input + 4);
    s2 = be_load_word32(input + 8);
    s3 = be_load_word32(input + 12);

    /* Mix in the last round key */
    s0 ^= rk[0];
    s1 ^= rk[1];
    s2 ^= rk[2];
    s3 ^= rk[3];
    rk -= 4;

    /* Perform all decryption rounds */
    for (round = 0; round < PYJAMASK_ROUNDS; ++round, rk -= 4) {
        /* Inverse mix of the rows in the state */
        s0 = pyjamask_matrix_multiply(0x2037a121U, s0);
        s1 = pyjamask_matrix_multiply(0x108ff2a0U, s1);
        s2 = pyjamask_matrix_multiply(0x9054d8c0U, s2);
        s3 = pyjamask_matrix_multiply(0x3354b117U, s3);

        /* Apply the inverse of the 128-bit Pyjamask sbox */
        s2 ^= s3;
        s3 ^= s2;
        s2 ^= s3;
        s3 = ~s3;
        s1 ^= s0;
        s2 ^= s1;
        s2 ^= s0 & s3;
        s1 ^= s2 & s3;
        s0 ^= s1 & s2;
        s3 ^= s0 & s1;
        s0 ^= s3;

        /* Add the round key to the state */
        s0 ^= rk[0];
        s1 ^= rk[1];
        s2 ^= rk[2];
        s3 ^= rk[3];
    }

    /* Write the plaintext to the output buffer */
    be_store_word32(output,      s0);
    be_store_word32(output + 4,  s1);
    be_store_word32(output + 8,  s2);
    be_store_word32(output + 12, s3);
}

void pyjamask_96_encrypt
    (const pyjamask_key_schedule_t *ks, unsigned char *output,
     const unsigned char *input)
{
    const uint32_t *rk = ks->k;
    uint32_t s0, s1, s2;
    uint8_t round;

    /* Load the plaintext from the input buffer */
    s0 = be_load_word32(input);
    s1 = be_load_word32(input + 4);
    s2 = be_load_word32(input + 8);

    /* Perform all encryption rounds */
    for (round = 0; round < PYJAMASK_ROUNDS; ++round, rk += 4) {
        /* Add the round key to the state */
        s0 ^= rk[0];
        s1 ^= rk[1];
        s2 ^= rk[2];

        /* Apply the 96-bit Pyjamask sbox */
        s0 ^= s1;
        s1 ^= s2;
        s2 ^= s0 & s1;
        s0 ^= s1 & s2;
        s1 ^= s0 & s2;
        s2 ^= s0;
        s2 = ~s2;
        s1 ^= s0;
        s0 ^= s1;

        /* Mix the rows of the state */
        s0 = pyjamask_matrix_multiply(0xa3861085U, s0);
        s1 = pyjamask_matrix_multiply(0x63417021U, s1);
        s2 = pyjamask_matrix_multiply(0x692cf280U, s2);
    }

    /* Mix in the key one last time */
    s0 ^= rk[0];
    s1 ^= rk[1];
    s2 ^= rk[2];

    /* Write the ciphertext to the output buffer */
    be_store_word32(output,      s0);
    be_store_word32(output + 4,  s1);
    be_store_word32(output + 8,  s2);
}

void pyjamask_96_decrypt
    (const pyjamask_key_schedule_t *ks, unsigned char *output,
     const unsigned char *input)
{
    const uint32_t *rk = ks->k + 4 * PYJAMASK_ROUNDS;
    uint32_t s0, s1, s2;
    uint8_t round;

    /* Load the plaintext from the input buffer */
    s0 = be_load_word32(input);
    s1 = be_load_word32(input + 4);
    s2 = be_load_word32(input + 8);

    /* Mix in the last round key */
    s0 ^= rk[0];
    s1 ^= rk[1];
    s2 ^= rk[2];
    rk -= 4;

    /* Perform all encryption rounds */
    for (round = 0; round < PYJAMASK_ROUNDS; ++round, rk -= 4) {
        /* Inverse mix of the rows in the state */
        s0 = pyjamask_matrix_multiply(0x2037a121U, s0);
        s1 = pyjamask_matrix_multiply(0x108ff2a0U, s1);
        s2 = pyjamask_matrix_multiply(0x9054d8c0U, s2);

        /* Apply the inverse of the 96-bit Pyjamask sbox */
        s0 ^= s1;
        s1 ^= s0;
        s2 = ~s2;
        s2 ^= s0;
        s1 ^= s0 & s2;
        s0 ^= s1 & s2;
        s2 ^= s0 & s1;
        s1 ^= s2;
        s0 ^= s1;

        /* Add the round key to the state */
        s0 ^= rk[0];
        s1 ^= rk[1];
        s2 ^= rk[2];
    }

    /* Write the ciphertext to the output buffer */
    be_store_word32(output,      s0);
    be_store_word32(output + 4,  s1);
    be_store_word32(output + 8,  s2);
}