1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef LOCATION_LBS_CONTEXTHUB_NANOAPPS_NEARBY_CRYPTO_AES_H_ 18 #define LOCATION_LBS_CONTEXTHUB_NANOAPPS_NEARBY_CRYPTO_AES_H_ 19 20 #ifdef __cplusplus 21 extern "C" { 22 #endif 23 24 /** 25 * Supported AES mode: 26 * - AES/CTR with 128/256-bit key 27 * 28 * External APIs: 29 * - aesCtrInit() for AES/CTR initialization 30 * - aesCtr() for AES/CTR encryption and decryption 31 */ 32 33 #include <stddef.h> 34 #include <stdint.h> 35 36 #define AES_128_KEY_WORDS 4 37 #define AES_192_KEY_WORDS 6 38 #define AES_256_KEY_WORDS 8 39 #define AES_KEY_MAX_WORDS AES_256_KEY_WORDS 40 #define AES_BLOCK_WORDS 4 41 #define AES_BLOCK_SIZE 16 // in bytes 42 // AES key type 43 enum AesKeyType { AES_128_KEY_TYPE, AES_192_KEY_TYPE, AES_256_KEY_TYPE }; 44 45 // basic AES context 46 struct AesContext { 47 uint32_t round_key[AES_KEY_MAX_WORDS * 4 + 28]; 48 uint32_t aes_key_words; 49 uint32_t aes_num_rounds; 50 }; 51 52 // basic AES block ops 53 int aesInitForEncr(struct AesContext *ctx, const uint32_t *k); 54 void aesEncr(struct AesContext *ctx, const uint32_t *src, uint32_t *dst); 55 56 // AES-CTR context 57 struct AesCtrContext { 58 struct AesContext aes; 59 uint32_t iv[AES_BLOCK_WORDS]; 60 }; 61 62 /** 63 * aesCtrInit: 64 * @ctx: AES/CTR context 65 * @k: AES encryption/decryption key. 66 * the size must match with the key type. 67 * @iv: AES/CTR 16 byte counter block. 68 * the size must fit exactly 16 bytes. 69 * @key_type: AES encryption/decryption key type. 70 * it must be either AES_128_KEY_TYPE or AES_256_KEY_TYPE. 71 * 72 * Initialize AES/CTR by 73 * creating round keys and copying the counter blocks 74 * 75 * Returns: 0 if key_type is valid otherwise -1 76 */ 77 int aesCtrInit(struct AesCtrContext *ctx, const void *k, const void *iv, 78 enum AesKeyType key_type); 79 80 /** 81 * aesCtr: 82 * @ctx: AES/CTR context initialized 83 * @src: source. plain text for encryption or cipher text for decryption. 84 * the size must be same as the size of destination. 85 * @dst: destination. cipher text for encryption or plain text for decryption. 86 * the size must be same as the size of source. 87 * @data_len: number of bytes of the source or destination byte array 88 * 89 * Encrypt/Decrypt by AES/CTR mode 90 * 91 * Returns: 92 */ 93 void aesCtr(struct AesCtrContext *ctx, const void *src, void *dst, 94 size_t data_len); 95 96 #ifdef __cplusplus 97 } 98 #endif 99 #endif // LOCATION_LBS_CONTEXTHUB_NANOAPPS_NEARBY_CRYPTO_AES_H_ 100