将零填充添加到数组

Ann*_*nne 8 c cryptography aes-gcm

我正在为AES-GCM实现做一个GHASH.

GHASH

我需要实现这一点

等式

其中v是A的最后一个块的位长度,u是C的最后一个块的位长度,|| 表示位串的串联.

如何进行A块的串联以填充从0到128位的零填充,因为我不知道整个A块的长度.所以我只需要A块并将其与一个128的数组进行异或运算.位

void GHASH(uint8_t H[16], uint8_t len_A, uint8_t A_i[len_A], uint8_t len_C,
    uint8_t C_i[len_C], uint8_t X_i[16]) {

uint8_t m;
uint8_t n;
uint8_t i;
uint8_t j;
uint8_t zeros[16] = {0};

 if (i == m + n) {
        for(j=16; j>=0; j--){
        C_i[j] = C_i[j] ^ zeros[j]; //XOR with zero array to fill in 0 of length 128-u
        tmp[j] = X_i[j] ^ C_i[j]; // X[m+n+1] XOR C[i] left shift by (128bit-u) and store into tmp
        gmul(tmp, H, X_i); //Do Multiplication of tmp to H and store into X
        }
    }
Run Code Online (Sandbox Code Playgroud)

我很确定我不对.但我不知道该怎么做.

and*_*oke 3

如果您不关心每一点效率(我认为这是为了实验,而不是真正使用?),只需重新分配和填充(实际上,您可以在第一次声明这些时进行舍入和调用):

size_t round16(size_t n) {
    // if n isn't a multiple of 16, round up to next multiple
    if (n % 16) return 16 * (1 + n / 16);
    return n;
}

size_t realloc16(uint8_t **data, size_t len) {
    // if len isn't a multiple of 16, extend with 0s to next multiple
    size_t n = round16(len);
    *data = realloc(*data, n);
    for (size_t i = len; i < n; ++i) (*data)[i] = 0;
    return n;
}

void xor16(uint8_t *result, uint8_t *a, uint8_t *b) {
    // 16 byte xor
    for (size_t i = 0; i < 16; ++i) result[i] = a[i] ^ b[i];
}

void xorandmult(uint8_t *x, uint8_t *data, size_t n, unint8_t *h) {
    // run along the length of the (extended) data, xoring and mutliplying
    uint8_t tmp[16];
    for (size_t i = 0; i < n / 16; ++i) {
        xor16(tmp, x, data+i*16);
        multgcm(x, h, tmp);
    }
}

void ghash(uint8_t *x, uint8_t **a, size_t len_a, uint8_t **c, size_t len_c, uint8_t *h) {
    size_t m = realloc16(a, len_a);
    xorandmult(x, *a, m, h);
    size_t n = realloc16(c, len_c);
    xorandmult(x, *c, n, h);

    // then handle lengths
}

uint8_t x[16] = {0};
ghash(x, &a, len_a, &c, len_c, h);
Run Code Online (Sandbox Code Playgroud)

免责声明 - 不是专家,只是浏览了规范。代码未经编译、未经检查,且不适合“真正”使用。另外,规范支持任意(位)长度,但我假设您正在以字节为单位工作。

另外,我仍然不确定我是否回答了正确的问题。