anjana_sreekumar@infosys.com | 991c206 | 2020-01-08 11:42:57 +0530 | [diff] [blame^] | 1 | |
| 2 | #include "f8.h" |
| 3 | #include <stdio.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <string.h> |
| 6 | |
| 7 | |
| 8 | |
| 9 | /* The code has been referred from |
| 10 | * |
| 11 | * 1.https://www.gsma.com/aboutus/wp-content/uploads/2014/12/uea2uia2d1v21.pdf |
| 12 | * 2.https://www.gsma.com/aboutus/wp-content/uploads/2014/12/snow3gspec.pdf |
| 13 | * |
| 14 | */ |
| 15 | |
| 16 | |
| 17 | |
| 18 | /* f8. |
| 19 | * Input key: 128 bit Confidentiality Key. |
| 20 | * Input count:32-bit Count, Frame dependent input. |
| 21 | * Input bearer: 5-bit Bearer identity (in the LSB side). |
| 22 | * Input dir:1 bit, direction of transmission. |
| 23 | * Input data: length number of bits, input bit stream. |
| 24 | * Input length: 32 bit Length, i.e., the number of bits to be encrypted or |
| 25 | * decrypted. |
| 26 | * Output data: Output bit stream. Assumes data is suitably memory |
| 27 | * allocated. |
| 28 | * Encrypts/decrypts blocks of data between 1 and 2^32 bits in length as |
| 29 | * defined in Section 3. |
| 30 | */ |
| 31 | |
| 32 | void f8( u8 *key, u32 count, u32 bearer, u32 dir, u8 *data, u32 length ) |
| 33 | { |
| 34 | u32 K[4],IV[4]; |
| 35 | int n = ( length + 31 ) / 32; |
| 36 | int i=0; |
| 37 | u32 *KS; |
| 38 | |
| 39 | /*Initialisation*/ |
| 40 | |
| 41 | /* Load the confidentiality key for SNOW 3G initialization as in section |
| 42 | 3.4.(https://www.gsma.com/aboutus/wp-content/uploads/2014/12/snow3gspec.pdf) |
| 43 | */ |
| 44 | |
| 45 | for (i=0; i<4; i++) |
| 46 | K[3-i] = (key[4*i] << 24) ^ (key[4*i+1] << 16) ^ (key[4*i+2] << 8) ^ |
| 47 | (key[4*i+3]); |
| 48 | |
| 49 | /* Prepare the initialization vector (IV) for SNOW 3G initialization as in |
| 50 | section 3.4.(https://www.gsma.com/aboutus/wp-content/uploads/2014/12/snow3gspec.pdf) */ |
| 51 | |
| 52 | |
| 53 | IV[3] = count; |
| 54 | IV[2] = (bearer << 27) | ((dir & 0x1) << 26); |
| 55 | IV[1] = IV[3]; |
| 56 | IV[0] = IV[2]; |
| 57 | |
| 58 | /* Run SNOW 3G algorithm to generate sequence of key stream bits KS*/ |
| 59 | |
| 60 | Initialize(K,IV); |
| 61 | KS = (u32 *)malloc(4*n); |
| 62 | GenerateKeystream(n,(u32*)KS); |
| 63 | |
| 64 | /* Exclusive-OR the input data with keystream to generate the output bit |
| 65 | stream */ |
| 66 | |
| 67 | for (i=0; i<n; i++) |
| 68 | { |
| 69 | data[4*i+0] ^= (u8) (KS[i] >> 24) & 0xff; |
| 70 | data[4*i+1] ^= (u8) (KS[i] >> 16) & 0xff; |
| 71 | data[4*i+2] ^= (u8) (KS[i] >> 8) & 0xff; |
| 72 | data[4*i+3] ^= (u8) (KS[i] ) & 0xff; |
| 73 | } |
| 74 | |
| 75 | free(KS); |
| 76 | |
| 77 | } |
| 78 | /* End of f8.c */ |
| 79 | |
| 80 | |