encrypt.c 3.33 KB
Newer Older
lwc-tester 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
/*
 *  Copyright 2019 Mitsubishi Electric Corporation. All Rights Reserved.
 *
 *  SAEAES
 *
 *  version 1.0.0
 *  February 2019
 */

#include "saeaes.h"

/*
 *  Hash
 */
static void hash (
          unsigned char      *s,
    const unsigned char      *ad,
          unsigned long long adlen,
    const unsigned char      *npub,
    const unsigned long      *ekey
)
{
    unsigned long long i;

    for( i=0; i<AES_BLOCK; i++ ) s[i] = 0;

    while( adlen > SAEAES_R1 ) {
        for( i=0; i<SAEAES_R1; i++ ) s[i] ^= ad[i];

        AesEnc( s, ekey );

        ad    += SAEAES_R1;
        adlen -= SAEAES_R1;
    }

    for( i=0; i<adlen; i++ ) s[i] ^= ad[i];

    if( adlen==SAEAES_R1 ) {
        s[AES_BLOCK-1] ^= 1;
    }
    else {
        s[adlen] ^= 0x80;
        s[AES_BLOCK-1] ^= 2;
    }

    AesEnc( s, ekey );

    for( i=0; i<CRYPTO_NPUBBYTES; i++ ) s[i] ^= npub[i];
    s[AES_BLOCK-1] ^= 3;
}


/*
 *  Encryption
 */
int crypto_aead_encrypt (
          unsigned char      *c,
          unsigned long long *clen,
    const unsigned char      *m,
          unsigned long long mlen,
    const unsigned char      *ad,
          unsigned long long adlen,
    const unsigned char      *nsec,
    const unsigned char      *npub,
    const unsigned char      *k
)
{
    unsigned long ekey[AES_EKEY];
    unsigned char s[AES_BLOCK];
    unsigned long long i;

    *clen = mlen + CRYPTO_ABYTES;

    AesKey( k, ekey );
    hash  ( s, ad, adlen, npub, ekey );
    AesEnc( s, ekey );

    while( mlen > SAEAES_R ) {
        for( i=0; i<SAEAES_R; i++ ) {
            s[i] ^= m[i];
            c[i]  = s[i];
        }

        AesEnc( s, ekey );

        m    += SAEAES_R;
        c    += SAEAES_R;
        mlen -= SAEAES_R;
    }

    for( i=0; i<mlen; i++ ) {
        s[i] ^= m[i];
        c[i]  = s[i];
    }
    c += mlen;

    if( mlen==SAEAES_R ) {
        s[AES_BLOCK-1] ^= 1;
    }
    else {
        s[mlen] ^= 0x80;
        s[AES_BLOCK-1] ^= 2;
    }

    AesEnc( s, ekey );

    for( i=0; i<CRYPTO_ABYTES; i++ ) c[i] = s[i];

    return 0;
}


/*
 *  Decryption
 */
int crypto_aead_decrypt (
          unsigned char      *m,
          unsigned long long *mlen,
          unsigned char      *nsec,
    const unsigned char      *c,
          unsigned long long clen,
    const unsigned char      *ad,
          unsigned long long adlen,
    const unsigned char      *npub,
    const unsigned char      *k
)
{
    unsigned long ekey[AES_EKEY];
    unsigned char s[AES_BLOCK];
    unsigned long long i;

    if( clen < CRYPTO_ABYTES ) return -2;

    clen -= CRYPTO_ABYTES;
    *mlen = clen;

    AesKey( k, ekey );
    hash  ( s, ad, adlen, npub, ekey );
    AesEnc( s, ekey );

    while( clen > SAEAES_R ) {
        for( i=0; i<SAEAES_R; i++ ) {
            m[i]  = s[i] ^ c[i];
            s[i] ^= m[i];
        }

        AesEnc( s, ekey );

        m    += SAEAES_R;
        c    += SAEAES_R;
        clen -= SAEAES_R;
    }

    for( i=0; i<clen; i++ ) {
        m[i]  = s[i] ^ c[i];
        s[i] ^= m[i];
    }
    m += clen;
    c += clen;

    if( clen==SAEAES_R ) {
        s[AES_BLOCK-1] ^= 1;
    }
    else {
        s[clen] ^= 0x80;
        s[AES_BLOCK-1] ^= 2;
    }

    AesEnc( s, ekey );

    for( i=0; i<CRYPTO_ABYTES; i++ ) {
        if( c[i] != s[i] ) {
            m -= *mlen;
            for( i=0; i<*mlen; i++ ) m[i] = 0;
            return -1;
        }
    }

    return 0;
}