Branch data Line data Source code
1 : : /* 2 : : Copyright (c) 2021 Fraunhofer AISEC. See the COPYRIGHT 3 : : file at the top-level directory of this distribution. 4 : : 5 : : Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or 6 : : http://www.apache.org/licenses/LICENSE-2.0> or the MIT license 7 : : <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your 8 : : option. This file may not be copied, modified, or distributed 9 : : except according to those terms. 10 : : */ 11 : : 12 : : #include <stdio.h> 13 : : #include <string.h> 14 : : 15 : : #include "oscore/nonce.h" 16 : : #include "oscore/security_context.h" 17 : : 18 : : #include "common/print_util.h" 19 : : #include "common/memcpy_s.h" 20 : : 21 : 24 : enum err create_nonce(struct byte_array *id_piv, struct byte_array *piv, 22 : : struct byte_array *common_iv, struct byte_array *nonce) 23 : : { 24 : : /* "1. left-padding the PIV in network byte order with zeroes to exactly 5 bytes"*/ 25 : 24 : uint8_t padded_piv[MAX_PIV_LEN] = { 0 }; 26 [ - + ]: 24 : TRY(_memcpy_s(&padded_piv[sizeof(padded_piv) - piv->len], piv->len, 27 : : piv->ptr, piv->len)); 28 : : 29 : : /* "2. left-padding the ID_PIV in network byte order with zeroes to exactly nonce length minus 6 bytes," */ 30 : : 31 : 24 : uint8_t padded_id_piv[NONCE_LEN - MAX_PIV_LEN - 1] = { 0 }; 32 : 24 : const uint8_t padded_id_piv_len = sizeof(padded_id_piv); 33 [ - + ]: 24 : TRY(_memcpy_s(&padded_id_piv[sizeof(padded_id_piv) - id_piv->len], 34 : : id_piv->len, id_piv->ptr, id_piv->len)); 35 : : 36 : : /* "3. concatenating the size of the ID_PIV (a single byte S) with the padded ID_PIV and the padded PIV,"*/ 37 : 24 : nonce->ptr[0] = (uint8_t)id_piv->len; 38 [ - + ]: 24 : TRY(_memcpy_s(&nonce->ptr[1], padded_id_piv_len, padded_id_piv, 39 : : padded_id_piv_len)); 40 : : 41 [ - + ]: 24 : TRY(_memcpy_s(&nonce->ptr[1 + sizeof(padded_id_piv)], 42 : : sizeof(padded_piv), padded_piv, sizeof(padded_piv))); 43 : : 44 : 24 : PRINT_ARRAY("nonce input A", nonce->ptr, nonce->len); 45 : 24 : PRINT_ARRAY("nonce input B", common_iv->ptr, common_iv->len); 46 : : /* "4. and then XORing with the Common IV."*/ 47 [ + + ]: 336 : for (uint32_t i = 0; i < common_iv->len; i++) { 48 : 312 : nonce->ptr[i] ^= common_iv->ptr[i]; 49 : : } 50 : : 51 : 24 : PRINT_ARRAY("nonce", nonce->ptr, nonce->len); 52 : 24 : return ok; 53 : : }